
























@@ -11,9 +11,19 @@ import {
1111} from "./bot.media.test-utils.js";
12121313type ReplyPayload = { Body: string; MediaPaths?: string[] } & Record<string, unknown>;
14+type MockWithCalls = { mock: { calls: unknown[][] } };
15+16+function mockCall(mock: MockWithCalls, index: number): unknown[] {
17+const resolvedIndex = index < 0 ? mock.mock.calls.length + index : index;
18+const call = mock.mock.calls[resolvedIndex];
19+if (!call) {
20+throw new Error(`expected mock call ${index}`);
21+}
22+return call;
23+}
14241525function replyPayload(replySpy: ReturnType<typeof vi.fn>, index = 0): ReplyPayload {
16-const payload = replySpy.mock.calls.at(index)?.at(0);
26+const payload = mockCall(replySpy, index)[0];
1727if (typeof payload !== "object" || payload === null) {
1828throw new Error(`expected reply payload ${index}`);
1929}
@@ -27,7 +37,7 @@ function downloadRequest(
2737filePathHint?: string;
2838url?: string;
2939} {
30-const request = fetchSpy.mock.calls.at(index)?.at(0);
40+const request = mockCall(fetchSpy, index)[0];
3141if (typeof request !== "object" || request === null) {
3242throw new Error(`expected download request ${index}`);
3343}
@@ -64,11 +74,9 @@ describe("telegram inbound media", () => {
6474runtimeError: ReturnType<typeof vi.fn>;
6575}) => {
6676expect(params.runtimeError).not.toHaveBeenCalled();
67-const downloadRequest = params.fetchSpy.mock.calls.at(-1)?.[0] as
68-| { filePathHint?: string; url?: string }
69-| undefined;
70-expect(downloadRequest?.url).toBe("https://api.telegram.org/file/bottok/photos/1.jpg");
71-expect(downloadRequest?.filePathHint).toBe("photos/1.jpg");
77+const request = downloadRequest(params.fetchSpy, -1);
78+expect(request.url).toBe("https://api.telegram.org/file/bottok/photos/1.jpg");
79+expect(request.filePathHint).toBe("photos/1.jpg");
7280expect(params.replySpy).toHaveBeenCalledTimes(1);
7381const payload = replyPayload(params.replySpy);
7482expect(payload.Body).toContain("<media:image>");
@@ -185,9 +193,9 @@ describe("telegram inbound media", () => {
185193});
186194187195expect(runtimeError).not.toHaveBeenCalled();
188-const proxyCall = proxyFetch.mock.calls.at(-1);
189-expect(proxyCall?.[0]).toBe("https://api.telegram.org/file/bottok/photos/2.jpg");
190-expect((proxyCall?.[1] as RequestInit | undefined)?.redirect).toBe("manual");
196+const proxyCall = mockCall(proxyFetch, -1);
197+expect(proxyCall[0]).toBe("https://api.telegram.org/file/bottok/photos/2.jpg");
198+expect((proxyCall[1] as RequestInit | undefined)?.redirect).toBe("manual");
191199192200globalFetchSpy.mockRestore();
193201});
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。