From d8da642611093b6df7f04f9e0f0075d1c60be9c8 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sat, 14 Feb 2026 22:41:30 +0000 Subject: [PATCH] refactor(test): share temp home env harness --- src/config/config.identity-defaults.test.ts | 86 +++------------------ src/config/home-env.test-harness.ts | 64 +++++++++++++++ src/config/io.write-config.test.ts | 85 +++----------------- 3 files changed, 82 insertions(+), 153 deletions(-) create mode 100644 src/config/home-env.test-harness.ts diff --git a/src/config/config.identity-defaults.test.ts b/src/config/config.identity-defaults.test.ts index 48a6710a44a..a2d67e3f5e8 100644 --- a/src/config/config.identity-defaults.test.ts +++ b/src/config/config.identity-defaults.test.ts @@ -1,55 +1,11 @@ import fs from "node:fs/promises"; -import os from "node:os"; import path from "node:path"; -import { afterAll, beforeAll, describe, expect, it } from "vitest"; +import { describe, expect, it } from "vitest"; import { DEFAULT_AGENT_MAX_CONCURRENT, DEFAULT_SUBAGENT_MAX_CONCURRENT } from "./agent-limits.js"; import { loadConfig } from "./config.js"; - -type HomeEnvSnapshot = { - home: string | undefined; - userProfile: string | undefined; - homeDrive: string | undefined; - homePath: string | undefined; - stateDir: string | undefined; -}; - -function snapshotHomeEnv(): HomeEnvSnapshot { - return { - home: process.env.HOME, - userProfile: process.env.USERPROFILE, - homeDrive: process.env.HOMEDRIVE, - homePath: process.env.HOMEPATH, - stateDir: process.env.OPENCLAW_STATE_DIR, - }; -} - -function restoreHomeEnv(snapshot: HomeEnvSnapshot) { - const restoreKey = (key: string, value: string | undefined) => { - if (value === undefined) { - delete process.env[key]; - } else { - process.env[key] = value; - } - }; - restoreKey("HOME", snapshot.home); - restoreKey("USERPROFILE", snapshot.userProfile); - restoreKey("HOMEDRIVE", snapshot.homeDrive); - restoreKey("HOMEPATH", snapshot.homePath); - restoreKey("OPENCLAW_STATE_DIR", snapshot.stateDir); -} +import { withTempHome } from "./home-env.test-harness.js"; describe("config identity defaults", () => { - let fixtureRoot = ""; - let fixtureCount = 0; - - beforeAll(async () => { - fixtureRoot = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-config-identity-")); - }); - - afterAll(async () => { - await fs.rm(fixtureRoot, { recursive: true, force: true }); - }); - const writeAndLoadConfig = async (home: string, config: Record) => { const configDir = path.join(home, ".openclaw"); await fs.mkdir(configDir, { recursive: true }); @@ -61,32 +17,8 @@ describe("config identity defaults", () => { return loadConfig(); }; - const withTempHome = async (fn: (home: string) => Promise): Promise => { - const home = path.join(fixtureRoot, `home-${fixtureCount++}`); - await fs.mkdir(path.join(home, ".openclaw"), { recursive: true }); - - const snapshot = snapshotHomeEnv(); - process.env.HOME = home; - process.env.USERPROFILE = home; - process.env.OPENCLAW_STATE_DIR = path.join(home, ".openclaw"); - - if (process.platform === "win32") { - const match = home.match(/^([A-Za-z]:)(.*)$/); - if (match) { - process.env.HOMEDRIVE = match[1]; - process.env.HOMEPATH = match[2] || "\\"; - } - } - - try { - return await fn(home); - } finally { - restoreHomeEnv(snapshot); - } - }; - it("does not derive mention defaults and only sets ackReactionScope when identity is present", async () => { - await withTempHome(async (home) => { + await withTempHome("openclaw-config-identity-", async (home) => { const cfg = await writeAndLoadConfig(home, { agents: { list: [ @@ -111,7 +43,7 @@ describe("config identity defaults", () => { }); it("keeps ackReaction unset and does not synthesize agent/session defaults when identity is missing", async () => { - await withTempHome(async (home) => { + await withTempHome("openclaw-config-identity-", async (home) => { const cfg = await writeAndLoadConfig(home, { messages: {} }); expect(cfg.messages?.ackReaction).toBeUndefined(); @@ -126,7 +58,7 @@ describe("config identity defaults", () => { }); it("does not override explicit values", async () => { - await withTempHome(async (home) => { + await withTempHome("openclaw-config-identity-", async (home) => { const cfg = await writeAndLoadConfig(home, { agents: { list: [ @@ -152,7 +84,7 @@ describe("config identity defaults", () => { }); it("supports provider textChunkLimit config", async () => { - await withTempHome(async (home) => { + await withTempHome("openclaw-config-identity-", async (home) => { const cfg = await writeAndLoadConfig(home, { messages: { messagePrefix: "[openclaw]", @@ -184,7 +116,7 @@ describe("config identity defaults", () => { }); it("accepts blank model provider apiKey values", async () => { - await withTempHome(async (home) => { + await withTempHome("openclaw-config-identity-", async (home) => { const cfg = await writeAndLoadConfig(home, { models: { mode: "merge", @@ -219,7 +151,7 @@ describe("config identity defaults", () => { }); it("respects empty responsePrefix to disable identity defaults", async () => { - await withTempHome(async (home) => { + await withTempHome("openclaw-config-identity-", async (home) => { const cfg = await writeAndLoadConfig(home, { agents: { list: [ @@ -241,7 +173,7 @@ describe("config identity defaults", () => { }); it("does not derive responsePrefix from identity emoji", async () => { - await withTempHome(async (home) => { + await withTempHome("openclaw-config-identity-", async (home) => { const cfg = await writeAndLoadConfig(home, { agents: { list: [ diff --git a/src/config/home-env.test-harness.ts b/src/config/home-env.test-harness.ts new file mode 100644 index 00000000000..02808461b0f --- /dev/null +++ b/src/config/home-env.test-harness.ts @@ -0,0 +1,64 @@ +import fs from "node:fs/promises"; +import os from "node:os"; +import path from "node:path"; + +type HomeEnvSnapshot = { + home: string | undefined; + userProfile: string | undefined; + homeDrive: string | undefined; + homePath: string | undefined; + stateDir: string | undefined; +}; + +function snapshotHomeEnv(): HomeEnvSnapshot { + return { + home: process.env.HOME, + userProfile: process.env.USERPROFILE, + homeDrive: process.env.HOMEDRIVE, + homePath: process.env.HOMEPATH, + stateDir: process.env.OPENCLAW_STATE_DIR, + }; +} + +function restoreHomeEnv(snapshot: HomeEnvSnapshot) { + const restoreKey = (key: string, value: string | undefined) => { + if (value === undefined) { + delete process.env[key]; + } else { + process.env[key] = value; + } + }; + restoreKey("HOME", snapshot.home); + restoreKey("USERPROFILE", snapshot.userProfile); + restoreKey("HOMEDRIVE", snapshot.homeDrive); + restoreKey("HOMEPATH", snapshot.homePath); + restoreKey("OPENCLAW_STATE_DIR", snapshot.stateDir); +} + +export async function withTempHome( + prefix: string, + fn: (home: string) => Promise, +): Promise { + const home = await fs.mkdtemp(path.join(os.tmpdir(), prefix)); + await fs.mkdir(path.join(home, ".openclaw"), { recursive: true }); + + const snapshot = snapshotHomeEnv(); + process.env.HOME = home; + process.env.USERPROFILE = home; + process.env.OPENCLAW_STATE_DIR = path.join(home, ".openclaw"); + + if (process.platform === "win32") { + const match = home.match(/^([A-Za-z]:)(.*)$/); + if (match) { + process.env.HOMEDRIVE = match[1]; + process.env.HOMEPATH = match[2] || "\\"; + } + } + + try { + return await fn(home); + } finally { + restoreHomeEnv(snapshot); + await fs.rm(home, { recursive: true, force: true }); + } +} diff --git a/src/config/io.write-config.test.ts b/src/config/io.write-config.test.ts index 59af3e99383..216d08eafb8 100644 --- a/src/config/io.write-config.test.ts +++ b/src/config/io.write-config.test.ts @@ -1,84 +1,17 @@ import fs from "node:fs/promises"; -import os from "node:os"; import path from "node:path"; -import { afterAll, beforeAll, describe, expect, it, vi } from "vitest"; +import { describe, expect, it, vi } from "vitest"; +import { withTempHome } from "./home-env.test-harness.js"; import { createConfigIO } from "./io.js"; -type HomeEnvSnapshot = { - home: string | undefined; - userProfile: string | undefined; - homeDrive: string | undefined; - homePath: string | undefined; - stateDir: string | undefined; -}; - -function snapshotHomeEnv(): HomeEnvSnapshot { - return { - home: process.env.HOME, - userProfile: process.env.USERPROFILE, - homeDrive: process.env.HOMEDRIVE, - homePath: process.env.HOMEPATH, - stateDir: process.env.OPENCLAW_STATE_DIR, - }; -} - -function restoreHomeEnv(snapshot: HomeEnvSnapshot) { - const restoreKey = (key: string, value: string | undefined) => { - if (value === undefined) { - delete process.env[key]; - } else { - process.env[key] = value; - } - }; - restoreKey("HOME", snapshot.home); - restoreKey("USERPROFILE", snapshot.userProfile); - restoreKey("HOMEDRIVE", snapshot.homeDrive); - restoreKey("HOMEPATH", snapshot.homePath); - restoreKey("OPENCLAW_STATE_DIR", snapshot.stateDir); -} - describe("config io write", () => { - let fixtureRoot = ""; - let fixtureCount = 0; const silentLogger = { warn: () => {}, error: () => {}, }; - beforeAll(async () => { - fixtureRoot = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-config-io-")); - }); - - afterAll(async () => { - await fs.rm(fixtureRoot, { recursive: true, force: true }); - }); - - const withTempHome = async (fn: (home: string) => Promise): Promise => { - const home = path.join(fixtureRoot, `home-${fixtureCount++}`); - await fs.mkdir(path.join(home, ".openclaw"), { recursive: true }); - - const snapshot = snapshotHomeEnv(); - process.env.HOME = home; - process.env.USERPROFILE = home; - process.env.OPENCLAW_STATE_DIR = path.join(home, ".openclaw"); - - if (process.platform === "win32") { - const match = home.match(/^([A-Za-z]:)(.*)$/); - if (match) { - process.env.HOMEDRIVE = match[1]; - process.env.HOMEPATH = match[2] || "\\"; - } - } - - try { - return await fn(home); - } finally { - restoreHomeEnv(snapshot); - } - }; - it("persists caller changes onto resolved config without leaking runtime defaults", async () => { - await withTempHome(async (home) => { + await withTempHome("openclaw-config-io-", async (home) => { const configPath = path.join(home, ".openclaw", "openclaw.json"); await fs.mkdir(path.dirname(configPath), { recursive: true }); await fs.writeFile( @@ -119,7 +52,7 @@ describe("config io write", () => { }); it("preserves env var references when writing", async () => { - await withTempHome(async (home) => { + await withTempHome("openclaw-config-io-", async (home) => { const configPath = path.join(home, ".openclaw", "openclaw.json"); await fs.mkdir(path.dirname(configPath), { recursive: true }); await fs.writeFile( @@ -178,7 +111,7 @@ describe("config io write", () => { }); it("keeps env refs in arrays when appending entries", async () => { - await withTempHome(async (home) => { + await withTempHome("openclaw-config-io-", async (home) => { const configPath = path.join(home, ".openclaw", "openclaw.json"); await fs.mkdir(path.dirname(configPath), { recursive: true }); await fs.writeFile( @@ -251,7 +184,7 @@ describe("config io write", () => { }); it("logs an overwrite audit entry when replacing an existing config file", async () => { - await withTempHome(async (home) => { + await withTempHome("openclaw-config-io-", async (home) => { const configPath = path.join(home, ".openclaw", "openclaw.json"); await fs.mkdir(path.dirname(configPath), { recursive: true }); await fs.writeFile( @@ -290,7 +223,7 @@ describe("config io write", () => { }); it("does not log an overwrite audit entry when creating config for the first time", async () => { - await withTempHome(async (home) => { + await withTempHome("openclaw-config-io-", async (home) => { const warn = vi.fn(); const io = createConfigIO({ env: {} as NodeJS.ProcessEnv, @@ -313,7 +246,7 @@ describe("config io write", () => { }); it("appends config write audit JSONL entries with forensic metadata", async () => { - await withTempHome(async (home) => { + await withTempHome("openclaw-config-io-", async (home) => { const configPath = path.join(home, ".openclaw", "openclaw.json"); const auditPath = path.join(home, ".openclaw", "logs", "config-audit.jsonl"); await fs.mkdir(path.dirname(configPath), { recursive: true }); @@ -358,7 +291,7 @@ describe("config io write", () => { }); it("records gateway watch session markers in config audit entries", async () => { - await withTempHome(async (home) => { + await withTempHome("openclaw-config-io-", async (home) => { const configPath = path.join(home, ".openclaw", "openclaw.json"); const auditPath = path.join(home, ".openclaw", "logs", "config-audit.jsonl"); await fs.mkdir(path.dirname(configPath), { recursive: true });