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

cliArgs.test.ts

69 lines2.2 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("parses --key=value mixed with boolean flags", () => {
44 expect(parseCliArgs("--chrome --model=claude-sonnet-4-6 --debug")).toEqual({
45 flags: { chrome: null, model: "claude-sonnet-4-6", debug: null },
46 positionals: [],
47 });
48 });
49​
50 it("collects positionals mixed with flags (argv array)", () => {
51 expect(parseCliArgs(["1.2.3", "--root", "/path", "--github-output"])).toEqual({
52 flags: { root: "/path", "github-output": null },
53 positionals: ["1.2.3"],
54 });
55 });
56​
57 it("ignores bare -- with no flag name", () => {
58 expect(parseCliArgs("--")).toEqual({ flags: {}, positionals: [] });
59 });
60​
61 it("boolean flag does not consume next token as value", () => {
62 expect(parseCliArgs(["--github-output", "1.2.3"], { booleanFlags: ["github-output"] })).toEqual(
63 {
64 flags: { "github-output": null },
65 positionals: ["1.2.3"],
66 },
67 );
68 });
69});