import { test } from "node:test"; import assert from "node:assert/strict"; import { addDays, anchorFor, cadenceDays, cadenceWords, chipWords, dueTick, instantOf, localDate, nextTickAfter, parseCadence, wallOf, type Cadence, } from "./cadence"; const NY = "America/New_York"; const iso = (t: number) => new Date(t).toISOString(); const at = (s: string) => Date.parse(s); test("parseCadence: objects and phrases; what to fix when wrong", () => { assert.deepEqual(parseCadence({ kind: "daily", at: "6:00" }), { kind: "daily", at: "06:00" }); assert.deepEqual(parseCadence("weekly mon 06:00"), { kind: "weekly", dow: 1, at: "06:00" }); assert.deepEqual(parseCadence("weekly Monday"), { kind: "weekly", dow: 1, at: "06:00" }); assert.deepEqual(parseCadence("every 3 days 21:30"), { kind: "every", days: 3, at: "21:30" }); assert.deepEqual(parseCadence({ kind: "weekly", dow: "fri", at: "07:15" }), { kind: "weekly", dow: 5, at: "07:15" }); assert.throws(() => parseCadence({ kind: "every", days: 1, at: "06:00" }), /2 to 365/); assert.throws(() => parseCadence({ kind: "daily", at: "25:00" }), /HH:MM/); assert.throws(() => parseCadence("monthly"), /a cadence is/); assert.throws(() => parseCadence({ kind: "weekly", dow: 9 }), /day of the week/); assert.equal(cadenceDays({ kind: "weekly", dow: 1, at: "06:00" }), 7); assert.equal(cadenceDays({ kind: "every", days: 3, at: "06:00" }), 3); }); test("daily: the next HH:MM after an instant, on the local clock", () => { const c: Cadence = { kind: "daily", at: "06:00" }; // 2026-10-14 05:00 New York (EDT, UTC-4) → 06:00 the same day. assert.equal(iso(nextTickAfter(c, at("2026-10-14T09:00:00Z"), NY)), "2026-10-14T10:00:00.000Z"); // Exactly at the tick: strictly after, so tomorrow. assert.equal(iso(nextTickAfter(c, at("2026-10-14T10:00:00Z"), NY)), "2026-10-15T10:00:00.000Z"); }); test("DST: a fall-back keeps the wall time (and the first 01:30); a spring-forward skips the gap, never the day", () => { const six: Cadence = { kind: "daily", at: "06:00" }; // New York falls back on 2026-11-01: 06:00 EDT (10:00Z) on Oct 31, 06:00 EST (11:00Z) on Nov 1. const a = nextTickAfter(six, at("2026-10-31T12:00:00Z"), NY); assert.equal(iso(a), "2026-11-01T11:00:00.000Z"); assert.equal(wallOf(a, NY).hh, 6); // 01:30 happens twice on Nov 1: the first (EDT, 05:30Z). assert.equal(iso(instantOf({ y: 2026, m: 11, d: 1, hh: 1, mm: 30 }, NY)), "2026-11-01T05:30:00.000Z"); // Springs forward on 2026-03-08: 02:30 does not exist; the tick is 03:30 EDT (07:30Z), that day. const half: Cadence = { kind: "daily", at: "02:30" }; const b = nextTickAfter(half, at("2026-03-08T00:00:00Z"), NY); assert.equal(iso(b), "2026-03-08T07:30:00.000Z"); assert.equal(localDate(b, NY), "2026-03-08"); // And the day after is 02:30 again. assert.equal(iso(nextTickAfter(half, b, NY)), "2026-03-09T06:30:00.000Z"); // Europe changes on its own Sunday. const ldn = nextTickAfter(six, at("2026-03-29T00:00:00Z"), "Europe/London"); assert.equal(iso(ldn), "2026-03-29T05:00:00.000Z"); }); test("weekly on a day: that weekday's HH:MM, never another", () => { const mon: Cadence = { kind: "weekly", dow: 1, at: "06:00" }; // Wed 2026-10-14 → Mon 2026-10-19. const t = nextTickAfter(mon, at("2026-10-14T12:00:00Z"), NY); assert.equal(localDate(t, NY), "2026-10-19"); assert.equal(iso(t), "2026-10-19T10:00:00.000Z"); // From Monday 05:59 local: the same day. assert.equal(localDate(nextTickAfter(mon, at("2026-10-19T09:59:00Z"), NY), NY), "2026-10-19"); // From Monday 06:00 local: next week. assert.equal(localDate(nextTickAfter(mon, at("2026-10-19T10:00:00Z"), NY), NY), "2026-10-26"); }); test("every N days: anchored to the first tick's date, whatever the instant asked from", () => { const c: Cadence = { kind: "every", days: 3, at: "06:00" }; const first = nextTickAfter(c, at("2026-10-14T12:00:00Z"), NY); assert.equal(localDate(first, NY), "2026-10-15"); const anchor = anchorFor(c, first, NY); assert.equal(anchor, "2026-10-15"); assert.equal(localDate(nextTickAfter(c, first, NY, anchor), NY), "2026-10-18"); // From any day in between, the next on the anchor's grid. assert.equal(localDate(nextTickAfter(c, at("2026-10-19T12:00:00Z"), NY, anchor), NY), "2026-10-21"); assert.equal(localDate(nextTickAfter(c, at("2026-10-21T09:00:00Z"), NY, anchor), NY), "2026-10-21"); // Across the fall-back, still 06:00 local. const after = nextTickAfter(c, at("2026-10-30T12:00:00Z"), NY, anchor); assert.equal(localDate(after, NY), "2026-11-02"); assert.equal(wallOf(after, NY).hh, 6); assert.equal(anchorFor({ kind: "daily", at: "06:00" }, first, NY), null); }); test("a missed window is ONE catch-up tick, never N", () => { const c: Cadence = { kind: "daily", at: "06:00" }; const s = { cadence: c, tz: NY, nextTick: "2026-10-10T10:00:00.000Z" }; // Down from the 10th; back on the 14th at 08:00 local (12:00Z): four windows passed. const now = at("2026-10-14T12:00:00Z"); const d = dueTick(s, now); assert.equal(d.due, true); if (!d.due) return; assert.equal(iso(d.scheduledFor), "2026-10-10T10:00:00.000Z"); assert.equal(d.missed, 4); assert.equal(iso(d.next), "2026-10-15T10:00:00.000Z"); // The schedule moved on: nothing else is due now. assert.deepEqual(dueTick({ ...s, nextTick: iso(d.next) }, now), { due: false }); // Not yet, or paused, or never. assert.deepEqual(dueTick(s, at("2026-10-10T09:59:59Z")), { due: false }); assert.deepEqual(dueTick({ ...s, paused: true }, now), { due: false }); assert.deepEqual(dueTick({ ...s, nextTick: null }, now), { due: false }); // On time: due, nothing missed. const onTime = dueTick(s, at("2026-10-10T10:00:30Z")); assert.equal(onTime.due && onTime.missed, 0); }); test("words: the cadence, the chip", () => { assert.equal(cadenceWords({ kind: "weekly", dow: 1, at: "06:00" }), "weekly · Mon 06:00"); assert.equal(cadenceWords({ kind: "every", days: 3, at: "06:00" }), "every 3 days 06:00"); const s = { cadence: { kind: "weekly", dow: 1, at: "06:00" } as Cadence, tz: NY, nextTick: "2026-10-19T10:00:00.000Z" }; assert.equal(chipWords(s, at("2026-10-14T12:00:00Z")), "weekly · next Mon 06:00"); assert.equal(chipWords({ ...s, paused: true }), "weekly · paused"); assert.equal(chipWords({ ...s, cadence: { kind: "daily", at: "06:00" } }, at("2026-10-01T12:00:00Z")), "daily · next 2026-10-19 06:00"); assert.equal(addDays("2026-03-01", -1), "2026-02-28"); assert.equal(addDays("2026-12-31", 7), "2027-01-07"); });