test (telegram): assert webhook callback timeout-safe options

This commit is contained in:
Vignesh Natarajan
2026-02-14 18:57:05 -08:00
parent f032ade9c8
commit 69a1ab2319

View File

@@ -9,6 +9,7 @@ const handlerSpy = vi.fn(
);
const setWebhookSpy = vi.fn();
const stopSpy = vi.fn();
const webhookCallbackSpy = vi.fn(() => handlerSpy);
const createTelegramBotSpy = vi.fn(() => ({
api: { setWebhook: setWebhookSpy },
@@ -17,7 +18,10 @@ const createTelegramBotSpy = vi.fn(() => ({
vi.mock("grammy", async (importOriginal) => {
const actual = await importOriginal<typeof import("grammy")>();
return { ...actual, webhookCallback: () => handlerSpy };
return {
...actual,
webhookCallback: (...args: unknown[]) => webhookCallbackSpy(...args),
};
});
vi.mock("./bot.js", () => ({
@@ -27,6 +31,7 @@ vi.mock("./bot.js", () => ({
describe("startTelegramWebhook", () => {
it("starts server, registers webhook, and serves health", async () => {
createTelegramBotSpy.mockClear();
webhookCallbackSpy.mockClear();
const abort = new AbortController();
const cfg = { bindings: [] };
const { server } = await startTelegramWebhook({
@@ -52,6 +57,19 @@ describe("startTelegramWebhook", () => {
const health = await fetch(`${url}/healthz`);
expect(health.status).toBe(200);
expect(setWebhookSpy).toHaveBeenCalled();
expect(webhookCallbackSpy).toHaveBeenCalledWith(
expect.objectContaining({
api: expect.objectContaining({
setWebhook: expect.any(Function),
}),
}),
"http",
{
secretToken: "secret",
onTimeout: "return",
timeoutMilliseconds: 10_000,
},
);
abort.abort();
});