/** * RED scaffold — eventChangeDispatcher (Plan 05-05 turns this GREEN). * * Asserts that dispatchEventChange: * - Fires for new events (operation='create') * - Fires for updated events with meaningful changes: time/date/title/location (D-04) * - Does NOT fire for description-only edits (D-04) * - Excludes the actor's own push subscriptions (D-03) * * These tests fail now because eventChangeDispatcher.ts does not yet exist. * Run: pnpm --filter @familysync/api exec vitest run tests/lib/eventChangeDispatcher.test.ts */ import { describe, it, expect, vi, beforeEach } from 'vitest' // Mock DB vi.mock('../../src/db/client.js', () => ({ db: { select: vi.fn().mockReturnValue({ from: vi.fn().mockReturnValue({ where: vi.fn().mockResolvedValue([]), }), }), }, })) // Mock pushDispatcher vi.mock('../../src/lib/pushDispatcher.js', () => ({ dispatchPush: vi.fn().mockResolvedValue(undefined), })) describe('eventChangeDispatcher — trigger conditions (D-04)', () => { beforeEach(() => { vi.clearAllMocks() vi.resetModules() }) it('dispatches for a new event (operation=create)', async () => { const { db } = await import('../../src/db/client.js') const { dispatchPush } = await import('../../src/lib/pushDispatcher.js') const { dispatchEventChange } = await import('../../src/lib/eventChangeDispatcher.js') const otherUserSub = { id: 2, userId: 2, endpoint: 'https://push.example.com/sub/2', p256dh: 'x', auth: 'y' } vi.mocked(db.select).mockReturnValue({ from: vi.fn().mockReturnValue({ where: vi.fn().mockResolvedValue([otherUserSub]), }), } as never) await dispatchEventChange( { uid: 'new-event-uid', title: 'Doctor appointment', operation: 'create' }, /* actorUserId */ 1, ) expect(vi.mocked(dispatchPush)).toHaveBeenCalled() }) it('dispatches for an event with a title change', async () => { const { db } = await import('../../src/db/client.js') const { dispatchPush } = await import('../../src/lib/pushDispatcher.js') const { dispatchEventChange } = await import('../../src/lib/eventChangeDispatcher.js') const otherUserSub = { id: 2, userId: 2, endpoint: 'https://push.example.com/sub/2', p256dh: 'x', auth: 'y' } vi.mocked(db.select).mockReturnValue({ from: vi.fn().mockReturnValue({ where: vi.fn().mockResolvedValue([otherUserSub]), }), } as never) await dispatchEventChange( { uid: 'event-uid-1', title: 'Renamed Event', operation: 'update', changedFields: ['title'], }, /* actorUserId */ 1, ) expect(vi.mocked(dispatchPush)).toHaveBeenCalled() }) it('does NOT dispatch for a description-only edit (D-04)', async () => { const { db } = await import('../../src/db/client.js') const { dispatchPush } = await import('../../src/lib/pushDispatcher.js') const { dispatchEventChange } = await import('../../src/lib/eventChangeDispatcher.js') vi.mocked(db.select).mockReturnValue({ from: vi.fn().mockReturnValue({ where: vi.fn().mockResolvedValue([ { id: 2, userId: 2, endpoint: 'https://push.example.com/sub/2', p256dh: 'x', auth: 'y' }, ]), }), } as never) await dispatchEventChange( { uid: 'event-uid-2', title: 'Team lunch', operation: 'update', changedFields: ['description'], // description-only — must NOT fire }, /* actorUserId */ 1, ) expect(vi.mocked(dispatchPush)).not.toHaveBeenCalled() }) it('excludes the actor user subscriptions from dispatch (D-03)', async () => { const { db } = await import('../../src/db/client.js') const { dispatchPush } = await import('../../src/lib/pushDispatcher.js') const { dispatchEventChange } = await import('../../src/lib/eventChangeDispatcher.js') // DB returns only the actor's own subscription (userId 1) vi.mocked(db.select).mockReturnValue({ from: vi.fn().mockReturnValue({ where: vi.fn().mockResolvedValue([ { id: 1, userId: 1, endpoint: 'https://push.example.com/sub/1', p256dh: 'x', auth: 'y' }, ]), }), } as never) await dispatchEventChange( { uid: 'event-uid-3', title: 'Soccer practice', operation: 'create' }, /* actorUserId */ 1, // actor is userId=1 — their subscription must be excluded ) // No subscriptions remain after excluding the actor — nothing dispatched expect(vi.mocked(dispatchPush)).not.toHaveBeenCalled() }) })