File size: 1,521 Bytes
f778c12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { CommanderError } from "commander";
import { describe, expect, it, vi } from "vitest";
import { OpenClawCommand } from "./openclaw-command.js";
import { registerLazyCommand } from "./register-lazy-command.js";

describe("lazy command help", () => {
  it.each(["--help", "-h"])("loads nested leaf options without executing for %s", async (flag) => {
    const action = vi.fn();
    let stdout = "";
    const program = new OpenClawCommand().name("openclaw").exitOverride();
    program.configureOutput({
      writeOut: (text) => {
        stdout += text;
      },
    });
    const parent = program.command("browser");
    registerLazyCommand({
      program: parent,
      name: "inspect",
      description: "Lazy placeholder",
      register: () => {
        parent
          .command("inspect")
          .description("Inspect the selected target")
          .argument("<target>")
          .option("--details", "Include target details")
          .action(action);
      },
    });

    const error = await program
      .parseAsync(["browser", "inspect", flag], { from: "user" })
      .catch((cause: unknown) => cause);

    expect(error).toBeInstanceOf(CommanderError);
    expect(error).toMatchObject({ code: "commander.helpDisplayed", exitCode: 0 });
    expect(stdout).toContain("Inspect the selected target");
    expect(stdout).toContain("<target>");
    expect(stdout).toContain("--details");
    expect(stdout).not.toContain("Lazy placeholder");
    expect(action).not.toHaveBeenCalled();
  });
});