




























11// OpenClaw agent database tests cover agent-scoped DB storage and migrations.
2+import { execFileSync } from "node:child_process";
23import fs from "node:fs";
34import os from "node:os";
45import path from "node:path";
@@ -154,6 +155,82 @@ describe("openclaw agent database", () => {
154155).toThrow(/run openclaw doctor --fix/);
155156});
156157158+it("keys explicit relative paths by resolved database pathname", () => {
159+const agentModuleUrl = new URL("./openclaw-agent-db.ts", import.meta.url).href;
160+const stateModuleUrl = new URL("./openclaw-state-db.ts", import.meta.url).href;
161+const output = execFileSync(
162+process.execPath,
163+[
164+"--import",
165+"tsx",
166+"--input-type=module",
167+"-e",
168+`
169+ import fs from "node:fs";
170+ import os from "node:os";
171+ import path from "node:path";
172+ import {
173+ closeOpenClawAgentDatabasesForTest,
174+ listOpenClawRegisteredAgentDatabases,
175+ openOpenClawAgentDatabase,
176+ } from ${JSON.stringify(agentModuleUrl)};
177+ import { closeOpenClawStateDatabaseForTest } from ${JSON.stringify(stateModuleUrl)};
178+179+ const stateDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-agent-db-state-"));
180+ const env = { OPENCLAW_STATE_DIR: stateDir };
181+ const root = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-agent-db-relative-"));
182+ const firstDir = path.join(root, "first");
183+ const secondDir = path.join(root, "second");
184+ fs.mkdirSync(firstDir);
185+ fs.mkdirSync(secondDir);
186+ const previousCwd = process.cwd();
187+ try {
188+ process.chdir(firstDir);
189+ const first = openOpenClawAgentDatabase({
190+ agentId: "worker-1",
191+ env,
192+ path: "agent.sqlite",
193+ });
194+195+ process.chdir(secondDir);
196+ const second = openOpenClawAgentDatabase({
197+ agentId: "worker-1",
198+ env,
199+ path: "agent.sqlite",
200+ });
201+202+ console.log(JSON.stringify({
203+ sameHandle: first === second,
204+ firstFileExists: fs.existsSync(path.join(firstDir, "agent.sqlite")),
205+ secondFileExists: fs.existsSync(path.join(secondDir, "agent.sqlite")),
206+ registeredPaths: listOpenClawRegisteredAgentDatabases({ env })
207+ .filter((entry) => entry.agentId === "worker-1")
208+ .map((entry) => entry.path),
209+ expectedPaths: [first.path, second.path].toSorted(),
210+ }));
211+ } finally {
212+ process.chdir(previousCwd);
213+ closeOpenClawAgentDatabasesForTest();
214+ closeOpenClawStateDatabaseForTest();
215+ }
216+ `,
217+],
218+{ encoding: "utf8" },
219+);
220+const result = JSON.parse(output) as {
221+expectedPaths: string[];
222+firstFileExists: boolean;
223+registeredPaths: string[];
224+sameHandle: boolean;
225+secondFileExists: boolean;
226+};
227+228+expect(result.sameHandle).toBe(false);
229+expect(result.firstFileExists).toBe(true);
230+expect(result.secondFileExists).toBe(true);
231+expect(result.registeredPaths).toEqual(result.expectedPaths);
232+});
233+157234it("rejects sharing one explicit database path across agent ids", () => {
158235const stateDir = createTempStateDir();
159236const env = { OPENCLAW_STATE_DIR: stateDir };
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。