|
| 1 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | + |
| 3 | +type MockTerminal = { |
| 4 | + name: string; |
| 5 | + show: ReturnType<typeof vi.fn>; |
| 6 | + sendText: ReturnType<typeof vi.fn>; |
| 7 | +}; |
| 8 | + |
| 9 | +const terminalListeners = new Set<(terminal: MockTerminal) => void>(); |
| 10 | +const terminals: MockTerminal[] = []; |
| 11 | + |
| 12 | +const windowMock = { |
| 13 | + terminals, |
| 14 | + activeTerminal: undefined as MockTerminal | undefined, |
| 15 | + createTerminal: vi.fn<(name?: string) => MockTerminal>(), |
| 16 | + onDidOpenTerminal: vi.fn((listener: (terminal: MockTerminal) => void) => { |
| 17 | + terminalListeners.add(listener); |
| 18 | + return { |
| 19 | + dispose: vi.fn(() => terminalListeners.delete(listener)), |
| 20 | + }; |
| 21 | + }), |
| 22 | +}; |
| 23 | + |
| 24 | +const commandsMock = { |
| 25 | + executeCommand: vi.fn<(command: string) => Promise<void>>(), |
| 26 | +}; |
| 27 | + |
| 28 | +const envMock = { |
| 29 | + remoteName: undefined as string | undefined, |
| 30 | +}; |
| 31 | + |
| 32 | +vi.mock("vscode", () => ({ |
| 33 | + window: windowMock, |
| 34 | + commands: commandsMock, |
| 35 | + env: envMock, |
| 36 | +})); |
| 37 | + |
| 38 | +function createTerminal(name: string): MockTerminal { |
| 39 | + return { |
| 40 | + name, |
| 41 | + show: vi.fn(), |
| 42 | + sendText: vi.fn(), |
| 43 | + }; |
| 44 | +} |
| 45 | + |
| 46 | +describe("runCommandInTerminal", () => { |
| 47 | + beforeEach(() => { |
| 48 | + vi.resetModules(); |
| 49 | + vi.clearAllMocks(); |
| 50 | + |
| 51 | + terminals.length = 0; |
| 52 | + terminalListeners.clear(); |
| 53 | + windowMock.activeTerminal = undefined; |
| 54 | + envMock.remoteName = undefined; |
| 55 | + |
| 56 | + windowMock.createTerminal.mockImplementation((name?: string) => { |
| 57 | + const terminal = createTerminal( |
| 58 | + name ?? "Terminal " + (terminals.length + 1), |
| 59 | + ); |
| 60 | + terminals.push(terminal); |
| 61 | + return terminal; |
| 62 | + }); |
| 63 | + |
| 64 | + commandsMock.executeCommand.mockResolvedValue(); |
| 65 | + }); |
| 66 | + |
| 67 | + it("reuses the active terminal and sends an executing command", async () => { |
| 68 | + const terminal = createTerminal("Active"); |
| 69 | + terminals.push(terminal); |
| 70 | + windowMock.activeTerminal = terminal; |
| 71 | + |
| 72 | + const { runCommandInTerminal } = await import("./runCommandInTerminal"); |
| 73 | + |
| 74 | + await runCommandInTerminal("echo hello"); |
| 75 | + |
| 76 | + expect(windowMock.createTerminal).not.toHaveBeenCalled(); |
| 77 | + expect(commandsMock.executeCommand).not.toHaveBeenCalled(); |
| 78 | + expect(terminal.show).toHaveBeenCalledOnce(); |
| 79 | + expect(terminal.sendText).toHaveBeenCalledWith("echo hello", true); |
| 80 | + }); |
| 81 | + |
| 82 | + it("creates a named local terminal when no reusable terminal exists", async () => { |
| 83 | + const { runCommandInTerminal } = await import("./runCommandInTerminal"); |
| 84 | + |
| 85 | + await runCommandInTerminal("npm test", { |
| 86 | + reuseTerminal: true, |
| 87 | + terminalName: "Start Ollama", |
| 88 | + }); |
| 89 | + |
| 90 | + expect(windowMock.createTerminal).toHaveBeenCalledWith("Start Ollama"); |
| 91 | + expect(commandsMock.executeCommand).not.toHaveBeenCalled(); |
| 92 | + |
| 93 | + const createdTerminal = terminals[0]; |
| 94 | + expect(createdTerminal.show).toHaveBeenCalledOnce(); |
| 95 | + expect(createdTerminal.sendText).toHaveBeenCalledWith("npm test", true); |
| 96 | + }); |
| 97 | + |
| 98 | + it("creates remote terminals through the remote-aware VS Code command", async () => { |
| 99 | + envMock.remoteName = "ssh-remote"; |
| 100 | + commandsMock.executeCommand.mockImplementation(async (command: string) => { |
| 101 | + expect(command).toBe("workbench.action.terminal.new"); |
| 102 | + const terminal = createTerminal("Remote Shell"); |
| 103 | + terminals.push(terminal); |
| 104 | + for (const listener of terminalListeners) { |
| 105 | + listener(terminal); |
| 106 | + } |
| 107 | + }); |
| 108 | + |
| 109 | + const { runCommandInTerminal } = await import("./runCommandInTerminal"); |
| 110 | + |
| 111 | + await runCommandInTerminal("pwd", { reuseTerminal: true }); |
| 112 | + |
| 113 | + expect(windowMock.createTerminal).not.toHaveBeenCalled(); |
| 114 | + expect(commandsMock.executeCommand).toHaveBeenCalledWith( |
| 115 | + "workbench.action.terminal.new", |
| 116 | + ); |
| 117 | + |
| 118 | + const createdTerminal = terminals[0]; |
| 119 | + expect(createdTerminal.show).toHaveBeenCalledOnce(); |
| 120 | + expect(createdTerminal.sendText).toHaveBeenCalledWith("pwd", true); |
| 121 | + }); |
| 122 | + |
| 123 | + it("reuses cached remote terminals for named commands", async () => { |
| 124 | + envMock.remoteName = "dev-container"; |
| 125 | + |
| 126 | + let createdCount = 0; |
| 127 | + commandsMock.executeCommand.mockImplementation(async () => { |
| 128 | + createdCount += 1; |
| 129 | + const terminal = createTerminal("Remote " + createdCount); |
| 130 | + terminals.push(terminal); |
| 131 | + for (const listener of terminalListeners) { |
| 132 | + listener(terminal); |
| 133 | + } |
| 134 | + }); |
| 135 | + |
| 136 | + const { runCommandInTerminal } = await import("./runCommandInTerminal"); |
| 137 | + |
| 138 | + await runCommandInTerminal("ollama serve", { |
| 139 | + reuseTerminal: true, |
| 140 | + terminalName: "Start Ollama", |
| 141 | + }); |
| 142 | + await runCommandInTerminal("ollama serve", { |
| 143 | + reuseTerminal: true, |
| 144 | + terminalName: "Start Ollama", |
| 145 | + }); |
| 146 | + |
| 147 | + expect(commandsMock.executeCommand).toHaveBeenCalledTimes(1); |
| 148 | + expect(terminals[0].sendText).toHaveBeenNthCalledWith( |
| 149 | + 1, |
| 150 | + "ollama serve", |
| 151 | + true, |
| 152 | + ); |
| 153 | + expect(terminals[0].sendText).toHaveBeenNthCalledWith( |
| 154 | + 2, |
| 155 | + "ollama serve", |
| 156 | + true, |
| 157 | + ); |
| 158 | + }); |
| 159 | +}); |
0 commit comments