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)
This commit is contained in:
Lucas Berger
2026-06-09 20:50:35 -04:00
parent 2cae72e9dd
commit ef558b65be
7 changed files with 619 additions and 2 deletions
+75
View File
@@ -0,0 +1,75 @@
/**
* RED scaffold — pushCoalescer (Plan 05-03 turns this GREEN).
*
* Asserts that coalesceListPush:
* - Fires the dispatch function exactly ONCE when N calls arrive within the coalesce window
* - Passes the correct count (N) to the dispatch function
* - Passes the actor's own userId as excludeUserId to suppress self-notifications (D-03)
*
* These tests fail now because pushCoalescer.ts does not yet exist.
* Run: pnpm --filter @familysync/api exec vitest run tests/lib/pushCoalescer.test.ts
*/
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
describe('pushCoalescer — list-change burst coalescing (D-01)', () => {
beforeEach(() => {
vi.useFakeTimers()
vi.resetModules()
})
afterEach(() => {
vi.useRealTimers()
})
it('collapses N rapid coalesceListPush calls into a single dispatch with count=N', async () => {
const dispatch = vi.fn().mockResolvedValue(undefined)
const { coalesceListPush } = await import('../../src/lib/pushCoalescer.js')
const listId = 1
const actorId = 10
const N = 5
for (let i = 0; i < N; i++) {
coalesceListPush(listId, actorId, dispatch)
}
// Advance time past the coalesce window
await vi.runAllTimersAsync()
expect(dispatch).toHaveBeenCalledTimes(1)
const [calledListId, calledActorId, calledCount] = dispatch.mock.calls[0]
expect(calledListId).toBe(listId)
expect(calledCount).toBe(N)
})
it('passes the actor userId as excludeUserId so the actor does not notify themselves (D-03)', async () => {
const dispatch = vi.fn().mockResolvedValue(undefined)
const { coalesceListPush } = await import('../../src/lib/pushCoalescer.js')
const listId = 2
const actorId = 99
coalesceListPush(listId, actorId, dispatch)
await vi.runAllTimersAsync()
expect(dispatch).toHaveBeenCalledTimes(1)
const [, calledActorId] = dispatch.mock.calls[0]
expect(calledActorId).toBe(actorId)
})
it('fires separate dispatches for different lists independently', async () => {
const dispatch = vi.fn().mockResolvedValue(undefined)
const { coalesceListPush } = await import('../../src/lib/pushCoalescer.js')
coalesceListPush(1, 10, dispatch)
coalesceListPush(1, 10, dispatch)
coalesceListPush(2, 10, dispatch) // different list
coalesceListPush(2, 10, dispatch)
await vi.runAllTimersAsync()
// Two separate dispatches — one per distinct listId
expect(dispatch).toHaveBeenCalledTimes(2)
})
})