Skip to content

Commit 0a14d67

Browse files
test(mimo): give the openai mock the abort class the error path instanceof-checks
The mimo mock returns a bare default from vi.mock, so openai's APIUserAbortError is absent from it. handleOpenAIRequestError instanceof-checks that class on every request error, and vitest fills an absent export with a throwing placeholder: measured on this spec a rejected create comes back as OpenAI completion error: [vitest] No "APIUserAbortError" export is defined on the "openai" mock. Did you forget to return it from "vi.mock"? so the reason the provider reported is replaced by the mock's own complaint and the abort branch cannot be reached from mimo at all. Export the same stub class base-openai-compatible-provider.spec.ts:20 exports, which is what makes the abort half of that classification reachable through a mimo handler. The three prefix rows asserted only "OpenAI completion error:", which the wrapper satisfies with any reason - including the placeholder above. Each now pins the reason text and the status the wrapper carries for Task.backoffAndAnnounce: 401 and 429 keep their number, and a connection failure keeps having none. Dropping the status from the 429 double fails that row on "expected undefined to be 429", and a wrong reason fails the containment, so neither assertion is free. The prefix is applied twice on this path (openai.ts:425 wraps the rejection, openai.ts:451 wraps the wrapper), so the reason is pinned by containment rather than as the whole message: the doubling is this path's real output and belongs to its own question, not to this row.
1 parent d8a862c commit 0a14d67

1 file changed

Lines changed: 31 additions & 5 deletions

File tree

‎src/api/providers/__tests__/mimo.spec.ts‎

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import { clearAllMocks } from "../../../test-utils/reset"
44
vi.mock("openai", () => {
55
return {
66
__esModule: true,
7+
// The provider normalizes an aborted request out of an APIUserAbortError, so the mock has to
8+
// export the class: without it the module under test destructures undefined and every abort
9+
// row here would throw before it reaches the handler.
10+
APIUserAbortError: class extends Error {},
711
default: vi.fn().mockImplementation(function () {
812
return {
913
chat: {
@@ -39,6 +43,12 @@ import { MimoHandler } from "../mimo"
3943
import { convertToR1Format } from "../../transform/r1-format"
4044
import { sanitizeOpenAiCallId } from "../../../utils/tool-id"
4145

46+
// The wrapped error carries the HTTP status that Task.backoffAndAnnounce uses for its retry
47+
// header. Read it through a shape check rather than widening the error type.
48+
function preservedStatus(error: unknown): number | undefined {
49+
return error instanceof Error && "status" in error ? (error as Error & { status: number }).status : undefined
50+
}
51+
4252
describe("MimoHandler", () => {
4353
let handler: MimoHandler
4454
let mockOptions: ApiHandlerOptions
@@ -891,9 +901,14 @@ describe("MimoHandler", () => {
891901
})
892902

893903
it("should handle API errors with provider prefix", async () => {
894-
mockCreate.mockRejectedValueOnce(new Error("401 Unauthorized"))
904+
mockCreate.mockRejectedValueOnce(Object.assign(new Error("401 Unauthorized"), { status: 401 }))
895905

896-
await expect(handler.completePrompt("Test prompt")).rejects.toThrow("OpenAI completion error:")
906+
const failure = await handler.completePrompt("Test prompt").catch((error: unknown) => error)
907+
// The prefix alone would pass on a wrapper that dropped the reason. Pin the text the SDK
908+
// reported as well, and the status that Task.backoffAndAnnounce reads to pick a retry.
909+
expect(failure).toBeInstanceOf(Error)
910+
expect((failure as Error).message).toContain("OpenAI completion error: 401 Unauthorized")
911+
expect(preservedStatus(failure)).toBe(401)
897912
})
898913

899914
it("should return empty string when choices array is empty", async () => {
@@ -915,13 +930,24 @@ describe("MimoHandler", () => {
915930
it("should propagate network errors with provider prefix", async () => {
916931
mockCreate.mockRejectedValueOnce(new Error("ECONNREFUSED"))
917932

918-
await expect(handler.completePrompt("Test prompt")).rejects.toThrow("OpenAI completion error:")
933+
const failure = await handler.completePrompt("Test prompt").catch((error: unknown) => error)
934+
expect(failure).toBeInstanceOf(Error)
935+
// Containment for the same reason as the row above: the doubled prefix is real output of this
936+
// path and is left as its own question rather than pinned as the expected shape.
937+
expect((failure as Error).message).toContain("OpenAI completion error: ECONNREFUSED")
938+
// A connection failure carries no HTTP status, and the wrapper must not invent one: a
939+
// fabricated status would reach the retry header as a number nobody earned.
940+
expect(preservedStatus(failure)).toBeUndefined()
919941
})
920942

921943
it("should propagate rate limit errors with provider prefix", async () => {
922-
mockCreate.mockRejectedValueOnce(new Error("429 Too Many Requests"))
944+
mockCreate.mockRejectedValueOnce(Object.assign(new Error("429 Too Many Requests"), { status: 429 }))
923945

924-
await expect(handler.completePrompt("Test prompt")).rejects.toThrow("OpenAI completion error:")
946+
const failure = await handler.completePrompt("Test prompt").catch((error: unknown) => error)
947+
expect(failure).toBeInstanceOf(Error)
948+
expect((failure as Error).message).toContain("OpenAI completion error: 429 Too Many Requests")
949+
// 429 is the status that decides the backoff, so it has to survive the wrap.
950+
expect(preservedStatus(failure)).toBe(429)
925951
})
926952

927953
it("should use correct model ID for mimo-v2.5 variant", async () => {

0 commit comments

Comments
 (0)