|
| 1 | +import { DevSmsProvider } from '../providers/devsms'; |
| 2 | +import type { SmsSendRequest } from '../providers/types'; |
| 3 | + |
| 4 | +const request: SmsSendRequest = { |
| 5 | + to: '+14155550123', |
| 6 | + body: 'Your sign-in code is 123456. Do not share this code.', |
| 7 | + senderId: 'TestSender', |
| 8 | + metadata: { |
| 9 | + jobId: 'job-1', |
| 10 | + databaseId: 'db-1', |
| 11 | + purpose: 'sign_in_otp' |
| 12 | + } |
| 13 | +}; |
| 14 | + |
| 15 | +const jsonResponse = (body: unknown, status = 201): Response => |
| 16 | + new Response(JSON.stringify(body), { |
| 17 | + status, |
| 18 | + headers: { 'content-type': 'application/json' } |
| 19 | + }); |
| 20 | + |
| 21 | +describe('DevSmsProvider', () => { |
| 22 | + it('sends the correct URL, method, headers, and request body', async () => { |
| 23 | + const fetchImpl = jest.fn().mockResolvedValue(jsonResponse({ |
| 24 | + id: 'row_1', |
| 25 | + provider_message_id: 'SM123', |
| 26 | + status: 'queued' |
| 27 | + })); |
| 28 | + const provider = new DevSmsProvider({ |
| 29 | + baseUrl: 'http://devsms:4000', |
| 30 | + requestTimeoutMs: 5000, |
| 31 | + fetchImpl: fetchImpl as unknown as typeof fetch |
| 32 | + }); |
| 33 | + |
| 34 | + await provider.send(request); |
| 35 | + |
| 36 | + expect(fetchImpl).toHaveBeenCalledWith( |
| 37 | + 'http://devsms:4000/api/sms/send/twilio', |
| 38 | + expect.objectContaining({ |
| 39 | + method: 'POST', |
| 40 | + headers: { 'content-type': 'application/json' }, |
| 41 | + body: JSON.stringify({ |
| 42 | + From: 'TestSender', |
| 43 | + To: '+14155550123', |
| 44 | + Body: 'Your sign-in code is 123456. Do not share this code.' |
| 45 | + }) |
| 46 | + }) |
| 47 | + ); |
| 48 | + }); |
| 49 | + |
| 50 | + it('maps provider responses to SmsSendResult', async () => { |
| 51 | + const fetchImpl = jest.fn().mockResolvedValue(jsonResponse({ |
| 52 | + id: 'row_1', |
| 53 | + provider_message_id: 'SM123', |
| 54 | + status: 'sent' |
| 55 | + })); |
| 56 | + const provider = new DevSmsProvider({ |
| 57 | + baseUrl: 'http://devsms:4000/', |
| 58 | + requestTimeoutMs: 5000, |
| 59 | + fetchImpl: fetchImpl as unknown as typeof fetch |
| 60 | + }); |
| 61 | + |
| 62 | + await expect(provider.send(request)).resolves.toEqual({ |
| 63 | + provider: 'devsms', |
| 64 | + messageId: 'SM123', |
| 65 | + status: 'sent' |
| 66 | + }); |
| 67 | + }); |
| 68 | + |
| 69 | + it('throws for non-2xx responses without exposing the response body', async () => { |
| 70 | + const fetchImpl = jest.fn().mockResolvedValue(jsonResponse({ |
| 71 | + error: 'OTP 123456 failed for +14155550123' |
| 72 | + }, 400)); |
| 73 | + const provider = new DevSmsProvider({ |
| 74 | + baseUrl: 'http://devsms:4000', |
| 75 | + requestTimeoutMs: 5000, |
| 76 | + fetchImpl: fetchImpl as unknown as typeof fetch |
| 77 | + }); |
| 78 | + |
| 79 | + const error = await provider.send(request).then( |
| 80 | + () => undefined, |
| 81 | + (cause: unknown) => cause as Error |
| 82 | + ); |
| 83 | + |
| 84 | + expect(error).toBeInstanceOf(Error); |
| 85 | + expect(error?.message).toBe('DevSmsProvider request failed with 400'); |
| 86 | + expect(error?.message).not.toContain('123456'); |
| 87 | + expect(error?.message).not.toContain('+14155550123'); |
| 88 | + }); |
| 89 | + |
| 90 | + it('throws on timeout', async () => { |
| 91 | + jest.useFakeTimers(); |
| 92 | + const fetchImpl = jest.fn((_url: string, init: RequestInit) => |
| 93 | + new Promise((_resolve, reject) => { |
| 94 | + init.signal?.addEventListener('abort', () => { |
| 95 | + const error = new Error('aborted'); |
| 96 | + error.name = 'AbortError'; |
| 97 | + reject(error); |
| 98 | + }); |
| 99 | + }) |
| 100 | + ); |
| 101 | + const provider = new DevSmsProvider({ |
| 102 | + baseUrl: 'http://devsms:4000', |
| 103 | + requestTimeoutMs: 10, |
| 104 | + fetchImpl: fetchImpl as unknown as typeof fetch |
| 105 | + }); |
| 106 | + |
| 107 | + const promise = provider.send(request); |
| 108 | + jest.advanceTimersByTime(10); |
| 109 | + await expect(promise).rejects.toThrow('DevSmsProvider timed out after 10ms'); |
| 110 | + jest.useRealTimers(); |
| 111 | + }); |
| 112 | + |
| 113 | + it('throws for invalid JSON responses', async () => { |
| 114 | + const fetchImpl = jest.fn().mockResolvedValue(new Response('not-json', { status: 201 })); |
| 115 | + const provider = new DevSmsProvider({ |
| 116 | + baseUrl: 'http://devsms:4000', |
| 117 | + requestTimeoutMs: 5000, |
| 118 | + fetchImpl: fetchImpl as unknown as typeof fetch |
| 119 | + }); |
| 120 | + |
| 121 | + await expect(provider.send(request)).rejects.toThrow('DevSmsProvider returned invalid JSON'); |
| 122 | + }); |
| 123 | + |
| 124 | + it('throws when the response is missing a message ID', async () => { |
| 125 | + const fetchImpl = jest.fn().mockResolvedValue(jsonResponse({ status: 'queued' })); |
| 126 | + const provider = new DevSmsProvider({ |
| 127 | + baseUrl: 'http://devsms:4000', |
| 128 | + requestTimeoutMs: 5000, |
| 129 | + fetchImpl: fetchImpl as unknown as typeof fetch |
| 130 | + }); |
| 131 | + |
| 132 | + await expect(provider.send(request)).rejects.toThrow('DevSmsProvider response missing message ID'); |
| 133 | + }); |
| 134 | +}); |
0 commit comments