// fetchPosts over a FORUM THREAD (release 37 lane G), end to end through the // real XenForo fetcher reading SYNTHETIC saved pages (E2E_FORUM_FIXTURE_DIR, // social/forumFixtureLoader.ts): no browser, no network. The two positions in // posts-state.json, an old single cursor migrating, the full re-walk's edits, // and the retry on a block over a fake clock. // // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test controller/fetchForumPosts.test.ts import { test } from "node:test"; import assert from "node:assert/strict"; import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; const ROOT = await mkdtemp(path.join(os.tmpdir(), "fetchforum-")); const FIXTURES = path.join(ROOT, "forum-fixture"); // getPaths() is resolved lazily and cached: set before the first call. process.env.TRANSCRIPTS_DIR = path.join(ROOT, "transcripts"); process.env.E2E_FORUM_FIXTURE_DIR = FIXTURES; const { THREAD_URL, CHALLENGE_PAGE } = await import("../social/__fixtures__/xenforoPages"); const { FakeForumThread, FAKE_THREAD_ID } = await import("../social/__fixtures__/fakeForumThread"); const { fetchPosts, sleepOrStop } = await import("./fetchPosts"); const { readAllPosts, readPostFetchState, readSeenPostIds, writePostFetchState } = await import("../lib/posts-server"); const { getPaths } = await import("../lib/paths"); let n = 0; async function channel(): Promise<{ slug: string; root: string }> { const slug = `forum-${++n}`; const root = path.join(ROOT, "transcripts", "channels", slug); await mkdir(root, { recursive: true }); await writeFile( path.join(root, "config.json"), JSON.stringify({ handling: "transcribe", sourceKind: "social", platform: "xenforo", postFetcher: "xenforo-thread", socialHandle: "the-teapot-collectors-thread.4242", name: "Teapot thread", url: THREAD_URL, }), ); return { slug, root }; } async function run(slug: string, over: Partial[0]> = {}) { const log: string[] = []; const result = await fetchPosts({ paths: getPaths(), slug, settings: {}, onLog: (l) => log.push(l), ...over }); return { result, log: log.join("\n") }; } test("a capped catch-up leaves the history position alone; posts-state.json keeps both, and no cursor", async () => { const { slug, root } = await channel(); const t = new FakeForumThread(30); // 10 pages await t.writeTo(FIXTURES); const first = await run(slug, { pages: 5 }); assert.equal(first.result.ok, true); let state = await readPostFetchState(root); assert.equal(state?.cursor, undefined); assert.deepEqual(state?.forum, { newestPage: 10, oldestPageReached: 6, historyPage: 5, newestPostId: "9030" }); t.add(9); await t.writeTo(FIXTURES); const capped = await run(slug, { pages: 2 }); assert.match(capped.log, /caught up to page 10; history resumes at page 5/); state = await readPostFetchState(root); assert.equal(state?.forum?.historyPage, 5); assert.equal(state?.forum?.catchUpPage, 11); const rest = await run(slug); assert.equal(rest.result.complete, true); state = await readPostFetchState(root); assert.equal(state?.forum?.complete, true); assert.equal(state?.forum?.catchUpPage, undefined); assert.deepEqual([...(await readSeenPostIds(root))].sort(), t.ids().sort()); }); test("an old state's single cursor migrates: read once as the history position, healed from the posts, then dropped", async () => { const { slug, root } = await channel(); const t = new FakeForumThread(30); await t.writeTo(FIXTURES); // An old fetch read pages 10–5 (posts 13–30) and then a capped catch-up // overwrote the cursor with its own page (9). await run(slug, { pages: 6 }); await writePostFetchState(root, { lastFetchedAt: "2026-10-20T00:00:00.000Z", cursor: "9", lastFetchedCount: 3 }); const res = await run(slug); assert.match(res.log, /Read the old single resume point \(page 9\) as the history position\./); assert.match(res.log, /history resumes at page 6/); assert.equal(res.result.complete, true); const state = await readPostFetchState(root); assert.equal(state?.cursor, undefined, "the old cursor is gone"); assert.equal(state?.forum?.complete, true); assert.deepEqual([...(await readSeenPostIds(root))].sort(), t.ids().sort()); }); test("a full re-walk re-reads every page and appends a newer record of an edited post", async () => { const { slug, root } = await channel(); const t = new FakeForumThread(9); await t.writeTo(FIXTURES); await run(slug); // Post 9005 edited since; post 9002 deleted. t.posts = t.posts.map((p) => (p.id === 9005 ? { ...p, body: "Edited words.", editedTs: p.ts + 60 } : p)); t.remove([9002]); await t.writeTo(FIXTURES); const res = await run(slug, { full: true }); assert.equal(res.result.ok, true); assert.equal(res.result.updated, 1, "the edit, not the posts renumbered after the deletion"); assert.match(res.log, /a full re-walk/); assert.match(res.log, /1 archived post\(s\) were on no page of the thread .*: 9002\. Kept as archived/); const posts = await readAllPosts(root); const edited = posts.filter((p) => p.id === "9005"); assert.match(edited[edited.length - 1].text, /Edited words\./); assert.ok(posts.some((p) => p.id === "9002"), "a post no page shows is kept"); const state = await readPostFetchState(root); assert.equal(state?.forum?.complete, true); }); test("retry on a block: a challenge page is waited out, `minutes` on the fake clock, and the walk resumes", async () => { const { slug, root } = await channel(); const t = new FakeForumThread(15); // 5 pages await t.writeTo(FIXTURES); const blocked = path.join(FIXTURES, FAKE_THREAD_ID, "page-3.200.html"); await writeFile(blocked, CHALLENGE_PAGE); const waits: number[] = []; const res = await run(slug, { retryOnBlock: { minutes: 30, max: 12 }, sleep: async (ms) => { waits.push(ms); // Two waits, then the check clears. if (waits.length === 2) await rm(blocked); return "slept"; }, }); assert.deepEqual(waits, [30 * 60_000, 30 * 60_000]); assert.match(res.log, /Waiting 30 min after a challenge page, try 2\/12\./); assert.match(res.log, /Waiting 30 min after a challenge page, try 3\/12\./); assert.equal(res.result.ok, true); assert.equal(res.result.complete, true); assert.equal(res.result.written, 15); assert.deepEqual([...(await readSeenPostIds(root))].sort(), t.ids().sort()); }); test("retry on a block gives up after `max` tries, never retries a captcha, and a drain during the wait ends cleanly", async () => { const { slug } = await channel(); const t = new FakeForumThread(6); await t.writeTo(FIXTURES); const p2 = path.join(FIXTURES, FAKE_THREAD_ID, "page-2.403.html"); await writeFile(p2, "Access denied"); const waits: number[] = []; const res = await run(slug, { retryOnBlock: { minutes: 5, max: 3 }, sleep: async (ms) => { waits.push(ms); return "slept"; }, }); assert.equal(waits.length, 2, "three tries in all"); assert.equal(res.result.ok, false); assert.equal(res.result.block, "blocked"); assert.match(res.log, /Still refused after 3 tries; stopped\./); const drained = await run(slug, { retryOnBlock: { minutes: 5, max: 3 }, sleep: async () => "drained" }); assert.equal(drained.result.ok, true); assert.equal(drained.result.drained, true); await rm(p2); // A captcha needs a person: no wait at all. const { slug: s2 } = await channel(); const p1 = path.join(FIXTURES, FAKE_THREAD_ID, "page-2.200.html"); await writeFile(p1, '
'); const none: number[] = []; const cap = await run(s2, { retryOnBlock: { minutes: 5, max: 3 }, sleep: async (ms) => (none.push(ms), "slept") }); assert.equal(none.length, 0); assert.equal(cap.result.block, "captcha"); await rm(p1); // Bounds are the controller's too. const bad = await run(s2, { retryOnBlock: { minutes: 1, max: 3 } }); assert.match(bad.result.error ?? "", /minutes/); const older = await run(s2, { retryOnBlock: { minutes: 30, max: 3 }, older: true }); assert.match(older.result.error ?? "", /Retry on a block/); }); test("the real wait ends on a cancel or a drain", async () => { const cancel = new AbortController(); const p = sleepOrStop(60_000, cancel.signal); cancel.abort(); assert.equal(await p, "aborted"); const drain = new AbortController(); const q = sleepOrStop(60_000, undefined, drain.signal); drain.abort(); assert.equal(await q, "drained"); assert.equal(await sleepOrStop(1), "slept"); }); // Lane x-followups: X is asked only by the editor's jobs; a forum thread keeps the direct path // (`pnpm archilyzer posts fetch --slug --pages N`, README): its pacing is the fetcher's own. test("a forum thread is fetched by a direct call, outside any job: the CLI's path stays", async () => { const { currentJobContext } = await import("../lib/jobContext"); assert.equal(currentJobContext(), undefined); const { slug } = await channel(); const thread = new FakeForumThread(3); await thread.writeTo(FIXTURES); const r = await fetchPosts({ paths: getPaths(), slug, settings: {}, pages: 1 }); assert.equal(r.ok, true, r.error); assert.ok(r.written > 0); });