// Release 37 lane F: `import-page` of an archive.today copy end to end on the // fake archive.today — the copy by its short id and nearest a date, stored AS // SERVED with its source, records that say where they are from, never fetched // twice, a check refused with its code and nothing written, and a re-extract // that reads the stored copy unwrapped. Also the Wayback exclusion (a CDX 403) // as an import's typed refusal. // // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test controller/importPage.archiveToday.test.ts import test, { after } from "node:test"; import assert from "node:assert/strict"; import { mkdtempSync, rmSync } from "node:fs"; import { mkdir, readFile, writeFile } from "node:fs/promises"; import { gunzipSync } from "node:zlib"; import { tmpdir } from "node:os"; import path from "node:path"; import { startFakeArchiveToday, fixtureCopy, COPIES, AT_ARTICLE_ID, AT_ARTICLE_LATER_ID, AT_ARTICLE_LATER_TS, AT_ARTICLE_TS, AT_ARTICLE_URL, AT_CHECKED_ID, } from "../testing/web/fakeArchiveToday.mjs"; import { startFakeWayback } from "../testing/web/fakeWayback.mjs"; import type { Paths } from "../lib/paths"; import { ArchiveTodayClient } from "../lib/archives/archiveTodayClient"; import { WaybackClient } from "../lib/waybackClient"; import { importPage, importPageQueueOf, planImportPage } from "./importPage"; import { reextractPages } from "./reextractPages"; import { readAllPosts } from "../lib/posts-server"; import { pageRecordId } from "../lib/waybackPages/types"; import { listStoredCaptures, pageCaptureDir } from "../lib/waybackPages/store-server"; import { pageArchiveLink } from "../lib/archives/mementos"; const ROOT = mkdtempSync(path.join(tmpdir(), "import-page-at-")); after(() => rmSync(ROOT, { recursive: true, force: true })); const paths = { channelsDir: path.join(ROOT, "channels"), transcriptsDir: ROOT, jobsDir: path.join(ROOT, "jobs") } as unknown as Paths; const PUBLIC = { social: { x: { visibility: "public" } } }; async function pagesChannel(slug: string, target: string) { const dir = path.join(paths.channelsDir, slug); await mkdir(dir, { recursive: true }); await writeFile( path.join(dir, "config.json"), JSON.stringify({ handling: "transcribe", name: slug, sourceKind: "social", platform: "webpage", socialHandle: target.split("/")[0], url: `https://web.archive.org/web/*/${target}*`, wayback: { targets: [{ url: target, match: "prefix", sources: ["wayback", "archive.today"] }] }, }), ); return dir; } const TWEET_URL = "https://twitter.com/TeapotFan/status/697374449464250369"; const TWEET_ID = "Tw33t"; const TWEET_TS = "20160211000000"; async function withFake(fn: (client: ArchiveTodayClient, at: Awaited>) => Promise, opts: Parameters[0] = {}) { const at = await startFakeArchiveToday({ copies: [...COPIES, fixtureCopy("x-2016-status.html", { id: TWEET_ID, original: TWEET_URL, timestamp: TWEET_TS })], ...opts, }); try { await fn(new ArchiveTodayClient({ fetch: at.fetch, sleep: async () => {} }, { minGapMs: 0 }), at); } finally { await at.close(); } } test("planned per source: an archive.today copy runs on its own queue; a short id is checked again after the read", async () => { await pagesChannel("deleted-journal", "journal.example/"); const p = await planImportPage(paths, { slug: "deleted-journal", url: `https://archive.ph/${AT_ARTICLE_ID}`, settings: PUBLIC }); assert.ok(p.ok); assert.equal(importPageQueueOf(p.plan.ask), "platform:archive.today"); const w = await planImportPage(paths, { slug: "deleted-journal", url: "https://web.archive.org/web/2019/https://journal.example/", settings: PUBLIC }); assert.ok(w.ok); assert.equal(importPageQueueOf(w.plan.ask), "platform:archive.org"); const video = await planImportPage(paths, { slug: "deleted-journal", url: "https://archive.ph/newest/https://www.youtube.com/watch?v=abcdefghijk", settings: PUBLIC }); assert.equal(video.ok, false); assert.match(video.ok ? "" : video.error, /use import-video/); }); test("a copy by its short id: stored as served with its source, records that name archive.today, never fetched twice", async () => { const channelRoot = await pagesChannel("deleted-journal", "journal.example/"); await withFake(async (client, at) => { const dry = await importPage({ paths, slug: "deleted-journal", url: `https://archive.is/${AT_ARTICLE_ID}`, dryRun: true, archiveTodayClient: client, settings: PUBLIC }); assert.equal(dry.ok, true); assert.match(String(dry.wouldFetch), new RegExp(`https://archive\\.ph/${AT_ARTICLE_ID}`)); assert.equal(at.requests.length, 0, "a dry run asks nothing"); const res = await importPage({ paths, slug: "deleted-journal", url: `https://archive.is/${AT_ARTICLE_ID}`, archiveTodayClient: client, settings: PUBLIC }); assert.equal(res.ok, true, res.error); assert.equal(res.source, "archive.today"); assert.equal(res.captureTs, AT_ARTICLE_TS); assert.equal(res.originalUrl, AT_ARTICLE_URL); assert.equal(res.mementoUrl, `https://archive.ph/${AT_ARTICLE_ID}`); assert.equal(res.written, 1); const dir = pageCaptureDir(channelRoot, AT_ARTICLE_TS, AT_ARTICLE_URL); const capture = JSON.parse(await readFile(path.join(dir, "capture.json"), "utf8")) as Record; assert.equal(capture.source, "archive.today"); assert.equal(capture.mementoUrl, `https://archive.ph/${AT_ARTICLE_ID}`); assert.equal(capture.extractor, "generic"); const raw = gunzipSync(await readFile(path.join(dir, "raw.html.gz"))).toString("utf8"); assert.match(raw, /id="HEADER"/, "the copy as served, bar and all"); const [post] = (await readAllPosts(channelRoot)).filter((p) => p.page); assert.equal(post.id, pageRecordId(AT_ARTICLE_TS, AT_ARTICLE_URL)); assert.equal(post.platform, "webpage"); assert.equal(post.url, AT_ARTICLE_URL); assert.equal(post.author, "Sam Example"); assert.doesNotMatch(post.text, /webpage capture|Saved from/); assert.equal(post.page?.source, "archive.today"); assert.equal(post.page?.archiveUrl, `https://archive.ph/${AT_ARTICLE_ID}`); assert.equal(post.page?.waybackUrl, `https://archive.ph/${AT_ARTICLE_ID}`); assert.deepEqual(pageArchiveLink(post.page!), { source: "archive.today", label: "archive.today", url: `https://archive.ph/${AT_ARTICLE_ID}` }); assert.match(String(post.media?.[0]?.url), new RegExp(`^https://archive\\.ph/${AT_ARTICLE_ID}/`), "the image is archive.today's cached copy"); assert.deepEqual(post.links, ["https://journal.example/2019/05/minutes.html", "https://other.example/account"]); // Again, by another of its names: nothing fetched. const n = at.requests.length; const again = await importPage({ paths, slug: "deleted-journal", url: `https://archive.ph/${AT_ARTICLE_ID}`, archiveTodayClient: client, settings: PUBLIC }); assert.equal(again.already, true); assert.equal(at.requests.length, n); const dryAgain = await importPage({ paths, slug: "deleted-journal", url: `https://archive.li/${AT_ARTICLE_ID}`, dryRun: true, archiveTodayClient: client, settings: PUBLIC }); assert.equal(dryAgain.already, true); }); }); test("the copy nearest a date (one timemap request), a later version of the page; re-extract reads the stored copy unwrapped", async () => { const channelRoot = await pagesChannel("deleted-journal", "journal.example/"); await withFake(async (client, at) => { const res = await importPage({ paths, slug: "deleted-journal", url: AT_ARTICLE_URL, at: "20191201", source: "archive.today", archiveTodayClient: client, settings: PUBLIC }); assert.equal(res.ok, true, res.error); assert.equal(res.captureTs, AT_ARTICLE_LATER_TS); assert.equal(res.mementoUrl, `https://archive.ph/${AT_ARTICLE_LATER_ID}`); assert.deepEqual(at.requests.map((r) => r.path), [`/timemap/${AT_ARTICLE_URL}`, `/${AT_ARTICLE_LATER_ID}`]); const posts = (await readAllPosts(channelRoot)).filter((p) => p.page); const later = posts.find((p) => p.page!.captureTs === AT_ARTICLE_LATER_TS)!; assert.equal(later.page?.versionOf, pageRecordId(AT_ARTICLE_TS, AT_ARTICLE_URL), "a later copy is a version of the first held"); assert.match(later.text, /a second meeting is planned/); const re = await reextractPages({ paths, slug: "deleted-journal" }); assert.equal(re.ok, true, re.error); assert.equal(re.stale, 0, "nothing stale: the stored copies read the same unwrapped"); assert.equal(re.records.changed, 0); assert.equal(re.records.removed, 0); assert.equal((await listStoredCaptures(channelRoot)).every((e) => e.capture.source === "archive.today"), true); }); }); test("a tweet held by archive.today goes to the X extractor, as from the Wayback Machine", async () => { const channelRoot = await pagesChannel("teapot-x", "twitter.com/TeapotFan"); await withFake(async (client) => { const res = await importPage({ paths, slug: "teapot-x", url: `https://archive.ph/${TWEET_ID}`, archiveTodayClient: client, settings: PUBLIC }); assert.equal(res.ok, true, res.error); assert.match(String(res.extractor), /^x v\d+$/); const tweets = (await readAllPosts(channelRoot)).filter((p) => p.page?.kind === "tweet"); assert.ok(tweets.length > 0); assert.ok(tweets.every((t) => t.page?.source === "archive.today")); }); }); test("a check: refused with its code, nothing stored; a 429 refused as rate-limited", async () => { const channelRoot = await pagesChannel("checked-pages", "checked.example/"); await withFake( async (client) => { const res = await importPage({ paths, slug: "checked-pages", url: `https://archive.ph/${AT_CHECKED_ID}`, archiveTodayClient: client, settings: PUBLIC }); assert.equal(res.ok, false); assert.equal(res.refusal, "challenge"); assert.equal(res.source, "archive.today"); assert.match(String(res.error), /Connect archive session/); assert.deepEqual(await listStoredCaptures(channelRoot), []); assert.deepEqual(await readAllPosts(channelRoot), []); }, { challenge: [AT_CHECKED_ID] }, ); await withFake( async (client) => { const res = await importPage({ paths, slug: "checked-pages", url: `https://archive.ph/${AT_CHECKED_ID}`, archiveTodayClient: client, settings: PUBLIC }); assert.equal(res.refusal, "rate-limited"); assert.match(String(res.error), /too many requests/); }, { busyTimes: 1 }, ); }); test("a Wayback exclusion (a CDX 403) is an import's typed refusal", async () => { await pagesChannel("excluded-pages", "gone.example/"); const wb = await startFakeWayback({ excluded: ["https://gone.example/profile"] }); try { const client = new WaybackClient({ fetch: wb.fetch }, { minGapMs: 0 }); const res = await importPage({ paths, slug: "excluded-pages", url: "https://gone.example/profile", at: "2014", client, settings: PUBLIC }); assert.equal(res.ok, false); assert.equal(res.refusal, "excluded"); assert.match(String(res.error), /excluded from the Wayback Machine \(cause unknown\)/); } finally { await wb.close(); } });