test: rename state cleanup helper

This commit is contained in:
Peter Steinberger
2026-05-09 21:23:10 +01:00
parent ab33190f6f
commit c0d27a96ff
12 changed files with 30 additions and 30 deletions

View File

@@ -10,7 +10,7 @@ export function withModelsTempHome<T>(fn: (home: string) => Promise<T>): Promise
// unrelated session database state during temp-home teardown.
return withTempHomeBase(fn, {
prefix: "openclaw-models-",
skipSessionCleanup: true,
skipStateCleanup: true,
});
}

View File

@@ -7,7 +7,7 @@ export async function withSandboxMediaTempHome<T>(
prefix: string,
fn: (home: string) => Promise<T>,
): Promise<T> {
return withTempHomeBase(async (home) => await fn(home), { prefix, skipSessionCleanup: true });
return withTempHomeBase(async (home) => await fn(home), { prefix, skipStateCleanup: true });
}
export function createSandboxMediaContexts(mediaPath: string): {

View File

@@ -17,7 +17,7 @@ async function withTempHome<T>(fn: (home: string) => Promise<T>): Promise<T> {
},
{
prefix: "openclaw-agent-session-",
skipSessionCleanup: true,
skipStateCleanup: true,
},
);
}

View File

@@ -257,7 +257,7 @@ async function withTempHome<T>(fn: (home: string) => Promise<T>): Promise<T> {
{
prefix: "openclaw-agent-",
skipHomeCleanup: true,
skipSessionCleanup: true,
skipStateCleanup: true,
},
);
}

View File

@@ -1848,7 +1848,7 @@ describe("doctor config flow", () => {
blockStreaming: true,
});
},
{ skipSessionCleanup: true },
{ skipStateCleanup: true },
);
});
@@ -2197,7 +2197,7 @@ describe("doctor config flow", () => {
"1212",
]);
},
{ skipSessionCleanup: true },
{ skipStateCleanup: true },
);
});
@@ -2307,7 +2307,7 @@ describe("doctor config flow", () => {
confirm: async () => false,
});
},
{ skipSessionCleanup: true },
{ skipStateCleanup: true },
);
const cfg = result.cfg as {
@@ -2657,7 +2657,7 @@ describe("doctor config flow", () => {
noteSpy.mockClear();
}
},
{ skipSessionCleanup: true },
{ skipStateCleanup: true },
);
});

View File

