import { describe, expect, it } from "vitest"; import { planInstances, windowTarget, INSTANCE_WINDOW_WEEKS } from "./instances"; const TZ = "America/New_York"; const weeklyService = { rrule: "FREQ=WEEKLY;BYDAY=SU", startTimeLocal: "09:00", timezone: TZ, durationMinutes: 75, }; /** YYYY-MM-DD of a UTC instant rendered in the church timezone. */ const localDay = (d: Date) => `${d.getUTCFullYear()}-${String(d.getUTCMonth() + 1).padStart(2, "0")}-${String( d.getUTCDate(), ).padStart(2, "0")}`; describe("planInstances — window boundary", () => { it("emits exactly the Sundays in [now, now + 3 weeks]", () => { const now = new Date(Date.UTC(2026, 2, 1, 12, 0, 0)); // 2026-03-01 (a Sunday) const target = windowTarget(now, 3); const plan = planInstances(weeklyService, now, target); const days = plan.map((i) => i.occursOn); expect(days).toEqual(["2026-03-01", "2026-03-08", "2026-03-15", "2026-03-22"]); }); it("endsAt is start + duration", () => { const now = new Date(Date.UTC(2026, 2, 1, 12, 0, 0)); const target = windowTarget(now, 1); const [first] = planInstances(weeklyService, now, target); expect((first.endsAt.getTime() - first.startsAt.getTime()) / 60_000).toBe(75); }); }); describe("planInstances — DST-safe instants", () => { it("keeps 9 AM local across the spring-forward Sunday", () => { // 2026-03-01 is EST; 2026-03-08 is the spring-forward Sunday (EDT). const now = new Date(Date.UTC(2026, 2, 1, 12, 0, 0)); const target = windowTarget(now, 2); const plan = planInstances(weeklyService, now, target); const before = plan.find((i) => i.occursOn === "2026-03-01")!; const after = plan.find((i) => i.occursOn === "2026-03-08")!; expect(before.startsAt.getUTCHours()).toBe(14); // 09:00 EST (UTC-5) expect(after.startsAt.getUTCHours()).toBe(13); // 09:00 EDT (UTC-4) }); }); describe("planInstances — deterministic (drives idempotent upsert)", () => { it("produces the same instants for identical input", () => { const now = new Date(Date.UTC(2026, 2, 1, 12, 0, 0)); const target = windowTarget(now, 2); const a = planInstances(weeklyService, now, target); const b = planInstances(weeklyService, now, target); expect(a.map((i) => i.startsAt.getTime())).toEqual(b.map((i) => i.startsAt.getTime())); expect(a.map((i) => i.occursOn)).toEqual(b.map((i) => i.occursOn)); }); }); describe("planInstances — biweekly phase", () => { it("lands every other Sunday on a stable phase", () => { const service = { ...weeklyService, rrule: "FREQ=WEEKLY;INTERVAL=2;BYDAY=SU" }; const now = new Date(Date.UTC(2026, 0, 4, 12, 0, 0)); // 2026-01-04 (anchor Sunday) const target = windowTarget(now, 3); const days = planInstances(service, now, target).map((i) => i.occursOn); expect(days).toEqual(["2026-01-04", "2026-01-18"]); }); }); describe("planInstances — monthly nth weekday", () => { it("plans the 1st Sunday across a year boundary", () => { const service = { ...weeklyService, rrule: "FREQ=MONTHLY;BYDAY=1SU" }; const now = new Date(Date.UTC(2026, 11, 1, 12, 0, 0)); // 2026-12-01 const target = windowTarget(now, 10); const days = planInstances(service, now, target).map((i) => i.occursOn); expect(days).toEqual(["2026-12-06", "2027-01-03", "2027-02-07"]); }); }); describe("windowTarget", () => { it("defaults to a 16-week window", () => { const now = new Date(Date.UTC(2026, 0, 1, 12, 0, 0)); const target = windowTarget(now); expect(INSTANCE_WINDOW_WEEKS).toBe(16); // 16 weeks = 112 days from Jan 1. expect(localDay(target)).toBe("2026-04-23"); }); });