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

cliArgs.test.ts

101 lines3.3 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import { parseCliArgs, tokenizeCliArgs } from "./cliArgs.ts";
4​
5describe("tokenizeCliArgs", () => {
6 it("preserves quoted values and escaped spaces", () => {
7 expect(
8 tokenizeCliArgs(
9 String.raw`--config model="gpt 5" --enable foo\ bar --config=profile='work profile'`,
10 ),
11 ).toEqual(["--config", "model=gpt 5", "--enable", "foo bar", "--config=profile=work profile"]);
12 });
13​
14 it("preserves literal backslashes in path values", () => {
15 expect(
16 tokenizeCliArgs(String.raw`--config cacheDir=C:\Users\me --config "quoted=C:\Users\me"`),
17 ).toEqual([
18 "--config",
19 String.raw`cacheDir=C:\Users\me`,
20 "--config",
21 String.raw`quoted=C:\Users\me`,
22 ]);
23 });
24});
25​
26describe("parseCliArgs", () => {
27 it.each([{ input: "" }, { input: " " }, { input: [] }, { input: "--" }])(
28 "returns an empty result for $input",
29 ({ input }) => {
30 expect(parseCliArgs(input)).toEqual({ flags: {}, positionals: [] });
31 },
32 );
33​
34 it.each([
35 ["--chrome", { chrome: null }],
36 ["--chrome --verbose", { chrome: null, verbose: null }],
37 ["--effort high", { effort: "high" }],
38 ["--chrome --effort high --debug", { chrome: null, effort: "high", debug: null }],
39 ["--model claude-sonnet-4-6", { model: "claude-sonnet-4-6" }],
40 [
41 "--append-system-prompt always-think-step-by-step --chrome",
42 { "append-system-prompt": "always-think-step-by-step", chrome: null },
43 ],
44 [
45 '--append-system-prompt "always think step by step" --chrome',
46 { "append-system-prompt": "always think step by step", chrome: null },
47 ],
48 ["--chrome --max-budget-usd 5.00", { chrome: null, "max-budget-usd": "5.00" }],
49 ["--effort=high", { effort: "high" }],
50 [
51 "--chrome --model=claude-sonnet-4-6 --debug",
52 { chrome: null, model: "claude-sonnet-4-6", debug: null },
53 ],
54 [" --chrome --verbose ", { chrome: null, verbose: null }],
55 ])("parses flags from %s", (input, flags) => {
56 expect(parseCliArgs(input)).toEqual({ flags, positionals: [] });
57 });
58​
59 it("collects positional arguments", () => {
60 expect(parseCliArgs("1.2.3")).toEqual({
61 flags: {},
62 positionals: ["1.2.3"],
63 });
64 });
65​
66 it("collects positionals mixed with flags (argv array)", () => {
67 expect(parseCliArgs(["1.2.3", "--root", "/path", "--github-output"])).toEqual({
68 flags: { root: "/path", "github-output": null },
69 positionals: ["1.2.3"],
70 });
71 });
72​
73 it("boolean flag does not consume next token as value", () => {
74 expect(parseCliArgs(["--github-output", "1.2.3"], { booleanFlags: ["github-output"] })).toEqual(
75 {
76 flags: { "github-output": null },
77 positionals: ["1.2.3"],
78 },
79 );
80 });
81​
82 it("non-boolean flag still consumes next token", () => {
83 expect(parseCliArgs(["--root", "/path", "1.2.3"], { booleanFlags: ["github-output"] })).toEqual(
84 {
85 flags: { root: "/path" },
86 positionals: ["1.2.3"],
87 },
88 );
89 });
90​
91 it("mixes boolean and value flags with positionals", () => {
92 expect(
93 parseCliArgs(["--github-output", "--root", "/path", "1.2.3"], {
94 booleanFlags: ["github-output"],
95 }),
96 ).toEqual({
97 flags: { "github-output": null, root: "/path" },
98 positionals: ["1.2.3"],
99 });
100 });
101});