fix(test): isolate shared vitest home setup

This commit is contained in:
Peter Steinberger
2026-04-07 06:32:26 +01:00
parent 2aabe0e8fd
commit e2f0ea4625
3 changed files with 50 additions and 2 deletions

View File

@@ -0,0 +1,13 @@
import path from "node:path";
import { describe, expect, it } from "vitest";
import { createConfigIO } from "../src/config/config.js";
describe("shared test setup home isolation", () => {
it("routes default config IO through the per-worker temp home", () => {
const testHome = process.env.OPENCLAW_TEST_HOME;
expect(testHome).toBeTruthy();
expect(process.env.HOME).toBe(testHome);
expect(process.env.USERPROFILE).toBe(testHome);
expect(createConfigIO().configPath).toBe(path.join(testHome!, ".openclaw", "openclaw.json"));
});
});

View File

@@ -51,13 +51,41 @@ type SharedTestSetupOptions = {
loadProfileEnv?: boolean;
};
const SHARED_TEST_SETUP = Symbol.for("openclaw.sharedTestSetup");
type SharedTestSetupHandle = {
cleanup: () => void;
tempHome: string;
};
export function installSharedTestSetup(options?: SharedTestSetupOptions): {
cleanup: () => void;
tempHome: string;
} {
const globalState = globalThis as typeof globalThis & {
[SHARED_TEST_SETUP]?: SharedTestSetupHandle;
};
const existing = globalState[SHARED_TEST_SETUP];
if (existing) {
return existing;
}
const testEnv = withIsolatedTestHome({
loadProfileEnv: options?.loadProfileEnv,
});
installProcessWarningFilter();
return testEnv;
let cleaned = false;
const handle: SharedTestSetupHandle = {
tempHome: testEnv.tempHome,
cleanup: () => {
if (cleaned) {
return;
}
cleaned = true;
testEnv.cleanup();
},
};
globalState[SHARED_TEST_SETUP] = handle;
return handle;
}

View File

@@ -1 +1,8 @@
import "./setup.shared.js";
import { afterAll } from "vitest";
import { installSharedTestSetup } from "./setup.shared.js";
const testEnv = installSharedTestSetup();
afterAll(() => {
testEnv.cleanup();
});