/** * Tests for Telegram API wrapper. */ import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"; import { TelegramAPI } from "../src/telegram-api.js"; describe("telegram-api", () => { const originalFetch = global.fetch; beforeEach(() => { global.fetch = vi.fn(); }); afterEach(() => { global.fetch = originalFetch; }); it("should call getMe", async () => { (global.fetch as ReturnType).mockResolvedValueOnce({ ok: true, json: async () => ({ ok: true, result: { id: 123, first_name: "Bot" } }), }); const api = new TelegramAPI("test-token"); const result = await api.getMe(); expect(result).toEqual({ id: 123, first_name: "Bot" }); expect(global.fetch).toHaveBeenCalledWith( "https://api.telegram.org/bottest-token/getMe", expect.objectContaining({ method: "POST" }) ); }); it("should call sendMessage", async () => { (global.fetch as ReturnType).mockResolvedValueOnce({ ok: true, json: async () => ({ ok: true, result: { message_id: 456 } }), }); const api = new TelegramAPI("test-token"); const result = await api.sendMessage("123", "Hello"); expect(result).toEqual({ message_id: 456 }); expect(global.fetch).toHaveBeenCalledWith( "https://api.telegram.org/bottest-token/sendMessage", expect.objectContaining({ method: "POST", body: JSON.stringify({ chat_id: "123", text: "Hello" }), }) ); }); it("should call editMessageText", async () => { (global.fetch as ReturnType).mockResolvedValueOnce({ ok: true, json: async () => ({ ok: true, result: true }), }); const api = new TelegramAPI("test-token"); await api.editMessageText("123", 456, "Updated"); expect(global.fetch).toHaveBeenCalledWith( "https://api.telegram.org/bottest-token/editMessageText", expect.objectContaining({ method: "POST", body: JSON.stringify({ chat_id: "123", message_id: 456, text: "Updated" }), }) ); }); it("should call deleteMessage", async () => { (global.fetch as ReturnType).mockResolvedValueOnce({ ok: true, json: async () => ({ ok: true, result: true }), }); const api = new TelegramAPI("test-token"); await api.deleteMessage("123", 456); expect(global.fetch).toHaveBeenCalledWith( "https://api.telegram.org/bottest-token/deleteMessage", expect.objectContaining({ method: "POST", body: JSON.stringify({ chat_id: "123", message_id: 456 }), }) ); }); it("should call setWebhook", async () => { (global.fetch as ReturnType).mockResolvedValueOnce({ ok: true, json: async () => ({ ok: true, result: true }), }); const api = new TelegramAPI("test-token"); await api.setWebhook("https://example.com/webhook", "secret"); expect(global.fetch).toHaveBeenCalledWith( "https://api.telegram.org/bottest-token/setWebhook", expect.objectContaining({ method: "POST", body: JSON.stringify({ url: "https://example.com/webhook", secret_token: "secret", allowed_updates: ["message"], }), }) ); }); it("should handle API errors", async () => { (global.fetch as ReturnType).mockResolvedValueOnce({ ok: false, status: 401, text: async () => "Unauthorized", }); const api = new TelegramAPI("bad-token"); await expect(api.getMe()).rejects.toThrow("Telegram API getMe failed (401): Unauthorized"); }); });