diff --git a/src/commands/doctor-config-flow.test-utils.ts b/src/commands/doctor-config-flow.test-utils.ts index c70c9f9d9bb..2fd32da87da 100644 --- a/src/commands/doctor-config-flow.test-utils.ts +++ b/src/commands/doctor-config-flow.test-utils.ts @@ -1,6 +1,28 @@ -import fs from "node:fs/promises"; -import path from "node:path"; -import { withTempHome } from "../../test/helpers/temp-home.js"; +const DOCTOR_CONFIG_TEST_INPUT = Symbol.for("openclaw.doctorConfigFlow.testInput"); + +type DoctorConfigTestInput = { + config: Record; + exists: boolean; + path: string; +}; + +function setDoctorConfigInputForTest(input: DoctorConfigTestInput | null): void { + const globalState = globalThis as typeof globalThis & { + [DOCTOR_CONFIG_TEST_INPUT]?: DoctorConfigTestInput; + }; + if (input) { + globalState[DOCTOR_CONFIG_TEST_INPUT] = input; + return; + } + delete globalState[DOCTOR_CONFIG_TEST_INPUT]; +} + +export function getDoctorConfigInputForTest(): DoctorConfigTestInput | null { + const globalState = globalThis as typeof globalThis & { + [DOCTOR_CONFIG_TEST_INPUT]?: DoctorConfigTestInput; + }; + return globalState[DOCTOR_CONFIG_TEST_INPUT] ?? null; +} export async function runDoctorConfigWithInput(params: { config: Record; @@ -10,22 +32,17 @@ export async function runDoctorConfigWithInput(params: { confirm: () => Promise; }) => Promise; }) { - return withTempHome( - async (home) => { - const configDir = path.join(home, ".openclaw"); - await fs.mkdir(configDir, { recursive: true }); - await fs.writeFile( - path.join(configDir, "openclaw.json"), - JSON.stringify(params.config, null, 2), - "utf-8", - ); - return params.run({ - options: { nonInteractive: true, repair: params.repair }, - confirm: async () => false, - }); - }, - { - skipSessionCleanup: true, - }, - ); + setDoctorConfigInputForTest({ + config: structuredClone(params.config), + exists: true, + path: "/virtual/.openclaw/openclaw.json", + }); + try { + return await params.run({ + options: { nonInteractive: true, repair: params.repair }, + confirm: async () => false, + }); + } finally { + setDoctorConfigInputForTest(null); + } } diff --git a/src/commands/doctor-config-flow.test.ts b/src/commands/doctor-config-flow.test.ts index 4cddc38e18c..e7933975393 100644 --- a/src/commands/doctor-config-flow.test.ts +++ b/src/commands/doctor-config-flow.test.ts @@ -3,7 +3,10 @@ import path from "node:path"; import { beforeEach, describe, expect, it, vi } from "vitest"; import { withTempHome } from "../../test/helpers/temp-home.js"; import { loadAndMaybeMigrateDoctorConfig } from "./doctor-config-flow.js"; -import { runDoctorConfigWithInput } from "./doctor-config-flow.test-utils.js"; +import { + getDoctorConfigInputForTest, + runDoctorConfigWithInput, +} from "./doctor-config-flow.test-utils.js"; type TerminalNote = (message: string, title?: string) => void; @@ -534,14 +537,19 @@ vi.mock("./doctor-config-preflight.js", async () => { return { runDoctorConfigPreflight: vi.fn(async () => { - const configPath = resolveConfigPath(); - let parsed: Record = {}; - let exists = false; - try { - parsed = JSON.parse(await fs.readFile(configPath, "utf-8")) as Record; - exists = true; - } catch { - parsed = {}; + const injected = getDoctorConfigInputForTest(); + const configPath = injected?.path ?? resolveConfigPath(); + let parsed: Record = injected?.config + ? structuredClone(injected.config) + : {}; + let exists = injected?.exists ?? false; + if (!injected) { + try { + parsed = JSON.parse(await fs.readFile(configPath, "utf-8")) as Record; + exists = true; + } catch { + parsed = {}; + } } const legacyIssues = findLegacyConfigIssues( parsed,