|
| 1 | +import { describe, it, expect, vi } from 'vitest'; |
| 2 | +import TelegramBot from '../src/telegram_bot'; |
| 3 | +import TelegramApi from '../src/telegram_api'; |
| 4 | +import Webhook from '../src/webhook'; |
| 5 | +describe('telegram bot', () => { |
| 6 | + // Test for inline query handling |
| 7 | + it('inline response', async () => { |
| 8 | + const bot = new TelegramBot('123456789').on(':message', async () => { |
| 9 | + return Promise.resolve(new Response('ok')); |
| 10 | + }); |
| 11 | + const request = new Request('http://example.com/123456789', { |
| 12 | + method: 'POST', |
| 13 | + body: JSON.stringify({ inline_query: { query: 'hello' } }), |
| 14 | + }); |
| 15 | + expect(await (await bot.handle(request)).text()).toBe('ok'); |
| 16 | + expect(bot.currentContext.update_type).toBe('inline'); |
| 17 | + }); |
| 18 | + // Test for message handling |
| 19 | + it('message response', async () => { |
| 20 | + const bot = new TelegramBot('123456789').on(':message', async () => { |
| 21 | + return Promise.resolve(new Response('ok')); |
| 22 | + }); |
| 23 | + const request = new Request('http://example.com/123456789', { |
| 24 | + method: 'POST', |
| 25 | + body: JSON.stringify({ message: { text: 'hello' } }), |
| 26 | + }); |
| 27 | + expect(await (await bot.handle(request)).text()).toBe('ok'); |
| 28 | + expect(bot.currentContext.update_type).toBe('message'); |
| 29 | + }); |
| 30 | + // Test for guest message handling |
| 31 | + it('guest message response', async () => { |
| 32 | + const bot = new TelegramBot('123456789').on(':guest_message', async () => { |
| 33 | + return Promise.resolve(new Response('ok')); |
| 34 | + }); |
| 35 | + const request = new Request('http://example.com/123456789', { |
| 36 | + method: 'POST', |
| 37 | + body: JSON.stringify({ guest_message: { text: 'hello', guest_query_id: 'guest123', chat: { id: 123, type: 'private' } } }), |
| 38 | + }); |
| 39 | + expect(await (await bot.handle(request)).text()).toBe('ok'); |
| 40 | + expect(bot.currentContext.update_type).toBe('guest_message'); |
| 41 | + }); |
| 42 | + it('throws error on non-200 telegram api response', async () => { |
| 43 | + const api = new TelegramApi(); |
| 44 | + // Mock global fetch |
| 45 | + const originalFetch = globalThis.fetch; |
| 46 | + globalThis.fetch = vi.fn().mockResolvedValue(new Response('Error', { status: 400, statusText: 'Bad Request' })); |
| 47 | + try { |
| 48 | + await api.sendMessage('https://api.telegram.org/bot123456789', { chat_id: 123, text: 'hello', parse_mode: 'HTML' }); |
| 49 | + expect.fail('Should have thrown an error'); |
| 50 | + } |
| 51 | + catch (e) { |
| 52 | + expect(e.message).toContain('Telegram API error: 400 Bad Request'); |
| 53 | + } |
| 54 | + finally { |
| 55 | + globalThis.fetch = originalFetch; |
| 56 | + } |
| 57 | + }); |
| 58 | + it('throws error on non-200 telegram file api response', async () => { |
| 59 | + const api = new TelegramApi(); |
| 60 | + const originalFetch = globalThis.fetch; |
| 61 | + // First fetch for getFile (returns file path) |
| 62 | + // Second fetch for the actual file |
| 63 | + globalThis.fetch = vi |
| 64 | + .fn() |
| 65 | + .mockResolvedValueOnce(new Response(JSON.stringify({ ok: true, result: { file_path: 'foo/bar.jpg' } }), { status: 200 })) |
| 66 | + .mockResolvedValueOnce(new Response('Error', { status: 404, statusText: 'Not Found' })); |
| 67 | + try { |
| 68 | + await api.getFile('https://api.telegram.org/bot123456789', { file_id: '123' }, '123456789'); |
| 69 | + expect.fail('Should have thrown an error'); |
| 70 | + } |
| 71 | + catch (e) { |
| 72 | + expect(e.message).toContain('Telegram File API error: 404 Not Found'); |
| 73 | + } |
| 74 | + finally { |
| 75 | + globalThis.fetch = originalFetch; |
| 76 | + } |
| 77 | + }); |
| 78 | + it('throws error on non-200 webhook set response', async () => { |
| 79 | + const webhook = new Webhook('123456789', new Request('https://example.com/123456789')); |
| 80 | + const originalFetch = globalThis.fetch; |
| 81 | + globalThis.fetch = vi.fn().mockResolvedValue(new Response('Error', { status: 500, statusText: 'Internal Server Error' })); |
| 82 | + try { |
| 83 | + await webhook.set(); |
| 84 | + expect.fail('Should have thrown an error'); |
| 85 | + } |
| 86 | + catch (e) { |
| 87 | + expect(e.message).toContain('Telegram API error (setWebhook): 500 Internal Server Error'); |
| 88 | + } |
| 89 | + finally { |
| 90 | + globalThis.fetch = originalFetch; |
| 91 | + } |
| 92 | + }); |
| 93 | + it('throws error on non-200 webhook delete response', async () => { |
| 94 | + const webhook = new Webhook('123456789', new Request('https://example.com/123456789')); |
| 95 | + const originalFetch = globalThis.fetch; |
| 96 | + globalThis.fetch = vi.fn().mockResolvedValue(new Response('Error', { status: 403, statusText: 'Forbidden' })); |
| 97 | + try { |
| 98 | + await webhook.delete(); |
| 99 | + expect.fail('Should have thrown an error'); |
| 100 | + } |
| 101 | + catch (e) { |
| 102 | + expect(e.message).toContain('Telegram API error (deleteWebhook): 403 Forbidden'); |
| 103 | + } |
| 104 | + finally { |
| 105 | + globalThis.fetch = originalFetch; |
| 106 | + } |
| 107 | + }); |
| 108 | + // Test for business message handling |
| 109 | + it('business message from owner should be skipped', async () => { |
| 110 | + const handler = vi.fn().mockResolvedValue(new Response('handler_called')); |
| 111 | + const bot = new TelegramBot('123456789').on(':message', handler); |
| 112 | + const ownerId = 999; |
| 113 | + const connectionId = 'conn123'; |
| 114 | + const originalFetch = globalThis.fetch; |
| 115 | + globalThis.fetch = vi.fn().mockResolvedValue(new Response(JSON.stringify({ |
| 116 | + ok: true, |
| 117 | + result: { |
| 118 | + user: { id: ownerId }, |
| 119 | + can_reply: true, |
| 120 | + }, |
| 121 | + }), { status: 200 })); |
| 122 | + const request = new Request('http://example.com/123456789', { |
| 123 | + method: 'POST', |
| 124 | + body: JSON.stringify({ |
| 125 | + business_message: { |
| 126 | + business_connection_id: connectionId, |
| 127 | + from: { id: ownerId }, |
| 128 | + chat: { id: 123, type: 'private' }, |
| 129 | + text: 'Hello from owner', |
| 130 | + message_id: 1, |
| 131 | + }, |
| 132 | + }), |
| 133 | + }); |
| 134 | + const response = await bot.handle(request); |
| 135 | + expect(await response.text()).toBe('handler_called'); |
| 136 | + expect(handler).toHaveBeenCalled(); |
| 137 | + globalThis.fetch = originalFetch; |
| 138 | + }); |
| 139 | + it('business message from user should be processed', async () => { |
| 140 | + const handler = vi.fn().mockResolvedValue(new Response('handler_called')); |
| 141 | + const bot = new TelegramBot('123456789').on(':message', handler); |
| 142 | + const ownerId = 999; |
| 143 | + const userId = 123; |
| 144 | + const connectionId = 'conn456'; |
| 145 | + const originalFetch = globalThis.fetch; |
| 146 | + globalThis.fetch = vi.fn().mockResolvedValue(new Response(JSON.stringify({ |
| 147 | + ok: true, |
| 148 | + result: { |
| 149 | + user: { id: ownerId }, |
| 150 | + can_reply: true, |
| 151 | + }, |
| 152 | + }), { status: 200 })); |
| 153 | + const request = new Request('http://example.com/123456789', { |
| 154 | + method: 'POST', |
| 155 | + body: JSON.stringify({ |
| 156 | + business_message: { |
| 157 | + business_connection_id: connectionId, |
| 158 | + from: { id: userId }, |
| 159 | + chat: { id: userId, type: 'private' }, |
| 160 | + text: 'Hello from user', |
| 161 | + message_id: 2, |
| 162 | + }, |
| 163 | + }), |
| 164 | + }); |
| 165 | + const response = await bot.handle(request); |
| 166 | + expect(await response.text()).toBe('handler_called'); |
| 167 | + expect(handler).toHaveBeenCalled(); |
| 168 | + globalThis.fetch = originalFetch; |
| 169 | + }); |
| 170 | +}); |
0 commit comments