File size: 17,364 Bytes
c4b6558 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 | import fs from "node:fs/promises";
import path from "node:path";
import { withTempHome } from "openclaw/plugin-sdk/test-env";
import { describe, expect, it } from "vitest";
import { runBuiltCli } from "./cli-json-stdout.test-support.js";
describe("cli json stdout contract", () => {
it.each([
{
name: "add without an interactive terminal in human mode",
args: ["agents", "add", "work"],
message:
"Agent creation needs an interactive TTY. Use `openclaw agents add <id> --non-interactive --workspace <dir>` for automation.",
human: true,
},
{
name: "add without an interactive terminal in JSON wizard mode",
args: ["agents", "add", "work", "--json"],
message:
"Agent creation needs an interactive TTY. Use `openclaw agents add <id> --non-interactive --workspace <dir>` for automation.",
},
{
name: "add without a workspace in human mode",
args: ["agents", "add", "work", "--non-interactive"],
message:
"Non-interactive agent creation requires --workspace. Re-run openclaw agents add <id> --workspace <path> or omit flags to use the wizard.",
human: true,
},
{
name: "add without a workspace in explicit non-interactive mode",
args: ["agents", "add", "work", "--non-interactive", "--json"],
message:
"Non-interactive agent creation requires --workspace. Re-run openclaw agents add <id> --workspace <path> or omit flags to use the wizard.",
},
{
name: "add without a workspace when a model selects automation",
args: ["agents", "add", "work", "--model", "openai/gpt-5.6-luna", "--json"],
message:
"Non-interactive agent creation requires --workspace. Re-run openclaw agents add <id> --workspace <path> or omit flags to use the wizard.",
},
{
name: "add without a workspace before its missing name",
args: ["agents", "add", "--non-interactive", "--json"],
message:
"Non-interactive agent creation requires --workspace. Re-run openclaw agents add <id> --workspace <path> or omit flags to use the wizard.",
},
{
name: "add without a name after a valid workspace",
args: ["agents", "add", "--workspace", "$WORKSPACE", "--json"],
message:
"Agent name is required in non-interactive mode. Run openclaw agents add <id> --workspace <path>.",
},
{
name: "add with an invalid agent id",
args: ["agents", "add", "агент✨", "--workspace", "$WORKSPACE", "--json"],
message:
'Agent name "агент✨" has no valid id characters. Use at least one letter a-z or digit.',
},
...["openclaw", "crestodian"].map((agentId) => ({
name: `add with reserved system-agent id ${agentId}`,
args: ["agents", "add", agentId, "--workspace", "$WORKSPACE", "--json"],
message: `"${agentId}" is reserved. Choose another name, or run openclaw agents list to inspect configured agents.`,
})),
{
name: "add with an already-configured agent",
args: ["agents", "add", "main", "--workspace", "$WORKSPACE", "--json"],
message: 'Agent "main" already exists.',
},
{
name: "add with a malformed binding",
args: ["agents", "add", "work", "--workspace", "$WORKSPACE", "--bind", "telegram:", "--json"],
message:
'Invalid binding "telegram:". Account id is empty. Use <channel>:<account>, for example telegram:default.',
},
{
name: "add with multiple malformed bindings in input order",
args: [
"agents",
"add",
"work",
"--workspace",
"$WORKSPACE",
"--bind",
"telegram:",
"--bind",
"telegram:work:extra",
"--json",
],
message: [
'Invalid binding "telegram:". Account id is empty. Use <channel>:<account>, for example telegram:default.',
'Invalid binding "telegram:work:extra". Account id cannot contain ":". Use <channel>:<account>, for example telegram:default.',
].join("\n"),
},
{
name: "add with an unknown binding channel",
args: [
"agents",
"add",
"work",
"--workspace",
"$WORKSPACE",
"--bind",
"definitely-not-a-channel",
"--json",
],
message:
'Unknown channel "definitely-not-a-channel". Run `openclaw channels list --all` to see configured and installable channels.',
},
{
name: "add with a normalized id before a malformed binding",
args: ["agents", "add", "Work", "--workspace", "$WORKSPACE", "--bind", "telegram:", "--json"],
message:
'Invalid binding "telegram:". Account id is empty. Use <channel>:<account>, for example telegram:default.',
},
{
name: "add without a workspace through dual-TTY finalization",
args: ["agents", "add", "work", "--non-interactive", "--json"],
message:
"Non-interactive agent creation requires --workspace. Re-run openclaw agents add <id> --workspace <path> or omit flags to use the wizard.",
tty: true,
},
{
name: "add with a malformed binding through dual-TTY finalization",
args: ["agents", "add", "work", "--workspace", "$WORKSPACE", "--bind", "telegram:", "--json"],
message:
'Invalid binding "telegram:". Account id is empty. Use <channel>:<account>, for example telegram:default.',
tty: true,
},
{
name: "bindings with an invalid agent",
args: ["agents", "bindings", "--agent", "агент✨", "--json"],
message: 'Agent "агент✨" not found. Run openclaw agents list to see configured agents.',
},
{
name: "bindings with an unknown agent",
args: ["agents", "bindings", "--json", "--agent", "ghost"],
message: 'Agent "ghost" not found. Run openclaw agents list to see configured agents.',
},
{
name: "bind with an invalid agent",
args: ["agents", "bind", "--agent", "агент✨", "--bind", "telegram", "--json"],
message: 'Agent "агент✨" not found. Run openclaw agents list to see configured agents.',
},
{
name: "bind with an unknown agent before missing bindings",
args: ["agents", "bind", "--json", "--agent", "ghost"],
message: 'Agent "ghost" not found. Run openclaw agents list to see configured agents.',
},
{
name: "bind without bindings",
args: ["agents", "bind", "--json"],
message: "Provide at least one --bind <channel[:accountId]>.",
},
{
name: "bind with only a blank binding",
args: ["agents", "bind", "--bind", " ", "--json"],
message: "Provide at least one --bind <channel[:accountId]>.",
},
{
name: "bind with multiple malformed bindings in input order",
args: ["agents", "bind", "--bind", "telegram:", "--bind", "telegram:work:extra", "--json"],
message: [
'Invalid binding "telegram:". Account id is empty. Use <channel>:<account>, for example telegram:default.',
'Invalid binding "telegram:work:extra". Account id cannot contain ":". Use <channel>:<account>, for example telegram:default.',
].join("\n"),
},
{
name: "bind with an unknown channel",
args: ["agents", "bind", "--json", "--bind", "definitely-not-a-channel"],
message:
'Unknown channel "definitely-not-a-channel". Run `openclaw channels list --all` to see configured and installable channels.',
},
{
name: "unbind with an invalid agent",
args: ["agents", "unbind", "--agent", "агент✨", "--all", "--json"],
message: 'Agent "агент✨" not found. Run openclaw agents list to see configured agents.',
},
{
name: "unbind with an unknown agent before incompatible options",
args: ["agents", "unbind", "--agent", "ghost", "--all", "--bind", "telegram", "--json"],
message: 'Agent "ghost" not found. Run openclaw agents list to see configured agents.',
},
{
name: "unbind without bindings",
args: ["agents", "unbind", "--json"],
message: "Provide at least one --bind <channel[:accountId]> or use --all.",
},
{
name: "unbind with a malformed binding",
args: ["agents", "unbind", "--bind", "telegram:work:extra", "--json"],
message:
'Invalid binding "telegram:work:extra". Account id cannot contain ":". Use <channel>:<account>, for example telegram:default.',
},
{
name: "unbind with incompatible options in human mode",
args: ["agents", "unbind", "--all", "--bind", "telegram"],
message: "Use either --all or --bind, not both.",
human: true,
},
{
name: "unbind with incompatible options in JSON mode",
args: ["agents", "unbind", "--all", "--bind", "telegram", "--json"],
message: "Use either --all or --bind, not both.",
},
{
name: "bind without bindings through dual-TTY finalization",
args: ["agents", "bind", "--json"],
message: "Provide at least one --bind <channel[:accountId]>.",
tty: true,
},
{
name: "set-identity with an unknown agent in human mode",
args: ["agents", "set-identity", "--agent", "ghost", "--name", "Ghost"],
message: 'Agent "ghost" not found. Create it with `openclaw agents add`.',
human: true,
},
{
name: "set-identity with an unknown agent in JSON mode",
args: ["agents", "set-identity", "--agent", "ghost", "--name", "Ghost", "--json"],
message: 'Agent "ghost" not found. Create it with `openclaw agents add`.',
},
{
name: "set-identity with an invalid agent before identity-file resolution",
args: ["agents", "set-identity", "--agent", "агент✨", "--from-identity", "--json"],
message: 'Agent "агент✨" not found. Create it with `openclaw agents add`.',
},
{
name: "set-identity with an unmatched workspace",
args: ["agents", "set-identity", "--workspace", "$WORKSPACE", "--name", "Ghost", "--json"],
message: "No agent workspace matches ~/workspace. Pass --agent to target a specific agent.",
},
{
name: "set-identity with a missing workspace identity file",
args: [
"agents",
"set-identity",
"--agent",
"main",
"--workspace",
"$WORKSPACE",
"--from-identity",
"--json",
],
message: "No identity data found in ~/workspace/IDENTITY.md.",
},
{
name: "set-identity with a missing explicit identity file",
args: [
"agents",
"set-identity",
"--agent",
"main",
"--identity-file",
"$WORKSPACE",
"--json",
],
message: "No identity data found in ~/workspace.",
},
{
name: "set-identity with an unknown agent through dual-TTY finalization",
args: ["agents", "set-identity", "--agent", "ghost", "--name", "Ghost", "--json"],
message: 'Agent "ghost" not found. Create it with `openclaw agents add`.',
tty: true,
},
])("renders agent management $name through the canonical failure owner", async (testCase) => {
await withTempHome(
async (tempHome) => {
const configPath = path.join(tempHome, "missing-openclaw.json");
const workspace = path.join(tempHome, "workspace");
const preload = `data:text/javascript,${encodeURIComponent(
'Object.defineProperty(process.stdout, "isTTY", { value: true, configurable: true }); Object.defineProperty(process.stderr, "isTTY", { value: true, configurable: true });',
)}`;
const args = testCase.args.map((argument) =>
argument === "$WORKSPACE" ? workspace : argument,
);
const result = runBuiltCli(tempHome, args, {
OPENCLAW_STATE_DIR: path.join(tempHome, "isolated-state"),
OPENCLAW_CONFIG_PATH: configPath,
...("tty" in testCase ? { NODE_OPTIONS: `--import=${preload}`, FORCE_COLOR: "1" } : {}),
});
expect(result.status, result.stderr).toBe(1);
if ("human" in testCase) {
expect(result.stdout).toBe("");
} else {
expect(result.stdout, result.stderr).not.toMatch(/[\u001B\u0007]/u);
expect(JSON.parse(result.stdout)).toEqual({
ok: false,
error: { type: "cli_error", message: testCase.message },
});
}
expect(result.stderr).toContain(testCase.message);
expect(result.stderr.split(testCase.message)).toHaveLength(2);
if ("tty" in testCase) {
expect(result.stderr).toContain("\u001B[?25h");
}
await expect(fs.access(configPath)).rejects.toMatchObject({ code: "ENOENT" });
await expect(fs.access(workspace)).rejects.toMatchObject({ code: "ENOENT" });
},
{ prefix: "openclaw-agent-management-json-failure-e2e-" },
);
});
it("leaves existing config and IDENTITY.md untouched when set-identity rejects an agent", async () => {
await withTempHome(
async (tempHome) => {
const configPath = path.join(tempHome, "openclaw.json");
const workspace = path.join(tempHome, "workspace");
const identityPath = path.join(workspace, "IDENTITY.md");
const originalConfig = `${JSON.stringify({
agents: { entries: { main: { workspace, identity: { name: "Original" } } } },
})}\n`;
const originalIdentity = "- Name: Original workspace identity\n";
await fs.mkdir(workspace, { recursive: true });
await fs.writeFile(configPath, originalConfig, "utf8");
await fs.writeFile(identityPath, originalIdentity, "utf8");
const result = runBuiltCli(
tempHome,
["agents", "set-identity", "--agent", "ghost", "--name", "Ghost", "--json"],
{
OPENCLAW_STATE_DIR: path.join(tempHome, "isolated-state"),
OPENCLAW_CONFIG_PATH: configPath,
},
);
expect(result.status, result.stderr).toBe(1);
expect(JSON.parse(result.stdout)).toEqual({
ok: false,
error: {
type: "cli_error",
message: 'Agent "ghost" not found. Create it with `openclaw agents add`.',
},
});
await expect(fs.readFile(configPath, "utf8")).resolves.toBe(originalConfig);
await expect(fs.readFile(identityPath, "utf8")).resolves.toBe(originalIdentity);
},
{ prefix: "openclaw-agent-identity-json-failure-e2e-" },
);
});
it.each([
{
name: "bindings list success",
args: ["agents", "bindings", "--json"],
payload: [],
},
{
name: "bind success",
args: ["agents", "bind", "--bind", "telegram:work", "--json"],
payload: {
agentId: "main",
added: ["telegram accountId=work"],
updated: [],
skipped: [],
conflicts: [],
},
writesConfig: true,
},
{
name: "unbind-all success",
args: ["agents", "unbind", "--all", "--json"],
payload: { agentId: "main", removed: [], missing: [], conflicts: [] },
},
{
name: "bind ownership conflict",
args: ["agents", "bind", "--agent", "main", "--bind", "telegram:work", "--json"],
payload: {
agentId: "main",
added: [],
updated: [],
skipped: [],
conflicts: ["telegram accountId=work (agent=ops)"],
},
conflict: true,
},
{
name: "unbind ownership conflict",
args: ["agents", "unbind", "--agent", "main", "--bind", "telegram:work", "--json"],
payload: {
agentId: "main",
removed: [],
missing: [],
conflicts: ["telegram accountId=work (agent=ops)"],
},
conflict: true,
},
])("preserves agent binding $name as its existing domain payload", async (testCase) => {
await withTempHome(
async (tempHome) => {
const configPath = path.join(tempHome, "openclaw.json");
const existingConfig = `${JSON.stringify({
agents: {
ownership: "explicit",
list: [
{ id: "main", workspace: path.join(tempHome, "main") },
{ id: "ops", workspace: path.join(tempHome, "ops") },
],
},
bindings: [
{ type: "route", agentId: "ops", match: { channel: "telegram", accountId: "work" } },
],
})}\n`;
if ("conflict" in testCase) {
await fs.writeFile(configPath, existingConfig, "utf8");
}
const result = runBuiltCli(tempHome, testCase.args, {
OPENCLAW_STATE_DIR: path.join(tempHome, "isolated-state"),
OPENCLAW_CONFIG_PATH: configPath,
});
expect(result.status, result.stderr).toBe("conflict" in testCase ? 1 : 0);
expect(result.stdout, result.stderr).not.toBe("");
expect(JSON.parse(result.stdout)).toEqual(testCase.payload);
if ("writesConfig" in testCase) {
await expect(fs.access(configPath)).resolves.toBeUndefined();
} else if ("conflict" in testCase) {
await expect(fs.readFile(configPath, "utf8")).resolves.toBe(existingConfig);
} else {
await expect(fs.access(configPath)).rejects.toMatchObject({ code: "ENOENT" });
}
},
{ prefix: "openclaw-agent-bindings-domain-payload-e2e-" },
);
});
});
|