From 086bb3514f12be458cea2f7c471c8b95122a5f1b Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Mon, 11 May 2026 08:41:46 +0100 Subject: [PATCH] test: tighten matrix route assertions --- extensions/matrix/src/session-route.test.ts | 70 +++++++++++---------- 1 file changed, 38 insertions(+), 32 deletions(-) diff --git a/extensions/matrix/src/session-route.test.ts b/extensions/matrix/src/session-route.test.ts index 30045046f2a6..5ca8c5b38ac4 100644 --- a/extensions/matrix/src/session-route.test.ts +++ b/extensions/matrix/src/session-route.test.ts @@ -145,14 +145,14 @@ function resolveUserRouteForCurrentSession(params: { } function expectCurrentDmRoomRoute(route: ReturnType) { - expect(route).toMatchObject({ - sessionKey: currentDmSessionKey, - baseSessionKey: currentDmSessionKey, - peer: { kind: "channel", id: "!dm:example.org" }, - chatType: "direct", - from: "matrix:@alice:example.org", - to: "room:!dm:example.org", - }); + const currentRoute = expectRoute(route); + expect(currentRoute.sessionKey).toBe(currentDmSessionKey); + expect(currentRoute.baseSessionKey).toBe(currentDmSessionKey); + expect(currentRoute.peer.kind).toBe("channel"); + expect(currentRoute.peer.id).toBe("!dm:example.org"); + expect(currentRoute.chatType).toBe("direct"); + expect(currentRoute.from).toBe("matrix:@alice:example.org"); + expect(currentRoute.to).toBe("room:!dm:example.org"); } function expectFallbackUserRoute( @@ -162,14 +162,21 @@ function expectFallbackUserRoute( }, ) { const userId = params?.userId ?? "@alice:example.org"; - expect(route).toMatchObject({ - sessionKey: "agent:main:main", - baseSessionKey: "agent:main:main", - peer: { kind: "direct", id: userId }, - chatType: "direct", - from: `matrix:${userId}`, - to: `room:${userId}`, - }); + const fallbackRoute = expectRoute(route); + expect(fallbackRoute.sessionKey).toBe("agent:main:main"); + expect(fallbackRoute.baseSessionKey).toBe("agent:main:main"); + expect(fallbackRoute.peer.kind).toBe("direct"); + expect(fallbackRoute.peer.id).toBe(userId); + expect(fallbackRoute.chatType).toBe("direct"); + expect(fallbackRoute.from).toBe(`matrix:${userId}`); + expect(fallbackRoute.to).toBe(`room:${userId}`); +} + +function expectRoute(route: ReturnType) { + if (!route) { + throw new Error("Expected Matrix route"); + } + return route; } afterEach(() => { @@ -275,11 +282,12 @@ describe("resolveMatrixOutboundSessionRoute", () => { currentSessionKey: "agent:main:matrix:channel:!ops:example.org:thread:$RootEvent:Example.Org", }); - expect(route).toMatchObject({ - sessionKey: "agent:main:matrix:channel:!ops:example.org:thread:$RootEvent:Example.Org", - baseSessionKey: "agent:main:matrix:channel:!ops:example.org", - threadId: "$RootEvent:Example.Org", - }); + const channelRoute = expectRoute(route); + expect(channelRoute.sessionKey).toBe( + "agent:main:matrix:channel:!ops:example.org:thread:$RootEvent:Example.Org", + ); + expect(channelRoute.baseSessionKey).toBe("agent:main:matrix:channel:!ops:example.org"); + expect(channelRoute.threadId).toBe("$RootEvent:Example.Org"); }); it("resolves per-room DM metadata from the base key when currentSessionKey has a thread suffix", () => { @@ -306,12 +314,11 @@ describe("resolveMatrixOutboundSessionRoute", () => { currentSessionKey: `${route?.baseSessionKey}:thread:$DmRoot:Example.Org`, }); - expect(threadedRoute).toMatchObject({ - sessionKey: `${route?.baseSessionKey}:thread:$DmRoot:Example.Org`, - baseSessionKey: route?.baseSessionKey, - to: "room:!dm:example.org", - threadId: "$DmRoot:Example.Org", - }); + const dmThreadRoute = expectRoute(threadedRoute); + expect(dmThreadRoute.sessionKey).toBe(`${route?.baseSessionKey}:thread:$DmRoot:Example.Org`); + expect(dmThreadRoute.baseSessionKey).toBe(route?.baseSessionKey); + expect(dmThreadRoute.to).toBe("room:!dm:example.org"); + expect(dmThreadRoute.threadId).toBe("$DmRoot:Example.Org"); }); it('does not recover currentSessionKey threads for shared dmScope "main" DMs', () => { @@ -327,10 +334,9 @@ describe("resolveMatrixOutboundSessionRoute", () => { }, }); - expect(route).toMatchObject({ - sessionKey: "agent:main:main", - baseSessionKey: "agent:main:main", - }); - expect(route?.threadId).toBeUndefined(); + const dmRoute = expectRoute(route); + expect(dmRoute.sessionKey).toBe("agent:main:main"); + expect(dmRoute.baseSessionKey).toBe("agent:main:main"); + expect(dmRoute.threadId).toBeUndefined(); }); });