mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-18 19:27:26 +00:00
test(perf): avoid module reload churn in config guard tests
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
const loadAndMaybeMigrateDoctorConfigMock = vi.hoisted(() => vi.fn());
|
||||
const readConfigFileSnapshotMock = vi.hoisted(() => vi.fn());
|
||||
@@ -43,14 +43,15 @@ async function withCapturedStdout(run: () => Promise<void>): Promise<string> {
|
||||
}
|
||||
|
||||
describe("ensureConfigReady", () => {
|
||||
async function loadEnsureConfigReady() {
|
||||
vi.resetModules();
|
||||
return await import("./config-guard.js");
|
||||
}
|
||||
let ensureConfigReady: (params: {
|
||||
runtime: unknown;
|
||||
commandPath: string[];
|
||||
suppressDoctorStdout?: boolean;
|
||||
}) => Promise<void>;
|
||||
let resetConfigGuardStateForTests: () => void;
|
||||
|
||||
async function runEnsureConfigReady(commandPath: string[], suppressDoctorStdout = false) {
|
||||
const runtime = makeRuntime();
|
||||
const { ensureConfigReady } = await loadEnsureConfigReady();
|
||||
await ensureConfigReady({ runtime: runtime as never, commandPath, suppressDoctorStdout });
|
||||
return runtime;
|
||||
}
|
||||
@@ -65,8 +66,16 @@ describe("ensureConfigReady", () => {
|
||||
});
|
||||
}
|
||||
|
||||
beforeAll(async () => {
|
||||
({
|
||||
ensureConfigReady,
|
||||
__test__: { resetConfigGuardStateForTests },
|
||||
} = await import("./config-guard.js"));
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
resetConfigGuardStateForTests();
|
||||
readConfigFileSnapshotMock.mockResolvedValue(makeSnapshot());
|
||||
});
|
||||
|
||||
@@ -107,7 +116,6 @@ describe("ensureConfigReady", () => {
|
||||
it("runs doctor migration flow only once per module instance", async () => {
|
||||
const runtimeA = makeRuntime();
|
||||
const runtimeB = makeRuntime();
|
||||
const { ensureConfigReady } = await loadEnsureConfigReady();
|
||||
|
||||
await ensureConfigReady({ runtime: runtimeA as never, commandPath: ["message"] });
|
||||
await ensureConfigReady({ runtime: runtimeB as never, commandPath: ["message"] });
|
||||
|
||||
@@ -23,6 +23,11 @@ let didRunDoctorConfigFlow = false;
|
||||
let configSnapshotPromise: Promise<Awaited<ReturnType<typeof readConfigFileSnapshot>>> | null =
|
||||
null;
|
||||
|
||||
function resetConfigGuardStateForTests() {
|
||||
didRunDoctorConfigFlow = false;
|
||||
configSnapshotPromise = null;
|
||||
}
|
||||
|
||||
function formatConfigIssues(issues: Array<{ path: string; message: string }>): string[] {
|
||||
return issues.map((issue) => `- ${issue.path || "<root>"}: ${issue.message}`);
|
||||
}
|
||||
@@ -113,3 +118,7 @@ export async function ensureConfigReady(params: {
|
||||
params.runtime.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
export const __test__ = {
|
||||
resetConfigGuardStateForTests,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user