forked from Lumina-eX/TaskChain
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotifications.test.ts
More file actions
576 lines (514 loc) · 17.3 KB
/
Copy pathnotifications.test.ts
File metadata and controls
576 lines (514 loc) · 17.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
import { describe, it, expect, vi, beforeEach } from 'vitest'
import { NextRequest } from 'next/server'
vi.mock('@/lib/db', () => ({
sql: vi.fn(),
}))
vi.mock('@/lib/auth/middleware', async () => {
const actual = await vi.importActual<
typeof import('@/lib/auth/middleware')
>('@/lib/auth/middleware')
// Both auth wrappers inject the same fake AuthContext so the handler
// body runs as if a valid JWT was presented. Without mocking
// withAuthCtx too, PATCH routes (which use withAuthCtx to receive a
// Next.js route context) would fall through to the real wrapper and
// return 401.
const injectAuth = (handler: (...args: unknown[]) => unknown) =>
async (request: unknown, ...rest: unknown[]) =>
handler(
request,
{ walletAddress: '0xTEST_WALLET', tokenJti: 'test-jti' },
...rest,
)
return {
...actual,
withAuth: injectAuth,
withAuthCtx: injectAuth,
resolveUserIdByWallet: vi.fn(async (walletAddress: string) => {
if (walletAddress === '0xUNRESOLVED') return null
return 42
}),
}
})
import { sql } from '@/lib/db'
import { GET as listNotifications } from '@/app/api/notifications/route'
import {
PATCH as markRead,
} from '@/app/api/notifications/[id]/read/route'
import {
POST as markAllRead,
} from '@/app/api/notifications/read-all/route'
import {
GET as streamNotifications,
} from '@/app/api/notifications/stream/route'
import {
NotificationError,
NotificationHub,
createNotification,
listNotificationsForUser,
mapNotificationRow,
markAllNotificationsRead,
markNotificationRead,
parseNotificationQuery,
notifyContractCreated,
notifyMilestoneApproved,
notifyMilestoneSubmitted,
notifyDisputeCreated,
notifyEscrowReleased,
NOTIFICATION_EVENT_TYPES,
NOTIFICATION_MAX_LIMIT,
type Notification,
} from '@/lib/notifications'
type SqlMock = ReturnType<typeof vi.fn>
interface NotificationRowOverrides {
id?: number
user_id?: number
title?: string
message?: string
type?: string
event_type?: string
payload?: Record<string, unknown>
is_read?: boolean
channel?: string
created_at?: Date | string
delivered_at?: Date | string | null
total_count?: string | number
}
function buildNotificationRow(
overrides: NotificationRowOverrides = {},
): Array<Record<string, unknown>> {
return [
{
id: 1,
user_id: 42,
title: 'Milestone Approved',
message: 'Milestone "Build login" was approved.',
type: 'success',
event_type: 'milestone_approved',
payload: { milestoneId: 7 },
is_read: false,
channel: 'in_app',
created_at: new Date('2026-01-01T00:00:00Z'),
delivered_at: null,
total_count: '3',
...overrides,
},
]
}
function queueSqlFail(error: unknown): void {
;(sql as unknown as SqlMock).mockRejectedValueOnce(error)
}
function queueSql(responses: unknown[]): void {
const mock = sql as unknown as SqlMock
for (const response of responses) {
mock.mockResolvedValueOnce(response)
}
}
function makeRequest(url: string): NextRequest {
return new NextRequest(new Request(url))
}
beforeEach(() => {
vi.clearAllMocks()
NotificationHub.resetInstance()
})
describe('parseNotificationQuery', () => {
it('returns default values for empty params', () => {
expect(parseNotificationQuery(new URLSearchParams())).toEqual({
page: 1,
limit: 20,
type: null,
unreadOnly: false,
})
})
it('clamps limit to the maximum', () => {
const params = parseNotificationQuery(
new URLSearchParams(`limit=${NOTIFICATION_MAX_LIMIT * 5}`),
)
expect(params.limit).toBe(NOTIFICATION_MAX_LIMIT)
})
it('accepts a known event_type', () => {
const params = parseNotificationQuery(
new URLSearchParams('type=milestone_submitted'),
)
expect(params.type).toBe('milestone_submitted')
})
it('rejects an unknown event_type', () => {
expect(() =>
parseNotificationQuery(new URLSearchParams('type=banana')),
).toThrowError(NotificationError)
})
it('accepts truthy unreadOnly variants', () => {
expect(
parseNotificationQuery(new URLSearchParams('unreadOnly=true')).unreadOnly,
).toBe(true)
expect(
parseNotificationQuery(new URLSearchParams('unreadOnly=1')).unreadOnly,
).toBe(true)
})
it('rejects invalid page/limit', () => {
expect(() => parseNotificationQuery(new URLSearchParams('page=0')))
.toThrowError(NotificationError)
expect(() => parseNotificationQuery(new URLSearchParams('limit=0')))
.toThrowError(NotificationError)
})
it('rejects non-integer page/limit instead of silently defaulting', () => {
const pageErr = (() => {
try {
parseNotificationQuery(new URLSearchParams('page=banana'))
return null
} catch (e) {
return e
}
})()
expect(pageErr).toBeInstanceOf(NotificationError)
expect((pageErr as NotificationError).code).toBe('INVALID_PAGE')
const limitErr = (() => {
try {
parseNotificationQuery(new URLSearchParams('limit=banana'))
return null
} catch (e) {
return e
}
})()
expect(limitErr).toBeInstanceOf(NotificationError)
expect((limitErr as NotificationError).code).toBe('INVALID_LIMIT')
})
it('rejects empty page/limit strings', () => {
expect(() => parseNotificationQuery(new URLSearchParams('page=')))
.toThrowError(NotificationError)
expect(() => parseNotificationQuery(new URLSearchParams('limit=')))
.toThrowError(NotificationError)
})
})
describe('mapNotificationRow', () => {
it('converts a row into the API shape', () => {
const notification = mapNotificationRow({
id: 9,
user_id: 42,
title: 'T',
message: 'M',
type: 'success',
event_type: 'escrow_released',
payload: { foo: 'bar' },
is_read: false,
channel: 'in_app',
created_at: new Date('2026-02-02T00:00:00Z'),
delivered_at: null,
})
expect(notification).toMatchObject({
id: 9,
userId: 42,
title: 'T',
message: 'M',
type: 'success',
eventType: 'escrow_released',
payload: { foo: 'bar' },
isRead: false,
channel: 'in_app',
})
expect(notification.createdAt).toMatch(/2026-02-02T/)
expect(notification.deliveredAt).toBeNull()
})
})
describe('createNotification', () => {
it('persists and publishes to the hub', async () => {
queueSql([buildNotificationRow({ id: 101 })])
const hub = NotificationHub.getInstance()
const received: Notification[] = []
hub.subscribe(42, (n) => received.push(n))
const created = await createNotification({
userId: 42,
title: 'A',
message: 'B',
type: 'success',
eventType: 'milestone_approved',
payload: { milestoneId: 1 },
})
expect(created.id).toBe(101)
expect(received).toHaveLength(1)
expect(received[0].id).toBe(101)
})
it('still returns the persisted notification even if hub publish throws', async () => {
queueSql([buildNotificationRow({ id: 102 })])
const hub = NotificationHub.getInstance()
hub.subscribe(42, () => {
throw new Error('broken subscriber')
})
const created = await createNotification({
userId: 42,
title: 'A',
message: 'B',
type: 'success',
eventType: 'milestone_approved',
payload: {},
})
expect(created.id).toBe(102)
})
it('throws NotificationError when the INSERT returns no rows', async () => {
queueSql([[]])
await expect(
createNotification({
userId: 1,
title: 'A',
message: 'B',
type: 'info',
eventType: 'contract_created',
}),
).rejects.toBeInstanceOf(NotificationError)
})
})
describe('listNotificationsForUser', () => {
it('returns empty result for an out-of-range page', async () => {
queueSql([[]]) // main list empty (no COUNT fallback needed since 0 rows handled inline)
const result = await listNotificationsForUser(42, {
page: 99,
limit: 10,
type: null,
unreadOnly: false,
})
expect(result.totalItems).toBe(0)
expect(result.notifications).toEqual([])
})
it('returns paginated rows with total count via COUNT(*) OVER()', async () => {
queueSql([buildNotificationRow({ id: 1, total_count: '5' })])
const result = await listNotificationsForUser(42, {
page: 1,
limit: 1,
type: null,
unreadOnly: false,
})
expect(result.totalItems).toBe(5)
expect(result.notifications).toHaveLength(1)
expect(result.notifications[0].id).toBe(1)
})
it('rejects an invalid user id', async () => {
await expect(
listNotificationsForUser(0, {
page: 1,
limit: 10,
type: null,
unreadOnly: false,
}),
).rejects.toBeInstanceOf(NotificationError)
})
it('rejects page/limit pairs whose offset exceeds the DoS cap', async () => {
// With page=502, limit=100: offset = (502 - 1) * 100 = 50_100 > 50_000 cap,
// so PAGE_TOO_LARGE is thrown before any SQL is issued.
await expect(
listNotificationsForUser(42, {
page: 502,
limit: 100,
type: null,
unreadOnly: false,
}),
).rejects.toMatchObject({ code: 'PAGE_TOO_LARGE' })
})
it('accepts the post-cap boundary (page=501, limit=100, offset=50_000)', async () => {
// offset == cap -> allowed. The exact cap is inclusive (offset <= cap).
queueSql([buildNotificationRow({ id: 1, total_count: '1' })])
const result = await listNotificationsForUser(42, {
page: 501,
limit: 100,
type: null,
unreadOnly: false,
})
expect(result.totalItems).toBe(1)
expect(result.notifications).toHaveLength(1)
})
})
describe('markNotificationRead', () => {
it('returns null when the notification does not belong to the caller', async () => {
queueSql([[]])
const result = await markNotificationRead(99, 42)
expect(result.notification).toBeNull()
})
it('returns the mapped notification when marked', async () => {
queueSql([buildNotificationRow({ id: 1, is_read: true })])
const result = await markNotificationRead(1, 42)
expect(result.notification?.isRead).toBe(true)
})
})
describe('markAllNotificationsRead', () => {
it('returns the updated row count', async () => {
// markAllNotificationsRead now issues exactly ONE sql call (CTE).
queueSql([[{ updated_count: 7 }]])
const result = await markAllNotificationsRead(42)
expect(result.updatedCount).toBe(7)
})
})
describe('NotificationHub', () => {
it('subscriber count drops to zero after unsubscribe', () => {
const hub = NotificationHub.getInstance()
expect(hub.subscriberCount(42)).toBe(0)
const unsub = hub.subscribe(42, () => undefined)
expect(hub.subscriberCount(42)).toBe(1)
unsub()
expect(hub.subscriberCount(42)).toBe(0)
})
it('isolated fan-out: only the targeted user receives the notification', () => {
const hub = NotificationHub.getInstance()
const seen = { a: 0, b: 0 }
hub.subscribe(1, () => {
seen.a += 1
})
hub.subscribe(2, () => {
seen.b += 1
})
hub.publish({
id: 1,
userId: 1,
title: 'x',
message: 'x',
type: 'info',
eventType: 'contract_created',
payload: {},
isRead: false,
channel: 'in_app',
createdAt: '2026-01-01T00:00:00Z',
deliveredAt: null,
})
expect(seen).toEqual({ a: 1, b: 0 })
})
})
describe('GET /api/notifications', () => {
it('returns the list with meta', async () => {
// resolveUserIdByWallet is mocked, so no sql for the user lookup;
// the route issues 2 sql calls (listNotifications + unreadCount).
queueSql([
buildNotificationRow({ id: 1, total_count: '1' }), // list (with total_count)
[{ unread: 1 }], // unread count
])
const response = await listNotifications(
makeRequest('http://localhost/api/notifications?limit=5'),
)
expect(response.status).toBe(200)
const body = await response.json()
expect(body.data).toHaveLength(1)
expect(body.meta).toMatchObject({
totalCount: 1,
limit: 5,
page: 1,
unreadCount: 1,
})
})
it('400 with structured code on invalid type', async () => {
const response = await listNotifications(
makeRequest('http://localhost/api/notifications?type=banana'),
)
expect(response.status).toBe(400)
const body = await response.json()
expect(body.code).toBe('INVALID_TYPE')
})
it('503 on DB failure', async () => {
queueSqlFail(new Error('connection reset'))
const response = await listNotifications(
makeRequest('http://localhost/api/notifications'),
)
expect(response.status).toBe(503)
const body = await response.json()
expect(body.code).toBe('NOTIFICATIONS_LIST_FAILED')
})
})
describe('PATCH /api/notifications/[id]/read', () => {
it('marks the notification read for the caller', async () => {
// resolveUserIdByWallet is mocked; the route issue 1 sql call.
queueSql([buildNotificationRow({ id: 17, is_read: true })])
const response = await markRead(makeRequest('http://localhost/x'), {
params: Promise.resolve({ id: '17' }),
})
expect(response.status).toBe(200)
const body = await response.json()
expect(body.notification.isRead).toBe(true)
})
it('returns 404 when no row was updated', async () => {
queueSql([[]])
const response = await markRead(makeRequest('http://localhost/x'), {
params: Promise.resolve({ id: '17' }),
})
expect(response.status).toBe(404)
expect((await response.json()).code).toBe('NOT_FOUND')
})
it('returns 400 for a non-numeric id', async () => {
const response = await markRead(makeRequest('http://localhost/x'), {
params: Promise.resolve({ id: 'banana' }),
})
expect(response.status).toBe(400)
expect((await response.json()).code).toBe('INVALID_ID')
})
it('returns 404 when the wallet does not map to a user', async () => {
// Switch the mocked resolveUserIdByWallet to return null for this test.
const { resolveUserIdByWallet } = await import('@/lib/auth/middleware')
;(resolveUserIdByWallet as ReturnType<typeof vi.fn>).mockResolvedValueOnce(
null,
)
const response = await markRead(makeRequest('http://localhost/x'), {
params: Promise.resolve({ id: '17' }),
})
expect(response.status).toBe(404)
expect((await response.json()).code).toBe('USER_NOT_FOUND')
})
})
describe('POST /api/notifications/read-all', () => {
it('returns the updated count', async () => {
// resolveUserIdByWallet is mocked, so no sql call for the user
// lookup; the route then issues a single CTE sql call.
queueSql([[{ updated_count: 4 }]])
const response = await markAllRead(makeRequest('http://localhost/x'))
expect(response.status).toBe(200)
expect((await response.json()).updatedCount).toBe(4)
})
})
describe('GET /api/notifications/stream', () => {
it('returns a text/event-stream response for the authenticated user', async () => {
// resolveUserIdByWallet is mocked to return 42; no sql call needed.
const response = await streamNotifications(
makeRequest('http://localhost/api/notifications/stream'),
)
expect(response.status).toBe(200)
expect(response.headers.get('content-type')).toMatch(/text\/event-stream/)
expect(NotificationHub.getInstance().subscriberCount(42)).toBe(1)
})
it('drops subscribers on stream cancel', async () => {
const response = await streamNotifications(
makeRequest('http://localhost/api/notifications/stream'),
)
expect(response.status).toBe(200)
expect(NotificationHub.getInstance().subscriberCount(42)).toBe(1)
await response.body?.cancel()
// give the cancel() callback a tick to run
await Promise.resolve()
expect(NotificationHub.getInstance().subscriberCount(42)).toBe(0)
})
})
describe('event-creation helpers', () => {
it('notifyContractCreated emits one notification per recipient', async () => {
queueSql([
buildNotificationRow({ id: 1, user_id: 11, event_type: 'contract_created' }),
buildNotificationRow({ id: 2, user_id: 22, event_type: 'contract_created' }),
])
await notifyContractCreated(11, 22, 99, 'Logo redesign')
expect(NOTIFICATION_EVENT_TYPES).toContain('contract_created')
})
it('notifyMilestoneSubmitted creates exactly one notification', async () => {
queueSql([buildNotificationRow({ event_type: 'milestone_submitted' })])
await notifyMilestoneSubmitted(11, 5, 'Build login')
})
it('notifyMilestoneApproved creates exactly one notification', async () => {
queueSql([buildNotificationRow({ event_type: 'milestone_approved' })])
await notifyMilestoneApproved(11, 5, 'Build login')
})
it('notifyEscrowReleased skips the freelancer when null', async () => {
queueSql([buildNotificationRow({ event_type: 'escrow_released' })])
await notifyEscrowReleased(11, null, 7, '5.00', 'XLM')
})
it('notifyEscrowReleased includes the freelancer when provided', async () => {
queueSql([
buildNotificationRow({ id: 1, user_id: 11, event_type: 'escrow_released' }),
buildNotificationRow({ id: 2, user_id: 22, event_type: 'escrow_released' }),
])
await notifyEscrowReleased(11, 22, 7, '5.00', 'XLM')
})
it('notifyDisputeCreated creates exactly one notification', async () => {
queueSql([buildNotificationRow({ event_type: 'dispute_created' })])
await notifyDisputeCreated(11, 3, 7)
})
})