test(telegram): harden media tests
This commit is contained in:
@@ -62,66 +62,73 @@ 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 =
|
||||||
const { createTelegramBot } = await import("./bot.js");
|
process.platform === "win32" ? 30_000 : 10_000;
|
||||||
const replyModule = await import("../auto-reply/reply.js");
|
|
||||||
const replySpy = replyModule.__replySpy as unknown as ReturnType<
|
|
||||||
typeof vi.fn
|
|
||||||
>;
|
|
||||||
|
|
||||||
onSpy.mockReset();
|
it(
|
||||||
replySpy.mockReset();
|
"downloads media via file_path (no file.download)",
|
||||||
sendChatActionSpy.mockReset();
|
async () => {
|
||||||
|
const { createTelegramBot } = await import("./bot.js");
|
||||||
|
const replyModule = await import("../auto-reply/reply.js");
|
||||||
|
const replySpy = replyModule.__replySpy as unknown as ReturnType<
|
||||||
|
typeof vi.fn
|
||||||
|
>;
|
||||||
|
|
||||||
const runtimeLog = vi.fn();
|
onSpy.mockReset();
|
||||||
const runtimeError = vi.fn();
|
replySpy.mockReset();
|
||||||
createTelegramBot({
|
sendChatActionSpy.mockReset();
|
||||||
token: "tok",
|
|
||||||
runtime: {
|
const runtimeLog = vi.fn();
|
||||||
log: runtimeLog,
|
const runtimeError = vi.fn();
|
||||||
error: runtimeError,
|
createTelegramBot({
|
||||||
exit: () => {
|
token: "tok",
|
||||||
throw new Error("exit");
|
runtime: {
|
||||||
|
log: runtimeLog,
|
||||||
|
error: runtimeError,
|
||||||
|
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();
|
|
||||||
|
|
||||||
const fetchSpy = vi
|
const fetchSpy = vi
|
||||||
.spyOn(globalThis, "fetch" as never)
|
.spyOn(globalThis, "fetch" as never)
|
||||||
.mockResolvedValueOnce({
|
.mockResolvedValueOnce({
|
||||||
ok: true,
|
ok: true,
|
||||||
status: 200,
|
status: 200,
|
||||||
statusText: "OK",
|
statusText: "OK",
|
||||||
headers: { get: () => "image/jpeg" },
|
headers: { get: () => "image/jpeg" },
|
||||||
arrayBuffer: async () =>
|
arrayBuffer: async () =>
|
||||||
new Uint8Array([0xff, 0xd8, 0xff, 0x00]).buffer,
|
new Uint8Array([0xff, 0xd8, 0xff, 0x00]).buffer,
|
||||||
} as Response);
|
} as Response);
|
||||||
|
|
||||||
await handler({
|
await handler({
|
||||||
message: {
|
message: {
|
||||||
message_id: 1,
|
message_id: 1,
|
||||||
chat: { id: 1234, type: "private" },
|
chat: { id: 1234, type: "private" },
|
||||||
photo: [{ file_id: "fid" }],
|
photo: [{ file_id: "fid" }],
|
||||||
date: 1736380800, // 2025-01-09T00:00:00Z
|
date: 1736380800, // 2025-01-09T00:00:00Z
|
||||||
},
|
},
|
||||||
me: { username: "clawdbot_bot" },
|
me: { username: "clawdbot_bot" },
|
||||||
getFile: async () => ({ file_path: "photos/1.jpg" }),
|
getFile: async () => ({ file_path: "photos/1.jpg" }),
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(runtimeError).not.toHaveBeenCalled();
|
expect(runtimeError).not.toHaveBeenCalled();
|
||||||
expect(fetchSpy).toHaveBeenCalledWith(
|
expect(fetchSpy).toHaveBeenCalledWith(
|
||||||
"https://api.telegram.org/file/bottok/photos/1.jpg",
|
"https://api.telegram.org/file/bottok/photos/1.jpg",
|
||||||
);
|
);
|
||||||
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("<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);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user