Top-level run(argv,{out,err}) accepts an injectable output sink so
handlers can be unit-tested without spawning a subprocess. Handler
map is empty for now; subcommands are wired in as they land.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
47 lines
1.4 KiB
JavaScript
47 lines
1.4 KiB
JavaScript
'use strict';
|
|
|
|
const test = require('node:test');
|
|
const assert = require('node:assert/strict');
|
|
const { run } = require('../src/cli');
|
|
|
|
class Sink {
|
|
constructor() { this.chunks = []; }
|
|
write(s) { this.chunks.push(s); }
|
|
get text() { return this.chunks.join(''); }
|
|
}
|
|
|
|
test('cli: no args prints usage to stderr, exit 1', () => {
|
|
const out = new Sink(); const err = new Sink();
|
|
const code = run([], { out, err });
|
|
assert.equal(code, 1);
|
|
assert.match(err.text, /usage/i);
|
|
assert.match(err.text, /encode/);
|
|
assert.match(err.text, /decode/);
|
|
assert.match(err.text, /build/);
|
|
assert.match(err.text, /inspect/);
|
|
assert.equal(out.text, '');
|
|
});
|
|
|
|
test('cli: --help prints usage to stdout, exit 0', () => {
|
|
const out = new Sink(); const err = new Sink();
|
|
const code = run(['--help'], { out, err });
|
|
assert.equal(code, 0);
|
|
assert.match(out.text, /usage/i);
|
|
assert.equal(err.text, '');
|
|
});
|
|
|
|
test('cli: -h alias for --help', () => {
|
|
const out = new Sink(); const err = new Sink();
|
|
const code = run(['-h'], { out, err });
|
|
assert.equal(code, 0);
|
|
assert.match(out.text, /usage/i);
|
|
});
|
|
|
|
test('cli: unknown subcommand goes to stderr, exit 1', () => {
|
|
const out = new Sink(); const err = new Sink();
|
|
const code = run(['frobnicate'], { out, err });
|
|
assert.equal(code, 1);
|
|
assert.match(err.text, /unknown subcommand/i);
|
|
assert.match(err.text, /frobnicate/);
|
|
});
|