|
1 | 1 | import { NotificationsService } from './notifications.service'; |
2 | 2 |
|
| 3 | +jest.mock('ioredis', () => |
| 4 | + jest.fn().mockImplementation(() => ({ |
| 5 | + publish: jest.fn().mockResolvedValue(undefined), |
| 6 | + quit: jest.fn().mockResolvedValue(undefined), |
| 7 | + subscribe: jest.fn().mockResolvedValue(undefined), |
| 8 | + on: jest.fn(), |
| 9 | + })), |
| 10 | +); |
| 11 | + |
3 | 12 | describe('NotificationsService', () => { |
4 | | - it('should be defined', () => { |
5 | | - expect(NotificationsService).toBeDefined(); |
| 13 | + function createService() { |
| 14 | + const prisma: any = { |
| 15 | + pushToken: { |
| 16 | + findMany: jest.fn(), |
| 17 | + deleteMany: jest.fn().mockResolvedValue({ count: 0 }), |
| 18 | + }, |
| 19 | + }; |
| 20 | + const jobsQueueService: any = { |
| 21 | + enqueueNotification: jest.fn().mockResolvedValue(undefined), |
| 22 | + }; |
| 23 | + const config: any = { |
| 24 | + redisUrl: 'redis://localhost:6379', |
| 25 | + }; |
| 26 | + const service = new NotificationsService(config, prisma, jobsQueueService); |
| 27 | + |
| 28 | + (service as any).publisher = { |
| 29 | + publish: jest.fn().mockResolvedValue(undefined), |
| 30 | + quit: jest.fn().mockResolvedValue(undefined), |
| 31 | + }; |
| 32 | + (service as any).subscriber = { |
| 33 | + subscribe: jest.fn().mockResolvedValue(undefined), |
| 34 | + on: jest.fn(), |
| 35 | + quit: jest.fn().mockResolvedValue(undefined), |
| 36 | + }; |
| 37 | + (service as any).expo = { |
| 38 | + chunkPushNotifications: jest.fn((messages) => [messages]), |
| 39 | + sendPushNotificationsAsync: jest.fn(), |
| 40 | + }; |
| 41 | + |
| 42 | + return { service, prisma, jobsQueueService }; |
| 43 | + } |
| 44 | + |
| 45 | + it('queues a notification event and background job', async () => { |
| 46 | + const { service, jobsQueueService } = createService(); |
| 47 | + |
| 48 | + await service.sendPushNotification(['user-1'], { |
| 49 | + type: 'expense_created', |
| 50 | + title: 'Expense added', |
| 51 | + body: 'Dinner was added', |
| 52 | + }); |
| 53 | + |
| 54 | + expect(jobsQueueService.enqueueNotification).toHaveBeenCalledWith({ |
| 55 | + userIds: ['user-1'], |
| 56 | + payload: { |
| 57 | + type: 'expense_created', |
| 58 | + title: 'Expense added', |
| 59 | + body: 'Dinner was added', |
| 60 | + }, |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + it('removes invalid Expo tokens after a push send response', async () => { |
| 65 | + const { service, prisma } = createService(); |
| 66 | + prisma.pushToken.findMany.mockResolvedValue([ |
| 67 | + { token: 'ExponentPushToken[validToken]' }, |
| 68 | + { token: 'ExponentPushToken[invalidToken]' }, |
| 69 | + ]); |
| 70 | + (service as any).expo.sendPushNotificationsAsync.mockResolvedValue([ |
| 71 | + { status: 'ok', id: 'ticket-1' }, |
| 72 | + { status: 'error', details: { error: 'DeviceNotRegistered' } }, |
| 73 | + ]); |
| 74 | + |
| 75 | + await service.processQueuedNotification({ |
| 76 | + userIds: ['user-1'], |
| 77 | + payload: { |
| 78 | + type: 'group_invite', |
| 79 | + title: 'Invite', |
| 80 | + body: 'You were invited', |
| 81 | + }, |
| 82 | + }); |
| 83 | + |
| 84 | + expect(prisma.pushToken.deleteMany).toHaveBeenCalledWith({ |
| 85 | + where: { |
| 86 | + token: { |
| 87 | + in: ['ExponentPushToken[invalidToken]'], |
| 88 | + }, |
| 89 | + }, |
| 90 | + }); |
| 91 | + }); |
| 92 | + |
| 93 | + it('retries Expo delivery with exponential backoff for transient failures', async () => { |
| 94 | + const { service, prisma } = createService(); |
| 95 | + prisma.pushToken.findMany.mockResolvedValue([{ token: 'ExponentPushToken[retryToken]' }]); |
| 96 | + const sendPushNotificationsAsync = (service as any).expo.sendPushNotificationsAsync as jest.Mock; |
| 97 | + sendPushNotificationsAsync |
| 98 | + .mockRejectedValueOnce(new Error('transient failure')) |
| 99 | + .mockResolvedValueOnce([{ status: 'ok', id: 'ticket-1' }]); |
| 100 | + |
| 101 | + const setTimeoutSpy = jest.spyOn(global, 'setTimeout').mockImplementation((callback: TimerHandler) => { |
| 102 | + if (typeof callback === 'function') { |
| 103 | + callback(); |
| 104 | + } |
| 105 | + return 0 as unknown as ReturnType<typeof setTimeout>; |
| 106 | + }); |
| 107 | + |
| 108 | + await service.processQueuedNotification({ |
| 109 | + userIds: ['user-1'], |
| 110 | + payload: { |
| 111 | + type: 'settlement_created', |
| 112 | + title: 'Settlement', |
| 113 | + body: 'A settlement was recorded', |
| 114 | + }, |
| 115 | + }); |
| 116 | + |
| 117 | + expect(sendPushNotificationsAsync).toHaveBeenCalledTimes(2); |
| 118 | + expect(setTimeoutSpy).toHaveBeenCalledWith(expect.any(Function), 500); |
| 119 | + |
| 120 | + setTimeoutSpy.mockRestore(); |
6 | 121 | }); |
7 | 122 | }); |
0 commit comments