|
| 1 | +// npx vitest run src/integrations/terminal/__tests__/TerminalProcess.python.spec.ts |
| 2 | + |
| 3 | +import * as vscode from "vscode" |
| 4 | + |
| 5 | +import { TerminalProcess } from "../TerminalProcess" |
| 6 | +import { Terminal } from "../Terminal" |
| 7 | +import { TerminalRegistry } from "../TerminalRegistry" |
| 8 | + |
| 9 | +vi.mock("execa", () => ({ |
| 10 | + execa: vi.fn(), |
| 11 | +})) |
| 12 | + |
| 13 | +describe("TerminalProcess Python Command Handling", () => { |
| 14 | + let terminalProcess: TerminalProcess |
| 15 | + let mockTerminal: any |
| 16 | + let mockTerminalInfo: Terminal |
| 17 | + let mockExecution: any |
| 18 | + let mockStream: AsyncIterableIterator<string> |
| 19 | + |
| 20 | + beforeEach(() => { |
| 21 | + // Create properly typed mock terminal |
| 22 | + mockTerminal = { |
| 23 | + shellIntegration: { |
| 24 | + executeCommand: vi.fn(), |
| 25 | + }, |
| 26 | + name: "Roo Code", |
| 27 | + processId: Promise.resolve(123), |
| 28 | + creationOptions: {}, |
| 29 | + exitStatus: undefined, |
| 30 | + state: { isInteractedWith: true }, |
| 31 | + dispose: vi.fn(), |
| 32 | + hide: vi.fn(), |
| 33 | + show: vi.fn(), |
| 34 | + sendText: vi.fn(), |
| 35 | + } as unknown as vscode.Terminal & { |
| 36 | + shellIntegration: { |
| 37 | + executeCommand: any |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + mockTerminalInfo = new Terminal(1, mockTerminal, "./") |
| 42 | + |
| 43 | + // Create a process for testing |
| 44 | + terminalProcess = new TerminalProcess(mockTerminalInfo) |
| 45 | + |
| 46 | + TerminalRegistry["terminals"].push(mockTerminalInfo) |
| 47 | + |
| 48 | + // Reset event listeners |
| 49 | + terminalProcess.removeAllListeners() |
| 50 | + }) |
| 51 | + |
| 52 | + describe("isPythonCommand detection", () => { |
| 53 | + it("detects basic python commands", () => { |
| 54 | + const testCases = [ |
| 55 | + "python script.py", |
| 56 | + "python3 script.py", |
| 57 | + "python -c \"print('hello')\"", |
| 58 | + "python3 -c 'print(\"hello\")'", |
| 59 | + "uv run python -c \"print('test')\"", |
| 60 | + "pipx run python script.py", |
| 61 | + 'poetry run python -c "import sys; print(sys.version)"', |
| 62 | + ] |
| 63 | + |
| 64 | + testCases.forEach((command) => { |
| 65 | + expect(terminalProcess["isPythonCommand"](command)).toBe(true) |
| 66 | + }) |
| 67 | + }) |
| 68 | + |
| 69 | + it("does not detect non-python commands", () => { |
| 70 | + const testCases = [ |
| 71 | + "echo hello", |
| 72 | + "ls -la", |
| 73 | + "npm run build", |
| 74 | + "node script.js", |
| 75 | + "pythonic-tool --help", // Contains "python" but not a python command |
| 76 | + "grep python file.txt", |
| 77 | + ] |
| 78 | + |
| 79 | + testCases.forEach((command) => { |
| 80 | + expect(terminalProcess["isPythonCommand"](command)).toBe(false) |
| 81 | + }) |
| 82 | + }) |
| 83 | + }) |
| 84 | + |
| 85 | + describe("containsVsceEndMarkers detection", () => { |
| 86 | + it("detects VSCode shell integration end markers", () => { |
| 87 | + expect(terminalProcess["containsVsceEndMarkers"]("\x1b]633;D\x07")).toBe(true) |
| 88 | + expect(terminalProcess["containsVsceEndMarkers"]("\x1b]133;D\x07")).toBe(true) |
| 89 | + expect(terminalProcess["containsVsceEndMarkers"]("some output\x1b]633;D\x07more")).toBe(true) |
| 90 | + expect(terminalProcess["containsVsceEndMarkers"]("regular output")).toBe(false) |
| 91 | + }) |
| 92 | + }) |
| 93 | + |
| 94 | + describe("Python command execution with extended timeout", () => { |
| 95 | + it("handles multi-line Python command execution", async () => { |
| 96 | + let lines: string[] = [] |
| 97 | + let completedOutput = "" |
| 98 | + |
| 99 | + terminalProcess.on("completed", (output) => { |
| 100 | + completedOutput = output || "" |
| 101 | + if (output) { |
| 102 | + lines = output.split("\n") |
| 103 | + } |
| 104 | + }) |
| 105 | + |
| 106 | + // Mock stream data for a multi-line Python command |
| 107 | + mockStream = (async function* () { |
| 108 | + yield "\x1b]633;C\x07" // Command start marker |
| 109 | + yield "look at this python script\n" |
| 110 | + yield "\x1b]633;D\x07" // Command end marker |
| 111 | + |
| 112 | + // Simulate shell execution complete event |
| 113 | + setTimeout(() => { |
| 114 | + terminalProcess.emit("shell_execution_complete", { exitCode: 0 }) |
| 115 | + }, 10) |
| 116 | + })() |
| 117 | + |
| 118 | + mockExecution = { |
| 119 | + read: vi.fn().mockReturnValue(mockStream), |
| 120 | + } |
| 121 | + |
| 122 | + mockTerminal.shellIntegration.executeCommand.mockReturnValue(mockExecution) |
| 123 | + |
| 124 | + const pythonCommand = |
| 125 | + 'uv run python -c "\nimport logging\nimport time\n\nprint(\\"look at this python script\\")\n"' |
| 126 | + |
| 127 | + const runPromise = terminalProcess.run(pythonCommand) |
| 128 | + terminalProcess.emit("stream_available", mockStream) |
| 129 | + await runPromise |
| 130 | + |
| 131 | + expect(completedOutput).toContain("look at this python script") |
| 132 | + expect(terminalProcess.isHot).toBe(false) |
| 133 | + }) |
| 134 | + |
| 135 | + it("handles Python command without end markers gracefully", async () => { |
| 136 | + let completedOutput = "" |
| 137 | + |
| 138 | + terminalProcess.on("completed", (output) => { |
| 139 | + completedOutput = output || "" |
| 140 | + }) |
| 141 | + |
| 142 | + // Mock stream data without proper end markers (simulating the bug scenario) |
| 143 | + mockStream = (async function* () { |
| 144 | + yield "\x1b]633;C\x07" // Command start marker |
| 145 | + yield "Python output without end marker\n" |
| 146 | + // No end marker - simulating the problematic scenario |
| 147 | + |
| 148 | + // Simulate delayed shell execution complete event |
| 149 | + setTimeout(() => { |
| 150 | + terminalProcess.emit("shell_execution_complete", { exitCode: 0 }) |
| 151 | + }, 100) |
| 152 | + })() |
| 153 | + |
| 154 | + mockExecution = { |
| 155 | + read: vi.fn().mockReturnValue(mockStream), |
| 156 | + } |
| 157 | + |
| 158 | + mockTerminal.shellIntegration.executeCommand.mockReturnValue(mockExecution) |
| 159 | + |
| 160 | + const pythonCommand = 'python -c "print(\\"test\\")"' |
| 161 | + |
| 162 | + const runPromise = terminalProcess.run(pythonCommand) |
| 163 | + terminalProcess.emit("stream_available", mockStream) |
| 164 | + await runPromise |
| 165 | + |
| 166 | + expect(completedOutput).toContain("Python output without end marker") |
| 167 | + expect(terminalProcess.isHot).toBe(false) |
| 168 | + }) |
| 169 | + |
| 170 | + it("applies extended timeout for Python commands", async () => { |
| 171 | + // Spy on the timeout to verify it's extended for Python commands |
| 172 | + const originalTimeout = Terminal.getShellIntegrationTimeout() |
| 173 | + |
| 174 | + // Mock a Python command that would normally timeout |
| 175 | + const pythonCommand = 'python -c "import time; time.sleep(0.1); print(\\"done\\")"' |
| 176 | + |
| 177 | + // The test verifies that isPythonCommand returns true for this command |
| 178 | + expect(terminalProcess["isPythonCommand"](pythonCommand)).toBe(true) |
| 179 | + |
| 180 | + // For this test, we'll just verify the command is detected as Python |
| 181 | + // The actual timeout extension is tested implicitly in the execution tests above |
| 182 | + }) |
| 183 | + }) |
| 184 | + |
| 185 | + describe("non-Python command execution", () => { |
| 186 | + it("uses normal timeout for non-Python commands", async () => { |
| 187 | + let completedOutput = "" |
| 188 | + |
| 189 | + terminalProcess.on("completed", (output) => { |
| 190 | + completedOutput = output || "" |
| 191 | + }) |
| 192 | + |
| 193 | + // Mock stream data for a regular command |
| 194 | + mockStream = (async function* () { |
| 195 | + yield "\x1b]633;C\x07" // Command start marker |
| 196 | + yield "Hello World\n" |
| 197 | + yield "\x1b]633;D\x07" // Command end marker |
| 198 | + terminalProcess.emit("shell_execution_complete", { exitCode: 0 }) |
| 199 | + })() |
| 200 | + |
| 201 | + mockExecution = { |
| 202 | + read: vi.fn().mockReturnValue(mockStream), |
| 203 | + } |
| 204 | + |
| 205 | + mockTerminal.shellIntegration.executeCommand.mockReturnValue(mockExecution) |
| 206 | + |
| 207 | + const regularCommand = 'echo "Hello World"' |
| 208 | + |
| 209 | + // Verify this is not detected as a Python command |
| 210 | + expect(terminalProcess["isPythonCommand"](regularCommand)).toBe(false) |
| 211 | + |
| 212 | + const runPromise = terminalProcess.run(regularCommand) |
| 213 | + terminalProcess.emit("stream_available", mockStream) |
| 214 | + await runPromise |
| 215 | + |
| 216 | + expect(completedOutput).toContain("Hello World") |
| 217 | + expect(terminalProcess.isHot).toBe(false) |
| 218 | + }) |
| 219 | + }) |
| 220 | +}) |
0 commit comments