|
| 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 | +}); |
0 commit comments