import { describe, expect, it } from 'vitest'; import { parseArgs } from '../src/roundtrip.js'; // R-Cfg-1: parseArgs is exported so the CLI flag parsing is unit-testable. The // tricky bits are the --corpus lookahead (its directory arg is optional and must // NOT swallow a following flag), the `argv[++i]` value consumption for // --fixture/--page, and the empty-argv default. describe('parseArgs', () => { it('returns an empty object for empty argv (no flags)', () => { expect(parseArgs([])).toEqual({}); }); it('reads the value after --fixture (argv[++i])', () => { expect(parseArgs(['--fixture', 'path/to/doc.json'])).toEqual({ fixture: 'path/to/doc.json', }); }); it('reads the value after --page (argv[++i])', () => { expect(parseArgs(['--page', 'page-123'])).toEqual({ page: 'page-123' }); }); it('--corpus consumes the directory when the next token is not a flag', () => { expect(parseArgs(['--corpus', 'test/fixtures/corpus'])).toEqual({ corpus: 'test/fixtures/corpus', }); }); it('--corpus uses the DEFAULT dir when followed by another flag (lookahead)', () => { // The lookahead must NOT consume "--fixture" as the corpus directory; corpus // falls back to its default and --fixture is still parsed on the next loop. const parsed = parseArgs(['--corpus', '--fixture', 'x']); expect(parsed.corpus).toBe('test/fixtures/corpus'); expect(parsed.fixture).toBe('x'); }); it('--corpus uses the default dir when it is the LAST token (no lookahead value)', () => { expect(parseArgs(['--corpus'])).toEqual({ corpus: 'test/fixtures/corpus' }); }); it('a trailing --fixture with no value consumes undefined (off-by-one argv[++i])', () => { // `argv[++i]` reads past the end -> undefined; the loop then terminates. expect(parseArgs(['--fixture'])).toEqual({ fixture: undefined }); }); it('parses multiple flags together (page then a defaulted corpus)', () => { const parsed = parseArgs(['--page', 'p-1', '--corpus']); expect(parsed.page).toBe('p-1'); expect(parsed.corpus).toBe('test/fixtures/corpus'); }); });