// The forum walk's positions, pure (release 37 lane G). // // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test lib/forumWalk.test.ts import { test } from "node:test"; import assert from "node:assert/strict"; import { deriveForumWalk, describeForumWalk, describeRetryOnBlock, forumWalkStartLine, heldBand, meanPagePauseSeconds, parseRetryOnBlock, retryOnBlockProblem, roughDuration, } from "./forumWalk"; const range = (a: number, b: number) => Array.from({ length: b - a + 1 }, (_, i) => a + i); test("the held band runs down from the newest held page; an isolated low page is not part of it", () => { assert.equal(heldBand([]), null); assert.deepEqual(heldBand([...range(2341, 2598), 1]), { low: 2341, high: 2598 }); assert.deepEqual(heldBand([5, 5, 4, 2, 1]), { low: 4, high: 5 }); }); test("the live bug's state migrates: an old single cursor above the held band heals to one page above its foot", () => { // posts-state.json {"cursor": "2507"}; the archive holds pages 2341–2598. const d = deriveForumWalk(undefined, "2507", range(2341, 2598)); assert.equal(d.walk.historyPage, 2342); assert.equal(d.walk.newestPage, 2598); assert.equal(d.walk.oldestPageReached, 2341); assert.equal(d.walk.newestPostId, undefined, "no mark yet: the first catch-up stops at any archived post"); assert.equal(d.notes.length, 2); // An old cursor BELOW the band (a history walk that was honest) is kept. assert.equal(deriveForumWalk(undefined, "2340", range(2341, 2598)).walk.historyPage, 2340); }); test("derive, don't trust: a stored position never above the band's foot + 1, complete only when page 1 is held", () => { assert.equal(deriveForumWalk({ historyPage: 100 }, undefined, range(50, 200)).walk.historyPage, 51); assert.equal(deriveForumWalk({ historyPage: 40 }, undefined, range(50, 200)).walk.historyPage, 40); // No position at all (posts imported, never walked): from the posts. assert.equal(deriveForumWalk(undefined, undefined, range(50, 200)).walk.historyPage, 51); // Marked complete, but the pages stop at 50: reopened. const reopened = deriveForumWalk({ complete: true }, undefined, range(50, 200)); assert.equal(reopened.walk.complete, false); assert.equal(reopened.walk.historyPage, 51); // Every page from 1 up held: complete, whatever was stored. const done = deriveForumWalk({ historyPage: 3 }, undefined, range(1, 200)); assert.equal(done.walk.complete, true); assert.equal(done.walk.historyPage, undefined); // Nothing held: the stored positions as they are. assert.deepEqual(deriveForumWalk({ historyPage: 7 }, undefined, []).walk, { historyPage: 7 }); // A cursor that is not a page number is not read. assert.deepEqual(deriveForumWalk(undefined, "abc", []).walk, {}); }); test("the start line and the channel page's status line", () => { assert.equal( forumWalkStartLine({ newestPage: 2600, historyPage: 2338 }), "caught up to page 2600; history resumes at page 2338", ); assert.equal( forumWalkStartLine({ newestPage: 2600, catchUpPage: 2590, complete: true }), "caught up to page 2600; an unfinished catch-up continues at page 2590; history complete", ); assert.equal(forumWalkStartLine({}), "nothing read yet; history from the last page"); const pace = meanPagePauseSeconds(12, 5); assert.equal(pace, 15); assert.equal( describeForumWalk({ newestPage: 2600, oldestPageReached: 2338, historyPage: 2337 }, 12.3), "pages read 2338–2600 of 2600 · history to page 1 remaining: 2337 pages (~8 h)", ); assert.equal( describeForumWalk({ newestPage: 40, oldestPageReached: 1, complete: true, catchUpPage: 38 }, pace), "pages read 1–40 of 40 · catch-up unfinished at page 38 · history complete", ); assert.equal(describeForumWalk(undefined, pace), null); assert.equal(describeForumWalk({}, pace), null); assert.equal(roughDuration(40), "~40 s"); assert.equal(roughDuration(25 * 60), "~25 min"); assert.equal(roughDuration(3 * 3600 + 600), "~3.2 h"); }); test("retry on a block: bounds, parsing, words", () => { assert.equal(retryOnBlockProblem(undefined), null); assert.equal(retryOnBlockProblem({ minutes: 30, max: 12 }), null); assert.match(retryOnBlockProblem({ minutes: 1, max: 12 }) ?? "", /minutes/); assert.match(retryOnBlockProblem({ minutes: 30, max: 0 }) ?? "", /max/); assert.match(retryOnBlockProblem({ minutes: 30, max: 1.5 }) ?? "", /max/); assert.deepEqual(parseRetryOnBlock({ minutes: 30, max: 12, extra: 1 }), { minutes: 30, max: 12 }); assert.equal(parseRetryOnBlock({ minutes: 2, max: 12 }), undefined); assert.equal(parseRetryOnBlock("30x12"), undefined); assert.equal(describeRetryOnBlock({ minutes: 30, max: 12 }), "wait 30 min after a block, up to 12 tries"); assert.equal(describeRetryOnBlock(undefined), "off"); }); test("an interrupted catch-up's own gap does not unmark a complete history", () => { // 2026-10-26: complete through page 987, a catch-up read 1090 down to 988 and was cut off with page 987's newer // half still owed (its older posts held under a page number that has since drifted to 986). const pages = [...Array.from({ length: 986 }, (_, i) => i + 1), ...Array.from({ length: 103 }, (_, i) => 988 + i)]; const stored = { newestPage: 1090, oldestPageReached: 1, newestPostId: "25880529", complete: true, catchUpPage: 987 }; const { walk, notes } = deriveForumWalk(stored, undefined, pages); assert.equal(walk.complete, true); assert.equal(walk.historyPage, undefined); assert.equal(walk.catchUpPage, 987, "the catch-up still owes its page"); assert.deepEqual(notes, []); // Without an unfinished catch-up the same gap is the history's, as before. const { walk: w2 } = deriveForumWalk({ ...stored, catchUpPage: undefined }, undefined, pages); assert.equal(w2.complete, false); assert.equal(w2.historyPage, 989); });