import { test } from "node:test"; import assert from "node:assert/strict"; import { api } from "../lib/client-api"; test("API sends explicit falsy JSON bodies and prevents cached private responses", async (t) => { const fetch = t.mock.method( globalThis, "fetch", async (_url: unknown, options: RequestInit) => { assert.equal(options.method, "POST"); assert.equal(options.body, "false"); assert.equal(options.cache, "no-store"); return Response.json({ ok: true }); }, ); assert.deepEqual(await api("test", false), { ok: true }); assert.equal(fetch.mock.callCount(), 1); }); test("API explains HTML gateway failures, JSON errors, and empty error responses", async (t) => { const fetch = t.mock.method( globalThis, "fetch", async () => new Response("Bad gateway", { status: 502 }), ); await assert.rejects(api("test"), /unreadable response/); fetch.mock.mockImplementation(async () => Response.json({ error: "Map unavailable" }, { status: 404 }), ); await assert.rejects(api("test"), /Map unavailable/); fetch.mock.mockImplementation(async () => Response.json(null, { status: 503 }), ); await assert.rejects(api("test"), /Request failed/); }); test("API reports timeouts and offline connections with a recovery action", async (t) => { const fetch = t.mock.method(globalThis, "fetch", async () => { throw new DOMException("timeout", "TimeoutError"); }); await assert.rejects(api("test"), /timed out.*Reconnect/); fetch.mock.mockImplementation(async () => { throw new TypeError("Failed to fetch"); }); await assert.rejects(api("test"), /Check your connection/); });