import { test, expect, describe, beforeEach, afterEach } from "bun:test"; import { TelegramNotifier } from "../../src/infrastructure/telegram/telegram-notifier.ts"; describe("TelegramNotifier", () => { let notifier: TelegramNotifier; let fetchMock: typeof fetch; beforeEach(() => { fetchMock = async (url: string, options: any) => { const body = JSON.parse(options.body); return new Response(JSON.stringify({ ok: true, result: { message_id: 123 } }), { status: 200, headers: { "content-type": "application/json" }, }); }; notifier = new TelegramNotifier({ botToken: "123456:TEST_TOKEN", chatId: "-1001234567890", fetchFn: fetchMock, delayMs: 0, }); }); test("sendMessage: sucesso", async () => { const result = await notifier.sendMessage("Teste mensagem"); expect(result).toBe(true); }); test("sendMessage: sem config retorna false", async () => { const emptyNotifier = new TelegramNotifier({ fetchFn: fetchMock }); const result = await emptyNotifier.sendMessage("teste"); expect(result).toBe(false); }); test("sendMessage: override chatId", async () => { let capturedChatId: string | null = null; const mockFetch = async (url: string, options: any) => { const body = JSON.parse(options.body); capturedChatId = body.chat_id; return new Response(JSON.stringify({ ok: true, result: { message_id: 123 } }), { status: 200, headers: { "content-type": "application/json" }, }); }; const n = new TelegramNotifier({ botToken: "123456:TEST", chatId: "-1001111111111", fetchFn: mockFetch, delayMs: 0, }); await n.sendMessage("teste", "-1009999999999"); expect(capturedChatId).toBe("-1009999999999"); }); test("sendMessage: rate limit 429 com retry", async () => { let attempts = 0; const mockFetch = async (url: string, options: any) => { attempts++; if (attempts === 1) { return new Response(JSON.stringify({ ok: false, error_code: 429, parameters: { retry_after: 1 } }), { status: 429, headers: { "content-type": "application/json" }, }); } return new Response(JSON.stringify({ ok: true, result: { message_id: 123 } }), { status: 200, headers: { "content-type": "application/json" }, }); }; const n = new TelegramNotifier({ botToken: "123456:TEST", chatId: "-1001234567890", fetchFn: mockFetch, delayMs: 0, maxRetries: 3, }); const result = await n.sendMessage("teste rate limit"); expect(result).toBe(true); expect(attempts).toBe(2); }); test("sendMessage: falha de rede faz retry", async () => { let attempts = 0; const mockFetch = async () => { attempts++; if (attempts < 3) throw new Error("Network error"); return new Response(JSON.stringify({ ok: true, result: { message_id: 123 } }), { status: 200, headers: { "content-type": "application/json" }, }); }; const n = new TelegramNotifier({ botToken: "123456:TEST", chatId: "-1001234567890", fetchFn: mockFetch, delayMs: 0, maxRetries: 3, }); const result = await n.sendMessage("teste rede"); expect(result).toBe(true); expect(attempts).toBe(3); }); test("sendMessage: HTML parsing mode", async () => { let capturedBody: any = null; const mockFetch = async (url: string, options: any) => { capturedBody = JSON.parse(options.body); return new Response(JSON.stringify({ ok: true, result: { message_id: 123 } }), { status: 200, headers: { "content-type": "application/json" }, }); }; const n = new TelegramNotifier({ botToken: "123456:TEST", chatId: "-1001234567890", fetchFn: mockFetch, delayMs: 0, }); await n.sendMessage("Negrito e itálico"); expect(capturedBody.parse_mode).toBe("HTML"); expect(capturedBody.disable_web_page_preview).toBe(true); }); });