From 8f86cb92ac86e627cbe5cfff189a97d4e973eab2 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Fri, 13 Mar 2026 18:49:21 +0000 Subject: [PATCH] test: harden device identity state dir coverage --- src/infra/device-identity.state-dir.test.ts | 36 +++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/infra/device-identity.state-dir.test.ts b/src/infra/device-identity.state-dir.test.ts index 71281344819..785fa343ec0 100644 --- a/src/infra/device-identity.state-dir.test.ts +++ b/src/infra/device-identity.state-dir.test.ts @@ -13,4 +13,40 @@ describe("device identity state dir defaults", () => { expect(raw.deviceId).toBe(identity.deviceId); }); }); + + it("reuses the stored identity on subsequent loads", async () => { + await withStateDirEnv("openclaw-identity-state-", async ({ stateDir }) => { + const first = loadOrCreateDeviceIdentity(); + const second = loadOrCreateDeviceIdentity(); + const identityPath = path.join(stateDir, "identity", "device.json"); + const raw = JSON.parse(await fs.readFile(identityPath, "utf8")) as { + deviceId?: string; + publicKeyPem?: string; + }; + + expect(second).toEqual(first); + expect(raw.deviceId).toBe(first.deviceId); + expect(raw.publicKeyPem).toBe(first.publicKeyPem); + }); + }); + + it("repairs stored device IDs that no longer match the public key", async () => { + await withStateDirEnv("openclaw-identity-state-", async ({ stateDir }) => { + const original = loadOrCreateDeviceIdentity(); + const identityPath = path.join(stateDir, "identity", "device.json"); + const raw = JSON.parse(await fs.readFile(identityPath, "utf8")) as Record; + + await fs.writeFile( + identityPath, + `${JSON.stringify({ ...raw, deviceId: "stale-device-id" }, null, 2)}\n`, + "utf8", + ); + + const repaired = loadOrCreateDeviceIdentity(); + const stored = JSON.parse(await fs.readFile(identityPath, "utf8")) as { deviceId?: string }; + + expect(repaired.deviceId).toBe(original.deviceId); + expect(stored.deviceId).toBe(original.deviceId); + }); + }); });