mirror of
https://github.com/openclaw/openclaw.git
synced 2026-06-06 05:51:15 +08:00
test: scope subagent sqlite state env
This commit is contained in:
@@ -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({
|
||||
@@ -135,4 +144,5 @@ describe("subagent registry sqlite store", () => {
|
||||
openOpenClawStateDatabase().db.prepare("SELECT COUNT(*) AS count FROM subagent_runs").get(),
|
||||
).toEqual({ count: 1 });
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user