|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { |
| 3 | + extractLeadingHttpStatus, |
| 4 | + extractProviderWrappedHttpStatus, |
| 5 | + formatRawAssistantErrorForUi, |
| 6 | + parseApiErrorInfo, |
| 7 | +} from "./assistant-error-format.js"; |
| 8 | + |
| 9 | +describe("extractLeadingHttpStatus", () => { |
| 10 | + it("accepts status codes in the valid HTTP range 100-599", () => { |
| 11 | + expect(extractLeadingHttpStatus("100 everything is fine")).toEqual({ |
| 12 | + code: 100, |
| 13 | + rest: "everything is fine", |
| 14 | + }); |
| 15 | + expect(extractLeadingHttpStatus("500 internal error")).toEqual({ |
| 16 | + code: 500, |
| 17 | + rest: "internal error", |
| 18 | + }); |
| 19 | + expect(extractLeadingHttpStatus("599 something rest")).toEqual({ |
| 20 | + code: 599, |
| 21 | + rest: "something rest", |
| 22 | + }); |
| 23 | + }); |
| 24 | + |
| 25 | + it("rejects 3-digit sequences outside the HTTP status code range", () => { |
| 26 | + // 000 / 099 / 999 / 600 — the regex would capture these as 3-digit |
| 27 | + // numbers, but they are not valid HTTP statuses and should not be |
| 28 | + // surfaced as "HTTP <code>" in user-visible messages or in retry |
| 29 | + // classification. |
| 30 | + expect(extractLeadingHttpStatus("000 something")).toBeNull(); |
| 31 | + expect(extractLeadingHttpStatus("099 something")).toBeNull(); |
| 32 | + expect(extractLeadingHttpStatus("600 something")).toBeNull(); |
| 33 | + expect(extractLeadingHttpStatus("999 something")).toBeNull(); |
| 34 | + }); |
| 35 | + |
| 36 | + it("rejects strings that do not start with a 3-digit HTTP status", () => { |
| 37 | + expect(extractLeadingHttpStatus("no status here")).toBeNull(); |
| 38 | + expect(extractLeadingHttpStatus("")).toBeNull(); |
| 39 | + }); |
| 40 | +}); |
| 41 | + |
| 42 | +describe("extractProviderWrappedHttpStatus", () => { |
| 43 | + it("accepts provider-wrapped statuses inside the valid HTTP range", () => { |
| 44 | + expect(extractProviderWrappedHttpStatus("OpenAI API error (503): service down")).toEqual({ |
| 45 | + code: 503, |
| 46 | + rest: "service down", |
| 47 | + }); |
| 48 | + expect(extractProviderWrappedHttpStatus("API error (429): rate limited")).toEqual({ |
| 49 | + code: 429, |
| 50 | + rest: "rate limited", |
| 51 | + }); |
| 52 | + }); |
| 53 | + |
| 54 | + it("rejects provider-wrapped statuses outside the valid HTTP range", () => { |
| 55 | + expect(extractProviderWrappedHttpStatus("API error (000): something")).toBeNull(); |
| 56 | + expect(extractProviderWrappedHttpStatus("API error (999): something")).toBeNull(); |
| 57 | + expect(extractProviderWrappedHttpStatus("API error (600): something")).toBeNull(); |
| 58 | + }); |
| 59 | +}); |
| 60 | + |
| 61 | +describe("HTTP status consumers", () => { |
| 62 | + it("formats only status lines inside the HTTP range", () => { |
| 63 | + expect(formatRawAssistantErrorForUi("100 Continue")).toBe("HTTP 100: Continue"); |
| 64 | + expect(formatRawAssistantErrorForUi("599 Provider Error")).toBe("HTTP 599: Provider Error"); |
| 65 | + expect(formatRawAssistantErrorForUi("000 Invalid")).toBe("000 Invalid"); |
| 66 | + expect(formatRawAssistantErrorForUi("600 Invalid")).toBe("600 Invalid"); |
| 67 | + expect(formatRawAssistantErrorForUi("999 Invalid")).toBe("999 Invalid"); |
| 68 | + }); |
| 69 | + |
| 70 | + it("does not attach invalid status prefixes to API payloads", () => { |
| 71 | + const payload = '{"type":"error","error":{"type":"server_error","message":"Provider failed."}}'; |
| 72 | + |
| 73 | + expect(parseApiErrorInfo(`599 ${payload}`)).toMatchObject({ |
| 74 | + httpCode: "599", |
| 75 | + type: "server_error", |
| 76 | + message: "Provider failed.", |
| 77 | + }); |
| 78 | + expect(formatRawAssistantErrorForUi(`599 ${payload}`)).toBe( |
| 79 | + "HTTP 599 server_error: Provider failed.", |
| 80 | + ); |
| 81 | + |
| 82 | + for (const code of ["000", "600", "999"]) { |
| 83 | + expect(parseApiErrorInfo(`${code} ${payload}`)).toBeNull(); |
| 84 | + expect(formatRawAssistantErrorForUi(`${code} ${payload}`)).toBe(`${code} ${payload}`); |
| 85 | + } |
| 86 | + }); |
| 87 | +}); |
0 commit comments