import { describe, expect, it } from "vitest"; import { AnalysisOutputError } from "../domain/analysisErrorCore.js"; import { projectAnalysisError } from "../domain/analysisErrorProjection.js"; import { UnclassifiedProviderFailure } from "../domain/unclassifiedProviderFailure.js"; import { classifyProviderOutcome, type ProviderDialectOutcome, type ProviderOutcomeContext, } from "./classifyProviderOutcome.js"; import { capturedProviderOutput } from "./capturedProviderOutput.js"; import type { ProviderProcessSnapshot } from "./ProviderProcess.js"; const snapshot: ProviderProcessSnapshot = { stdout: { text: "", bytes: 0, observedBytes: 0 }, stderr: { text: "unrecognised diagnostic", bytes: 23, observedBytes: 23 }, exitCode: 3, signal: null, }; const context = ( dialect: ProviderOutcomeContext["dialect"], ): ProviderOutcomeContext => ({ providerId: "example", operation: "inspect_target", capturedOutput: capturedProviderOutput, timeout: { ms: 1000 }, outputLimit: { limits: null, recovery: {} }, dialect, }); /** A dialect that recognises nothing and reports its phase. */ const unrecognised = { ...context((_outcome, _details, unclassified) => unclassified({ diagnostics: { phase: "worker" } }), ), details: { diagnostics: { receipt: "finalization failed" } }, }; describe("an outcome the provider dialect does not recognise", () => { const cases: readonly { readonly name: string; readonly outcome: ProviderDialectOutcome; readonly examined: Record; }[] = [ { name: "an exit", outcome: { kind: "exited", cause: new Error("exited with 3"), message: "exited with 3", output: { snapshot: { ...snapshot, diagnosticTruncated: true, stderr: { ...snapshot.stderr, observedBytes: 4096 }, }, truncated: true, }, exitCode: 3, signal: null, stopRequested: true, }, examined: { failure_kind: "exited", reason: "exited with 3", exit_code: 3, signal: null, stop_requested: true, output_truncated: true, }, }, { name: "a stream failure keeping the recorded status", outcome: { kind: "protocol", cause: new Error("stdout stream failed"), message: "stdout stream failed", output: { snapshot, truncated: false }, }, examined: { failure_kind: "protocol", exit_code: 3, signal: null, output_truncated: false, }, }, ]; it.each(cases)( "reports $name with the examined outcome", ({ outcome, examined }) => { const failure = classifyProviderOutcome(outcome, unrecognised); const diagnostics = { receipt: "finalization failed", phase: "worker", ...examined, }; const captured = { stdout: "", stderr: "unrecognised diagnostic", stdout_bytes: 0, stderr_bytes: outcome.kind === "exited" ? 4096 : 23, exit_code: 3, signal: null, truncated: outcome.kind === "exited", }; expect(failure).toBeInstanceOf(UnclassifiedProviderFailure); expect(failure.cause).toBe(outcome.cause); expect(failure).toMatchObject({ _tag: "ProviderAdapterError", providerId: "example", operation: "inspect_target", examined, diagnostics, capturedOutput: captured, }); expect(projectAnalysisError(failure)).toMatchObject({ code: "execution_failure", details: { diagnostics, captured_output: captured }, }); }, ); it("does not report an outcome the dialect classified", () => { const failure = classifyProviderOutcome( { kind: "exited", cause: new Error("exited with 3"), message: "exited with 3", output: { snapshot, truncated: false }, exitCode: 3, signal: null, stopRequested: false, }, context( (outcome, details) => new AnalysisOutputError("inspect_target", outcome.message, details), ), ); expect(failure).toBeInstanceOf(AnalysisOutputError); expect(failure.diagnostics).toBeUndefined(); }); });