diff --git a/src/__tests__/guardian-notification-policy.test.ts b/src/__tests__/guardian-notification-policy.test.ts new file mode 100644 index 000000000..23fae3050 --- /dev/null +++ b/src/__tests__/guardian-notification-policy.test.ts @@ -0,0 +1,108 @@ +import { beforeEach, describe, expect, it, vi } from "vitest"; + +const { findUnique, findMany } = vi.hoisted(() => ({ + findUnique: vi.fn(), + findMany: vi.fn(), +})); + +vi.mock("@/lib/db", () => ({ + prisma: { + user: { findUnique }, + accountGrant: { findMany }, + }, +})); + +import { + isManagedGuardianFanoutAllowed, + resolveManagedGuardianRecipientIds, +} from "@/lib/notifications/delivery-identity"; + +const recordUserId = "managed-record"; + +function payload(over: Record = {}) { + return { + eventType: "MEDICATION_REMINDER" as const, + userId: recordUserId, + title: "Record content", + message: "Record schedule", + ...over, + }; +} + +beforeEach(() => { + vi.resetAllMocks(); + findUnique.mockResolvedValue({ managedProfileAt: new Date() }); + findMany.mockResolvedValue([{ granteeId: "guardian-one" }]); +}); + +describe("guardian notification policy — allowlist and eligibility", () => { + it("allows exactly medication, measurement, low-stock, and explicitly tagged safety-floor alerts", () => { + expect(isManagedGuardianFanoutAllowed(payload())).toBe(true); + expect( + isManagedGuardianFanoutAllowed( + payload({ eventType: "MEASUREMENT_REMINDER" }), + ), + ).toBe(true); + expect( + isManagedGuardianFanoutAllowed( + payload({ eventType: "MEDICATION_LOW_STOCK" }), + ), + ).toBe(true); + expect( + isManagedGuardianFanoutAllowed( + payload({ + eventType: "SYSTEM_ALERT", + managedFanoutEvent: "SAFETY_FLOOR_ALERT", + }), + ), + ).toBe(true); + + expect( + isManagedGuardianFanoutAllowed(payload({ eventType: "SYSTEM_ALERT" })), + ).toBe(false); + expect( + isManagedGuardianFanoutAllowed( + payload({ eventType: "MEASUREMENT_ANOMALY" }), + ), + ).toBe(false); + expect( + isManagedGuardianFanoutAllowed( + payload({ + eventType: "SYSTEM_ALERT", + managedFanoutEvent: "ILLNESS_RED_FLAG", + }), + ), + ).toBe(false); + }); + + it("enumerates only active Guardians of the marked record", async () => { + await expect(resolveManagedGuardianRecipientIds(payload())).resolves.toEqual( + ["guardian-one"], + ); + + expect(findMany).toHaveBeenCalledWith({ + where: expect.objectContaining({ grantorId: recordUserId }), + select: { granteeId: true }, + }); + }); + + it("does not fan out unmarked records, disallowed events, self delivery, or invalid Guardians", async () => { + findUnique.mockResolvedValueOnce({ managedProfileAt: null }); + await expect(resolveManagedGuardianRecipientIds(payload())).resolves.toBeNull(); + + await expect( + resolveManagedGuardianRecipientIds( + payload({ eventType: "MEASUREMENT_ANOMALY" }), + ), + ).resolves.toBeNull(); + + findMany.mockResolvedValueOnce([ + { granteeId: recordUserId }, + { granteeId: "managed-guardian", managedProfileAt: new Date() }, + { granteeId: "guardian-two" }, + ]); + await expect(resolveManagedGuardianRecipientIds(payload())).resolves.toEqual([ + "guardian-two", + ]); + }); +});