Skip to content

Commit 30f552d

Browse files
committed
chore: added tests
1 parent ce2b486 commit 30f552d

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

server/__tests__/args-parsing.test.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,47 @@ describe("/config endpoint: JSON array → shell-quoted string for client UI", (
9797
const parsed = shellParseArgs(result) as string[];
9898
expect(parsed).toEqual(["--message", 'say "hello"']);
9999
});
100+
101+
it("handles empty string element", () => {
102+
const rawArgs = JSON.stringify(["--flag", ""]);
103+
const result = processArgsForClient(rawArgs);
104+
const parsed = shellParseArgs(result) as string[];
105+
expect(parsed).toEqual(["--flag", ""]);
106+
});
107+
108+
it("handles arg with newline", () => {
109+
const rawArgs = JSON.stringify(["--msg", "line1\nline2"]);
110+
const result = processArgsForClient(rawArgs);
111+
const parsed = shellParseArgs(result) as string[];
112+
expect(parsed).toEqual(["--msg", "line1\nline2"]);
113+
});
114+
115+
it("handles arg with backslashes", () => {
116+
const rawArgs = JSON.stringify(["--path", "C:\\Users\\foo"]);
117+
const result = processArgsForClient(rawArgs);
118+
const parsed = shellParseArgs(result) as string[];
119+
expect(parsed).toEqual(["--path", "C:\\Users\\foo"]);
120+
});
121+
122+
it("handles arg that looks like a JSON object", () => {
123+
// e.g. --config '{"key":"val"}' — must not be mistaken for an outer JSON array
124+
const rawArgs = JSON.stringify(["--config", '{"key":"val"}']);
125+
const result = processArgsForClient(rawArgs);
126+
const parsed = shellParseArgs(result) as string[];
127+
expect(parsed).toEqual(["--config", '{"key":"val"}']);
128+
});
129+
130+
it("handles arg that looks like a JSON array", () => {
131+
const rawArgs = JSON.stringify(["--list", '["a","b"]']);
132+
const result = processArgsForClient(rawArgs);
133+
const parsed = shellParseArgs(result) as string[];
134+
expect(parsed).toEqual(["--list", '["a","b"]']);
135+
});
136+
137+
it("handles single-character flags", () => {
138+
const rawArgs = JSON.stringify(["-v", "-x"]);
139+
expect(processArgsForClient(rawArgs)).toBe("-v -x");
140+
});
100141
});
101142

102143
describe("createTransport: args string → parsed args array", () => {
@@ -122,6 +163,22 @@ describe("createTransport: args string → parsed args array", () => {
122163
it("handles empty string", () => {
123164
expect(parseArgsForTransport("")).toEqual([]);
124165
});
166+
167+
it("does not misinterpret a JSON-object arg as the outer container", () => {
168+
// An arg value that is itself a JSON object should not be parsed as the array
169+
const jsonArgs = JSON.stringify(["--config", '{"key":"val"}']);
170+
const result = parseArgsForTransport(jsonArgs);
171+
expect(result).toEqual(["--config", '{"key":"val"}']);
172+
});
173+
174+
it("does not misinterpret a non-array JSON value as an args array", () => {
175+
// If someone passes a plain JSON string (not an array), fall back to shellParseArgs
176+
const notAnArray = '"just a string"';
177+
const result = parseArgsForTransport(notAnArray);
178+
// JSON.parse gives a string, not an array → falls back to shellParseArgs
179+
// shellParseArgs on a quoted string returns the unquoted value
180+
expect(result).toEqual(["just a string"]);
181+
});
125182
});
126183

127184
describe("full round-trip: start.js → /config → createTransport", () => {

0 commit comments

Comments
 (0)