// Queueing imports of chosen scan rows (release 37 S2): by key, by the // table's filter, a dry run, the batch cap, a refusal, and what wayback.auto // would take — one import-page request per row (the starter swapped for a // recorder, and S1's own door once), and the imported derivation over real // webpage records. Synthetic scan files and records only. // // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test controller/waybackImport.test.ts import { test, after } from "node:test"; import assert from "node:assert/strict"; import { mkdir, mkdtemp, rm } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; const ROOT = await mkdtemp(path.join(os.tmpdir(), "wayback-import-")); process.env.TRANSCRIPTS_DIR = path.join(ROOT, "transcripts"); after(() => rm(ROOT, { recursive: true, force: true })); const { getPaths } = await import("../lib/paths"); const { emptyScanFile, mergeCdxRows, summarizeScan } = await import("../lib/waybackPages/scanFile"); const { writeWaybackScan } = await import("../lib/waybackPages/scanFile-server"); const { IMPORT_BATCH_MAX, pendingAutoImports, queueCaptureImports, setImportPageStarterForTest, } = await import("./waybackImport"); const row = (originalUrl: string, timestamp: string, digest: string) => ({ originalUrl, timestamp, digest, mimetype: "text/html", status: 200 }); async function scan(slug: string, pass1: ReturnType[], pass2: ReturnType[] = []) { await mkdir(path.join(getPaths().channelsDir, slug), { recursive: true }); const file = emptyScanFile(slug, "2026-01-01T00:00:00.000Z"); mergeCdxRows(file, pass1); if (pass2.length) { file.pass = 2; mergeCdxRows(file, pass2); } file.summary = summarizeScan(file.rows); await writeWaybackScan(getPaths().channelsDir, file); } const asked: unknown[] = []; setImportPageStarterForTest(async (req) => { const { paths: _p, ...rest } = req; asked.push(rest); return { jobId: `job-${asked.length}` }; }); await scan( "blog", [ row("https://blog.example/", "20140102030405", "HOMEA"), row("https://blog.example/post/1", "20140203000000", "P1A"), row("https://blog.example/post/1", "20150203000000", "P1B"), row("https://blog.example/post/2", "20150506000000", "P2A"), ], [row("https://blog.example/post/3", "20160101000000", "P3A")], ); test("by key: one import-page request per row, the capture it was first listed at; an unknown key is skipped", async () => { asked.length = 0; const r = await queueCaptureImports({ paths: getPaths(), slug: "blog", keys: ["P1B https://blog.example/post/1", "NOPE https://blog.example/x", "P1B https://blog.example/post/1"], requestedBy: "captures", }); assert.equal(r.ok, true); assert.equal(r.chosen, 1); assert.deepEqual(asked, [{ slug: "blog", url: "https://web.archive.org/web/20150203000000/https://blog.example/post/1", requestedBy: "captures" }]); assert.deepEqual(r.queued, [{ key: "P1B https://blog.example/post/1", url: "https://web.archive.org/web/20150203000000/https://blog.example/post/1", jobId: "job-1" }]); assert.deepEqual(r.skipped, [{ key: "NOPE https://blog.example/x", why: "not in the scan" }]); }); test("by filter (Import all in range): year, prefix, text, new; a dry run queues nothing", async () => { asked.length = 0; const y = await queueCaptureImports({ paths: getPaths(), slug: "blog", filter: { year: "2015" } }); assert.deepEqual(y.queued.map((q) => q.key), ["P1B https://blog.example/post/1", "P2A https://blog.example/post/2"]); const p = await queueCaptureImports({ paths: getPaths(), slug: "blog", filter: { prefix: "blog.example/post/" }, dryRun: true }); assert.equal(p.queued.length, 4); assert.equal(p.queued.every((q) => q.jobId === undefined), true); const n = await queueCaptureImports({ paths: getPaths(), slug: "blog", filter: { show: "new" }, dryRun: true }); assert.deepEqual(n.queued.map((q) => q.key), ["P3A https://blog.example/post/3"]); const q = await queueCaptureImports({ paths: getPaths(), slug: "blog", filter: { q: "POST/2" }, dryRun: true }); assert.deepEqual(q.queued.map((x) => x.key), ["P2A https://blog.example/post/2"]); assert.equal(asked.length, 2, "only the real run asked"); }); test("no scan is a refusal; a starter's refusal stops the batch with its sentence", async () => { const none = await queueCaptureImports({ paths: getPaths(), slug: "nothing", filter: {} }); assert.equal(none.ok, false); assert.match(none.error ?? "", /no Wayback scan yet/); setImportPageStarterForTest(async () => { throw new Error("the platform is held"); }); try { const r = await queueCaptureImports({ paths: getPaths(), slug: "blog", filter: {} }); assert.equal(r.ok, false); assert.equal(r.error, "the platform is held"); assert.equal(r.queued.length, 0); } finally { setImportPageStarterForTest(async (req) => { const { paths: _p, ...rest } = req; asked.push(rest); return { jobId: `job-${asked.length}` }; }); } }); test("at most IMPORT_BATCH_MAX a call; the rest are left for the next", async () => { const many = Array.from({ length: IMPORT_BATCH_MAX + 7 }, (_, i) => row(`https://many.example/p/${i}`, `2014010100${String(i).padStart(4, "0")}`, `D${i}`)); await scan("many", many); const r = await queueCaptureImports({ paths: getPaths(), slug: "many", filter: {}, dryRun: true }); assert.equal(r.chosen, IMPORT_BATCH_MAX + 7); assert.equal(r.queued.length, IMPORT_BATCH_MAX); assert.equal(r.left, 7); }); test("wayback.auto takes the rows listed after the first pass, oldest first", async () => { const pending = await pendingAutoImports(getPaths(), "blog"); assert.deepEqual(pending.map((r) => r.key), ["P3A https://blog.example/post/3"]); assert.deepEqual(await pendingAutoImports(getPaths(), "nothing"), []); }); test("the default door is S1's import-page: refused with its sentence for a channel that is not a Wayback pages channel", async () => { setImportPageStarterForTest(null); const r = await queueCaptureImports({ paths: getPaths(), slug: "blog", keys: ["HOMEA https://blog.example/"] }); assert.equal(r.ok, false); assert.ok(r.error && r.error.length > 0); assert.equal(r.queued.length, 0); }); test("imported: a webpage record of a row's content is marked and skipped, through the posts store's reader", async () => { setImportPageStarterForTest(async (req) => { const { paths: _p, ...rest } = req; asked.push(rest); return { jobId: `job-${asked.length}` }; }); const { writePosts } = await import("../lib/posts-server"); const { readImportedIndex } = await import("../lib/waybackPages/scanFile-server"); const root = path.join(getPaths().channelsDir, "blog"); const page = (originalUrl: string, captureTs: string, digest?: string) => ({ originalUrl, captureTs, waybackUrl: `https://web.archive.org/web/${captureTs}/${originalUrl}`, rawUrl: `https://web.archive.org/web/${captureTs}id_/${originalUrl}`, kind: "generic" as const, extractor: "generic", extractorVersion: 1, ...(digest ? { digest } : {}), }); const post = (id: string, p: ReturnType) => ({ id, slug: `blog/${id}`, channelSlug: "blog", author: "blog.example", createdAt: "2015-02-03T00:00:00.000Z", uploadDate: "20150203", text: "A post.", url: p.originalUrl, platform: "webpage" as const, isReply: false, isRepost: false, links: [], threadId: "page-x", page: p, }); await writePosts(root, [ post("20150203000000-aaaaaaaaaaaa", page("https://blog.example/post/1", "20150203000000", "P1B")), post("20140102030405-bbbbbbbbbbbb", page("http://www.blog.example/", "20140102030405")), ]); const index = await readImportedIndex(getPaths().channelsDir, "blog"); assert.equal(index.size, 2); asked.length = 0; const r = await queueCaptureImports({ paths: getPaths(), slug: "blog", filter: {} }); assert.equal(r.ok, true); assert.deepEqual( r.skipped.map((x) => x.key).sort(), ["HOMEA https://blog.example/", "P1B https://blog.example/post/1"], ); assert.equal(r.skipped.every((x) => x.why.startsWith("imported as ")), true); assert.deepEqual(r.queued.map((q) => q.key), ["P1A https://blog.example/post/1", "P2A https://blog.example/post/2", "P3A https://blog.example/post/3"]); const auto = await pendingAutoImports(getPaths(), "blog"); assert.deepEqual(auto.map((x) => x.key), ["P3A https://blog.example/post/3"]); });