Skip to content

Commit dd20e9c

Browse files
committed
test(agents): split the delete suite so the new cron coverage stays under the cap
The 40-line cron regression test added in the previous commit pushed `src/commands/agents.delete.test.ts` to 1018 code lines, over the 1000 cap, and `check-lint-core-3` went red. Repo policy forbids a `max-lines` suppression. Unlike the earlier `cron/view.test.ts` split there was no describe-level seam -- 23 flat tests in a single describe -- so the split follows subject instead. The seven workspace-lifecycle tests (trashing, sharing, overlap, symlink reachability, workspace-state cleanup) move to `agents.delete.workspace.test.ts`. `vi.mock` and `vi.hoisted` are per-file and cannot be imported, so the mock preamble and the shared `beforeEach` are declared in both files; the helper block above them is unchanged in each. Each file then imports only what it uses, which is why the import lists differ. Trimming to a hair under the cap by moving only the new test was possible and rejected: it would have left the file at ~978 code lines, back at the cap within a couple of changes. This leaves 749 and 603 physical lines. No test content changed: 27 passed before, 27 after.
1 parent df680f7 commit dd20e9c

2 files changed

Lines changed: 603 additions & 374 deletions

File tree

‎src/commands/agents.delete.test.ts‎

Lines changed: 0 additions & 374 deletions
Original file line numberDiff line numberDiff line change
@@ -746,378 +746,4 @@ describe("agents delete command", () => {
746746
expect(readAgentDeletionJournal("ops")?.cleanupCompleted).toBe(true);
747747
});
748748
});
749-
750-
it("deletes workspace state after local workspace removal", async () => {
751-
await withStateDirEnv("openclaw-agents-delete-workspace-state-", async ({ stateDir }) => {
752-
const opsWorkspace = path.join(stateDir, "workspace-ops");
753-
const cfg: OpenClawConfig = {
754-
agents: {
755-
list: [
756-
{ id: "main", workspace: path.join(stateDir, "workspace-main") },
757-
{ id: "ops", workspace: opsWorkspace },
758-
],
759-
},
760-
} satisfies OpenClawConfig;
761-
await arrangeAgentsDeleteTest({
762-
stateDir,
763-
cfg,
764-
deletedAgentId: "ops",
765-
sessions: {},
766-
});
767-
await agentsDeleteCommand({ id: "ops", force: true, json: true }, runtime);
768-
769-
expect(workspaceStateMocks.deleteWorkspaceState).toHaveBeenCalledWith({
770-
workspaceDir: opsWorkspace,
771-
});
772-
const workspaceTrashOrder = fsSafeMocks.movePathToTrash.mock.invocationCallOrder[0];
773-
const stateDeleteOrder = workspaceStateMocks.deleteWorkspaceState.mock.invocationCallOrder[0];
774-
expect(workspaceTrashOrder).toBeLessThan(stateDeleteOrder ?? 0);
775-
});
776-
});
777-
778-
it("finishes agent-directory cleanup when workspace state deletion fails", async () => {
779-
await withStateDirEnv("openclaw-agents-delete-state-failure-", async ({ stateDir }) => {
780-
const opsWorkspace = path.join(stateDir, "workspace-ops");
781-
const opsAgentDir = path.join(stateDir, "agents", "ops", "agent");
782-
const cfg: OpenClawConfig = {
783-
agents: {
784-
list: [
785-
{ id: "main", workspace: path.join(stateDir, "workspace-main") },
786-
{ id: "ops", workspace: opsWorkspace },
787-
],
788-
},
789-
} satisfies OpenClawConfig;
790-
await arrangeAgentsDeleteTest({ stateDir, cfg, deletedAgentId: "ops", sessions: {} });
791-
workspaceStateMocks.deleteWorkspaceState.mockImplementationOnce(() => {
792-
throw new Error("state database unavailable");
793-
});
794-
795-
await expect(
796-
agentsDeleteCommand({ id: "ops", force: true, json: true }, runtime),
797-
).rejects.toThrow("state database unavailable");
798-
799-
const trashedPaths = fsSafeMocks.movePathToTrash.mock.calls.map(([targetPath]) => targetPath);
800-
const expectedAgentDir = path.join(
801-
await fs.realpath(path.dirname(opsAgentDir)),
802-
path.basename(opsAgentDir),
803-
);
804-
expect(trashedPaths).toContain(expectedAgentDir);
805-
});
806-
});
807-
808-
it("refuses deleting the sole configured agent", async () => {
809-
await withStateDirEnv("openclaw-agents-delete-main-alias-", async ({ stateDir }) => {
810-
const now = Date.now();
811-
const cfg: OpenClawConfig = {
812-
agents: {
813-
list: [{ id: "ops", default: true, workspace: path.join(stateDir, "workspace-ops") }],
814-
},
815-
};
816-
await arrangeAgentsDeleteTest({
817-
stateDir,
818-
cfg,
819-
sessions: {
820-
"agent:main:main": { sessionId: "sess-default-alias", updatedAt: now + 1 },
821-
"agent:ops:quietchat:direct:u1": { sessionId: "sess-ops-direct", updatedAt: now + 2 },
822-
"agent:main:quietchat:direct:u2": {
823-
sessionId: "sess-stale-main",
824-
updatedAt: now + 3,
825-
},
826-
global: { sessionId: "sess-global", updatedAt: now + 4 },
827-
},
828-
});
829-
830-
await agentsDeleteCommand({ id: "ops", force: true, json: true }, runtime);
831-
832-
expect(runtime.error).not.toHaveBeenCalled();
833-
expect(readJsonLogs()).toEqual([
834-
{
835-
ok: false,
836-
error: {
837-
type: "cli_error",
838-
message: 'Agent "ops" is the only configured agent and cannot be deleted.',
839-
},
840-
},
841-
]);
842-
expect(runtime.exit).toHaveBeenCalledWith(1, { resetStream: process.stderr });
843-
expectSessionStore(cfg, {
844-
"agent:main:main": { sessionId: "sess-default-alias", updatedAt: now + 1 },
845-
"agent:ops:quietchat:direct:u1": { sessionId: "sess-ops-direct", updatedAt: now + 2 },
846-
"agent:main:quietchat:direct:u2": {
847-
sessionId: "sess-stale-main",
848-
updatedAt: now + 3,
849-
},
850-
global: { sessionId: "sess-global", updatedAt: now + 4 },
851-
});
852-
});
853-
});
854-
855-
it("preserves canonical main-agent keys when deleting another agent", async () => {
856-
await withStateDirEnv("openclaw-agents-delete-shared-store-", async ({ stateDir }) => {
857-
const now = Date.now();
858-
const cfg: OpenClawConfig = {
859-
session: { store: path.join(stateDir, "shared-sessions.sqlite") },
860-
agents: {
861-
list: [
862-
{ id: "main", default: true, workspace: path.join(stateDir, "workspace-main") },
863-
{ id: "ops", workspace: path.join(stateDir, "workspace-ops") },
864-
],
865-
},
866-
};
867-
await arrangeAgentsDeleteTest({
868-
stateDir,
869-
cfg,
870-
sessions: {
871-
"agent:main:main": { sessionId: "sess-main", updatedAt: now + 1 },
872-
"agent:main:quietchat:direct:u1": {
873-
sessionId: "sess-main-direct",
874-
updatedAt: now + 2,
875-
},
876-
"agent:ops:main": { sessionId: "sess-ops-main", updatedAt: now + 3 },
877-
"agent:ops:quietchat:direct:u2": { sessionId: "sess-ops-direct", updatedAt: now + 4 },
878-
},
879-
});
880-
881-
await agentsDeleteCommand({ id: "ops", force: true, json: true }, runtime);
882-
883-
expect(runtime.exit).not.toHaveBeenCalled();
884-
expectSessionStore(
885-
cfg,
886-
{
887-
"agent:main:main": { sessionId: "sess-main", updatedAt: now + 1 },
888-
"agent:main:quietchat:direct:u1": {
889-
sessionId: "sess-main-direct",
890-
updatedAt: now + 2,
891-
},
892-
},
893-
"main",
894-
);
895-
});
896-
});
897-
898-
it("skips workspace removal when another agent shares the same workspace (#70890)", async () => {
899-
await withStateDirEnv("openclaw-agents-delete-shared-workspace-", async ({ stateDir }) => {
900-
const sharedWorkspace = path.join(stateDir, "workspace-shared");
901-
await fs.mkdir(sharedWorkspace, { recursive: true });
902-
903-
const now = Date.now();
904-
const cfg: OpenClawConfig = {
905-
agents: {
906-
list: [
907-
{ id: "main", workspace: sharedWorkspace },
908-
{ id: "ops", workspace: sharedWorkspace },
909-
],
910-
},
911-
} satisfies OpenClawConfig;
912-
await arrangeAgentsDeleteTest({
913-
stateDir,
914-
cfg,
915-
deletedAgentId: "ops",
916-
sessions: {
917-
"agent:ops:main": { sessionId: "sess-ops-main", updatedAt: now + 1 },
918-
"agent:main:main": { sessionId: "sess-main", updatedAt: now + 2 },
919-
},
920-
});
921-
922-
await agentsDeleteCommand({ id: "ops", force: true, json: true }, runtime);
923-
924-
// Workspace should still exist — it was shared
925-
const retainedWorkspaceStats = await fs.stat(sharedWorkspace);
926-
expect(retainedWorkspaceStats.isDirectory()).toBe(true);
927-
928-
// The JSON output should report why the workspace was retained.
929-
const jsonOutput = readJsonLogs();
930-
expect(jsonOutput).toHaveLength(1);
931-
expect(jsonOutput[0]?.workspaceRetained).toBe(true);
932-
expect(jsonOutput[0]?.workspaceRetainedReason).toBe("shared");
933-
expect(jsonOutput[0]?.workspaceSharedWith).toEqual(["main"]);
934-
const trashedPaths = fsSafeMocks.movePathToTrash.mock.calls.map(([targetPath]) => targetPath);
935-
expect(trashedPaths).not.toContain(sharedWorkspace);
936-
expect(workspaceStateMocks.deleteWorkspaceState).not.toHaveBeenCalled();
937-
});
938-
});
939-
940-
it("skips workspace removal when another agent workspace overlaps a child path (#70890)", async () => {
941-
await withStateDirEnv("openclaw-agents-delete-overlapping-workspace-", async ({ stateDir }) => {
942-
const sharedWorkspace = path.join(stateDir, "workspace-shared");
943-
const childWorkspace = path.join(sharedWorkspace, "ops-child");
944-
await fs.mkdir(childWorkspace, { recursive: true });
945-
946-
const now = Date.now();
947-
const cfg: OpenClawConfig = {
948-
agents: {
949-
list: [
950-
{ id: "main", workspace: sharedWorkspace },
951-
{ id: "ops", workspace: childWorkspace },
952-
],
953-
},
954-
} satisfies OpenClawConfig;
955-
await arrangeAgentsDeleteTest({
956-
stateDir,
957-
cfg,
958-
deletedAgentId: "ops",
959-
sessions: {
960-
"agent:ops:main": { sessionId: "sess-ops-main", updatedAt: now + 1 },
961-
"agent:main:main": { sessionId: "sess-main", updatedAt: now + 2 },
962-
},
963-
});
964-
965-
await agentsDeleteCommand({ id: "ops", force: true, json: true }, runtime);
966-
967-
const output = readJsonLogs()[0];
968-
expect(output?.workspaceRetained).toBe(true);
969-
expect(output?.workspaceSharedWith).toEqual(["main"]);
970-
const trashedPaths = fsSafeMocks.movePathToTrash.mock.calls.map(([targetPath]) => targetPath);
971-
expect(trashedPaths).not.toContain(childWorkspace);
972-
});
973-
});
974-
975-
it("skips workspace removal when deleting a parent workspace that contains another agent workspace (#70890)", async () => {
976-
await withStateDirEnv("openclaw-agents-delete-parent-workspace-", async ({ stateDir }) => {
977-
const sharedWorkspace = path.join(stateDir, "workspace-shared");
978-
const childWorkspace = path.join(sharedWorkspace, "main-child");
979-
await fs.mkdir(childWorkspace, { recursive: true });
980-
981-
const now = Date.now();
982-
const cfg: OpenClawConfig = {
983-
agents: {
984-
list: [
985-
{ id: "main", workspace: childWorkspace },
986-
{ id: "ops", workspace: sharedWorkspace },
987-
],
988-
},
989-
} satisfies OpenClawConfig;
990-
await arrangeAgentsDeleteTest({
991-
stateDir,
992-
cfg,
993-
deletedAgentId: "ops",
994-
sessions: {
995-
"agent:ops:main": { sessionId: "sess-ops-main", updatedAt: now + 1 },
996-
"agent:main:main": { sessionId: "sess-main", updatedAt: now + 2 },
997-
},
998-
});
999-
1000-
await agentsDeleteCommand({ id: "ops", force: true, json: true }, runtime);
1001-
1002-
const output = readJsonLogs()[0];
1003-
expect(output?.workspaceRetained).toBe(true);
1004-
expect(output?.workspaceSharedWith).toEqual(["main"]);
1005-
const trashedPaths = fsSafeMocks.movePathToTrash.mock.calls.map(([targetPath]) => targetPath);
1006-
expect(trashedPaths).not.toContain(sharedWorkspace);
1007-
});
1008-
});
1009-
1010-
it.runIf(process.platform !== "win32")(
1011-
"skips workspace removal when another agent reaches the same directory through a symlink (#70890)",
1012-
async () => {
1013-
await withStateDirEnv("openclaw-agents-delete-symlink-workspace-", async ({ stateDir }) => {
1014-
const realWorkspace = path.join(stateDir, "workspace-real");
1015-
const aliasWorkspace = path.join(stateDir, "workspace-alias");
1016-
await fs.mkdir(realWorkspace, { recursive: true });
1017-
await fs.symlink(realWorkspace, aliasWorkspace, "dir");
1018-
1019-
const now = Date.now();
1020-
const cfg: OpenClawConfig = {
1021-
agents: {
1022-
list: [
1023-
{ id: "main", workspace: realWorkspace },
1024-
{ id: "ops", workspace: aliasWorkspace },
1025-
],
1026-
},
1027-
} satisfies OpenClawConfig;
1028-
await arrangeAgentsDeleteTest({
1029-
stateDir,
1030-
cfg,
1031-
deletedAgentId: "ops",
1032-
sessions: {
1033-
"agent:ops:main": { sessionId: "sess-ops-main", updatedAt: now + 1 },
1034-
"agent:main:main": { sessionId: "sess-main", updatedAt: now + 2 },
1035-
},
1036-
});
1037-
1038-
await agentsDeleteCommand({ id: "ops", force: true, json: true }, runtime);
1039-
1040-
const output = readJsonLogs()[0];
1041-
expect(output?.workspaceRetained).toBe(true);
1042-
expect(output?.workspaceSharedWith).toEqual(["main"]);
1043-
const trashedPaths = fsSafeMocks.movePathToTrash.mock.calls.map(
1044-
([targetPath]) => targetPath,
1045-
);
1046-
expect(trashedPaths).not.toContain(aliasWorkspace);
1047-
});
1048-
},
1049-
);
1050-
1051-
it("trashes workspace when no other agent shares it", async () => {
1052-
await withStateDirEnv("openclaw-agents-delete-unique-workspace-", async ({ stateDir }) => {
1053-
const opsWorkspace = path.join(stateDir, "workspace-ops");
1054-
const mainWorkspace = path.join(stateDir, "workspace-main");
1055-
await fs.mkdir(opsWorkspace, { recursive: true });
1056-
await fs.mkdir(mainWorkspace, { recursive: true });
1057-
1058-
const now = Date.now();
1059-
const cfg: OpenClawConfig = {
1060-
agents: {
1061-
list: [
1062-
{ id: "main", workspace: mainWorkspace },
1063-
{ id: "ops", workspace: opsWorkspace },
1064-
],
1065-
},
1066-
} satisfies OpenClawConfig;
1067-
await arrangeAgentsDeleteTest({
1068-
stateDir,
1069-
cfg,
1070-
deletedAgentId: "ops",
1071-
sessions: {
1072-
"agent:ops:main": { sessionId: "sess-ops-main", updatedAt: now + 1 },
1073-
"agent:main:main": { sessionId: "sess-main", updatedAt: now + 2 },
1074-
},
1075-
});
1076-
1077-
const expectedOpsWorkspace = path.join(
1078-
await fs.realpath(path.dirname(opsWorkspace)),
1079-
path.basename(opsWorkspace),
1080-
);
1081-
1082-
await agentsDeleteCommand({ id: "ops", force: true, json: true }, runtime);
1083-
1084-
expect(fsSafeMocks.movePathToTrash).toHaveBeenCalledWith(expectedOpsWorkspace, {
1085-
allowedRoots: [path.dirname(expectedOpsWorkspace)],
1086-
});
1087-
expect(workspaceStateMocks.deleteWorkspaceState).toHaveBeenCalledWith({
1088-
workspaceDir: opsWorkspace,
1089-
});
1090-
expect(processMocks.runCommandWithTimeout).not.toHaveBeenCalled();
1091-
});
1092-
});
1093-
1094-
it("retains workspace state when workspace trash fails", async () => {
1095-
await withStateDirEnv("openclaw-agents-delete-trash-failure-", async ({ stateDir }) => {
1096-
const opsWorkspace = path.join(stateDir, "workspace-ops");
1097-
const opsAgentDir = path.join(stateDir, "agents", "ops", "agent");
1098-
const opsSessionsDir = path.join(stateDir, "agents", "ops", "sessions");
1099-
const cfg: OpenClawConfig = {
1100-
agents: {
1101-
list: [
1102-
{ id: "main", workspace: path.join(stateDir, "workspace-main") },
1103-
{ id: "ops", workspace: opsWorkspace },
1104-
],
1105-
},
1106-
} satisfies OpenClawConfig;
1107-
await arrangeAgentsDeleteTest({ stateDir, cfg, sessions: {} });
1108-
fsSafeMocks.movePathToTrash.mockRejectedValueOnce(new Error("trash unavailable"));
1109-
1110-
await agentsDeleteCommand({ id: "ops", force: true, json: true }, runtime);
1111-
1112-
expect(workspaceStateMocks.deleteWorkspaceState).not.toHaveBeenCalled();
1113-
expect(readJsonLogs()[0]).toMatchObject({
1114-
removed: [
1115-
{ path: opsAgentDir, method: "trash" },
1116-
{ path: opsSessionsDir, method: "missing" },
1117-
],
1118-
failed: [{ path: opsWorkspace, reason: "trash unavailable" }],
1119-
});
1120-
expect(readAgentDeletionJournal("ops")?.cleanupCompleted).toBe(false);
1121-
});
1122-
});
1123749
});

0 commit comments

Comments
 (0)