From da620e4feb365a56fc985a2d7f620a6bf03562ee Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Mon, 11 May 2026 02:42:58 +0100 Subject: [PATCH] test: tighten matrix group history assertions --- .../monitor/handler.group-history.test.ts | 64 +++++++++---------- 1 file changed, 29 insertions(+), 35 deletions(-) diff --git a/extensions/matrix/src/matrix/monitor/handler.group-history.test.ts b/extensions/matrix/src/matrix/monitor/handler.group-history.test.ts index 98d0dc3a8fa..a4df868dd31 100644 --- a/extensions/matrix/src/matrix/monitor/handler.group-history.test.ts +++ b/extensions/matrix/src/matrix/monitor/handler.group-history.test.ts @@ -140,6 +140,20 @@ function createFinalDeliveryFailureHandler(finalizeInboundContext: (ctx: unknown }); } +function inboundHistoryBodies(finalizeInboundContext: ReturnType, callIndex: number) { + const ctx = finalizeInboundContext.mock.calls[callIndex]?.[0] as Record; + const history = ctx["InboundHistory"] as Array<{ body: string }> | undefined; + return history?.map((entry) => entry.body) ?? []; +} + +function expectSomeBodyContaining(bodies: readonly string[], fragment: string) { + expect(bodies.some((body) => body.includes(fragment))).toBe(true); +} + +function expectNoBodyContaining(bodies: readonly string[], fragment: string) { + expect(bodies.some((body) => body.includes(fragment))).toBe(false); +} + describe("matrix group chat history — scenario 1: basic accumulation", () => { it("pending messages appear in InboundHistory; trigger itself does not", async () => { const finalizeInboundContext = vi.fn((ctx: unknown) => ctx); @@ -199,15 +213,10 @@ describe("matrix group chat history — scenario 1: basic accumulation", () => { currentAgentId = "agent_b"; await handler(DEFAULT_ROOM, makeRoomTriggerEvent({ eventId: "$c", body: "msg C", ts: 3000 })); { - const ctx = finalizeInboundContext.mock.calls[1]?.[0] as Record; - const history = ctx["InboundHistory"] as Array<{ body: string }>; - expect(history).toHaveLength(2); - expect(history.map((h) => h.body)).toEqual( - expect.arrayContaining([expect.stringContaining("msg A")]), - ); - expect(history.map((h) => h.body)).toEqual( - expect.arrayContaining([expect.stringContaining("msg B")]), - ); + const bodies = inboundHistoryBodies(finalizeInboundContext, 1); + expect(bodies).toHaveLength(2); + expectSomeBodyContaining(bodies, "msg A"); + expectSomeBodyContaining(bodies, "msg B"); } // @agent_b trigger D — A/B/C consumed; history is empty @@ -398,10 +407,9 @@ describe("matrix group chat history — scenario 1: basic accumulation", () => { makeRoomTriggerEvent({ eventId: "$trigger-media", body: "trigger", ts: 2000 }), ); expect(finalizeInboundContext).toHaveBeenCalledOnce(); - const ctx = finalizeInboundContext.mock.calls[0]?.[0] as Record; - const history = ctx["InboundHistory"] as Array<{ body: string }> | undefined; - expect(history?.map((entry) => entry.body)).toEqual( - expect.arrayContaining([expect.stringContaining("[matrix image attachment]")]), + expectSomeBodyContaining( + inboundHistoryBodies(finalizeInboundContext, 0), + "[matrix image attachment]", ); }); @@ -465,11 +473,7 @@ describe("matrix group chat history — scenario 1: basic accumulation", () => { expect(getEvent).toHaveBeenCalledOnce(); expect(getRelations).toHaveBeenCalledOnce(); - const ctx = finalizeInboundContext.mock.calls[0]?.[0] as Record; - const history = ctx["InboundHistory"] as Array<{ body: string }> | undefined; - expect(history?.map((entry) => entry.body)).toEqual( - expect.arrayContaining([expect.stringContaining("Lunch?")]), - ); + expectSomeBodyContaining(inboundHistoryBodies(finalizeInboundContext, 0), "Lunch?"); }); }); @@ -520,14 +524,9 @@ describe("matrix group chat history — scenario 2: race condition safety", () = await handler(DEFAULT_ROOM, makeRoomTriggerEvent({ eventId: "$c", body: "msg C", ts: 3000 })); expect(finalizeInboundContext).toHaveBeenCalledTimes(2); - const ctxForC = finalizeInboundContext.mock.calls[1]?.[0] as Record; - const history = ctxForC["InboundHistory"] as Array<{ body: string }>; - expect(history.map((h) => h.body)).toEqual( - expect.arrayContaining([expect.stringContaining("msg B")]), - ); - expect(history.map((h) => h.body)).not.toEqual( - expect.arrayContaining([expect.stringContaining("msg A")]), - ); + const bodies = inboundHistoryBodies(finalizeInboundContext, 1); + expectSomeBodyContaining(bodies, "msg B"); + expectNoBodyContaining(bodies, "msg A"); }); it("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", () = ); expect(finalizeInboundContext).toHaveBeenCalledTimes(2); { - const ctx = finalizeInboundContext.mock.calls[1]?.[0] as Record; - const history = ctx["InboundHistory"] as Array<{ body: string }> | undefined; - expect(history?.map((h) => h.body)).toEqual( - expect.arrayContaining([expect.stringContaining("pending msg")]), - ); + expectSomeBodyContaining(inboundHistoryBodies(finalizeInboundContext, 1), "pending msg"); } }); @@ -639,10 +634,9 @@ describe("matrix group chat history — scenario 2: race condition safety", () = resolveFirstName?.(); await triggerDone; - const ctx = finalizeInboundContext.mock.calls[0]?.[0] as Record; - const history = ctx["InboundHistory"] as Array<{ body: string }> | undefined; - expect(history?.map((entry) => entry.body)).toEqual( - expect.arrayContaining([expect.stringContaining("plain before trigger")]), + expectSomeBodyContaining( + inboundHistoryBodies(finalizeInboundContext, 0), + "plain before trigger", ); });