From 3e0eb77b4d19f3f2ed48fe502e4530317a86fe66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Bombeck?= Date: Fri, 22 May 2026 08:44:00 +0200 Subject: [PATCH] test(intake): pin wall-clock on idempotency tests Both 'is idempotent on a second pass' and 'does not double-mint when today's rows already exist' hard-coded the scheduledFor to 2026-05-21T05:00Z while the route's projection uses new Date(). On 2026-05-22 the projected day diverged from the existence-row date and createMany fired, breaking the regression guard. vi.setSystemTime pins both ends to the same calendar day. --- src/app/api/dashboard/summary/__tests__/route.test.ts | 8 ++++++++ src/app/api/medications/intake/__tests__/route.test.ts | 7 +++++++ 2 files changed, 15 insertions(+) diff --git a/src/app/api/dashboard/summary/__tests__/route.test.ts b/src/app/api/dashboard/summary/__tests__/route.test.ts index 30d0a50c6..92e523fec 100644 --- a/src/app/api/dashboard/summary/__tests__/route.test.ts +++ b/src/app/api/dashboard/summary/__tests__/route.test.ts @@ -604,6 +604,13 @@ describe("GET /api/dashboard/summary", () => { // slot (e.g. the intake route or the reminder worker minted it // first), the dashboard route's backfill `createMany` must not // fire — the route simply re-reads + returns. + // + // v1.4.47 — pin the wall-clock so the projected scheduledFor and + // the mocked existence row land on the same day regardless of + // when the suite runs (previously hard-coded 2026-05-21 — broke + // on 2026-05-22). + vi.useFakeTimers(); + vi.setSystemTime(new Date("2026-05-21T08:00:00.000Z")); vi.mocked(getSession).mockResolvedValue(SESSION_OK as never); const scheduledFor = new Date("2026-05-21T05:00:00.000Z"); vi.mocked(prisma.medication.findMany).mockResolvedValue([ @@ -646,6 +653,7 @@ describe("GET /api/dashboard/summary", () => { data: { compliance: { scheduledToday: number; takenToday: number } }; }; expect(body.data.compliance.scheduledToday).toBe(1); + vi.useRealTimers(); }); it("computes intake compliance for today", async () => { diff --git a/src/app/api/medications/intake/__tests__/route.test.ts b/src/app/api/medications/intake/__tests__/route.test.ts index 59d6b7b15..c5c4ccada 100644 --- a/src/app/api/medications/intake/__tests__/route.test.ts +++ b/src/app/api/medications/intake/__tests__/route.test.ts @@ -218,6 +218,12 @@ describe("GET /api/medications/intake", () => { }); it("does not double-mint when today's rows already exist", async () => { + // v1.4.47 — pin the wall-clock so the projected scheduledFor and + // the mocked existence row land on the same day regardless of + // when the suite runs (previously hard-coded 2026-05-21 — broke + // on 2026-05-22). + vi.useFakeTimers(); + vi.setSystemTime(new Date("2026-05-21T08:00:00.000Z")); vi.mocked(getSession).mockResolvedValue(SESSION_OK as never); const scheduledFor = new Date("2026-05-21T05:00:00.000Z"); vi.mocked(prisma.medication.findMany).mockResolvedValue([ @@ -256,6 +262,7 @@ describe("GET /api/medications/intake", () => { ); expect(res.status).toBe(200); expect(prisma.medicationIntakeEvent.createMany).not.toHaveBeenCalled(); + vi.useRealTimers(); }); it("returns compliance buckets for the last N days", async () => {