// Release 37 S1: a Wayback pages channel's scope (`config.wayback`), what the // New channel form derives from a URL, and the URL readers of an import. // // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test lib/waybackPages/config.test.ts import test from "node:test"; import assert from "node:assert/strict"; import { coerceWaybackPagesConfig, isWaybackPagesChannel, waybackTargetFromUrl, waybackTargetHost, } from "./config"; import { captureTsIso, isCaptureAt, isPageMimetype, isWaybackPagesUrl, isXHostUrl, isXTargetUrl, parsePageCaptureAsk, videoPageOf, } from "./urls"; import { parseChannelConfig } from "../channelConfig"; import { channelConfigSchema } from "../channelConfigSchema"; import { isXPostsChannel, postsVisibleTo } from "../postsVisibility"; test("config.wayback: valid targets kept, invalid ones dropped one by one, none = the key omitted", () => { assert.deepEqual( coerceWaybackPagesConfig({ targets: [ { url: " example.com/blog ", match: "prefix", from: "2014", to: "20161231", mime: ["Text/HTML", "nonsense"], include: "\\.html$" }, { url: "example.com", match: "everything" }, { match: "exact" }, { url: "a b.example", match: "exact" }, { url: "example.org", match: "domain", from: "14" }, ], collapse: "digest", }), { targets: [ { url: "example.com/blog", match: "prefix", from: "2014", to: "20161231", mime: ["text/html"], include: "\\.html$" }, { url: "example.org", match: "domain" }, ], collapse: "digest", }, ); assert.equal(coerceWaybackPagesConfig({ targets: [{ url: "x", match: "nope" }] }), undefined); assert.equal(coerceWaybackPagesConfig({ targets: [] }), undefined); assert.equal(coerceWaybackPagesConfig([]), undefined); assert.equal(coerceWaybackPagesConfig("example.com"), undefined); }); test("the client-safe parser and the zod schema agree on config.wayback", () => { const raw = { handling: "transcribe", sourceKind: "social", platform: "webpage", name: "Old blog", url: "https://web.archive.org/web/*/blog.example/*", socialHandle: "blog.example", wayback: { targets: [{ url: "blog.example/", match: "prefix" }, { url: "", match: "exact" }] }, }; const a = parseChannelConfig(raw); const b = channelConfigSchema.parse(raw); assert.deepEqual(a, b); assert.deepEqual(a?.wayback, { targets: [{ url: "blog.example/", match: "prefix" }] }); assert.equal(a?.platform, "webpage"); assert.equal(isWaybackPagesChannel(a), true); assert.equal(isWaybackPagesChannel({ sourceKind: "social", platform: "twitter" }), false); assert.equal(isWaybackPagesChannel({ platform: "webpage" }), false); }); test("a target from a URL: the Wayback listing forms, a capture, a bare URL", () => { assert.deepEqual(waybackTargetFromUrl("https://web.archive.org/web/*/example.com/blog*"), { url: "example.com/blog", match: "prefix" }); assert.deepEqual(waybackTargetFromUrl("https://web.archive.org/web/*/*.example.com*"), { url: "example.com", match: "domain" }); assert.deepEqual(waybackTargetFromUrl("https://web.archive.org/web/*/https://example.com/about"), { url: "https://example.com/about", match: "exact" }); assert.deepEqual(waybackTargetFromUrl("https://web.archive.org/web/2014*/example.com/*"), { url: "example.com/", match: "prefix", from: "2014", to: "2014", }); assert.deepEqual( waybackTargetFromUrl("https://web.archive.org/web/20140305120000/https://blog.example/a.html"), { url: "https://blog.example/a.html", match: "exact" }, ); assert.deepEqual(waybackTargetFromUrl("blog.example/2014/*"), { url: "blog.example/2014/", match: "prefix" }); assert.deepEqual(waybackTargetFromUrl("*.example.net"), { url: "example.net", match: "domain" }); assert.equal(waybackTargetFromUrl(""), null); assert.equal(waybackTargetFromUrl("not a url"), null); assert.equal(waybackTargetFromUrl("https://web.archive.org/"), null); assert.equal(waybackTargetHost({ url: "https://www.Example.com/x" }), "example.com"); assert.equal(waybackTargetHost({ url: "example.com/blog" }), "example.com"); }); test("an import's ask: a capture URL, or a page and a date; everything else refused with a sentence", () => { const cap = parsePageCaptureAsk("https://web.archive.org/web/20140305120000/https://blog.example/a.html"); assert.deepEqual(cap, { ok: true, ask: { kind: "capture", captureTs: "20140305120000", originalUrl: "https://blog.example/a.html" } }); const raw = parsePageCaptureAsk("https://web.archive.org/web/2014id_/http://blog.example/a.html"); assert.deepEqual(raw, { ok: true, ask: { kind: "capture", captureTs: "2014", originalUrl: "http://blog.example/a.html" } }); assert.deepEqual(parsePageCaptureAsk("https://blog.example/a.html", "20140301"), { ok: true, ask: { kind: "nearest", at: "20140301", originalUrl: "https://blog.example/a.html" }, }); const listing = parsePageCaptureAsk("https://web.archive.org/web/*/blog.example/*"); assert.equal(listing.ok, false); assert.match(!listing.ok ? listing.error : "", /a Wayback Machine listing, not one capture/); const bare = parsePageCaptureAsk("https://blog.example/a.html"); assert.match(!bare.ok ? bare.error : "", /is not a Wayback Machine capture/); const both = parsePageCaptureAsk("https://web.archive.org/web/20140305120000/https://blog.example/a.html", "2015"); assert.match(!both.ok ? both.error : "", /"at" goes with a page's own URL/); const badAt = parsePageCaptureAsk("https://blog.example/a.html", "March"); assert.match(!badAt.ok ? badAt.error : "", /"at" must be a date/); assert.equal(parsePageCaptureAsk("ftp://x.example/").ok, false); assert.equal(parsePageCaptureAsk("").ok, false); assert.equal(isCaptureAt("2014"), true); assert.equal(isCaptureAt("20140305120000"), true); assert.equal(isCaptureAt("201403051"), false); }); test("capture time as ISO; a timestamp that is no real time is none", () => { assert.equal(captureTsIso("20140305120000"), "2014-03-05T12:00:00.000Z"); assert.equal(captureTsIso("2014"), "2014-01-01T00:00:00.000Z"); assert.equal(captureTsIso("201403"), "2014-03-01T00:00:00.000Z"); assert.equal(captureTsIso("20140231000000"), ""); assert.equal(captureTsIso("abc"), ""); }); test("a video is import-video's; a channel page, an About page or a post is not a video", () => { assert.equal(videoPageOf("https://www.youtube.com/watch?v=abc"), "YouTube"); assert.equal(videoPageOf("https://youtu.be/abc"), "YouTube"); assert.equal(videoPageOf("https://www.youtube.com/shorts/abc"), "YouTube"); assert.equal(videoPageOf("https://www.youtube.com/@someone/about"), null); assert.equal(videoPageOf("https://www.youtube.com/c/someone"), null); assert.equal(videoPageOf("https://rumble.com/v4abc-a-title.html"), "Rumble"); assert.equal(videoPageOf("https://rumble.com/c/someone"), null); assert.equal(videoPageOf("https://cdn.example/clip.mp4"), "a media file"); assert.equal(videoPageOf("https://x.com/someone/status/1"), null); assert.equal(videoPageOf("https://blog.example/a.html"), null); assert.equal(isWaybackPagesUrl("https://web.archive.org/web/*/blog.example/*"), true); assert.equal(isWaybackPagesUrl("https://web.archive.org/web/20140305120000/https://blog.example/a.html"), true); assert.equal(isWaybackPagesUrl("https://web.archive.org/web/20140305120000/https://www.youtube.com/watch?v=abc"), false); assert.equal(isWaybackPagesUrl("https://blog.example/a.html"), false); assert.equal(isPageMimetype("text/html; charset=UTF-8"), true); assert.equal(isPageMimetype("application/json"), true); assert.equal(isPageMimetype("image/jpeg"), false); }); test("X: a capture of x.com/twitter.com is X's; a Wayback pages channel scoped on X is an X channel for the visibility rule", () => { assert.equal(isXHostUrl("https://twitter.com/someone"), true); assert.equal(isXHostUrl("https://mobile.twitter.com/someone/status/1"), true); assert.equal(isXHostUrl("https://x.com/someone"), true); assert.equal(isXHostUrl("https://notx.com/"), false); assert.equal(isXTargetUrl("twitter.com/someone"), true); const xPages = { sourceKind: "social" as const, platform: "webpage" as const, wayback: { targets: [{ url: "twitter.com/someone", match: "prefix" as const }] }, }; const blogPages = { sourceKind: "social" as const, platform: "webpage" as const, wayback: { targets: [{ url: "blog.example/", match: "prefix" as const }] }, }; assert.equal(isXPostsChannel(xPages), true); assert.equal(isXPostsChannel(blogPages), false); assert.equal( isXPostsChannel({ sourceKind: "social", platform: "webpage", url: "https://web.archive.org/web/*/twitter.com/someone*" }), true, ); const priv = { social: { x: { visibility: "private" } } }; assert.equal(postsVisibleTo({}, xPages, priv), false); assert.equal(postsVisibleTo({ audience: "private" }, xPages, priv), true); assert.equal(postsVisibleTo({}, blogPages, priv), true); assert.equal(postsVisibleTo({}, xPages, {}), true); });