import { describe, it, expect } from "vitest"; import { formatStreamError } from "../src/agent/stream-errors.js"; describe("formatStreamError", () => { it("prefixes provider errors with their HTTP status", () => { const err = Object.assign(new Error("Payment Required"), { statusCode: 402 }); expect(formatStreamError(err)).toBe("HTTP 402: Payment Required"); }); it("prefers the human message from a JSON response body", () => { const err = Object.assign(new Error("Bad"), { statusCode: 402, responseBody: JSON.stringify({ error: "You have exceeded your monthly included credits for Inference Providers." }), }); expect(formatStreamError(err)).toBe( "HTTP 402: You have exceeded your monthly included credits for Inference Providers.", ); }); it("reads nested { error: { message } } bodies and the status of a wrapped cause", () => { const cause = Object.assign(new Error("x"), { statusCode: 429, responseBody: JSON.stringify({ error: { message: "Rate limit reached" } }), }); const err = Object.assign(new Error("Rate limit reached"), { cause }); expect(formatStreamError(err)).toBe("HTTP 429: Rate limit reached"); }); it("keeps plain errors unchanged", () => { expect(formatStreamError(new Error("socket hang up"))).toBe("socket hang up"); expect(formatStreamError(undefined)).toBe("Stream error"); }); });