// A fake archive.org for the research tests (release 27 S3b): the advanced // search and the item metadata API, on loopback, over invented items. No test // talks to archive.org. // // const ia = await startFakeArchiveOrg(); // ITEMS below, or opts.items // new ArchiveOrgClient({ fetch: ia.fetch }, { minGapMs: 0 }) // ia.requests every request path, in order // await ia.close() // // `ia.fetch` is a fetch that sends https://archive.org/… to the fake instead, // so the real client (its gap, its retries, its URLs) is what is tested. // // Routes: // GET /advancedsearch.php?q=…&rows=N items whose identifier, title or // subject contains every word of q // (`*` matches all), identifier order; // a q of `field:value` clauses joined by // OR (release 29: a YouTube id search) // matches an item whose metadata field // contains any clause's value (quotes, // parentheses, `*` and `\` dropped) // GET /metadata/ the item, or {} (archive.org's answer // for an unknown id) // opts.busyTimes the first N requests answer 429 import http from "node:http"; // Invented items: a kitchen show's episodes, a restricted one, an audio tape. export const ITEMS = [ { metadata: { identifier: "synth-kitchen-episodes-2026", title: "Synth Kitchen — episodes, spring 2026", date: "2026-04-01", mediatype: "movies", collection: ["opensource_movies"], creator: "Synth Kitchen", subject: ["baking", "bakery"], }, files: [ { name: "Bread day.mp4", source: "original", format: "MPEG4", size: "104857600", length: "70.0" }, { name: "Pie stream.mkv", source: "original", format: "Matroska", size: "209715200", length: "02:00" }, { name: "Pie stream.mp4", source: "derivative", format: "h.264", original: "Pie stream.mkv", size: "50000000" }, { name: "Bread day.info.json", source: "original", format: "JSON", size: "2048" }, { name: "synth-kitchen-episodes-2026_meta.xml", source: "metadata", format: "Metadata", size: "900" }, ], }, { metadata: { identifier: "synth-kitchen-restricted", title: "Synth Kitchen — members stream", mediatype: "movies", collection: "opensource_movies", "access-restricted-item": "true", }, files: [{ name: "members.mp4", source: "original", format: "MPEG4", size: "5000" }], }, // A mirror of a recording deleted on its platform (release 29): the corpus's // GONE_RECORD, uploaded by a mirroring tool with the id in its identifier, // its description and its original URL. Not "bakery": the search tests // count those. { metadata: { identifier: "youtube-gone-video-05", title: "The old oven review", date: "2026-01-20", mediatype: "movies", collection: ["opensource_movies"], description: "Mirror of https://video.example/watch?v=gone-video-05", originalurl: "https://video.example/watch?v=gone-video-05", }, files: [ { name: "The old oven review-gone-video-05.webm", source: "original", format: "WebM", size: "3000000", length: "20.0" }, { name: "The old oven review-gone-video-05.mp4", source: "derivative", format: "h.264", original: "The old oven review-gone-video-05.webm", size: "2000000" }, { name: "The old oven review-gone-video-05.info.json", source: "original", format: "JSON", size: "1500" }, ], }, { metadata: { identifier: "synth-radio-tape-01", title: "Synth radio — the bakery call-in", date: "2025-11-02", mediatype: "audio", collection: ["opensource_audio"], subject: "bakery", }, files: [ { name: "tape1.flac", source: "original", format: "Flac", size: "300000000", length: "3600" }, { name: "tape1.mp3", source: "derivative", format: "VBR MP3", original: "tape1.flac", size: "60000000" }, ], }, ]; const words = (s) => String(s ?? "").toLowerCase().split(/[^a-z0-9]+/).filter(Boolean); const flat = (v) => (Array.isArray(v) ? v.join(" ") : (v ?? "")); export async function startFakeArchiveOrg(opts = {}) { const items = opts.items ?? ITEMS; const requests = []; let busy = opts.busyTimes ?? 0; const server = http.createServer((req, res) => { const u = new URL(req.url ?? "/", "http://fake"); requests.push(u.pathname + u.search); const json = (status, body, headers = {}) => { res.writeHead(status, { "content-type": "application/json", ...headers }); res.end(JSON.stringify(body)); }; if (busy > 0) { busy--; return json(429, { error: "slow down" }, { "retry-after": "1" }); } if (u.pathname === "/advancedsearch.php") { const q = u.searchParams.get("q") ?? ""; const rows = Number(u.searchParams.get("rows") ?? 50); const want = q.trim() === "*" ? [] : words(q); const clauses = q.split(/\s+OR\s+/).map((c) => c.trim().replace(/^\(+|\)+$/g, "")); const fielded = clauses.length > 0 && clauses.every((c) => /^[a-z_]+:/i.test(c)); const fieldMatch = (it) => clauses.some((c) => { const i = c.indexOf(":"); const field = c.slice(0, i); const value = c.slice(i + 1).replace(/["()*\\]/g, "").trim().toLowerCase(); return value !== "" && flat(it.metadata[field]).toLowerCase().includes(value); }); const hits = items .filter((it) => { if (fielded) return fieldMatch(it); const hay = new Set(words(`${it.metadata.identifier} ${flat(it.metadata.title)} ${flat(it.metadata.subject)}`)); return want.every((w) => hay.has(w)); }) .sort((a, b) => a.metadata.identifier.localeCompare(b.metadata.identifier)); return json(200, { response: { numFound: hits.length, docs: hits.slice(0, rows).map((it) => ({ identifier: it.metadata.identifier, title: it.metadata.title })), }, }); } const m = /^\/metadata\/([^/]+)$/.exec(u.pathname); if (m) { const it = items.find((x) => x.metadata.identifier === decodeURIComponent(m[1])); return json(200, it ?? {}); } return json(404, { error: "not found" }); }); await new Promise((r) => server.listen(0, "127.0.0.1", r)); const base = `http://127.0.0.1:${server.address().port}`; return { base, requests, fetch: (url, init) => fetch(String(url).replace(/^https:\/\/archive\.org/, base), init), close: () => new Promise((r) => server.close(() => r())), }; }