test(telegram): relax media group test timeouts

This commit is contained in:
Peter Steinberger
2026-01-10 02:34:51 +01:00
parent 991f6dda38
commit 8f8caa8d89

View File

@@ -228,143 +228,162 @@ describe("telegram inbound media", () => {
}); });
describe("telegram media groups", () => { describe("telegram media groups", () => {
const MEDIA_GROUP_POLL_TIMEOUT_MS = 15_000;
const MEDIA_GROUP_TEST_TIMEOUT_MS = 20_000;
const waitForMediaGroupProcessing = async ( const waitForMediaGroupProcessing = async (
replySpy: ReturnType<typeof vi.fn>, replySpy: ReturnType<typeof vi.fn>,
expectedCalls: number, expectedCalls: number,
) => { ) => {
await expect await expect
.poll(() => replySpy.mock.calls.length, { timeout: 10_000 }) .poll(() => replySpy.mock.calls.length, {
timeout: MEDIA_GROUP_POLL_TIMEOUT_MS,
})
.toBe(expectedCalls); .toBe(expectedCalls);
}; };
it("buffers messages with same media_group_id and processes them together", async () => { it(
const { createTelegramBot } = await import("./bot.js"); "buffers messages with same media_group_id and processes them together",
const replyModule = await import("../auto-reply/reply.js"); async () => {
const replySpy = replyModule.__replySpy as unknown as ReturnType< const { createTelegramBot } = await import("./bot.js");
typeof vi.fn const replyModule = await import("../auto-reply/reply.js");
>; const replySpy = replyModule.__replySpy as unknown as ReturnType<
typeof vi.fn
>;
onSpy.mockReset(); onSpy.mockReset();
replySpy.mockReset(); replySpy.mockReset();
const runtimeError = vi.fn(); const runtimeError = vi.fn();
const fetchSpy = vi.spyOn(globalThis, "fetch" as never).mockResolvedValue({ const fetchSpy = vi
ok: true, .spyOn(globalThis, "fetch" as never)
status: 200, .mockResolvedValue({
statusText: "OK", ok: true,
headers: { get: () => "image/png" }, status: 200,
arrayBuffer: async () => new Uint8Array([0x89, 0x50, 0x4e, 0x47]).buffer, statusText: "OK",
} as Response); headers: { get: () => "image/png" },
arrayBuffer: async () =>
new Uint8Array([0x89, 0x50, 0x4e, 0x47]).buffer,
} as Response);
createTelegramBot({ createTelegramBot({
token: "tok", token: "tok",
runtime: { runtime: {
log: vi.fn(), log: vi.fn(),
error: runtimeError, error: runtimeError,
exit: () => { exit: () => {
throw new Error("exit"); throw new Error("exit");
},
}, },
}, });
}); const handler = onSpy.mock.calls.find(
const handler = onSpy.mock.calls.find( (call) => call[0] === "message",
(call) => call[0] === "message", )?.[1] as (ctx: Record<string, unknown>) => Promise<void>;
)?.[1] as (ctx: Record<string, unknown>) => Promise<void>; expect(handler).toBeDefined();
expect(handler).toBeDefined();
await handler({ await handler({
message: { message: {
chat: { id: 42, type: "private" }, chat: { id: 42, type: "private" },
message_id: 1, message_id: 1,
caption: "Here are my photos", caption: "Here are my photos",
date: 1736380800, date: 1736380800,
media_group_id: "album123", media_group_id: "album123",
photo: [{ file_id: "photo1" }], photo: [{ file_id: "photo1" }],
}, },
me: { username: "clawdbot_bot" }, me: { username: "clawdbot_bot" },
getFile: async () => ({ file_path: "photos/photo1.jpg" }), getFile: async () => ({ file_path: "photos/photo1.jpg" }),
}); });
await handler({ await handler({
message: { message: {
chat: { id: 42, type: "private" }, chat: { id: 42, type: "private" },
message_id: 2, message_id: 2,
date: 1736380801, date: 1736380801,
media_group_id: "album123", media_group_id: "album123",
photo: [{ file_id: "photo2" }], photo: [{ file_id: "photo2" }],
}, },
me: { username: "clawdbot_bot" }, me: { username: "clawdbot_bot" },
getFile: async () => ({ file_path: "photos/photo2.jpg" }), getFile: async () => ({ file_path: "photos/photo2.jpg" }),
}); });
expect(replySpy).not.toHaveBeenCalled(); expect(replySpy).not.toHaveBeenCalled();
await waitForMediaGroupProcessing(replySpy, 1); await waitForMediaGroupProcessing(replySpy, 1);
expect(runtimeError).not.toHaveBeenCalled(); expect(runtimeError).not.toHaveBeenCalled();
expect(replySpy).toHaveBeenCalledTimes(1); expect(replySpy).toHaveBeenCalledTimes(1);
const payload = replySpy.mock.calls[0][0]; const payload = replySpy.mock.calls[0][0];
expect(payload.Body).toContain("Here are my photos"); expect(payload.Body).toContain("Here are my photos");
expect(payload.MediaPaths).toHaveLength(2); expect(payload.MediaPaths).toHaveLength(2);
fetchSpy.mockRestore(); fetchSpy.mockRestore();
}, 10_000); },
MEDIA_GROUP_TEST_TIMEOUT_MS,
);
it("processes separate media groups independently", async () => { it(
const { createTelegramBot } = await import("./bot.js"); "processes separate media groups independently",
const replyModule = await import("../auto-reply/reply.js"); async () => {
const replySpy = replyModule.__replySpy as unknown as ReturnType< const { createTelegramBot } = await import("./bot.js");
typeof vi.fn const replyModule = await import("../auto-reply/reply.js");
>; const replySpy = replyModule.__replySpy as unknown as ReturnType<
typeof vi.fn
>;
onSpy.mockReset(); onSpy.mockReset();
replySpy.mockReset(); replySpy.mockReset();
const fetchSpy = vi.spyOn(globalThis, "fetch" as never).mockResolvedValue({ const fetchSpy = vi
ok: true, .spyOn(globalThis, "fetch" as never)
status: 200, .mockResolvedValue({
statusText: "OK", ok: true,
headers: { get: () => "image/png" }, status: 200,
arrayBuffer: async () => new Uint8Array([0x89, 0x50, 0x4e, 0x47]).buffer, statusText: "OK",
} as Response); headers: { get: () => "image/png" },
arrayBuffer: async () =>
new Uint8Array([0x89, 0x50, 0x4e, 0x47]).buffer,
} as Response);
createTelegramBot({ token: "tok" }); createTelegramBot({ token: "tok" });
const handler = onSpy.mock.calls.find( const handler = onSpy.mock.calls.find(
(call) => call[0] === "message", (call) => call[0] === "message",
)?.[1] as (ctx: Record<string, unknown>) => Promise<void>; )?.[1] as (ctx: Record<string, unknown>) => Promise<void>;
expect(handler).toBeDefined(); expect(handler).toBeDefined();
await handler({ await handler({
message: { message: {
chat: { id: 42, type: "private" }, chat: { id: 42, type: "private" },
message_id: 1, message_id: 1,
caption: "Album A", caption: "Album A",
date: 1736380800, date: 1736380800,
media_group_id: "albumA", media_group_id: "albumA",
photo: [{ file_id: "photoA1" }], photo: [{ file_id: "photoA1" }],
}, },
me: { username: "clawdbot_bot" }, me: { username: "clawdbot_bot" },
getFile: async () => ({ file_path: "photos/photoA1.jpg" }), getFile: async () => ({ file_path: "photos/photoA1.jpg" }),
}); });
await handler({ await handler({
message: { message: {
chat: { id: 42, type: "private" }, chat: { id: 42, type: "private" },
message_id: 2, message_id: 2,
caption: "Album B", caption: "Album B",
date: 1736380801, date: 1736380801,
media_group_id: "albumB", media_group_id: "albumB",
photo: [{ file_id: "photoB1" }], photo: [{ file_id: "photoB1" }],
}, },
me: { username: "clawdbot_bot" }, me: { username: "clawdbot_bot" },
getFile: async () => ({ file_path: "photos/photoB1.jpg" }), getFile: async () => ({ file_path: "photos/photoB1.jpg" }),
}); });
expect(replySpy).not.toHaveBeenCalled(); expect(replySpy).not.toHaveBeenCalled();
await waitForMediaGroupProcessing(replySpy, 2); await waitForMediaGroupProcessing(replySpy, 2);
expect(replySpy).toHaveBeenCalledTimes(2); expect(replySpy).toHaveBeenCalledTimes(2);
fetchSpy.mockRestore(); fetchSpy.mockRestore();
}, 10_000); },
MEDIA_GROUP_TEST_TIMEOUT_MS,
);
}); });
describe("telegram location parsing", () => { describe("telegram location parsing", () => {