// archive.org reads for a research run (release 27 S3b), against the test // kit's fake archive.org through the real polite client: the search, one // item's file list, its pace and its back-off. import { after, before, test } from "node:test"; import assert from "node:assert/strict"; import { startFakeArchiveOrg, type FakeArchiveOrg } from "../testing/archiveOrg/fakeArchiveOrg.mjs"; import { ArchiveOrgClient } from "../lib/archiveOrgClient"; import { describeArchiveOrgItem, searchArchiveOrgItems } from "./archiveOrgImport"; // The exact gaps: the jitter on them is tested in lib/jitter.test.ts and // beside each wait (E2E_PACING_JITTER, lib/envVars.ts). process.env.E2E_PACING_JITTER = "0"; let ia: FakeArchiveOrg; before(async () => { ia = await startFakeArchiveOrg(); }); after(() => ia.close()); function client(f: (url: string, init: RequestInit) => Promise = ia.fetch) { let now = 0; const sleeps: number[] = []; const c = new ArchiveOrgClient( { fetch: f, now: () => now, sleep: async (ms) => { sleeps.push(ms); now += ms; }, random: () => 0.5, }, {}, ); return { c, sleeps }; } test("search: identifiers and titles, in identifier order, paced 2 s apart", async () => { const { c, sleeps } = client(); const r = await searchArchiveOrgItems("bakery", { client: c, rows: 10 }); assert.equal(r.found, 2); assert.deepEqual(r.items, [ { identifier: "synth-kitchen-episodes-2026", title: "Synth Kitchen — episodes, spring 2026" }, { identifier: "synth-radio-tape-01", title: "Synth radio — the bakery call-in" }, ]); await searchArchiveOrgItems("members", { client: c }); assert.deepEqual(sleeps, [2000]); assert.match(ia.requests[0]!, /^\/advancedsearch\.php\?q=bakery&fl%5B%5D=identifier/); }); test("an item described: its files, sizes, lengths, which are the media originals, and what is restricted", async () => { const { c } = client(); const d = await describeArchiveOrgItem("synth-kitchen-episodes-2026", { client: c }); assert.equal(d.url, "https://archive.org/details/synth-kitchen-episodes-2026"); assert.equal(d.title, "Synth Kitchen — episodes, spring 2026"); assert.deepEqual(d.collections, ["opensource_movies"]); assert.equal(d.restricted, false); assert.equal(d.mediaFiles, 2); assert.equal(d.mediaBytes, 104857600 + 209715200); const byName = Object.fromEntries(d.files.map((f) => [f.name, f])); assert.deepEqual(byName["Bread day.mp4"], { name: "Bread day.mp4", format: "MPEG4", source: "original", size: 104857600, seconds: 70, media: true, restricted: false }); assert.equal(byName["Pie stream.mkv"]!.seconds, 120); assert.equal(byName["Pie stream.mp4"]!.media, false, "a derivative is the same recording"); assert.equal(byName["Pie stream.mp4"]!.original, "Pie stream.mkv"); const r = await describeArchiveOrgItem("synth-kitchen-restricted", { client: c }); assert.equal(r.restricted, true); assert.equal(r.files[0]!.restricted, true); await assert.rejects(describeArchiveOrgItem("no-such-item", { client: c })); }); test("a 429 backs off (Retry-After) and the read goes through", async () => { const busy = await startFakeArchiveOrg({ busyTimes: 1 }); try { const { c, sleeps } = client(busy.fetch); const r = await searchArchiveOrgItems("radio", { client: c }); assert.equal(r.items[0]!.identifier, "synth-radio-tape-01"); assert.deepEqual(sleeps, [1000, 1000], "Retry-After: 1, then the rest of the 2 s gap"); } finally { await busy.close(); } });