// Release 38 lane C: the raw captures a Wayback pages channel keeps are what // make a new extractor worth having. A Newgrounds user page imported while // only the generic extractor read it is STALE now, and `reextract-pages` // turns its record into the Newgrounds extractor's, asking nobody anything. // // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test controller/reextractPages.newgrounds.test.ts import test, { after } from "node:test"; import assert from "node:assert/strict"; import { mkdtempSync, readFileSync, rmSync } from "node:fs"; import { mkdir, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import { createHash } from "node:crypto"; import type { Paths } from "../lib/paths"; import { reextractPages } from "./reextractPages"; import { readAllPosts, upsertPosts } from "../lib/posts-server"; import { genericExtractor } from "../lib/waybackPages/extractors/generic"; import { pageRecordId } from "../lib/waybackPages/types"; import { draftToPost } from "../lib/waybackPages/toPost"; import { listStoredCaptures, writeStoredCapture } from "../lib/waybackPages/store-server"; const ROOT = mkdtempSync(path.join(tmpdir(), "reextract-ng-")); after(() => rmSync(ROOT, { recursive: true, force: true })); const paths = { channelsDir: path.join(ROOT, "channels"), transcriptsDir: ROOT, jobsDir: path.join(ROOT, "jobs") } as unknown as Paths; const USER = "http://pocketfox.newgrounds.com/"; const TS = "20140304120000"; const HTML = readFileSync(new URL("../lib/waybackPages/extractors/__fixtures__/ng-user-2014.html", import.meta.url), "utf8"); test("a Newgrounds capture the generic extractor read is stale, and a re-extract reads it as a user page", async () => { const root = path.join(paths.channelsDir, "ng-pages"); await mkdir(root, { recursive: true }); await writeFile( path.join(root, "config.json"), JSON.stringify({ handling: "transcribe", name: "Newgrounds pages", sourceKind: "social", platform: "webpage", socialHandle: "pocketfox.newgrounds.com", url: "https://web.archive.org/web/*/pocketfox.newgrounds.com*", wayback: { targets: [{ url: "pocketfox.newgrounds.com/", match: "prefix" }] }, }), ); // As an import before this extractor existed stored it: the body, and the generic extractor's record. const body = Buffer.from(HTML, "utf8"); const capture = { originalUrl: USER, captureTs: TS }; const old = genericExtractor.extract({ html: HTML, capture: { ...capture, waybackUrl: "", rawUrl: "" } }); const oldPost = draftToPost(old[0], { channelSlug: "ng-pages", capture, extractor: { name: "generic", version: 1 } }); await writeStoredCapture( root, { ...capture, mimetype: "text/html", bytes: body.length, sha256: createHash("sha256").update(body).digest("hex"), fetchedAt: "2026-10-20T00:00:00.000Z", extractor: "generic", extractorVersion: 1, records: [oldPost.id], }, body, ); await upsertPosts(root, [oldPost]); const dry = await reextractPages({ paths, slug: "ng-pages", dryRun: true }); assert.equal(dry.ok, true, dry.error); assert.equal(dry.stale, 1, "made by the generic extractor, now claimed by newgrounds"); assert.equal(dry.written, false); const done = await reextractPages({ paths, slug: "ng-pages" }); assert.equal(done.ok, true, done.error); assert.equal(done.written, true); const posts = await readAllPosts(root); assert.equal(posts.length, 1); const p = posts[0]; assert.equal(p.id, pageRecordId(TS, USER), "a user page's record is the capture's, as before"); assert.equal(p.page?.kind, "ng-user"); assert.equal(p.page?.extractor, "newgrounds"); assert.equal(p.author, "pocketfox"); assert.match(p.text, /Location: Lake Erie/); assert.match(p.text, /The lighthouse loop finally went up/); const [entry] = await listStoredCaptures(root); assert.equal(entry.capture.extractor, "newgrounds"); const again = await reextractPages({ paths, slug: "ng-pages" }); assert.equal(again.stale, 0); assert.equal(again.written, false); });