// The timeline's two walks — sync new and resume backfill — as pure // decisions, and the read of a posts-state.json written before they split. // // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test lib/timelineWalk.test.ts import { test } from "node:test"; import assert from "node:assert/strict"; import { describeTimelineBackfill, describeTimelineSync, hasTimelineResumePoint, NO_BACKFILL_TO_RESUME, planTimelineRun, settleTimelineRun, timelineRunLine, timelineWalkFields, timelineWalks, withResumePoint, type TimelineRunPlan, } from "./timelineWalk"; const NOW = "2026-11-06T12:00:00.000Z"; const WATERMARK = "2026-11-01T08:00:00.000Z"; test("MIGRATION: a legacy `cursor` is read as the backfill's position, and never written back", () => { // The madamsavvy-x shape: a state from before the split, an unfinished // backfill's cursor beside the rest. const legacy = { lastFetchedAt: "2026-11-05T00:00:00.000Z", lastFetchedCount: 0, cursor: "1/LEGACY" }; const walks = timelineWalks(legacy); assert.deepEqual(walks, { backfill: { cursor: "1/LEGACY" }, migrated: true }); assert.equal(hasTimelineResumePoint(legacy), true); // What is written: the backfill, no `cursor`, no `migrated` marker. assert.deepEqual(timelineWalkFields(walks), { backfill: { cursor: "1/LEGACY" } }); // And read back, it is the same position — not migrated again. assert.deepEqual(timelineWalks(timelineWalkFields(walks)), { backfill: { cursor: "1/LEGACY" } }); }); test("MIGRATION: a `backfill` already present wins over a stray legacy `cursor`; empty values are nothing", () => { assert.deepEqual(timelineWalks({ cursor: "1/OLD", backfill: { cursor: "1/NEW" } }), { backfill: { cursor: "1/NEW" }, }); assert.deepEqual(timelineWalks({ cursor: "" }), {}); assert.deepEqual(timelineWalks({ backfill: { cursor: "" }, sync: { cursor: "" } }), {}); assert.deepEqual(timelineWalks(null), {}); assert.deepEqual(timelineWalks({}), {}); assert.equal(hasTimelineResumePoint({}), false); assert.equal(hasTimelineResumePoint({ sync: { cursor: "1/S", since: WATERMARK } }), true); }); test("SYNC NEW walks from the top down to the watermark, past an unfinished backfill it does not read", () => { const walks = timelineWalks({ cursor: "1/LEGACY" }); const plan = planTimelineRun("sync", walks, WATERMARK); assert.deepEqual(plan, { mode: "sync", since: WATERMARK, saveTo: "sync" }); // It finishes ("caught up"): the backfill's position is exactly as it was. const after = settleTimelineRun(plan as TimelineRunPlan, walks, { complete: true }, NOW); assert.deepEqual(after.backfill, { cursor: "1/LEGACY" }); assert.equal(after.sync, undefined); }); test("SYNC NEW that stops part-way keeps its own resume point with the watermark it began with", () => { const walks = timelineWalks({ backfill: { cursor: "1/BF" } }); const plan = planTimelineRun("sync", walks, WATERMARK) as TimelineRunPlan; const stopped = settleTimelineRun(plan, walks, { complete: false, cursor: "1/S-P3" }, NOW); assert.deepEqual(stopped, { backfill: { cursor: "1/BF" }, sync: { cursor: "1/S-P3", since: WATERMARK, savedAt: NOW }, }); // The next sync continues it, down to THAT watermark — not the newer one // the posts it just wrote would make. const next = planTimelineRun("sync", stopped, "2026-11-06T09:00:00.000Z"); assert.deepEqual(next, { mode: "sync", cursor: "1/S-P3", since: WATERMARK, saveTo: "sync" }); assert.match(timelineRunLine(next as TimelineRunPlan), /continuing the unfinished sync down to 2026-11-01/); // Finished, the sync's slot clears; the backfill's stays. assert.deepEqual(settleTimelineRun(next as TimelineRunPlan, stopped, { complete: true }, NOW), { backfill: { cursor: "1/BF" }, }); }); test("SYNC NEW with nothing archived is the first full-history walk: an unfinished one leaves a BACKFILL", () => { const plan = planTimelineRun("sync", {}, null) as TimelineRunPlan; assert.deepEqual(plan, { mode: "sync", saveTo: "backfill" }); assert.match(timelineRunLine(plan), /first full-history walk/); const after = settleTimelineRun(plan, {}, { complete: false, cursor: "1/FIRST-P2" }, NOW); assert.deepEqual(after, { backfill: { cursor: "1/FIRST-P2", savedAt: NOW } }); // The next plain fetch (posts now archived) syncs new; it does not resume it. assert.deepEqual(planTimelineRun("sync", after, WATERMARK), { mode: "sync", since: WATERMARK, saveTo: "sync" }); }); test("SYNC NEW on an empty archive never moves or clears a backfill that is part-way", () => { const walks = timelineWalks({ cursor: "1/LEGACY" }); const plan = planTimelineRun("sync", walks, null) as TimelineRunPlan; assert.deepEqual(plan, { mode: "sync", saveTo: "sync" }); assert.deepEqual(settleTimelineRun(plan, walks, { complete: true }, NOW), { backfill: { cursor: "1/LEGACY" } }); assert.deepEqual(settleTimelineRun(plan, walks, { complete: false, cursor: "1/S" }, NOW), { backfill: { cursor: "1/LEGACY" }, sync: { cursor: "1/S", savedAt: NOW }, }); assert.deepEqual(withResumePoint(plan, walks, "1/CP", NOW).backfill, { cursor: "1/LEGACY" }); }); test("RESUME BACKFILL continues the saved point with no watermark, and never touches the sync's", () => { const walks = timelineWalks({ backfill: { cursor: "1/BF" }, sync: { cursor: "1/S", since: WATERMARK }, }); const plan = planTimelineRun("backfill", walks, WATERMARK) as TimelineRunPlan; assert.deepEqual(plan, { mode: "backfill", cursor: "1/BF", saveTo: "backfill" }); assert.match(timelineRunLine(plan), /^resume backfill/); const further = settleTimelineRun(plan, walks, { complete: false, cursor: "1/BF-2" }, NOW); assert.deepEqual(further.sync, { cursor: "1/S", since: WATERMARK }); assert.deepEqual(further.backfill, { cursor: "1/BF-2", savedAt: NOW }); const done = settleTimelineRun(plan, further, { complete: true }, NOW); assert.deepEqual(done, { sync: { cursor: "1/S", since: WATERMARK } }); }); test("RESUME BACKFILL with no backfill part-way is refused", () => { assert.deepEqual(planTimelineRun("backfill", {}, WATERMARK), { error: NO_BACKFILL_TO_RESUME }); assert.deepEqual(planTimelineRun("backfill", { sync: { cursor: "1/S", since: WATERMARK } }, WATERMARK), { error: NO_BACKFILL_TO_RESUME, }); }); test("a run that stops with no resume point keeps the one it had", () => { const walks = { backfill: { cursor: "1/BF" } }; const plan = planTimelineRun("backfill", walks, null) as TimelineRunPlan; assert.deepEqual(settleTimelineRun(plan, walks, { complete: false }, NOW), walks); }); test("FULL re-walks from the top; unfinished it replaces the backfill's point, finished it clears both", () => { const walks = { backfill: { cursor: "1/BF" }, sync: { cursor: "1/S", since: WATERMARK } }; const plan = planTimelineRun("full", walks, WATERMARK) as TimelineRunPlan; assert.deepEqual(plan, { mode: "full", saveTo: "backfill" }); assert.deepEqual(settleTimelineRun(plan, walks, { complete: false, cursor: "1/F" }, NOW), { backfill: { cursor: "1/F", savedAt: NOW }, sync: { cursor: "1/S", since: WATERMARK }, }); assert.deepEqual(settleTimelineRun(plan, walks, { complete: true }, NOW), {}); }); test("a checkpoint saves into the run's own slot", () => { const walks = { backfill: { cursor: "1/BF" } }; const sync = planTimelineRun("sync", walks, WATERMARK) as TimelineRunPlan; assert.deepEqual(withResumePoint(sync, walks, "1/CP", NOW), { backfill: { cursor: "1/BF" }, sync: { cursor: "1/CP", since: WATERMARK, savedAt: NOW }, }); const bf = planTimelineRun("backfill", walks, null) as TimelineRunPlan; assert.deepEqual(withResumePoint(bf, walks, "1/CP", NOW), { backfill: { cursor: "1/CP", savedAt: NOW } }); }); test("the status lines say a backfill is unfinished, and what continues a stopped sync", () => { assert.equal( describeTimelineBackfill({ backfill: { cursor: "1/BF" } }, 9936), "backfill unfinished — 9936 archived; Resume backfill continues it", ); assert.equal(describeTimelineBackfill({}, 10), "no backfill part-way"); assert.equal(describeTimelineSync({}), null); assert.match(describeTimelineSync({ sync: { cursor: "1/S", since: WATERMARK } }) ?? "", /down to 2026-11-01/); }); // Lane x-followups: a resumed backfill re-read archived posts, was taken for finished ("caught up") // and lost its resume point. test("a backfill is never finished by meeting known posts: its resume point is kept", () => { const walks = { backfill: { cursor: "3_1899000000000000041/", savedAt: "2026-11-17T00:00:00.000Z" }, sync: { cursor: "1/S", since: "2026-11-01T00:00:00.000Z" } }; const plan = planTimelineRun("backfill", walks, null) as TimelineRunPlan; assert.deepEqual(settleTimelineRun(plan, walks, { complete: true, caughtUp: true }, NOW), walks); // The timeline's real end clears it, as before. assert.deepEqual(settleTimelineRun(plan, walks, { complete: true }, NOW), { sync: walks.sync }); // A SYNC that caught up is finished: that is what catching up means for it. const sync = planTimelineRun("sync", walks, "2026-11-10T00:00:00.000Z") as TimelineRunPlan; assert.deepEqual(settleTimelineRun(sync, walks, { complete: true, caughtUp: true }, NOW), { backfill: walks.backfill }); });