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-prepare/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 no media is touched. const ROOT = await mkdtemp(path.join(os.tmpdir(), "reports-prepare-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-prepare", { 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, a site id, and the only key", 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", reportId: "r1" }); assert.equal(unknown.status, 400); assert.match(unknown.error, /unknown key\(s\): reportId — this route accepts siteId/); }); test("a site that does not exist is refused before any job", async () => { const r = await post({ siteId: "demo-site" }); assert.equal(r.status, 400); assert.match(r.error, /No site "demo-site"/); });