From a69bd1fde8a01f0f0b88ac4ee0be334f854e4020 Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Sun, 12 Apr 2026 04:41:19 +0100 Subject: [PATCH] test(matrix): dedupe session route assertions --- extensions/matrix/src/session-route.test.ts | 100 +++++++------------- 1 file changed, 36 insertions(+), 64 deletions(-) diff --git a/extensions/matrix/src/session-route.test.ts b/extensions/matrix/src/session-route.test.ts index abf2907d225..6bab33a168f 100644 --- a/extensions/matrix/src/session-route.test.ts +++ b/extensions/matrix/src/session-route.test.ts @@ -126,6 +126,34 @@ function resolveUserRoute(params: { cfg: OpenClawConfig; accountId?: string; tar }); } +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", + }); +} + +function expectFallbackUserRoute( + route: ReturnType, + params?: { + userId?: string; + }, +) { + 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}`, + }); +} + afterEach(() => { for (const tempDir of tempDirs) { fs.rmSync(tempDir, { recursive: true, force: true }); @@ -144,14 +172,7 @@ describe("resolveMatrixOutboundSessionRoute", () => { accountId: "ops", }); - 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", - }); + expectCurrentDmRoomRoute(route); }); it("falls back to user-scoped routing when the current session is for another DM peer", () => { @@ -164,14 +185,7 @@ describe("resolveMatrixOutboundSessionRoute", () => { accountId: "ops", }); - expect(route).toMatchObject({ - sessionKey: "agent:main:main", - baseSessionKey: "agent:main:main", - peer: { kind: "direct", id: "@alice:example.org" }, - chatType: "direct", - from: "matrix:@alice:example.org", - to: "room:@alice:example.org", - }); + expectFallbackUserRoute(route); }); it("falls back to user-scoped routing when the current session belongs to another Matrix account", () => { @@ -184,14 +198,7 @@ describe("resolveMatrixOutboundSessionRoute", () => { accountId: "support", }); - expect(route).toMatchObject({ - sessionKey: "agent:main:main", - baseSessionKey: "agent:main:main", - peer: { kind: "direct", id: "@alice:example.org" }, - chatType: "direct", - from: "matrix:@alice:example.org", - to: "room:@alice:example.org", - }); + expectFallbackUserRoute(route); }); it("reuses the canonical DM room after user-target outbound metadata overwrites latest to fields", () => { @@ -211,14 +218,7 @@ describe("resolveMatrixOutboundSessionRoute", () => { accountId: "ops", }); - 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", - }); + expectCurrentDmRoomRoute(route); }); it("does not reuse the canonical DM room for a different Matrix user after latest metadata drift", () => { @@ -239,14 +239,7 @@ describe("resolveMatrixOutboundSessionRoute", () => { target: "@bob:example.org", }); - expect(route).toMatchObject({ - sessionKey: "agent:main:main", - baseSessionKey: "agent:main:main", - peer: { kind: "direct", id: "@bob:example.org" }, - chatType: "direct", - from: "matrix:@bob:example.org", - to: "room:@bob:example.org", - }); + expectFallbackUserRoute(route, { userId: "@bob:example.org" }); }); it("does not reuse a room after the session metadata was overwritten by a non-DM Matrix send", () => { @@ -259,14 +252,7 @@ describe("resolveMatrixOutboundSessionRoute", () => { accountId: "ops", }); - expect(route).toMatchObject({ - sessionKey: "agent:main:main", - baseSessionKey: "agent:main:main", - peer: { kind: "direct", id: "@alice:example.org" }, - chatType: "direct", - from: "matrix:@alice:example.org", - to: "room:@alice:example.org", - }); + expectFallbackUserRoute(route); }); it("uses the effective default Matrix account when accountId is omitted", () => { @@ -281,14 +267,7 @@ describe("resolveMatrixOutboundSessionRoute", () => { cfg, }); - 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", - }); + expectCurrentDmRoomRoute(route); }); it("reuses the current DM room when stored account metadata is missing", () => { @@ -303,13 +282,6 @@ describe("resolveMatrixOutboundSessionRoute", () => { cfg, }); - 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", - }); + expectCurrentDmRoomRoute(route); }); });