|
| 1 | +import fs from "node:fs"; |
| 2 | +import fsp from "node:fs/promises"; |
| 3 | +import path from "node:path"; |
| 4 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 5 | +import { useAutoCleanupTempDirTracker } from "../../test/helpers/temp-dir.js"; |
| 6 | +import { loadDeviceAuthToken } from "../infra/device-auth-store.js"; |
| 7 | +import { generateStoredDeviceIdentity } from "../infra/device-identity-store.js"; |
| 8 | +import { loadDeviceIdentityIfPresent } from "../infra/device-identity.js"; |
| 9 | +import { resolveExecApprovalsPath } from "../infra/exec-approvals-config.js"; |
| 10 | +import { loadExecApprovals } from "../infra/exec-approvals-store.js"; |
| 11 | +import { testing as execApprovalsStoreTesting } from "../infra/exec-approvals-store.test-support.js"; |
| 12 | +import { acquireGatewayLock } from "../infra/gateway-lock.js"; |
| 13 | +import { closeOpenClawStateDatabaseForTest } from "../state/openclaw-state-db.js"; |
| 14 | +import { captureEnv, setTestEnvValue } from "../test-utils/env.js"; |
| 15 | +import { runStartupMigrations } from "./startup-state-migrations.js"; |
| 16 | + |
| 17 | +describe("node-host startup state migrations", () => { |
| 18 | + const envSnapshot = captureEnv(["OPENCLAW_STATE_DIR"]); |
| 19 | + const log = { |
| 20 | + info: vi.fn<(message: string) => void>(), |
| 21 | + warn: vi.fn<(message: string) => void>(), |
| 22 | + }; |
| 23 | + const tempDirs = useAutoCleanupTempDirTracker((cleanup) => { |
| 24 | + afterEach(() => { |
| 25 | + closeOpenClawStateDatabaseForTest(); |
| 26 | + execApprovalsStoreTesting.reset(); |
| 27 | + envSnapshot.restore(); |
| 28 | + cleanup(); |
| 29 | + }); |
| 30 | + }); |
| 31 | + |
| 32 | + beforeEach(() => { |
| 33 | + vi.clearAllMocks(); |
| 34 | + }); |
| 35 | + |
| 36 | + function useStateDir(): { env: NodeJS.ProcessEnv; stateDir: string } { |
| 37 | + const stateDir = fs.realpathSync(tempDirs.make("openclaw-node-host-migrations-")); |
| 38 | + return { |
| 39 | + env: { ...process.env, HOME: stateDir, OPENCLAW_STATE_DIR: stateDir }, |
| 40 | + stateDir, |
| 41 | + }; |
| 42 | + } |
| 43 | + |
| 44 | + async function writeDeviceAuth(stateDir: string): Promise<string> { |
| 45 | + const sourcePath = path.join(stateDir, "identity", "device-auth.json"); |
| 46 | + await fsp.mkdir(path.dirname(sourcePath), { recursive: true }); |
| 47 | + await fsp.writeFile( |
| 48 | + sourcePath, |
| 49 | + JSON.stringify({ |
| 50 | + version: 1, |
| 51 | + deviceId: "device-1", |
| 52 | + tokens: { |
| 53 | + operator: { |
| 54 | + token: "legacy-token", |
| 55 | + scopes: ["operator.write"], |
| 56 | + updatedAtMs: 10, |
| 57 | + }, |
| 58 | + }, |
| 59 | + }), |
| 60 | + ); |
| 61 | + return sourcePath; |
| 62 | + } |
| 63 | + |
| 64 | + async function writeDeviceIdentity(stateDir: string): Promise<{ |
| 65 | + deviceId: string; |
| 66 | + sourcePath: string; |
| 67 | + }> { |
| 68 | + const identity = generateStoredDeviceIdentity(1_700_000_000_000); |
| 69 | + const sourcePath = path.join(stateDir, "identity", "device.json"); |
| 70 | + await fsp.mkdir(path.dirname(sourcePath), { recursive: true }); |
| 71 | + await fsp.writeFile(sourcePath, JSON.stringify({ version: 1, ...identity })); |
| 72 | + return { deviceId: identity.deviceId, sourcePath }; |
| 73 | + } |
| 74 | + |
| 75 | + async function writeExecApprovals(env: NodeJS.ProcessEnv): Promise<string> { |
| 76 | + const sourcePath = resolveExecApprovalsPath(env); |
| 77 | + await fsp.writeFile( |
| 78 | + sourcePath, |
| 79 | + `${JSON.stringify({ |
| 80 | + version: 1, |
| 81 | + defaults: { security: "deny" }, |
| 82 | + agents: {}, |
| 83 | + })}\n`, |
| 84 | + ); |
| 85 | + return sourcePath; |
| 86 | + } |
| 87 | + |
| 88 | + it("migrates device auth before the runtime gate reads it", async () => { |
| 89 | + const { env, stateDir } = useStateDir(); |
| 90 | + const sourcePath = await writeDeviceAuth(stateDir); |
| 91 | + |
| 92 | + await runStartupMigrations({ env, log }); |
| 93 | + |
| 94 | + expect(fs.existsSync(sourcePath)).toBe(false); |
| 95 | + expect(loadDeviceAuthToken({ deviceId: "device-1", role: "operator", env })).toMatchObject({ |
| 96 | + token: "legacy-token", |
| 97 | + scopes: ["operator.read", "operator.write"], |
| 98 | + }); |
| 99 | + expect(log.info).toHaveBeenCalledWith("Migrated 1 device-auth token to SQLite."); |
| 100 | + expect(log.warn).not.toHaveBeenCalled(); |
| 101 | + }); |
| 102 | + |
| 103 | + it("migrates legacy exec approvals into the canonical store", async () => { |
| 104 | + const { env, stateDir } = useStateDir(); |
| 105 | + const sourcePath = await writeExecApprovals(env); |
| 106 | + setTestEnvValue("OPENCLAW_STATE_DIR", stateDir); |
| 107 | + execApprovalsStoreTesting.reset(); |
| 108 | + |
| 109 | + await runStartupMigrations({ env, log }); |
| 110 | + |
| 111 | + expect(fs.existsSync(sourcePath)).toBe(false); |
| 112 | + expect(loadExecApprovals().defaults?.security).toBe("deny"); |
| 113 | + expect(log.info).toHaveBeenCalledWith( |
| 114 | + "Imported legacy exec approvals into shared SQLite state.", |
| 115 | + ); |
| 116 | + expect(log.warn).not.toHaveBeenCalled(); |
| 117 | + }); |
| 118 | + |
| 119 | + it("migrates a legacy device identity", async () => { |
| 120 | + const { env, stateDir } = useStateDir(); |
| 121 | + const { deviceId, sourcePath } = await writeDeviceIdentity(stateDir); |
| 122 | + |
| 123 | + await runStartupMigrations({ env, log }); |
| 124 | + |
| 125 | + expect(fs.existsSync(sourcePath)).toBe(false); |
| 126 | + expect(loadDeviceIdentityIfPresent({ env })?.deviceId).toBe(deviceId); |
| 127 | + expect(log.info).toHaveBeenCalledWith("Migrated primary device identity to SQLite."); |
| 128 | + expect(log.warn).not.toHaveBeenCalled(); |
| 129 | + }); |
| 130 | + |
| 131 | + it("preserves a pending native device identity claim and continues", async () => { |
| 132 | + const { env, stateDir } = useStateDir(); |
| 133 | + const { sourcePath } = await writeDeviceIdentity(stateDir); |
| 134 | + const nativeClaimPath = `${sourcePath}.native-importing`; |
| 135 | + await fsp.rename(sourcePath, nativeClaimPath); |
| 136 | + |
| 137 | + await expect(runStartupMigrations({ env, log })).resolves.toBeUndefined(); |
| 138 | + |
| 139 | + expect(fs.existsSync(nativeClaimPath)).toBe(true); |
| 140 | + expect(log.info).not.toHaveBeenCalled(); |
| 141 | + expect(log.warn).toHaveBeenCalledWith( |
| 142 | + "Native device identity import is pending; restart the native app before running Doctor.", |
| 143 | + ); |
| 144 | + }); |
| 145 | + |
| 146 | + it("warns and leaves every legacy file untouched when the state lock is held", async () => { |
| 147 | + const { env, stateDir } = useStateDir(); |
| 148 | + const deviceAuthPath = await writeDeviceAuth(stateDir); |
| 149 | + const { sourcePath: deviceIdentityPath } = await writeDeviceIdentity(stateDir); |
| 150 | + const execApprovalsPath = await writeExecApprovals(env); |
| 151 | + const gatewayLock = await acquireGatewayLock({ |
| 152 | + allowInTests: true, |
| 153 | + env, |
| 154 | + pollIntervalMs: 10, |
| 155 | + port: 18_792, |
| 156 | + timeoutMs: 100, |
| 157 | + }); |
| 158 | + if (!gatewayLock) { |
| 159 | + throw new Error("expected test Gateway lock"); |
| 160 | + } |
| 161 | + |
| 162 | + try { |
| 163 | + await expect(runStartupMigrations({ env, log })).resolves.toBeUndefined(); |
| 164 | + } finally { |
| 165 | + await gatewayLock.release(); |
| 166 | + } |
| 167 | + |
| 168 | + expect(fs.existsSync(deviceAuthPath)).toBe(true); |
| 169 | + expect(fs.existsSync(deviceIdentityPath)).toBe(true); |
| 170 | + expect(fs.existsSync(execApprovalsPath)).toBe(true); |
| 171 | + expect(log.info).not.toHaveBeenCalled(); |
| 172 | + expect(log.warn).toHaveBeenCalledTimes(3); |
| 173 | + }); |
| 174 | + |
| 175 | + it("is silent when no retired state is present", async () => { |
| 176 | + const { env } = useStateDir(); |
| 177 | + |
| 178 | + await runStartupMigrations({ env, log }); |
| 179 | + |
| 180 | + expect(log.info).not.toHaveBeenCalled(); |
| 181 | + expect(log.warn).not.toHaveBeenCalled(); |
| 182 | + }); |
| 183 | +}); |
0 commit comments