forked from elizaOS/eliza
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage.handler.test.ts
67 lines (53 loc) · 2.1 KB
/
message.handler.test.ts
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
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { MessageHandler } from '../../src/handlers/message.handler';
import { WhatsAppClient } from '../../src/client';
import { WhatsAppMessage } from '../../src/types';
describe('MessageHandler', () => {
let messageHandler: MessageHandler;
let mockClient: WhatsAppClient;
beforeEach(() => {
mockClient = {
sendMessage: vi.fn(),
} as any as WhatsAppClient;
messageHandler = new MessageHandler(mockClient);
});
it('should successfully send a message', async () => {
const mockMessage: WhatsAppMessage = {
type: 'text',
to: '1234567890',
content: 'Test message'
};
const mockResponse = {
messaging_product: 'whatsapp',
contacts: [{ input: '1234567890', wa_id: 'WHATSAPP_ID' }],
messages: [{ id: 'MESSAGE_ID' }]
};
(mockClient.sendMessage as any).mockResolvedValue({ data: mockResponse });
const result = await messageHandler.send(mockMessage);
expect(mockClient.sendMessage).toHaveBeenCalledWith(mockMessage);
expect(result).toEqual(mockResponse);
});
it('should handle client errors with error message', async () => {
const mockMessage: WhatsAppMessage = {
type: 'text',
to: '1234567890',
content: 'Test message'
};
const errorMessage = 'API Error';
(mockClient.sendMessage as any).mockRejectedValue(new Error(errorMessage));
await expect(messageHandler.send(mockMessage))
.rejects
.toThrow(`Failed to send WhatsApp message: ${errorMessage}`);
});
it('should handle unknown errors', async () => {
const mockMessage: WhatsAppMessage = {
type: 'text',
to: '1234567890',
content: 'Test message'
};
(mockClient.sendMessage as any).mockRejectedValue('Unknown error');
await expect(messageHandler.send(mockMessage))
.rejects
.toThrow('Failed to send WhatsApp message');
});
});