Add Zalo channel adapter with webhook and send path

This commit is contained in:
William Valentin
2026-02-16 13:11:51 -08:00
parent 891ccb696e
commit 8bed99c770
16 changed files with 491 additions and 6 deletions
+106
View File
@@ -0,0 +1,106 @@
import { describe, expect, it, vi, beforeEach } from 'vitest';
import type { IncomingMessage, ServerResponse } from 'http';
import { ZaloAdapter } from './adapter.js';
const mockFetch = vi.fn();
vi.stubGlobal('fetch', mockFetch);
function mockReq(body: string, token?: string): IncomingMessage {
const req = {
headers: token ? { 'x-zalo-token': token } : {},
on(event: string, handler: (...args: unknown[]) => void) {
if (event === 'data') {
handler(Buffer.from(body, 'utf8'));
}
if (event === 'end') {
handler();
}
return this;
},
off: () => req,
destroy: () => undefined,
} as unknown as IncomingMessage;
return req;
}
function mockRes() {
const state = { statusCode: 0, body: '' };
const res = {
writeHead: (code: number) => {
state.statusCode = code;
},
end: (chunk?: string) => {
state.body = chunk ?? '';
},
} as unknown as ServerResponse;
return { res, state };
}
describe('ZaloAdapter', () => {
beforeEach(() => {
vi.clearAllMocks();
mockFetch.mockReset();
});
it('has name zalo and starts disconnected', () => {
const adapter = new ZaloAdapter({ oaAccessToken: 'token' });
expect(adapter.name).toBe('zalo');
expect(adapter.status).toBe('disconnected');
});
it('send posts to zalo message API', async () => {
const adapter = new ZaloAdapter({ oaAccessToken: 'token' });
await adapter.connect();
mockFetch.mockResolvedValue({
ok: true,
status: 200,
text: async () => '',
} as Response);
await adapter.send('uid-1', { text: 'hello zalo' });
expect(mockFetch).toHaveBeenCalledTimes(1);
expect(String(mockFetch.mock.calls[0]?.[0])).toContain('/v3.0/oa/message/cs');
});
it('handleEvent emits inbound message', async () => {
const adapter = new ZaloAdapter({ oaAccessToken: 'token', requireMention: false });
const inbound: Array<{ channel: string; senderId: string; text: string }> = [];
adapter.onMessage((msg) => inbound.push({ channel: msg.channel, senderId: msg.senderId, text: msg.text }));
await adapter.handleEvent({
sender: { id: 'uid-1' },
recipient: { id: 'oa-1' },
message: { msg_id: 'm1', text: 'ping' },
timestamp: 123,
});
expect(inbound).toEqual([{ channel: 'zalo', senderId: 'uid-1', text: 'ping' }]);
});
it('enforces webhook token when configured', async () => {
const adapter = new ZaloAdapter({ oaAccessToken: 'token', webhookToken: 'secret' });
const body = JSON.stringify({
sender: { id: 'uid-1' },
message: { msg_id: 'm1', text: 'ping' },
});
const req = mockReq(body, 'wrong');
const { res, state } = mockRes();
await adapter.handleRequest(req, res);
expect(state.statusCode).toBe(401);
});
it('drops messages missing required mention', async () => {
const adapter = new ZaloAdapter({ oaAccessToken: 'token', requireMention: true, mentionName: 'flynn' });
const handler = vi.fn();
adapter.onMessage(handler);
await adapter.handleEvent({
sender: { id: 'uid-1' },
message: { msg_id: 'm1', text: 'hello there' },
});
expect(handler).not.toHaveBeenCalled();
});
});