// Run with: // pnpm -C editor exec tsx --test "app/api/ops/_copies.test.ts" // // `pnpm ops get copies`, `pnpm ops copies-scan` and `pnpm ops copies-release` // (lane cross-source-dupes, slice 1) against the route handlers over a temp // corpus: the refusals before any job, the report, the release. The scan // itself is common/controller/copies.test.ts's. Every channel is invented. import { test } from "node:test"; import assert from "node:assert/strict"; import { mkdir, readFile, writeFile } from "node:fs/promises"; import path from "node:path"; import { callGet, callPost, setupOpsCorpus } from "./_testCorpus"; const corpus = await setupOpsCorpus(null); const { GET } = await import("./copies/route"); const { POST: SCAN } = await import("./copies-scan/route"); const { POST: RELEASE } = await import("./copies-release/route"); const { POST: CONFIRM } = await import("./copies-confirm/route"); const { POST: REJECT } = await import("./copies-reject/route"); test.after(() => corpus.cleanup()); const GROUP = { id: "stream-copies", channels: ["pref-chan", "mirror-chan"] }; async function channels() { for (const slug of GROUP.channels) { await corpus.writeChannelConfig(slug, { url: `https://www.youtube.com/@${slug.replace(/-/g, "")}` }); await mkdir(path.join(corpus.transcripts, "channels", slug, "data"), { recursive: true }); } } test("get copies: no group is an empty report; an unknown group is a 404", async () => { await corpus.writeSettings({ minFreeDiskGB: 0 }); const r = await callGet(GET!, "http://localhost/api/ops/copies"); assert.equal(r.status, 200, JSON.stringify(r.body)); assert.deepEqual(r.body.groups, []); assert.deepEqual(r.body.lanes, {}); assert.equal((await callGet(GET!, "http://localhost/api/ops/copies?group=nope")).status, 404); assert.equal((await callGet(GET!, "http://localhost/api/ops/copies?other=1")).status, 400); }); test("copies-scan refuses before any job: no group, an unknown group, too few channels", async () => { await corpus.writeSettings({ minFreeDiskGB: 0 }); assert.equal((await callPost(SCAN!, {})).status, 400); const unknown = await callPost(SCAN!, { group: "stream-copies" }); assert.equal(unknown.status, 400); assert.match(unknown.body.error!, /no source group "stream-copies"/); await corpus.writeSettings({ minFreeDiskGB: 0, sourceGroups: [GROUP] }); const short = await callPost(SCAN!, { group: "stream-copies" }); assert.match(short.body.error!, /needs two of its channels/); assert.equal((await callPost(SCAN!, { group: "stream-copies", extra: 1 })).status, 400); assert.deepEqual(await corpus.listJobIds(), []); }); // The scan's file, as a scan writes it: one metadata-only pair whose mirror // video's dir (the URL slug) is not its id (the embed id). async function scanFile() { const member = (channel: string, dir: string, id: string, duration: number) => ({ channel, dir, id, title: `${dir} title`, duration, date: "20260301" }); await mkdir(path.join(corpus.transcripts, "copies"), { recursive: true }); await writeFile( path.join(corpus.transcripts, "copies", "stream-copies.json"), JSON.stringify({ format: "archilyzer-copies", version: 1, group: "stream-copies", channels: GROUP.channels, generatedAt: "2026-03-01T00:00:00.000Z", rules: { dateWindowDays: 1, durationToleranceSeconds: 120, durationToleranceRatio: 0.02, withinChannels: false }, pairs: [ { key: "pref-chan/p1|mirror-chan/slug-m1", a: member("pref-chan", "p1", "p1", 7200), b: member("mirror-chan", "slug-m1", "m1embed", 7230), link: "metadata", withinChannel: false, evidence: { kind: "metadata", dateDeltaDays: 0, durationOk: true }, firstSeenAt: "2026-03-01T00:00:00.000Z", }, ], totals: { videos: {}, undated: 0, candidates: 1, confirmed: 0, metadata: 1, partOf: 0, overlaps: 0, rejected: 0, withinChannel: 0 }, }), ); } test("get copies with a group, and copies-release / -confirm / -reject write the one decisions file", async () => { await corpus.writeSettings({ minFreeDiskGB: 0, sourceGroups: [GROUP] }); await channels(); const r = await callGet(GET!, "http://localhost/api/ops/copies?group=stream-copies&pairs=1"); assert.equal(r.status, 200, JSON.stringify(r.body)); const [g] = r.body.groups as { id: string; scan: unknown; channels: { slug: string; rank: number; inCorpus: boolean }[]; list: unknown[] }[]; assert.equal(g.id, "stream-copies"); assert.equal(g.scan, null, "no scan yet"); assert.deepEqual(g.channels.map((c) => [c.slug, c.rank, c.inCorpus]), [["pref-chan", 0, true], ["mirror-chan", 1, true]]); assert.deepEqual(g.list, []); assert.deepEqual(Object.keys(r.body.lanes as object).sort(), ["backfill", "transcription"]); const bad = await callPost(RELEASE!, { group: "stream-copies", video: "no-slash" }); assert.equal(bad.status, 400); assert.equal((await callPost(RELEASE!, { group: "other", video: "mirror-chan/m1" })).status, 404); // No scan yet: nothing is known, nothing is stored. const none = await callPost(RELEASE!, { group: "stream-copies", video: "mirror-chan/slug-m1" }); assert.deepEqual([none.body.applied, none.body.unknown], [[], ["mirror-chan/slug-m1"]]); await scanFile(); // By the embed id, and an unknown key reported beside it. const ok = await callPost(RELEASE!, { group: "stream-copies", videos: ["mirror-chan/m1embed", "mirror-chan/m2"], note: "listened", by: "operator" }); assert.equal(ok.status, 200, JSON.stringify(ok.body)); assert.deepEqual([ok.body.applied, ok.body.unknown, ok.body.now], [["mirror-chan/slug-m1"], ["mirror-chan/m2"], ["mirror-chan/slug-m1"]]); const file = JSON.parse(await readFile(path.join(corpus.transcripts, "copies", "stream-copies.decisions.json"), "utf8")); assert.equal(file.released["mirror-chan/slug-m1"].note, "listened"); assert.equal(file.released["mirror-chan/slug-m1"].by, "operator"); const undo = await callPost(RELEASE!, { group: "stream-copies", video: "mirror-chan/slug-m1", undo: true }); assert.deepEqual(undo.body.now, []); const listed = await callGet(GET!, "http://localhost/api/ops/copies"); const lg = (listed.body.groups as { metadataPairs: { key: string }[] }[])[0]; assert.deepEqual(lg.metadataPairs.map((x) => x.key), [], "first seen long ago: past its wait, nothing held"); assert.equal((await callPost(CONFIRM!, { group: "stream-copies", pair: "nope" })).status, 400); assert.equal((await callPost(CONFIRM!, { group: "stream-copies", pair: "a/b|c/d", by: "someone" })).status, 400); const conf = await callPost(CONFIRM!, { group: "stream-copies", pairs: ["mirror-chan/m1embed|pref-chan/p1"], by: "operator" }); assert.deepEqual(conf.body.applied, ["pref-chan/p1|mirror-chan/slug-m1"]); const rej = await callPost(REJECT!, { group: "stream-copies", pair: "pref-chan/p1|mirror-chan/slug-m1" }); assert.deepEqual([rej.body.applied, rej.body.now], [["pref-chan/p1|mirror-chan/slug-m1"], ["pref-chan/p1|mirror-chan/slug-m1"]]); const after = await callGet(GET!, "http://localhost/api/ops/copies"); const ag = (after.body.groups as { decisions: { confirmed: unknown[]; rejected: { key: string; by: string }[] } }[])[0]; assert.deepEqual(ag.decisions.confirmed, [], "the reject replaced the confirm"); assert.deepEqual(ag.decisions.rejected.map((d) => [d.key, d.by]), [["pref-chan/p1|mirror-chan/slug-m1", "agent"]]); await writeFile(path.join(corpus.transcripts, "copies", "stream-copies.decisions.json"), "{broken"); const broken = await callGet(GET!, "http://localhost/api/ops/copies"); assert.deepEqual((broken.body.groups as { decisions: { released: string[] } }[])[0].decisions.released, [], "an unreadable decisions file decides nothing"); });