






















@@ -140,6 +140,20 @@ function createFinalDeliveryFailureHandler(finalizeInboundContext: (ctx: unknown
140140});
141141}
142142143+function inboundHistoryBodies(finalizeInboundContext: ReturnType<typeof vi.fn>, callIndex: number) {
144+const ctx = finalizeInboundContext.mock.calls[callIndex]?.[0] as Record<string, unknown>;
145+const history = ctx["InboundHistory"] as Array<{ body: string }> | undefined;
146+return history?.map((entry) => entry.body) ?? [];
147+}
148+149+function expectSomeBodyContaining(bodies: readonly string[], fragment: string) {
150+expect(bodies.some((body) => body.includes(fragment))).toBe(true);
151+}
152+153+function expectNoBodyContaining(bodies: readonly string[], fragment: string) {
154+expect(bodies.some((body) => body.includes(fragment))).toBe(false);
155+}
156+143157describe("matrix group chat history — scenario 1: basic accumulation", () => {
144158it("pending messages appear in InboundHistory; trigger itself does not", async () => {
145159const finalizeInboundContext = vi.fn((ctx: unknown) => ctx);
@@ -199,15 +213,10 @@ describe("matrix group chat history — scenario 1: basic accumulation", () => {
199213currentAgentId = "agent_b";
200214await handler(DEFAULT_ROOM, makeRoomTriggerEvent({ eventId: "$c", body: "msg C", ts: 3000 }));
201215{
202-const ctx = finalizeInboundContext.mock.calls[1]?.[0] as Record<string, unknown>;
203-const history = ctx["InboundHistory"] as Array<{ body: string }>;
204-expect(history).toHaveLength(2);
205-expect(history.map((h) => h.body)).toEqual(
206-expect.arrayContaining([expect.stringContaining("msg A")]),
207-);
208-expect(history.map((h) => h.body)).toEqual(
209-expect.arrayContaining([expect.stringContaining("msg B")]),
210-);
216+const bodies = inboundHistoryBodies(finalizeInboundContext, 1);
217+expect(bodies).toHaveLength(2);
218+expectSomeBodyContaining(bodies, "msg A");
219+expectSomeBodyContaining(bodies, "msg B");
211220}
212221213222// @agent_b trigger D — A/B/C consumed; history is empty
@@ -398,10 +407,9 @@ describe("matrix group chat history — scenario 1: basic accumulation", () => {
398407makeRoomTriggerEvent({ eventId: "$trigger-media", body: "trigger", ts: 2000 }),
399408);
400409expect(finalizeInboundContext).toHaveBeenCalledOnce();
401-const ctx = finalizeInboundContext.mock.calls[0]?.[0] as Record<string, unknown>;
402-const history = ctx["InboundHistory"] as Array<{ body: string }> | undefined;
403-expect(history?.map((entry) => entry.body)).toEqual(
404-expect.arrayContaining([expect.stringContaining("[matrix image attachment]")]),
410+expectSomeBodyContaining(
411+inboundHistoryBodies(finalizeInboundContext, 0),
412+"[matrix image attachment]",
405413);
406414});
407415@@ -465,11 +473,7 @@ describe("matrix group chat history — scenario 1: basic accumulation", () => {
465473466474expect(getEvent).toHaveBeenCalledOnce();
467475expect(getRelations).toHaveBeenCalledOnce();
468-const ctx = finalizeInboundContext.mock.calls[0]?.[0] as Record<string, unknown>;
469-const history = ctx["InboundHistory"] as Array<{ body: string }> | undefined;
470-expect(history?.map((entry) => entry.body)).toEqual(
471-expect.arrayContaining([expect.stringContaining("Lunch?")]),
472-);
476+expectSomeBodyContaining(inboundHistoryBodies(finalizeInboundContext, 0), "Lunch?");
473477});
474478});
475479@@ -520,14 +524,9 @@ describe("matrix group chat history — scenario 2: race condition safety", () =
520524await handler(DEFAULT_ROOM, makeRoomTriggerEvent({ eventId: "$c", body: "msg C", ts: 3000 }));
521525522526expect(finalizeInboundContext).toHaveBeenCalledTimes(2);
523-const ctxForC = finalizeInboundContext.mock.calls[1]?.[0] as Record<string, unknown>;
524-const history = ctxForC["InboundHistory"] as Array<{ body: string }>;
525-expect(history.map((h) => h.body)).toEqual(
526-expect.arrayContaining([expect.stringContaining("msg B")]),
527-);
528-expect(history.map((h) => h.body)).not.toEqual(
529-expect.arrayContaining([expect.stringContaining("msg A")]),
530-);
527+const bodies = inboundHistoryBodies(finalizeInboundContext, 1);
528+expectSomeBodyContaining(bodies, "msg B");
529+expectNoBodyContaining(bodies, "msg A");
531530});
532531533532it("watermark does not advance when final reply delivery fails (retry sees same history)", async () => {
@@ -559,11 +558,7 @@ describe("matrix group chat history — scenario 2: race condition safety", () =
559558);
560559expect(finalizeInboundContext).toHaveBeenCalledTimes(2);
561560{
562-const ctx = finalizeInboundContext.mock.calls[1]?.[0] as Record<string, unknown>;
563-const history = ctx["InboundHistory"] as Array<{ body: string }> | undefined;
564-expect(history?.map((h) => h.body)).toEqual(
565-expect.arrayContaining([expect.stringContaining("pending msg")]),
566-);
561+expectSomeBodyContaining(inboundHistoryBodies(finalizeInboundContext, 1), "pending msg");
567562}
568563});
569564@@ -639,10 +634,9 @@ describe("matrix group chat history — scenario 2: race condition safety", () =
639634resolveFirstName?.();
640635await triggerDone;
641636642-const ctx = finalizeInboundContext.mock.calls[0]?.[0] as Record<string, unknown>;
643-const history = ctx["InboundHistory"] as Array<{ body: string }> | undefined;
644-expect(history?.map((entry) => entry.body)).toEqual(
645-expect.arrayContaining([expect.stringContaining("plain before trigger")]),
637+expectSomeBodyContaining(
638+inboundHistoryBodies(finalizeInboundContext, 0),
639+"plain before trigger",
646640);
647641});
648642此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。