File size: 5,007 Bytes
34810d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { Command } from "commander";
import { expect, it, vi } from "vitest";

const loaded = vi.hoisted(() => {
  const modules = new Set<string>();
  return {
    modules,
    mock(name: string, exportName: string) {
      modules.add(name);
      return {
        [exportName]: (capability: Command) => capability.command(name).description(name),
      };
    },
  };
});

vi.mock("../agents/auth-profiles.js", () => {
  throw new Error("Inference metadata must not import provider auth runtime");
});

vi.mock("./capability-cli/audio.js", () => loaded.mock("audio", "registerAudioCapabilityCommands"));
vi.mock("./capability-cli/embedding.js", () =>
  loaded.mock("embedding", "registerEmbeddingCapabilityCommands"),
);
vi.mock("./capability-cli/image.js", () => loaded.mock("image", "registerImageCapabilityCommands"));
vi.mock("./capability-cli/model.js", () => loaded.mock("model", "registerModelCapabilityCommands"));
vi.mock("./capability-cli/tts.js", () => loaded.mock("tts", "registerTtsCapabilityCommands"));
vi.mock("./capability-cli/video.js", () => loaded.mock("video", "registerVideoCapabilityCommands"));
vi.mock("./capability-cli/web.js", () => loaded.mock("web", "registerWebCapabilityCommands"));

it("keeps metadata and selected-domain help behind the inference import boundary", async () => {
  const { registerCapabilityCli } = await import("./capability-cli.js");
  for (const args of [
    ["infer", "list", "--json"],
    ["capability", "inspect", "--name", "image.generate", "--help"],
  ]) {
    const metadataProgram = new Command().enablePositionalOptions();
    await registerCapabilityCli(metadataProgram, ["node", "openclaw", ...args]);
    const capability = metadataProgram.commands.find((command) => command.name() === "infer");
    expect(capability?.commands.map((command) => command.name())).toEqual(["list", "inspect"]);
    expect(loaded.modules).toEqual(new Set());
  }

  await registerCapabilityCli(new Command(), [
    "node",
    "openclaw",
    "infer",
    "--log-level",
    "debug",
    "image",
    "providers",
  ]);

  expect(loaded.modules).toEqual(new Set(["image"]));
});

const allDomains = ["model", "image", "audio", "tts", "video", "web", "embedding"];

it.each([
  { args: ["infer", "--help"], domains: allDomains },
  { args: ["infer", "--help", "image"], domains: allDomains },
  { args: ["infer", "--bad", "image", "--help"], domains: allDomains },
  { args: ["infer", "imgae"], domains: allDomains },
  { args: ["infer", "help", "image"], domains: allDomains },
  { args: ["completion", "--shell", "image"], domains: allDomains },
  { args: ["infer", "--", "image"], domains: allDomains },
  { args: ["infer", "--", "--log-level", "debug", "image"], domains: allDomains },
  { args: ["--", "infer", "image", "providers"], domains: ["image"] },
  { args: ["--", "infer", "--log-level", "debug", "image"], domains: allDomains },
  { args: ["--profile", "image", "infer", "list", "--help"], domains: [] },
  { args: ["infer", "--log-level", "debug", "list", "--json"], domains: [] },
  { args: ["infer", "--log-level", "debug", "image", "providers"], domains: ["image"] },
  { args: ["capability", "--log-level=debug", "image", "--help"], domains: ["image"] },
  { args: ["infer", "--log-level", "--help", "image"], domains: allDomains },
  { args: ["infer", "--log-level=", "image", "--help"], domains: allDomains },
  { args: ["infer", "--log-level", "debug", "--help", "image"], domains: allDomains },
  { args: ["capability", "image", "--help"], domains: ["image"] },
  { args: ["infer", "image", "providers", "--json"], domains: ["image"] },
  { args: ["infer", "model", "run", "--prompt", "--help"], domains: ["model"] },
])("preserves the command inventory for $args", async ({ args, domains }) => {
  const { registerCapabilityCli } = await import("./capability-cli.js");
  const program = new Command().enablePositionalOptions();
  await registerCapabilityCli(program, ["node", "openclaw", ...args]);
  const capability = program.commands.find((command) => command.name() === "infer");
  expect(capability?.commands.map((command) => command.name())).toEqual([
    "list",
    "inspect",
    ...domains,
  ]);
});

it.each([
  ["--help", "image"],
  ["--bad", "image", "--help"],
  ["--log-level", "--help", "image"],
  ["--log-level=", "image", "--help"],
])("prints complete parent help when options precede the domain: %j", async (...args) => {
  const { registerCapabilityCli } = await import("./capability-cli.js");
  let output = "";
  const program = new Command()
    .name("openclaw")
    .enablePositionalOptions()
    .exitOverride()
    .configureOutput({ writeOut: (value) => (output += value) });
  await registerCapabilityCli(program, ["node", "openclaw", "infer", ...args]);
  await expect(program.parseAsync(["infer", ...args], { from: "user" })).rejects.toMatchObject({
    code: "commander.helpDisplayed",
    exitCode: 0,
  });
  for (const domain of allDomains) {
    expect(output).toContain(`${domain} `);
  }
});