// An edition's window (R5 review ruling): whole days, the day after the last // edition through the day before the tick, never a day twice. import { test } from "node:test"; import assert from "node:assert/strict"; import { editionWindow, editionWindowAt, nothingToCover, type Edition } from "./types"; import { nextTickAfter, type Cadence } from "./cadence"; const scope = { channels: ["c"] }; const ed = (n: number, from: string, to: string, status: Edition["status"] = "done"): Edition => ({ n, at: "2026-01-01T00:00:00.000Z", window: { from, to }, title: "t", status, usd: 0 }); const daily: Cadence = { kind: "daily", at: "06:00" }; const weekly: Cadence = { kind: "weekly", dow: 1, at: "06:00" }; const every3: Cadence = { kind: "every", days: 3, at: "06:00" }; test("daily: yesterday, whole; then the day after the last one", () => { assert.deepEqual(editionWindow({ cadence: daily, scope }, "2026-10-14"), { window: { from: "2026-10-13", to: "2026-10-13" } }); assert.deepEqual(editionWindow({ cadence: daily, scope, editions: [ed(1, "2026-10-13", "2026-10-13")] }, "2026-10-15"), { window: { from: "2026-10-14", to: "2026-10-14" } }); // Missed days are covered by the next edition, none read twice. assert.deepEqual(editionWindow({ cadence: daily, scope, editions: [ed(1, "2026-10-13", "2026-10-13")] }, "2026-10-18"), { window: { from: "2026-10-14", to: "2026-10-17" } }); }); test("weekly on Monday: the previous Mon … Sun, seven days; the next week starts the day after", () => { // Mon 2026-10-19 tick. const w1 = editionWindow({ cadence: weekly, scope }, "2026-10-19"); assert.deepEqual(w1, { window: { from: "2026-10-12", to: "2026-10-18" } }); const w2 = editionWindow({ cadence: weekly, scope, editions: [ed(1, "2026-10-12", "2026-10-18")] }, "2026-10-26"); assert.deepEqual(w2, { window: { from: "2026-10-19", to: "2026-10-25" } }); // A quiet edition covered its window too; a failed one did not. assert.deepEqual(editionWindow({ cadence: weekly, scope, editions: [ed(1, "2026-10-12", "2026-10-18"), ed(2, "2026-10-19", "2026-10-25", "quiet")] }, "2026-11-02"), { window: { from: "2026-10-26", to: "2026-11-01" } }); assert.deepEqual(editionWindow({ cadence: weekly, scope, editions: [ed(1, "2026-10-12", "2026-10-18"), ed(2, "2026-10-19", "2026-10-25", "failed")] }, "2026-11-02"), { window: { from: "2026-10-19", to: "2026-11-01" } }); }); test("every N days: N whole days back, then the day after; the scope's from floors the first", () => { assert.deepEqual(editionWindow({ cadence: every3, scope }, "2026-10-14"), { window: { from: "2026-10-11", to: "2026-10-13" } }); assert.deepEqual(editionWindow({ cadence: every3, scope, editions: [ed(1, "2026-10-11", "2026-10-13")] }, "2026-10-17"), { window: { from: "2026-10-14", to: "2026-10-16" } }); assert.deepEqual(editionWindow({ cadence: every3, scope: { ...scope, from: "2026-10-12" } }, "2026-10-14"), { window: { from: "2026-10-12", to: "2026-10-13" } }); }); test("empty: a Tick now right after a tick (or a from in the future) has nothing to cover", () => { const r = editionWindow({ cadence: weekly, scope, editions: [ed(1, "2026-10-12", "2026-10-18")] }, "2026-10-19"); assert.deepEqual(r, { empty: true, nextFrom: "2026-10-19" }); assert.equal(nothingToCover("2026-10-19"), "Nothing to cover yet — the next edition starts 2026-10-19"); assert.deepEqual(editionWindow({ cadence: daily, scope: { ...scope, from: "2026-12-01" } }, "2026-10-14"), { empty: true, nextFrom: "2026-12-01" }); }); test("DST: the window is the schedule's local days, whatever the instant's UTC date", () => { const NY = "America/New_York"; // New York falls back on Sun 2026-11-01; the Monday tick after it is 06:00 EST. const t = nextTickAfter(weekly, Date.parse("2026-10-31T12:00:00Z"), NY); assert.equal(new Date(t).toISOString(), "2026-11-02T11:00:00.000Z"); assert.deepEqual(editionWindowAt({ cadence: weekly, scope, tz: NY, editions: [ed(1, "2026-10-19", "2026-10-25")] }, t), { window: { from: "2026-10-26", to: "2026-11-01" } }); // A daily 00:30 tick in Tokyo is the previous UTC date: the window is Tokyo's yesterday. const tokyo = nextTickAfter({ kind: "daily", at: "00:30" }, Date.parse("2026-10-14T00:00:00Z"), "Asia/Tokyo"); assert.equal(new Date(tokyo).toISOString(), "2026-10-14T15:30:00.000Z"); assert.deepEqual(editionWindowAt({ cadence: daily, scope, tz: "Asia/Tokyo" }, tokyo), { window: { from: "2026-10-14", to: "2026-10-14" } }); // Spring forward (Sun 2026-03-08): a 02:30 daily tick lands 03:30 that day; its window is the 7th. const spring = nextTickAfter({ kind: "daily", at: "02:30" }, Date.parse("2026-03-08T00:00:00Z"), NY); assert.deepEqual(editionWindowAt({ cadence: daily, scope, tz: NY }, spring), { window: { from: "2026-03-07", to: "2026-03-07" } }); });