|
| 1 | +// cli-parse.test.mjs — argument-parsing guards for the CLI tools that lacked a |
| 2 | +// dedicated parse test (shot, compose, tape). Locks in the behaviour intensive |
| 3 | +// testing exercised by hand: positional mapping, numeric/string flag validation, |
| 4 | +// unknown-flag rejection, and the surplus-positional guard. |
| 5 | + |
| 6 | +import { test } from 'node:test'; |
| 7 | +import assert from 'node:assert/strict'; |
| 8 | +import { parse as shotParse } from '../shot.mjs'; |
| 9 | +import { parse as composeParse } from '../compose.mjs'; |
| 10 | +import { parseArgs as tapeParse } from '../tape.mjs'; |
| 11 | + |
| 12 | +test('shot: positionals map to url/selector/out; flags parse', () => { |
| 13 | + const a = shotParse(['http://x/', '.card', 'out.png', '--width', '1440', '--pad', '8']); |
| 14 | + assert.equal(a.url, 'http://x/'); |
| 15 | + assert.equal(a.selector, '.card'); |
| 16 | + assert.equal(a.out, 'out.png'); |
| 17 | + assert.equal(a.width, 1440); |
| 18 | + assert.equal(a.pad, 8); |
| 19 | +}); |
| 20 | + |
| 21 | +test('shot: bad numeric flag, unknown flag, and surplus positionals all throw', () => { |
| 22 | + assert.throws(() => shotParse(['u', 's', 'o', '--width', 'abc']), /--width must be a number/); |
| 23 | + assert.throws(() => shotParse(['u', 's', 'o', '--nope']), /shot: unknown arg --nope/); |
| 24 | + assert.throws(() => shotParse(['u', 's', 'o', 'extra']), /too many positional/); |
| 25 | +}); |
| 26 | + |
| 27 | +test('compose: positionals map to aPng/bPng/out; flags parse', () => { |
| 28 | + const a = composeParse(['a.png', 'b.png', 'out.png', '--labels', 'Before,After', '--gap', '12']); |
| 29 | + assert.equal(a.aPng, 'a.png'); |
| 30 | + assert.equal(a.bPng, 'b.png'); |
| 31 | + assert.equal(a.out, 'out.png'); |
| 32 | + assert.equal(a.labels, 'Before,After'); |
| 33 | + assert.equal(a.gap, 12); |
| 34 | +}); |
| 35 | + |
| 36 | +test('compose: unknown flag, surplus positionals, and a flag-as-value all throw', () => { |
| 37 | + assert.throws(() => composeParse(['a', 'b', 'o', '--frob']), /compose: unknown arg --frob/); |
| 38 | + assert.throws(() => composeParse(['a', 'b', 'o', 'x']), /too many positional/); |
| 39 | + assert.throws(() => composeParse(['a', 'b', 'o', '--labels', '--gap']), /--labels needs a value/); |
| 40 | +}); |
| 41 | + |
| 42 | +test('tape: positionals collected; numeric + string flags parse', () => { |
| 43 | + const a = tapeParse(['out.gif', '--width', '900', '--steps-json', '[]', '--theme', 'dark']); |
| 44 | + assert.deepEqual(a.positional, ['out.gif']); |
| 45 | + assert.equal(a.width, 900); |
| 46 | + assert.equal(a.stepsJson, '[]'); |
| 47 | + assert.equal(a.theme, 'dark'); |
| 48 | +}); |
| 49 | + |
| 50 | +test('tape: unknown flag and a non-integer width throw', () => { |
| 51 | + assert.throws(() => tapeParse(['o.gif', '--bogus']), /tape: unknown arg --bogus/); |
| 52 | + assert.throws(() => tapeParse(['o.gif', '--width', '12.5']), /--width must be a whole number/); |
| 53 | +}); |
0 commit comments