| 1 | import { extractCommand } from './commandExtractor'; |
| 2 | |
| 3 | describe('extractCommand', () => { |
| 4 | // Test basic command extraction |
| 5 | it('should extract command from $ prompt', () => { |
| 6 | expect(extractCommand('$ echo hello')).toBe('echo hello'); |
| 7 | expect(extractCommand('$ echo hello')).toBe('echo hello'); |
| 8 | expect(extractCommand('$echo hello')).toBe('echo hello'); |
| 9 | }); |
| 10 | |
| 11 | // Test comment removal |
| 12 | it('should remove comments at beginning of lines', () => { |
| 13 | expect(extractCommand('# shell comment\necho hello')).toBe('echo hello'); |
| 14 | expect(extractCommand('// js comment\necho hello')).toBe('echo hello'); |
| 15 | expect(extractCommand('/* multi-line\ncomment */\necho hello')).toBe('echo hello'); |
| 16 | }); |
| 17 | |
| 18 | // Test multiline comment removal |
| 19 | it('should remove multiline comments anywhere in the text', () => { |
| 20 | expect(extractCommand('echo /* comment */ hello')).toBe('echo hello'); |
| 21 | expect(extractCommand('echo hello /* comment */')).toBe('echo hello'); |
| 22 | expect(extractCommand('echo /* multiple\nline\ncomment */ hello')).toBe('echo hello'); |
| 23 | }); |
| 24 | |
| 25 | // Test line continuation handling |
| 26 | it('should handle line continuations correctly', () => { |
| 27 | expect(extractCommand('echo hello && \\\necho world')).toBe('echo hello && echo world'); |
| 28 | expect(extractCommand('echo hello | \\\ngrep h')).toBe('echo hello | grep h'); |
| 29 | expect(extractCommand('echo hello \\\nworld')).toBe('echo hello world'); |
| 30 | }); |
| 31 | |
| 32 | // Test complex command combinations |
| 33 | it('should handle complex command combinations', () => { |
| 34 | expect(extractCommand(` |
| 35 | # shell comment |
| 36 | echo hello /* inline comment */ && \\ |
| 37 | // js comment |
| 38 | echo world |
| 39 | `)).toBe('echo hello && echo world'); |
| 40 | }); |
| 41 | |
| 42 | // Test empty and whitespace handling |
| 43 | it('should handle empty and whitespace inputs', () => { |
| 44 | expect(extractCommand('')).toBe(''); |
| 45 | expect(extractCommand(' \n \t ')).toBe(''); |
| 46 | }); |
| 47 | |
| 48 | // Test comment-only inputs |
| 49 | it('should handle comment-only inputs', () => { |
| 50 | expect(extractCommand('/* comment */')).toBe(''); |
| 51 | expect(extractCommand('# comment')).toBe(''); |
| 52 | expect(extractCommand('// comment')).toBe(''); |
| 53 | expect(extractCommand('/* multi\nline\ncomment */')).toBe(''); |
| 54 | }); |
| 55 | |
| 56 | // Test inline comments |
| 57 | it('should handle inline comments correctly according to implementation', () => { |
| 58 | // But removes multiline comments even when inline |
| 59 | expect(extractCommand('/* inline */ echo hello world')).toBe('echo hello world'); |
| 60 | }); |
| 61 | |
| 62 | // Test multiple line continuations |
| 63 | it('should handle multiple line continuations', () => { |
| 64 | expect(extractCommand(` |
| 65 | echo hello && \\ |
| 66 | echo world && \\ |
| 67 | echo test |
| 68 | `)).toBe('echo hello && echo world && echo test'); |
| 69 | }); |
| 70 | |
| 71 | // Test nested line continuations |
| 72 | it('should handle nested line continuations', () => { |
| 73 | expect(extractCommand(` |
| 74 | echo hello && \\ |
| 75 | (echo nested && \\ |
| 76 | echo more) && \\ |
| 77 | echo final |
| 78 | `)).toBe('echo hello && (echo nested && echo more) && echo final'); |
| 79 | }); |
| 80 | }); |