// Release 37 S5: a page record's ORIGINAL — the marker table, the verdict on // one answer, the same-site rule, grouping by original URL, and the request // side (HEAD, then GET) through a web pacer over a stub fetch. No network. // // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test lib/waybackPages/liveness.test.ts import test from "node:test"; import assert from "node:assert/strict"; import { classifyOriginalAnswer, foldPageText, groupByOriginalUrl, hasGoneMarkers, PAGE_GONE_MARKERS, pageGoneMarker, sameSite, siteOf, } from "./liveness"; import { checkOriginalUrl } from "./liveness-server"; import { WebPacer } from "../web/pacer"; // Synthetic "gone" pages: each marker's words in the platform's own furniture. const page = (words: string) => `t

${words}

Try something else.

`; const MARKER_CASES: { id: string; url: string; html: string }[] = [ { id: "x-no-account", url: "https://x.com/someone", html: page("This account doesn’t exist") }, { id: "x-no-page", url: "https://twitter.com/someone/status/1", html: page("Hmm...this page doesn’t exist. Try searching for something else.") }, { id: "x-suspended", url: "https://mobile.twitter.com/someone", html: page("Account suspended") }, { id: "tumblr-nothing-here", url: "https://someone.tumblr.com/post/123", html: page("There's nothing here.") }, { id: "tumblr-nothing-here", url: "https://www.tumblr.com/someone", html: page("Whatever you were looking for doesn't currently exist at this address. Unless you were looking for this error page, in which case: Congrats!") }, { id: "gdocs-no-file", url: "https://docs.google.com/document/d/abc/edit", html: page("Sorry, the file you have requested does not exist.") }, { id: "gdocs-permission", url: "https://drive.google.com/file/d/abc/view", html: page("You need access") }, { id: "youtube-no-channel", url: "https://www.youtube.com/@someone/about", html: page("This channel does not exist.") }, { id: "youtube-terminated", url: "https://m.youtube.com/channel/UCabc", html: page("This account has been terminated for a violation of YouTube's Terms of Service.") }, ]; test("the marker table: every platform's gone page is told by its words, on its own hosts only", () => { for (const c of MARKER_CASES) { assert.equal(pageGoneMarker(c.url, c.html)?.id, c.id, c.url); } // Every marker has a case. assert.deepEqual(new Set(MARKER_CASES.map((c) => c.id)), new Set(PAGE_GONE_MARKERS.map((m) => m.id))); // The same words elsewhere are not a gone page (a blog quoting X's message). assert.equal(pageGoneMarker("https://blog.example/post", page("This account doesn't exist")), null); assert.equal(pageGoneMarker("https://example.tumblr.net/", page("There's nothing here.")), null); // A live page on the platform is not one either. assert.equal(pageGoneMarker("https://someone.tumblr.com/post/1", page("A post about bread")), null); // Words inside a script are not the page's. assert.equal(pageGoneMarker("https://x.com/a", `

hello

