|
| 1 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import { classifyProviderFailoverSignalWithPlugin } from "../../plugins/provider-failover.js"; |
| 3 | +import { buildApiErrorObservationFields } from "../embedded-agent-error-observation.js"; |
| 4 | +import { classifyFailoverSignal, isContextOverflowError } from "../failover/classify.js"; |
| 5 | +import { PROVIDER_SCHEMA_REJECTION_USER_TEXT } from "../failover/user-copy.js"; |
| 6 | +import { makeAssistantMessageFixture } from "../test-helpers/assistant-message-fixtures.js"; |
| 7 | +import { formatAssistantErrorText } from "./error-text.js"; |
| 8 | +import { classifyProviderRuntimeFailureKind } from "./provider-runtime-failure.js"; |
| 9 | + |
| 10 | +vi.mock("../../plugins/provider-failover.js", () => ({ |
| 11 | + classifyProviderFailoverSignalWithPlugin: vi.fn(), |
| 12 | +})); |
| 13 | + |
| 14 | +describe("assistant diagnostic provider ownership", () => { |
| 15 | + beforeEach(() => { |
| 16 | + vi.mocked(classifyProviderFailoverSignalWithPlugin) |
| 17 | + .mockReset() |
| 18 | + .mockReturnValue("model_not_found"); |
| 19 | + }); |
| 20 | + |
| 21 | + it.each(["provider sent an opaque failure", "prompt reached the tenant maximum"])( |
| 22 | + "renders %j without discovering provider policy", |
| 23 | + (errorMessage) => { |
| 24 | + expect(formatAssistantErrorText(makeAssistantMessageFixture({ errorMessage }))).toBe( |
| 25 | + errorMessage, |
| 26 | + ); |
| 27 | + expect(classifyProviderFailoverSignalWithPlugin).not.toHaveBeenCalled(); |
| 28 | + }, |
| 29 | + ); |
| 30 | + |
| 31 | + it("keeps the prepared owner through schema diagnostics", () => { |
| 32 | + const classifyFailoverReason = vi.fn(() => "format" as const); |
| 33 | + expect( |
| 34 | + formatAssistantErrorText( |
| 35 | + makeAssistantMessageFixture({ errorMessage: "provider rejected this payload" }), |
| 36 | + { providerOwner: { id: "synthetic-owner", classifyFailoverReason } }, |
| 37 | + ), |
| 38 | + ).toBe(PROVIDER_SCHEMA_REJECTION_USER_TEXT); |
| 39 | + expect(classifyFailoverReason).toHaveBeenCalledWith( |
| 40 | + expect.objectContaining({ provider: "synthetic-owner" }), |
| 41 | + ); |
| 42 | + expect(classifyProviderFailoverSignalWithPlugin).not.toHaveBeenCalled(); |
| 43 | + }); |
| 44 | + |
| 45 | + it("keeps the prepared owner through context diagnostics", () => { |
| 46 | + expect( |
| 47 | + formatAssistantErrorText( |
| 48 | + makeAssistantMessageFixture({ errorMessage: "prompt reached the tenant maximum" }), |
| 49 | + { |
| 50 | + providerOwner: { |
| 51 | + id: "synthetic-owner", |
| 52 | + matchesContextOverflowError: () => true, |
| 53 | + }, |
| 54 | + }, |
| 55 | + ), |
| 56 | + ).toContain("Context overflow: prompt too large for the model."); |
| 57 | + expect(classifyProviderFailoverSignalWithPlugin).not.toHaveBeenCalled(); |
| 58 | + }); |
| 59 | + |
| 60 | + it("records diagnostics without discovering provider policy", () => { |
| 61 | + expect( |
| 62 | + buildApiErrorObservationFields("provider sent an opaque failure", { provider: "openai" }), |
| 63 | + ).toMatchObject({ providerRuntimeFailureKind: "unclassified" }); |
| 64 | + expect(classifyProviderFailoverSignalWithPlugin).not.toHaveBeenCalled(); |
| 65 | + }); |
| 66 | + |
| 67 | + it("preserves runtime discovery when no prepared owner is supplied", () => { |
| 68 | + expect( |
| 69 | + classifyFailoverSignal({ provider: "synthetic-owner", message: "opaque runtime failure" }), |
| 70 | + ).toEqual({ kind: "reason", reason: "model_not_found" }); |
| 71 | + expect(classifyProviderRuntimeFailureKind("opaque runtime failure")).toBe("model_not_found"); |
| 72 | + vi.mocked(classifyProviderFailoverSignalWithPlugin).mockReturnValue("context_overflow"); |
| 73 | + expect(isContextOverflowError("prompt reached the tenant maximum")).toBe(true); |
| 74 | + expect(classifyProviderFailoverSignalWithPlugin).toHaveBeenCalledTimes(3); |
| 75 | + }); |
| 76 | +}); |
0 commit comments