/** * RED scaffold — reminderScheduler (Plan 05-06 turns this GREEN). * * Asserts that the reminder scan: * - Selects ONLY shared (isShared=true) AND timed (allDay=false) events * in the [now+14min, now+16min] window (D-05/D-06/D-07) * - Does NOT dispatch reminders for all-day events (D-07) * - Does NOT dispatch reminders for non-shared (personal) events (D-05) * - Does NOT dispatch the same (eventUid, minuteBucket) twice within the same minute * * These tests fail now because reminderScheduler.ts does not yet exist. * Run: pnpm --filter @familysync/api exec vitest run tests/broker/reminderScheduler.test.ts */ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest' // Mock the DB so we can control what events are returned vi.mock('../../src/db/client.js', () => ({ db: { select: vi.fn(), }, })) // Mock pushDispatcher so no real push occurs vi.mock('../../src/lib/pushDispatcher.js', () => ({ dispatchPush: vi.fn().mockResolvedValue(undefined), })) describe('reminderScheduler — shared+timed event filtering (D-05/D-06/D-07)', () => { beforeEach(() => { vi.useFakeTimers() vi.resetModules() }) afterEach(() => { vi.useRealTimers() vi.clearAllMocks() }) it('does not dispatch reminders for all-day events (D-07)', async () => { const now = new Date('2026-06-15T10:00:00Z') vi.setSystemTime(now) const { db } = await import('../../src/db/client.js') const { dispatchPush } = await import('../../src/lib/pushDispatcher.js') const { runReminderCheck } = await import('../../src/broker/reminderScheduler.js') // Simulate: query returns an all-day event (allDay=true) // The scheduler should have filtered this out in SQL — return empty results vi.mocked(db.select).mockReturnValue({ from: vi.fn().mockReturnValue({ innerJoin: vi.fn().mockReturnValue({ innerJoin: vi.fn().mockReturnValue({ where: vi.fn().mockResolvedValue([]), }), }), }), } as never) await runReminderCheck() expect(vi.mocked(dispatchPush)).not.toHaveBeenCalled() }) it('does not dispatch reminders for non-shared (personal) calendar events (D-05)', async () => { const now = new Date('2026-06-15T10:00:00Z') vi.setSystemTime(now) const { db } = await import('../../src/db/client.js') const { dispatchPush } = await import('../../src/lib/pushDispatcher.js') const { runReminderCheck } = await import('../../src/broker/reminderScheduler.js') // SQL WHERE clause must include isShared=true; no personal events returned vi.mocked(db.select).mockReturnValue({ from: vi.fn().mockReturnValue({ innerJoin: vi.fn().mockReturnValue({ innerJoin: vi.fn().mockReturnValue({ where: vi.fn().mockResolvedValue([]), }), }), }), } as never) await runReminderCheck() expect(vi.mocked(dispatchPush)).not.toHaveBeenCalled() }) it('does not dispatch the same (eventUid, minuteBucket) pair twice within the same run', async () => { const now = new Date('2026-06-15T10:00:00Z') vi.setSystemTime(now) const { db } = await import('../../src/db/client.js') const { dispatchPush } = await import('../../src/lib/pushDispatcher.js') const { runReminderCheck } = await import('../../src/broker/reminderScheduler.js') const sharedTimedEvent = { uid: 'event-uid-123', title: 'Team standup', dtstartUtc: new Date('2026-06-15T10:15:00Z'), // 15 min from now allDay: false, isShared: true, subscriptions: [], } vi.mocked(db.select) .mockReturnValueOnce({ from: vi.fn().mockReturnValue({ innerJoin: vi.fn().mockReturnValue({ innerJoin: vi.fn().mockReturnValue({ where: vi.fn().mockResolvedValue([sharedTimedEvent]), }), }), }), } as never) .mockReturnValue({ from: vi.fn().mockReturnValue({ innerJoin: vi.fn().mockReturnValue({ innerJoin: vi.fn().mockReturnValue({ where: vi.fn().mockResolvedValue([sharedTimedEvent]), }), }), }), } as never) // First run — should dispatch await runReminderCheck() const firstCallCount = vi.mocked(dispatchPush).mock.calls.length // Second run in the same minute — same (uid, minuteBucket) — should NOT dispatch again await runReminderCheck() const secondCallCount = vi.mocked(dispatchPush).mock.calls.length expect(secondCallCount).toBe(firstCallCount) }) })