// Release 37 lane F: the archive.today client on the fake archive.today — // timemaps, copies in every URL form, one canonical host, the long gap, a 429 // that stops and rests the client, a check page refused (never parsed), and a // check read once more through a (fake) browser. // // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test lib/archives/archiveTodayClient.test.ts import test from "node:test"; import assert from "node:assert/strict"; import { startFakeArchiveToday, wrapLikeArchiveToday, CHALLENGE_HTML, COPIES, AT_ARTICLE_ID, AT_ARTICLE_LATER_ID, AT_ARTICLE_LATER_TS, AT_ARTICLE_TS, AT_ARTICLE_URL, AT_CHECKED_ID, AT_CHECKED_TS, AT_CHECKED_URL, } from "../../testing/web/fakeArchiveToday.mjs"; import { ArchiveTodayClient, ARCHIVE_TODAY_USER_AGENT, type ArchiveTodayBrowser } from "./archiveTodayClient"; import { ArchiveRefusal } from "./types"; // A clock the client's sleeps move, so the gap is measured, not waited. function fakeClock() { let t = 1_000_000; const sleeps: number[] = []; return { now: () => t, sleep: async (ms: number) => { sleeps.push(ms); t += ms; }, sleeps, }; } async function withFake( opts: Parameters[0], fn: (at: Awaited>, clock: ReturnType) => Promise, ) { const at = await startFakeArchiveToday(opts); const clock = fakeClock(); try { await fn(at, clock); } finally { await at.close(); } } const refusalOf = async (p: Promise) => { try { await p; } catch (e) { if (e instanceof ArchiveRefusal) return e; throw e; } assert.fail("expected a refusal"); }; test("the timemap's copies, oldest first, bounded; a page it holds nothing of is none; the nearest copy", async () => { await withFake({}, async (at, clock) => { const c = new ArchiveTodayClient({ fetch: at.fetch, now: clock.now, sleep: clock.sleep, random: () => 0 }); const rows = await c.listCaptures(AT_ARTICLE_URL); assert.deepEqual( rows.map((r) => [r.captureTs, r.mementoUrl]), [ [AT_ARTICLE_TS, `https://archive.ph/${AT_ARTICLE_ID}`], [AT_ARTICLE_LATER_TS, `https://archive.ph/${AT_ARTICLE_LATER_ID}`], ], ); assert.deepEqual((await c.listCaptures(AT_ARTICLE_URL, { from: "2020" })).map((r) => r.captureTs), [AT_ARTICLE_LATER_TS]); assert.equal((await c.listCaptures(AT_ARTICLE_URL, { limit: 1 })).length, 1); assert.deepEqual(await c.listCaptures("https://nothing.example/"), []); assert.equal((await c.closestCapture(AT_ARTICLE_URL, "20191215"))?.mementoUrl, `https://archive.ph/${AT_ARTICLE_LATER_ID}`); assert.equal(await c.closestCapture("https://nothing.example/", "2019"), null); assert.ok(at.requests.every((r) => r.ua === ARCHIVE_TODAY_USER_AGENT), "identified on every request"); }); }); test("one copy by its short id, its time and page from its Memento headers; the bytes as served, the page unwrapped", async () => { await withFake({}, async (at, clock) => { const c = new ArchiveTodayClient({ fetch: at.fetch, now: clock.now, sleep: clock.sleep, random: () => 0 }); const cap = await c.getCapture(`https://archive.is/${AT_ARTICLE_ID}`); assert.equal(cap.source, "archive.today"); assert.equal(cap.finalTs, AT_ARTICLE_TS); assert.equal(cap.originalUrl, AT_ARTICLE_URL); assert.equal(cap.mementoUrl, `https://archive.ph/${AT_ARTICLE_ID}`); assert.equal(cap.mimetype, "text/html"); assert.equal(cap.unwrapped, true); assert.match(cap.body.toString("utf8"), /id="HEADER"/, "the body is the copy as served"); assert.doesNotMatch(cap.page, /id="HEADER"/, "the page is unwrapped"); assert.equal(cap.bytes, cap.body.byteLength); assert.match(cap.sha256, /^[0-9a-f]{64}$/); // Asked on archive.ph whatever name the URL used. assert.deepEqual(at.requests.map((r) => r.path), [`/${AT_ARTICLE_ID}`]); }); }); test("a timestamp URL, newest and oldest: redirected inside archive.today to the copy", async () => { await withFake({}, async (at, clock) => { const c = new ArchiveTodayClient({ fetch: at.fetch, now: clock.now, sleep: clock.sleep, random: () => 0 }); assert.equal((await c.getCapture(`https://archive.ph/2019.12.20-000000/${AT_ARTICLE_URL}`)).mementoUrl, `https://archive.ph/${AT_ARTICLE_LATER_ID}`); assert.equal((await c.getCapture(`https://archive.ph/oldest/${AT_ARTICLE_URL}`)).finalTs, AT_ARTICLE_TS); assert.equal((await c.getCapture(`https://archive.ph/newest/${AT_ARTICLE_URL}`)).finalTs, AT_ARTICLE_LATER_TS); const none = await refusalOf(c.getCapture("https://archive.ph/Zz0zZ")); assert.equal(none.code, "not-archived"); }); }); test("ONE stream, the long gap from the end of one request to the start of the next, jittered", async () => { await withFake({}, async (at, clock) => { const c = new ArchiveTodayClient({ fetch: at.fetch, now: clock.now, sleep: clock.sleep, random: () => 0.5 }, { minGapMs: 8_000, jitter: 0.25 }); await Promise.all([c.listCaptures(AT_ARTICLE_URL), c.getCapture(`https://archive.ph/${AT_ARTICLE_ID}`), c.getCapture(`https://archive.ph/${AT_ARTICLE_LATER_ID}`)]); assert.equal(c.requests, 3); // The first request waits for nothing; each later one 8 s × (1 + 0.5 × 0.25). assert.deepEqual(clock.sleeps, [9_000, 9_000]); }); }); test("a 429 stops the run with a sentence and rests the client: nothing more is sent until the cool-down ends", async () => { await withFake({ busyTimes: 1 }, async (at, clock) => { const c = new ArchiveTodayClient({ fetch: at.fetch, now: clock.now, sleep: clock.sleep, random: () => 0 }, { cooldownMs: 30 * 60_000 }); const r = await refusalOf(c.getCapture(`https://archive.ph/${AT_ARTICLE_ID}`)); assert.equal(r.code, "rate-limited"); assert.match(r.message, /HTTP 429 \(too many requests\).*stopped.*30 min/); assert.equal(at.requests.length, 1, "no retry of a 429"); const again = await refusalOf(c.listCaptures(AT_ARTICLE_URL)); assert.equal(again.code, "rate-limited"); assert.match(again.message, /was not asked/); assert.equal(at.requests.length, 1, "nothing sent during the cool-down"); assert.ok(c.cooldownUntil > clock.now()); await clock.sleep(31 * 60_000); assert.equal((await c.listCaptures(AT_ARTICLE_URL)).length, 2); }); }); test("a check page is a typed refusal and never a record; it names Connect archive session", async () => { for (const challengeStatus of [200, 429, 503]) { await withFake({ challenge: [AT_CHECKED_ID], challengeStatus }, async (at, clock) => { const c = new ArchiveTodayClient({ fetch: at.fetch, now: clock.now, sleep: clock.sleep, random: () => 0 }); const r = await refusalOf(c.getCapture(`https://archive.ph/${AT_CHECKED_ID}`)); assert.equal(r.code, "challenge", `status ${challengeStatus}`); assert.match(r.message, /a CAPTCHA instead of the copy; nothing was read\. Connect archive session/); assert.equal(c.cooldownUntil, 0, "a check is not a rate limit"); }); } }); test("a check read once more through the browser: cleared there, the copy is read from the page; still there, refused", async () => { await withFake({ challenge: [AT_CHECKED_ID] }, async (at, clock) => { const wrapped = wrapLikeArchiveToday(COPIES[2].page(), { id: AT_CHECKED_ID, original: AT_CHECKED_URL, timestamp: AT_CHECKED_TS }); let opened = 0; let closed = 0; const cleared = async (): Promise => { opened++; return { load: async (url) => ({ html: wrapped, text: "", url }), close: async () => { closed++; }, }; }; const c = new ArchiveTodayClient({ fetch: at.fetch, now: clock.now, sleep: clock.sleep, random: () => 0, openBrowser: cleared }); const cap = await c.getCapture(`https://archive.ph/${AT_CHECKED_ID}`); assert.equal(cap.viaBrowser, true); assert.equal(cap.finalTs, AT_CHECKED_TS, "time read from the copy's header bar"); assert.equal(cap.originalUrl, AT_CHECKED_URL); assert.match(cap.page, /A page held behind a check/); assert.equal(c.browserLoads, 1); assert.deepEqual([opened, closed], [1, 1], "the browser is closed after the one read"); const stillChecked = new ArchiveTodayClient({ fetch: at.fetch, now: clock.now, sleep: clock.sleep, random: () => 0, openBrowser: async () => ({ load: async (url) => ({ html: CHALLENGE_HTML, text: "", url }), close: async () => {} }), }); const r = await refusalOf(stillChecked.getCapture(`https://archive.ph/${AT_CHECKED_ID}`)); assert.equal(r.code, "challenge"); assert.match(r.message, /in the headless browser too/); const off = new ArchiveTodayClient({ fetch: at.fetch, now: clock.now, sleep: clock.sleep, random: () => 0, openBrowser: cleared, browserEnabled: () => false }); assert.equal((await refusalOf(off.getCapture(`https://archive.ph/${AT_CHECKED_ID}`))).code, "challenge"); assert.equal(opened, 1, "a browser turned off in settings is not opened"); }); }); test("the canonical host comes from settings; every name is sent there", async () => { await withFake({}, async (at, clock) => { const hosts: string[] = []; const c = new ArchiveTodayClient({ fetch: (url, init) => { hosts.push(new URL(url).hostname); return at.fetch(url, init); }, now: clock.now, sleep: clock.sleep, random: () => 0, host: () => "archive.li", }); const cap = await c.getCapture(`https://archive.vn/oldest/${AT_ARTICLE_URL}`); assert.equal(cap.mementoUrl, `https://archive.li/${AT_ARTICLE_ID}`); assert.deepEqual([...new Set(hosts)], ["archive.li"]); }); }); test("an answer that is not a page, and one over the cap, are refused", async () => { const fetchOf = (body: string, type: string) => async () => new Response(body, { status: 200, headers: { "content-type": type, "memento-datetime": "Sun, 12 May 2019 08:30:00 GMT" } }); const clock = fakeClock(); const img = new ArchiveTodayClient({ fetch: fetchOf("x", "image/png"), now: clock.now, sleep: clock.sleep }); assert.equal((await refusalOf(img.getCapture("https://archive.ph/Ab3dE"))).code, "mimetype"); const big = new ArchiveTodayClient({ fetch: fetchOf("x".repeat(5000), "text/html"), now: clock.now, sleep: clock.sleep }); assert.equal((await refusalOf(big.getCapture("https://archive.ph/Ab3dE", { maxBytes: 1000 }))).code, "too-large"); const nameless = new ArchiveTodayClient({ fetch: async () => new Response("a page", { status: 200, headers: { "content-type": "text/html" } }), now: clock.now, sleep: clock.sleep, }); assert.equal((await refusalOf(nameless.getCapture("https://archive.ph/Ab3dE"))).code, "unreadable"); const away = new ArchiveTodayClient({ fetch: async () => new Response("", { status: 302, headers: { location: "https://elsewhere.example/" } }), now: clock.now, sleep: clock.sleep, }); assert.equal((await refusalOf(away.getCapture("https://archive.ph/Ab3dE"))).code, "offsite-redirect"); });