Files
openclaw/extensions/browser/src/cli/browser-cli-actions-input/shared.test.ts
lsr911 e924f30bf5 fix(browser): guard readFields JSON.parse against malformed CLI input (#98861)
* fix(browser): guard readFields JSON.parse against malformed user input

Wraps JSON.parse(payload) in readFields() with try/catch, throwing a
descriptive Error when CLI --fields or --fields-file input contains
malformed JSON instead of raw SyntaxError.

8/8 proof assertions pass using standalone script that imports and
calls the real exported readFields() with malformed field strings.

Co-Authored-By: Claude <noreply@anthropic.com>
Signed-off-by: lsr911 <liao.shirong@xydigit.com>

* fix(browser): add vitest malformed JSON tests, remove standalone proof

ClawSweeper feedback: added 2 vitest tests (malformed JSON, empty fields)
to the existing shared.test.ts, removed standalone proof script.

Co-Authored-By: Claude <noreply@anthropic.com>
Signed-off-by: lsr911 <liao.shirong@xydigit.com>

* fix(browser): cover malformed fields at CLI boundary

---------

Signed-off-by: lsr911 <liao.shirong@xydigit.com>
Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: Peter Steinberger <steipete@gmail.com>
2026-07-06 23:14:00 +01:00

42 lines
1.3 KiB
TypeScript

// Browser tests cover shared plugin behavior.
import { describe, expect, it } from "vitest";
import { readFields } from "./shared.js";
describe("readFields", () => {
it.each([
{
name: "keeps explicit type",
fields: '[{"ref":"6","type":"textbox","value":"hello"}]',
expected: [{ ref: "6", type: "textbox", value: "hello" }],
},
{
name: "defaults missing type to text",
fields: '[{"ref":"7","value":"world"}]',
expected: [{ ref: "7", type: "text", value: "world" }],
},
{
name: "defaults blank type to text",
fields: '[{"ref":"8","type":" ","value":"blank"}]',
expected: [{ ref: "8", type: "text", value: "blank" }],
},
])("$name", async ({ fields, expected }) => {
await expect(readFields({ fields })).resolves.toEqual(expected);
});
it("requires ref", async () => {
await expect(readFields({ fields: '[{"type":"textbox","value":"world"}]' })).rejects.toThrow(
"fields[0] must include ref",
);
});
it("throws descriptive error on malformed JSON", async () => {
await expect(readFields({ fields: "NOT JSON {{{" })).rejects.toThrow(
"fields must be valid JSON.",
);
});
it("throws descriptive error on empty fields", async () => {
await expect(readFields({ fields: "" })).rejects.toThrow("fields are required");
});
});