




























@@ -122,7 +122,7 @@ function mockFirstReplyFailureWithWrappedError(msg: WebInboundMsg, message: stri
122122}
123123124124function expectFirstSendMediaPayload(msg: WebInboundMsg) {
125-const payload = vi.mocked(msg.sendMedia).mock.calls[0]?.[0];
125+const payload = mockCallArg(msg.sendMedia, 0, 0, "sendMedia");
126126if (!payload) {
127127throw new Error("expected first WhatsApp sendMedia payload");
128128}
@@ -141,13 +141,17 @@ function mockCallArg(mock: unknown, callIndex: number, argIndex: number, label:
141141if (!Array.isArray(calls)) {
142142throw new Error(`expected ${label} mock calls`);
143143}
144-const call = calls[callIndex];
144+const call = calls.at(callIndex);
145145if (!call) {
146146throw new Error(`expected ${label} call ${callIndex + 1}`);
147147}
148148return call[argIndex];
149149}
150150151+function replyText(msg: WebInboundMsg, callIndex = 0): string {
152+return String(mockCallArg(msg.reply, callIndex, 0, "reply"));
153+}
154+151155function findLoggerContext(mock: unknown, message: string, label: string) {
152156const calls = (mock as { mock?: { calls?: unknown[][] } }).mock?.calls;
153157if (!Array.isArray(calls)) {
@@ -301,7 +305,7 @@ describe("deliverWebReply", () => {
301305});
302306303307expect(msg.reply).toHaveBeenCalledTimes(1);
304-const sentText = vi.mocked(msg.reply).mock.calls[0]?.[0];
308+const sentText = replyText(msg);
305309expect(sentText).not.toContain("function_calls");
306310expect(sentText).not.toContain("invoke");
307311expect(sentText).toContain("Before");
@@ -331,7 +335,7 @@ describe("deliverWebReply", () => {
331335});
332336333337expect(msg.reply).toHaveBeenCalledTimes(1);
334-expect(vi.mocked(msg.reply).mock.calls[0]?.[0]).toBe("Before\n\nAfter\n");
338+expect(replyText(msg)).toBe("Before\n\nAfter\n");
335339});
336340337341it("strips legacy uppercase TOOL_CALL text before WhatsApp text delivery", async () => {
@@ -353,7 +357,7 @@ describe("deliverWebReply", () => {
353357});
354358355359expect(msg.reply).toHaveBeenCalledTimes(1);
356-expect(vi.mocked(msg.reply).mock.calls[0]?.[0]).toBe("Before\n\nAfter");
360+expect(replyText(msg)).toBe("Before\n\nAfter");
357361});
358362359363it("keeps quote threading on every text chunk for a threaded reply", async () => {
@@ -558,12 +562,8 @@ describe("deliverWebReply", () => {
558562});
559563560564expect(msg.reply).toHaveBeenCalledTimes(1);
561-expect(
562-String((msg.reply as unknown as { mock: { calls: unknown[][] } }).mock.calls[0]?.[0]),
563-).toContain("⚠️ Media failed");
564-expect(
565-String((msg.reply as unknown as { mock: { calls: unknown[][] } }).mock.calls[0]?.[0]),
566-).not.toContain("boom");
565+expect(replyText(msg)).toContain("⚠️ Media failed");
566+expect(replyText(msg)).not.toContain("boom");
567567const warnContext = findLoggerContext(
568568replyLogger.warn,
569569"failed to send web media reply",
@@ -626,12 +626,8 @@ describe("deliverWebReply", () => {
626626expect(secondPayload.mimetype).toBe("application/pdf");
627627expect(mockCallArg(msg.sendMedia, 1, 1, "sendMedia")).toBeUndefined();
628628expect(msg.reply).toHaveBeenCalledTimes(1);
629-expect(
630-String((msg.reply as unknown as { mock: { calls: unknown[][] } }).mock.calls[0]?.[0]),
631-).toContain("⚠️ Media failed");
632-expect(
633-String((msg.reply as unknown as { mock: { calls: unknown[][] } }).mock.calls[0]?.[0]),
634-).not.toContain("boom");
629+expect(replyText(msg)).toContain("⚠️ Media failed");
630+expect(replyText(msg)).not.toContain("boom");
635631});
636632637633it("sanitizes XML tool-call blocks for outbound sendPayload delivery", async () => {
@@ -651,7 +647,7 @@ describe("deliverWebReply", () => {
651647});
652648653649expect(sendWhatsApp).toHaveBeenCalledTimes(1);
654-const sentText = sendWhatsApp.mock.calls[0]?.[1];
650+const sentText = mockCallArg(sendWhatsApp, 0, 1, "sendWhatsApp");
655651expect(sentText).not.toContain("function_calls");
656652expect(sentText).not.toContain("invoke");
657653expect(sentText).toContain("Before");
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。