|
| 1 | +#!/usr/bin/env tsx |
| 2 | +/** |
| 3 | + * The CLI parser runs with `strictOptions()`, so a mistyped flag fails the |
| 4 | + * command. The error must name the closest flag of the command that failed, |
| 5 | + * and stay quiet when nothing is close. |
| 6 | + * |
| 7 | + * Offline: every case fails in argument parsing, before a provider is built. |
| 8 | + * |
| 9 | + * Run: pnpm run test:cli-unknown-option |
| 10 | + */ |
| 11 | +import { |
| 12 | + assert, |
| 13 | + assertEqual, |
| 14 | + assertIncludes, |
| 15 | + defineSuite, |
| 16 | + runCLI, |
| 17 | +} from "./helpers/harness.js"; |
| 18 | +import { assertDistFresh } from "./helpers/distFreshness.js"; |
| 19 | + |
| 20 | +assertDistFresh({ entrypoints: ["dist/cli/index.js"] }); |
| 21 | +const { test, runSuite } = defineSuite("CLI unknown-option suggestions", { |
| 22 | + offline: true, |
| 23 | +}); |
| 24 | + |
| 25 | +const countOf = (haystack: string, needle: string): number => |
| 26 | + haystack.split(needle).length - 1; |
| 27 | + |
| 28 | +await test("a one-letter typo of a generate flag names the real flag", async () => { |
| 29 | + const result = await runCLI(["generate", "hi", "--temprature", "0.2"]); |
| 30 | + assert(result.exitCode !== 0, "a mistyped flag did not fail the command"); |
| 31 | + assertIncludes( |
| 32 | + result.stderr, |
| 33 | + "Did you mean --temperature instead of --temprature?", |
| 34 | + "no suggestion for --temprature", |
| 35 | + ); |
| 36 | + assertIncludes(result.stderr, "Use --help to see available options."); |
| 37 | +}); |
| 38 | + |
| 39 | +await test("each mistyped flag gets one suggestion, a kebab-case typo only once", async () => { |
| 40 | + const result = await runCLI([ |
| 41 | + "generate", |
| 42 | + "hi", |
| 43 | + "--providr", |
| 44 | + "openai", |
| 45 | + "--max-tokenz", |
| 46 | + "5", |
| 47 | + ]); |
| 48 | + assertIncludes( |
| 49 | + result.stderr, |
| 50 | + "Did you mean --provider instead of --providr?", |
| 51 | + "no suggestion for --providr", |
| 52 | + ); |
| 53 | + assertEqual( |
| 54 | + countOf(result.stderr, "Did you mean --maxTokens"), |
| 55 | + 1, |
| 56 | + "--max-tokenz was not suggested exactly once", |
| 57 | + ); |
| 58 | +}); |
| 59 | + |
| 60 | +await test("suggestions come from the failing command's own flags", async () => { |
| 61 | + const typo = await runCLI(["models", "list", "--providr", "openai"]); |
| 62 | + assertIncludes( |
| 63 | + typo.stderr, |
| 64 | + "Did you mean --provider instead of --providr?", |
| 65 | + "models list did not suggest its own --provider", |
| 66 | + ); |
| 67 | + const otherCommand = await runCLI(["models", "list", "--temprature", "1"]); |
| 68 | + assert(otherCommand.exitCode !== 0, "a generate-only flag was accepted"); |
| 69 | + assert( |
| 70 | + !otherCommand.stderr.includes("Did you mean"), |
| 71 | + "models list suggested a flag it does not have", |
| 72 | + ); |
| 73 | +}); |
| 74 | + |
| 75 | +await test("a name far from every flag gets no suggestion", async () => { |
| 76 | + const result = await runCLI(["generate", "hi", "--zzzzzzzz"]); |
| 77 | + assert(result.exitCode !== 0, "an unknown flag did not fail the command"); |
| 78 | + assert( |
| 79 | + !result.stderr.includes("Did you mean"), |
| 80 | + "a suggestion was made for a name near no flag", |
| 81 | + ); |
| 82 | + assertIncludes(result.stderr, "Use --help to see available options."); |
| 83 | +}); |
| 84 | + |
| 85 | +await runSuite(); |
0 commit comments