// The site-extractor registry: each synthetic capture goes to the extractor // made for it, an unknown page to none (the generic extractor's), and every // extractor is pure — the same input gives the same drafts, and the input is // left as it was. // // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test lib/waybackPages/extractors/index.test.ts import { test } from "node:test"; import assert from "node:assert/strict"; import { SITE_EXTRACTORS } from "./index"; import { PAGE_KINDS } from "../types"; import { captureOf, fixture } from "./__fixtures__/capture"; import { LIVE_PAGE, LOGIN_WALL, REPLAY_PAGE, THREAD_URL } from "./__fixtures__/xenforoCaptures"; const GDOC = "https://docs.google.com/document/d/1AbCdEfGhIjKlMnOpQrStUvWxYz0123456789"; const CASES: [html: string, url: string, expected: string | null][] = [ [fixture("tumblr-2012-classic-post.html"), "http://teapot-notes.tumblr.com/post/20000000001/on-spouts", "tumblr"], [fixture("tumblr-2012-classic-index.html"), "http://teapot-notes.tumblr.com/", "tumblr"], [fixture("tumblr-2014-article-index.html"), "http://blog.kettle-studio.example/page/2", "tumblr"], [fixture("tumblr-minimal-tagged.html"), "http://quiet-kettle.tumblr.com/tagged/steam", "tumblr"], [fixture("x-2015-profile.html"), "https://twitter.com/TeapotFan", "x"], [fixture("x-2012-profile.html"), "https://twitter.com/kettlefriend", "x"], [fixture("x-2016-status.html"), "https://twitter.com/TeapotFan/status/697374449464250369", "x"], [fixture("x-mobile-profile.html"), "https://mobile.twitter.com/TeapotFan", "x"], [fixture("x-2022-shell.html"), "https://x.com/TeapotFan", "x"], [LIVE_PAGE, `${THREAD_URL}page-2`, "xenforo"], [REPLAY_PAGE, `${THREAD_URL}page-2`, "xenforo"], [LOGIN_WALL, `${THREAD_URL}page-2`, "xenforo"], [fixture("gdoc-mobilebasic.html"), `${GDOC}/mobilebasic`, "gdoc"], [fixture("gdoc-pub.html"), "https://docs.google.com/document/d/e/2PACX-1vTeapot/pub?embedded=true", "gdoc"], [fixture("gdoc-edit-shell.html"), `${GDOC}/edit`, "gdoc"], [fixture("yt-about-2013.html"), "http://www.youtube.com/user/teapotchannel/about", "yt-about"], [fixture("yt-about-2019-polymer.html"), "https://www.youtube.com/channel/UCteapot0000000000000001/about", "yt-about"], [fixture("yt-channel-2021.html"), "https://www.youtube.com/c/TeapotChannel", "yt-about"], [fixture("yt-about-2024-handle.html"), "https://www.youtube.com/@TeapotChannel/about", "yt-about"], [fixture("ng-user-2014.html"), "http://pocketfox.newgrounds.com/", "newgrounds"], [fixture("ng-user-2010.html"), "http://pocketfox.newgrounds.com/", "newgrounds"], [fixture("ng-art-2014.html"), "http://www.newgrounds.com/art/view/pocketfox/lighthouse-at-dusk", "newgrounds"], [fixture("ng-movie-2014.html"), "http://www.newgrounds.com/portal/view/634512", "newgrounds"], // Pages no site extractor claims: an article, a personal site, a video page. [`A story

A story

Words.

`, "https://news.example/2014/05/a-story", null], [`

My homepage

a post`, "http://home.example/", null], [`video`, "https://www.youtube.com/watch?v=AbCdEfGhIjK", null], ]; const pick = (html: string, url: string) => SITE_EXTRACTORS.find((e) => e.matches(new URL(url), html))?.name ?? null; test("the registry sends each capture to its extractor, and an unknown page to none", () => { for (const [html, url, expected] of CASES) assert.equal(pick(html, url), expected, url); }); test("the registry: six extractors, unique names, version 1, most specific first", () => { assert.deepEqual(SITE_EXTRACTORS.map((e) => e.name), ["gdoc", "yt-about", "x", "newgrounds", "xenforo", "tumblr"]); for (const e of SITE_EXTRACTORS) assert.equal(e.version, 1); }); test("every extractor is pure: the same drafts twice, kinds from the contract, ids unique per capture", () => { for (const [html, url, expected] of CASES) { if (!expected) continue; const ex = SITE_EXTRACTORS.find((e) => e.name === expected)!; const input = { html, capture: captureOf(url, "20200101000000") }; const before = JSON.stringify(input); const a = ex.extract(input); const b = ex.extract(input); assert.deepEqual(a, b, url); assert.equal(JSON.stringify(input), before, url); assert.ok(a.length > 0, url); for (const d of a) { assert.ok((PAGE_KINDS as readonly string[]).includes(d.kind), `${url}: ${d.kind}`); assert.equal(typeof d.text, "string"); assert.ok(Array.isArray(d.links)); } const ids = a.map((d) => d.itemId).filter(Boolean); assert.equal(new Set(ids).size, ids.length, url); } });