/** * Unit-level companion to `tests/scanners/cli-unknown-flag-rejection.test.mjs`. * That file measures the real CLIs end-to-end; this one pins the decision table * itself, including the cases no CLI happens to exercise today. */ import { test } from 'node:test'; import { strict as assert } from 'node:assert'; import { findArgError } from '../../scanners/lib/cli-args.mjs'; const SPEC = { boolean: ['--json', '--raw'], value: ['--output-file', '--context-window'] }; test('well-formed argv passes', () => { assert.equal(findArgError([], SPEC), null); assert.equal(findArgError(['--json'], SPEC), null); assert.equal(findArgError(['--output-file', 'out.json', '--raw'], SPEC), null); assert.equal(findArgError(['/some/target', '--json'], SPEC), null); }); test('an unknown flag is named in the diagnostic', () => { const err = findArgError(['--zzz'], SPEC); assert.match(err, /unknown flag "--zzz"/, 'the caller must be able to find the offending token'); }); test('a value flag followed by another flag is rejected', () => { const err = findArgError(['--output-file', '--json'], SPEC); assert.match(err, /--output-file/); assert.match(err, /--json/, 'both the flag and the thing mistaken for its value must appear'); }); test('a value flag with nothing after it is rejected', () => { assert.match(findArgError(['--output-file'], SPEC), /nothing followed it/); }); test('a value is never re-read as a flag', () => { // Without the consume step, a value that looks like a positional is harmless, // but a spec change could make this the difference between pass and reject. assert.equal(findArgError(['--output-file', 'report.json'], SPEC), null); }); test('positionals and subcommands pass through untouched', () => { assert.equal(findArgError(['init', '/target'], SPEC), null); }); test('the FIRST error is reported, not the last', () => { // A caller fixing errors one at a time should see them in argv order. assert.match(findArgError(['--zzz', '--output-file'], SPEC), /--zzz/); }); test('a spec with no value flags still rejects unknown flags', () => { assert.match(findArgError(['--nope'], { boolean: ['--json'] }), /unknown flag "--nope"/); });