// Release 37 S5: check-post-availability on a Wayback pages channel — each // record's ORIGINAL URL asked at its own site (a stub fetch behind a web // pacer), ONE request per distinct URL however many records share it, the // verdicts and their status/reason in posts-availability.json, history only // on a change, modes and limits counted in URLs. No network. // // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test controller/checkPageAvailability.test.ts import test, { after } from "node:test"; import assert from "node:assert/strict"; import { mkdtempSync, rmSync } from "node:fs"; import { mkdir, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import type { Paths } from "../lib/paths"; import type { Post } from "../lib/posts"; import { readPostAvailability, upsertPosts } from "../lib/posts-server"; import { WebPacer } from "../lib/web/pacer"; import { checkPostAvailability } from "./checkPostAvailability"; import { selectUrlsToCheck } from "./checkPageAvailability"; const ROOT = mkdtempSync(path.join(tmpdir(), "check-pages-")); after(() => rmSync(ROOT, { recursive: true, force: true })); const paths = { channelsDir: path.join(ROOT, "channels"), transcriptsDir: ROOT } as unknown as Paths; function record(slug: string, id: string, originalUrl: string, captureTs: string): Post { return { id, slug: `${slug}/${id}`, channelSlug: slug, author: "blog.example", createdAt: "2014-03-05T12:00:00.000Z", uploadDate: "20140305", text: `the words of ${id}`, url: originalUrl, platform: "webpage", isReply: false, isRepost: false, links: [], page: { originalUrl, captureTs, waybackUrl: `https://web.archive.org/web/${captureTs}/${originalUrl}`, rawUrl: `https://web.archive.org/web/${captureTs}id_/${originalUrl}`, kind: "generic", extractor: "generic", extractorVersion: 1, }, }; } async function pagesChannel(slug: string, posts: Post[]) { const dir = path.join(paths.channelsDir, slug); await mkdir(dir, { recursive: true }); await writeFile( path.join(dir, "config.json"), JSON.stringify({ name: slug, handling: "transcribe", sourceKind: "social", platform: "webpage", socialHandle: "blog.example", url: "https://web.archive.org/web/*/blog.example/*", wayback: { targets: [{ url: "blog.example/", match: "prefix" }] }, }), ); await upsertPosts(dir, posts); return dir; } function stub(statusOf: (url: string) => number) { const sent: string[] = []; const pacer = new WebPacer( { fetch: async (url, init) => { sent.push(`${init.method} ${url}`); return new Response(null, { status: statusOf(url) }); }, sleep: async () => {}, }, { minGapMs: 0, maxAttempts: 1 }, ); return { sent, pageCheck: { pacer, resolve: async () => ["93.184.216.34"] } }; } const GONE = "https://blog.example/2014/gone.html"; const LIVE = "https://blog.example/2014/live.html"; const DOWN = "https://blog.example/2014/down.html"; test("one request per original URL; every record of it gets the verdict, with its status and reason", async () => { const dir = await pagesChannel("old-blog", [ record("old-blog", "20140305120000-aaaa", GONE, "20140305120000"), record("old-blog", "20150611090000-aaaa", GONE, "20150611090000"), record("old-blog", "20140101000000-bbbb", LIVE, "20140101000000"), record("old-blog", "20140202000000-cccc", DOWN, "20140202000000"), ]); const s = stub((url) => (url === GONE ? 404 : url === DOWN ? 500 : 200)); const lines: string[] = []; const r = await checkPostAvailability({ paths, slug: "old-blog", pageCheck: s.pageCheck, onLog: (l) => lines.push(l) }); assert.equal(r.ok, true); assert.deepEqual(s.sent.sort(), [`HEAD ${DOWN}`, `HEAD ${GONE}`, `HEAD ${LIVE}`]); assert.equal(r.checked, 4); assert.deepEqual(r.newlyDeleted.sort(), ["20140305120000-aaaa", "20150611090000-aaaa"]); const av = await readPostAvailability(dir); assert.deepEqual( [av["20140305120000-aaaa"]!.availability, av["20140305120000-aaaa"]!.status, av["20140305120000-aaaa"]!.reason], ["deleted", 404, "HTTP 404"], ); assert.equal(av["20150611090000-aaaa"]!.availability, "deleted"); assert.equal(av["20140101000000-bbbb"]!.availability, "available"); // An error is an error, never a deletion. assert.deepEqual([av["20140202000000-cccc"]!.availability, av["20140202000000-cccc"]!.status], ["error", 500]); assert.ok(lines.some((l) => /3 of 3 original URL\(s\) \(4 page record\(s\)\)/.test(l))); assert.ok(lines.some((l) => /newly GONE/.test(l))); // Again, the down page back up: history appended for the change only. const s2 = stub((url) => (url === GONE ? 404 : 200)); await checkPostAvailability({ paths, slug: "old-blog", mode: "all", pageCheck: s2.pageCheck }); const av2 = await readPostAvailability(dir); assert.equal(av2["20140202000000-cccc"]!.availability, "available"); assert.deepEqual(av2["20140202000000-cccc"]!.history?.map((h) => h.availability), ["error"]); assert.deepEqual(av2["20140305120000-aaaa"]!.history ?? [], []); // `unchecked`: nothing left to ask. const s3 = stub(() => 200); const r3 = await checkPostAvailability({ paths, slug: "old-blog", mode: "unchecked", pageCheck: s3.pageCheck }); assert.equal(r3.checked, 0); assert.equal(s3.sent.length, 0); }); test("the limit counts URLs, least recently checked first", () => { const groups = new Map([ ["https://a.example/", ["a1", "a2"]], ["https://b.example/", ["b1"]], ["https://c.example/", ["c1"]], ]); const av = { a1: { availability: "available" as const, checkedAt: "2026-10-01T00:00:00Z" }, a2: { availability: "available" as const, checkedAt: "2026-10-02T00:00:00Z" }, b1: { availability: "available" as const, checkedAt: "2026-09-01T00:00:00Z" }, }; assert.deepEqual(selectUrlsToCheck(groups, av, "stale", 2), ["https://c.example/", "https://b.example/"]); assert.deepEqual(selectUrlsToCheck(groups, av, "unchecked", 5), ["https://c.example/"]); assert.deepEqual(selectUrlsToCheck(groups, av, "all", 5).length, 3); }); test("a channel with no page records checks nothing", async () => { await pagesChannel("empty-pages", []); const s = stub(() => 200); const r = await checkPostAvailability({ paths, slug: "empty-pages", pageCheck: s.pageCheck }); assert.deepEqual([r.ok, r.checked, s.sent.length], [true, 0, 0]); });