// The mirror scan (release 31, lane D) over the synthetic mirror corpus // (common/testing/mirrors/): off disk with no index, then through the REAL // buildIndex (the records from `sums`, the recorded dates the index derived), // the same totals both ways; a dry run writes nothing; a channel with no // mirror.of is refused; the job kind reads the text tier. // // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test controller/mirrorScan.test.ts import { after, test } from "node:test"; import assert from "node:assert/strict"; import { existsSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; import { readFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; const ROOT = mkdtempSync(path.join(tmpdir(), "mirror-scan-")); const PINNED: Record = { TRANSCRIPTS_DIR: path.join(ROOT, "transcripts"), SAVED_VIDEOS_DIR: path.join(ROOT, "saved-videos"), SITES_DIR: path.join(ROOT, "transcripts", "sites"), SETTINGS_FILE: path.join(ROOT, "settings.json"), EXPORT_PUBLIC_DIR: path.join(ROOT, "public"), EXPORT_INDEX_DIR: path.join(ROOT, ".export-index"), EXPORT_BUILDS_DIR: path.join(ROOT, ".export-builds"), EDITOR_CHANGELOG_FILE: path.join(ROOT, "editor-CHANGELOG.md"), EXPORT_CHANGELOG_FILE: path.join(ROOT, "export-CHANGELOG.md"), CHARTS_CONFIG_FILE: path.join(ROOT, "chart-templates.json"), SEARCH_ALIASES_FILE: path.join(ROOT, "transcripts", "search-aliases.json"), CURATED_TAGS_FILE: path.join(ROOT, "transcripts", "tags.json"), ARCHILYZER_CONFIG_DIR: path.join(ROOT, "config"), ARCHILYZER_SOURCE_SCRATCH: path.join(ROOT, "source-scratch"), }; Object.assign(process.env, PINNED); delete process.env.ARCHILYZER_INDEX_ALLOW_HELD; // A job's end still writes after its status reads done (its log and sidecar, // and the report regeneration it arms in the channel dir): waited for before // the corpus is removed (testing/jobs/quiesce.ts). const { quiesceJobs } = await import("../testing/jobs/quiesce"); after(async () => { await quiesceJobs(); rmSync(ROOT, { recursive: true, force: true }); }); const { getPaths } = await import("../lib/paths"); const { buildIndex } = await import("./buildIndex"); const { mirrorScanRefusal, parseMirrorScanBody, readChannelRecords, runMirrorScan, mirrorScanSummary } = await import("./mirrorScan"); const { readMirrorScan, listMirrorScans, mirrorScanPath } = await import("../lib/mirrors/scanFile-server"); const { copiesIndex } = await import("../lib/mirrors/scanFile"); const { kindNeedsText, kindNeedsMedia, jobKindLabel } = await import("../jobs/jobKinds"); const { writeMirrorCorpus, EXPECTED, MIRROR_SLUG, CREATOR_SLUG, RUMBLE_SLUG } = await import("../testing/mirrors/mirrorCorpus.mjs"); const paths = getPaths(); mkdirSync(paths.channelsDir, { recursive: true }); writeFileSync(paths.settingsFile, "{}"); await writeMirrorCorpus(paths.channelsDir); function checkTotals(f: Awaited>["file"]) { assert.deepEqual(f.window, EXPECTED.window); assert.deepEqual(Object.fromEntries(f.videos.map((v) => [v.id, v.status])), EXPECTED.status); assert.deepEqual(f.totals.byStatus, EXPECTED.byStatus); assert.deepEqual(f.totals.byPlatform, EXPECTED.byPlatform); assert.equal(f.totals.creatorRemoved.inWindow, EXPECTED.creatorRemoved.inWindow); assert.equal(f.totals.creatorRemoved.held, EXPECTED.creatorRemoved.held); assert.deepEqual(Object.fromEntries(f.removed.map((r) => [r.id, r.heldBy])), EXPECTED.held); assert.deepEqual(f.platforms, { [CREATOR_SLUG]: "youtube", [RUMBLE_SLUG]: "rumble" }); } test("the job kind reads the text tier", () => { assert.equal(kindNeedsText("mirror-scan"), true); assert.equal(kindNeedsMedia("mirror-scan"), false); assert.equal(jobKindLabel("mirror-scan"), "Mirror scan"); }); test("the body: a slug, dryRun a boolean", () => { assert.deepEqual(parseMirrorScanBody({ slug: " a-b " }), { slug: "a-b", dryRun: false }); assert.deepEqual(parseMirrorScanBody({ slug: "a", dryRun: true }), { slug: "a", dryRun: true }); assert.throws(() => parseMirrorScanBody({}), /"slug" is required/); assert.throws(() => parseMirrorScanBody({ slug: "../x" }), /not a channel slug/); assert.throws(() => parseMirrorScanBody({ slug: "a", dryRun: "yes" }), /"dryRun" must be true or false/); }); test("refused: an unknown channel, a channel with no mirror.of", async () => { assert.match((await mirrorScanRefusal(paths, "no-such")) ?? "", /Channel "no-such" not found/); assert.match((await mirrorScanRefusal(paths, CREATOR_SLUG)) ?? "", /is not a mirror: its config has no mirror\.of/); assert.equal(await mirrorScanRefusal(paths, MIRROR_SLUG), null); await assert.rejects(runMirrorScan(paths, CREATOR_SLUG), /is not a mirror/); }); test("off disk (no index): the records, the recorded dates from the rule, the totals both ways; a dry run writes nothing", async () => { const m = await readChannelRecords(paths, MIRROR_SLUG); assert.equal(m.fromIndex, 0); assert.equal(m.fromDisk, 6); assert.equal(m.platform, "odysee"); assert.equal(m.records.find((r) => r.id === "vod-m1")?.recordedDate, "20230301"); assert.equal(m.records.find((r) => r.id === "vod-m7")?.recordedDate, undefined); const r = await readChannelRecords(paths, RUMBLE_SLUG, { goneWindow: { from: "20230101", to: "20231231" } }); assert.deepEqual(r.records.map((x) => [x.id, x.dir]).sort(), [ ["v4embed1", "v1abc-big-news-day"], ["v4embed2", "v1abd-oven-lawsuit"], ["v4embed5", "v1abe-pan-review"], ]); const c = await readChannelRecords(paths, CREATOR_SLUG, { goneWindow: { from: "20230228", to: "20230322" } }); assert.equal(c.records.find((x) => x.id === "yt-s2")?.gone, "deleted"); assert.equal(c.records.find((x) => x.id === "yt-s3")?.gone, "private"); assert.equal(c.records.find((x) => x.id === "yt-x1")?.gone, null, "outside the window: not read"); assert.equal(c.records.find((x) => x.id === "yt-s1")?.wasLive, true); const dry = await runMirrorScan(paths, MIRROR_SLUG, { dryRun: true, now: () => new Date("2026-10-16T00:00:00Z") }); assert.equal(dry.path, undefined); assert.equal(existsSync(mirrorScanPath(paths.transcriptsDir, MIRROR_SLUG)), false); checkTotals(dry.file); assert.equal(dry.file.generatedAt, "2026-10-16T00:00:00.000Z"); assert.deepEqual(mirrorScanSummary(dry, true).byStatus, EXPECTED.byStatus); }); test("through the real index: the same totals, the file written and read back, the copies index", async () => { mkdirSync(path.join(paths.sitesDir, "testsite"), { recursive: true }); writeFileSync( path.join(paths.sitesDir, "testsite", "site.json"), JSON.stringify({ siteId: "testsite", siteTitle: "Test Site", siteDescription: "fixture", headerTitle: "Test Site", homeTagline: "", socialLinks: [], groups: [{ id: "default", name: "All channels", selectedByDefault: true }], defaultGroupId: "default", channels: [MIRROR_SLUG, CREATOR_SLUG, RUMBLE_SLUG].map((slug) => ({ slug, groupId: "default" })), }), ); await buildIndex({ paths, onLog: () => {} }); const m = await readChannelRecords(paths, MIRROR_SLUG); assert.equal(m.fromIndex, 6, "every mirror video from the index"); assert.equal(m.records.find((r) => r.id === "vod-m2")?.recordedDate, "20230305", "the index's recorded date"); const r = await readChannelRecords(paths, RUMBLE_SLUG); assert.equal(r.fromIndex, 3); assert.equal(r.records.find((x) => x.id === "v4embed2")?.dir, "v1abd-oven-lawsuit"); const out = await runMirrorScan(paths, MIRROR_SLUG, { jobId: "job-1" }); assert.equal(out.path, mirrorScanPath(paths.transcriptsDir, MIRROR_SLUG)); checkTotals(out.file); const back = await readMirrorScan(paths.transcriptsDir, MIRROR_SLUG); assert.ok(back); assert.equal(back.jobId, "job-1"); assert.equal(back.format, "archilyzer-mirror-scan"); assert.equal(back.version, 1); assert.deepEqual(JSON.parse(await readFile(out.path!, "utf8")).totals, out.file.totals); const all = await listMirrorScans(paths.transcriptsDir); assert.equal(all.length, 1); const idx = copiesIndex(all); assert.deepEqual(idx.get(`${MIRROR_SLUG}/vod-m2`), { role: "mirror", mirror: MIRROR_SLUG, status: "creator-deleted", matches: [ { channel: CREATOR_SLUG, id: "yt-s2", gone: "deleted" }, { channel: RUMBLE_SLUG, id: "v4embed2" }, ], }); assert.deepEqual(idx.get(`${CREATOR_SLUG}/yt-s3`), { role: "creator", copies: [{ mirror: MIRROR_SLUG, id: "vod-m3" }], gone: "private" }); assert.equal(idx.get(`${CREATOR_SLUG}/yt-x2`), undefined, "removed, not held"); }); test("as a job: refused before any job without mirror.of; the log ends with the totals; onDone after a write only", async () => { const { enqueueMirrorScan } = await import("./mirrorScan"); const { getRegistry } = await import("../jobs/registry"); const refused = await enqueueMirrorScan({ slug: CREATOR_SLUG }, { paths }); assert.equal(refused.ok, false); const done: string[] = []; for (const dryRun of [true, false]) { const out = await enqueueMirrorScan({ slug: MIRROR_SLUG, dryRun }, { paths, onDone: (s) => done.push(s) }); assert.ok(out.ok, JSON.stringify(out)); const job = getRegistry().get(out.jobId)!; assert.equal(job.kind, "mirror-scan"); assert.equal(job.queueKey, "mirror-scan"); assert.equal(job.channelSlug, MIRROR_SLUG); void out.stream.cancel(); const end = await out.done; const log = await readFile(job.logPath, "utf8"); assert.equal(end.status, "done", log); const line = log.split("\n").find((l) => l.startsWith("@@mirror-scan "))!; const summary = JSON.parse(line.slice("@@mirror-scan ".length)); assert.equal(summary.dryRun, dryRun); assert.deepEqual(summary.byStatus, EXPECTED.byStatus); assert.equal(summary.creatorRemoved.held, EXPECTED.creatorRemoved.held); assert.equal("file" in summary, !dryRun); assert.match(log, /the creator removed 3 in this window; the mirror holds 2/); } assert.deepEqual(done, [MIRROR_SLUG]); });