-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathnotification-service.test.ts
More file actions
142 lines (118 loc) · 4.26 KB
/
Copy pathnotification-service.test.ts
File metadata and controls
142 lines (118 loc) · 4.26 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
import { describe, it, expect } from 'vitest';
import type { INotificationService, NotificationMessage, NotificationResult } from './notification-service';
describe('Notification Service Contract', () => {
it('should allow a minimal INotificationService implementation with required methods', () => {
const service: INotificationService = {
send: async (_message) => ({ success: true }),
};
expect(typeof service.send).toBe('function');
});
it('should allow a full implementation with optional methods', () => {
const service: INotificationService = {
send: async () => ({ success: true }),
sendBatch: async (messages) => messages.map(() => ({ success: true })),
getChannels: () => ['email', 'sms', 'push'],
};
expect(service.sendBatch).toBeDefined();
expect(service.getChannels).toBeDefined();
});
it('should send a notification successfully', async () => {
const sent: NotificationMessage[] = [];
const service: INotificationService = {
send: async (message): Promise<NotificationResult> => {
sent.push(message);
return { success: true, messageId: `msg-${sent.length}` };
},
};
const result = await service.send({
channel: 'email',
to: 'user@example.com',
subject: 'Welcome',
body: 'Hello, welcome to the platform!',
});
expect(result.success).toBe(true);
expect(result.messageId).toBe('msg-1');
expect(sent).toHaveLength(1);
expect(sent[0].channel).toBe('email');
});
it('should handle notification failures', async () => {
const service: INotificationService = {
send: async (_message): Promise<NotificationResult> => ({
success: false,
error: 'Invalid recipient',
}),
};
const result = await service.send({
channel: 'sms',
to: '',
body: 'Test',
});
expect(result.success).toBe(false);
expect(result.error).toBe('Invalid recipient');
});
it('should send to multiple recipients', async () => {
const delivered: string[] = [];
const service: INotificationService = {
send: async (message) => {
const recipients = Array.isArray(message.to) ? message.to : [message.to];
delivered.push(...recipients);
return { success: true };
},
};
await service.send({
channel: 'push',
to: ['user-1', 'user-2', 'user-3'],
body: 'New update available',
});
expect(delivered).toEqual(['user-1', 'user-2', 'user-3']);
});
it('should support batch sending', async () => {
const service: INotificationService = {
send: async () => ({ success: true }),
sendBatch: async (messages) =>
messages.map((_, i) => ({
success: i !== 1, // Second message fails
messageId: `msg-${i}`,
error: i === 1 ? 'Rate limited' : undefined,
})),
};
const results = await service.sendBatch!([
{ channel: 'email', to: 'a@test.com', body: 'Hello A' },
{ channel: 'email', to: 'b@test.com', body: 'Hello B' },
{ channel: 'email', to: 'c@test.com', body: 'Hello C' },
]);
expect(results).toHaveLength(3);
expect(results[0].success).toBe(true);
expect(results[1].success).toBe(false);
expect(results[1].error).toBe('Rate limited');
expect(results[2].success).toBe(true);
});
it('should list available channels', () => {
const service: INotificationService = {
send: async () => ({ success: true }),
getChannels: () => ['email', 'sms', 'in-app'],
};
const channels = service.getChannels!();
expect(channels).toContain('email');
expect(channels).toContain('sms');
expect(channels).toContain('in-app');
});
it('should support template-based notifications', async () => {
const sent: NotificationMessage[] = [];
const service: INotificationService = {
send: async (message) => {
sent.push(message);
return { success: true };
},
};
await service.send({
channel: 'email',
to: 'user@example.com',
body: '',
templateId: 'welcome-email',
templateData: { userName: 'Alice', activationUrl: 'https://example.com/activate' },
});
expect(sent[0].templateId).toBe('welcome-email');
expect(sent[0].templateData?.userName).toBe('Alice');
});
});