mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 08:10:44 +00:00
perf(test): bypass fs in doctor config flow tests
This commit is contained in:
@@ -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<string, unknown>;
|
||||
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<T>(params: {
|
||||
config: Record<string, unknown>;
|
||||
@@ -10,22 +32,17 @@ export async function runDoctorConfigWithInput<T>(params: {
|
||||
confirm: () => Promise<boolean>;
|
||||
}) => Promise<T>;
|
||||
}) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<string, unknown> = {};
|
||||
let exists = false;
|
||||
try {
|
||||
parsed = JSON.parse(await fs.readFile(configPath, "utf-8")) as Record<string, unknown>;
|
||||
exists = true;
|
||||
} catch {
|
||||
parsed = {};
|
||||
const injected = getDoctorConfigInputForTest();
|
||||
const configPath = injected?.path ?? resolveConfigPath();
|
||||
let parsed: Record<string, unknown> = injected?.config
|
||||
? structuredClone(injected.config)
|
||||
: {};
|
||||
let exists = injected?.exists ?? false;
|
||||
if (!injected) {
|
||||
try {
|
||||
parsed = JSON.parse(await fs.readFile(configPath, "utf-8")) as Record<string, unknown>;
|
||||
exists = true;
|
||||
} catch {
|
||||
parsed = {};
|
||||
}
|
||||
}
|
||||
const legacyIssues = findLegacyConfigIssues(
|
||||
parsed,
|
||||
|
||||
Reference in New Issue
Block a user