


























@@ -10,6 +10,30 @@ import {
1010watchTelegramFetch,
1111} from "./bot.media.test-utils.js";
121213+type ReplyPayload = { Body: string; MediaPaths?: string[] } & Record<string, unknown>;
14+15+function replyPayload(replySpy: ReturnType<typeof vi.fn>, index = 0): ReplyPayload {
16+const payload = replySpy.mock.calls.at(index)?.at(0);
17+if (typeof payload !== "object" || payload === null) {
18+throw new Error(`expected reply payload ${index}`);
19+}
20+return payload as ReplyPayload;
21+}
22+23+function downloadRequest(
24+fetchSpy: ReturnType<typeof vi.spyOn>,
25+index: number,
26+): {
27+filePathHint?: string;
28+url?: string;
29+} {
30+const request = fetchSpy.mock.calls.at(index)?.at(0);
31+if (typeof request !== "object" || request === null) {
32+throw new Error(`expected download request ${index}`);
33+}
34+return request as { filePathHint?: string; url?: string };
35+}
36+1337describe("telegram inbound media", () => {
1438// Parallel vitest shards can make this suite slower than the standalone run.
1539const INBOUND_MEDIA_TEST_TIMEOUT_MS = process.platform === "win32" ? 120_000 : 90_000;
@@ -46,7 +70,7 @@ describe("telegram inbound media", () => {
4670expect(downloadRequest?.url).toBe("https://api.telegram.org/file/bottok/photos/1.jpg");
4771expect(downloadRequest?.filePathHint).toBe("photos/1.jpg");
4872expect(params.replySpy).toHaveBeenCalledTimes(1);
49-const payload = params.replySpy.mock.calls[0][0];
73+const payload = replyPayload(params.replySpy);
5074expect(payload.Body).toContain("<media:image>");
5175},
5276},
@@ -120,7 +144,7 @@ describe("telegram inbound media", () => {
120144121145expect(runtimeError).not.toHaveBeenCalled();
122146expect(replySpy).toHaveBeenCalledTimes(1);
123-const payload = replySpy.mock.calls[0]?.[0] as { Body?: string; MediaPaths?: string[] };
147+const payload = replyPayload(replySpy);
124148expect(payload.Body).toContain("<media:image>");
125149expect(payload.MediaPaths).toContain(inboundPath);
126150} finally {
@@ -222,7 +246,7 @@ describe("telegram inbound media", () => {
222246});
223247224248expect(replySpy).toHaveBeenCalledTimes(1);
225-const payload = replySpy.mock.calls[0][0] as Record<string, unknown>;
249+const payload = replyPayload(replySpy);
226250testCase.assert(payload);
227251}
228252});
@@ -292,8 +316,8 @@ describe("telegram media groups", () => {
292316);
293317294318expect(runtimeError).not.toHaveBeenCalled();
295-const firstDownloadRequest = fetchSpy.mock.calls[0]?.[0] as { url?: string } | undefined;
296-const secondDownloadRequest = fetchSpy.mock.calls[1]?.[0] as { url?: string } | undefined;
319+const firstDownloadRequest = downloadRequest(fetchSpy, 0);
320+const secondDownloadRequest = downloadRequest(fetchSpy, 1);
297321expect(firstDownloadRequest?.url).toBe(
298322"http://127.0.0.1:8081/custom-bot-api/file/bottok/photos/photo1.jpg",
299323);
@@ -341,7 +365,7 @@ describe("telegram media groups", () => {
341365],
342366expectedReplyCount: 1,
343367assert: (replySpy: ReturnType<typeof vi.fn>) => {
344-const payload = replySpy.mock.calls[0]?.[0];
368+const payload = replyPayload(replySpy);
345369expect(payload?.Body).toContain("Here are my photos");
346370expect(payload?.MediaPaths).toHaveLength(2);
347371},
@@ -452,7 +476,7 @@ describe("telegram forwarded bursts", () => {
452476});
453477454478expect(runtimeError).not.toHaveBeenCalled();
455-const payload = replySpy.mock.calls[0][0];
479+const payload = replyPayload(replySpy);
456480expect(payload.Body).toContain("Look at this");
457481expect(payload.MediaPaths).toHaveLength(1);
458482} finally {
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。