|
| 1 | +// background-execution.spec.ts |
| 2 | + |
| 3 | +import { describe, it, expect, vi } from "vitest" |
| 4 | +import { Task } from "../../../core/task/Task" |
| 5 | +import { executeCommand, ExecuteCommandOptions } from "../../../core/tools/executeCommandTool" |
| 6 | +import { TerminalRegistry } from "../TerminalRegistry" |
| 7 | +import { Terminal } from "../Terminal" |
| 8 | + |
| 9 | +// Mock Task class |
| 10 | +class MockTask { |
| 11 | + taskId = "test-task" |
| 12 | + cwd = "/test/workspace" |
| 13 | + consecutiveMistakeCount = 0 |
| 14 | + didRejectTool = false |
| 15 | + lastMessageTs = undefined |
| 16 | + terminalProcess = undefined |
| 17 | + |
| 18 | + async say() {} |
| 19 | + async ask() { |
| 20 | + throw new Error("Should not be called for background execution") |
| 21 | + } |
| 22 | + recordToolError() {} |
| 23 | +} |
| 24 | + |
| 25 | +vi.mock("fs/promises") |
| 26 | +vi.mock("../TerminalRegistry") |
| 27 | +vi.mock("../Terminal") |
| 28 | +vi.mock("../../../i18n") |
| 29 | + |
| 30 | +describe("Background execution", () => { |
| 31 | + let mockTask: MockTask |
| 32 | + let mockProvider: any |
| 33 | + |
| 34 | + beforeEach(() => { |
| 35 | + mockTask = new MockTask() |
| 36 | + mockProvider = { |
| 37 | + postMessageToWebview: vi.fn(), |
| 38 | + getState: vi.fn().mockResolvedValue({ |
| 39 | + terminalOutputLineLimit: 500, |
| 40 | + terminalOutputCharacterLimit: 10000, |
| 41 | + terminalShellIntegrationDisabled: true, |
| 42 | + }), |
| 43 | + } |
| 44 | + |
| 45 | + // Mock terminal methods |
| 46 | + vi.mocked(TerminalRegistry.getOrCreateTerminal).mockResolvedValue({ |
| 47 | + getCurrentWorkingDirectory: () => "/test/workspace", |
| 48 | + runCommand: vi.fn().mockReturnValue(Promise.resolve()), |
| 49 | + terminal: { |
| 50 | + show: vi.fn(), |
| 51 | + }, |
| 52 | + } as any) |
| 53 | + }) |
| 54 | + |
| 55 | + it("should execute command in background when background parameter is true", async () => { |
| 56 | + const options: ExecuteCommandOptions = { |
| 57 | + executionId: "test-id", |
| 58 | + command: "echo 'test'", |
| 59 | + background: true, |
| 60 | + terminalOutputLineLimit: 500, |
| 61 | + terminalOutputCharacterLimit: 10000, |
| 62 | + } |
| 63 | + |
| 64 | + // Mock the callbacks |
| 65 | + const callbacks = { |
| 66 | + onLine: vi.fn(), |
| 67 | + onCompleted: vi.fn(), |
| 68 | + onShellExecutionStarted: vi.fn(), |
| 69 | + onShellExecutionComplete: vi.fn(), |
| 70 | + } |
| 71 | + |
| 72 | + // Mock terminal instance |
| 73 | + const mockTerminal = { |
| 74 | + getCurrentWorkingDirectory: () => "/test/workspace", |
| 75 | + runCommand: vi.fn().mockReturnValue(Promise.resolve()), |
| 76 | + terminal: { show: vi.fn() }, |
| 77 | + } |
| 78 | + |
| 79 | + vi.mocked(TerminalRegistry.getOrCreateTerminal).mockResolvedValue(mockTerminal as any) |
| 80 | + |
| 81 | + // Execute command with background = true |
| 82 | + const [rejected, result] = await executeCommand(mockTask, options) |
| 83 | + |
| 84 | + // Verify that task.ask was NOT called (which means it ran in background) |
| 85 | + // Since we can't easily test the internal callback behavior, we verify the command was set up for background execution |
| 86 | + expect(rejected).toBe(false) |
| 87 | + expect(typeof result).toBe("string") |
| 88 | + }) |
| 89 | + |
| 90 | + it("should execute command with background=false by default", async () => { |
| 91 | + const options: ExecuteCommandOptions = { |
| 92 | + executionId: "test-id", |
| 93 | + command: "echo 'test'", |
| 94 | + background: false, |
| 95 | + terminalOutputLineLimit: 500, |
| 96 | + terminalOutputCharacterLimit: 10000, |
| 97 | + } |
| 98 | + |
| 99 | + // Mock terminal instance |
| 100 | + const mockTerminal = { |
| 101 | + getCurrentWorkingDirectory: () => "/test/workspace", |
| 102 | + runCommand: vi.fn().mockReturnValue(Promise.resolve()), |
| 103 | + terminal: { show: vi.fn() }, |
| 104 | + } |
| 105 | + |
| 106 | + vi.mocked(TerminalRegistry.getOrCreateTerminal).mockResolvedValue(mockTerminal as any) |
| 107 | + |
| 108 | + const [rejected, result] = await executeCommand(mockTask, options) |
| 109 | + |
| 110 | + expect(rejected).toBe(false) |
| 111 | + expect(typeof result).toBe("string") |
| 112 | + }) |
| 113 | + |
| 114 | + it("should handle background parameter as boolean true", async () => { |
| 115 | + const options: ExecuteCommandOptions = { |
| 116 | + executionId: "test-id", |
| 117 | + command: "echo 'test'", |
| 118 | + background: true, |
| 119 | + terminalOutputLineLimit: 500, |
| 120 | + terminalOutputCharacterLimit: 10000, |
| 121 | + } |
| 122 | + |
| 123 | + // Mock terminal instance |
| 124 | + const mockTerminal = { |
| 125 | + getCurrentWorkingDirectory: () => "/test/workspace", |
| 126 | + runCommand: vi.fn().mockReturnValue(Promise.resolve()), |
| 127 | + terminal: { show: vi.fn() }, |
| 128 | + } |
| 129 | + |
| 130 | + vi.mocked(TerminalRegistry.getOrCreateTerminal).mockResolvedValue(mockTerminal as any) |
| 131 | + |
| 132 | + const [rejected, result] = await executeCommand(mockTask, options) |
| 133 | + |
| 134 | + expect(rejected).toBe(false) |
| 135 | + // When background is true, the command should run and complete |
| 136 | + expect(result).toContain("Command executed in terminal") |
| 137 | + }) |
| 138 | + |
| 139 | + it("should default background to false when not provided", async () => { |
| 140 | + const options: ExecuteCommandOptions = { |
| 141 | + executionId: "test-id", |
| 142 | + command: "echo 'test'", |
| 143 | + terminalOutputLineLimit: 500, |
| 144 | + terminalOutputCharacterLimit: 10000, |
| 145 | + } |
| 146 | + |
| 147 | + // Mock terminal instance |
| 148 | + const mockTerminal = { |
| 149 | + getCurrentWorkingDirectory: () => "/test/workspace", |
| 150 | + runCommand: vi.fn().mockReturnValue(Promise.resolve()), |
| 151 | + terminal: { show: vi.fn() }, |
| 152 | + } |
| 153 | + |
| 154 | + vi.mocked(TerminalRegistry.getOrCreateTerminal).mockResolvedValue(mockTerminal as any) |
| 155 | + |
| 156 | + const [rejected, result] = await executeCommand(mockTask, options) |
| 157 | + |
| 158 | + expect(rejected).toBe(false) |
| 159 | + expect(typeof result).toBe("string") |
| 160 | + }) |
| 161 | +}) |
0 commit comments