From affd356677ece86de899744ec649d464d159fa2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Bombeck?= Date: Thu, 6 Aug 2026 04:59:50 +0200 Subject: [PATCH] test: retain sustained safety event claims - exercise the shared notification claim boundary\n- preserve rolling sustained-run dedup coverage --- .../__tests__/safety-floor-sustained.test.ts | 57 ++++++++++--------- 1 file changed, 30 insertions(+), 27 deletions(-) diff --git a/src/lib/illness/__tests__/safety-floor-sustained.test.ts b/src/lib/illness/__tests__/safety-floor-sustained.test.ts index 9c9866cc2..70f0f4915 100644 --- a/src/lib/illness/__tests__/safety-floor-sustained.test.ts +++ b/src/lib/illness/__tests__/safety-floor-sustained.test.ts @@ -12,8 +12,13 @@ */ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; -/** In-memory record event stream so the 24h dedupe behaves across calls. */ -const ledger: Array<{ recordUserId: string; dedupKey: string }> = []; +/** In-memory event claims so the 24h dedupe behaves across calls. */ +const ledger: Array<{ + recordUserId: string; + eventType: string; + dedupKey: string; + createdAt: Date; +}> = []; const measurementFindMany = vi.fn(); const dispatchMock = vi.fn(); @@ -25,31 +30,6 @@ vi.mock("@/lib/db", () => ({ user: { findUnique: vi.fn().mockResolvedValue({ managedProfileAt: null }), }, - notificationEvent: { - findFirst: ({ - where, - }: { - where: { recordUserId: string; dedupKey: string }; - }) => { - const hit = ledger.find( - (r) => - r.recordUserId === where.recordUserId && - r.dedupKey === where.dedupKey, - ); - return Promise.resolve(hit ? { id: "prior" } : null); - }, - create: ({ - data, - }: { - data: { recordUserId: string; dedupKey: string }; - }) => { - ledger.push({ - recordUserId: data.recordUserId, - dedupKey: data.dedupKey, - }); - return Promise.resolve({}); - }, - }, }, })); @@ -65,6 +45,29 @@ vi.mock("@/lib/notifications/dispatch-localised", () => ({ dispatchLocalisedNotification: (...a: unknown[]) => dispatchMock(...a), })); +vi.mock("@/lib/notifications/reminder-dedup", () => ({ + claimNotificationEvent: ( + _prisma: unknown, + input: { + recordUserId: string; + eventType: string; + dedupKey: string; + since: Date; + }, + ) => { + const existing = ledger.find( + (event) => + event.recordUserId === input.recordUserId && + event.eventType === input.eventType && + event.dedupKey === input.dedupKey && + event.createdAt >= input.since, + ); + if (existing) return Promise.resolve(false); + ledger.push({ ...input, createdAt: new Date() }); + return Promise.resolve(true); + }, +})); + import { runSafetyFloorCheck } from "../safety-floor-check"; /** A measured-at on a given UTC calendar day, mid-morning. */