test(matrix): dedupe session route assertions

This commit is contained in:
Vincent Koc
2026-04-12 04:41:19 +01:00
parent bf94513b11
commit a69bd1fde8

View File

@@ -126,6 +126,34 @@ function resolveUserRoute(params: { cfg: OpenClawConfig; accountId?: string; tar
});
}
function expectCurrentDmRoomRoute(route: ReturnType<typeof resolveMatrixOutboundSessionRoute>) {
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<typeof resolveMatrixOutboundSessionRoute>,
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);
});
});