From 7036352d946623bbd505112bc934b3a4a6ad7ae7 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sat, 21 Feb 2026 19:32:06 +0000 Subject: [PATCH] test(config): dedupe temp roots and cover legacy state-dir fallback --- src/config/paths.test.ts | 39 ++++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/src/config/paths.test.ts b/src/config/paths.test.ts index 9d2ed808407..b8afe7674cb 100644 --- a/src/config/paths.test.ts +++ b/src/config/paths.test.ts @@ -37,6 +37,15 @@ describe("oauth paths", () => { }); describe("state + config path candidates", () => { + async function withTempRoot(prefix: string, run: (root: string) => Promise): Promise { + const root = await fs.mkdtemp(path.join(os.tmpdir(), prefix)); + try { + await run(root); + } finally { + await fs.rm(root, { recursive: true, force: true }); + } + } + function expectOpenClawHomeDefaults(env: NodeJS.ProcessEnv): void { const configuredHome = env.OPENCLAW_HOME; if (!configuredHome) { @@ -98,20 +107,25 @@ describe("state + config path candidates", () => { }); it("prefers ~/.openclaw when it exists and legacy dir is missing", async () => { - const root = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-state-")); - try { + await withTempRoot("openclaw-state-", async (root) => { const newDir = path.join(root, ".openclaw"); await fs.mkdir(newDir, { recursive: true }); const resolved = resolveStateDir({} as NodeJS.ProcessEnv, () => root); expect(resolved).toBe(newDir); - } finally { - await fs.rm(root, { recursive: true, force: true }); - } + }); + }); + + it("falls back to existing legacy state dir when ~/.openclaw is missing", async () => { + await withTempRoot("openclaw-state-legacy-", async (root) => { + const legacyDir = path.join(root, ".clawdbot"); + await fs.mkdir(legacyDir, { recursive: true }); + const resolved = resolveStateDir({} as NodeJS.ProcessEnv, () => root); + expect(resolved).toBe(legacyDir); + }); }); it("CONFIG_PATH prefers existing config when present", async () => { - const root = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-config-")); - try { + await withTempRoot("openclaw-config-", async (root) => { const legacyDir = path.join(root, ".openclaw"); await fs.mkdir(legacyDir, { recursive: true }); const legacyPath = path.join(legacyDir, "openclaw.json"); @@ -119,14 +133,11 @@ describe("state + config path candidates", () => { const resolved = resolveConfigPathCandidate({} as NodeJS.ProcessEnv, () => root); expect(resolved).toBe(legacyPath); - } finally { - await fs.rm(root, { recursive: true, force: true }); - } + }); }); it("respects state dir overrides when config is missing", async () => { - const root = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-config-override-")); - try { + await withTempRoot("openclaw-config-override-", async (root) => { const legacyDir = path.join(root, ".openclaw"); await fs.mkdir(legacyDir, { recursive: true }); const legacyConfig = path.join(legacyDir, "openclaw.json"); @@ -136,8 +147,6 @@ describe("state + config path candidates", () => { const env = { OPENCLAW_STATE_DIR: overrideDir } as NodeJS.ProcessEnv; const resolved = resolveConfigPath(env, overrideDir, () => root); expect(resolved).toBe(path.join(overrideDir, "openclaw.json")); - } finally { - await fs.rm(root, { recursive: true, force: true }); - } + }); }); });