// The Wayback pages scan's pure half (release 37 S2): the targets reader, the // CDX URL per match type, the page parse with its resume key, the merge by // (URL, digest), the asset/include/exclude filters, the summaries, and which // rows are already imported. Synthetic URLs only. // // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test lib/waybackPages/scan.test.ts import { test } from "node:test"; import assert from "node:assert/strict"; import { cdxUrlParam, isAssetUrl, keepCaptureUrl, waybackConfigOf, waybackTargetKey, type WaybackTarget, } from "./targets"; import { parseScanCdxPage, scanCdxUrl } from "./scanCdx"; import { cdxTsBefore, displayPathOf, emptyScanFile, isNewRow, mergeCdxRows, pathPrefixOf, summarizeScan, } from "./scanFile"; import { buildImportedIndex, importedRecordOf, pageUrlKey } from "./imported"; test("the targets reader is the schema's: bad targets dropped, regexes that do not compile dropped, auto read", () => { assert.equal(waybackConfigOf({}), null); assert.equal(waybackConfigOf({ wayback: { targets: [] } }), null); assert.equal(waybackConfigOf({ wayback: { targets: [{ match: "host" }] } }), null); const w = waybackConfigOf({ handling: "transcribe", wayback: { targets: [ { url: " blog.example ", match: "host", from: "2014", to: "20151231", mime: ["text/html"], include: "/post/", exclude: "(", other: 1 }, { url: "https://example.org/a", match: "sideways" }, { url: "https://example.org/b", match: "exact", from: "14" }, ], collapse: "none", auto: "yes", extra: true, }, }); assert.deepEqual(w, { targets: [ { url: "blog.example", match: "host", from: "2014", to: "20151231", mime: ["text/html"], include: "/post/" }, { url: "https://example.org/b", match: "exact" }, ], collapse: "digest", auto: false, }); assert.equal(waybackConfigOf({ wayback: { targets: [{ url: "x.example", match: "host" }], auto: true } })?.auto, true); }); test("a target's key changes with what it asks, not with its filters", () => { const a: WaybackTarget = { url: "blog.example", match: "host" }; assert.equal(waybackTargetKey(a), waybackTargetKey({ ...a, include: "/post/" })); assert.notEqual(waybackTargetKey(a), waybackTargetKey({ ...a, from: "2014" })); assert.notEqual(waybackTargetKey(a), waybackTargetKey({ ...a, match: "domain" })); }); test("the CDX URL per match type: url, matchType, the 200 and mime filters, collapse, bounds, paging", () => { const params = (t: WaybackTarget, extra: Partial[0]> = {}) => new URL(scanCdxUrl({ target: t, limit: 5000, ...extra })).searchParams; for (const match of ["exact", "prefix", "host", "domain"] as const) { const url = match === "host" || match === "domain" ? "https://www.blog.example/whatever" : "https://blog.example/post/"; const p = params({ url, match }); assert.equal(p.get("matchType"), match); assert.equal(p.get("url"), match === "host" || match === "domain" ? "www.blog.example" : url); assert.deepEqual(p.getAll("filter"), ["statuscode:200", "mimetype:text\\/html"]); assert.equal(p.get("collapse"), "digest"); assert.equal(p.get("showResumeKey"), "true"); assert.equal(p.get("output"), "json"); assert.equal(p.get("limit"), "5000"); assert.equal(p.get("resumeKey"), null); } assert.equal(cdxUrlParam({ url: "blog.example", match: "domain" }), "blog.example"); const p = params( { url: "blog.example", match: "host", from: "2014", to: "2015", mime: ["text/html", "text/plain", "application/json"] }, { resumeKey: "abc 123", from: "20140601000000" }, ); assert.equal(p.getAll("filter")[1], "mimetype:text\\/html|text\\/plain|application\\/json"); assert.equal(p.get("from"), "20140601000000", "the later of the target's from and the pass's"); assert.equal(p.get("to"), "2015"); assert.equal(p.get("resumeKey"), "abc 123"); assert.equal(params({ url: "blog.example", match: "host", from: "2016" }, { from: "20140601000000" }).get("from"), "2016"); assert.ok(scanCdxUrl({ target: { url: "a.example", match: "host" }, limit: 1 }).startsWith("https://web.archive.org/cdx/search/cdx?")); }); test("a CDX page: rows by the header's columns, the resume key after an empty row, [] as none", () => { assert.deepEqual(parseScanCdxPage(""), { rows: [] }); assert.deepEqual(parseScanCdxPage("[]"), { rows: [] }); const page = parseScanCdxPage( JSON.stringify([ ["original", "timestamp", "digest", "mimetype", "statuscode"], ["https://blog.example/", "20140102030405", "HOMEA", "text/html", "200"], ["https://blog.example/x", "bad", "X", "text/html", "200"], ["https://blog.example/y", "20140102030406", "", "text/html", "200"], [], ["key-2"], ]), ); assert.equal(page.resumeKey, "key-2"); assert.deepEqual(page.rows, [ { originalUrl: "https://blog.example/", timestamp: "20140102030405", digest: "HOMEA", mimetype: "text/html", status: 200 }, { originalUrl: "https://blog.example/y", timestamp: "20140102030406", digest: "ts-20140102030406", mimetype: "text/html", status: 200 }, ]); assert.equal(parseScanCdxPage(JSON.stringify([["original", "timestamp"], ["https://a.example/", "20140102030405"]])).resumeKey, undefined); assert.throws(() => parseScanCdxPage(""), /did not answer JSON/); assert.throws(() => parseScanCdxPage("{}"), /not a list/); }); test("assets are dropped by extension unless include names them; exclude always wins", () => { for (const u of ["https://blog.example/style.css", "https://blog.example/a/logo.PNG?v=2", "https://blog.example/f.woff2", "https://blog.example/app.js"]) { assert.equal(isAssetUrl(u), true, u); } for (const u of ["https://blog.example/", "https://blog.example/post/1", "https://blog.example/page.html", "https://blog.example/x?f=a.css"]) { assert.equal(isAssetUrl(u), false, u); } const t: WaybackTarget = { url: "blog.example", match: "host" }; assert.equal(keepCaptureUrl(t, "https://blog.example/post/1"), true); assert.equal(keepCaptureUrl(t, "https://blog.example/style.css"), false); assert.equal(keepCaptureUrl({ ...t, include: "\\.css$" }, "https://blog.example/style.css"), true); assert.equal(keepCaptureUrl({ ...t, include: "/post/" }, "https://blog.example/tagged/x"), false); assert.equal(keepCaptureUrl({ ...t, exclude: "/post/1$" }, "https://blog.example/post/1"), false); assert.equal(keepCaptureUrl({ ...t, include: "/post/", exclude: "/post/1$" }, "https://blog.example/post/1"), false); }); const row = (originalUrl: string, timestamp: string, digest: string) => ({ originalUrl, timestamp, digest, mimetype: "text/html", status: 200 }); test("the merge: one row per (URL, digest); a later sighting widens first/last and counts; a second pass marks what it added", () => { const file = emptyScanFile("blog", "2026-01-01T00:00:00.000Z"); const added = mergeCdxRows(file, [ row("https://blog.example/", "20140601000000", "HOMEA"), row("https://blog.example/", "20150101000000", "HOMEB"), row("https://blog.example/", "20160101000000", "HOMEA"), row("https://blog.example/post/1", "20140203000000", "P1A"), ]); assert.equal(added, 3); const home = file.rows.find((r) => r.digest === "HOMEA")!; assert.deepEqual([home.firstTs, home.lastTs, home.count], ["20140601000000", "20160101000000", 2]); // The same capture again (an overlapping later pass) is not counted twice. file.pass = 2; assert.equal(mergeCdxRows(file, [row("https://blog.example/", "20160101000000", "HOMEA"), row("https://blog.example/", "20140102000000", "HOMEA")]), 0); assert.deepEqual([home.firstTs, home.lastTs, home.count], ["20140102000000", "20160101000000", 3]); assert.equal(mergeCdxRows(file, [row("https://blog.example/post/2", "20170101000000", "P2A")]), 1); const p2 = file.rows.find((r) => r.digest === "P2A")!; assert.equal(isNewRow(file, p2), true); assert.equal(isNewRow(file, home), false); assert.equal(isNewRow({ pass: 1 }, { pass: 1 }), false, "nothing is new on the first pass"); }); test("summaries: pages, versions, rows by first-seen year and by one- and two-segment prefix", () => { const file = emptyScanFile("blog", "2026-01-01T00:00:00.000Z"); mergeCdxRows(file, [ row("https://blog.example/", "20140601000000", "H"), row("https://www.blog.example/post/1/first", "20140203000000", "P1A"), row("https://blog.example/post/1/first", "20150203000000", "P1B"), row("https://blog.example/post/2/second", "20150203000000", "P2A"), row("https://blog.example/tagged/bread", "20150203000000", "T"), ]); const s = summarizeScan(file.rows); assert.equal(s.versions, 5); assert.equal(s.pages, 5, "www. and bare are two original URLs"); assert.deepEqual(s.byYear, { "2014": 2, "2015": 3 }); assert.deepEqual(s.byPrefix, { "blog.example/": 1, "blog.example/post/": 3, "blog.example/post/1/": 2, "blog.example/tagged/": 1, }); assert.equal(pathPrefixOf("https://blog.example/post/1/first", 1), "blog.example/post/"); assert.equal(pathPrefixOf("https://blog.example/post/1/", 2), "blog.example/post/1/"); assert.equal(displayPathOf("https://blog.example/post/1?x=2", false), "/post/1?x=2"); assert.equal(displayPathOf("https://www.blog.example/post/1", true), "blog.example/post/1"); assert.equal(cdxTsBefore("2026-02-01T12:00:00.000Z", 31), "20260101120000"); }); test("imported: a record of the same page with the row's digest, or with no digest a capture inside its range", () => { const index = buildImportedIndex([ { id: "20140203000000-aaa", page: { originalUrl: "http://www.blog.example/post/1", captureTs: "20140203000000", digest: "P1A", waybackUrl: "https://web.archive.org/web/20140203000000/http://www.blog.example/post/1" } }, { id: "20150301000000-bbb", page: { originalUrl: "https://blog.example/", captureTs: "20150301000000" } }, { id: "a-post-with-no-page" }, ]); assert.equal(pageUrlKey("http://www.Blog.example:80/post/1"), "blog.example/post/1"); const r = (originalUrl: string, digest: string, firstTs: string, lastTs: string) => ({ originalUrl, digest, firstTs, lastTs }); assert.deepEqual(importedRecordOf(r("https://blog.example/post/1", "P1A", "20140203000000", "20140304000000"), index), { id: "20140203000000-aaa", captureTs: "20140203000000", waybackUrl: "https://web.archive.org/web/20140203000000/http://www.blog.example/post/1", }); assert.equal(importedRecordOf(r("https://blog.example/post/1", "P1B", "20150405000000", "20150405000000"), index), null, "another version is not imported"); assert.equal(importedRecordOf(r("https://blog.example/", "HOMEB", "20150101000000", "20150601000000"), index)?.id, "20150301000000-bbb"); assert.equal(importedRecordOf(r("https://blog.example/", "HOMEA", "20140101000000", "20140601000000"), index), null); assert.equal(importedRecordOf(r("https://blog.example/post/9", "X", "2014", "2015"), index), null); });