matrix: prefer named default account

This commit is contained in:
Gustavo Madeira Santana
2026-04-14 17:01:16 -04:00
parent 70b67b0c68
commit 5bf30d258f
4 changed files with 48 additions and 6 deletions

View File

@@ -56,6 +56,22 @@ describe("matrix account selection", () => {
expect(requiresExplicitMatrixDefaultAccount(cfg)).toBe(true);
});
it('uses a named "default" Matrix account when defaultAccount is unset', () => {
const cfg: OpenClawConfig = {
channels: {
matrix: {
accounts: {
default: { homeserver: "https://matrix.example.org" },
ops: { homeserver: "https://matrix.example.org" },
},
},
},
};
expect(resolveMatrixDefaultOrOnlyAccountId(cfg)).toBe("default");
expect(requiresExplicitMatrixDefaultAccount(cfg)).toBe(false);
});
it("finds the raw Matrix account entry by normalized account id", () => {
const cfg: OpenClawConfig = {
channels: {
@@ -93,7 +109,7 @@ describe("matrix account selection", () => {
expect(requiresExplicitMatrixDefaultAccount(cfg, env)).toBe(false);
});
it("treats mixed default and named env-backed Matrix accounts as multi-account", () => {
it('uses the "default" Matrix account when mixed default and named env-backed accounts exist', () => {
const keys = getMatrixScopedEnvVarNames("team-ops");
const cfg: OpenClawConfig = {
channels: {
@@ -108,7 +124,8 @@ describe("matrix account selection", () => {
} satisfies NodeJS.ProcessEnv;
expect(resolveConfiguredMatrixAccountIds(cfg, env)).toEqual(["default", "team-ops"]);
expect(requiresExplicitMatrixDefaultAccount(cfg, env)).toBe(true);
expect(resolveMatrixDefaultOrOnlyAccountId(cfg, env)).toBe("default");
expect(requiresExplicitMatrixDefaultAccount(cfg, env)).toBe(false);
});
it("discovers default Matrix accounts backed only by global env vars", () => {

View File

@@ -213,6 +213,9 @@ export function requiresExplicitMatrixDefaultAccount(
if (configuredAccountIds.length <= 1) {
return false;
}
if (configuredAccountIds.includes(DEFAULT_ACCOUNT_ID)) {
return false;
}
const configuredDefault = normalizeOptionalAccountId(
typeof channel.defaultAccount === "string" ? channel.defaultAccount : undefined,
);

View File

@@ -403,6 +403,29 @@ describe("Matrix auth/config live surfaces", () => {
);
});
it('uses a named "default" account implicitly when multiple Matrix accounts exist', () => {
const cfg = {
channels: {
matrix: {
accounts: {
default: {
homeserver: "https://matrix.default.example.org",
accessToken: "default-token",
},
ops: {
homeserver: "https://matrix.ops.example.org",
accessToken: "ops-token",
},
},
},
},
} as CoreConfig;
expect(resolveMatrixAuthContext({ cfg, env: {} as NodeJS.ProcessEnv }).accountId).toBe(
"default",
);
});
it("does not materialize a default account from shared top-level defaults alone", () => {
const cfg = {
channels: {
@@ -439,7 +462,7 @@ describe("Matrix auth/config live surfaces", () => {
expect(resolveMatrixAuthContext({ cfg, env: {} as NodeJS.ProcessEnv }).accountId).toBe("ops");
});
it("honors injected env when implicit Matrix account selection becomes ambiguous", () => {
it('uses the injected env-backed "default" Matrix account when implicit selection is available', () => {
const cfg = {
channels: {
matrix: {},
@@ -452,9 +475,7 @@ describe("Matrix auth/config live surfaces", () => {
MATRIX_OPS_ACCESS_TOKEN: "ops-token",
} as NodeJS.ProcessEnv;
expect(() => resolveMatrixAuthContext({ cfg, env })).toThrow(
/channels\.matrix\.defaultAccount.*--account <id>/i,
);
expect(resolveMatrixAuthContext({ cfg, env }).accountId).toBe("default");
});
it("does not materialize a default env account from partial global auth fields", () => {