|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import type { INotificationService, NotificationMessage, NotificationResult } from './notification-service'; |
| 3 | + |
| 4 | +describe('Notification Service Contract', () => { |
| 5 | + it('should allow a minimal INotificationService implementation with required methods', () => { |
| 6 | + const service: INotificationService = { |
| 7 | + send: async (_message) => ({ success: true }), |
| 8 | + }; |
| 9 | + |
| 10 | + expect(typeof service.send).toBe('function'); |
| 11 | + }); |
| 12 | + |
| 13 | + it('should allow a full implementation with optional methods', () => { |
| 14 | + const service: INotificationService = { |
| 15 | + send: async () => ({ success: true }), |
| 16 | + sendBatch: async (messages) => messages.map(() => ({ success: true })), |
| 17 | + getChannels: () => ['email', 'sms', 'push'], |
| 18 | + }; |
| 19 | + |
| 20 | + expect(service.sendBatch).toBeDefined(); |
| 21 | + expect(service.getChannels).toBeDefined(); |
| 22 | + }); |
| 23 | + |
| 24 | + it('should send a notification successfully', async () => { |
| 25 | + const sent: NotificationMessage[] = []; |
| 26 | + |
| 27 | + const service: INotificationService = { |
| 28 | + send: async (message): Promise<NotificationResult> => { |
| 29 | + sent.push(message); |
| 30 | + return { success: true, messageId: `msg-${sent.length}` }; |
| 31 | + }, |
| 32 | + }; |
| 33 | + |
| 34 | + const result = await service.send({ |
| 35 | + channel: 'email', |
| 36 | + to: 'user@example.com', |
| 37 | + subject: 'Welcome', |
| 38 | + body: 'Hello, welcome to the platform!', |
| 39 | + }); |
| 40 | + |
| 41 | + expect(result.success).toBe(true); |
| 42 | + expect(result.messageId).toBe('msg-1'); |
| 43 | + expect(sent).toHaveLength(1); |
| 44 | + expect(sent[0].channel).toBe('email'); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should handle notification failures', async () => { |
| 48 | + const service: INotificationService = { |
| 49 | + send: async (_message): Promise<NotificationResult> => ({ |
| 50 | + success: false, |
| 51 | + error: 'Invalid recipient', |
| 52 | + }), |
| 53 | + }; |
| 54 | + |
| 55 | + const result = await service.send({ |
| 56 | + channel: 'sms', |
| 57 | + to: '', |
| 58 | + body: 'Test', |
| 59 | + }); |
| 60 | + |
| 61 | + expect(result.success).toBe(false); |
| 62 | + expect(result.error).toBe('Invalid recipient'); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should send to multiple recipients', async () => { |
| 66 | + const delivered: string[] = []; |
| 67 | + |
| 68 | + const service: INotificationService = { |
| 69 | + send: async (message) => { |
| 70 | + const recipients = Array.isArray(message.to) ? message.to : [message.to]; |
| 71 | + delivered.push(...recipients); |
| 72 | + return { success: true }; |
| 73 | + }, |
| 74 | + }; |
| 75 | + |
| 76 | + await service.send({ |
| 77 | + channel: 'push', |
| 78 | + to: ['user-1', 'user-2', 'user-3'], |
| 79 | + body: 'New update available', |
| 80 | + }); |
| 81 | + |
| 82 | + expect(delivered).toEqual(['user-1', 'user-2', 'user-3']); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should support batch sending', async () => { |
| 86 | + const service: INotificationService = { |
| 87 | + send: async () => ({ success: true }), |
| 88 | + sendBatch: async (messages) => |
| 89 | + messages.map((_, i) => ({ |
| 90 | + success: i !== 1, // Second message fails |
| 91 | + messageId: `msg-${i}`, |
| 92 | + error: i === 1 ? 'Rate limited' : undefined, |
| 93 | + })), |
| 94 | + }; |
| 95 | + |
| 96 | + const results = await service.sendBatch!([ |
| 97 | + { channel: 'email', to: 'a@test.com', body: 'Hello A' }, |
| 98 | + { channel: 'email', to: 'b@test.com', body: 'Hello B' }, |
| 99 | + { channel: 'email', to: 'c@test.com', body: 'Hello C' }, |
| 100 | + ]); |
| 101 | + |
| 102 | + expect(results).toHaveLength(3); |
| 103 | + expect(results[0].success).toBe(true); |
| 104 | + expect(results[1].success).toBe(false); |
| 105 | + expect(results[1].error).toBe('Rate limited'); |
| 106 | + expect(results[2].success).toBe(true); |
| 107 | + }); |
| 108 | + |
| 109 | + it('should list available channels', () => { |
| 110 | + const service: INotificationService = { |
| 111 | + send: async () => ({ success: true }), |
| 112 | + getChannels: () => ['email', 'sms', 'in-app'], |
| 113 | + }; |
| 114 | + |
| 115 | + const channels = service.getChannels!(); |
| 116 | + expect(channels).toContain('email'); |
| 117 | + expect(channels).toContain('sms'); |
| 118 | + expect(channels).toContain('in-app'); |
| 119 | + }); |
| 120 | + |
| 121 | + it('should support template-based notifications', async () => { |
| 122 | + const sent: NotificationMessage[] = []; |
| 123 | + |
| 124 | + const service: INotificationService = { |
| 125 | + send: async (message) => { |
| 126 | + sent.push(message); |
| 127 | + return { success: true }; |
| 128 | + }, |
| 129 | + }; |
| 130 | + |
| 131 | + await service.send({ |
| 132 | + channel: 'email', |
| 133 | + to: 'user@example.com', |
| 134 | + body: '', |
| 135 | + templateId: 'welcome-email', |
| 136 | + templateData: { userName: 'Alice', activationUrl: 'https://example.com/activate' }, |
| 137 | + }); |
| 138 | + |
| 139 | + expect(sent[0].templateId).toBe('welcome-email'); |
| 140 | + expect(sent[0].templateData?.userName).toBe('Alice'); |
| 141 | + }); |
| 142 | +}); |
0 commit comments