

























@@ -141,11 +141,22 @@ function createFinalDeliveryFailureHandler(finalizeInboundContext: (ctx: unknown
141141}
142142143143function inboundHistoryBodies(finalizeInboundContext: ReturnType<typeof vi.fn>, callIndex: number) {
144-const ctx = finalizeInboundContext.mock.calls[callIndex]?.[0] as Record<string, unknown>;
144+const ctx = finalizeInboundContextCall(finalizeInboundContext, callIndex);
145145const history = ctx["InboundHistory"] as Array<{ body: string }> | undefined;
146146return history?.map((entry) => entry.body) ?? [];
147147}
148148149+function finalizeInboundContextCall(
150+finalizeInboundContext: ReturnType<typeof vi.fn>,
151+callIndex: number,
152+) {
153+const ctx = finalizeInboundContext.mock.calls.at(callIndex)?.[0];
154+if (!ctx || typeof ctx !== "object") {
155+throw new Error(`Expected finalizeInboundContext call ${callIndex + 1}`);
156+}
157+return ctx as Record<string, unknown>;
158+}
159+149160function expectSomeBodyContaining(bodies: readonly string[], fragment: string) {
150161expect(bodies.some((body) => body.includes(fragment))).toBe(true);
151162}
@@ -175,7 +186,7 @@ describe("matrix group chat history — scenario 1: basic accumulation", () => {
175186// Trigger B — history must contain [msg A] only, not the trigger itself
176187await handler(DEFAULT_ROOM, makeRoomTriggerEvent({ eventId: "$b", body: "msg B", ts: 2000 }));
177188expect(finalizeInboundContext).toHaveBeenCalledOnce();
178-const ctx = finalizeInboundContext.mock.calls[0]?.[0] as Record<string, unknown>;
189+const ctx = finalizeInboundContextCall(finalizeInboundContext, 0);
179190const history = ctx["InboundHistory"] as Array<{ body: string; sender: string }>;
180191expect(history).toHaveLength(1);
181192expect(history[0]?.body).toContain("msg A");
@@ -203,7 +214,7 @@ describe("matrix group chat history — scenario 1: basic accumulation", () => {
203214currentAgentId = "agent_a";
204215await handler(DEFAULT_ROOM, makeRoomTriggerEvent({ eventId: "$b", body: "msg B", ts: 2000 }));
205216{
206-const ctx = finalizeInboundContext.mock.calls[0]?.[0] as Record<string, unknown>;
217+const ctx = finalizeInboundContextCall(finalizeInboundContext, 0);
207218const history = ctx["InboundHistory"] as Array<{ body: string }>;
208219expect(history).toHaveLength(1);
209220expect(history[0]?.body).toContain("msg A");
@@ -223,7 +234,7 @@ describe("matrix group chat history — scenario 1: basic accumulation", () => {
223234currentAgentId = "agent_b";
224235await handler(DEFAULT_ROOM, makeRoomTriggerEvent({ eventId: "$d", body: "msg D", ts: 4000 }));
225236{
226-const ctx = finalizeInboundContext.mock.calls[2]?.[0] as Record<string, unknown>;
237+const ctx = finalizeInboundContextCall(finalizeInboundContext, 2);
227238const history = ctx["InboundHistory"] as Array<unknown> | undefined;
228239expect(history ?? []).toHaveLength(0);
229240}
@@ -250,7 +261,7 @@ describe("matrix group chat history — scenario 1: basic accumulation", () => {
250261}
251262252263await handler(DEFAULT_ROOM, makeRoomTriggerEvent({ eventId: "$t", body: "trigger", ts: 5000 }));
253-const ctx = finalizeInboundContext.mock.calls[0]?.[0] as Record<string, unknown>;
264+const ctx = finalizeInboundContextCall(finalizeInboundContext, 0);
254265const history = ctx["InboundHistory"] as Array<{ body: string }>;
255266expect(history).toHaveLength(2);
256267expect(history[0]?.body).toContain("pending 3");
@@ -273,7 +284,7 @@ describe("matrix group chat history — scenario 1: basic accumulation", () => {
273284await handler(DEFAULT_ROOM, makeRoomPlainEvent({ eventId: "$p", body: "pending" }));
274285await handler(DEFAULT_ROOM, makeRoomTriggerEvent({ eventId: "$t", body: "trigger" }));
275286276-const ctx = finalizeInboundContext.mock.calls[0]?.[0] as Record<string, unknown>;
287+const ctx = finalizeInboundContextCall(finalizeInboundContext, 0);
277288const history = ctx["InboundHistory"] as Array<unknown> | undefined;
278289expect(history ?? []).toHaveLength(0);
279290});
@@ -545,7 +556,7 @@ describe("matrix group chat history — scenario 2: race condition safety", () =
545556);
546557expect(finalizeInboundContext).toHaveBeenCalledOnce();
547558{
548-const ctx = finalizeInboundContext.mock.calls[0]?.[0] as Record<string, unknown>;
559+const ctx = finalizeInboundContextCall(finalizeInboundContext, 0);
549560const history = ctx["InboundHistory"] as Array<{ body: string }>;
550561expect(history).toHaveLength(1);
551562expect(history[0]?.body).toContain("pending msg");
@@ -581,10 +592,10 @@ describe("matrix group chat history — scenario 2: race condition safety", () =
581592);
582593583594expect(finalizeInboundContext).toHaveBeenCalledTimes(2);
584-const firstHistory = (finalizeInboundContext.mock.calls[0]?.[0] as Record<string, unknown>)[
595+const firstHistory = finalizeInboundContextCall(finalizeInboundContext, 0)[
585596"InboundHistory"
586597] as Array<{ body: string }>;
587-const retryHistory = (finalizeInboundContext.mock.calls[1]?.[0] as Record<string, unknown>)[
598+const retryHistory = finalizeInboundContextCall(finalizeInboundContext, 1)[
588599"InboundHistory"
589600] as Array<{ body: string }>;
590601@@ -683,7 +694,7 @@ describe("matrix group chat history — scenario 2: race condition safety", () =
683694releaseFirstGetUserId?.();
684695await Promise.all([plainPromise, triggerPromise]);
685696686-const ctx = finalizeInboundContext.mock.calls[0]?.[0] as Record<string, unknown>;
697+const ctx = finalizeInboundContextCall(finalizeInboundContext, 0);
687698const history = ctx["InboundHistory"] as Array<{ body: string }>;
688699expect(history.map((entry) => entry.body)).toEqual(["msg A"]);
689700});
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。