diff --git a/extensions/matrix/src/matrix/client/storage.test.ts b/extensions/matrix/src/matrix/client/storage.test.ts index e273d5c3979..146aad14d87 100644 --- a/extensions/matrix/src/matrix/client/storage.test.ts +++ b/extensions/matrix/src/matrix/client/storage.test.ts @@ -6,7 +6,9 @@ import { afterEach, describe, expect, it, vi } from "vitest"; import { setMatrixRuntime } from "../../runtime.js"; import { maybeMigrateLegacyStorage, resolveMatrixStoragePaths } from "./storage.js"; -const maybeCreateMatrixMigrationSnapshotMock = vi.hoisted(() => vi.fn(async () => undefined)); +const maybeCreateMatrixMigrationSnapshotMock = vi.hoisted(() => + vi.fn(async (_params: unknown) => undefined), +); vi.mock("openclaw/plugin-sdk/matrix", async (importOriginal) => { const actual = await importOriginal(); diff --git a/extensions/matrix/src/matrix/monitor/auto-join.test.ts b/extensions/matrix/src/matrix/monitor/auto-join.test.ts index eb4d0220da8..94493100d9e 100644 --- a/extensions/matrix/src/matrix/monitor/auto-join.test.ts +++ b/extensions/matrix/src/matrix/monitor/auto-join.test.ts @@ -1,7 +1,7 @@ import type { PluginRuntime } from "openclaw/plugin-sdk/matrix"; import { beforeEach, describe, expect, it, vi } from "vitest"; import { setMatrixRuntime } from "../../runtime.js"; -import type { CoreConfig } from "../../types.js"; +import type { MatrixConfig } from "../../types.js"; import { registerMatrixAutoJoin } from "./auto-join.js"; type InviteHandler = (roomId: string, inviteEvent: unknown) => Promise; @@ -39,17 +39,13 @@ describe("registerMatrixAutoJoin", () => { it("joins all invites when autoJoin=always", async () => { const { client, getInviteHandler, joinRoom } = createClientStub(); - const cfg: CoreConfig = { - channels: { - matrix: { - autoJoin: "always", - }, - }, + const accountConfig: MatrixConfig = { + autoJoin: "always", }; registerMatrixAutoJoin({ client, - cfg, + accountConfig, runtime: { log: vi.fn(), error: vi.fn(), @@ -69,18 +65,14 @@ describe("registerMatrixAutoJoin", () => { alias: "#other:example.org", alt_aliases: ["#else:example.org"], }); - const cfg: CoreConfig = { - channels: { - matrix: { - autoJoin: "allowlist", - autoJoinAllowlist: ["#allowed:example.org"], - }, - }, + const accountConfig: MatrixConfig = { + autoJoin: "allowlist", + autoJoinAllowlist: ["#allowed:example.org"], }; registerMatrixAutoJoin({ client, - cfg, + accountConfig, runtime: { log: vi.fn(), error: vi.fn(), @@ -100,18 +92,40 @@ describe("registerMatrixAutoJoin", () => { alias: "#allowed:example.org", alt_aliases: ["#backup:example.org"], }); - const cfg: CoreConfig = { - channels: { - matrix: { - autoJoin: "allowlist", - autoJoinAllowlist: [" #allowed:example.org "], - }, - }, + const accountConfig: MatrixConfig = { + autoJoin: "allowlist", + autoJoinAllowlist: [" #allowed:example.org "], }; registerMatrixAutoJoin({ client, - cfg, + accountConfig, + runtime: { + log: vi.fn(), + error: vi.fn(), + } as unknown as import("openclaw/plugin-sdk/matrix").RuntimeEnv, + }); + + const inviteHandler = getInviteHandler(); + expect(inviteHandler).toBeTruthy(); + await inviteHandler!("!room:example.org", {}); + + expect(joinRoom).toHaveBeenCalledWith("!room:example.org"); + }); + + it("uses account-scoped auto-join settings for non-default accounts", async () => { + const { client, getInviteHandler, joinRoom, getRoomStateEvent } = createClientStub(); + getRoomStateEvent.mockResolvedValue({ + alias: "#ops-allowed:example.org", + alt_aliases: [], + }); + + registerMatrixAutoJoin({ + client, + accountConfig: { + autoJoin: "allowlist", + autoJoinAllowlist: ["#ops-allowed:example.org"], + }, runtime: { log: vi.fn(), error: vi.fn(), diff --git a/extensions/matrix/src/matrix/monitor/auto-join.ts b/extensions/matrix/src/matrix/monitor/auto-join.ts index 72d6cd79699..27f6a93e847 100644 --- a/extensions/matrix/src/matrix/monitor/auto-join.ts +++ b/extensions/matrix/src/matrix/monitor/auto-join.ts @@ -1,14 +1,14 @@ import type { RuntimeEnv } from "openclaw/plugin-sdk/matrix"; import { getMatrixRuntime } from "../../runtime.js"; -import type { CoreConfig } from "../../types.js"; +import type { MatrixConfig } from "../../types.js"; import type { MatrixClient } from "../sdk.js"; export function registerMatrixAutoJoin(params: { client: MatrixClient; - cfg: CoreConfig; + accountConfig: Pick; runtime: RuntimeEnv; }) { - const { client, cfg, runtime } = params; + const { client, accountConfig, runtime } = params; const core = getMatrixRuntime(); const logVerbose = (message: string) => { if (!core.logging.shouldLogVerbose()) { @@ -16,11 +16,9 @@ export function registerMatrixAutoJoin(params: { } runtime.log?.(message); }; - const autoJoin = cfg.channels?.["matrix"]?.autoJoin ?? "always"; + const autoJoin = accountConfig.autoJoin ?? "always"; const autoJoinAllowlist = new Set( - (cfg.channels?.["matrix"]?.autoJoinAllowlist ?? []) - .map((entry) => String(entry).trim()) - .filter(Boolean), + (accountConfig.autoJoinAllowlist ?? []).map((entry) => String(entry).trim()).filter(Boolean), ); if (autoJoin === "off") { diff --git a/extensions/matrix/src/matrix/monitor/index.ts b/extensions/matrix/src/matrix/monitor/index.ts index e50f62aac2e..5eb9f03c99e 100644 --- a/extensions/matrix/src/matrix/monitor/index.ts +++ b/extensions/matrix/src/matrix/monitor/index.ts @@ -164,7 +164,7 @@ export async function monitorMatrixProvider(opts: MonitorMatrixOpts = {}): Promi const startupMs = Date.now(); const startupGraceMs = 0; const directTracker = createDirectRoomTracker(client, { log: logVerboseMessage }); - registerMatrixAutoJoin({ client, cfg, runtime }); + registerMatrixAutoJoin({ client, accountConfig, runtime }); const warnedEncryptedRooms = new Set(); const warnedCryptoMissingRooms = new Set(); diff --git a/extensions/matrix/src/matrix/sdk/idb-persistence.test.ts b/extensions/matrix/src/matrix/sdk/idb-persistence.test.ts index 054638e2bd8..0c62f319583 100644 --- a/extensions/matrix/src/matrix/sdk/idb-persistence.test.ts +++ b/extensions/matrix/src/matrix/sdk/idb-persistence.test.ts @@ -76,7 +76,8 @@ async function readDatabaseRecords(params: { return; } db.close(); - resolve(keys.map((key, index) => ({ key, value: values[index] }))); + const resolvedValues = values; + resolve(keys.map((key, index) => ({ key, value: resolvedValues[index] }))); }; keysReq.onsuccess = () => {