import { describe, expect, it } from "vitest"; import { API_SURFACE, DEFAULT_API_BASE_URL, PIPELINE, apiBaseUrl, fetchApiStatus, } from "../src/lib/api-status"; function jsonResponse(body: unknown, status = 200): Response { return new Response(JSON.stringify(body), { status, headers: { "content-type": "application/json" } }); } describe("apiBaseUrl", () => { it("falls back to the canonical API host", () => { expect(apiBaseUrl({})).toBe(DEFAULT_API_BASE_URL); }); it("uses the configured base URL and strips trailing slashes", () => { expect(apiBaseUrl({ C0PY_API_BASE_URL: "https://c0py-api.example.test/" })).toBe( "https://c0py-api.example.test", ); }); }); describe("fetchApiStatus", () => { it("reports the status the API actually returned", async () => { const result = await fetchApiStatus("https://api.test", async (input) => { expect(String(input)).toBe("https://api.test/health"); return jsonResponse({ status: "ok", timestamp: "2026-11-11T00:00:00.000Z", cl0ud: "disabled" }); }); expect(result.reachable).toBe(true); if (!result.reachable) throw new Error("expected reachable"); expect(result.status).toBe("ok"); expect(result.cl0ud).toBe("disabled"); expect(result.timestamp).toBe("2026-11-11T00:00:00.000Z"); }); it("does not invent a status when the API answers an error code", async () => { const result = await fetchApiStatus("https://api.test", async () => jsonResponse({}, 503)); expect(result.reachable).toBe(false); if (result.reachable) throw new Error("expected unreachable"); expect(result.httpStatus).toBe(503); expect(result.reason).toContain("503"); }); it("does not invent a status when the API cannot be reached", async () => { const result = await fetchApiStatus("https://api.test", async () => { throw new Error("getaddrinfo ENOTFOUND api.test"); }); expect(result.reachable).toBe(false); if (result.reachable) throw new Error("expected unreachable"); expect(result.reason).toContain("ENOTFOUND"); }); }); describe("declared product surface", () => { it("documents the public health route and the bearer-only v1 routes", () => { expect(API_SURFACE.find((r) => r.path === "/health")?.auth).toBe("public"); const v1 = API_SURFACE.filter((r) => r.path.startsWith("/v1/c0py/")); expect(v1.length).toBeGreaterThanOrEqual(4); expect(v1.every((r) => r.auth === "bearer")).toBe(true); }); it("keeps the canonical pipeline from the masterplan", () => { expect(PIPELINE[0]).toBe("Source product"); expect(PIPELINE).toContain("Evidence Graph / Canonical IR"); expect(PIPELINE.at(-1)).toBe("typed repair loop"); }); });