|
1 | | -import { describe, expect, test, vi } from 'vitest'; |
2 | | -import { TextLkSmsProvider } from './textlk.provider'; |
| 1 | +import { |
| 2 | + ChannelTypeEnum, |
| 3 | + ISendMessageSuccessResponse, |
| 4 | + ISmsOptions, |
| 5 | + ISmsProvider, |
| 6 | +} from '@novu/stateless'; |
| 7 | +import axios from 'axios'; |
3 | 8 |
|
4 | | -const mockPost = vi.fn(); |
| 9 | +export class TextLkSmsProvider implements ISmsProvider { |
| 10 | + id = 'textlk'; |
| 11 | + channelType = ChannelTypeEnum.SMS as ChannelTypeEnum.SMS; |
5 | 12 |
|
6 | | -vi.mock('axios', () => ({ |
7 | | - default: { |
8 | | - post: (...args: any[]) => { |
9 | | - mockPost(...args); |
10 | | - return Promise.resolve({ |
11 | | - data: { |
12 | | - uid: 'mock-uid-123', |
13 | | - status: 'success' |
14 | | - } |
15 | | - }); |
| 13 | + constructor( |
| 14 | + private config: { |
| 15 | + apiKey: string; |
16 | 16 | } |
17 | | - } |
18 | | -})); |
| 17 | + ) {} |
19 | 18 |
|
20 | | -describe('TextLkSmsProvider', () => { |
21 | | - test('should trigger textlk API correctly', async () => { |
22 | | - const provider = new TextLkSmsProvider({ |
23 | | - apiKey: 'test-api-key', |
24 | | - }); |
| 19 | + async sendMessage( |
| 20 | + options: ISmsOptions |
| 21 | + ): Promise<ISendMessageSuccessResponse> { |
| 22 | + const BASE_URL = 'https://app.text.lk/api/v3/sms/send'; |
25 | 23 |
|
26 | | - const result = await provider.sendMessage({ |
27 | | - to: '+94771234567', |
28 | | - content: 'Hello World', |
29 | | - from: 'MyCompany' |
30 | | - }); |
| 24 | + // Fix: Handle comma-separated numbers and strip leading '+' |
| 25 | + const recipient = options.to |
| 26 | + .split(',') |
| 27 | + .map((value) => value.trim().replace(/^\+/, '')) |
| 28 | + .join(','); |
31 | 29 |
|
32 | | - expect(mockPost).toHaveBeenCalled(); |
33 | | - expect(mockPost).toHaveBeenCalledWith( |
34 | | - 'https://app.text.lk/api/v3/sms/send', |
35 | | - { |
36 | | - recipient: '+94771234567', |
37 | | - sender_id: 'MyCompany', |
38 | | - type: 'plain', |
39 | | - message: 'Hello World', |
40 | | - }, |
41 | | - { |
42 | | - headers: { |
43 | | - 'Authorization': 'Bearer test-api-key', |
44 | | - 'Content-Type': 'application/json', |
45 | | - 'Accept': 'application/json', |
46 | | - }, |
47 | | - } |
48 | | - ); |
| 30 | + const payload = { |
| 31 | + recipient, |
| 32 | + sender_id: options.from || 'Text.lk', |
| 33 | + type: 'plain', |
| 34 | + message: options.content, |
| 35 | + }; |
49 | 36 |
|
50 | | - expect(result).toEqual({ |
51 | | - id: 'mock-uid-123', |
52 | | - date: expect.any(String), |
| 37 | + const response = await axios.post(BASE_URL, payload, { |
| 38 | + headers: { |
| 39 | + 'Authorization': `Bearer ${this.config.apiKey}`, |
| 40 | + 'Content-Type': 'application/json', |
| 41 | + 'Accept': 'application/json', |
| 42 | + }, |
53 | 43 | }); |
54 | | - }); |
55 | | -}); |
| 44 | + |
| 45 | + return { |
| 46 | + id: response.data.uid || new Date().getTime().toString(), |
| 47 | + date: new Date().toISOString(), |
| 48 | + }; |
| 49 | + } |
| 50 | +} |
0 commit comments