|
| 1 | +// npx vitest run src/integrations/terminal/__tests__/BaseTerminal.spec.ts |
| 2 | + |
| 3 | +import { BaseTerminal } from "../BaseTerminal" |
| 4 | +import type { RooTerminalProcess } from "../types" |
| 5 | + |
| 6 | +// Create a concrete implementation of BaseTerminal for testing |
| 7 | +class TestTerminal extends BaseTerminal { |
| 8 | + constructor(id: number = 1, cwd: string = "/test") { |
| 9 | + super("vscode", id, cwd) |
| 10 | + } |
| 11 | + |
| 12 | + isClosed(): boolean { |
| 13 | + return false |
| 14 | + } |
| 15 | + |
| 16 | + runCommand(): never { |
| 17 | + throw new Error("Not implemented") |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +// Create a mock process for testing |
| 22 | +function createMockProcess(command: string): RooTerminalProcess { |
| 23 | + const events: Record<string, ((...args: any[]) => void)[]> = {} |
| 24 | + |
| 25 | + return { |
| 26 | + command, |
| 27 | + isHot: false, |
| 28 | + run: vi.fn(), |
| 29 | + continue: vi.fn(), |
| 30 | + abort: vi.fn(), |
| 31 | + hasUnretrievedOutput: vi.fn().mockReturnValue(false), |
| 32 | + getUnretrievedOutput: vi.fn().mockReturnValue(""), |
| 33 | + trimRetrievedOutput: vi.fn(), |
| 34 | + on: vi.fn((event: string, handler: (...args: any[]) => void) => { |
| 35 | + if (!events[event]) { |
| 36 | + events[event] = [] |
| 37 | + } |
| 38 | + events[event].push(handler) |
| 39 | + }), |
| 40 | + once: vi.fn((event: string, handler: (...args: any[]) => void) => { |
| 41 | + if (!events[event]) { |
| 42 | + events[event] = [] |
| 43 | + } |
| 44 | + events[event].push(handler) |
| 45 | + }), |
| 46 | + emit: vi.fn((event: string, ...args: any[]) => { |
| 47 | + const handlers = events[event] || [] |
| 48 | + handlers.forEach((handler) => handler(...args)) |
| 49 | + return true |
| 50 | + }), |
| 51 | + off: vi.fn(), |
| 52 | + removeListener: vi.fn(), |
| 53 | + removeAllListeners: vi.fn(), |
| 54 | + listeners: vi.fn(), |
| 55 | + rawListeners: vi.fn(), |
| 56 | + listenerCount: vi.fn(), |
| 57 | + prependListener: vi.fn(), |
| 58 | + prependOnceListener: vi.fn(), |
| 59 | + eventNames: vi.fn(), |
| 60 | + addListener: vi.fn(), |
| 61 | + setMaxListeners: vi.fn(), |
| 62 | + getMaxListeners: vi.fn(), |
| 63 | + } as unknown as RooTerminalProcess |
| 64 | +} |
| 65 | + |
| 66 | +// Create a mock async iterable stream for testing |
| 67 | +async function* createMockStream(): AsyncGenerator<string> { |
| 68 | + yield "test output" |
| 69 | +} |
| 70 | + |
| 71 | +describe("BaseTerminal", () => { |
| 72 | + describe("commandsMatch", () => { |
| 73 | + it("returns true for exact match", () => { |
| 74 | + expect(BaseTerminal.commandsMatch("npm test", "npm test")).toBe(true) |
| 75 | + }) |
| 76 | + |
| 77 | + it("returns true for exact match with whitespace trimming", () => { |
| 78 | + expect(BaseTerminal.commandsMatch(" npm test ", "npm test")).toBe(true) |
| 79 | + expect(BaseTerminal.commandsMatch("npm test", " npm test ")).toBe(true) |
| 80 | + }) |
| 81 | + |
| 82 | + it("returns true when actual command starts with expected command (PowerShell workaround)", () => { |
| 83 | + // PowerShell counter workaround appends extra commands |
| 84 | + const expected = "npm test" |
| 85 | + const actual = 'npm test ; "(Roo/PS Workaround: 1)" > $null' |
| 86 | + expect(BaseTerminal.commandsMatch(expected, actual)).toBe(true) |
| 87 | + }) |
| 88 | + |
| 89 | + it("returns true when actual command has trailing sleep command", () => { |
| 90 | + const expected = "npm test" |
| 91 | + const actual = "npm test ; start-sleep -milliseconds 50" |
| 92 | + expect(BaseTerminal.commandsMatch(expected, actual)).toBe(true) |
| 93 | + }) |
| 94 | + |
| 95 | + it("returns false for completely different commands", () => { |
| 96 | + expect(BaseTerminal.commandsMatch("npm test", "conda activate base")).toBe(false) |
| 97 | + }) |
| 98 | + |
| 99 | + it("returns false when commands are similar but not matching", () => { |
| 100 | + expect(BaseTerminal.commandsMatch("npm install", "npm run build")).toBe(false) |
| 101 | + }) |
| 102 | + |
| 103 | + it("returns false for reversed prefix (actual is prefix of expected)", () => { |
| 104 | + // This case should not match - the expected command should be a prefix of actual |
| 105 | + expect(BaseTerminal.commandsMatch("npm test --coverage", "npm test")).toBe(false) |
| 106 | + }) |
| 107 | + |
| 108 | + it("handles empty strings", () => { |
| 109 | + expect(BaseTerminal.commandsMatch("", "")).toBe(true) |
| 110 | + expect(BaseTerminal.commandsMatch("npm test", "")).toBe(false) |
| 111 | + expect(BaseTerminal.commandsMatch("", "npm test")).toBe(false) |
| 112 | + }) |
| 113 | + |
| 114 | + it("handles commands with special characters", () => { |
| 115 | + const expected = 'echo "hello world"' |
| 116 | + const actual = 'echo "hello world"' |
| 117 | + expect(BaseTerminal.commandsMatch(expected, actual)).toBe(true) |
| 118 | + }) |
| 119 | + |
| 120 | + it("handles conda activation commands correctly", () => { |
| 121 | + // Roo's command should not match conda's auto-activation |
| 122 | + expect(BaseTerminal.commandsMatch("npm test", "conda activate base")).toBe(false) |
| 123 | + expect(BaseTerminal.commandsMatch("npm test", "source activate myenv")).toBe(false) |
| 124 | + }) |
| 125 | + }) |
| 126 | + |
| 127 | + describe("setActiveStream", () => { |
| 128 | + let terminal: TestTerminal |
| 129 | + let mockProcess: RooTerminalProcess |
| 130 | + |
| 131 | + beforeEach(() => { |
| 132 | + terminal = new TestTerminal() |
| 133 | + mockProcess = createMockProcess("npm test") |
| 134 | + terminal.process = mockProcess |
| 135 | + }) |
| 136 | + |
| 137 | + it("sets stream when no eventCommand is provided (backwards compatibility)", () => { |
| 138 | + const stream = createMockStream() |
| 139 | + terminal.setActiveStream(stream) |
| 140 | + |
| 141 | + expect(terminal.running).toBe(true) |
| 142 | + expect(mockProcess.emit).toHaveBeenCalledWith("shell_execution_started", undefined) |
| 143 | + expect(mockProcess.emit).toHaveBeenCalledWith("stream_available", stream) |
| 144 | + }) |
| 145 | + |
| 146 | + it("sets stream when eventCommand matches process command", () => { |
| 147 | + const stream = createMockStream() |
| 148 | + terminal.setActiveStream(stream, undefined, "npm test") |
| 149 | + |
| 150 | + expect(terminal.running).toBe(true) |
| 151 | + expect(mockProcess.emit).toHaveBeenCalledWith("stream_available", stream) |
| 152 | + }) |
| 153 | + |
| 154 | + it("sets stream when eventCommand starts with process command (PowerShell case)", () => { |
| 155 | + const stream = createMockStream() |
| 156 | + terminal.setActiveStream(stream, undefined, 'npm test ; "(Roo/PS Workaround: 1)" > $null') |
| 157 | + |
| 158 | + expect(terminal.running).toBe(true) |
| 159 | + expect(mockProcess.emit).toHaveBeenCalledWith("stream_available", stream) |
| 160 | + }) |
| 161 | + |
| 162 | + it("ignores stream when eventCommand does not match process command", () => { |
| 163 | + const stream = createMockStream() |
| 164 | + const consoleSpy = vi.spyOn(console, "warn").mockImplementation(() => {}) |
| 165 | + |
| 166 | + terminal.setActiveStream(stream, undefined, "conda activate base") |
| 167 | + |
| 168 | + expect(terminal.running).toBe(false) |
| 169 | + expect(mockProcess.emit).not.toHaveBeenCalledWith("stream_available", expect.anything()) |
| 170 | + expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining("Ignoring shell execution")) |
| 171 | + |
| 172 | + consoleSpy.mockRestore() |
| 173 | + }) |
| 174 | + |
| 175 | + it("accepts stream when process has no command set (backward compatibility)", () => { |
| 176 | + mockProcess.command = "" |
| 177 | + const stream = createMockStream() |
| 178 | + |
| 179 | + // When process.command is empty string, the command verification is skipped |
| 180 | + // for backward compatibility. The process should accept any stream. |
| 181 | + terminal.setActiveStream(stream, undefined, "conda activate base") |
| 182 | + |
| 183 | + // Since process.command is empty, no verification is done and stream is accepted |
| 184 | + expect(terminal.running).toBe(true) |
| 185 | + expect(mockProcess.emit).toHaveBeenCalledWith("stream_available", stream) |
| 186 | + }) |
| 187 | + |
| 188 | + it("cleans up when stream is undefined", () => { |
| 189 | + terminal.setActiveStream(undefined) |
| 190 | + |
| 191 | + expect(terminal.isStreamClosed).toBe(true) |
| 192 | + }) |
| 193 | + |
| 194 | + it("handles missing process gracefully", () => { |
| 195 | + terminal.process = undefined |
| 196 | + const stream = createMockStream() |
| 197 | + const consoleSpy = vi.spyOn(console, "warn").mockImplementation(() => {}) |
| 198 | + |
| 199 | + terminal.setActiveStream(stream) |
| 200 | + |
| 201 | + expect(terminal.running).toBe(false) |
| 202 | + expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining("process is undefined")) |
| 203 | + |
| 204 | + consoleSpy.mockRestore() |
| 205 | + }) |
| 206 | + |
| 207 | + it("passes pid to shell_execution_started event", () => { |
| 208 | + const stream = createMockStream() |
| 209 | + const pid = 12345 |
| 210 | + |
| 211 | + terminal.setActiveStream(stream, pid, "npm test") |
| 212 | + |
| 213 | + expect(mockProcess.emit).toHaveBeenCalledWith("shell_execution_started", pid) |
| 214 | + }) |
| 215 | + }) |
| 216 | +}) |
0 commit comments