fix(channels): forward line and zalo URL attachments

This commit is contained in:
William Valentin
2026-02-17 10:19:00 -08:00
parent 27b3acf5e6
commit 288ef5ac3c
4 changed files with 130 additions and 8 deletions
+50
View File
@@ -74,6 +74,56 @@ describe('LineAdapter', () => {
expect(mockFetch.mock.calls[0]?.[0]).toBe('https://api.line.me/v2/bot/message/push');
});
it('send emits URL attachments and warns for binary attachments', async () => {
const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
const adapter = new LineAdapter({
channelAccessToken: 'token',
channelSecret: 'secret',
});
await adapter.connect();
mockFetch.mockResolvedValue({
ok: true,
status: 200,
text: async () => '',
} as Response);
await adapter.send('U123', {
text: 'hello line',
attachments: [
{ mimeType: 'text/plain', url: 'https://example.com/file.txt', filename: 'file.txt' },
{ mimeType: 'image/png', data: 'aGVsbG8=' },
],
});
expect(mockFetch).toHaveBeenCalledTimes(2);
const secondBody = JSON.parse(String(mockFetch.mock.calls[1]?.[1]?.body ?? '{}'));
expect(secondBody.messages?.[0]?.text).toBe('file.txt: https://example.com/file.txt');
expect(warnSpy).toHaveBeenCalledWith('LINE: skipping attachment data (image/png) — upload not implemented');
warnSpy.mockRestore();
});
it('send delivers URL attachment even when text is empty', async () => {
const adapter = new LineAdapter({
channelAccessToken: 'token',
channelSecret: 'secret',
});
await adapter.connect();
mockFetch.mockResolvedValue({
ok: true,
status: 200,
text: async () => '',
} as Response);
await adapter.send('U123', {
text: ' ',
attachments: [{ mimeType: 'text/plain', url: 'https://example.com/file.txt', filename: 'file.txt' }],
});
expect(mockFetch).toHaveBeenCalledTimes(1);
const body = JSON.parse(String(mockFetch.mock.calls[0]?.[1]?.body ?? '{}'));
expect(body.messages?.[0]?.text).toBe('file.txt: https://example.com/file.txt');
});
it('handleRequest validates signature and dispatches text event', async () => {
const adapter = new LineAdapter({
channelAccessToken: 'token',
+18 -4
View File
@@ -70,13 +70,27 @@ export class LineAdapter implements ChannelAdapter {
}
const text = message.text.trim();
if (!text) {
const attachments = message.attachments ?? [];
if (!text && attachments.length === 0) {
return;
}
const chunks = text.length > MAX_MESSAGE_LENGTH ? splitMessage(text, MAX_MESSAGE_LENGTH) : [text];
for (const chunk of chunks) {
await this.sendPush(peerId, chunk);
if (text) {
const chunks = text.length > MAX_MESSAGE_LENGTH ? splitMessage(text, MAX_MESSAGE_LENGTH) : [text];
for (const chunk of chunks) {
await this.sendPush(peerId, chunk);
}
}
for (const attachment of attachments) {
if (attachment.url) {
const line = attachment.filename ? `${attachment.filename}: ${attachment.url}` : attachment.url;
await this.sendPush(peerId, line);
continue;
}
if (attachment.data) {
console.warn(`LINE: skipping attachment data (${attachment.mimeType}) — upload not implemented`);
}
}
}
+44
View File
@@ -63,6 +63,50 @@ describe('ZaloAdapter', () => {
expect(String(mockFetch.mock.calls[0]?.[0])).toContain('/v3.0/oa/message/cs');
});
it('send emits URL attachments and warns for binary attachments', async () => {
const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
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',
attachments: [
{ mimeType: 'text/plain', url: 'https://example.com/file.txt', filename: 'file.txt' },
{ mimeType: 'application/pdf', data: 'aGVsbG8=' },
],
});
expect(mockFetch).toHaveBeenCalledTimes(2);
const secondBody = JSON.parse(String(mockFetch.mock.calls[1]?.[1]?.body ?? '{}'));
expect(secondBody.message?.text).toBe('file.txt: https://example.com/file.txt');
expect(warnSpy).toHaveBeenCalledWith('Zalo: skipping attachment data (application/pdf) — upload not implemented');
warnSpy.mockRestore();
});
it('send delivers URL attachment even when text is empty', 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: ' ',
attachments: [{ mimeType: 'text/plain', url: 'https://example.com/file.txt', filename: 'file.txt' }],
});
expect(mockFetch).toHaveBeenCalledTimes(1);
const body = JSON.parse(String(mockFetch.mock.calls[0]?.[1]?.body ?? '{}'));
expect(body.message?.text).toBe('file.txt: https://example.com/file.txt');
});
it('handleEvent emits inbound message', async () => {
const adapter = new ZaloAdapter({ oaAccessToken: 'token', requireMention: false });
const inbound: Array<{ channel: string; senderId: string; text: string }> = [];
+18 -4
View File
@@ -60,13 +60,27 @@ export class ZaloAdapter implements ChannelAdapter {
}
const text = message.text.trim();
if (!text) {
const attachments = message.attachments ?? [];
if (!text && attachments.length === 0) {
return;
}
const chunks = text.length > MAX_MESSAGE_LENGTH ? splitMessage(text, MAX_MESSAGE_LENGTH) : [text];
for (const chunk of chunks) {
await this.sendText(peerId, chunk);
if (text) {
const chunks = text.length > MAX_MESSAGE_LENGTH ? splitMessage(text, MAX_MESSAGE_LENGTH) : [text];
for (const chunk of chunks) {
await this.sendText(peerId, chunk);
}
}
for (const attachment of attachments) {
if (attachment.url) {
const line = attachment.filename ? `${attachment.filename}: ${attachment.url}` : attachment.url;
await this.sendText(peerId, line);
continue;
}
if (attachment.data) {
console.warn(`Zalo: skipping attachment data (${attachment.mimeType}) — upload not implemented`);
}
}
}