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