|
1 | 1 | // Error output tests cover program-level error display and exit messaging. |
| 2 | +import { CommanderError } from "commander"; |
2 | 3 | import { describe, expect, it } from "vitest"; |
| 4 | +import { |
| 5 | + getCommanderErrorCommandNames, |
| 6 | + getCommanderErrorCommandPath, |
| 7 | +} from "./commander-parse-facts.js"; |
3 | 8 | import { formatCliParseErrorOutput } from "./error-output.js"; |
| 9 | +import { OpenClawCommand } from "./openclaw-command.js"; |
| 10 | +import { registerLazyCommand } from "./register-lazy-command.js"; |
| 11 | + |
| 12 | +async function parseLazyGroupError(params: { |
| 13 | + argv: string[]; |
| 14 | + group: string; |
| 15 | + subcommands: Array<{ name: string; aliases?: string[] }>; |
| 16 | +}): Promise<{ error: CommanderError; output: string }> { |
| 17 | + const originalArgv = process.argv; |
| 18 | + process.argv = ["node", "openclaw", ...params.argv]; |
| 19 | + let output = ""; |
| 20 | + try { |
| 21 | + const program = new OpenClawCommand().name("openclaw").exitOverride(); |
| 22 | + program.configureOutput({ |
| 23 | + writeErr: (value) => { |
| 24 | + output += value; |
| 25 | + }, |
| 26 | + outputError: (value, write) => { |
| 27 | + write( |
| 28 | + formatCliParseErrorOutput(value, { |
| 29 | + argv: process.argv, |
| 30 | + commandPath: getCommanderErrorCommandPath(program), |
| 31 | + commandNames: getCommanderErrorCommandNames(program), |
| 32 | + }), |
| 33 | + ); |
| 34 | + }, |
| 35 | + }); |
| 36 | + registerLazyCommand({ |
| 37 | + program, |
| 38 | + name: params.group, |
| 39 | + description: `${params.group} commands`, |
| 40 | + register: () => { |
| 41 | + const group = program.command(params.group).action(() => {}); |
| 42 | + for (const subcommand of params.subcommands) { |
| 43 | + const command = group.command(subcommand.name).action(() => {}); |
| 44 | + for (const alias of subcommand.aliases ?? []) { |
| 45 | + command.alias(alias); |
| 46 | + } |
| 47 | + } |
| 48 | + }, |
| 49 | + }); |
| 50 | + |
| 51 | + const error = await program.parseAsync(process.argv).catch((cause: unknown) => cause); |
| 52 | + expect(error).toBeInstanceOf(CommanderError); |
| 53 | + return { error: error as CommanderError, output }; |
| 54 | + } finally { |
| 55 | + process.argv = originalArgv; |
| 56 | + } |
| 57 | +} |
4 | 58 |
|
5 | 59 | describe("formatCliParseErrorOutput", () => { |
6 | 60 | it("explains unknown commands with root help and plugin hints", () => { |
@@ -36,6 +90,43 @@ describe("formatCliParseErrorOutput", () => { |
36 | 90 | ); |
37 | 91 | }); |
38 | 92 |
|
| 93 | + it("reports an unmatched lazy subcommand and suggests a live child command", async () => { |
| 94 | + const { error, output } = await parseLazyGroupError({ |
| 95 | + argv: ["sessions", "lst"], |
| 96 | + group: "sessions", |
| 97 | + subcommands: [{ name: "list" }, { name: "cleanup" }], |
| 98 | + }); |
| 99 | + |
| 100 | + expect(error.code).toBe("commander.unknownCommand"); |
| 101 | + expect(output).toBe( |
| 102 | + 'OpenClaw sessions has no command "lst".\nDid you mean this?\n openclaw sessions list\nTry: openclaw sessions --help\nDocs: https://docs.openclaw.ai/cli\n', |
| 103 | + ); |
| 104 | + }); |
| 105 | + |
| 106 | + it("suggests aliases from the live child command tree", async () => { |
| 107 | + const { error, output } = await parseLazyGroupError({ |
| 108 | + argv: ["cron", "remov"], |
| 109 | + group: "cron", |
| 110 | + subcommands: [{ name: "rm", aliases: ["remove", "delete"] }, { name: "edit" }], |
| 111 | + }); |
| 112 | + |
| 113 | + expect(error.code).toBe("commander.unknownCommand"); |
| 114 | + expect(output).toContain("Did you mean this?\n openclaw cron remove\n"); |
| 115 | + }); |
| 116 | + |
| 117 | + it("keeps excess arguments on a matched lazy subcommand", async () => { |
| 118 | + const { error, output } = await parseLazyGroupError({ |
| 119 | + argv: ["sessions", "list", "extra1", "extra2"], |
| 120 | + group: "sessions", |
| 121 | + subcommands: [{ name: "list" }], |
| 122 | + }); |
| 123 | + |
| 124 | + expect(error.code).toBe("commander.excessArguments"); |
| 125 | + expect(output).toBe( |
| 126 | + "Too many arguments for this command.\nTry: openclaw sessions list --help\n", |
| 127 | + ); |
| 128 | + }); |
| 129 | + |
39 | 130 | it("suggests close known commands for unknown commands", () => { |
40 | 131 | const output = formatCliParseErrorOutput("error: unknown command 'upate'\n", { |
41 | 132 | argv: ["node", "openclaw", "upate"], |
|
0 commit comments