test: scope subagent sqlite state env

This commit is contained in:
Shakker
2026-06-05 16:48:32 +01:00
parent 1e683ff245
commit c85b0ee3db

View File

@@ -8,7 +8,7 @@ import {
closeOpenClawStateDatabaseForTest,
openOpenClawStateDatabase,
} from "../state/openclaw-state-db.js";
import { captureEnv } from "../test-utils/env.js";
import { withEnvAsync } from "../test-utils/env.js";
import {
loadSubagentRegistryFromSqlite,
saveSubagentRegistryToSqlite,
@@ -56,12 +56,10 @@ function createRun(overrides: Partial<SubagentRunRecord> = {}): SubagentRunRecor
}
describe("subagent registry sqlite store", () => {
const envSnapshot = captureEnv(["OPENCLAW_STATE_DIR"]);
let tempStateDir: string | null = null;
beforeEach(async () => {
tempStateDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-subagent-sqlite-"));
process.env.OPENCLAW_STATE_DIR = tempStateDir;
});
afterEach(async () => {
@@ -70,10 +68,17 @@ describe("subagent registry sqlite store", () => {
await fs.rm(tempStateDir, { recursive: true, force: true, maxRetries: 5, retryDelay: 50 });
tempStateDir = null;
}
envSnapshot.restore();
});
async function withTempStateEnv<T>(fn: () => Promise<T>): Promise<T> {
if (!tempStateDir) {
throw new Error("expected temp state dir");
}
return await withEnvAsync({ OPENCLAW_STATE_DIR: tempStateDir }, fn);
}
it("persists subagent runs in the shared sqlite state database", async () => {
await withTempStateEnv(async () => {
const run = createRun();
saveSubagentRegistryToSqlite(new Map([[run.runId, run]]));
@@ -92,8 +97,10 @@ describe("subagent registry sqlite store", () => {
expect(await fs.stat(path.join(tempStateDir!, "state", "openclaw.sqlite"))).toBeTruthy();
await expect(fs.stat(path.join(tempStateDir!, "subagents", "runs.json"))).rejects.toThrow();
});
});
it("uses save calls as whole-registry snapshots", () => {
it("uses save calls as whole-registry snapshots", async () => {
await withTempStateEnv(async () => {
const first = createRun({ runId: "run-one", childSessionKey: "agent:main:subagent:one" });
const second = createRun({ runId: "run-two", childSessionKey: "agent:main:subagent:two" });
@@ -107,8 +114,10 @@ describe("subagent registry sqlite store", () => {
expect([...loadSubagentRegistryFromSqlite().keys()]).toEqual(["run-two"]);
});
});
it("imports the legacy json registry when sqlite has no runs", async () => {
await withTempStateEnv(async () => {
// Import deletes the JSON source after the first successful migration so
// later loads treat SQLite as canonical state.
const legacyRun = createRun({
@@ -136,3 +145,4 @@ describe("subagent registry sqlite store", () => {
).toEqual({ count: 1 });
});
});
});