| 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( |
| 16 | "echo hello", |
| 17 | ); |
| 18 | }); |
| 19 | |
| 20 | // Test multiline comment removal |
| 21 | it("should remove multiline comments anywhere in the text", () => { |
| 22 | expect(extractCommand("echo /* comment */ hello")).toBe("echo hello"); |
| 23 | expect(extractCommand("echo hello /* comment */")).toBe("echo hello"); |
| 24 | expect(extractCommand("echo /* multiple\nline\ncomment */ hello")).toBe( |
| 25 | "echo hello", |
| 26 | ); |
| 27 | }); |
| 28 | |
| 29 | // Test line continuation handling |
| 30 | it("should handle line continuations correctly", () => { |
| 31 | expect(extractCommand("echo hello && \\\necho world")).toBe( |
| 32 | "echo hello && echo world", |
| 33 | ); |
| 34 | expect(extractCommand("echo hello | \\\ngrep h")).toBe( |
| 35 | "echo hello | grep h", |
| 36 | ); |
| 37 | expect(extractCommand("echo hello \\\nworld")).toBe("echo hello world"); |
| 38 | }); |
| 39 | |
| 40 | // Test complex command combinations |
| 41 | it("should handle complex command combinations", () => { |
| 42 | expect( |
| 43 | extractCommand(` |
| 44 | # shell comment |
| 45 | echo hello /* inline comment */ && \\ |
| 46 | // js comment |
| 47 | echo world |
| 48 | `), |
| 49 | ).toBe("echo hello && echo world"); |
| 50 | }); |
| 51 | |
| 52 | // Test empty and whitespace handling |
| 53 | it("should handle empty and whitespace inputs", () => { |
| 54 | expect(extractCommand("")).toBe(""); |
| 55 | expect(extractCommand(" \n \t ")).toBe(""); |
| 56 | }); |
| 57 | |
| 58 | // Test comment-only inputs |
| 59 | it("should handle comment-only inputs", () => { |
| 60 | expect(extractCommand("/* comment */")).toBe(""); |
| 61 | expect(extractCommand("# comment")).toBe(""); |
| 62 | expect(extractCommand("// comment")).toBe(""); |
| 63 | expect(extractCommand("/* multi\nline\ncomment */")).toBe(""); |
| 64 | }); |
| 65 | |
| 66 | // Test inline comments |
| 67 | it("should handle inline comments correctly according to implementation", () => { |
| 68 | // But removes multiline comments even when inline |
| 69 | expect(extractCommand("/* inline */ echo hello world")).toBe( |
| 70 | "echo hello world", |
| 71 | ); |
| 72 | }); |
| 73 | |
| 74 | // Test multiple line continuations |
| 75 | it("should handle multiple line continuations", () => { |
| 76 | expect( |
| 77 | extractCommand(` |
| 78 | echo hello && \\ |
| 79 | echo world && \\ |
| 80 | echo test |
| 81 | `), |
| 82 | ).toBe("echo hello && echo world && echo test"); |
| 83 | }); |
| 84 | |
| 85 | // Test nested line continuations |
| 86 | it("should handle nested line continuations", () => { |
| 87 | expect( |
| 88 | extractCommand(` |
| 89 | echo hello && \\ |
| 90 | (echo nested && \\ |
| 91 | echo more) && \\ |
| 92 | echo final |
| 93 | `), |
| 94 | ).toBe("echo hello && (echo nested && echo more) && echo final"); |
| 95 | }); |
| 96 | }); |