import { describe, expect, it, vi } from "vitest"; import { answerRefusal, FAULT_HEADER, refuse, toldFault } from "../src/http.ts"; describe("an object's 500 is logged on the Worker's own tail event", () => { it("names what threw to the front Worker, which logs it and takes it off the answer", async () => { const quiet = vi.spyOn(console, "error").mockImplementation(() => {}); const inner = answerRefusal(new Error("too many SQL variables at offset 252: SQLITE_ERROR")); expect(inner.status).toBe(500); expect(inner.headers.get(FAULT_HEADER)).toMatch(/^Error: too many SQL variables at offset 252: SQLITE_ERROR/); quiet.mockClear(); const outer = toldFault(new Request("https://x.example/v1/mail/arrived", { method: "POST" }), "/v1/mail/arrived", inner); expect(outer.status).toBe(500); expect(outer.headers.get(FAULT_HEADER)).toBeNull(); expect(await outer.json()).toEqual({ code: "internal_error", message: "the business did not answer" }); expect(quiet).toHaveBeenCalledWith(expect.stringMatching(/^cottondb: 500 POST \/v1\/mail\/arrived: Error: too many SQL variables/)); quiet.mockRestore(); }); it("leaves a refusal and a good answer as they are", () => { let refused: Response | null = null; try { refuse(404, "no_such_record", "no such record"); } catch (error) { refused = answerRefusal(error); } expect(refused!.headers.get(FAULT_HEADER)).toBeNull(); const ok = new Response("{}", { status: 200 }); expect(toldFault(new Request("https://x.example/"), "/", ok)).toBe(ok); }); });