// An X search channel's walk, planned: the first walk from until/tomorrow down // to since, a resume before anything else, the new days with a day of overlap, // the earlier days when since moves back, and nothing when all is covered. // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test lib/xSearch/walk.test.ts import { test } from "node:test"; import assert from "node:assert/strict"; import { coverAfterWalk, describeXSearchWalk, planXSearchWalk, startXSearchWalk, xSearchStateFor, } from "./walk"; const NOW = new Date("2026-10-20T12:00:00Z"); test("the first walk starts at tomorrow (or until) and its windows reach no lower than since", () => { const state = xSearchStateFor({ query: "zephyr" }, undefined); assert.deepEqual(state, { queryKey: "zephyr" }); assert.deepEqual(planXSearchWalk({ query: "zephyr" }, state, NOW), { kind: "first", walk: { top: "2026-10-21", since: "2026-07-21", until: "2026-10-21", emptyWindows: 0 }, }); assert.deepEqual(planXSearchWalk({ query: "zephyr", since: "2026-09-01", until: "2026-10-01", windowMonths: 1 }, state, NOW), { kind: "first", walk: { top: "2026-10-01", floor: "2026-09-01", since: "2026-09-01", until: "2026-10-01", emptyWindows: 0 }, }); assert.equal(planXSearchWalk({ query: "zephyr", since: "2026-11-01" }, state, NOW).kind, "none"); }); test("a walk in progress is resumed before anything else", () => { const walk = { ...startXSearchWalk("2026-10-21", "2025-01-01", 3), maxId: "1899" }; const plan = planXSearchWalk({ query: "zephyr", since: "2025-01-01" }, { queryKey: "zephyr", walk, coveredUntil: "2026-01-01" }, NOW); assert.deepEqual(plan, { kind: "resume", walk }); }); test("after a finished walk: the new days with a day of overlap, then the earlier days, then nothing", () => { const covered = { queryKey: "zephyr", coveredSince: "2025-01-01", coveredUntil: "2026-10-15" }; assert.deepEqual(planXSearchWalk({ query: "zephyr", since: "2025-01-01" }, covered, NOW), { kind: "new", walk: { top: "2026-10-21", floor: "2026-10-14", since: "2026-10-14", until: "2026-10-21", emptyWindows: 0 }, }); const upToDate = { ...covered, coveredUntil: "2026-10-21" }; const none = planXSearchWalk({ query: "zephyr", since: "2025-01-01" }, upToDate, NOW); assert.equal(none.kind, "none"); assert.match((none as { reason: string }).reason, /already searched 2025-01-01 – 2026-10-21/); assert.deepEqual(planXSearchWalk({ query: "zephyr", since: "2024-06-01" }, upToDate, NOW), { kind: "earlier", walk: { top: "2025-01-02", floor: "2024-06-01", since: "2024-10-02", until: "2025-01-02", emptyWindows: 0 }, }); // A fixed until already covered: nothing new above it. assert.equal(planXSearchWalk({ query: "zephyr", since: "2025-01-01", until: "2026-10-15" }, covered, NOW).kind, "none"); }); test("a new query starts afresh; the dates alone do not", () => { const stored = { queryKey: "zephyr", coveredSince: "2025-01-01", coveredUntil: "2026-10-15" }; assert.equal(xSearchStateFor({ query: "zephyr" }, stored), stored); assert.deepEqual(xSearchStateFor({ query: "zephyr OR zephyrs" }, stored), { queryKey: "zephyr OR zephyrs" }); assert.deepEqual(xSearchStateFor({ query: "zephyr", lang: "en" }, stored), { queryKey: "zephyr lang:en" }); }); test("coverage after a walk is the union of what was covered and what was walked", () => { assert.deepEqual(coverAfterWalk({}, { top: "2026-10-21" }, "2025-01-01"), { coveredSince: "2025-01-01", coveredUntil: "2026-10-21", }); assert.deepEqual(coverAfterWalk({ coveredSince: "2025-01-01", coveredUntil: "2026-10-15" }, { top: "2026-10-21" }, "2026-10-14"), { coveredSince: "2025-01-01", coveredUntil: "2026-10-21", }); assert.deepEqual(coverAfterWalk({ coveredSince: "2025-01-01", coveredUntil: "2026-10-21" }, { top: "2025-01-02" }, "2024-06-01"), { coveredSince: "2024-06-01", coveredUntil: "2026-10-21", }); assert.equal(describeXSearchWalk(undefined), "not searched yet"); assert.equal( describeXSearchWalk({ queryKey: "z", coveredSince: "2025-01-01", coveredUntil: "2026-10-21", walk: { top: "2025-01-02", floor: "2024-06-01", since: "2024-10-02", until: "2025-01-02", emptyWindows: 0, maxId: "9" } }), "searched 2025-01-01 – 2026-10-21; walking 2024-10-02 – 2025-01-02 (down to 2024-06-01), resuming inside the window", ); });