132 lines
4.0 KiB
TypeScript
132 lines
4.0 KiB
TypeScript
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: any;
|
|
|
|
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("<b>Teste</b> 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 as any,
|
|
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 as any,
|
|
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 as any,
|
|
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 as any,
|
|
delayMs: 0,
|
|
});
|
|
|
|
await n.sendMessage("<b>Negrito</b> e <i>itálico</i>");
|
|
expect(capturedBody.parse_mode).toBe("HTML");
|
|
expect(capturedBody.disable_web_page_preview).toBe(true);
|
|
});
|
|
}); |