Files
2026-11-16 17:26:44 -08:00

56 lines
2.5 KiB
TypeScript

import { test, expect, describe, afterEach } from "bun:test";
import { startBoardBoot } from "../src/lib/boardLoad";
// Records every request path in order and answers with a list shaped for it.
const realFetch = globalThis.fetch;
let seen: string[] = [];
function mockFetch(): void {
seen = [];
globalThis.fetch = (async (input: RequestInfo | URL) => {
const url = new URL(String(input), "http://x");
const path = url.pathname + url.search;
seen.push(path);
const key = url.pathname.split("/").pop()!;
const phase = url.searchParams.get("phase") ?? "all";
const body: Record<string, unknown> =
key === "tasks" ? { tasks: [{ id: `t-${phase}` }], currentId: null }
: key === "nodes" ? { nodes: [], eval: { inFlight: 0, capacity: 0 }, fix: { inFlight: 0, capacity: 0 } }
: { [key]: [{ id: `${key}-${phase}` }] };
return new Response(JSON.stringify(body), { headers: { "content-type": "application/json" } });
}) as typeof fetch;
}
afterEach(() => { globalThis.fetch = realFetch; });
describe("startBoardBoot", () => {
test("first phase asks for the active slice; done follows it", async () => {
mockFetch();
const boot = startBoardBoot(null);
const a = await boot.active;
expect(a.tasks.map((t) => t.id)).toEqual(["t-active"]);
expect(a.features.map((f) => f.id)).toEqual(["features-active"]);
expect(a.prototypes.map((p) => p.id)).toEqual(["prototypes-all"]);
const d = await boot.done;
expect(d.tasks.map((t) => t.id)).toEqual(["t-done"]);
expect(d.security.map((s) => s.id)).toEqual(["security-done"]);
const firstDone = seen.findIndex((p) => p.includes("phase=done"));
const lastActive = Math.max(...seen.map((p, i) => (p.includes("phase=active") ? i : -1)));
expect(firstDone).toBeGreaterThan(lastActive);
// Prototypes / verifications are never sliced.
expect(seen.filter((p) => p.startsWith("/api/prototypes"))).toEqual(["/api/prototypes"]);
expect(seen.filter((p) => p.startsWith("/api/verifications"))).toEqual(["/api/verifications"]);
});
test("a pending hold gates every request", async () => {
mockFetch();
let release!: () => void;
const hold = new Promise<void>((r) => { release = r; });
const boot = startBoardBoot(hold);
await new Promise((r) => setTimeout(r, 20));
expect(seen).toEqual([]);
release();
await boot.active;
expect(seen.some((p) => p === "/api/tasks?phase=active")).toBe(true);
expect(seen.some((p) => p === "/api/nodes")).toBe(true);
});
});