test: share agents delete setup

This commit is contained in:
Peter Steinberger
2026-04-20 16:01:38 +01:00
parent 09171eee8d
commit f930b23dad

View File

@@ -29,6 +29,38 @@ import { agentsDeleteCommand } from "./agents.js";
const runtime = createTestRuntime();
async function arrangeAgentsDeleteTest(params: {
stateDir: string;
cfg: OpenClawConfig;
deletedAgentId?: string;
sessions: Record<string, { sessionId: string; updatedAt: number }>;
}) {
const deletedAgentId = params.deletedAgentId ?? "ops";
const storePath = resolveStorePath(params.cfg.session?.store, { agentId: deletedAgentId });
await saveSessionStore(storePath, params.sessions);
await fs.mkdir(path.join(params.stateDir, `workspace-${deletedAgentId}`), { recursive: true });
await fs.mkdir(path.join(params.stateDir, "agents", deletedAgentId, "agent"), {
recursive: true,
});
configMocks.readConfigFileSnapshot.mockResolvedValue({
...baseConfigSnapshot,
config: params.cfg,
runtimeConfig: params.cfg,
sourceConfig: params.cfg,
resolved: params.cfg,
});
return storePath;
}
function expectSessionStore(
storePath: string,
sessions: Record<string, { sessionId: string; updatedAt: number }>,
) {
expect(loadSessionStore(storePath, { skipCache: true })).toEqual(sessions);
}
describe("agents delete command", () => {
beforeEach(() => {
configMocks.readConfigFileSnapshot.mockReset();
@@ -49,21 +81,14 @@ describe("agents delete command", () => {
],
},
} satisfies OpenClawConfig;
const storePath = resolveStorePath(cfg.session?.store, { agentId: "ops" });
await saveSessionStore(storePath, {
"agent:ops:main": { sessionId: "sess-ops-main", updatedAt: 1 },
"agent:ops:discord:direct:u1": { sessionId: "sess-ops-direct", updatedAt: 2 },
"agent:main:main": { sessionId: "sess-main", updatedAt: 3 },
});
await fs.mkdir(path.join(stateDir, "workspace-ops"), { recursive: true });
await fs.mkdir(path.join(stateDir, "agents", "ops", "agent"), { recursive: true });
configMocks.readConfigFileSnapshot.mockResolvedValue({
...baseConfigSnapshot,
config: cfg,
runtimeConfig: cfg,
sourceConfig: cfg,
resolved: cfg,
const storePath = await arrangeAgentsDeleteTest({
stateDir,
cfg,
sessions: {
"agent:ops:main": { sessionId: "sess-ops-main", updatedAt: 1 },
"agent:ops:discord:direct:u1": { sessionId: "sess-ops-direct", updatedAt: 2 },
"agent:main:main": { sessionId: "sess-main", updatedAt: 3 },
},
});
await agentsDeleteCommand({ id: "ops", force: true, json: true }, runtime);
@@ -76,7 +101,7 @@ describe("agents delete command", () => {
},
}),
);
expect(loadSessionStore(storePath, { skipCache: true })).toEqual({
expectSessionStore(storePath, {
"agent:main:main": { sessionId: "sess-main", updatedAt: 3 },
});
});
@@ -89,28 +114,21 @@ describe("agents delete command", () => {
list: [{ id: "ops", default: true, workspace: path.join(stateDir, "workspace-ops") }],
},
};
const storePath = resolveStorePath(cfg.session?.store, { agentId: "ops" });
await saveSessionStore(storePath, {
"agent:main:main": { sessionId: "sess-default-alias", updatedAt: 1 },
"agent:ops:discord:direct:u1": { sessionId: "sess-ops-direct", updatedAt: 2 },
"agent:main:discord:direct:u2": { sessionId: "sess-stale-main", updatedAt: 3 },
global: { sessionId: "sess-global", updatedAt: 4 },
});
await fs.mkdir(path.join(stateDir, "workspace-ops"), { recursive: true });
await fs.mkdir(path.join(stateDir, "agents", "ops", "agent"), { recursive: true });
configMocks.readConfigFileSnapshot.mockResolvedValue({
...baseConfigSnapshot,
config: cfg,
runtimeConfig: cfg,
sourceConfig: cfg,
resolved: cfg,
const storePath = await arrangeAgentsDeleteTest({
stateDir,
cfg,
sessions: {
"agent:main:main": { sessionId: "sess-default-alias", updatedAt: 1 },
"agent:ops:discord:direct:u1": { sessionId: "sess-ops-direct", updatedAt: 2 },
"agent:main:discord:direct:u2": { sessionId: "sess-stale-main", updatedAt: 3 },
global: { sessionId: "sess-global", updatedAt: 4 },
},
});
await agentsDeleteCommand({ id: "ops", force: true, json: true }, runtime);
expect(runtime.exit).not.toHaveBeenCalled();
expect(loadSessionStore(storePath, { skipCache: true })).toEqual({
expectSessionStore(storePath, {
"agent:main:discord:direct:u2": { sessionId: "sess-stale-main", updatedAt: 3 },
global: { sessionId: "sess-global", updatedAt: 4 },
});
@@ -128,28 +146,21 @@ describe("agents delete command", () => {
],
},
};
const storePath = resolveStorePath(cfg.session?.store, { agentId: "ops" });
await saveSessionStore(storePath, {
main: { sessionId: "sess-main", updatedAt: 1 },
"discord:direct:u1": { sessionId: "sess-main-direct", updatedAt: 2 },
"agent:ops:main": { sessionId: "sess-ops-main", updatedAt: 3 },
"agent:ops:discord:direct:u2": { sessionId: "sess-ops-direct", updatedAt: 4 },
});
await fs.mkdir(path.join(stateDir, "workspace-ops"), { recursive: true });
await fs.mkdir(path.join(stateDir, "agents", "ops", "agent"), { recursive: true });
configMocks.readConfigFileSnapshot.mockResolvedValue({
...baseConfigSnapshot,
config: cfg,
runtimeConfig: cfg,
sourceConfig: cfg,
resolved: cfg,
const storePath = await arrangeAgentsDeleteTest({
stateDir,
cfg,
sessions: {
main: { sessionId: "sess-main", updatedAt: 1 },
"discord:direct:u1": { sessionId: "sess-main-direct", updatedAt: 2 },
"agent:ops:main": { sessionId: "sess-ops-main", updatedAt: 3 },
"agent:ops:discord:direct:u2": { sessionId: "sess-ops-direct", updatedAt: 4 },
},
});
await agentsDeleteCommand({ id: "ops", force: true, json: true }, runtime);
expect(runtime.exit).not.toHaveBeenCalled();
expect(loadSessionStore(storePath, { skipCache: true })).toEqual({
expectSessionStore(storePath, {
main: { sessionId: "sess-main", updatedAt: 1 },
"discord:direct:u1": { sessionId: "sess-main-direct", updatedAt: 2 },
});