Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

cliArgs.test.ts

185 lines5.6 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import { parseCliArgs, tokenizeCliArgs } from "./cliArgs.ts";
4​
5describe("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​
30 it("preserves literal backslashes in path values", () => {
31 expect(
32 tokenizeCliArgs(String.raw`--config cacheDir=C:\Users\me --config "quoted=C:\Users\me"`),
33 ).toEqual([
34 "--config",
35 String.raw`cacheDir=C:\Users\me`,
36 "--config",
37 String.raw`quoted=C:\Users\me`,
38 ]);
39 });
40});
41​
42describe("parseCliArgs", () => {
43 it("keeps escaped apostrophes in flag values without consuming the next flag", () => {
44 expect(parseCliArgs(String.raw`--append-system-prompt don\'t --verbose`)).toEqual({
45 flags: { "append-system-prompt": "don't", verbose: null },
46 positionals: [],
47 });
48 });
49​
50 it("returns empty result for empty string", () => {
51 expect(parseCliArgs("")).toEqual({ flags: {}, positionals: [] });
52 });
53​
54 it("returns empty result for whitespace-only string", () => {
55 expect(parseCliArgs(" ")).toEqual({ flags: {}, positionals: [] });
56 });
57​
58 it("returns empty result for empty array", () => {
59 expect(parseCliArgs([])).toEqual({ flags: {}, positionals: [] });
60 });
61​
62 it("parses --chrome boolean flag", () => {
63 expect(parseCliArgs("--chrome")).toEqual({
64 flags: { chrome: null },
65 positionals: [],
66 });
67 });
68​
69 it("parses --chrome with --verbose", () => {
70 expect(parseCliArgs("--chrome --verbose")).toEqual({
71 flags: { chrome: null, verbose: null },
72 positionals: [],
73 });
74 });
75​
76 it("parses --effort with a value", () => {
77 expect(parseCliArgs("--effort high")).toEqual({
78 flags: { effort: "high" },
79 positionals: [],
80 });
81 });
82​
83 it("parses --chrome --effort high --debug", () => {
84 expect(parseCliArgs("--chrome --effort high --debug")).toEqual({
85 flags: { chrome: null, effort: "high", debug: null },
86 positionals: [],
87 });
88 });
89​
90 it("parses --model with full model name", () => {
91 expect(parseCliArgs("--model claude-sonnet-4-6")).toEqual({
92 flags: { model: "claude-sonnet-4-6" },
93 positionals: [],
94 });
95 });
96​
97 it("parses --append-system-prompt with value and --chrome", () => {
98 expect(parseCliArgs("--append-system-prompt always-think-step-by-step --chrome")).toEqual({
99 flags: { "append-system-prompt": "always-think-step-by-step", chrome: null },
100 positionals: [],
101 });
102 });
103​
104 it("parses quoted --append-system-prompt with value and --chrome", () => {
105 expect(parseCliArgs(`--append-system-prompt "always think step by step" --chrome`)).toEqual({
106 flags: { "append-system-prompt": "always think step by step", chrome: null },
107 positionals: [],
108 });
109 });
110​
111 it("parses --max-budget-usd with numeric value", () => {
112 expect(parseCliArgs("--chrome --max-budget-usd 5.00")).toEqual({
113 flags: { chrome: null, "max-budget-usd": "5.00" },
114 positionals: [],
115 });
116 });
117​
118 it("parses --effort=high syntax", () => {
119 expect(parseCliArgs("--effort=high")).toEqual({
120 flags: { effort: "high" },
121 positionals: [],
122 });
123 });
124​
125 it("parses --key=value mixed with boolean flags", () => {
126 expect(parseCliArgs("--chrome --model=claude-sonnet-4-6 --debug")).toEqual({
127 flags: { chrome: null, model: "claude-sonnet-4-6", debug: null },
128 positionals: [],
129 });
130 });
131​
132 it("collects positional arguments", () => {
133 expect(parseCliArgs("1.2.3")).toEqual({
134 flags: {},
135 positionals: ["1.2.3"],
136 });
137 });
138​
139 it("collects positionals mixed with flags (argv array)", () => {
140 expect(parseCliArgs(["1.2.3", "--root", "/path", "--github-output"])).toEqual({
141 flags: { root: "/path", "github-output": null },
142 positionals: ["1.2.3"],
143 });
144 });
145​
146 it("handles extra whitespace between tokens", () => {
147 expect(parseCliArgs(" --chrome --verbose ")).toEqual({
148 flags: { chrome: null, verbose: null },
149 positionals: [],
150 });
151 });
152​
153 it("ignores bare -- with no flag name", () => {
154 expect(parseCliArgs("--")).toEqual({ flags: {}, positionals: [] });
155 });
156​
157 it("boolean flag does not consume next token as value", () => {
158 expect(parseCliArgs(["--github-output", "1.2.3"], { booleanFlags: ["github-output"] })).toEqual(
159 {
160 flags: { "github-output": null },
161 positionals: ["1.2.3"],
162 },
163 );
164 });
165​
166 it("non-boolean flag still consumes next token", () => {
167 expect(parseCliArgs(["--root", "/path", "1.2.3"], { booleanFlags: ["github-output"] })).toEqual(
168 {
169 flags: { root: "/path" },
170 positionals: ["1.2.3"],
171 },
172 );
173 });
174​
175 it("mixes boolean and value flags with positionals", () => {
176 expect(
177 parseCliArgs(["--github-output", "--root", "/path", "1.2.3"], {
178 booleanFlags: ["github-output"],
179 }),
180 ).toEqual({
181 flags: { "github-output": null, root: "/path" },
182 positionals: ["1.2.3"],
183 });
184 });
185});