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
@@ -0,0 +1,131 @@
/**
* 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()
})
})
+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)
})
})
+96
View File
@@ -0,0 +1,96 @@
/**
* RED scaffold — pushDispatcher (Plan 05-02 turns this GREEN).
*
* Asserts that dispatchPush:
* - Deletes the push_subscriptions row when the push service returns 410 (Gone)
* - Deletes the push_subscriptions row when the push service returns 404 (Not Found)
* - Does NOT delete the row on 201 (success)
* - Does NOT delete the row on transient 5xx errors
*
* These tests fail now because pushDispatcher.ts does not yet exist.
* Run: pnpm --filter @familysync/api exec vitest run tests/lib/pushDispatcher.test.ts
*/
import { describe, it, expect, vi, beforeEach } from 'vitest'
import type { PushSubscription } from '../../src/lib/pushDispatcher.js'
// Mock web-push so no real network calls occur
vi.mock('web-push', () => ({
default: {
sendNotification: vi.fn(),
setVapidDetails: vi.fn(),
},
}))
// Mock the DB module so we can assert on deletes without a real database
vi.mock('../../src/db/client.js', () => ({
db: {
delete: vi.fn().mockReturnValue({
where: vi.fn().mockResolvedValue(undefined),
}),
},
}))
const FAKE_SUB: PushSubscription = {
id: 1,
userId: 42,
endpoint: 'https://push.example.com/sub/abc',
p256dh: 'fake_p256dh',
auth: 'fake_auth',
}
describe('pushDispatcher — 410/404 subscription pruning', () => {
beforeEach(() => {
vi.clearAllMocks()
})
it('deletes the subscription row when the push service returns 410 (Gone)', async () => {
const webpush = (await import('web-push')).default
const { db } = await import('../../src/db/client.js')
vi.mocked(webpush.sendNotification).mockRejectedValueOnce(
Object.assign(new Error('Gone'), { statusCode: 410 }),
)
const { dispatchPush } = await import('../../src/lib/pushDispatcher.js')
await dispatchPush(FAKE_SUB, { title: 'Test', body: 'Hello' })
expect(vi.mocked(db.delete)).toHaveBeenCalled()
})
it('deletes the subscription row when the push service returns 404 (Not Found)', async () => {
const webpush = (await import('web-push')).default
const { db } = await import('../../src/db/client.js')
vi.mocked(webpush.sendNotification).mockRejectedValueOnce(
Object.assign(new Error('Not Found'), { statusCode: 404 }),
)
const { dispatchPush } = await import('../../src/lib/pushDispatcher.js')
await dispatchPush(FAKE_SUB, { title: 'Test', body: 'Hello' })
expect(vi.mocked(db.delete)).toHaveBeenCalled()
})
it('does NOT delete the row on 201 success', async () => {
const webpush = (await import('web-push')).default
const { db } = await import('../../src/db/client.js')
vi.mocked(webpush.sendNotification).mockResolvedValueOnce({ statusCode: 201, body: '', headers: {} })
const { dispatchPush } = await import('../../src/lib/pushDispatcher.js')
await dispatchPush(FAKE_SUB, { title: 'Test', body: 'Hello' })
expect(vi.mocked(db.delete)).not.toHaveBeenCalled()
})
it('does NOT delete the row on transient 5xx error', async () => {
const webpush = (await import('web-push')).default
const { db } = await import('../../src/db/client.js')
vi.mocked(webpush.sendNotification).mockRejectedValueOnce(
Object.assign(new Error('Service Unavailable'), { statusCode: 503 }),
)
const { dispatchPush } = await import('../../src/lib/pushDispatcher.js')
await dispatchPush(FAKE_SUB, { title: 'Test', body: 'Hello' })
expect(vi.mocked(db.delete)).not.toHaveBeenCalled()
})
})