import test from "node:test"; import assert from "node:assert/strict"; import { writeFile } from "node:fs/promises"; import path from "node:path"; import { setupOpsCorpus, callGet } from "../_testCorpus"; // Run with: // pnpm -C editor exec tsx --test "app/api/ops/space-suggestions/route.test.ts" // // The read of the space scan's file: it never computes. No scan yet is an // answer, not an error; a disk names whose suggestions come back. const corpus = await setupOpsCorpus(null); const { GET } = await import("./route"); test.after(() => corpus.cleanup()); const URL_BASE = "http://localhost/api/ops/space-suggestions"; test("no scan yet: computedAt null, nothing computed", async () => { const r = await callGet(GET, URL_BASE); assert.equal(r.status, 200, JSON.stringify(r.body)); assert.equal(r.body.ok, true); assert.equal(r.body.computedAt, null); }); test("refusals: an unknown query key, a disk the scan does not know", async () => { await seed(); assert.equal((await callGet(GET, `${URL_BASE}?disc=corpus`)).status, 400); const r = await callGet(GET, `${URL_BASE}?disk=nowhere`); assert.equal(r.status, 400); assert.match(r.body.error!, /no disk "nowhere".*corpus, platter/); }); test("the fullest disk's suggestions by default, another disk's when named; the probe memo left out", async () => { await seed(); const r = await callGet(GET, URL_BASE); assert.equal(r.status, 200, JSON.stringify(r.body)); assert.equal(r.body.disk, "corpus"); assert.deepEqual((r.body.disks as { id: string }[]).map((d) => d.id), ["corpus", "platter"]); assert.deepEqual((r.body.suggestions as { kind: string }[]).map((s) => s.kind), ["compress-captions", "evict-clips"]); assert.equal("probes" in r.body, false); // The page's groups: caption compression leads the corpus disk. assert.deepEqual((r.body.groups as { kind: string }[]).map((g) => g.kind), ["compress-captions", "evict-clips"]); const p = await callGet(GET, `${URL_BASE}?disk=platter`); assert.deepEqual((p.body.suggestions as { kind: string }[]).map((s) => s.kind), ["drop-original"]); assert.ok(typeof p.body.ageSeconds === "number"); }); async function seed(): Promise { const GB = 1024 ** 3; const disk = (id: string, size: number, free: number) => ({ id, label: id, mountpoint: `/${id}`, status: "ok", roles: [], locationIds: id === "platter" ? ["platter"] : [], sizeBytes: size * GB, freeBytes: free * GB, archive: null, archiveBytes: null, }); const sug = (kind: string, disk: string, bytes: number) => ({ key: `${kind}:${disk}`, kind, title: kind, frees: { disk, bytes: bytes * GB }, basis: "measured", basisText: "", loss: "", }); await writeFile( path.join(corpus.transcripts, ".space-suggestions.json"), JSON.stringify({ version: 2, computedAt: new Date().toISOString(), tookMs: 1, floorBytes: 0, disks: [disk("platter", 1800, 202), disk("corpus", 1500, 24)], suggestions: [sug("evict-clips", "corpus", 9), sug("drop-original", "platter", 30), sug("compress-captions", "corpus", 62)], overflow: [], held: [], refused: [], unmanaged: [], notScanned: [], compactionAt: null, probes: { "/x": { size: 1, mtimeMs: 1, probe: null } }, }), ); }