test: remove agent session store path fixture

This commit is contained in:
Peter Steinberger
2026-05-08 09:23:59 +01:00
parent 9c8cf19477
commit b89dbce763

View File

@@ -1,23 +1,29 @@
import fs from "node:fs";
import path from "node:path";
import { withTempHome as withTempHomeBase } from "openclaw/plugin-sdk/test-env";
import { beforeEach, describe, expect, it } from "vitest";
import { afterEach, describe, expect, it, vi } from "vitest";
import { resolveAgentDir, resolveSessionAgentId } from "../agents/agent-scope.js";
import { resolveSession } from "../agents/command/session.js";
import { clearSessionStoreCacheForTest } from "../config/sessions/store.js";
import { upsertSessionEntry } from "../config/sessions/store.js";
import type { SessionEntry } from "../config/sessions/types.js";
import type { OpenClawConfig } from "../config/types.openclaw.js";
import { buildOutboundSessionContext } from "../infra/outbound/session-context.js";
import { closeOpenClawAgentDatabasesForTest } from "../state/openclaw-agent-db.js";
async function withTempHome<T>(fn: (home: string) => Promise<T>): Promise<T> {
return withTempHomeBase(fn, {
prefix: "openclaw-agent-session-",
skipSessionCleanup: true,
});
return withTempHomeBase(
async (home) => {
vi.stubEnv("OPENCLAW_STATE_DIR", path.join(home, ".openclaw"));
return await fn(home);
},
{
prefix: "openclaw-agent-session-",
skipSessionCleanup: true,
},
);
}
function mockConfig(
home: string,
storePath: string,
agentsList?: Array<{ id: string; default?: boolean }>,
): OpenClawConfig {
return {
@@ -29,47 +35,46 @@ function mockConfig(
},
list: agentsList,
},
session: { store: storePath, mainKey: "main" },
session: { mainKey: "main" },
} as OpenClawConfig;
}
function writeSessionStoreSeed(
storePath: string,
async function writeSessionRows(
agentId: string,
sessions: Record<string, Record<string, unknown>>,
) {
fs.mkdirSync(path.dirname(storePath), { recursive: true });
fs.writeFileSync(storePath, JSON.stringify(sessions));
): Promise<void> {
for (const [sessionKey, entry] of Object.entries(sessions)) {
upsertSessionEntry({ agentId, sessionKey, entry: entry as SessionEntry });
}
}
async function withCrossAgentResumeFixture(
run: (params: { sessionId: string; sessionKey: string; cfg: OpenClawConfig }) => Promise<void>,
): Promise<void> {
await withTempHome(async (home) => {
const storePattern = path.join(home, "sessions", "{agentId}", "sessions.json");
const execStore = path.join(home, "sessions", "exec", "sessions.json");
const sessionId = "session-exec-hook";
const sessionKey = "agent:exec:hook:gmail:thread-1";
writeSessionStoreSeed(execStore, {
await writeSessionRows("exec", {
[sessionKey]: {
sessionId,
updatedAt: Date.now(),
systemSent: true,
},
});
const cfg = mockConfig(home, storePattern, [{ id: "dev" }, { id: "exec", default: true }]);
const cfg = mockConfig(home, [{ id: "dev" }, { id: "exec", default: true }]);
await run({ sessionId, sessionKey, cfg });
});
}
beforeEach(() => {
clearSessionStoreCacheForTest();
afterEach(() => {
closeOpenClawAgentDatabasesForTest();
vi.unstubAllEnvs();
});
describe("agent session resolution", () => {
it("creates a stable session key for explicit session-id-only runs", async () => {
await withTempHome(async (home) => {
const store = path.join(home, "sessions.json");
const cfg = mockConfig(home, store);
const cfg = mockConfig(home);
const resolution = resolveSession({ cfg, sessionId: "explicit-session-123" });
@@ -92,44 +97,37 @@ describe("agent session resolution", () => {
it("resolves duplicate cross-agent sessionIds deterministically", async () => {
await withTempHome(async (home) => {
const storePattern = path.join(home, "sessions", "{agentId}", "sessions.json");
const otherStore = path.join(home, "sessions", "other", "sessions.json");
const retiredStore = path.join(home, "sessions", "retired", "sessions.json");
writeSessionStoreSeed(otherStore, {
await writeSessionRows("other", {
"agent:other:main": {
sessionId: "run-dup",
updatedAt: Date.now() + 1_000,
},
});
writeSessionStoreSeed(retiredStore, {
await writeSessionRows("retired", {
"agent:retired:acp:run-dup": {
sessionId: "run-dup",
updatedAt: Date.now(),
},
});
const cfg = mockConfig(home, storePattern, [
{ id: "other" },
{ id: "retired", default: true },
]);
const cfg = mockConfig(home, [{ id: "other" }, { id: "retired", default: true }]);
const resolution = resolveSession({ cfg, sessionId: "run-dup" });
expect(resolution.sessionKey).toBe("agent:retired:acp:run-dup");
expect(resolution.storePath).toBe(retiredStore);
expect(resolution.agentId).toBe("retired");
});
});
it("uses origin.provider for channel-specific session reset overrides", async () => {
await withTempHome(async (home) => {
const store = path.join(home, "sessions.json");
writeSessionStoreSeed(store, {
await writeSessionRows("main", {
main: {
sessionId: "origin-provider-reset",
updatedAt: Date.now() - 30 * 60_000,
origin: { provider: "quietchat" },
},
});
const cfg = mockConfig(home, store);
const cfg = mockConfig(home);
cfg.session = {
...cfg.session,
reset: { mode: "idle", idleMinutes: 10 },