import test from "node:test"; import assert from "node:assert/strict"; import { readFile, writeFile } from "node:fs/promises"; import path from "node:path"; import { callPost, setupOpsCorpus } from "./_testCorpus"; // Run with: // pnpm -C editor exec tsx --test "app/api/ops/_markProvenance.test.ts" // // Lane mark-provenance over ops, against a temp corpus: the opt-in flag is // checked at the door and the plan counts its group apart; marks-backfill is a // dry run unless told otherwise, writes nothing then, and refuses an unknown // key. Nothing here starts a job or revalidates a page. const corpus = await setupOpsCorpus("one-youtube-channel-with-data"); type Post = Parameters[0]; const route = async (name: string) => (await import(`./${name}/route`)).POST as Post; const SLUG = "test-youtube"; const VIDEO = "20240101_test1234567"; test.after(() => corpus.cleanup()); const videoDir = () => path.join(corpus.transcripts, "channels", SLUG, "data", VIDEO); test("cleanup: includeSourceGone needs unchecked; the dry plan answers the source-gone group on its own", async () => { const cleanup = await route("cleanup"); const refused = await callPost(cleanup, { slug: SLUG, sweep: "transcribed", includeSourceGone: true }); assert.equal(refused.status, 400); assert.match(String(refused.body.error), /"includeSourceGone" applies to a clean without checking only/); const bad = await callPost(cleanup, { slug: SLUG, sweep: "transcribed", unchecked: true, dryRun: true, includeSourceGone: "yes" }); assert.equal(bad.status, 400); const dry = await callPost(cleanup, { slug: SLUG, sweep: "transcribed", unchecked: true, dryRun: true, includeSourceGone: true }); assert.equal(dry.status, 200, JSON.stringify(dry.body)); const plan = dry.body.plan as { sourceGone: Record }; assert.deepEqual(Object.keys(plan.sourceGone).sort(), ["bytes", "files", "ids", "inferred", "videos"]); assert.equal(dry.body.includeSourceGone, true); }); test("marks-backfill: a dry run by default, writes nothing, says why a mark is not proven; unknown keys refused", async () => { const backfill = await route("marks-backfill"); const markFile = path.join(videoDir(), "do-not-clean.json"); const before = JSON.stringify({ setAt: "2026-10-20T10:00:30.000Z", note: "deleted from source (pre-clean check): deleted" }); await writeFile(markFile, before); const r = await callPost(backfill, { slug: SLUG }); assert.equal(r.status, 200, JSON.stringify(r.body)); assert.equal(r.body.dryRun, true); assert.deepEqual(r.body.relabelled, []); assert.deepEqual(r.body.notProven, [{ id: VIDEO, setAt: "2026-10-20T10:00:30.000Z", reason: "no-availability-record" }]); assert.equal(await readFile(markFile, "utf8"), before); const unknown = await callPost(backfill, { slug: SLUG, apply: true }); assert.equal(unknown.status, 400); const noChannel = await callPost(backfill, { slug: "no-such" }); assert.equal(noChannel.status, 400); assert.match(String(noChannel.body.error), /no-such/); }); // --- Review 1 --------------------------------------------------------------- const MACHINE = { setAt: "2026-11-03T08:00:00.000Z", note: "deleted from source (pre-clean check): deleted", by: "availability-check", via: "pre-clean-check" }; test("F4: expectSourceGone makes the dry run binding — a changed count is refused and nothing is queued", async () => { const cleanup = await route("cleanup"); await writeFile(path.join(videoDir(), "do-not-clean.json"), JSON.stringify(MACHINE)); const alone = await callPost(cleanup, { slug: SLUG, sweep: "transcribed", unchecked: true, expectSourceGone: 1 }); assert.equal(alone.status, 400); assert.match(String(alone.body.error), /"expectSourceGone" goes with "includeSourceGone": true/); const dry = await callPost(cleanup, { slug: SLUG, sweep: "transcribed", unchecked: true, dryRun: true, includeSourceGone: true }); const now = (dry.body.plan as { sourceGone: { videos: number } }).sourceGone.videos; const off = await callPost(cleanup, { slug: SLUG, sweep: "transcribed", unchecked: true, includeSourceGone: true, expectSourceGone: now + 1 }); assert.equal(off.status, 409, JSON.stringify(off.body)); assert.match(String(off.body.error), new RegExp(`is now ${now} video\\(s\\), not the ${now + 1} expected: nothing was queued`)); assert.deepEqual(await corpus.listJobIds(), []); }); test("F2: get marks says who set each mark and when; marks-backfill undo is a dry run first", async () => { await writeFile(path.join(videoDir(), "do-not-clean.json"), JSON.stringify(MACHINE)); const { GET } = (await import("./marks/route")) as { GET: (r: Request) => Promise }; const res = await GET(new Request(`http://x/api/ops/marks?slug=${SLUG}`, { headers: { authorization: `Bearer ${process.env.WORKER_TOKEN}` } })); const body = (await res.json()) as { count: number; sourceGone: number; marks: { id: string; setBy: string; mine: boolean }[] }; assert.equal(res.status, 200, JSON.stringify(body)); assert.equal(body.count, 1); assert.equal(body.sourceGone, 1); assert.match(body.marks[0].setBy, /^availability check, 3 Nov( 2026)?; the source was gone$/); assert.equal(body.marks[0].mine, false); const backfill = await route("marks-backfill"); const undo = await callPost(backfill, { slug: SLUG, undo: true }); assert.equal(undo.status, 200, JSON.stringify(undo.body)); assert.deepEqual([undo.body.undo, undo.body.dryRun, undo.body.unlabelled], [true, true, []]); const keep = await route("do-not-clean"); const badNote = await callPost(keep, { slug: SLUG, id: VIDEO, note: 5 }); assert.equal(badNote.status, 400, "the note is checked at the door"); });