`), null); assert.equal(hasGoneMarkers("https://www.youtube.com/@a/about"), true); assert.equal(hasGoneMarkers("https://blog.example/"), false); assert.equal(foldPageText("

Doesn’t  EXIST

"), "doesn't exist"); }); test("one answer's verdict: 404/410 deleted, 2xx available on the same site, a marker, anything else an error with its status", () => { const v = (url: string, finalUrl: string, status: number, body?: string) => classifyOriginalAnswer({ url, finalUrl, status, ...(body !== undefined ? { body } : {}) }); assert.deepEqual( [v("https://a.example/x", "https://a.example/x", 404).availability, v("https://a.example/x", "https://a.example/x", 410).availability], ["deleted", "deleted"], ); const ok = v("http://a.example/x", "https://www.a.example/x/", 200); assert.equal(ok.availability, "available"); assert.equal(ok.finalUrl, "https://www.a.example/x/"); const off = v("https://a.example/x", "https://parked.example/", 200); assert.equal(off.availability, "error"); assert.match(off.reason, /redirects to another site \(parked\.example\)/); const gone = v("https://someone.tumblr.com/post/1", "https://www.tumblr.com/someone", 200, page("There's nothing here.")); assert.deepEqual([gone.availability, gone.reason], ["deleted", "Tumblr says there's nothing here"]); const walled = v("https://x.com/a", "https://x.com/a", 403); assert.deepEqual([walled.availability, walled.status, walled.reason], ["error", 403, "HTTP 403"]); assert.equal(v("https://a.example/", "https://a.example/", 500).availability, "error"); // A gone page behind another status still says so. assert.equal(v("https://docs.google.com/d/1", "https://docs.google.com/d/1", 401, page("You need permission")).availability, "account_unavailable"); }); test("same site: www, http→https, a subdomain of the same domain, two-label suffixes, moved houses", () => { assert.equal(sameSite("http://blog.example/", "https://www.blog.example/a"), true); assert.equal(sameSite("https://someone.tumblr.com/", "https://www.tumblr.com/someone"), true); assert.equal(sameSite("https://twitter.com/a", "https://x.com/a"), true); assert.equal(sameSite("https://a.example.co.uk/", "https://b.example.co.uk/"), true); assert.equal(sameSite("https://a.co.uk/", "https://b.co.uk/"), false); assert.equal(sameSite("https://blog.example/", "https://other.example/"), false); assert.equal(siteOf("www.news.bbc.co.uk"), "bbc.co.uk"); }); test("records grouped by original URL: one request per URL, whatever a capture split into", () => { const rec = (id: string, originalUrl?: string) => ({ id, ...(originalUrl ? { page: { originalUrl } } : {}) }); const g = groupByOriginalUrl([rec("t1", "https://x.com/a"), rec("p1", "https://blog.example/"), rec("t2", "https://x.com/a"), rec("none")]); assert.deepEqual([...g.entries()], [ ["https://x.com/a", ["t1", "t2"]], ["https://blog.example/", ["p1"]], ]); }); // ─── The request side, over a stub fetch ─── type Route = (method: string, url: string) => { status: number; headers?: Record; body?: string }; function stubPacer(route: Route) { const sent: string[] = []; const pacer = new WebPacer( { fetch: async (url, init) => { const method = (init.method ?? "GET").toUpperCase(); sent.push(`${method} ${url}`); const r = route(method, url); return new Response(method === "HEAD" ? null : (r.body ?? ""), { status: r.status, headers: r.headers ?? {} }); }, sleep: async () => {}, }, { minGapMs: 0, maxAttempts: 2 }, ); // Every public name resolves to a public address; nothing is looked up. return { pacer, sent, opts: { pacer, resolve: async () => ["93.184.216.34"] } }; } test("checkOriginalUrl: a HEAD is enough for a plain page; a refused HEAD is asked again as a GET", async () => { const live = stubPacer(() => ({ status: 200 })); assert.equal((await checkOriginalUrl("https://blog.example/a", live.opts)).availability, "available"); assert.deepEqual(live.sent, ["HEAD https://blog.example/a"]); const refused = stubPacer((m) => (m === "HEAD" ? { status: 405 } : { status: 200, body: "

hi

" })); const r = await checkOriginalUrl("https://blog.example/a", refused.opts); assert.equal(r.availability, "available"); assert.deepEqual(refused.sent, ["HEAD https://blog.example/a", "GET https://blog.example/a"]); const gone = stubPacer(() => ({ status: 410 })); assert.deepEqual( [(await checkOriginalUrl("https://blog.example/a", gone.opts)).availability, gone.sent.length], ["deleted", 1], ); }); test("checkOriginalUrl: redirects followed by hand; a platform with a gone page is read with a GET", async () => { const s = stubPacer((m, url) => { if (url === "https://someone.tumblr.com/post/1") return { status: 301, headers: { location: "https://www.tumblr.com/someone/1" } }; return m === "HEAD" ? { status: 200 } : { status: 200, body: page("There’s nothing here.") }; }); const r = await checkOriginalUrl("https://someone.tumblr.com/post/1", s.opts); assert.deepEqual([r.availability, r.finalUrl, r.reason], ["deleted", "https://www.tumblr.com/someone/1", "Tumblr says there's nothing here"]); assert.deepEqual(s.sent, [ "HEAD https://someone.tumblr.com/post/1", "HEAD https://www.tumblr.com/someone/1", "GET https://someone.tumblr.com/post/1", "GET https://www.tumblr.com/someone/1", ]); const off = stubPacer((_m, url) => (url.includes("blog.example") ? { status: 302, headers: { location: "https://parked.example/" } } : { status: 200 })); const o = await checkOriginalUrl("https://blog.example/", off.opts); assert.equal(o.availability, "error"); assert.match(o.reason, /another site/); }); test("checkOriginalUrl: an error never reads as deleted — a 403 wall, a 503 that stays, a private address, no answer", async () => { const wall = stubPacer(() => ({ status: 403 })); const w = await checkOriginalUrl("https://blog.example/", wall.opts); assert.deepEqual([w.availability, w.status], ["error", 403]); assert.equal(wall.sent.length, 2, "HEAD, then GET"); const down = stubPacer(() => ({ status: 503 })); const d = await checkOriginalUrl("https://blog.example/", down.opts); assert.deepEqual([d.availability, d.status], ["error", 503]); const inside = stubPacer(() => ({ status: 200 })); const p = await checkOriginalUrl("http://127.0.0.1:9/", inside.opts); assert.equal(p.availability, "error"); assert.match(p.reason, /private address/); assert.equal(inside.sent.length, 0); const dead = new WebPacer({ fetch: async () => { throw new Error("getaddrinfo ENOTFOUND"); }, sleep: async () => {} }, { minGapMs: 0 }); const n = await checkOriginalUrl("https://blog.example/", { pacer: dead, resolve: async () => ["93.184.216.34"] }); assert.deepEqual([n.availability, n.status], ["error", null]); });