-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdevsms.test.ts
More file actions
134 lines (118 loc) · 4.18 KB
/
Copy pathdevsms.test.ts
File metadata and controls
134 lines (118 loc) · 4.18 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
import { DevSmsProvider } from '../providers/devsms';
import type { SmsSendRequest } from '../providers/types';
const request: SmsSendRequest = {
to: '+14155550123',
body: 'Your sign-in code is 123456. Do not share this code.',
senderId: 'TestSender',
metadata: {
jobId: 'job-1',
databaseId: 'db-1',
purpose: 'sign_in_otp'
}
};
const jsonResponse = (body: unknown, status = 201): Response =>
new Response(JSON.stringify(body), {
status,
headers: { 'content-type': 'application/json' }
});
describe('DevSmsProvider', () => {
it('sends the correct URL, method, headers, and request body', async () => {
const fetchImpl = jest.fn().mockResolvedValue(jsonResponse({
id: 'row_1',
provider_message_id: 'SM123',
status: 'queued'
}));
const provider = new DevSmsProvider({
baseUrl: 'http://devsms:4000',
requestTimeoutMs: 5000,
fetchImpl: fetchImpl as unknown as typeof fetch
});
await provider.send(request);
expect(fetchImpl).toHaveBeenCalledWith(
'http://devsms:4000/api/sms/send/twilio',
expect.objectContaining({
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({
From: 'TestSender',
To: '+14155550123',
Body: 'Your sign-in code is 123456. Do not share this code.'
})
})
);
});
it('maps provider responses to SmsSendResult', async () => {
const fetchImpl = jest.fn().mockResolvedValue(jsonResponse({
id: 'row_1',
provider_message_id: 'SM123',
status: 'sent'
}));
const provider = new DevSmsProvider({
baseUrl: 'http://devsms:4000/',
requestTimeoutMs: 5000,
fetchImpl: fetchImpl as unknown as typeof fetch
});
await expect(provider.send(request)).resolves.toEqual({
provider: 'devsms',
messageId: 'SM123',
status: 'sent'
});
});
it('throws for non-2xx responses without exposing the response body', async () => {
const fetchImpl = jest.fn().mockResolvedValue(jsonResponse({
error: 'OTP 123456 failed for +14155550123'
}, 400));
const provider = new DevSmsProvider({
baseUrl: 'http://devsms:4000',
requestTimeoutMs: 5000,
fetchImpl: fetchImpl as unknown as typeof fetch
});
const error = await provider.send(request).then(
() => undefined,
(cause: unknown) => cause as Error
);
expect(error).toBeInstanceOf(Error);
expect(error?.message).toBe('DevSmsProvider request failed with 400');
expect(error?.message).not.toContain('123456');
expect(error?.message).not.toContain('+14155550123');
});
it('throws on timeout', async () => {
jest.useFakeTimers();
const fetchImpl = jest.fn((_url: string, init: RequestInit) =>
new Promise((_resolve, reject) => {
init.signal?.addEventListener('abort', () => {
const error = new Error('aborted');
error.name = 'AbortError';
reject(error);
});
})
);
const provider = new DevSmsProvider({
baseUrl: 'http://devsms:4000',
requestTimeoutMs: 10,
fetchImpl: fetchImpl as unknown as typeof fetch
});
const promise = provider.send(request);
jest.advanceTimersByTime(10);
await expect(promise).rejects.toThrow('DevSmsProvider timed out after 10ms');
jest.useRealTimers();
});
it('throws for invalid JSON responses', async () => {
const fetchImpl = jest.fn().mockResolvedValue(new Response('not-json', { status: 201 }));
const provider = new DevSmsProvider({
baseUrl: 'http://devsms:4000',
requestTimeoutMs: 5000,
fetchImpl: fetchImpl as unknown as typeof fetch
});
await expect(provider.send(request)).rejects.toThrow('DevSmsProvider returned invalid JSON');
});
it('throws when the response is missing a message ID', async () => {
const fetchImpl = jest.fn().mockResolvedValue(jsonResponse({ status: 'queued' }));
const provider = new DevSmsProvider({
baseUrl: 'http://devsms:4000',
requestTimeoutMs: 5000,
fetchImpl: fetchImpl as unknown as typeof fetch
});
await expect(provider.send(request)).rejects.toThrow('DevSmsProvider response missing message ID');
});
});