fix: preserve watcher test state

This commit is contained in:
Shakker
2026-06-26 13:05:44 +01:00
parent e4f63577d0
commit 338e119533
2 changed files with 30 additions and 4 deletions
@@ -16,6 +16,19 @@ const createEmbeddingProviderMock = vi.hoisted(() =>
providerUnavailableReason: "No embeddings provider available.",
})),
);
const originalSelfHealStateDir = process.env.OPENCLAW_STATE_DIR;
function setSelfHealStateDir(stateDir: string): void {
Reflect.set(process.env, "OPENCLAW_STATE_DIR", stateDir);
}
function restoreSelfHealStateDir(): void {
if (originalSelfHealStateDir === undefined) {
Reflect.deleteProperty(process.env, "OPENCLAW_STATE_DIR");
} else {
Reflect.set(process.env, "OPENCLAW_STATE_DIR", originalSelfHealStateDir);
}
}
vi.mock("./embeddings.js", () => ({
createEmbeddingProvider: createEmbeddingProviderMock,
@@ -49,7 +62,7 @@ describe("memory manager self-heal missing identity with FTS-only chunks", () =>
workspaceDir = path.join(fixtureRoot, `case-${caseId++}`);
await fs.mkdir(path.join(workspaceDir, "memory"), { recursive: true });
await fs.writeFile(path.join(workspaceDir, "MEMORY.md"), "Alpha topic\n\nKeep this note.");
vi.stubEnv("OPENCLAW_STATE_DIR", path.join(workspaceDir, "state"));
setSelfHealStateDir(path.join(workspaceDir, "state"));
indexPath = resolveOpenClawAgentSqlitePath({ agentId: "main" });
});
@@ -59,7 +72,7 @@ describe("memory manager self-heal missing identity with FTS-only chunks", () =>
manager = null;
}
await closeAllMemorySearchManagers();
vi.unstubAllEnvs();
restoreSelfHealStateDir();
});
afterAll(async () => {
@@ -121,6 +121,19 @@ const {
const CHOKIDAR_FACTORY_KEY = Symbol.for("openclaw.test.memoryWatchFactory");
const NATIVE_FACTORY_KEY = Symbol.for("openclaw.test.memoryNativeWatchFactory");
const originalWatcherStateDir = process.env.OPENCLAW_STATE_DIR;
function setWatcherStateDir(stateDir: string): void {
Reflect.set(process.env, "OPENCLAW_STATE_DIR", stateDir);
}
function restoreWatcherStateDir(): void {
if (originalWatcherStateDir === undefined) {
Reflect.deleteProperty(process.env, "OPENCLAW_STATE_DIR");
} else {
Reflect.set(process.env, "OPENCLAW_STATE_DIR", originalWatcherStateDir);
}
}
vi.mock("openclaw/plugin-sdk/memory-core-host-engine-foundation", async (importOriginal) => {
const actual =
@@ -194,7 +207,7 @@ describe("memory watcher config", () => {
}
await closeAllMemorySearchManagers();
clearRegistry();
vi.unstubAllEnvs();
restoreWatcherStateDir();
if (workspaceDir) {
await fs.rm(workspaceDir, { recursive: true, force: true });
workspaceDir = "";
@@ -204,7 +217,7 @@ describe("memory watcher config", () => {
async function setupWatcherWorkspace(seedFile: { name: string; contents: string }) {
workspaceDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-memory-watch-"));
vi.stubEnv("OPENCLAW_STATE_DIR", path.join(workspaceDir, "state"));
setWatcherStateDir(path.join(workspaceDir, "state"));
extraDir = path.join(workspaceDir, "extra");
await fs.mkdir(path.join(workspaceDir, "memory"), { recursive: true });
await fs.mkdir(extraDir, { recursive: true });