diff --git a/src/infra/matrix-migration-config.test.ts b/src/infra/matrix-migration-config.test.ts new file mode 100644 index 00000000000..3bdae56ed15 --- /dev/null +++ b/src/infra/matrix-migration-config.test.ts @@ -0,0 +1,57 @@ +import fs from "node:fs"; +import path from "node:path"; +import { describe, expect, it } from "vitest"; +import { withTempHome } from "../../test/helpers/temp-home.js"; +import type { OpenClawConfig } from "../config/config.js"; +import { resolveMatrixMigrationAccountTarget } from "./matrix-migration-config.js"; + +function writeFile(filePath: string, value: string) { + fs.mkdirSync(path.dirname(filePath), { recursive: true }); + fs.writeFileSync(filePath, value, "utf8"); +} + +describe("resolveMatrixMigrationAccountTarget", () => { + it("ignores stored device IDs from stale cached Matrix credentials", async () => { + await withTempHome(async (home) => { + const stateDir = path.join(home, ".openclaw"); + writeFile( + path.join(stateDir, "credentials", "matrix", "credentials-ops.json"), + JSON.stringify( + { + homeserver: "https://matrix.example.org", + userId: "@old-bot:example.org", + accessToken: "tok-old", + deviceId: "DEVICE-OLD", + }, + null, + 2, + ), + ); + + const cfg: OpenClawConfig = { + channels: { + matrix: { + accounts: { + ops: { + homeserver: "https://matrix.example.org", + userId: "@new-bot:example.org", + accessToken: "tok-new", + }, + }, + }, + }, + }; + + const target = resolveMatrixMigrationAccountTarget({ + cfg, + env: process.env, + accountId: "ops", + }); + + expect(target).not.toBeNull(); + expect(target?.userId).toBe("@new-bot:example.org"); + expect(target?.accessToken).toBe("tok-new"); + expect(target?.storedDeviceId).toBeNull(); + }); + }); +}); diff --git a/src/infra/matrix-migration-config.ts b/src/infra/matrix-migration-config.ts index 1b40059e9da..2b434d39f2c 100644 --- a/src/infra/matrix-migration-config.ts +++ b/src/infra/matrix-migration-config.ts @@ -211,7 +211,7 @@ export function resolveMatrixMigrationAccountTarget(params: { userId, accessToken, rootDir, - storedDeviceId: stored?.deviceId ?? null, + storedDeviceId: matchingStored?.deviceId ?? null, }; }