// The older-posts backfill's window arithmetic (olderBackfill.ts). // // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test social/olderBackfill.test.ts import { test } from "node:test"; import assert from "node:assert/strict"; import { addUtcDays, addUtcMonths, describeOlderBackfill, firstOlderWindow, isUtcDay, olderFloorDay, stepOlderWindow, } from "./olderBackfill"; test("day arithmetic is UTC and clamps a month to its last day", () => { assert.equal(addUtcDays("2020-12-31", 1), "2021-01-01"); assert.equal(addUtcDays("2020-03-01", -1), "2020-02-29"); assert.equal(addUtcMonths("2024-05-31", -3), "2024-02-29"); assert.equal(addUtcMonths("2021-01-15", -3), "2020-10-15"); assert.equal(isUtcDay("2020-02-29"), true); assert.equal(isUtcDay("2021-02-29"), false); assert.equal(isUtcDay("2021-2-1"), false); }); test("the first window ends the day after the oldest archived post", () => { assert.deepEqual(firstOlderWindow({ oldestArchivedAt: "2021-03-10T23:59:00.000Z" }), { since: "2020-12-11", until: "2021-03-11", emptyWindows: 0, }); // Nothing archived: it ends tomorrow. assert.equal( firstOlderWindow({ now: new Date("2026-10-03T12:00:00Z") }).until, "2026-10-04", ); // A floor inside the first window clamps its start. assert.equal( firstOlderWindow({ oldestArchivedAt: "2021-03-10T00:00:00Z", floor: "2021-01-01" }).since, "2021-01-01", ); }); test("windows step back with no gap, count empty ones, and end after a run of them", () => { let pos = firstOlderWindow({ oldestArchivedAt: "2021-03-10T00:00:00Z" }); const seen: string[] = []; const held = new Set(["2020-12-11"]); // only the first window holds posts for (let i = 0; i < 10; i++) { seen.push(`${pos.since}..${pos.until}`); const step = stepOlderWindow(pos, { hadPosts: held.has(pos.since) }); if (step.complete) { assert.equal(step.emptyWindows, 4); assert.match(step.reason, /4 windows of 3 months in a row held no posts, back to 2019-12-11/); break; } assert.equal(step.next.until, pos.since); assert.equal(step.next.maxId, undefined); pos = step.next; } assert.deepEqual(seen, [ "2020-12-11..2021-03-11", "2020-09-11..2020-12-11", "2020-06-11..2020-09-11", "2020-03-11..2020-06-11", "2019-12-11..2020-03-11", ]); }); test("nothing archived and nothing found: the walk ends after two empty windows, and says why", () => { const first = stepOlderWindow( { since: "2026-07-05", until: "2026-10-05", emptyWindows: 0 }, { hadPosts: false, nothingFound: true }, ); assert.equal(first.complete, false); if (first.complete) return; const second = stepOlderWindow(first.next, { hadPosts: false, nothingFound: true }); assert.equal(second.complete, true); if (!second.complete) return; assert.equal(second.emptyWindows, 2); assert.match(second.reason, /^2 windows of 3 months in a row held no posts, back to 2026-04-05, with nothing archived and none found/); // Without it, the same two windows are not the end: a year of them is. const year = stepOlderWindow(first.next, { hadPosts: false }); assert.equal(year.complete, false); }); test("a window with posts resets the empty count", () => { const step = stepOlderWindow( { since: "2020-01-01", until: "2020-04-01", emptyWindows: 3 }, { hadPosts: true }, ); assert.equal(step.complete, false); if (!step.complete) assert.equal(step.next.emptyWindows, 0); }); test("the floor clamps the last window and ends the walk after it", () => { const step = stepOlderWindow( { since: "2020-04-01", until: "2020-07-01", emptyWindows: 0 }, { hadPosts: true, floor: "2020-02-15" }, ); assert.equal(step.complete, false); if (step.complete) return; assert.deepEqual(step.next, { since: "2020-02-15", until: "2020-04-01", emptyWindows: 0 }); const last = stepOlderWindow(step.next, { hadPosts: false, floor: "2020-02-15" }); assert.equal(last.complete, true); if (last.complete) assert.match(last.reason, /reached 2020-02-15/); }); test("the floor is the later of the given date and the account's creation day", () => { assert.equal(olderFloorDay(undefined, undefined), undefined); assert.equal(olderFloorDay("2015-01-01", undefined), "2015-01-01"); assert.equal(olderFloorDay(undefined, "2012-06-30T22:00:00.000Z"), "2012-06-30"); assert.equal(olderFloorDay("2015-01-01", "2012-06-30T22:00:00.000Z"), "2015-01-01"); assert.equal(olderFloorDay("2010-01-01", "2012-06-30T22:00:00.000Z"), "2012-06-30"); }); test("the channel page's one line", () => { assert.equal(describeOlderBackfill(undefined), "not started"); assert.equal( describeOlderBackfill({ since: "2019-01-01", until: "2019-04-01", emptyWindows: 0 }), "walked back to 2019-04-01; next 2019-01-01 – 2019-04-01", ); assert.equal( describeOlderBackfill({ since: "2019-01-01", until: "2019-04-01", emptyWindows: 4, complete: true, completeReason: "reached 2019-01-01, the earliest date to walk to", }), "complete — reached 2019-01-01, the earliest date to walk to", ); });