import { test } from 'node:test'; import { strict as assert } from 'node:assert'; import { spawnSync } from 'node:child_process'; import { existsSync, mkdtempSync, rmSync } from 'node:fs'; import { tmpdir } from 'node:os'; import { dirname, join } from 'node:path'; import { fileURLToPath, pathToFileURL } from 'node:url'; import { parseArgs } from '../../lib/parsers/arg-parser.mjs'; import { childEnv } from '../helpers/child-env.mjs'; const PARSER = join(dirname(fileURLToPath(import.meta.url)), '..', '..', 'lib', 'parsers', 'arg-parser.mjs'); const PARSER_URL = pathToFileURL(PARSER).href; test('trekbrief — empty args', () => { const r = parseArgs('', 'trekbrief'); assert.equal(r.command, 'trekbrief'); assert.deepEqual(r.flags, {}); }); test('trekbrief — --quick boolean', () => { const r = parseArgs('--quick', 'trekbrief'); assert.equal(r.flags['--quick'], true); }); test('trekresearch — --project value capture', () => { const r = parseArgs('--project .claude/projects/2026-04-30-foo', 'trekresearch'); assert.equal(r.flags['--project'], '.claude/projects/2026-04-30-foo'); }); test('trekresearch — --quick --local combined', () => { const r = parseArgs('--quick --local', 'trekresearch'); assert.equal(r.flags['--quick'], true); assert.equal(r.flags['--local'], true); }); test('trekplan — --research multi-value', () => { const r = parseArgs('--research a.md b.md c.md', 'trekplan'); assert.deepEqual(r.flags['--research'], ['a.md', 'b.md', 'c.md']); }); test('trekplan — --research multi stops at next flag', () => { const r = parseArgs('--research a.md b.md --project /x', 'trekplan'); assert.deepEqual(r.flags['--research'], ['a.md', 'b.md']); assert.equal(r.flags['--project'], '/x'); }); test('trekplan — --brief required-value flag', () => { const r = parseArgs('--brief brief.md', 'trekplan'); assert.equal(r.flags['--brief'], 'brief.md'); }); test('trekplan — missing value for --brief produces error', () => { const r = parseArgs('--brief --quick', 'trekplan'); assert.ok(r.errors.find(e => e.code === 'ARG_MISSING_VALUE')); }); test('trekplan — --decompose value flag', () => { const r = parseArgs('--decompose plan.md', 'trekplan'); assert.equal(r.flags['--decompose'], 'plan.md'); }); test('trekexecute — --resume + --project', () => { const r = parseArgs('--resume --project /tmp/p', 'trekexecute'); assert.equal(r.flags['--resume'], true); assert.equal(r.flags['--project'], '/tmp/p'); }); test('trekexecute — --step N value', () => { const r = parseArgs('--step 3', 'trekexecute'); assert.equal(r.flags['--step'], '3'); }); test('trekexecute — unknown flag goes to unknown[]', () => { const r = parseArgs('--mystery foo', 'trekexecute'); assert.ok(r.unknown.includes('--mystery')); }); test('quoted positional with spaces preserved', () => { const r = parseArgs('"hello world" simple', 'trekbrief'); assert.deepEqual(r.positional, ['hello world', 'simple']); }); test('unknown command reported as error', () => { const r = parseArgs('--quick', 'notacommand'); assert.ok(r.errors.find(e => e.code === 'ARG_UNKNOWN_COMMAND')); }); test('trekreview — --project value capture', () => { const r = parseArgs('--project .claude/projects/2026-05-01-foo', 'trekreview'); assert.equal(r.flags['--project'], '.claude/projects/2026-05-01-foo'); }); test('trekreview — --since value', () => { const r = parseArgs('--since HEAD~5', 'trekreview'); assert.equal(r.flags['--since'], 'HEAD~5'); }); test('trekreview — --quick + --validate combined', () => { const r = parseArgs('--quick --validate', 'trekreview'); assert.equal(r.flags['--quick'], true); assert.equal(r.flags['--validate'], true); }); test('trekreview — unknown flag goes to unknown[]', () => { const r = parseArgs('--mystery foo', 'trekreview'); assert.ok(r.unknown.includes('--mystery')); }); // --- S11 (NW2 part B) — opt-in --workflow substrate flag --- test('trekreview — --workflow boolean flag (NW2 opt-in substrate)', () => { const r = parseArgs('--workflow', 'trekreview'); assert.equal(r.flags['--workflow'], true); assert.ok(!r.unknown.includes('--workflow'), '--workflow must be a recognized trekreview flag, not unknown[]'); }); test('trekreview — --workflow combines with --quick + --project', () => { const r = parseArgs('--workflow --quick --project .claude/projects/x', 'trekreview'); assert.equal(r.flags['--workflow'], true); assert.equal(r.flags['--quick'], true); assert.equal(r.flags['--project'], '.claude/projects/x'); assert.deepEqual(r.unknown, []); }); test('trekcontinue — empty args produce no flags and no positional', () => { const r = parseArgs('', 'trekcontinue'); assert.equal(r.command, 'trekcontinue'); assert.deepEqual(r.flags, {}); assert.deepEqual(r.positional, []); assert.deepEqual(r.errors, []); }); test('trekcontinue — --help boolean flag', () => { const r = parseArgs('--help', 'trekcontinue'); assert.equal(r.flags['--help'], true); }); test('trekcontinue — -h treated as positional (no alias resolution)', () => { const r = parseArgs('-h', 'trekcontinue'); assert.deepEqual(r.positional, ['-h']); assert.deepEqual(r.errors, []); assert.equal(r.flags['--help'], undefined); }); test('trekcontinue — --cleanup boolean flag', () => { const r = parseArgs('--cleanup', 'trekcontinue'); assert.equal(r.flags['--cleanup'], true); }); test('trekcontinue — --cleanup --confirm both flags', () => { const r = parseArgs('--cleanup --confirm', 'trekcontinue'); assert.equal(r.flags['--cleanup'], true); assert.equal(r.flags['--confirm'], true); }); test('trekcontinue — positional project dir captured', () => { const r = parseArgs('.claude/projects/2026-05-04-foo', 'trekcontinue'); assert.deepEqual(r.positional, ['.claude/projects/2026-05-04-foo']); }); test('trekcontinue — .md positional accepted by parser (rejection is command-level)', () => { const r = parseArgs('NEXT-SESSION-PROMPT.local.md', 'trekcontinue'); assert.deepEqual(r.positional, ['NEXT-SESSION-PROMPT.local.md']); assert.deepEqual(r.errors, []); }); // --- #15 — /trekendsession reads its arguments with the parser --- // A slug or label holding "-h" is an argument, not a help request: only the // exact --help flag or a lone -h as the first positional asks for help. test('[#15] trekendsession — a slug is one positional, with no errors', () => { const r = parseArgs('2026-05-01-hotfix', 'trekendsession'); assert.deepEqual(r.errors, []); assert.deepEqual(r.positional, ['2026-05-01-hotfix']); assert.equal(r.flags['--help'], undefined); }); test('[#15] trekendsession — a path and a quoted label are two positionals, with no --help', () => { const r = parseArgs('fix-header "Session 2"', 'trekendsession'); assert.deepEqual(r.errors, []); assert.deepEqual(r.positional, ['fix-header', 'Session 2']); assert.equal(r.flags['--help'], undefined); }); test('[#15] trekendsession — --help is the boolean help flag', () => { const r = parseArgs('--help', 'trekendsession'); assert.deepEqual(r.errors, []); assert.equal(r.flags['--help'], true); assert.deepEqual(r.unknown, []); }); test('[#15] trekendsession — the CLI with --command trekendsession -- fix-header exits 0', () => { const r = spawnSync(process.execPath, [PARSER, '--command', 'trekendsession', '--', 'fix-header'], { encoding: 'utf8' }); assert.equal(r.status, 0, `the parser CLI refused trekendsession: ${r.stdout}${r.stderr}`); const out = JSON.parse(r.stdout); assert.deepEqual(out.positional, ['fix-header']); assert.equal(out.flags['--help'], undefined); }); test('trekendsession — -h stays a positional (no alias resolution)', () => { const r = parseArgs('-h', 'trekendsession'); assert.deepEqual(r.positional, ['-h']); assert.deepEqual(r.errors, []); assert.equal(r.flags['--help'], undefined); }); // --- ARG / M10 (fix round 4.5b) — the arguments reach the parser as stdin // text, never as shell words, and the label is read whole from `tail` --- function runStdin(command, input, cwd) { return spawnSync(process.execPath, [PARSER, '--command', command, '--stdin'], { input, cwd, encoding: 'utf8', env: childEnv(), }); } test('[ARG] --stdin keeps ; $( ) and * as literal characters and creates no file', async () => { const mod = await import(PARSER_URL); assert.equal(typeof mod.parseArgs, 'function', 'arg-parser.mjs exports parseArgs'); const cwd = mkdtempSync(join(tmpdir(), 'arg-parser-stdin-')); try { const r = runStdin('trekendsession', 'brief.md ; touch PWNED $(touch PWNED2) * ~/x $HOME\n', cwd); assert.equal(r.status, 0, `the parser CLI refused --stdin: ${r.stdout}${r.stderr}`); const out = JSON.parse(r.stdout); assert.deepEqual(out.positional, ['brief.md', ';', 'touch', 'PWNED', '$(touch', 'PWNED2)', '*', '~/x', '$HOME']); assert.equal(out.tail, '; touch PWNED $(touch PWNED2) * ~/x $HOME'); assert.equal(existsSync(join(cwd, 'PWNED')), false, 'no PWNED file was created'); assert.equal(existsSync(join(cwd, 'PWNED2')), false, 'no PWNED2 file was created'); } finally { rmSync(cwd, { recursive: true, force: true }); } }); test('[ARG] a balanced quote groups words', async () => { const mod = await import(PARSER_URL); assert.equal(typeof mod.tokenize, 'function', 'arg-parser.mjs exports tokenize'); assert.deepEqual(mod.tokenize(`a "b c" 'd e' f`), [ { value: 'a', start: 0, end: 1 }, { value: 'b c', start: 2, end: 7 }, { value: 'd e', start: 8, end: 13 }, { value: 'f', start: 14, end: 15 }, ]); assert.deepEqual(mod.tokenize(`brief.md "Fix the user's login"`).map((t) => t.value), ['brief.md', "Fix the user's login"]); }); test('[ARG] an unbalanced quote stays a literal character', async () => { const mod = await import(PARSER_URL); assert.equal(typeof mod.tokenize, 'function', 'arg-parser.mjs exports tokenize'); assert.deepEqual(mod.tokenize(`Fix the user's login`).map((t) => t.value), ['Fix', 'the', "user's", 'login']); assert.deepEqual(mod.tokenize('say "hi there').map((t) => t.value), ['say', '"hi', 'there']); assert.deepEqual(mod.parseArgs(`brief.md it's`, 'trekendsession').positional, ['brief.md', "it's"]); }); test('[ARG] empty stdin gives no positional, an empty tail and exit 0', async () => { const mod = await import(PARSER_URL); assert.equal(typeof mod.parseArgs, 'function', 'arg-parser.mjs exports parseArgs'); for (const input of ['', '\n']) { const r = runStdin('trekendsession', input); assert.equal(r.status, 0, `the parser CLI refused empty stdin: ${r.stdout}${r.stderr}`); const out = JSON.parse(r.stdout); assert.deepEqual(out.positional, []); assert.deepEqual(out.errors, []); assert.equal(out.tail, '', `tail for ${JSON.stringify(input)}`); } }); test('[M10] tail keeps a multi-word label whole', async () => { const mod = await import(PARSER_URL); assert.equal(typeof mod.parseArgs, 'function', 'arg-parser.mjs exports parseArgs'); const r = mod.parseArgs('brief.md Session 2 of 3', 'trekendsession'); assert.equal(r.tail, 'Session 2 of 3'); assert.equal(r.positional[0], 'brief.md'); }); test('[M10] one outer quote pair around the label is removed', async () => { const mod = await import(PARSER_URL); assert.equal(typeof mod.parseArgs, 'function', 'arg-parser.mjs exports parseArgs'); assert.equal(mod.parseArgs(`brief.md 'Session 2 of 3'`, 'trekendsession').tail, 'Session 2 of 3'); assert.equal(mod.parseArgs(`brief.md "Session 2 of 3"`, 'trekendsession').tail, 'Session 2 of 3'); }); test('[M10] an inner apostrophe stays', async () => { const mod = await import(PARSER_URL); assert.equal(typeof mod.parseArgs, 'function', 'arg-parser.mjs exports parseArgs'); assert.equal(mod.parseArgs(`brief.md Fix the user's login`, 'trekendsession').tail, "Fix the user's login"); });