forked from elizaOS/eliza
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebhook.handler.test.ts
134 lines (115 loc) · 4.2 KB
/
webhook.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
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 { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { WebhookHandler } from '../../src/handlers/webhook.handler';
import { WhatsAppClient } from '../../src/client';
import { WhatsAppWebhookEvent } from '../../src/types';
describe('WebhookHandler', () => {
let webhookHandler: WebhookHandler;
let mockClient: WhatsAppClient;
let consoleSpy: any;
beforeEach(() => {
mockClient = {} as WhatsAppClient;
webhookHandler = new WebhookHandler(mockClient);
consoleSpy = vi.spyOn(console, 'log');
});
afterEach(() => {
consoleSpy.mockRestore();
});
it('should handle message events correctly', async () => {
const mockMessage = {
from: '1234567890',
id: 'msg_id',
timestamp: '1234567890',
text: {
body: 'Test message'
}
};
const mockEvent: WhatsAppWebhookEvent = {
object: 'whatsapp_business_account',
entry: [{
id: 'BUSINESS_ID',
changes: [{
value: {
messaging_product: 'whatsapp',
metadata: {
display_phone_number: '1234567890',
phone_number_id: 'PHONE_ID'
},
messages: [mockMessage]
}
}]
}]
};
await webhookHandler.handle(mockEvent);
expect(consoleSpy).toHaveBeenCalledWith('Received message:', mockMessage);
});
it('should handle status updates correctly', async () => {
const mockStatus = {
id: 'status_id',
status: 'delivered',
timestamp: '1234567890',
recipient_id: '1234567890'
};
const mockEvent: WhatsAppWebhookEvent = {
object: 'whatsapp_business_account',
entry: [{
id: 'BUSINESS_ID',
changes: [{
value: {
messaging_product: 'whatsapp',
metadata: {
display_phone_number: '1234567890',
phone_number_id: 'PHONE_ID'
},
statuses: [mockStatus]
},
field: ''
}]
}]
};
await webhookHandler.handle(mockEvent);
expect(consoleSpy).toHaveBeenCalledWith('Received status update:', mockStatus);
});
it('should handle events with both messages and statuses', async () => {
const mockMessage = {
from: '1234567890',
id: 'msg_id',
timestamp: '1234567890',
text: {
body: 'Test message'
}
};
const mockStatus = {
id: 'status_id',
status: 'delivered',
timestamp: '1234567890',
recipient_id: '1234567890'
};
const mockEvent: WhatsAppWebhookEvent = {
object: 'whatsapp_business_account',
entry: [{
id: 'BUSINESS_ID',
changes: [{
value: {
messaging_product: 'whatsapp',
metadata: {
display_phone_number: '1234567890',
phone_number_id: 'PHONE_ID'
},
messages: [mockMessage],
statuses: [mockStatus]
}
}]
}]
};
await webhookHandler.handle(mockEvent);
expect(consoleSpy).toHaveBeenCalledWith('Received message:', mockMessage);
expect(consoleSpy).toHaveBeenCalledWith('Received status update:', mockStatus);
});
it('should handle errors correctly', async () => {
const mockEvent = {} as WhatsAppWebhookEvent;
// The handler should not throw an error for an empty event
await expect(webhookHandler.handle(mockEvent)).resolves.not.toThrow();
// Verify that no messages or statuses were processed
expect(consoleSpy).not.toHaveBeenCalled();
});
});