File size: 10,252 Bytes
eb3f11e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
// Action reparse tests cover Commander action reparsing for nested CLI commands.
import { Command } from "commander";
import { describe, expect, it, vi } from "vitest";
import { reparseProgramFromActionCommand } from "./action-reparse.js";
import { registerLazyCommand } from "./register-lazy-command.js";

function setRawArgs(command: Command, rawArgs: string[]): void {
  (command as Command & { rawArgs: string[] }).rawArgs = rawArgs;
}

async function expectReparseArgv(params: {
  parent: Command;
  action: Command;
  argv: string[];
  expected: string[];
}): Promise<void> {
  let root = params.parent;
  while (root.parent) {
    root = root.parent;
  }
  setRawArgs(root, params.argv);
  const parseAsync = vi.spyOn(root, "parseAsync").mockResolvedValue(root);

  await reparseProgramFromActionCommand(params.parent, params.action);

  expect(parseAsync).toHaveBeenCalledWith(params.expected);
}

describe("reparseProgramFromActionCommand", () => {
  it.each([
    { args: ["--", "config", "get", "--help"] },
    { args: ["config", "--", "get", "--help"] },
    { args: ["config", "get", "--", "--help"] },
  ])("keeps literal flag-looking values through actual lazy reparse: $args", async ({ args }) => {
    const program = new Command().name("openclaw").enablePositionalOptions();
    const received: string[] = [];
    registerLazyCommand({
      program,
      name: "config",
      description: "Read config",
      register: () => {
        program
          .command("config")
          .command("get")
          .argument("<path>")
          .action((value: string) => {
            received.push(value);
          });
      },
    });
    await program.parseAsync(["node", "openclaw", ...args]);
    expect(received).toEqual(["--help"]);
  });

  it("uses root raw args and reparses the root for nested lazy commands", async () => {
    const root = new Command().name("openclaw");
    setRawArgs(root, ["node", "openclaw", "workspaces", "audit", "export", "--since", "1"]);
    const workspaces = root.command("workspaces");
    const audit = workspaces.command("audit");
    const exportCommand = audit.command("export");
    const parseAsync = vi.spyOn(root, "parseAsync").mockResolvedValue(root);
    const auditParseAsync = vi.spyOn(audit, "parseAsync");

    await reparseProgramFromActionCommand(audit, exportCommand);

    expect(parseAsync).toHaveBeenCalledWith([
      "node",
      "openclaw",
      "workspaces",
      "audit",
      "export",
      "--since",
      "1",
    ]);
    expect(auditParseAsync).not.toHaveBeenCalled();
  });

  it("hoists a trailing lazy-parent option before the loaded command", async () => {
    const root = new Command().name("openclaw");
    const browser = root.command("browser").option("--browser-profile <name>");
    const tabs = browser.command("tabs");
    await expectReparseArgv({
      parent: browser,
      action: tabs,
      argv: ["node", "openclaw", "browser", "tabs", "--browser-profile", "remote"],
      expected: ["node", "openclaw", "browser", "--browser-profile", "remote", "tabs"],
    });
  });

  it("hoists a lazy-parent short option with an attached required value", async () => {
    const root = new Command().name("openclaw");
    const browser = root.command("browser").option("-p, --browser-profile <name>");
    const tabs = browser.command("tabs");
    await expectReparseArgv({
      parent: browser,
      action: tabs,
      argv: ["node", "openclaw", "browser", "tabs", "-premote"],
      expected: ["node", "openclaw", "browser", "-premote", "tabs"],
    });
  });

  it("hoists a lazy-parent short option with an attached optional value", async () => {
    const root = new Command().name("openclaw");
    const browser = root.command("browser").option("-p, --browser-profile [name]");
    const tabs = browser.command("tabs");
    await expectReparseArgv({
      parent: browser,
      action: tabs,
      argv: ["node", "openclaw", "browser", "tabs", "-premote"],
      expected: ["node", "openclaw", "browser", "-premote", "tabs"],
    });
  });

  it("skips root option values that match the parent command name", async () => {
    const root = new Command().name("openclaw").option("--profile <name>");
    const browser = root.command("browser").option("--browser-profile <name>");
    const tabs = browser.command("tabs");
    await expectReparseArgv({
      parent: browser,
      action: tabs,
      argv: [
        "node",
        "openclaw",
        "--profile",
        "browser",
        "browser",
        "tabs",
        "--browser-profile",
        "remote",
      ],
      expected: [
        "node",
        "openclaw",
        "--profile",
        "browser",
        "browser",
        "--browser-profile",
        "remote",
        "tabs",
      ],
    });
  });

  it("skips an attached root option value that matches the parent command name", async () => {
    const root = new Command().name("openclaw").option("-p, --profile <name>");
    const browser = root.command("browser").option("--browser-profile <name>");
    const tabs = browser.command("tabs");
    await expectReparseArgv({
      parent: browser,
      action: tabs,
      argv: ["node", "openclaw", "-pbrowser", "browser", "tabs", "--browser-profile", "remote"],
      expected: ["node", "openclaw", "-pbrowser", "browser", "--browser-profile", "remote", "tabs"],
    });
  });

  it("hoists parent options after nested lazy commands", async () => {
    const root = new Command().name("openclaw");
    const browser = root.command("browser").option("--browser-profile <name>");
    const tab = browser.command("tab");
    tab.command("new");
    await expectReparseArgv({
      parent: browser,
      action: tab,
      argv: ["node", "openclaw", "browser", "tab", "new", "--browser-profile", "work"],
      expected: ["node", "openclaw", "browser", "--browser-profile", "work", "tab", "new"],
    });
  });

  it("leaves a child-owned option collision after the child command", async () => {
    const root = new Command().name("openclaw");
    const browser = root.command("browser").option("--json");
    const extension = browser.command("extension");
    extension.command("path");
    extension.command("pair").option("--json");
    const argv = ["node", "openclaw", "browser", "extension", "pair", "--json"];
    await expectReparseArgv({ parent: browser, action: extension, argv, expected: argv });
  });

  it("leaves a child-owned attached short option after the child command", async () => {
    const root = new Command().name("openclaw");
    const browser = root.command("browser").option("-p, --browser-profile <name>");
    const extension = browser.command("extension");
    extension.command("pair").option("-p, --pairing-profile <name>");
    const argv = ["node", "openclaw", "browser", "extension", "pair", "-premote"];
    await expectReparseArgv({ parent: browser, action: extension, argv, expected: argv });
  });

  it("preserves an unknown suffix after a child-owned boolean short flag", async () => {
    const root = new Command().name("openclaw");
    const browser = root.command("browser").option("-p, --browser-profile <name>");
    const extension = browser.command("extension");
    const pair = extension.command("pair").option("-p, --preview");

    const parsed = pair.parseOptions(["-pfoo"]);

    expect(parsed.unknown).toEqual(["-foo"]);
    expect(pair.opts()).toEqual({ preview: true });

    const argv = ["node", "openclaw", "browser", "extension", "pair", "-pfoo"];
    await expectReparseArgv({ parent: browser, action: extension, argv, expected: argv });
  });

  it.each([
    {
      label: "boolean flags",
      retryFlags: "-r, --retry",
      tokens: ["-pr"],
      expected: { preview: true, retry: true },
    },
    {
      label: "a required attached value",
      retryFlags: "-r, --retry <value>",
      tokens: ["-prremote"],
      expected: { preview: true, retry: "remote" },
    },
    {
      label: "a required separate value",
      retryFlags: "-r, --retry <value>",
      tokens: ["-pr", "remote"],
      expected: { preview: true, retry: "remote" },
    },
    {
      label: "an optional attached value",
      retryFlags: "-r, --retry [value]",
      tokens: ["-prremote"],
      expected: { preview: true, retry: "remote" },
    },
    {
      label: "an optional separate value",
      retryFlags: "-r, --retry [value]",
      tokens: ["-pr", "remote"],
      expected: { preview: true, retry: "remote" },
    },
  ] as const)(
    "preserves child-owned short groups with $label",
    async ({ retryFlags, tokens, expected }) => {
      const root = new Command().name("openclaw");
      const browser = root.command("browser").option("-p, --browser-profile <name>");
      const extension = browser.command("extension");
      const pair = extension.command("pair").option("-p, --preview").option(retryFlags);

      const parsed = pair.parseOptions([...tokens]);

      expect(parsed.unknown).toEqual([]);
      expect(pair.opts()).toEqual(expected);

      const argv = ["node", "openclaw", "browser", "extension", "pair", ...tokens];
      await expectReparseArgv({ parent: browser, action: extension, argv, expected: argv });
    },
  );

  it("hoists a parent option when only a sibling command owns the same flag", async () => {
    const root = new Command().name("openclaw");
    const browser = root.command("browser").option("--url <url>");
    const cookies = browser.command("cookies");
    cookies.command("list");
    cookies.command("set").option("--url <url>");
    await expectReparseArgv({
      parent: browser,
      action: cookies,
      argv: ["node", "openclaw", "browser", "cookies", "list", "--url", "ws://gateway"],
      expected: ["node", "openclaw", "browser", "--url", "ws://gateway", "cookies", "list"],
    });
  });

  it("keeps a missing parent option value after the loaded command", async () => {
    const root = new Command().name("openclaw");
    const browser = root.command("browser").option("--browser-profile <name>");
    const tabs = browser.command("tabs");
    const argv = ["node", "openclaw", "browser", "tabs", "--browser-profile"];
    await expectReparseArgv({ parent: browser, action: tabs, argv, expected: argv });
  });
});