






















@@ -34,6 +34,24 @@ type ParsedLineWebhookPayload = {
3434events: unknown;
3535};
363637+function firstMockCall(
38+mock: { mock: { calls: Array<readonly unknown[]> } },
39+label: string,
40+): readonly unknown[] {
41+const call = mock.mock.calls[0];
42+if (!call) {
43+throw new Error(`expected ${label} call`);
44+}
45+return call;
46+}
47+48+function firstParsedPayload(
49+mock: { mock: { calls: Array<readonly unknown[]> } },
50+label: string,
51+): ParsedLineWebhookPayload {
52+return firstMockCall(mock, label)[0] as ParsedLineWebhookPayload;
53+}
54+3755type RuntimeEnvMock = RuntimeEnv & {
3856error: ReturnType<typeof vi.fn<(...args: unknown[]) => void>>;
3957exit: ReturnType<typeof vi.fn<(code: number) => void>>;
@@ -234,9 +252,9 @@ async function expectSignedRawBodyWins(params: { rawBody: string | Buffer; signe
234252235253expect(res.status).toHaveBeenCalledWith(200);
236254expect(onEvents).toHaveBeenCalledTimes(1);
237-const processedBody = (
238-onEvents.mock.calls.at(0) as unknown as [{ events?: Array<{ source?: { userId?: string } }> }]
239-)?.[0];
255+const processedBody = firstMockCall(onEvents, "LINE webhook events")[0] as {
256+events?: Array<{ source?: { userId?: string } }>;
257+};
240258expect(processedBody?.events?.[0]?.source?.userId).toBe(params.signedUserId);
241259expect(processedBody?.events?.[0]?.source?.userId).not.toBe("tampered-user");
242260}
@@ -414,7 +432,7 @@ describe("createLineNodeWebhookHandler", () => {
414432415433expect(res.statusCode).toBe(200);
416434expect(bot.handleWebhook).toHaveBeenCalledTimes(1);
417-const [payload] = bot.handleWebhook.mock.calls.at(0) as unknown as [ParsedLineWebhookPayload];
435+const payload = firstParsedPayload(bot.handleWebhook, "LINE node webhook payload");
418436expect(payload.events).toEqual([{ type: "message" }]);
419437});
420438@@ -494,7 +512,7 @@ describe("createLineWebhookMiddleware", () => {
494512const { res, onEvents } = await invokeWebhook({ body });
495513expect(res.status).toHaveBeenCalledWith(200);
496514expect(onEvents).toHaveBeenCalledTimes(1);
497-const [payload] = onEvents.mock.calls.at(0) as unknown as [ParsedLineWebhookPayload];
515+const payload = firstParsedPayload(onEvents, "LINE middleware payload");
498516expect(payload.events).toEqual(expectedEvents);
499517});
500518此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。