test(telegram): harden media tests

This commit is contained in:
Peter Steinberger
2026-01-10 04:18:05 +01:00
parent 6274adce3a
commit 056c4ae622

View File

@@ -62,7 +62,12 @@ vi.mock("../auto-reply/reply.js", () => {
}); });
describe("telegram inbound media", () => { describe("telegram inbound media", () => {
it("downloads media via file_path (no file.download)", async () => { const INBOUND_MEDIA_TEST_TIMEOUT_MS =
process.platform === "win32" ? 30_000 : 10_000;
it(
"downloads media via file_path (no file.download)",
async () => {
const { createTelegramBot } = await import("./bot.js"); const { createTelegramBot } = await import("./bot.js");
const replyModule = await import("../auto-reply/reply.js"); const replyModule = await import("../auto-reply/reply.js");
const replySpy = replyModule.__replySpy as unknown as ReturnType< const replySpy = replyModule.__replySpy as unknown as ReturnType<
@@ -121,7 +126,9 @@ describe("telegram inbound media", () => {
expect(payload.Body).toContain("<media:image>"); expect(payload.Body).toContain("<media:image>");
fetchSpy.mockRestore(); fetchSpy.mockRestore();
}); },
INBOUND_MEDIA_TEST_TIMEOUT_MS,
);
it("prefers proxyFetch over global fetch", async () => { it("prefers proxyFetch over global fetch", async () => {
const { createTelegramBot } = await import("./bot.js"); const { createTelegramBot } = await import("./bot.js");
@@ -283,7 +290,7 @@ describe("telegram media groups", () => {
)?.[1] as (ctx: Record<string, unknown>) => Promise<void>; )?.[1] as (ctx: Record<string, unknown>) => Promise<void>;
expect(handler).toBeDefined(); expect(handler).toBeDefined();
await handler({ const first = handler({
message: { message: {
chat: { id: 42, type: "private" }, chat: { id: 42, type: "private" },
message_id: 1, message_id: 1,
@@ -296,7 +303,7 @@ describe("telegram media groups", () => {
getFile: async () => ({ file_path: "photos/photo1.jpg" }), getFile: async () => ({ file_path: "photos/photo1.jpg" }),
}); });
await handler({ const second = handler({
message: { message: {
chat: { id: 42, type: "private" }, chat: { id: 42, type: "private" },
message_id: 2, message_id: 2,
@@ -308,6 +315,8 @@ describe("telegram media groups", () => {
getFile: async () => ({ file_path: "photos/photo2.jpg" }), getFile: async () => ({ file_path: "photos/photo2.jpg" }),
}); });
await Promise.all([first, second]);
expect(replySpy).not.toHaveBeenCalled(); expect(replySpy).not.toHaveBeenCalled();
await waitForMediaGroupProcessing(replySpy, 1); await waitForMediaGroupProcessing(replySpy, 1);
@@ -351,7 +360,7 @@ describe("telegram media groups", () => {
)?.[1] as (ctx: Record<string, unknown>) => Promise<void>; )?.[1] as (ctx: Record<string, unknown>) => Promise<void>;
expect(handler).toBeDefined(); expect(handler).toBeDefined();
await handler({ const first = handler({
message: { message: {
chat: { id: 42, type: "private" }, chat: { id: 42, type: "private" },
message_id: 1, message_id: 1,
@@ -364,7 +373,7 @@ describe("telegram media groups", () => {
getFile: async () => ({ file_path: "photos/photoA1.jpg" }), getFile: async () => ({ file_path: "photos/photoA1.jpg" }),
}); });
await handler({ const second = handler({
message: { message: {
chat: { id: 42, type: "private" }, chat: { id: 42, type: "private" },
message_id: 2, message_id: 2,
@@ -377,6 +386,8 @@ describe("telegram media groups", () => {
getFile: async () => ({ file_path: "photos/photoB1.jpg" }), getFile: async () => ({ file_path: "photos/photoB1.jpg" }),
}); });
await Promise.all([first, second]);
expect(replySpy).not.toHaveBeenCalled(); expect(replySpy).not.toHaveBeenCalled();
await waitForMediaGroupProcessing(replySpy, 2); await waitForMediaGroupProcessing(replySpy, 2);