|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { test } from 'vitest'; |
| 3 | +import { listCliCommandNames } from '@agent-device/command-registry/catalog'; |
| 4 | +import type { CommandText } from '../commands/command-text.ts'; |
| 5 | +import { |
| 6 | + getCliCommandSchema, |
| 7 | + getFlagDefinitions, |
| 8 | + type CommandSchema, |
| 9 | + type FlagDefinition, |
| 10 | + type FlagKey, |
| 11 | +} from './command-schema.ts'; |
| 12 | +import { buildCommandUsage } from './usage.ts'; |
| 13 | + |
| 14 | +const TEXT: CommandText = { |
| 15 | + summary: 'Synopsis fixture', |
| 16 | + description: 'Synthetic grammar used to pin synopsis rendering rules.', |
| 17 | +}; |
| 18 | + |
| 19 | +function synopsisFor(grammar: Omit<CommandSchema, 'text'>): string { |
| 20 | + return buildCommandUsage('sample', { text: TEXT, ...grammar }); |
| 21 | +} |
| 22 | + |
| 23 | +function flagDefinitionsFor(key: FlagKey): FlagDefinition[] { |
| 24 | + return getFlagDefinitions().filter((definition) => definition.key === key); |
| 25 | +} |
| 26 | + |
| 27 | +/** The options a synopsis names in its generated flag tail. */ |
| 28 | +function tailFlags(schema: CommandSchema): readonly FlagKey[] { |
| 29 | + return schema.usageFlags ?? schema.allowedFlags ?? []; |
| 30 | +} |
| 31 | + |
| 32 | +/** A synopsis names an option by one of its CLI tokens, delimited so `--settle` is not `--settle-quiet`. */ |
| 33 | +function namesOption(synopsis: string, definition: FlagDefinition): boolean { |
| 34 | + // Flag tokens are letters, digits and dashes, so the name needs no escaping here. |
| 35 | + return definition.names.some((name) => |
| 36 | + new RegExp(String.raw`(?<![\w-])${name}(?![\w-])`).test(synopsis), |
| 37 | + ); |
| 38 | +} |
| 39 | + |
| 40 | +test('synopsis names each tailed option with its declared label, aliases included', () => { |
| 41 | + assert.equal( |
| 42 | + synopsisFor({ allowedFlags: ['snapshotDepth', 'snapshotInteractiveOnly', 'timeoutMs'] }), |
| 43 | + 'sample [--depth, -d <depth>] [-i] [--timeout <ms>]', |
| 44 | + ); |
| 45 | +}); |
| 46 | + |
| 47 | +test('synopsis omits a hidden option and an option with no CLI token', () => { |
| 48 | + assert.equal(synopsisFor({ allowedFlags: ['snapshotDiff', 'record'] }), 'sample [--diff]'); |
| 49 | + assert.equal(synopsisFor({ allowedFlags: ['snapshotDiff', 'installSource'] }), 'sample [--diff]'); |
| 50 | +}); |
| 51 | + |
| 52 | +test('synopsis renders positionals before the flag tail', () => { |
| 53 | + assert.equal( |
| 54 | + synopsisFor({ positionalArgs: ['kind', 'current?'], allowedFlags: ['threshold'] }), |
| 55 | + 'sample <kind> [current] [--threshold <0-1>]', |
| 56 | + ); |
| 57 | +}); |
| 58 | + |
| 59 | +test('usageFlags chooses the tail and a hand-written grammar keeps it generated', () => { |
| 60 | + assert.equal( |
| 61 | + synopsisFor({ |
| 62 | + usageOverride: 'sample first|second [--exclusive-a | --exclusive-b]', |
| 63 | + usageFlags: ['threshold'], |
| 64 | + allowedFlags: ['threshold', 'out'], |
| 65 | + }), |
| 66 | + 'sample first|second [--exclusive-a | --exclusive-b] [--threshold <0-1>]', |
| 67 | + ); |
| 68 | + assert.equal( |
| 69 | + synopsisFor({ usageOverride: 'sample only <arg>', usageFlags: [], allowedFlags: ['out'] }), |
| 70 | + 'sample only <arg>', |
| 71 | + ); |
| 72 | +}); |
| 73 | + |
| 74 | +test('snapshot synopsis is generated from its allowed flags', () => { |
| 75 | + const schema = getCliCommandSchema('snapshot'); |
| 76 | + assert.equal(schema.usageOverride, undefined); |
| 77 | + assert.equal( |
| 78 | + buildCommandUsage('snapshot', schema), |
| 79 | + 'snapshot [--diff] [-i] [--depth, -d <depth>] [--scope, -s <scope>] [--raw] [--actions] [--force-full] [--timeout <ms>]', |
| 80 | + ); |
| 81 | +}); |
| 82 | + |
| 83 | +test('a synopsis names no option its command refuses', () => { |
| 84 | + const offenders = listCliCommandNames().flatMap((command) => { |
| 85 | + const schema = getCliCommandSchema(command); |
| 86 | + const accepted = new Set<FlagKey>(schema.allowedFlags ?? []); |
| 87 | + const unaccepted = tailFlags(schema).filter((key) => !accepted.has(key)); |
| 88 | + if (unaccepted.length === 0) return []; |
| 89 | + return [`${command} tails ${unaccepted.join(', ')} outside its allowedFlags`]; |
| 90 | + }); |
| 91 | + assert.deepEqual( |
| 92 | + offenders, |
| 93 | + [], |
| 94 | + 'usageFlags is the tail of allowedFlags: an option the synopsis names must be one the ' + |
| 95 | + 'command parses. Add it to allowedFlags or drop it from usageFlags.', |
| 96 | + ); |
| 97 | +}); |
| 98 | + |
| 99 | +test('a generated flag tail repeats no bracket the grammar already wrote', () => { |
| 100 | + const offenders: string[] = []; |
| 101 | + for (const command of listCliCommandNames()) { |
| 102 | + const authored = getCliCommandSchema(command).usageOverride; |
| 103 | + if (authored === undefined) continue; |
| 104 | + const schema = getCliCommandSchema(command); |
| 105 | + const repeated = tailFlags(schema).filter((key) => |
| 106 | + flagDefinitionsFor(key).some((definition) => namesOption(authored, definition)), |
| 107 | + ); |
| 108 | + if (repeated.length > 0) offenders.push(`${command}: ${repeated.join(', ')}`); |
| 109 | + } |
| 110 | + assert.deepEqual( |
| 111 | + offenders, |
| 112 | + [], |
| 113 | + 'A hand-written grammar that names an option leaves it out of usageFlags, so the tail ' + |
| 114 | + 'generated after it renders that option exactly once.', |
| 115 | + ); |
| 116 | +}); |
| 117 | + |
| 118 | +test('an authored synopsis is not empty', () => { |
| 119 | + const offenders = listCliCommandNames().filter((command) => { |
| 120 | + const authored = getCliCommandSchema(command).usageOverride; |
| 121 | + return authored !== undefined && authored.trim().length === 0; |
| 122 | + }); |
| 123 | + assert.deepEqual( |
| 124 | + offenders, |
| 125 | + [], |
| 126 | + 'An empty usageOverride suppresses the whole synopsis; delete the field instead.', |
| 127 | + ); |
| 128 | +}); |
0 commit comments