Files
familysync/apps/api/tests/lib/eventChangeDispatcher.test.ts
T
Lucas Berger ef558b65be test(05-01): add Wave-0 RED scaffolds + VAPID fixture + setup truncation
- tests/fixtures/vapid.ts: static TEST_VAPID keypair for offline unit tests
- tests/lib/pushDispatcher.test.ts: RED — 410/404 prune + 201/5xx no-delete
- tests/lib/pushCoalescer.test.ts: RED — burst coalesce fires once with count=N; excludeUserId
- tests/broker/reminderScheduler.test.ts: RED — shared+timed filter; dedup by (uid,minuteBucket)
- tests/lib/eventChangeDispatcher.test.ts: RED — create/meaningful-update fires; description-only silent; actor excluded
- tests/routes/push.test.ts: RED — POST 201/401; DELETE removes rows; GET vapid-public-key
- test/setup.ts: import pushSubscriptions + add db.delete(pushSubscriptions) in afterEach
- all 5 RED files fail on missing-module (correct; implementations in Plans 05-02..05-06)
2026-06-09 20:50:35 -04:00

132 lines
4.5 KiB
TypeScript

/**
* 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()
})
})