// A FORUM THREAD's two buttons (release 37 lane G): Fetch posts catches up // and then continues the history where it stopped — never re-reading the band // between — and Full re-walk re-reads every page, behind a confirm with the // page count and the time at the channel's pace. Plus the status line and the // retry after a block. // // The forum is invented and never contacted: the test server's forum fetcher // reads SYNTHETIC saved pages from E2E_FORUM_FIXTURE_DIR (e2e/servers.ts sets // it to /test-transcripts/.forum-fixture; see // common/social/forumFixtureLoader.ts), written here from a FakeForumThread. import { test, expect, type Page } from "@playwright/test"; import { mkdir, readFile, writeFile } from "node:fs/promises"; import path from "node:path"; import { baseUrl } from "./baseUrl"; import { pathExists, readJson, resetData, testPaths } from "./helpers"; import { THREAD_URL } from "yt-dlp-transcript-common/social/__fixtures__/xenforoPages"; import { FakeForumThread } from "yt-dlp-transcript-common/social/__fixtures__/fakeForumThread"; const SLUG = "teapot-thread"; type ForumState = { forum?: { newestPage?: number; newestPostId?: string; catchUpPage?: number; historyPage?: number; oldestPageReached?: number; complete?: boolean; }; cursor?: string; }; const channelRoot = () => path.join(testPaths().transcriptsDir, "channels", SLUG); const fixtureDir = () => path.join(testPaths().transcriptsDir, ".forum-fixture"); async function state(): Promise { const file = path.join(channelRoot(), "posts-state.json"); return (await pathExists(file)) ? await readJson(file) : null; } async function archived(): Promise { const file = path.join(channelRoot(), "posts-archive"); if (!(await pathExists(file))) return []; return (await readFile(file, "utf8")).split("\n").filter(Boolean).map((l) => l.split(" ")[1]).sort(); } // A fetch's LAST write is the channel's lastSyncedAt (after posts-state.json): // a new stamp means the job is done and its state final. const lastSynced = async () => (await readJson<{ lastSyncedAt?: string }>(path.join(channelRoot(), "config.json"))).lastSyncedAt; async function untilDone(before: string | undefined) { await expect.poll(lastSynced, { timeout: 30_000 }).not.toBe(before); } async function fetchOnce(page: Page, pages: string) { await page.getByLabel("pages to fetch").fill(pages); const before = await lastSynced(); await page.getByRole("button", { name: "fetch posts" }).click(); await expect(page.getByRole("status").filter({ hasText: "Fetch queued" })).toBeVisible(); await untilDone(before); } test("a forum thread: Fetch posts resumes the history, Full re-walk asks first, the retry is a setting", async ({ page }) => { await resetData("empty"); await mkdir(channelRoot(), { recursive: true }); await writeFile( path.join(channelRoot(), "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, }), ); const thread = new FakeForumThread(30); // 10 pages await thread.writeTo(fixtureDir()); await fetch(`${baseUrl}/api/test/invalidate-cache`).catch(() => {}); await page.goto(`/channels/${SLUG}`); await expect(page.getByRole("button", { name: "fetch posts" })).toBeVisible(); await expect(page.getByRole("button", { name: "full re-walk" })).toBeVisible(); await expect(page.getByRole("button", { name: "refetch full history" })).toHaveCount(0); await expect(page.getByLabel("forum walk")).toHaveCount(0); // The latest 4 pages (10–7): the history stops at 6. await fetchOnce(page, "4"); expect((await state())?.forum).toEqual({ newestPage: 10, oldestPageReached: 7, historyPage: 6, newestPostId: "9030" }); expect((await state())?.cursor).toBeUndefined(); // Six new posts (pages 11–12); a 1-page run catches up page 12 only — the // history position stays where it was. thread.add(6); await thread.writeTo(fixtureDir()); await page.reload(); await expect(page.getByLabel("forum walk")).toHaveText( "pages read 7–10 of 10 · history to page 1 remaining: 6 pages (~2 min)", ); await fetchOnce(page, "1"); expect((await state())?.forum).toMatchObject({ newestPage: 12, catchUpPage: 11, historyPage: 6, newestPostId: "9030" }); await page.reload(); await expect(page.getByLabel("forum walk")).toHaveText( "pages read 7–12 of 12 · catch-up unfinished at page 11 · history to page 1 remaining: 6 pages (~2 min)", ); // No cap: the rest of the catch-up (11), then the history from 6 to 1. await fetchOnce(page, ""); expect((await state())?.forum).toMatchObject({ newestPage: 12, newestPostId: "9036", complete: true }); expect(await archived()).toEqual(thread.ids().sort()); await page.reload(); await expect(page.getByLabel("forum walk")).toHaveText("pages read 1–12 of 12 · history complete"); // FULL RE-WALK asks first: every page, and the time at the channel's pace. await page.getByRole("button", { name: "full re-walk" }).click(); const confirm = page.getByRole("alertdialog", { name: "confirm full re-walk" }); await expect(confirm).toContainText("Re-read all 12 pages, from the last to page 1 — ~3 min at this channel's pace."); await page.getByRole("button", { name: "cancel full re-walk" }).click(); await expect(confirm).toHaveCount(0); const before = await lastSynced(); await page.getByRole("button", { name: "full re-walk" }).click(); await page.getByRole("button", { name: "start full re-walk" }).click(); await expect(page.getByRole("status").filter({ hasText: "Full re-walk queued" })).toBeVisible(); await untilDone(before); expect((await state())?.forum).toMatchObject({ complete: true, newestPostId: "9036" }); expect(await archived()).toEqual(thread.ids().sort()); // RETRY AFTER A BLOCK: off until set; saved to settings.social.xenforo. await expect(page.getByLabel("retry after a block", { exact: true })).toContainText("Fetch posts: off"); await page.getByLabel("retry minutes").fill("30"); await page.getByLabel("retry tries").fill("12"); await page.getByRole("button", { name: "save retry after a block" }).click(); await expect(page.getByLabel("retry after a block", { exact: true })).toContainText("wait 30 min after a block, up to 12 tries"); await expect .poll(async () => (await readJson<{ social?: { xenforo?: unknown } }>(testPaths().settingsFile)).social?.xenforo) .toEqual({ retryOnBlock: { minutes: 30, max: 12 } }); await page.getByLabel("retry minutes").fill("2"); await page.getByRole("button", { name: "save retry after a block" }).click(); await expect(page.getByRole("alert").filter({ hasText: "minutes must be a whole number from 5" })).toBeVisible(); await page.getByRole("button", { name: "turn off retry after a block" }).click(); await expect(page.getByLabel("retry after a block", { exact: true })).toContainText("Fetch posts: off"); await expect .poll(async () => (await readJson<{ social?: { xenforo?: unknown } }>(testPaths().settingsFile)).social?.xenforo) .toBeUndefined(); });