8fb6459b0d
Service instance generation (ARCHITECTURE §5.1): for each active service, plan instances in the rolling window and upsert on (service_id, starts_at). onConflictDoNothing makes the whole thing idempotent — a re-run creates 0 rows, cancelled/published instances are never touched, past instances are never deleted. - src/domain/instances.ts: pure planInstances() + windowTarget(); DST-safe instants via localToUtc, no DB imports. - src/domain/instances.test.ts: 7 tests (window boundary, DST-safe instants, determinism, biweekly phase, monthly-nth, windowTarget). - src/db/instances.ts: generateInstances() idempotent upsert; occurs_on stored as zoned midnight (no UTC-midnight shift). - src/app/api/dev/cron/instances/route.ts: GET drives the daily job. No schema change (service_instances unique index predates this unit; slot materialization stays deferred to D3). Verified: run twice -> 0 created, count unchanged; published instances byte-identical; generated instances stored as DST-safe UTC instants.
89 lines
3.6 KiB
TypeScript
89 lines
3.6 KiB
TypeScript
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");
|
|
});
|
|
});
|