import test from "node:test"; import assert from "node:assert/strict"; import { mkdtemp, rm } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; // Run with: // pnpm -C editor exec tsx --test "app/api/ops/reports-export/route.test.ts" // // The body's shape and the refusals that come before any job, answered from an // empty temp corpus: no job is queued and nothing is exported. const ROOT = await mkdtemp(path.join(os.tmpdir(), "reports-export-route-")); // Set before the route (and getPaths, which caches) is first imported. process.env.WORKER_TOKEN = "test-token"; process.env.TRANSCRIPTS_DIR = ROOT; process.env.SETTINGS_FILE = path.join(ROOT, "settings.json"); const { POST } = await import("./route"); test.after(() => rm(ROOT, { recursive: true, force: true })); async function post(body: Record): Promise<{ status: number; error: string }> { const res = await POST( new Request("http://localhost/api/ops/reports-export", { method: "POST", headers: { authorization: "Bearer test-token", "content-type": "application/json", }, body: JSON.stringify(body), }), ); return { status: res.status, error: ((await res.json()) as { error?: string }).error ?? "" }; } test("siteId is required and a site id; reportId and formats are the other keys", async () => { assert.match((await post({})).error, /"siteId" is required/); const bad = await post({ siteId: "../x" }); assert.equal(bad.status, 400); assert.match(bad.error, /not a valid site id/); const unknown = await post({ siteId: "demo-site", pages: 1 }); assert.equal(unknown.status, 400); assert.match(unknown.error, /unknown key\(s\): pages — this route accepts siteId, reportId, formats/); }); test("a format not an export's is refused, naming it", async () => { const r = await post({ siteId: "demo-site", formats: ["html", "docx"] }); assert.equal(r.status, 400); assert.match(r.error, /1 of "formats" not in the export formats: docx/); }); test("a site that does not exist is refused before any job", async () => { const r = await post({ siteId: "demo-site", reportId: "r1", formats: ["md"] }); assert.equal(r.status, 400); assert.match(r.error, /No site "demo-site"/); });