Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Aquaticat/Monochromatic. TypeScript and Rust monorepo: Claude Code and Pi coding-agent plugins, a m
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334import { describe, expect, it,} from '@monochromatic-dev/module-test/ts';import { ArgvParseError, type ArgvSpec, parseArgv,} from './argv.ts';
/** Parse returned instead of refusing, which every refusal test rejects. */const NO_REFUSAL: unique symbol = Symbol('parse returned without refusing',);
/** Declared surface mirroring the optique parser this module replaced, used to pin behavior observed from `@optique/core` 1.1.1 before the migration. */const spec: ArgvSpec = { flags: { allFlags: { names: [ '-a', '--all', ], }, }, valueOptions: { message: { names: [ '-m', '--message', ], }, },};
/** Parses one region against the shared declared surface. @param args - exact argv region @returns compact facts comparable with observed optique output */function parse(args: readonly string[],) { /** Parsed region facts. */ const result = parseArgv({ args, spec, },); return { a: result.flagCounts .allFlags ?? 0, m: result.optionValues .message ?? [], pos: result.positionals, unk: result.unknownOptions, };}
await describe({ name: 'git argv region parser', children: [ it({ name: 'takes a declared option value and keeps following positionals', fn: async function testOptionValue() { expect(parse([ '-m', 'msg', 'a.txt', ],),).toEqual({ a: 0, m: ['msg',], pos: ['a.txt',], unk: [], },); }, },), it({ name: 'counts a declared flag without consuming following positionals', fn: async function testFlag() { expect(parse([ '-a', 'a.txt', 'b.txt', ],),).toEqual({ a: 1, m: [], pos: [ 'a.txt', 'b.txt', ], unk: [], },); }, },), it({ name: 'counts every repeated declared flag occurrence', fn: async function testRepeatedFlag() { expect(parse([ '-a', '-a', ],),).toEqual({ a: 2, m: [], pos: [], unk: [], },); }, },), it({ name: 'lets an undeclared option consume one following plain token', fn: async function testUnknownConsumes() { expect(parse([ '-q', 'a.txt', ],),).toEqual({ a: 0, m: [], pos: [], unk: [ '-q', 'a.txt', ], },); }, },), it({ name: 'stops an undeclared option from consuming a dash-led token', fn: async function testUnknownSpares() { expect(parse([ '-q', '-a', ],),).toEqual({ a: 1, m: [], pos: [], unk: ['-q',], },); }, },), it({ name: 'stops an undeclared option from consuming the terminator', fn: async function testUnknownSparesTerminator() { expect(parse([ '-q', '--', 'a.txt', ],),).toEqual({ a: 0, m: [], pos: ['a.txt',], unk: ['-q',], },); }, },), it({ name: 'leaves an undeclared option at end of region unconsumed', fn: async function testUnknownAtEnd() { expect(parse(['-q',],),).toEqual({ a: 0, m: [], pos: [], unk: ['-q',], },); }, },), it({ name: 'takes a declared joined option value', fn: async function testJoinedDeclared() { expect(parse([ '--message=msg', 'a.txt', ],),).toEqual({ a: 0, m: ['msg',], pos: ['a.txt',], unk: [], },); }, },), it({ name: 'treats an undeclared joined token as an undeclared option, never a path', fn: async function testJoinedUndeclared() { // Reading this as a path is what hands `--untracked-files=no` to the // commit-only rule as a pathspec. expect(parse([ '--unknown=v', 'a.txt', ],),).toEqual({ a: 0, m: [], pos: ['a.txt',], unk: ['--unknown=v',], },); }, },), it({ name: 'keeps an ordinary undeclared joined git option working', fn: async function testOrdinaryJoinedOption() { // `git commit -m msg --untracked-files=no a.txt` is valid git; these // specs declare a subset of git's options, so it arrives undeclared. expect(parse([ '-m', 'msg', '--untracked-files=no', 'a.txt', ],),).toEqual({ a: 0, m: ['msg',], pos: ['a.txt',], unk: ['--untracked-files=no',], },); }, },), it({ name: 'names the offending token and region when refusing', fn: async function testRefusalDetail() { /** Refusal captured from a value option missing its value. */ const caught = (function captureRefusal() { try { parse([ '-a', '-m', ],); return NO_REFUSAL; } catch (error: unknown) { return error; } })(); expect(caught,).toBeInstanceOf(ArgvParseError,); if (caught instanceof ArgvParseError) { expect(caught.token,).toBe('-m',); expect(caught.index,).toBe(1,); expect(caught.region,).toEqual([ '-a', '-m', ],); expect(caught.message,).toContain('-m',); } }, },), it({ name: 'refuses a declared option missing its value', fn: async function testOptionMissingValue() { expect(function parseOptionMissingValue() { parse(['-m',],); },).toThrow(ArgvParseError,); }, },), it({ name: 'takes a dash-led token as a declared option value', fn: async function testDashLedValue() { expect(parse([ '-m', '-a', ],),).toEqual({ a: 0, m: ['-a',], pos: [], unk: [], },); }, },), it({ name: 'parses declared options appearing after positionals', fn: async function testOptionAfterPositional() { expect(parse([ 'a.txt', '-m', 'msg', ],),).toEqual({ a: 0, m: ['msg',], pos: ['a.txt',], unk: [], },); }, },), it({ name: 'treats a lone dash as positional', fn: async function testLoneDash() { expect(parse(['-',],),).toEqual({ a: 0, m: [], pos: ['-',], unk: [], },); }, },), it({ name: 'treats every token after the terminator as positional', fn: async function testTerminator() { expect(parse([ '--', '-a', ],),).toEqual({ a: 0, m: [], pos: ['-a',], unk: [], },); }, },), it({ name: 'parses declared flags before the terminator', fn: async function testFlagBeforeTerminator() { expect(parse([ '-a', '--', 'a.txt', ],),).toEqual({ a: 1, m: [], pos: ['a.txt',], unk: [], },); }, },), it({ name: 'rejects a declared spelling that cannot introduce an option', fn: async function testInvalidSpec() { expect(function parseInvalidSpec() { parseArgv({ args: [], spec: { flags: { bad: { names: ['nodash',], }, }, valueOptions: {}, }, },); },).toThrow(); }, },), ],},);