import { extractErrorMessage } from "../http-error"; function jsonResponse(body: unknown): Response { return new Response(JSON.stringify(body), { status: 400, headers: { "Content-Type": "application/json" }, }); } describe("extractErrorMessage", () => { it("returns the top-level message when no details are supplied", async () => { const r = jsonResponse({ error: { message: "Bad request" } }); expect(await extractErrorMessage(r, "fb")).toBe("Bad request"); }); it("joins details with commas", async () => { const r = jsonResponse({ error: { message: "ignored", details: ["a", "b", "c"] } }); expect(await extractErrorMessage(r, "fb")).toBe("a, b, c"); }); it("caps the number of details rendered and marks the truncation", async () => { const details = Array.from({ length: 20 }, (_, i) => `e${i}`); const r = jsonResponse({ error: { message: "ignored", details } }); const out = await extractErrorMessage(r, "fb"); expect(out.endsWith(", …")).toBe(true); expect(out.split(", ").length).toBe(6); // 5 entries + the ellipsis marker }); it("returns fallback for unparseable bodies", async () => { const r = new Response("not json", { status: 500 }); expect(await extractErrorMessage(r, "fallback")).toBe("fallback"); }); });