diff --git a/lib/analysis.js b/lib/analysis.js index cb851ba..5f36965 100644 --- a/lib/analysis.js +++ b/lib/analysis.js @@ -100,6 +100,7 @@ export async function analyseScenario(scenario, opts = {}) { "500", e.providerApiPath, e.providerExecution, + e.code, ); } @@ -175,7 +176,7 @@ function tryValidateAgainstSchema(data, schema) { // ── Result builders ────────────────────────────────── -function buildErrorResponse(message, elapsed, statusCode = 500, providerApiPath, providerExecution) { +function buildErrorResponse(message, elapsed, statusCode = 500, providerApiPath, providerExecution, code) { return { success: false, error: message, @@ -187,6 +188,7 @@ function buildErrorResponse(message, elapsed, statusCode = 500, providerApiPath, statusCode, providerApiPath, providerExecution, + code, }; } diff --git a/lib/graph/orchestrator.js b/lib/graph/orchestrator.js index cf42b88..86cf066 100644 --- a/lib/graph/orchestrator.js +++ b/lib/graph/orchestrator.js @@ -382,6 +382,7 @@ export async function startCase(body, dependencies = {}) { providerApiPath: analysis.providerApiPath ?? undefined, providerExecution: analysis.providerExecution ?? undefined, rawResponse: analysis.rawResponse ?? undefined, + code: analysis.code ?? undefined, statusCode: Number(analysis.statusCode) || 502, }; } diff --git a/tests/app/api/cases-start-route.test.js b/tests/app/api/cases-start-route.test.js index d36afca..c1f9230 100644 --- a/tests/app/api/cases-start-route.test.js +++ b/tests/app/api/cases-start-route.test.js @@ -1,13 +1,20 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; const mockStartCase = vi.fn(); +let realStartCase; let warnSpy; let errorSpy; -vi.mock("@/lib/graph/orchestrator.js", () => ({ - startCase: (...args) => mockStartCase(...args), +vi.mock("@/lib/config.js", () => ({ + getConfig: () => ({ ok: true }), })); +vi.mock("@/lib/graph/orchestrator.js", async (importOriginal) => { + const actual = await importOriginal(); + realStartCase = actual.startCase; + return { ...actual, startCase: (...args) => mockStartCase(...args) }; +}); + vi.mock("@/lib/supabase/api-auth.js", () => ({ withAuthenticatedApi: (handler) => handler, })); @@ -128,13 +135,21 @@ describe("app/api/cases/start route", () => { }); it("returns a sanitized 503 when the provider is unavailable", async () => { - mockStartCase.mockResolvedValue({ - success: false, - code: "PROVIDER_UNAVAILABLE", - error: "Ollama /api/generate request timed out after 5 minutes", - providerApiPath: "/api/generate", - providerExecution: { generateRequestAttempted: true }, - }); + const unavailableError = Object.assign( + new Error("Ollama /api/generate request timed out after 5 minutes"), + { + code: "PROVIDER_UNAVAILABLE", + providerApiPath: "/api/generate", + providerExecution: { generateRequestAttempted: true }, + }, + ); + const reconstructionProvider = { + generateReconstruction: vi.fn().mockRejectedValue(unavailableError), + }; + mockStartCase.mockImplementation((body) => realStartCase(body, { + reconstructionProvider, + reconstructionModelName: "configured-model", + })); const { POST } = await import("@/app/api/cases/start/route.js"); const response = await POST(