diff --git a/src/channels/line/adapter.test.ts b/src/channels/line/adapter.test.ts index db649cc..6dd908d 100644 --- a/src/channels/line/adapter.test.ts +++ b/src/channels/line/adapter.test.ts @@ -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', diff --git a/src/channels/line/adapter.ts b/src/channels/line/adapter.ts index b3ae055..844e8ac 100644 --- a/src/channels/line/adapter.ts +++ b/src/channels/line/adapter.ts @@ -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`); + } } } diff --git a/src/channels/zalo/adapter.test.ts b/src/channels/zalo/adapter.test.ts index 201b1b3..af62bec 100644 --- a/src/channels/zalo/adapter.test.ts +++ b/src/channels/zalo/adapter.test.ts @@ -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 }> = []; diff --git a/src/channels/zalo/adapter.ts b/src/channels/zalo/adapter.ts index 01bc693..bceb384 100644 --- a/src/channels/zalo/adapter.ts +++ b/src/channels/zalo/adapter.ts @@ -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`); + } } }