Skip to content

Commit 29be144

Browse files
committed
fix(cli): reconcile completion coverage with current main
1 parent 0bdb8ce commit 29be144

6 files changed

Lines changed: 297 additions & 298 deletions
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
import { describe, expect, it } from "vitest";
2+
import { getCompletionScript } from "./completion-cli.js";
3+
import {
4+
createAliasedCompletionProgram,
5+
itWithFish,
6+
itWithPowerShell,
7+
runGeneratedBashCompletion,
8+
runGeneratedFishCompletion,
9+
runGeneratedPowerShellCompletion,
10+
} from "./completion-cli.test-support.js";
11+
12+
// Aliases are typeable commands, so every shell must preserve their nested command paths.
13+
describe("completion-cli command aliases", () => {
14+
itWithFish.each([
15+
["a canonical root command", "openclaw --profile work inf", "infer"],
16+
["an aliased root command", "openclaw --profile work cap", "capability"],
17+
["an inline profile and alias", "openclaw --profile=work cap", "capability"],
18+
["an alias-shaped profile value", "openclaw --profile capability cap", "capability"],
19+
["a repeated profile and alias", "openclaw --profile first --profile second cap", "capability"],
20+
])("completes real Fish root aliases after %s", (_name, commandLine, expected) => {
21+
expect(runGeneratedFishCompletion(createAliasedCompletionProgram(), commandLine)).toContain(
22+
expected,
23+
);
24+
});
25+
26+
it("completes root and nested aliases in zsh lists and dispatch", () => {
27+
const script = getCompletionScript("zsh", createAliasedCompletionProgram());
28+
29+
expect(script).toContain("'capability[Run inference]'");
30+
expect(script).toContain("(infer|capability) _openclaw_infer ;;");
31+
expect(script).toContain("'create[Add a job]'");
32+
expect(script).toContain("(add|create) _openclaw_cron_add ;;");
33+
});
34+
35+
it("completes root and nested aliases in bash command paths", () => {
36+
const script = getCompletionScript("bash", createAliasedCompletionProgram());
37+
38+
expect(script).toContain('opts="infer capability cron --profile"');
39+
expect(script).toContain('"infer"|"capability")');
40+
expect(script).toContain('"cron")');
41+
expect(script).toContain('opts="add create"');
42+
expect(script).toContain('"cron add"|"cron create")');
43+
expect(script).toContain('opts="--at"');
44+
});
45+
46+
it.skipIf(process.platform === "win32")("offers options after a nested alias in bash", () => {
47+
expect(
48+
runGeneratedBashCompletion(createAliasedCompletionProgram(), [
49+
"openclaw",
50+
"--profile",
51+
"work",
52+
"cron",
53+
"create",
54+
"--a",
55+
]),
56+
).toEqual(["--at"]);
57+
});
58+
59+
it("completes aliases and their subtrees in fish", () => {
60+
const script = getCompletionScript("fish", createAliasedCompletionProgram());
61+
62+
expect(script).toContain(
63+
'complete -c openclaw -n "__openclaw_command_path_matches -- --profile" -a "capability" -d \'Run inference\'',
64+
);
65+
expect(script).toContain(
66+
'complete -c openclaw -n "__openclaw_command_path_matches capability -- --profile" -a "embed" -d \'Embed text\'',
67+
);
68+
expect(script).toContain(
69+
'complete -c openclaw -n "__openclaw_command_path_matches cron -- --profile" -a "create" -d \'Add a job\'',
70+
);
71+
expect(script).toContain(
72+
"complete -c openclaw -n \"__openclaw_command_path_matches cron create -- --profile --at\" -l at -r -d 'Schedule time'",
73+
);
74+
});
75+
76+
itWithFish.each([
77+
["an aliased nested command", "openclaw cron create -"],
78+
["a canonical nested command", "openclaw cron add -"],
79+
["a global profile", "openclaw --profile work cron create -"],
80+
["an inline global profile", "openclaw --profile=work cron create -"],
81+
["repeated global profiles", "openclaw --profile first --profile second cron create -"],
82+
["an inherited global profile", "openclaw cron --profile work create -"],
83+
["a parent long option", "openclaw cron --timezone UTC create -"],
84+
["a parent short option", "openclaw cron -z UTC create -"],
85+
["an inline parent option", "openclaw cron --timezone=UTC create -"],
86+
["a parent boolean option", "openclaw cron --verbose create -"],
87+
])("keeps real Fish alias completions scoped after %s", (_name, commandLine) => {
88+
const program = createAliasedCompletionProgram();
89+
const cron = program.commands.find((command) => command.name() === "cron");
90+
if (!cron) {
91+
throw new Error("Cron command is unavailable");
92+
}
93+
cron.option("-z, --timezone <zone>", "Time zone").option("--verbose", "Verbose output");
94+
95+
expect(runGeneratedFishCompletion(program, commandLine)).toEqual(["--at"]);
96+
});
97+
98+
itWithFish.each([
99+
["an aliased positional argument", "openclaw cron create meeting -"],
100+
["a canonical positional argument", "openclaw cron add meeting -"],
101+
["a profiled positional argument", "openclaw --profile work cron create meeting -"],
102+
["a parent option and positional argument", "openclaw cron -z UTC create meeting -"],
103+
])("keeps real Fish alias options after %s", (_name, commandLine) => {
104+
const program = createAliasedCompletionProgram();
105+
const cron = program.commands.find((command) => command.name() === "cron");
106+
const add = cron?.commands.find((command) => command.name() === "add");
107+
if (!cron || !add) {
108+
throw new Error("Cron add command is unavailable");
109+
}
110+
cron.option("-z, --timezone <zone>", "Time zone");
111+
add.argument("[label...]", "Job label");
112+
113+
expect(runGeneratedFishCompletion(program, commandLine)).toEqual(["--at"]);
114+
});
115+
116+
it("completes aliases and alias command paths in PowerShell", () => {
117+
const script = getCompletionScript("powershell", createAliasedCompletionProgram());
118+
119+
expect(script).toContain("$completions = @('infer','capability','cron','--profile')");
120+
expect(script).toContain("if ($commandPath -eq 'capability') {");
121+
expect(script).toContain("if ($commandPath -eq 'cron create') {");
122+
});
123+
124+
it("tracks PowerShell command paths past inherited value-taking flags", () => {
125+
const script = getCompletionScript("powershell", createAliasedCompletionProgram());
126+
127+
expect(script).toContain("$valueOptions = @('--profile')");
128+
expect(script).toContain("switch ($candidatePath)");
129+
expect(script).toContain("'cron create'");
130+
expect(script).toContain("'--profile','--at'");
131+
});
132+
133+
itWithPowerShell.each([
134+
["a global option", "openclaw --profile work cron create --a"],
135+
["an inline global option", "openclaw --profile=work cron create --a"],
136+
["repeated global options", "openclaw --profile first --profile second cron create --a"],
137+
["an inherited option after the parent", "openclaw cron --profile work create --a"],
138+
["the canonical nested command", "openclaw --profile work cron add --a"],
139+
])("completes real PowerShell nested aliases after %s", (_name, commandLine) => {
140+
expect(runGeneratedPowerShellCompletion(createAliasedCompletionProgram(), commandLine)).toEqual(
141+
["--at"],
142+
);
143+
});
144+
});
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
import { spawnSync } from "node:child_process";
2+
import { existsSync } from "node:fs";
3+
import path from "node:path";
4+
import { Command } from "commander";
5+
import { expect, it } from "vitest";
6+
import { getCompletionScript } from "./completion-cli.js";
7+
import { quoteCliArg } from "./quote-cli-arg.js";
8+
9+
export function createAliasedCompletionProgram(): Command {
10+
const program = new Command();
11+
program.name("openclaw");
12+
program.option("--profile <name>", "Profile");
13+
const infer = program.command("infer").alias("capability").description("Run inference");
14+
infer.command("embed").description("Embed text").option("--model <id>", "Model id");
15+
const cron = program.command("cron").description("Cron commands");
16+
cron
17+
.command("add")
18+
.alias("create")
19+
.description("Add a job")
20+
.option("--at <time>", "Schedule time");
21+
return program;
22+
}
23+
24+
export function runGeneratedBashCompletion(program: Command, words: readonly string[]): string[] {
25+
const script = getCompletionScript("bash", program);
26+
const result = spawnSync(
27+
"bash",
28+
[
29+
"--noprofile",
30+
"--norc",
31+
"-c",
32+
`${script}
33+
COMP_WORDS=(${words.map(quoteCliArg).join(" ")})
34+
COMP_CWORD=${words.length - 1}
35+
_openclaw_completion
36+
printf '%s\\n' "\${COMPREPLY[@]}"
37+
`,
38+
],
39+
{ encoding: "utf8" },
40+
);
41+
42+
if (result.error) {
43+
throw result.error;
44+
}
45+
expect(result.stderr).toBe("");
46+
expect(result.status).toBe(0);
47+
return result.stdout.split("\n").filter(Boolean);
48+
}
49+
50+
function findFish(): string | null {
51+
const executable = process.platform === "win32" ? "fish.exe" : "fish";
52+
const candidates = (process.env.PATH ?? "")
53+
.split(path.delimiter)
54+
.filter(Boolean)
55+
.map((directory) => path.join(directory, executable));
56+
return candidates.find((candidate) => existsSync(candidate)) ?? null;
57+
}
58+
59+
const fishPath = findFish();
60+
export const itWithFish = fishPath ? it : it.skip;
61+
62+
export function runGeneratedFishCompletion(program: Command, commandLine: string): string[] {
63+
if (!fishPath) {
64+
throw new Error("Fish is unavailable");
65+
}
66+
67+
const script = getCompletionScript("fish", program);
68+
const quotedCommandLine = commandLine.replaceAll("'", "\\'");
69+
const result = spawnSync(
70+
fishPath,
71+
["--no-config", "--command", `${script}\ncomplete --do-complete '${quotedCommandLine}'`],
72+
{ encoding: "utf8", timeout: 15_000 },
73+
);
74+
75+
if (result.error) {
76+
throw result.error;
77+
}
78+
expect(result.stderr).toBe("");
79+
expect(result.status).toBe(0);
80+
return result.stdout
81+
.split(/\r?\n/)
82+
.filter(Boolean)
83+
.map((completion) => completion.split("\t")[0] ?? completion);
84+
}
85+
86+
function findPowerShell(): string | null {
87+
const executable = process.platform === "win32" ? "pwsh.exe" : "pwsh";
88+
const candidates = [
89+
process.env.OPENCLAW_TEST_PWSH,
90+
...(process.env.PATH ?? "")
91+
.split(path.delimiter)
92+
.filter(Boolean)
93+
.map((directory) => path.join(directory, executable)),
94+
];
95+
return (
96+
candidates.find((candidate): candidate is string =>
97+
Boolean(candidate && existsSync(candidate)),
98+
) ?? null
99+
);
100+
}
101+
102+
const powerShellPath = findPowerShell();
103+
export const itWithPowerShell = powerShellPath ? it : it.skip;
104+
105+
export function runGeneratedPowerShellCompletion(program: Command, commandLine: string): string[] {
106+
if (!powerShellPath) {
107+
throw new Error("PowerShell is unavailable");
108+
}
109+
110+
const script = getCompletionScript("powershell", program);
111+
const quotedCommandLine = commandLine.replaceAll("'", "''");
112+
const result = spawnSync(
113+
powerShellPath,
114+
[
115+
"-NoLogo",
116+
"-NoProfile",
117+
"-NonInteractive",
118+
"-Command",
119+
`${script}
120+
$line = '${quotedCommandLine}'
121+
[System.Management.Automation.CommandCompletion]::CompleteInput($line, $line.Length, $null).CompletionMatches | ForEach-Object { $_.CompletionText }
122+
`,
123+
],
124+
{ encoding: "utf8" },
125+
);
126+
127+
if (result.error) {
128+
throw result.error;
129+
}
130+
expect(result.stderr).toBe("");
131+
expect(result.status).toBe(0);
132+
return result.stdout.split(/\r?\n/).filter(Boolean);
133+
}

0 commit comments

Comments
 (0)