// find-copy's scope mode (release 31, lane C) over a synthetic corpus on disk, // the fake Wayback and the fake archive.org: every gone video of a channel or // a site with no media (and, for "no-transcript", no transcript) — gone by the // one reading — untranscribed first; a recent terminal attempt keeps one out // unless `retryFailed`; `max` caps the lookups; a dry run records and fetches // nothing; a run records `none-found` and hands every copy found to ONE fetch. // // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test publish/findCopyScope.test.ts import { test } from "node:test"; import assert from "node:assert/strict"; import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import { startFakeWayback } from "../testing/web/fakeWayback.mjs"; import { startFakeArchiveOrg, type FakeArchiveOrgItem } from "../testing/archiveOrg/fakeArchiveOrg.mjs"; import { GONE_AVAILABILITY } from "../testing/draft/corpus.mjs"; import { ArchiveOrgClient } from "../lib/archiveOrgClient"; import { WaybackClient } from "../lib/waybackClient"; import { loadCopyAttempts, recordCopyAttempt } from "../lib/copyAttempts-server"; import type { Paths } from "../lib/paths"; import type { FetchCopyItem } from "../controller/fetchCopies"; import { enumerateGoneVideos, findWholeCopies, runFindWholeCopies } from "./findCopy"; const NOW = new Date("2026-10-16T00:00:00.000Z"); const WB = "wbArchived1"; // the Wayback Machine holds the video const IA = "iaMirrored1"; // an archive.org mirror of it const NONE = "noCopyAny01"; // no copy anywhere const TRANS = "transcribd1"; // gone, transcribed; the Wayback Machine holds it const MEDIA = "hasAudio001"; // gone, audio already on disk const LIVE = "stillLive01"; // not gone const RECENT = "triedBefor1"; // gone, none found two days ago const BETA = "betaGone001"; // gone, on the second channel; nothing holds it const RUMBLE = "v5rumble"; // gone, a Rumble record const watch = (id: string) => `https://www.youtube.com/watch?v=${id}`; const MIRROR: FakeArchiveOrgItem = { metadata: { identifier: `youtube-${IA}`, title: `An upload ${IA}`, mediatype: "movies", originalurl: watch(IA) }, files: [{ name: `An upload-${IA}.mp4`, source: "original", format: "MPEG4", size: "3000", length: "300.0" }], }; async function writeVideo(channelDir: string, id: string, o: { gone?: boolean; files?: Record; page?: string } = {}) { const v = path.join(channelDir, "data", id); await mkdir(v, { recursive: true }); const page = o.page ?? watch(id); await writeFile(path.join(v, "metadata.info.json"), JSON.stringify({ id, title: `Original ${id}`, webpage_url: page, duration: 300 })); if (o.gone !== false) await writeFile(path.join(v, "availability.json"), JSON.stringify({ ...GONE_AVAILABILITY, webpageUrl: page })); for (const [name, body] of Object.entries(o.files ?? {})) await writeFile(path.join(v, name), body); return v; } async function corpus() { const dir = await mkdtemp(path.join(os.tmpdir(), "find-copy-scope-")); const paths = { channelsDir: path.join(dir, "channels"), transcriptsDir: dir, sitesDir: path.join(dir, "sites") } as Paths; const alpha = path.join(paths.channelsDir, "alpha"); const beta = path.join(paths.channelsDir, "beta"); for (const ch of [alpha, beta]) { await mkdir(path.join(ch, "data"), { recursive: true }); await writeFile(path.join(ch, "config.json"), JSON.stringify({ handling: "youtube", url: "https://www.youtube.com/@x/videos" })); } for (const id of [WB, IA, NONE]) await writeVideo(alpha, id); await writeVideo(alpha, TRANS, { files: { "transcript.en.vtt": "WEBVTT\n\n00:00.000 --> 00:01.000\nhello\n" } }); await writeVideo(alpha, MEDIA, { files: { "audio.mp3": "x" } }); await writeVideo(alpha, LIVE, { gone: false }); const recentDir = await writeVideo(alpha, RECENT); await recordCopyAttempt(recentDir, { at: new Date(NOW.getTime() - 2 * 86_400_000).toISOString(), outcome: "none-found" }); await writeVideo(alpha, RUMBLE, { page: "https://rumble.com/v5rumble-a-stream.html" }); await writeVideo(beta, BETA); return { dir, paths, alpha, beta, close: () => rm(dir, { recursive: true, force: true }) }; } async function fakes() { const wb = await startFakeWayback({ videos: [WB, TRANS] }); const ia = await startFakeArchiveOrg({ items: [MIRROR] }); const deps = { wayback: new WaybackClient({ fetch: wb.fetch, sleep: async () => {} }, { minGapMs: 0, maxAttempts: 2 }), archive: new ArchiveOrgClient({ fetch: ia.fetch }, { minGapMs: 0 }), siteChannels: () => ["beta", "alpha"], now: () => NOW, }; return { wb, ia, deps, close: async () => Promise.all([wb.close(), ia.close()]) }; } test("the candidates: gone by the one reading, no media, no transcript (or, for all, transcribed ones after), a recent attempt left out", async () => { const c = await corpus(); try { const nt = await enumerateGoneVideos({ paths: c.paths, slugs: ["beta", "alpha"], gone: "no-transcript", now: NOW.getTime() }); assert.deepEqual(nt.candidates.map((x) => x.id), [BETA, IA, NONE, RUMBLE, WB]); assert.equal(nt.skipped.media, 1); assert.equal(nt.skipped.transcript, 1); assert.deepEqual(nt.skipped.recent.map((r) => [r.id, r.outcome]), [[RECENT, "none-found"]]); const all = await enumerateGoneVideos({ paths: c.paths, slugs: ["alpha"], gone: "all", now: NOW.getTime(), retryFailed: true }); assert.deepEqual(all.candidates.map((x) => [x.id, x.noTranscript]), [ [IA, true], [NONE, true], [RECENT, true], [RUMBLE, true], [WB, true], [TRANS, false], ]); // An unreadable channel is named and skipped, never read as empty. const unreadable = await enumerateGoneVideos({ paths: c.paths, slugs: ["alpha"], gone: "all", deps: { assertTextReadable: async () => Promise.reject(new Error("its data/ is not readable")) }, }); assert.deepEqual(unreadable.skipped.unreadable, [{ slug: "alpha", error: "its data/ is not readable" }]); assert.equal(unreadable.candidates.length, 0); } finally { await c.close(); } }); test("a dry run of a site: the source each would use, nothing recorded, nothing fetched", async () => { const c = await corpus(); const f = await fakes(); try { const r = await findWholeCopies({ paths: c.paths, scope: { siteId: "jx" }, gone: "no-transcript", dryRun: true, deps: f.deps }); assert.deepEqual(r.looked.map((x) => [x.id, x.outcome]), [ [BETA, "none-found"], [IA, "fetch"], [NONE, "none-found"], [RUMBLE, "not-youtube"], [WB, "fetch"], ]); assert.equal(r.looked.find((x) => x.id === IA)?.via, `archive.org youtube-${IA} / An upload-${IA}.mp4`); assert.match(r.looked.find((x) => x.id === WB)?.via ?? "", /^the Wayback Machine/); assert.deepEqual(r.items.map((i) => [i.id, i.copy.from]), [[IA, "archive.org"], [WB, "wayback"]]); assert.equal(await loadCopyAttempts(path.join(c.alpha, "data", NONE)), null, "a dry run records nothing"); let started = 0; const run = await runFindWholeCopies({ paths: c.paths, scope: { siteId: "jx" }, gone: "no-transcript", dryRun: true, deps: f.deps, startFetch: async () => void started++, }); assert.equal(run.items.length, 2); assert.equal(started, 0, "a dry run starts no fetch"); } finally { await f.close(); await c.close(); } }); test("a run: none-found recorded (and then left out), ONE fetch of every copy found; max caps the lookups, not the non-YouTube ones", async () => { const c = await corpus(); const f = await fakes(); try { const fetches: FetchCopyItem[][] = []; const r = await runFindWholeCopies({ paths: c.paths, scope: { slug: "alpha" }, gone: "no-transcript", max: 2, deps: f.deps, startFetch: async (items) => { fetches.push(items); return "job-1"; }, }); // Alpha's untranscribed, in id order: IA, NONE (two lookups), then RUMBLE // (no lookup), then WB left by max. assert.deepEqual(r.looked.map((x) => [x.id, x.outcome]), [[IA, "fetch"], [NONE, "none-found"], [RUMBLE, "not-youtube"]]); assert.equal(r.left, 1); assert.equal(r.fetch, "job-1"); assert.deepEqual(fetches.map((items) => items.map((i) => i.id)), [[IA]]); const none = await loadCopyAttempts(path.join(c.alpha, "data", NONE)); assert.equal(none?.attempts?.[0]?.outcome, "none-found"); assert.match(none?.attempts?.[0]?.detail ?? "", /wayback none/); // The next run leaves NONE out (a recent terminal attempt) and reaches WB. const again = await findWholeCopies({ paths: c.paths, scope: { slug: "alpha" }, gone: "no-transcript", max: 5, deps: f.deps }); assert.ok(!again.looked.some((x) => x.id === NONE)); assert.ok(again.skipped.recent.some((x) => x.id === NONE)); assert.ok(again.looked.some((x) => x.id === WB && x.outcome === "fetch")); } finally { await f.close(); await c.close(); } }); test("(R8) a Wayback video whose stream does not answer is none-found, recorded 'page captured, no video' — not a copy, not failed at its source", async () => { const c = await corpus(); const wb = await startFakeWayback({ videos: [WB], streams: [] }); const ia = await startFakeArchiveOrg({ items: [] }); try { const deps = { wayback: new WaybackClient({ fetch: wb.fetch, sleep: async () => {} }, { minGapMs: 0, maxAttempts: 2 }), archive: new ArchiveOrgClient({ fetch: ia.fetch }, { minGapMs: 0 }), now: () => NOW, }; let started = 0; const r = await runFindWholeCopies({ paths: c.paths, scope: { slug: "alpha" }, gone: "no-transcript", deps, startFetch: async () => void started++ }); const rec = r.looked.find((x) => x.id === WB); assert.equal(rec?.outcome, "none-found"); assert.equal(started, 0, "nothing found: no fetch-copies"); const got = await loadCopyAttempts(path.join(c.alpha, "data", WB)); assert.deepEqual(got?.failed, []); assert.equal(got?.attempts?.[0]?.outcome, "none-found"); assert.match(got?.attempts?.[0]?.detail ?? "", /wayback none — page captured, no video/); } finally { await wb.close(); await ia.close(); await c.close(); } }); test("archive.org refusing stops the run there; a cancelled run starts no fetch", async () => { const c = await corpus(); const wb = await startFakeWayback({ videos: [] }); const ia = await startFakeArchiveOrg({ items: [MIRROR], busyTimes: 50 }); try { const deps = { wayback: new WaybackClient({ fetch: wb.fetch, sleep: async () => {} }, { minGapMs: 0, maxAttempts: 2 }), archive: new ArchiveOrgClient({ fetch: ia.fetch, sleep: async () => {} }, { minGapMs: 0, maxAttempts: 2 }), now: () => NOW, }; const r = await findWholeCopies({ paths: c.paths, scope: { slug: "alpha" }, gone: "no-transcript", deps }); assert.equal(r.looked.length, 1); assert.equal(r.looked[0]!.outcome, "error"); assert.match(r.stopped ?? "", /archive.org kept refusing/); assert.equal(await loadCopyAttempts(path.join(c.alpha, "data", IA)), null, "an error is not an attempt"); const ctl = new AbortController(); ctl.abort(); let started = 0; await runFindWholeCopies({ paths: c.paths, scope: { slug: "alpha" }, gone: "no-transcript", signal: ctl.signal, deps, startFetch: async () => void started++ }); assert.equal(started, 0); } finally { await wb.close(); await ia.close(); await c.close(); } });