codeburn/tests/bash-commands.test.ts
Rafael Calleja 45ce697eea fix: correct quote-handling alignment in extractBashCommands
Replace quoted strings with same-length spaces in stripQuotedStrings so
separator indices in the stripped string map correctly to the original.
Add test coverage for quoted separators and isBashTool.
2026-04-14 10:24:24 +02:00

66 lines
2.2 KiB
TypeScript

import { describe, it, expect } from 'vitest'
import { extractBashCommands, isBashTool } from '../src/bash-utils.js'
describe('extractBashCommands', () => {
it('extracts single command', () => {
expect(extractBashCommands('git status')).toEqual(['git'])
})
it('extracts chained commands with &&', () => {
expect(extractBashCommands('git add . && git commit -m "x"')).toEqual(['git', 'git'])
})
it('extracts chained commands with ;', () => {
expect(extractBashCommands('ls; pwd')).toEqual(['ls', 'pwd'])
})
it('extracts piped commands', () => {
expect(extractBashCommands('cat file | grep pattern')).toEqual(['cat', 'grep'])
})
it('filters out cd', () => {
expect(extractBashCommands('cd /path && git status')).toEqual(['git'])
})
it('returns empty for cd only', () => {
expect(extractBashCommands('cd /path')).toEqual([])
})
it('returns empty for empty string', () => {
expect(extractBashCommands('')).toEqual([])
})
it('returns empty for whitespace only', () => {
expect(extractBashCommands(' ')).toEqual([])
})
it('extracts basename from full path binary', () => {
expect(extractBashCommands('/usr/bin/git status')).toEqual(['git'])
})
it('handles mixed separators', () => {
expect(extractBashCommands('cd /x && npm install; npm run build | tee log')).toEqual(['npm', 'npm', 'tee'])
})
it('handles extra whitespace', () => {
expect(extractBashCommands(' git status ')).toEqual(['git'])
})
it('handles command with quotes containing separators', () => {
expect(extractBashCommands('echo "hello && world"')).toEqual(['echo'])
})
it('handles quoted separators followed by real separator', () => {
expect(extractBashCommands('echo "hello && world" && git status')).toEqual(['echo', 'git'])
})
it('handles single-quoted separators', () => {
expect(extractBashCommands("echo 'hello && world'")).toEqual(['echo'])
})
})
describe('isBashTool', () => {
it('recognizes Bash', () => { expect(isBashTool('Bash')).toBe(true) })
it('recognizes BashTool', () => { expect(isBashTool('BashTool')).toBe(true) })
it('rejects unknown tools', () => { expect(isBashTool('Read')).toBe(false) })
})