The best way to control Claude Code, Codex and more from any computer or phone

main
ts 135 lines 4.35 KB
Raw
1 import { describe, expect, it } from "vite-plus/test";
2
3 import { parseCliArgs, tokenizeCliArgs } from "./cliArgs.ts";
4
5 describe("tokenizeCliArgs", () => {
6 it("preserves escaped double quotes outside quoted values", () => {
7 expect(tokenizeCliArgs(String.raw`--config model=\"gpt-6.1-sol\" --strict-config`)).toEqual([
8 "--config",
9 'model="gpt-6.1-sol"',
10 "--strict-config",
11 ]);
12 });
13
14 it("preserves escaped single quotes outside quoted values", () => {
15 expect(tokenizeCliArgs(String.raw`--append-system-prompt don\'t --verbose`)).toEqual([
16 "--append-system-prompt",
17 "don't",
18 "--verbose",
19 ]);
20 });
21
22 it("preserves quoted values and escaped spaces", () => {
23 expect(
24 tokenizeCliArgs(
25 String.raw`--config model="gpt 5" --enable foo\ bar --config=profile='work profile'`,
26 ),
27 ).toEqual(["--config", "model=gpt 5", "--enable", "foo bar", "--config=profile=work profile"]);
28
29 for (const whitespace of [" ", "\t"]) {
30 const args = `--add-dir /tmp/project\\${whitespace}`;
31 expect(tokenizeCliArgs(args)).toEqual(["--add-dir", `/tmp/project${whitespace}`]);
32 expect(tokenizeCliArgs(`${args} --verbose`)).toEqual([
33 "--add-dir",
34 `/tmp/project${whitespace}`,
35 "--verbose",
36 ]);
37 }
38
39 for (const whitespace of ["\u00a0", "\u2003", "\u2028", "\u2029"]) {
40 const path = `/tmp/project${whitespace}name`;
41 expect(tokenizeCliArgs(`--add-dir ${path} --verbose`)).toEqual([
42 "--add-dir",
43 path,
44 "--verbose",
45 ]);
46 }
47 });
48
49 it("ignores unescaped surrounding whitespace", () => {
50 expect(tokenizeCliArgs(" \t\n")).toEqual([]);
51 expect(tokenizeCliArgs(" \t--add-dir /tmp/project \t\n")).toEqual([
52 "--add-dir",
53 "/tmp/project",
54 ]);
55 });
56
57 it("preserves literal backslashes in path values", () => {
58 expect(
59 tokenizeCliArgs(String.raw`--config cacheDir=C:\Users\me --config "quoted=C:\Users\me"`),
60 ).toEqual([
61 "--config",
62 String.raw`cacheDir=C:\Users\me`,
63 "--config",
64 String.raw`quoted=C:\Users\me`,
65 ]);
66 });
67 });
68
69 describe("parseCliArgs", () => {
70 it("parses --key=value mixed with boolean flags", () => {
71 expect(parseCliArgs("--chrome --model=claude-sonnet-4-6 --debug")).toEqual({
72 flags: { chrome: null, model: "claude-sonnet-4-6", debug: null },
73 positionals: [],
74 });
75 });
76
77 it("collects positionals mixed with flags (argv array)", () => {
78 expect(parseCliArgs(["1.2.3", "--root", "/path", "--github-output"])).toEqual({
79 flags: { root: "/path", "github-output": null },
80 positionals: ["1.2.3"],
81 });
82 });
83
84 it("accepts a trailing -- without operands", () => {
85 expect(parseCliArgs("--")).toEqual({ flags: {}, positionals: [] });
86 });
87
88 it.each([
89 { args: "--effort low -- --effort high --chrome" },
90 { args: ["--effort", "low", "--", "--effort", "high", "--chrome"] },
91 ])("ends option parsing at -- for $args", ({ args }) => {
92 expect(parseCliArgs(args)).toEqual({
93 flags: { effort: "low" },
94 positionals: ["--effort", "high", "--chrome"],
95 });
96 });
97
98 it("preserves all operands after --, including empty strings and another --", () => {
99 expect(parseCliArgs(["--", "", "--", "--model=example", "-x"])).toEqual({
100 flags: {},
101 positionals: ["", "--", "--model=example", "-x"],
102 });
103 });
104
105 it("ends option parsing with explicit boolean flags too", () => {
106 expect(parseCliArgs(["--chrome", "--", "--verbose"], { booleanFlags: ["chrome"] })).toEqual({
107 flags: { chrome: null },
108 positionals: ["--verbose"],
109 });
110 });
111
112 it("keeps -- inside an equals-form option value", () => {
113 expect(parseCliArgs("--append-system-prompt=-- --chrome")).toEqual({
114 flags: { "append-system-prompt": "--", chrome: null },
115 positionals: [],
116 });
117 });
118
119 it("value flag consumes a next token that starts with --", () => {
120 expect(
121 parseCliArgs(["--append-system-prompt", "-- text", "--chrome"], {
122 valueFlags: ["append-system-prompt"],
123 }),
124 ).toEqual({ flags: { "append-system-prompt": "-- text", chrome: null }, positionals: [] });
125 });
126
127 it("boolean flag does not consume next token as value", () => {
128 expect(parseCliArgs(["--github-output", "1.2.3"], { booleanFlags: ["github-output"] })).toEqual(
129 {
130 flags: { "github-output": null },
131 positionals: ["1.2.3"],
132 },
133 );
134 });
135 });