@@ -67,7 +67,7 @@ async function withMcpConfigHome<T>(
},
{
prefix: "openclaw-mcp-config-",
skipSessionCleanup: true,
skipStateCleanup: true,
env: {
OPENCLAW_CONFIG_PATH: undefined,
OPENCLAW_BUNDLED_PLUGINS_DIR: undefined,

View File

@@ -1,7 +1,7 @@
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { cleanupSessionStateForTest } from "../../test-utils/session-state-cleanup.js";
import { cleanupOpenClawStateForTest } from "../../test-utils/openclaw-state-cleanup.js";
type EnvValue = string | undefined | ((home: string) => string | undefined);
@@ -105,7 +105,7 @@ export async function withTempHome<T>(
env?: Record<string, EnvValue>;
prefix?: string;
skipHomeCleanup?: boolean;
skipSessionCleanup?: boolean;
skipStateCleanup?: boolean;
} = {},
): Promise<T> {
const prefix = opts.prefix ?? "openclaw-test-home-";
@@ -135,8 +135,8 @@ export async function withTempHome<T>(
try {
return await fn(base);
} finally {
if (!opts.skipSessionCleanup) {
await cleanupSessionStateForTest().catch(() => undefined);
if (!opts.skipStateCleanup) {
await cleanupOpenClawStateForTest().catch(() => undefined);
}
restoreExtraEnv(envSnapshot);
restoreEnv(snapshot);

View File

@@ -2,7 +2,7 @@ import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { captureEnv } from "../test-utils/env.js";
import { cleanupSessionStateForTest } from "../test-utils/session-state-cleanup.js";
import { cleanupOpenClawStateForTest } from "../test-utils/openclaw-state-cleanup.js";
export function snapshotStateDirEnv() {
return captureEnv(["OPENCLAW_STATE_DIR"]);
@@ -28,7 +28,7 @@ export async function withStateDirEnv<T>(
try {
return await fn({ tempRoot, stateDir });
} finally {
await cleanupSessionStateForTest().catch(() => undefined);
await cleanupOpenClawStateForTest().catch(() => undefined);
restoreStateDirEnv(snapshot);
await fs.rm(tempRoot, { recursive: true, force: true });
}

View File

@@ -1,19 +1,19 @@
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { resetFileLockStateForTest } from "../infra/file-lock.js";
import {
cleanupSessionStateForTest,
resetSessionStateCleanupRuntimeForTests,
setSessionStateCleanupRuntimeForTests,
} from "./session-state-cleanup.js";
cleanupOpenClawStateForTest,
resetOpenClawStateCleanupRuntimeForTests,
setOpenClawStateCleanupRuntimeForTests,
} from "./openclaw-state-cleanup.js";
const drainFileLockStateMock = vi.hoisted(() => vi.fn(async () => undefined));
describe("cleanupSessionStateForTest", () => {
describe("cleanupOpenClawStateForTest", () => {
beforeEach(() => {
vi.useRealTimers();
resetFileLockStateForTest();
drainFileLockStateMock.mockClear();
setSessionStateCleanupRuntimeForTests({
setOpenClawStateCleanupRuntimeForTests({
drainFileLockStateForTest: drainFileLockStateMock,
});
});
@@ -21,12 +21,12 @@ describe("cleanupSessionStateForTest", () => {
afterEach(() => {
vi.useRealTimers();
resetFileLockStateForTest();
resetSessionStateCleanupRuntimeForTests();
resetOpenClawStateCleanupRuntimeForTests();
vi.restoreAllMocks();
});
it("cleans file locks and closes SQLite state", async () => {
await cleanupSessionStateForTest();
await cleanupOpenClawStateForTest();
expect(drainFileLockStateMock).toHaveBeenCalledTimes(1);
});

View File

@@ -4,7 +4,7 @@ import { closeOpenClawStateDatabaseForTest } from "../state/openclaw-state-db.js
let fileLockDrainerForTests: typeof drainFileLockStateForTest | null = null;
export function setSessionStateCleanupRuntimeForTests(params: {
export function setOpenClawStateCleanupRuntimeForTests(params: {
drainFileLockStateForTest?: typeof drainFileLockStateForTest | null;
}): void {
if ("drainFileLockStateForTest" in params) {
@@ -12,11 +12,11 @@ export function setSessionStateCleanupRuntimeForTests(params: {
}
}
export function resetSessionStateCleanupRuntimeForTests(): void {
export function resetOpenClawStateCleanupRuntimeForTests(): void {
fileLockDrainerForTests = null;
}
export async function cleanupSessionStateForTest(): Promise<void> {
export async function cleanupOpenClawStateForTest(): Promise<void> {
await (fileLockDrainerForTests ?? drainFileLockStateForTest)();
closeOpenClawAgentDatabasesForTest();
closeOpenClawStateDatabaseForTest();

View File

@@ -2,7 +2,7 @@ import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { captureEnv } from "./env.js";
import { cleanupSessionStateForTest } from "./session-state-cleanup.js";
import { cleanupOpenClawStateForTest } from "./openclaw-state-cleanup.js";
type OpenClawTestStateLayout = "home" | "state-only" | "split";
@@ -321,7 +321,7 @@ export async function createOpenClawTestState(
return;
}
cleaned = true;
await cleanupSessionStateForTest().catch(() => undefined);
await cleanupOpenClawStateForTest().catch(() => undefined);
state.restoreEnv();
await fs.rm(root, { recursive: true, force: true });
},

View File

@@ -2,7 +2,7 @@ import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { captureEnv } from "./env.js";
import { cleanupSessionStateForTest } from "./session-state-cleanup.js";
import { cleanupOpenClawStateForTest } from "./openclaw-state-cleanup.js";
const HOME_ENV_KEYS = [
"HOME",
@@ -64,7 +64,7 @@ export async function createTempHomeEnv(prefix: string): Promise<TempHomeEnv> {
return {
home,
restore: async () => {
await cleanupSessionStateForTest().catch(() => undefined);
await cleanupOpenClawStateForTest().catch(() => undefined);
snapshot.restore();
await fs.rm(home, { recursive: true, force: true });
},