Files
openclaw/src/telegram/webhook.test.ts
T
Gustavo Madeira Santana ecb91bbb1a Add accountId and config support to Telegram webhook
The Telegram webhook and monitor now accept and pass through accountId and config parameters, enabling routing and configuration per Telegram account. Tests have been updated to verify correct bot instantiation and DM routing based on accountId bindings.
2026-01-13 03:34:32 +00:00

74 lines
2.1 KiB
TypeScript

import { describe, expect, it, vi } from "vitest";
import { startTelegramWebhook } from "./webhook.js";
const handlerSpy = vi.fn(
(
_req: unknown,
res: { writeHead: (status: number) => void; end: (body?: string) => void },
) => {
res.writeHead(200);
res.end("ok");
},
);
const setWebhookSpy = vi.fn();
const stopSpy = vi.fn();
const createTelegramBotSpy = vi.fn(() => ({
api: { setWebhook: setWebhookSpy },
stop: stopSpy,
}));
vi.mock("grammy", () => ({
webhookCallback: () => handlerSpy,
}));
vi.mock("./bot.js", () => ({
createTelegramBot: (...args: unknown[]) => createTelegramBotSpy(...args),
}));
describe("startTelegramWebhook", () => {
it("starts server, registers webhook, and serves health", async () => {
createTelegramBotSpy.mockClear();
const abort = new AbortController();
const { server } = await startTelegramWebhook({
token: "tok",
accountId: "opie",
config: { bindings: [] },
port: 0, // random free port
abortSignal: abort.signal,
});
expect(createTelegramBotSpy).toHaveBeenCalledWith(
expect.objectContaining({ accountId: "opie" }),
);
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();
createTelegramBotSpy.mockClear();
const abort = new AbortController();
const { server } = await startTelegramWebhook({
token: "tok",
accountId: "opie",
config: { bindings: [] },
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();
});
});