import { expect, it } from "vitest"; import { AnalysisCapabilityUnavailableError } from "../../domain/analysisErrorCore.js"; import { ProviderOwnedAnalysisError, providerErrorTag, type AnalysisErrorOwnPresentation, } from "../../domain/analysisErrorBase.js"; import { err } from "../../domain/result.js"; import { SessionRuntimeAvailability } from "./SessionRuntimeAvailability.js"; const provider = { id: "kuna", name: "Kuna", version: "1" } as const; const inspect = { operation: "list_strings", provider, available: true, } as const; const decode = { operation: "search_strings", provider, available: true, } as const; it.each([undefined, "Restore the fixture runtime."])( "records only durable owner-published remediation for provider-wide errors: %s", (remediation) => { class RuntimeUnavailable extends ProviderOwnedAnalysisError { readonly _tag = providerErrorTag("FixtureRuntimeUnavailable"); override get providerWide(): boolean { return true; } override ownPresentation(): AnalysisErrorOwnPresentation { return { code: "provider_unavailable", category: "unavailable", retry: "same_call", ...(remediation === undefined ? {} : { remediation }), continuing: { remediationAction: "Use the current fixture session." }, }; } } const availability = new SessionRuntimeAvailability(); availability.observe( availability.dispatch(), inspect, [inspect, decode], err(new RuntimeUnavailable("provider stopped")), ); expect(availability.entries.get("list_strings")?.reason).toBe( "FixtureRuntimeUnavailable: provider stopped", ); for (const operation of ["list_strings", "search_strings"]) { const entry = availability.entries.get(operation); expect(entry).toBeDefined(); if (remediation === undefined) expect(entry).not.toHaveProperty("remediation"); else expect(entry?.remediation).toBe("Restore the fixture runtime."); } }, ); it("records the repair a runtime refusal publishes", () => { const availability = new SessionRuntimeAvailability(); const remediation = "Restart Kuna from a host that provides its SDK."; const changed = availability.observe( availability.dispatch(), inspect, [inspect], err( new AnalysisCapabilityUnavailableError( "kuna", "list_strings", "the selected provider is unavailable", { remediationAction: remediation }, ), ), ); expect(changed).toBe(true); expect(availability.entries.get("list_strings")).toEqual({ reason: "Provider kuna cannot execute list_strings: the selected provider is unavailable", remediation, }); }); it.each([ { activeRepair: "repair one", incomingRepair: "repair two" }, { activeRepair: undefined, incomingRepair: "repair two" }, { activeRepair: "repair one", incomingRepair: undefined }, { activeRepair: undefined, incomingRepair: undefined }, ])( "keeps the active failure's repair $activeRepair when a sibling reports $incomingRepair", ({ activeRepair, incomingRepair }) => { const availability = new SessionRuntimeAvailability(); availability.observe( availability.dispatch(), inspect, [inspect, decode], err( new AnalysisCapabilityUnavailableError( "kuna", "list_strings", "first refusal", activeRepair === undefined ? {} : { remediationAction: activeRepair }, ), ), ); availability.observe( availability.dispatch(), decode, [inspect, decode], err( new AnalysisCapabilityUnavailableError( "kuna", "search_strings", "second refusal", { ...(incomingRepair === undefined ? {} : { remediationAction: incomingRepair }), siblingOperations: ["list_strings"], }, ), ), ); // The reason stays the one the active failure published, so its repair must // stay with it instead of being replaced by the incoming sibling report. expect(availability.entries.get("list_strings")).toEqual({ reason: "Provider kuna cannot execute list_strings: first refusal", ...(activeRepair === undefined ? {} : { remediation: activeRepair }), }); }, ); it("keeps the repair out of the recorded reason", () => { const availability = new SessionRuntimeAvailability(); const remediation = "Restart Kuna from a host that provides its SDK."; availability.observe( availability.dispatch(), inspect, [inspect], err( new AnalysisCapabilityUnavailableError( "kuna", "list_strings", "the selected provider is unavailable", { // A provider may fold the repair into the friendly message; the // recorded reason must still not contain it. userMessage: `The selected provider is unavailable. ${remediation}`, remediationAction: remediation, }, ), ), ); const entry = availability.entries.get("list_strings"); expect(entry?.remediation).toBe(remediation); expect(entry?.reason).not.toContain(remediation); });