|
| 1 | +import { describe, it, expect, beforeEach, afterEach, vi } from "vitest" |
| 2 | +import * as vscode from "vscode" |
| 3 | + |
| 4 | +import { Terminal } from "../Terminal" |
| 5 | +import { TerminalRegistry } from "../TerminalRegistry" |
| 6 | + |
| 7 | +describe("Terminal - Compound Command Handling", () => { |
| 8 | + beforeEach(() => { |
| 9 | + // Initialize the registry for tests |
| 10 | + vi.spyOn(TerminalRegistry, "initialize").mockImplementation(() => {}) |
| 11 | + TerminalRegistry.initialize() |
| 12 | + }) |
| 13 | + |
| 14 | + afterEach(() => { |
| 15 | + vi.restoreAllMocks() |
| 16 | + }) |
| 17 | + |
| 18 | + describe("isCompoundCommand", () => { |
| 19 | + it("should detect && operator", () => { |
| 20 | + expect(Terminal.isCompoundCommand("cd /tmp && ls")).toBe(true) |
| 21 | + expect(Terminal.isCompoundCommand("echo hello && echo world")).toBe(true) |
| 22 | + }) |
| 23 | + |
| 24 | + it("should detect || operator", () => { |
| 25 | + expect(Terminal.isCompoundCommand("cd /nonexistent || echo 'failed'")).toBe(true) |
| 26 | + expect(Terminal.isCompoundCommand("test -f file.txt || touch file.txt")).toBe(true) |
| 27 | + }) |
| 28 | + |
| 29 | + it("should detect ; operator", () => { |
| 30 | + expect(Terminal.isCompoundCommand("cd /tmp; ls")).toBe(true) |
| 31 | + expect(Terminal.isCompoundCommand("echo first; echo second; echo third")).toBe(true) |
| 32 | + }) |
| 33 | + |
| 34 | + it("should detect | pipe operator", () => { |
| 35 | + expect(Terminal.isCompoundCommand("ls | grep test")).toBe(true) |
| 36 | + expect(Terminal.isCompoundCommand("cat file.txt | head -10")).toBe(true) |
| 37 | + }) |
| 38 | + |
| 39 | + it("should detect & background operator", () => { |
| 40 | + expect(Terminal.isCompoundCommand("npm start &")).toBe(true) |
| 41 | + expect(Terminal.isCompoundCommand("python server.py &")).toBe(true) |
| 42 | + }) |
| 43 | + |
| 44 | + it("should not detect && in strings or comments", () => { |
| 45 | + // These are still detected as compound commands because we check for the operator presence |
| 46 | + // This is intentional to err on the side of caution |
| 47 | + expect(Terminal.isCompoundCommand('echo "&&"')).toBe(true) |
| 48 | + }) |
| 49 | + |
| 50 | + it("should not detect single & in the middle of command", () => { |
| 51 | + expect(Terminal.isCompoundCommand("echo 'this & that'")).toBe(false) |
| 52 | + expect(Terminal.isCompoundCommand("url?param1=a¶m2=b")).toBe(false) |
| 53 | + }) |
| 54 | + |
| 55 | + it("should handle complex compound commands", () => { |
| 56 | + expect(Terminal.isCompoundCommand("cd /tmp && npm install || echo 'failed'")).toBe(true) |
| 57 | + expect(Terminal.isCompoundCommand("test -d dir && (cd dir; make) || mkdir dir")).toBe(true) |
| 58 | + }) |
| 59 | + |
| 60 | + it("should return false for simple commands", () => { |
| 61 | + expect(Terminal.isCompoundCommand("ls")).toBe(false) |
| 62 | + expect(Terminal.isCompoundCommand("cd /tmp")).toBe(false) |
| 63 | + expect(Terminal.isCompoundCommand("echo hello")).toBe(false) |
| 64 | + expect(Terminal.isCompoundCommand("npm install")).toBe(false) |
| 65 | + }) |
| 66 | + |
| 67 | + it("should handle edge cases", () => { |
| 68 | + expect(Terminal.isCompoundCommand("")).toBe(false) |
| 69 | + expect(Terminal.isCompoundCommand(" ")).toBe(false) |
| 70 | + expect(Terminal.isCompoundCommand("&")).toBe(true) // Background operator |
| 71 | + expect(Terminal.isCompoundCommand("&&")).toBe(true) |
| 72 | + expect(Terminal.isCompoundCommand("||")).toBe(true) |
| 73 | + expect(Terminal.isCompoundCommand("|")).toBe(true) |
| 74 | + }) |
| 75 | + }) |
| 76 | + |
| 77 | + describe("Compound command execution with shell integration", () => { |
| 78 | + let mockTerminal: any |
| 79 | + let terminal: Terminal |
| 80 | + |
| 81 | + beforeEach(() => { |
| 82 | + // Create a mock VSCode terminal |
| 83 | + mockTerminal = { |
| 84 | + shellIntegration: undefined, |
| 85 | + sendText: vi.fn(), |
| 86 | + show: vi.fn(), |
| 87 | + hide: vi.fn(), |
| 88 | + dispose: vi.fn(), |
| 89 | + exitStatus: undefined, |
| 90 | + state: { isInteractedWith: false }, |
| 91 | + creationOptions: {}, |
| 92 | + name: "Test Terminal", |
| 93 | + processId: Promise.resolve(1234), |
| 94 | + } |
| 95 | + |
| 96 | + // Mock vscode.window.createTerminal |
| 97 | + vi.spyOn(vscode.window, "createTerminal").mockReturnValue(mockTerminal as any) |
| 98 | + |
| 99 | + // Create a Terminal instance |
| 100 | + terminal = new Terminal(1, undefined, "/tmp") |
| 101 | + }) |
| 102 | + |
| 103 | + it("should add delay for compound commands on new terminals", async () => { |
| 104 | + const command = "cd /tmp && npm test" |
| 105 | + const callbacks = { |
| 106 | + onLine: vi.fn(), |
| 107 | + onCompleted: vi.fn(), |
| 108 | + onShellExecutionStarted: vi.fn(), |
| 109 | + onShellExecutionComplete: vi.fn(), |
| 110 | + onNoShellIntegration: vi.fn(), |
| 111 | + } |
| 112 | + |
| 113 | + // Mock shell integration becoming available |
| 114 | + setTimeout(() => { |
| 115 | + mockTerminal.shellIntegration = { |
| 116 | + executeCommand: vi.fn(), |
| 117 | + cwd: { fsPath: "/tmp" }, |
| 118 | + } |
| 119 | + }, 50) |
| 120 | + |
| 121 | + const processPromise = terminal.runCommand(command, callbacks) |
| 122 | + |
| 123 | + // Wait a bit for the command to be processed |
| 124 | + await new Promise((resolve) => setTimeout(resolve, 200)) |
| 125 | + |
| 126 | + // Verify that the terminal is marked as busy initially |
| 127 | + expect(terminal.busy).toBe(true) |
| 128 | + |
| 129 | + // The shellIntegrationReady flag should be set after the delay |
| 130 | + expect((terminal as any).shellIntegrationReady).toBe(true) |
| 131 | + }) |
| 132 | + |
| 133 | + it("should not add delay for simple commands", async () => { |
| 134 | + const command = "ls -la" |
| 135 | + const callbacks = { |
| 136 | + onLine: vi.fn(), |
| 137 | + onCompleted: vi.fn(), |
| 138 | + onShellExecutionStarted: vi.fn(), |
| 139 | + onShellExecutionComplete: vi.fn(), |
| 140 | + onNoShellIntegration: vi.fn(), |
| 141 | + } |
| 142 | + |
| 143 | + // Mock shell integration being immediately available |
| 144 | + mockTerminal.shellIntegration = { |
| 145 | + executeCommand: vi.fn(), |
| 146 | + cwd: { fsPath: "/tmp" }, |
| 147 | + } |
| 148 | + |
| 149 | + const processPromise = terminal.runCommand(command, callbacks) |
| 150 | + |
| 151 | + // Wait a bit for the command to be processed |
| 152 | + await new Promise((resolve) => setTimeout(resolve, 100)) |
| 153 | + |
| 154 | + // Should execute without additional delay |
| 155 | + expect(mockTerminal.shellIntegration.executeCommand).toHaveBeenCalledWith(command) |
| 156 | + }) |
| 157 | + |
| 158 | + it("should not add delay for compound commands on terminals with ready shell integration", async () => { |
| 159 | + const command = "cd /tmp && npm test" |
| 160 | + const callbacks = { |
| 161 | + onLine: vi.fn(), |
| 162 | + onCompleted: vi.fn(), |
| 163 | + onShellExecutionStarted: vi.fn(), |
| 164 | + onShellExecutionComplete: vi.fn(), |
| 165 | + onNoShellIntegration: vi.fn(), |
| 166 | + } |
| 167 | + |
| 168 | + // Mock shell integration being immediately available |
| 169 | + mockTerminal.shellIntegration = { |
| 170 | + executeCommand: vi.fn(), |
| 171 | + cwd: { fsPath: "/tmp" }, |
| 172 | + } |
| 173 | + |
| 174 | + // Mark shell integration as ready (simulating a reused terminal) |
| 175 | + ;(terminal as any).shellIntegrationReady = true |
| 176 | + |
| 177 | + const processPromise = terminal.runCommand(command, callbacks) |
| 178 | + |
| 179 | + // Wait a bit for the command to be processed |
| 180 | + await new Promise((resolve) => setTimeout(resolve, 100)) |
| 181 | + |
| 182 | + // Should execute without additional delay since shellIntegrationReady is true |
| 183 | + expect(mockTerminal.shellIntegration.executeCommand).toHaveBeenCalledWith(command) |
| 184 | + }) |
| 185 | + }) |
| 186 | +}) |
0 commit comments