// Release 37: the dispatcher over the REGISTERED site extractors (lane C's) // with their synthetic fixtures — each capture reaches its own extractor, an // unknown page reaches the generic one — and their drafts as page records // (an item's own id; a shell or a document one record per capture). // // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test lib/waybackPages/extract.test.ts import test from "node:test"; import assert from "node:assert/strict"; import { chooseExtractor, extractPage } from "./extract"; import { SITE_EXTRACTORS } from "./extractors/index"; import { captureOf, fixture } from "./extractors/__fixtures__/capture"; import { draftToPost, isWholePageRecord } from "./toPost"; import { pageRecordId } from "./types"; const input = (file: string, url: string, ts: string) => ({ html: fixture(file), capture: captureOf(url, ts) }); test("a registered site's capture reaches its extractor; an unknown page reaches generic", () => { const cases: [string, string, string, string][] = [ ["tumblr-2012-classic-post.html", "http://teapot-notes.tumblr.com/post/20000000001/on-spouts", "20120601000000", "tumblr"], ["x-2016-status.html", "https://twitter.com/TeapotFan/status/697374449464250369", "20160211000000", "x"], ["yt-about-2013.html", "http://www.youtube.com/user/teapotchannel/about", "20131001000000", "yt-about"], ]; for (const [file, url, ts, want] of cases) { const i = input(file, url, ts); assert.equal(chooseExtractor(i).name, want, file); const res = extractPage(i); assert.equal(res.extractor.name, want, file); assert.equal(res.fallback, undefined, file); assert.ok(res.drafts.length > 0, file); } const unknown = { html: "

An ordinary page with words enough to read.

", capture: captureOf("https://plain.example/page", "20150101000000") }; assert.equal(chooseExtractor(unknown).name, "generic"); assert.equal(extractPage(unknown).drafts[0].kind, "generic"); assert.ok(SITE_EXTRACTORS.length >= 5); }); test("site drafts as page records: a tweet is its own record with X's refs; a shell is one record per capture", () => { const ts = "20160211000000"; const url = "https://twitter.com/TeapotFan/status/697374449464250369"; const res = extractPage(input("x-2016-status.html", url, ts)); const posts = res.drafts.map((d) => draftToPost(d, { channelSlug: "pages", capture: { originalUrl: url, captureTs: ts }, extractor: res.extractor }), ); const tweet = posts.find((p) => p.id === "tweet-697374449464250369"); assert.ok(tweet, posts.map((p) => p.id).join(", ")); assert.equal(tweet!.platform, "webpage"); assert.equal(tweet!.page?.kind, "tweet"); assert.equal(tweet!.page?.firstSeenTs, ts); assert.equal(isWholePageRecord(tweet!), false); const shellTs = "20221101000000"; const shellUrl = "https://twitter.com/TeapotFan"; const shell = extractPage(input("x-2022-shell.html", shellUrl, shellTs)); const [rec] = shell.drafts.map((d) => draftToPost(d, { channelSlug: "pages", capture: { originalUrl: shellUrl, captureTs: shellTs }, extractor: shell.extractor }), ); assert.equal(rec.id, pageRecordId(shellTs, shellUrl)); assert.equal(rec.page?.noContent, true); assert.equal(isWholePageRecord(rec), true); });