import test from "node:test"; import assert from "node:assert/strict"; import { mkdir, 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/coverage/route.test.ts" // // GET /api/ops/coverage (release 19 A9, the MCP's channel_coverage): the query // refusals and one channel's coverage off a temp corpus. The dating rules are // common/controller/channelCoverage.test.ts. const corpus = await setupOpsCorpus(null); await corpus.writeChannelConfig("demo-yt", { url: "https://www.youtube.com/@demo" }); for (const [id, date] of [["v1", "20240101"], ["v2", "20240301"]]) { const dir = path.join(corpus.transcripts, "channels", "demo-yt", "data", id); await mkdir(dir, { recursive: true }); await writeFile(path.join(dir, "metadata.info.json"), JSON.stringify({ id, title: id, upload_date: date })); } const { GET } = await import("./route"); test.after(() => corpus.cleanup()); const get = (q: string) => callGet(GET, `http://localhost/api/ops/coverage?${q}`); test("coverage refusals: slug, gapDays, dates, list, unknown keys, the token", async () => { const cases: [string, RegExp][] = [ ["", /"slug" is required/], ["slug=../x", /not a valid channel slug/], ["slug=demo-yt&gapDays=0", /"gapDays" must be a whole number/], ["slug=demo-yt&from=2024-01-01", /"from" must be a date YYYYMMDD/], ["slug=demo-yt&list=yes", /"list" is 1 or absent/], ["slug=demo-yt&channel=x", /unknown query key\(s\): channel/], ]; for (const [q, re] of cases) { const r = await get(q); assert.equal(r.status, 400, q); assert.match(r.body.error ?? "", re, q); } assert.equal((await get("slug=no-such")).status, 404); assert.equal((await callGet(GET, "http://localhost/api/ops/coverage?slug=demo-yt", {}, {})).status, 401); }); test("coverage answers the held videos by date and the gaps", async () => { const r = await get("slug=demo-yt&gapDays=30&list=1"); assert.equal(r.status, 200, JSON.stringify(r.body)); assert.equal(r.body.held, 2); assert.deepEqual(r.body.gaps, [{ after: "20240101", before: "20240301", days: 60 }]); assert.equal((r.body.videos as unknown[]).length, 2); });