import assert from "node:assert/strict"; import test from "node:test"; import { api, ApiError, describeError } from "../src/lib/api.ts"; import { errors } from "../src/copy/errors/index.ts"; // Guards the one place a caught error becomes text: a dropped connection must // read as the catalogue's network copy (not "TypeError: Failed to fetch"), an // expired session as sign-in copy, and a thrown non-Error never as // "[object Object]". test("describeError turns a failure into operator copy", async () => { const fallback = "Could not load tasks"; const realFetch = globalThis.fetch; globalThis.fetch = () => Promise.reject(new TypeError("Failed to fetch")); let dropped: unknown; try { await api.tasks(); } catch (err) { dropped = err; } finally { globalThis.fetch = realFetch; } const cases: { name: string; err: unknown; want: string }[] = [ { name: "dropped connection", err: dropped, want: errors.common.network }, { name: "expired session", err: new ApiError("unauthorized", 401), want: errors.common.sessionExpired }, { name: "server reason", err: new ApiError("task is not awaiting approval", 409), want: "task is not awaiting approval" }, { name: "plain object", err: { code: 1 }, want: fallback }, { name: "empty message", err: new Error(""), want: fallback }, ]; for (const c of cases) assert.equal(describeError(c.err, fallback), c.want, c.name); });