|
1 | 1 | import assert from 'node:assert/strict'; |
2 | 2 | import { test } from 'vitest'; |
3 | | -import { isKnownCliCommandName } from '@agent-device/command-registry/catalog'; |
| 3 | +import { isKnownCliCommandName, listCliCommandNames } from '@agent-device/command-registry/catalog'; |
4 | 4 | import { keyboardCliReader } from '../../../commands/system/index.ts'; |
5 | 5 | import { AppError } from '@agent-device/kernel/errors'; |
6 | 6 | import { parseArgs } from '../args.ts'; |
7 | 7 | import type { CliFlags } from '@agent-device/contracts/command'; |
8 | | -import { listCommandAliasSuggestionEntries, suggestCommandFor } from '../command-suggestions.ts'; |
| 8 | +import { getFlagDefinitions } from '../../../cli-schema/command-schema.ts'; |
| 9 | +import { isFlagSupportedForCommand } from '../../../cli-schema/option-schema.ts'; |
| 10 | +import { |
| 11 | + listCommandAliasSuggestionEntries, |
| 12 | + listFlagNameSuggestionEntries, |
| 13 | + suggestCommandFor, |
| 14 | + suggestFlagFor, |
| 15 | +} from '../command-suggestions.ts'; |
9 | 16 |
|
10 | 17 | // Guards against the curated alias map drifting to a command that no longer |
11 | 18 | // exists (renamed, removed, gated) in the live command registry. |
@@ -222,3 +229,62 @@ test('unrelated unknown flags are unaffected', () => { |
222 | 229 | error.message === 'Unknown flag: --not-a-real-flag', |
223 | 230 | ); |
224 | 231 | }); |
| 232 | + |
| 233 | +// Guards against the curated flag map drifting to a flag that no longer exists, or to |
| 234 | +// one no command accepts — a suggestion the parser would then refuse itself. |
| 235 | +test('every curated flag suggestion names a registered flag at least one command accepts', () => { |
| 236 | + const commands = listCliCommandNames(); |
| 237 | + for (const [guess, canonical] of listFlagNameSuggestionEntries()) { |
| 238 | + const definition = getFlagDefinitions().find((entry) => entry.names.includes(canonical)); |
| 239 | + assert.ok(definition, `flag suggestion for "${guess}" names unregistered flag "${canonical}"`); |
| 240 | + assert.ok( |
| 241 | + definition && commands.some((command) => isFlagSupportedForCommand(definition.key, command)), |
| 242 | + `flag suggestion "${canonical}" for "${guess}" is accepted by no command`, |
| 243 | + ); |
| 244 | + } |
| 245 | +}); |
| 246 | + |
| 247 | +for (const guess of ['--output', '--path', '--Output']) { |
| 248 | + test(`a guessed output-path flag on screenshot names ${guess} -> --out`, () => { |
| 249 | + assert.throws( |
| 250 | + () => parseArgs(['screenshot', guess, './shot.png']), |
| 251 | + (error: unknown) => |
| 252 | + error instanceof AppError && |
| 253 | + error.code === 'INVALID_ARGS' && |
| 254 | + error.message === `Unknown flag: ${guess}. Did you mean --out?`, |
| 255 | + ); |
| 256 | + }); |
| 257 | +} |
| 258 | + |
| 259 | +// The closest negative: `--path` means nothing to `trace`, whose path is positional and |
| 260 | +// which accepts no `--out` at all. A suggestion map applied without the per-command gate |
| 261 | +// would push a flag this command refuses. |
| 262 | +test('a guessed flag the command does not accept is not pushed at it', () => { |
| 263 | + assert.throws( |
| 264 | + () => parseArgs(['trace', 'start', '--path', './trace.log']), |
| 265 | + (error: unknown) => |
| 266 | + error instanceof AppError && |
| 267 | + error.code === 'INVALID_ARGS' && |
| 268 | + error.message === 'Unknown flag: --path', |
| 269 | + ); |
| 270 | +}); |
| 271 | + |
| 272 | +// The registry-derived fallback, not the curated map: `--pixel-densityy` is one edit from |
| 273 | +// a flag screenshot accepts, and the candidate list comes from that command's own surface. |
| 274 | +test('a mistyped flag the command accepts is suggested by nearest name', () => { |
| 275 | + assert.throws( |
| 276 | + () => parseArgs(['screenshot', '--pixel-densityy', '2']), |
| 277 | + (error: unknown) => |
| 278 | + error instanceof AppError && |
| 279 | + error.code === 'INVALID_ARGS' && |
| 280 | + error.message === 'Unknown flag: --pixel-densityy. Did you mean --pixel-density?', |
| 281 | + ); |
| 282 | +}); |
| 283 | + |
| 284 | +test('suggestFlagFor never throws for arbitrary input', () => { |
| 285 | + for (const input of ['', '-', '--', '@#$%', '--a'.repeat(200), '--JSON']) { |
| 286 | + for (const command of ['screenshot', 'open', null]) { |
| 287 | + assert.doesNotThrow(() => suggestFlagFor(input, command)); |
| 288 | + } |
| 289 | + } |
| 290 | +}); |
0 commit comments