Telegram: enable grammY throttler and webhook tests
This commit is contained in:
33
src/telegram/bot.test.ts
Normal file
33
src/telegram/bot.test.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
|
||||
const useSpy = vi.fn();
|
||||
const onSpy = vi.fn();
|
||||
const stopSpy = vi.fn();
|
||||
const apiStub = { config: { use: useSpy } };
|
||||
|
||||
vi.mock("grammy", () => ({
|
||||
Bot: class {
|
||||
api = apiStub as any;
|
||||
on = onSpy;
|
||||
stop = stopSpy;
|
||||
constructor(public token: string) {}
|
||||
},
|
||||
InputFile: class {},
|
||||
webhookCallback: vi.fn(),
|
||||
}));
|
||||
|
||||
const throttlerSpy = vi.fn(() => "throttler");
|
||||
|
||||
vi.mock("@grammyjs/transformer-throttler", () => ({
|
||||
apiThrottler: () => throttlerSpy(),
|
||||
}));
|
||||
|
||||
import { createTelegramBot } from "./bot.js";
|
||||
|
||||
describe("createTelegramBot", () => {
|
||||
it("installs grammY throttler", () => {
|
||||
createTelegramBot({ token: "tok" });
|
||||
expect(throttlerSpy).toHaveBeenCalledTimes(1);
|
||||
expect(useSpy).toHaveBeenCalledWith("throttler");
|
||||
});
|
||||
});
|
||||
@@ -1,6 +1,7 @@
|
||||
import { Buffer } from "node:buffer";
|
||||
|
||||
import { Bot, InputFile, webhookCallback } from "grammy";
|
||||
import { apiThrottler } from "@grammyjs/transformer-throttler";
|
||||
import type { ApiClientOptions } from "grammy";
|
||||
|
||||
import { chunkText } from "../auto-reply/chunk.js";
|
||||
@@ -38,6 +39,7 @@ export function createTelegramBot(opts: TelegramBotOptions) {
|
||||
: undefined;
|
||||
|
||||
const bot = new Bot(opts.token, { client });
|
||||
bot.api.config.use(apiThrottler());
|
||||
|
||||
const cfg = loadConfig();
|
||||
const requireMention =
|
||||
|
||||
57
src/telegram/webhook.test.ts
Normal file
57
src/telegram/webhook.test.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
|
||||
import { startTelegramWebhook } from "./webhook.js";
|
||||
|
||||
const handlerSpy = vi.fn((req: any, res: any) => {
|
||||
res.writeHead(200);
|
||||
res.end("ok");
|
||||
});
|
||||
const setWebhookSpy = vi.fn();
|
||||
const stopSpy = vi.fn();
|
||||
|
||||
vi.mock("grammy", () => ({
|
||||
webhookCallback: () => handlerSpy,
|
||||
}));
|
||||
|
||||
vi.mock("./bot.js", () => ({
|
||||
createTelegramBot: () => ({
|
||||
api: { setWebhook: setWebhookSpy },
|
||||
stop: stopSpy,
|
||||
}),
|
||||
}));
|
||||
|
||||
describe("startTelegramWebhook", () => {
|
||||
it("starts server, registers webhook, and serves health", async () => {
|
||||
const abort = new AbortController();
|
||||
const { server } = await startTelegramWebhook({
|
||||
token: "tok",
|
||||
port: 0, // random free port
|
||||
abortSignal: abort.signal,
|
||||
});
|
||||
const address = server.address();
|
||||
if (!address || typeof address === "string") throw new Error("no address");
|
||||
const url = `http://127.0.0.1:${address.port}`;
|
||||
|
||||
const health = await fetch(`${url}/healthz`);
|
||||
expect(health.status).toBe(200);
|
||||
expect(setWebhookSpy).toHaveBeenCalled();
|
||||
|
||||
abort.abort();
|
||||
});
|
||||
|
||||
it("invokes webhook handler on matching path", async () => {
|
||||
handlerSpy.mockClear();
|
||||
const abort = new AbortController();
|
||||
const { server } = await startTelegramWebhook({
|
||||
token: "tok",
|
||||
port: 0,
|
||||
abortSignal: abort.signal,
|
||||
path: "/hook",
|
||||
});
|
||||
const addr = server.address();
|
||||
if (!addr || typeof addr === "string") throw new Error("no addr");
|
||||
await fetch(`http://127.0.0.1:${addr.port}/hook`, { method: "POST" });
|
||||
expect(handlerSpy).toHaveBeenCalled();
|
||||
abort.abort();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user