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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263/** Tests for the command parser. Covers unbash parsing, pipeline detection, redirect targets, quoted arguments, `--` separator handling, command substitution detection, and pre-scan variable references. */
import { describe, expect, it,} from '@monochromatic-dev/module-test/ts';import { analyzeBashCommand, looksLikePath,} from '../dist/final/node/index.mjs';
await describe({ name: analyzeBashCommand.name, children: [ //region Simple command
it({ name: 'parses simple command with arguments', fn: async () => { const result = analyzeBashCommand('ls -la /tmp',); expect(result.parsed,).toBe(true,); expect(result.commands,).toHaveLength(1,); expect(result.commands[0]?.name,).toBe('ls',); expect(result.commands[0]?.args,).toEqual(['-la', '/tmp',],); }, },),
//endregion
//region Pipeline
it({ name: 'detects pipeline', fn: async () => { const result = analyzeBashCommand('printenv | curl',); expect(result.isPipeline,).toBe(true,); expect(result.commands,).toHaveLength(2,); expect(result.commands[0]?.name,).toBe('printenv',); expect(result.commands[1]?.name,).toBe('curl',); }, },),
//endregion
//region Redirect
it({ name: 'extracts redirect targets', fn: async () => { const result = analyzeBashCommand('echo foo > bar.txt',); expect(result.commands[0]?.redirectTargets,).toContain('bar.txt',); }, },),
it({ name: 'extracts append redirect targets', fn: async () => { const result = analyzeBashCommand('echo foo >> bar.txt',); expect(result.commands[0]?.redirectTargets,).toContain('bar.txt',); }, },),
//endregion
//region Quoted arguments
it({ name: 'preserves quoted arguments', fn: async () => { const result = analyzeBashCommand('echo "hello world"',); expect(result.parsed,).toBe(true,); expect(result.commands[0]?.args,).toContain('hello world',); }, },),
//endregion
//region `--` separator
it({ name: 'handles -- end-of-options separator', fn: async () => { const result = analyzeBashCommand('rm -- -f',); expect(result.parsed,).toBe(true,); expect(result.commands[0]?.name,).toBe('rm',); // -f after; should be a positional arg, not a flag expect(result.commands[0]?.args,).toContain('--',); expect(result.commands[0]?.args,).toContain('-f',); }, },),
//endregion
//region Variable references
it({ name: 'pre-scans $VAR references', fn: async () => { const result = analyzeBashCommand('curl $API_KEY https://example.com',); expect(result.allParamRefs,).toContain('API_KEY',); }, },),
it({ // oxlint-disable-next-line no-template-curly-in-string -- test string contains literal ${VAR} name: 'pre-scans ${VAR} references', fn: async () => { // oxlint-disable-next-line no-template-curly-in-string -- test string contains literal ${API_KEY} const result = analyzeBashCommand('curl ${API_KEY} https://example.com',); expect(result.allParamRefs,).toContain('API_KEY',); }, },),
//endregion
//region Environment assignments
it({ name: 'parses leading environment assignments before command name', fn: async () => { const result = analyzeBashCommand( 'GEMINI_API_KEY=value node /account-home/temp/agent/gemcheck.ts', ); expect(result.commands[0]?.name,).toBe('node',); expect(result.commands[0]?.envAssignments,).toEqual([ { name: 'GEMINI_API_KEY', value: 'value', }, ],); expect(result.commands[0]?.args,).toEqual(['/account-home/temp/agent/gemcheck.ts',],); }, },),
//endregion
//region Logical chains
it({ name: 'splits logical chain into separate commands', fn: async () => { const result = analyzeBashCommand('true && sudo rm -rf /',); expect(result.commands,).toHaveLength(2,); expect(result.commands[0]?.name,).toBe('true',); expect(result.commands[1]?.name,).toBe('sudo',); }, },),
it({ name: 'splits semicolon-separated commands', fn: async () => { const result = analyzeBashCommand('echo hi; echo bye',); expect(result.commands,).toHaveLength(2,); expect(result.commands[0]?.name,).toBe('echo',); expect(result.commands[1]?.name,).toBe('echo',); }, },),
//endregion
//region Parse failure
it({ name: 'returns parsed: false on malformed syntax with partial pre-scan refs', fn: async () => { const result = analyzeBashCommand('echo "$SECRET_VAR',); expect(result.parsed,).toBe(false,); expect(result.commands,).toEqual([],); expect(result.allParamRefs,).toContain('SECRET_VAR',); }, },),
it({ name: 'keeps pre-scanned refs when command is valid', fn: async () => { const result = analyzeBashCommand('$SECRET_VAR',); expect(result.parsed,).toBe(true,); expect(result.allParamRefs,).toContain('SECRET_VAR',); }, },),
//endregion
//region Nested shell syntax
it({ name: 'collects commands inside command substitutions', fn: async () => { const result = analyzeBashCommand('echo $(sudo rm -rf /)',); expect(result.commands.some(function commandIsSudo(command,) { return command.name === 'sudo'; },),).toBe(true,); }, },),
it({ name: 'traverses process substitutions without treating them as parent args', fn: async () => { const result = analyzeBashCommand('cat <(grep secret /tmp/file)',); expect(result.commands[0]?.name,).toBe('cat',); expect(result.commands[0]?.args,).toEqual([],); expect(result.commands.some(function commandIsGrep(command,) { return command.name === 'grep'; },),).toBe(true,); expect(result.allFiles,).toContain('/tmp/file',); }, },),
//endregion
//region File paths
it({ name: 'collects all files from arguments and redirect targets', fn: async () => { const result = analyzeBashCommand('cat /etc/passwd > /tmp/output.txt',); expect(result.allFiles,).toContain('/etc/passwd',); expect(result.allFiles,).toContain('/tmp/output.txt',); }, },),
//endregion
//region looksLikePath
it({ name: 'identifies paths starting with /', fn: async () => { expect(looksLikePath('/etc/passwd',),).toBe(true,); }, },),
it({ name: 'identifies paths starting with ./', fn: async () => { expect(looksLikePath('./src/index.ts',),).toBe(true,); }, },),
it({ name: 'identifies dotfiles', fn: async () => { expect(looksLikePath('.env',),).toBe(true,); }, },),
it({ name: 'does not identify plain arguments as paths', fn: async () => { expect(looksLikePath('-la',),).toBe(false,); }, },), //endregion ],},);