File size: 16,005 Bytes
205131a | 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 429 430 431 | import { randomBytes } from "node:crypto";
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: "routed agents list", overrides: {} },
{ name: "Commander agents list", overrides: { OPENCLAW_DISABLE_ROUTE_FIRST: "1" } },
])("drains large invalid-config JSON for $name", async ({ overrides }) => {
await withTempHome(
async (tempHome) => {
const configPath = path.join(tempHome, "openclaw.json");
const agentIds = Array.from({ length: 16_384 }, (_, index) => `agent_${index}`);
const config = JSON.stringify({
agents: {
ownership: "explicit",
entries: Object.fromEntries(agentIds.map((id) => [id, { workspace: 42 }])),
},
});
await fs.writeFile(configPath, config);
const result = runBuiltCli(
tempHome,
["agents", "list", "--json"],
{
OPENCLAW_CONFIG_PATH: configPath,
OPENCLAW_STATE_DIR: path.join(tempHome, "state"),
...overrides,
},
{ inheritEnvironment: false },
);
expect(result.error).toBeUndefined();
expect(result.status, result.stderr).toBe(1);
expect(JSON.parse(result.stdout)).toEqual({
ok: false,
error: {
type: "cli_error",
message: expect.stringContaining("OpenClaw config is invalid:"),
},
issues: agentIds.map((id) => ({
path: `agents.entries.${id}.workspace`,
message: "Invalid input: expected string, received number",
})),
});
expect(Buffer.byteLength(result.stdout)).toBeGreaterThan(1024 * 1024);
expect(result.stderr).toBe("");
expect(await fs.readFile(configPath, "utf8")).toBe(config);
},
{ prefix: "openclaw-agents-large-invalid-config-json-" },
);
});
it.each([
{ name: "routed agents list", overrides: {} },
{ name: "Commander agents list", overrides: { OPENCLAW_DISABLE_ROUTE_FIRST: "1" } },
])("reports shared invalid config as JSON for $name", async ({ overrides }) => {
await withTempHome(
async (tempHome) => {
const configPath = path.join(tempHome, "openclaw.json");
const config = `${JSON.stringify({ gateway: { port: "invalid-port" } })}\n`;
await fs.writeFile(configPath, config);
const result = runBuiltCli(
tempHome,
["agents", "list", "--json"],
{
OPENCLAW_CONFIG_PATH: configPath,
OPENCLAW_STATE_DIR: path.join(tempHome, "state"),
...overrides,
},
{ inheritEnvironment: false },
);
expect(result.error).toBeUndefined();
expect(result.status, result.stderr).toBe(1);
expect(JSON.parse(result.stdout)).toEqual({
ok: false,
error: {
type: "cli_error",
message: expect.stringContaining("OpenClaw config is invalid:"),
},
issues: [{ path: "gateway.port", message: expect.stringContaining("expected number") }],
});
expect(result.stderr).toBe("");
expect(await fs.readFile(configPath, "utf8")).toBe(config);
},
{ prefix: "openclaw-agents-invalid-config-json-" },
);
});
it.each([
["memory", "status", "--json"],
["nodes", "canvas", "snapshot", "--json"],
])("reports invalid config before discovering plugin command %s", async (...args) => {
await withTempHome(
async (tempHome) => {
const configPath = path.join(tempHome, "openclaw.json");
const stateDir = path.join(tempHome, "state");
await fs.writeFile(configPath, JSON.stringify({ gateway: { port: "invalid-port" } }));
const result = runBuiltCli(
tempHome,
args,
{
OPENCLAW_CONFIG_PATH: configPath,
OPENCLAW_STATE_DIR: stateDir,
},
{ inheritEnvironment: false },
);
expect(result.error).toBeUndefined();
expect(result.status, result.stderr).toBe(1);
expect(JSON.parse(result.stdout)).toMatchObject({
ok: false,
error: { type: "cli_error", message: expect.stringContaining("Invalid config at") },
});
expect(result.stdout).toContain("gateway.port");
expect(result.stderr).toContain("openclaw doctor");
await expect(
fs.access(path.join(stateDir, "state", "openclaw.sqlite")),
).rejects.toMatchObject({ code: "ENOENT" });
},
{ prefix: "openclaw-plugin-invalid-config-" },
);
});
it.each([
{
name: "node identity",
args: ["node", "identity", "--json"],
overrides: {},
error: "no node device identity found",
},
{
name: "routed config get",
args: ["config", "get", "gateway.port", "--json"],
overrides: {},
},
{
name: "Commander config get",
args: ["config", "get", "gateway.port", "--json"],
overrides: { OPENCLAW_DISABLE_ROUTE_FIRST: "1" },
},
{
name: "Nix config get",
args: ["config", "get", "gateway.port", "--json"],
overrides: { OPENCLAW_NIX_MODE: "1" },
},
{ name: "config schema", args: ["config", "schema"], overrides: {} },
{
name: "Nix config schema",
args: ["config", "schema"],
overrides: { OPENCLAW_NIX_MODE: "1" },
},
{ name: "config validate", args: ["config", "validate", "--json"], overrides: {} },
{
name: "Nix config validate",
args: ["config", "validate", "--json"],
overrides: { OPENCLAW_NIX_MODE: "1" },
},
])("does not initialize shared SQLite for $name", async (testCase) => {
await withTempHome(
async (tempHome) => {
const stateDir = path.join(tempHome, "read-only-state");
const configPath = path.join(tempHome, "read-only-openclaw.json");
const config = `${JSON.stringify({
gateway: {
mode: "local",
port: 18789,
auth: { mode: "token", token: randomBytes(32).toString("hex") },
},
plugins: { enabled: false },
browser: { enabled: false },
discovery: { mdns: { mode: "off" } },
logging: { file: path.join(stateDir, "openclaw.log") },
})}\n`;
await fs.writeFile(configPath, config, "utf8");
const configBefore = await fs.stat(configPath);
const tmpDir = path.join(tempHome, "tmp");
await fs.mkdir(tmpDir);
const result = runBuiltCli(
tempHome,
testCase.args,
{
OPENCLAW_HOME: tempHome,
OPENCLAW_CONFIG_PATH: configPath,
OPENCLAW_STATE_DIR: stateDir,
OPENCLAW_TEST_FAST: undefined,
TMPDIR: tmpDir,
TMP: tmpDir,
TEMP: tmpDir,
PATH: path.dirname(process.execPath),
...testCase.overrides,
},
{ inheritEnvironment: false },
);
expect(result.error).toBeUndefined();
expect(result.status, result.stderr).toBe("error" in testCase ? 1 : 0);
if ("error" in testCase) {
expect(result.stderr).toContain(testCase.error);
expect(result.stdout).toBe("");
} else {
expect(() => JSON.parse(result.stdout)).not.toThrow();
}
await expect(
fs.access(path.join(stateDir, "state", "openclaw.sqlite")),
).rejects.toMatchObject({
code: "ENOENT",
});
expect((await fs.readFile(configPath, "utf8")) === config).toBe(true);
expect(await fs.stat(configPath)).toMatchObject({
ino: configBefore.ino,
mode: configBefore.mode,
mtimeMs: configBefore.mtimeMs,
ctimeMs: configBefore.ctimeMs,
});
},
{ prefix: "openclaw-read-only-config-e2e-" },
);
});
it.each([
{ name: "routed malformed config get", overrides: {} },
{
name: "Commander malformed config get",
overrides: { OPENCLAW_DISABLE_ROUTE_FIRST: "1" },
},
])("returns actionable JSON without creating state for $name", async (testCase) => {
await withTempHome(
async (tempHome) => {
const stateDir = path.join(tempHome, "read-only-state");
const configPath = path.join(tempHome, "read-only-openclaw.json");
await fs.writeFile(configPath, "{}\n", "utf8");
const result = runBuiltCli(
tempHome,
["config", "get", "gateway.__proto__.token", "--json"],
{
OPENCLAW_CONFIG_PATH: configPath,
OPENCLAW_STATE_DIR: stateDir,
...testCase.overrides,
},
);
expect(result.status, result.stderr).toBe(1);
expect(JSON.parse(result.stdout)).toMatchObject({
ok: false,
error: {
type: "cli_error",
message: expect.stringContaining("Invalid path segment: __proto__"),
},
});
expect(result.stderr).toBe("");
await expect(
fs.access(path.join(stateDir, "state", "openclaw.sqlite")),
).rejects.toMatchObject({
code: "ENOENT",
});
},
{ prefix: "openclaw-read-only-invalid-config-e2e-" },
);
});
it.each([
{ name: "routed invalid config get", overrides: {} },
{
name: "Commander invalid config get",
overrides: { OPENCLAW_DISABLE_ROUTE_FIRST: "1" },
},
])("reports invalid configuration as JSON without creating state for $name", async (testCase) => {
await withTempHome(
async (tempHome) => {
const stateDir = path.join(tempHome, "read-only-state");
const configPath = path.join(tempHome, "read-only-openclaw.json");
await fs.writeFile(
configPath,
`${JSON.stringify({ gateway: { bind: "not-a-supported-mode" } })}\n`,
"utf8",
);
const result = runBuiltCli(tempHome, ["config", "get", "gateway.port", "--json"], {
OPENCLAW_CONFIG_PATH: configPath,
OPENCLAW_STATE_DIR: stateDir,
...testCase.overrides,
});
expect(result.status, result.stderr).toBe(1);
expect(JSON.parse(result.stdout)).toMatchObject({
ok: false,
error: {
type: "cli_error",
message: expect.stringContaining("OpenClaw config is invalid"),
},
issues: expect.arrayContaining([
expect.objectContaining({ path: "gateway.bind", message: expect.any(String) }),
]),
});
expect(result.stderr).toBe("");
await expect(
fs.access(path.join(stateDir, "state", "openclaw.sqlite")),
).rejects.toMatchObject({
code: "ENOENT",
});
},
{ prefix: "openclaw-read-only-invalid-snapshot-e2e-" },
);
});
it.each([
{ name: "default service", inheritedProfile: undefined, inheritedStateName: ".openclaw" },
{ name: "named service", inheritedProfile: "main", inheritedStateName: ".openclaw-main" },
])("resolves the requested profile from inherited $name state", async (inherited) => {
await withTempHome(
async (tempHome) => {
const inheritedStateDir = path.join(tempHome, inherited.inheritedStateName);
const result = runBuiltCli(tempHome, ["--profile", "work", "config", "file"], {
OPENCLAW_PROFILE: inherited.inheritedProfile,
OPENCLAW_STATE_DIR: inheritedStateDir,
OPENCLAW_CONFIG_PATH: path.join(inheritedStateDir, "openclaw.json"),
});
expect(result.status, result.stderr).toBe(0);
expect(result.stdout.trim()).toBe(path.join(tempHome, ".openclaw-work", "openclaw.json"));
await expect(fs.access(path.join(tempHome, ".openclaw-work"))).rejects.toMatchObject({
code: "ENOENT",
});
},
{ prefix: "openclaw-profile-isolation-e2e-" },
);
});
it("keeps default-profile exec approvals untouched for a scratch-state config query", async () => {
await withTempHome(
async (tempHome) => {
const defaultStateDir = path.join(tempHome, ".openclaw");
const scratchStateDir = path.join(tempHome, "scratch-state");
const approvalsPath = path.join(defaultStateDir, "exec-approvals.json");
const approvals = '{"version":1,"approvals":{"demo":true}}\n';
await fs.mkdir(defaultStateDir, { recursive: true });
await fs.mkdir(scratchStateDir, { recursive: true });
await fs.writeFile(approvalsPath, approvals, "utf8");
const result = runBuiltCli(tempHome, ["config", "file"], {
OPENCLAW_STATE_DIR: scratchStateDir,
});
expect(result.status, result.stderr).toBe(0);
expect(result.stdout.trim()).toBe(path.join(scratchStateDir, "openclaw.json"));
await expect(fs.readFile(approvalsPath, "utf8")).resolves.toBe(approvals);
await expect(fs.access(`${approvalsPath}.migrated`)).rejects.toMatchObject({
code: "ENOENT",
});
await expect(
fs.access(path.join(scratchStateDir, "exec-approvals.json")),
).rejects.toMatchObject({ code: "ENOENT" });
await expect(
fs.access(path.join(scratchStateDir, "state", "openclaw.sqlite")),
).rejects.toMatchObject({ code: "ENOENT" });
},
{ prefix: "openclaw-read-only-state-e2e-" },
);
});
it("keeps representative success payload bytes unchanged", async () => {
await withTempHome(
async (tempHome) => {
const configPath = path.join(tempHome, "openclaw.json");
await fs.writeFile(configPath, '{"gateway":{"port":28789}}\n', "utf8");
const env = { OPENCLAW_CONFIG_PATH: configPath };
const getResult = runBuiltCli(tempHome, ["config", "get", "gateway.port", "--json"], env);
const validateResult = runBuiltCli(tempHome, ["config", "validate", "--json"], env);
expect(getResult.status, getResult.stderr).toBe(0);
expect(getResult.stdout).toBe("28789\n");
expect(validateResult.status, validateResult.stderr).toBe(0);
expect(validateResult.stdout).toBe(
`${JSON.stringify({ valid: true, path: configPath, warnings: [] })}\n`,
);
},
{ prefix: "openclaw-json-success-bytes-e2e-" },
);
});
it("keeps `config schema` stdout parseable at debug log level", async () => {
await withTempHome(
async (tempHome) => {
const result = runBuiltCli(tempHome, ["config", "schema"], {
OPENCLAW_LOG_LEVEL: "debug",
});
expect(result.status).toBe(0);
const parsed = JSON.parse(result.stdout) as {
properties?: Record<string, unknown>;
};
expect(parsed.properties?.$schema).toEqual({ type: "string" });
expect(result.stdout).not.toContain("possibly sensitive key found");
expect(result.stderr).not.toContain("possibly sensitive key found");
},
{ prefix: "openclaw-config-schema-json-e2e-" },
);
});
it("keeps `config validate --json` stdout parseable at debug log level", async () => {
await withTempHome(
async (tempHome) => {
const configPath = path.join(tempHome, "openclaw.json");
await fs.writeFile(configPath, "{}", "utf8");
const result = runBuiltCli(tempHome, ["config", "validate", "--json"], {
OPENCLAW_CONFIG_PATH: configPath,
OPENCLAW_LOG_LEVEL: "debug",
});
expect(result.status).toBe(0);
expect(JSON.parse(result.stdout)).toMatchObject({
valid: true,
path: configPath,
});
expect(result.stdout).not.toContain("possibly sensitive key found");
},
{ prefix: "openclaw-config-validate-json-e2e-" },
);
});
});
|