Skip to content

Commit 433bb3f

Browse files
authored
fix(cli): make commands, completion, and JSON output reliable (#116033)
* fix(cli): make commands, completion, and JSON output reliable * fix(cli): reconcile completion coverage with current main * test(cli): keep test routing stable across isolation lanes
1 parent 4645d4a commit 433bb3f

70 files changed

Lines changed: 2789 additions & 551 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎extensions/browser/src/cli/browser-cli-actions-input/register.batch.test.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,23 @@ describe("browser action input batch command", () => {
160160
expect(getLastActionBody()).toMatchObject({ kind: "batch", actions: SAMPLE_ACTIONS });
161161
});
162162

163+
it("rejects conflicting inline and file actions before reading either source", async () => {
164+
const program = createActionInputProgram();
165+
166+
await expect(
167+
program.parseAsync(
168+
["browser", "batch", "--actions", "[]", "--actions-file", "/tmp/browser-actions.json"],
169+
{ from: "user" },
170+
),
171+
).rejects.toThrow("__exit__:1");
172+
173+
expect(getBrowserCliRuntimeCapture().runtimeErrors.join("\n")).toContain(
174+
"Specify only one of --actions or --actions-file",
175+
);
176+
expect(mocks.readActionsPayload).not.toHaveBeenCalled();
177+
expect(mocks.callBrowserRequest).not.toHaveBeenCalled();
178+
});
179+
163180
it("rejects malformed actions JSON before dispatch", async () => {
164181
mocks.readActionsPayload.mockResolvedValueOnce("NOT JSON {{{");
165182
const program = createActionInputProgram();

‎extensions/browser/src/cli/browser-cli-actions-input/register.batch.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ export function registerBrowserBatchCommands(
2828
.option("--target-id <id>", BROWSER_TAB_REFERENCE_HELP)
2929
.action(async (opts, cmd) => {
3030
const { parent, profile } = resolveBrowserActionContext(cmd, parentOpts);
31+
if (opts.actions !== undefined && opts.actionsFile !== undefined) {
32+
defaultRuntime.error(danger("Specify only one of --actions or --actions-file"));
33+
defaultRuntime.exit(1);
34+
return;
35+
}
3136
if (!opts.actions && !opts.actionsFile) {
3237
defaultRuntime.error(danger("Provide --actions, --actions-file, or --actions-file -"));
3338
defaultRuntime.exit(1);

‎extensions/browser/src/cli/browser-cli-actions-input/register.form-wait-eval.test.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,22 @@ describe("browser action input fill command", () => {
8484
);
8585
expect(mocks.callBrowserRequest).not.toHaveBeenCalled();
8686
});
87+
88+
it("rejects conflicting inline and file fields before dispatch", async () => {
89+
const program = createActionInputProgram();
90+
91+
await expect(
92+
program.parseAsync(
93+
["browser", "fill", "--fields", "[]", "--fields-file", "/tmp/browser-fields.json"],
94+
{ from: "user" },
95+
),
96+
).rejects.toThrow("__exit__:1");
97+
98+
expect(getBrowserCliRuntimeCapture().runtimeErrors.join("\n")).toContain(
99+
"Specify only one of --fields or --fields-file",
100+
);
101+
expect(mocks.callBrowserRequest).not.toHaveBeenCalled();
102+
});
87103
});
88104

89105
describe("browser action input wait command", () => {

‎extensions/browser/src/cli/browser-cli-actions-input/shared.test.ts‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Browser tests cover shared plugin behavior.
22
import { describe, expect, it } from "vitest";
3-
import { readFields } from "./shared.js";
3+
import { readActionsPayload, readFields } from "./shared.js";
44

55
describe("readFields", () => {
66
it.each([
@@ -38,4 +38,18 @@ describe("readFields", () => {
3838
it("throws descriptive error on empty fields", async () => {
3939
await expect(readFields({ fields: "" })).rejects.toThrow("fields are required");
4040
});
41+
42+
it("rejects conflicting inline and file form fields", async () => {
43+
await expect(
44+
readFields({ fields: "[]", fieldsFile: "/tmp/openclaw-browser-fields.json" }),
45+
).rejects.toThrow("Specify only one of --fields or --fields-file");
46+
});
47+
});
48+
49+
describe("readActionsPayload", () => {
50+
it("rejects conflicting inline and file actions before reading the file", async () => {
51+
await expect(
52+
readActionsPayload({ actions: "[]", actionsFile: "/tmp/openclaw-browser-actions.json" }),
53+
).rejects.toThrow("Specify only one of --actions or --actions-file");
54+
});
4155
});

‎extensions/browser/src/cli/browser-cli-actions-input/shared.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ export async function readFields(opts: {
9393
fields?: string;
9494
fieldsFile?: string;
9595
}): Promise<BrowserFormField[]> {
96+
if (opts.fields !== undefined && opts.fieldsFile !== undefined) {
97+
throw new Error("Specify only one of --fields or --fields-file");
98+
}
9699
const payload = opts.fieldsFile ? await readFile(opts.fieldsFile) : (opts.fields ?? "");
97100
if (!payload.trim()) {
98101
throw new Error("fields are required");
@@ -152,6 +155,9 @@ export async function readActionsPayload(opts: {
152155
actions?: string;
153156
actionsFile?: string;
154157
}): Promise<string> {
158+
if (opts.actions !== undefined && opts.actionsFile !== undefined) {
159+
throw new Error("Specify only one of --actions or --actions-file");
160+
}
155161
if (opts.actionsFile) {
156162
return opts.actionsFile === "-" ? await readStdinText() : await readFile(opts.actionsFile);
157163
}

‎extensions/browser/src/cli/browser-cli-inspect.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,26 @@ describe("browser cli snapshot defaults", () => {
186186
expect(params?.query?.depth).toBe(0);
187187
});
188188

189+
it.each([
190+
{
191+
args: ["screenshot", "tab-1", "--type", "webp"],
192+
error: "Invalid --type: expected png or jpeg",
193+
},
194+
{
195+
args: ["snapshot", "--format", "html"],
196+
error: "Invalid --format: expected aria or ai",
197+
},
198+
{
199+
args: ["snapshot", "--mode", "full"],
200+
error: "Invalid --mode: expected efficient",
201+
},
202+
])("rejects unsupported inspect option values before dispatch", async ({ args, error }) => {
203+
await expect(runBrowserInspect(args)).rejects.toThrow("__exit__:1");
204+
205+
expect(runtime.error.mock.calls.at(-1)?.[0]).toContain(error);
206+
expect(sharedMocks.callBrowserRequest).not.toHaveBeenCalled();
207+
});
208+
189209
it("sends screenshot request with trimmed target id and jpeg type", async () => {
190210
const params = await runBrowserInspect(["screenshot", " tab-1 ", "--type", "jpeg"], true);
191211
expect(params?.path).toBe("/screenshot");

‎extensions/browser/src/cli/browser-cli-inspect.ts‎

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,19 @@ function parseOptionalIntegerOption(
3939
return parsed;
4040
}
4141

42+
function parseBrowserChoiceOption<const T extends string>(
43+
value: string,
44+
label: string,
45+
choices: readonly T[],
46+
): T | undefined {
47+
if ((choices as readonly string[]).includes(value)) {
48+
return value as T;
49+
}
50+
defaultRuntime.error(danger(`Invalid ${label}: expected ${choices.join(" or ")}`));
51+
defaultRuntime.exit(1);
52+
return undefined;
53+
}
54+
4255
/** Registers Browser screenshot and snapshot commands. */
4356
export function registerBrowserInspectCommands(
4457
browser: Command,
@@ -60,6 +73,10 @@ export function registerBrowserInspectCommands(
6073
.action(async (targetId: string | undefined, opts, cmd) => {
6174
const parent = parentOpts(cmd);
6275
const profile = parent?.browserProfile;
76+
const type = parseBrowserChoiceOption(opts.type, "--type", ["png", "jpeg"]);
77+
if (type === undefined) {
78+
return;
79+
}
6380
try {
6481
const result = await callBrowserRequest<{ path: string }>(
6582
parent,
@@ -73,7 +90,7 @@ export function registerBrowserInspectCommands(
7390
ref: normalizeOptionalString(opts.ref),
7491
element: normalizeOptionalString(opts.element),
7592
labels: Boolean(opts.labels),
76-
type: opts.type === "jpeg" ? "jpeg" : "png",
93+
type,
7794
},
7895
},
7996
{ timeoutMs: 20000 },
@@ -108,15 +125,26 @@ export function registerBrowserInspectCommands(
108125
.action(async (opts, cmd: Command) => {
109126
const parent = parentOpts(cmd);
110127
const profile = parent?.browserProfile;
111-
const format = opts.format === "aria" ? "aria" : "ai";
128+
const format = parseBrowserChoiceOption(opts.format, "--format", ["aria", "ai"]);
129+
if (format === undefined) {
130+
return;
131+
}
132+
const explicitMode =
133+
opts.mode === undefined
134+
? undefined
135+
: parseBrowserChoiceOption(opts.mode, "--mode", ["efficient"]);
136+
if (opts.mode !== undefined && explicitMode === undefined) {
137+
return;
138+
}
112139
const formatWasExplicit = cmd.getOptionValueSource("format") === "cli";
113140
const configMode =
114141
!formatWasExplicit &&
115142
format === "ai" &&
116143
getRuntimeConfig().browser?.snapshotDefaults?.mode === "efficient"
117144
? "efficient"
118145
: undefined;
119-
const mode = opts.efficient === true || opts.mode === "efficient" ? "efficient" : configMode;
146+
const mode =
147+
opts.efficient === true || explicitMode === "efficient" ? "efficient" : configMode;
120148
const limit = parseOptionalIntegerOption(opts.limit, "--limit", { min: 1 });
121149
const depth = parseOptionalIntegerOption(opts.depth, "--depth", { min: 0 });
122150
if (

‎extensions/browser/src/cli/browser-cli-manage.test.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,21 @@ describe("browser manage output", () => {
475475
);
476476
});
477477

478+
it("rejects unsupported profile drivers before creating a profile", async () => {
479+
const program = createBrowserManageProgram();
480+
481+
await expect(
482+
program.parseAsync(["browser", "create-profile", "--name", "test", "--driver", "chromium"], {
483+
from: "user",
484+
}),
485+
).rejects.toThrow("__exit__:1");
486+
487+
expect(getBrowserCliRuntimeCapture().runtimeErrors.at(-1)).toContain(
488+
"--driver must be openclaw or existing-session",
489+
);
490+
expect(getBrowserManageCallBrowserRequestMock()).not.toHaveBeenCalled();
491+
});
492+
478493
it("prints a readable browser doctor report", async () => {
479494
getBrowserManageCallBrowserRequestMock().mockImplementation(async (_opts: unknown, req) => {
480495
if (req.path === "/") {

‎extensions/browser/src/cli/browser-cli-manage.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -837,6 +837,13 @@ export function registerBrowserManageCommands(
837837
) => {
838838
const parent = parentOpts(cmd);
839839
await runBrowserCommand(async () => {
840+
if (
841+
opts.driver !== undefined &&
842+
opts.driver !== "openclaw" &&
843+
opts.driver !== "existing-session"
844+
) {
845+
throw new Error("--driver must be openclaw or existing-session");
846+
}
840847
const result = await callBrowserRequest<BrowserCreateProfileResult>(
841848
parent,
842849
{

‎extensions/memory-lancedb/memory-cli.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,26 @@ describe("memory-lancedb CLI embedding lifecycle", () => {
6969
expect(harness.close).toHaveBeenCalledTimes(1);
7070
});
7171

72+
it("rejects an invalid limit before generating an embedding", async () => {
73+
const harness = createHarness();
74+
75+
await expect(
76+
harness.program.parseAsync([
77+
"node",
78+
"openclaw",
79+
"ltm",
80+
"search",
81+
"hello",
82+
"--limit",
83+
"5items",
84+
]),
85+
).rejects.toThrow("--limit must be a positive integer");
86+
87+
expect(harness.embed).not.toHaveBeenCalled();
88+
expect(harness.search).not.toHaveBeenCalled();
89+
expect(harness.close).toHaveBeenCalledTimes(1);
90+
});
91+
7292
it("preserves a falsy search rejection over cleanup failure", async () => {
7393
const harness = createHarness({
7494
embedError: null,

0 commit comments

Comments
 (0)