mirror of
https://github.com/harivansh-afk/pi-telegram-webhook.git
synced 2026-04-15 09:01:16 +00:00
initial: webhook telegram adapter for pi with streaming replies
- webhook server with secret validation, rate limiting, body guards - streaming replies via sendMessage + editMessageText throttled loop - RPC session management for persistent conversations - 15/15 tests passing
This commit is contained in:
parent
809e9b1df5
commit
ce9abc2a8e
18 changed files with 6991 additions and 1 deletions
123
tests/telegram-api.test.ts
Normal file
123
tests/telegram-api.test.ts
Normal file
|
|
@ -0,0 +1,123 @@
|
|||
/**
|
||||
* 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<typeof vi.fn>).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<typeof vi.fn>).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<typeof vi.fn>).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<typeof vi.fn>).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<typeof vi.fn>).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<typeof vi.fn>).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");
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue