|
| 1 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import { FsSafeError } from "../infra/fs-safe.js"; |
| 3 | + |
| 4 | +const mocks = vi.hoisted(() => ({ |
| 5 | + config: {} as Record<string, unknown>, |
| 6 | + persisted: {} as Record<string, unknown>, |
| 7 | + transformConfigFileWithRetry: vi.fn(), |
| 8 | + withConfigMutationExclusive: vi.fn(), |
| 9 | + parseBindingSpecs: vi.fn(), |
| 10 | + ensureAgentWorkspace: vi.fn(), |
| 11 | + resolveAgentWorkspaceDir: vi.fn(), |
| 12 | + resolveAgentDir: vi.fn(), |
| 13 | + rootRead: vi.fn(), |
| 14 | + rootWrite: vi.fn(), |
| 15 | + mkdir: vi.fn(), |
| 16 | +})); |
| 17 | + |
| 18 | +vi.mock("node:fs/promises", () => ({ default: { mkdir: mocks.mkdir } })); |
| 19 | + |
| 20 | +vi.mock("../config/config.js", async (importOriginal) => { |
| 21 | + const actual = await importOriginal<typeof import("../config/config.js")>(); |
| 22 | + return { |
| 23 | + ...actual, |
| 24 | + transformConfigFileWithRetry: mocks.transformConfigFileWithRetry, |
| 25 | + withConfigMutationExclusive: mocks.withConfigMutationExclusive, |
| 26 | + }; |
| 27 | +}); |
| 28 | + |
| 29 | +vi.mock("../commands/agents.bindings.js", async (importOriginal) => ({ |
| 30 | + ...(await importOriginal<typeof import("../commands/agents.bindings.js")>()), |
| 31 | + parseBindingSpecs: mocks.parseBindingSpecs, |
| 32 | +})); |
| 33 | + |
| 34 | +vi.mock("./agent-scope.js", async (importOriginal) => ({ |
| 35 | + ...(await importOriginal<typeof import("./agent-scope.js")>()), |
| 36 | + resolveAgentWorkspaceDir: mocks.resolveAgentWorkspaceDir, |
| 37 | + resolveAgentDir: mocks.resolveAgentDir, |
| 38 | +})); |
| 39 | + |
| 40 | +vi.mock("./workspace.js", async (importOriginal) => { |
| 41 | + const actual = await importOriginal<typeof import("./workspace.js")>(); |
| 42 | + return { ...actual, ensureAgentWorkspace: mocks.ensureAgentWorkspace }; |
| 43 | +}); |
| 44 | + |
| 45 | +vi.mock("../config/sessions/paths.js", () => ({ |
| 46 | + resolveSessionTranscriptsDirForAgent: (agentId: string) => `/tmp/transcripts-${agentId}`, |
| 47 | +})); |
| 48 | + |
| 49 | +vi.mock("../infra/fs-safe.js", async (importOriginal) => { |
| 50 | + const actual = await importOriginal<typeof import("../infra/fs-safe.js")>(); |
| 51 | + return { |
| 52 | + ...actual, |
| 53 | + root: vi.fn(async () => ({ |
| 54 | + read: mocks.rootRead, |
| 55 | + write: mocks.rootWrite, |
| 56 | + })), |
| 57 | + }; |
| 58 | +}); |
| 59 | + |
| 60 | +import { createAgent } from "./agent-create.js"; |
| 61 | + |
| 62 | +describe("createAgent", () => { |
| 63 | + beforeEach(() => { |
| 64 | + vi.clearAllMocks(); |
| 65 | + mocks.config = {}; |
| 66 | + mocks.persisted = {}; |
| 67 | + mocks.resolveAgentWorkspaceDir.mockReturnValue("/tmp/default-researcher"); |
| 68 | + mocks.resolveAgentDir.mockReturnValue("/tmp/agent-researcher"); |
| 69 | + mocks.ensureAgentWorkspace.mockImplementation(async ({ dir }: { dir: string }) => ({ |
| 70 | + dir, |
| 71 | + bootstrapPending: true, |
| 72 | + })); |
| 73 | + mocks.rootRead.mockResolvedValue({ buffer: Buffer.from("") }); |
| 74 | + mocks.rootWrite.mockResolvedValue(undefined); |
| 75 | + mocks.mkdir.mockResolvedValue(undefined); |
| 76 | + mocks.parseBindingSpecs.mockReturnValue({ bindings: [], errors: [] }); |
| 77 | + mocks.withConfigMutationExclusive.mockImplementation(async (fn: () => Promise<unknown>) => { |
| 78 | + return await fn(); |
| 79 | + }); |
| 80 | + mocks.transformConfigFileWithRetry.mockImplementation( |
| 81 | + async ({ |
| 82 | + transform, |
| 83 | + }: { |
| 84 | + transform: (config: Record<string, unknown>) => Promise<unknown>; |
| 85 | + }) => { |
| 86 | + const transformed = (await transform(structuredClone(mocks.config))) as { |
| 87 | + nextConfig: Record<string, unknown>; |
| 88 | + result: unknown; |
| 89 | + }; |
| 90 | + mocks.persisted = transformed.nextConfig; |
| 91 | + mocks.config = transformed.nextConfig; |
| 92 | + return { result: transformed.result, nextConfig: transformed.nextConfig }; |
| 93 | + }, |
| 94 | + ); |
| 95 | + }); |
| 96 | + |
| 97 | + it("returns validation errors before mutation", async () => { |
| 98 | + await expect(createAgent({ name: " " })).resolves.toMatchObject({ |
| 99 | + status: "error", |
| 100 | + reason: "invalid-name", |
| 101 | + }); |
| 102 | + for (const name of ["main", "OpenClaw", "crestodian"]) { |
| 103 | + await expect(createAgent({ name })).resolves.toMatchObject({ |
| 104 | + status: "error", |
| 105 | + reason: "reserved-id", |
| 106 | + }); |
| 107 | + } |
| 108 | + expect(mocks.transformConfigFileWithRetry).not.toHaveBeenCalled(); |
| 109 | + }); |
| 110 | + |
| 111 | + it("defaults the workspace through the agent-scoped resolver", async () => { |
| 112 | + const result = await createAgent({ name: "Researcher" }); |
| 113 | + |
| 114 | + expect(mocks.resolveAgentWorkspaceDir).toHaveBeenCalledWith(expect.any(Object), "researcher"); |
| 115 | + expect(result).toMatchObject({ |
| 116 | + status: "created", |
| 117 | + agentId: "researcher", |
| 118 | + workspace: "/tmp/default-researcher", |
| 119 | + bootstrapPending: true, |
| 120 | + }); |
| 121 | + }); |
| 122 | + |
| 123 | + it("respects skipBootstrap from the current config", async () => { |
| 124 | + mocks.config = { agents: { defaults: { skipBootstrap: true } } }; |
| 125 | + |
| 126 | + await createAgent({ name: "researcher", workspace: "/tmp/work" }); |
| 127 | + |
| 128 | + expect(mocks.ensureAgentWorkspace).toHaveBeenCalledWith( |
| 129 | + expect.objectContaining({ ensureBootstrapFiles: false }), |
| 130 | + ); |
| 131 | + }); |
| 132 | + |
| 133 | + it("persists the authoritative workspace returned by setup", async () => { |
| 134 | + mocks.ensureAgentWorkspace.mockResolvedValue({ |
| 135 | + dir: "/normalized/work", |
| 136 | + bootstrapPending: true, |
| 137 | + }); |
| 138 | + |
| 139 | + const result = await createAgent({ name: "researcher", workspace: "/tmp/work" }); |
| 140 | + |
| 141 | + const agents = mocks.persisted.agents as { list?: Array<{ workspace?: string }> } | undefined; |
| 142 | + expect(agents?.list?.at(-1)?.workspace).toBe("/normalized/work"); |
| 143 | + expect(result).toMatchObject({ status: "created", workspace: "/normalized/work" }); |
| 144 | + }); |
| 145 | + |
| 146 | + it("persists the canonical agent entry through retrying mutation", async () => { |
| 147 | + const result = await createAgent({ |
| 148 | + name: "Researcher", |
| 149 | + workspace: "/tmp/work", |
| 150 | + model: "openai/gpt-5.5", |
| 151 | + emoji: "🔎", |
| 152 | + }); |
| 153 | + |
| 154 | + expect(mocks.transformConfigFileWithRetry).toHaveBeenCalledOnce(); |
| 155 | + expect(mocks.persisted).toMatchObject({ |
| 156 | + agents: { |
| 157 | + list: expect.arrayContaining([ |
| 158 | + { |
| 159 | + id: "researcher", |
| 160 | + name: "Researcher", |
| 161 | + workspace: "/tmp/work", |
| 162 | + agentDir: "/tmp/agent-researcher", |
| 163 | + model: "openai/gpt-5.5", |
| 164 | + identity: { name: "Researcher", emoji: "🔎" }, |
| 165 | + }, |
| 166 | + ]), |
| 167 | + }, |
| 168 | + }); |
| 169 | + expect(result).toMatchObject({ status: "created", agentId: "researcher" }); |
| 170 | + }); |
| 171 | + |
| 172 | + it("finishes workspace setup before publishing config", async () => { |
| 173 | + mocks.ensureAgentWorkspace.mockImplementation(async ({ dir }: { dir: string }) => { |
| 174 | + expect(mocks.persisted).not.toHaveProperty("agents"); |
| 175 | + return { dir, bootstrapPending: true }; |
| 176 | + }); |
| 177 | + |
| 178 | + await createAgent({ name: "researcher" }); |
| 179 | + |
| 180 | + expect(mocks.ensureAgentWorkspace).toHaveBeenCalledOnce(); |
| 181 | + }); |
| 182 | + |
| 183 | + it("does not publish config when identity setup is unsafe", async () => { |
| 184 | + mocks.rootRead.mockRejectedValue(new FsSafeError("invalid-path", "unsafe identity path")); |
| 185 | + |
| 186 | + await expect(createAgent({ name: "researcher" })).resolves.toMatchObject({ |
| 187 | + status: "error", |
| 188 | + reason: "unsafe-identity-file", |
| 189 | + }); |
| 190 | + expect(mocks.transformConfigFileWithRetry).toHaveBeenCalledOnce(); |
| 191 | + expect(mocks.persisted).not.toHaveProperty("agents"); |
| 192 | + }); |
| 193 | + |
| 194 | + it("rejects a concurrent duplicate from the mutation snapshot", async () => { |
| 195 | + mocks.config = { agents: { list: [{ id: "researcher" }] } }; |
| 196 | + |
| 197 | + await expect(createAgent({ name: "researcher" })).resolves.toMatchObject({ |
| 198 | + status: "error", |
| 199 | + reason: "already-exists", |
| 200 | + }); |
| 201 | + expect(mocks.ensureAgentWorkspace).not.toHaveBeenCalled(); |
| 202 | + }); |
| 203 | + |
| 204 | + it("parses binding specs from the locked winning snapshot", async () => { |
| 205 | + mocks.parseBindingSpecs.mockReturnValue({ |
| 206 | + bindings: [], |
| 207 | + errors: ['Unknown channel "removed".'], |
| 208 | + }); |
| 209 | + const transformConfig = vi.fn(async ({ maxAttempts, transform }) => { |
| 210 | + expect(maxAttempts).toBe(1); |
| 211 | + return await transform({ agents: { list: [] } }); |
| 212 | + }); |
| 213 | + |
| 214 | + await expect( |
| 215 | + createAgent({ |
| 216 | + name: "researcher", |
| 217 | + bindingSpecs: ["removed"], |
| 218 | + transformConfig: transformConfig as never, |
| 219 | + }), |
| 220 | + ).resolves.toMatchObject({ status: "error", reason: "invalid-bindings" }); |
| 221 | + expect(mocks.parseBindingSpecs).toHaveBeenCalledOnce(); |
| 222 | + expect(mocks.ensureAgentWorkspace).not.toHaveBeenCalled(); |
| 223 | + }); |
| 224 | +}); |
0 commit comments