|
| 1 | +import { EventEmitter } from "node:events"; |
| 2 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 3 | +import { IDE, ToolExtras } from "../.."; |
| 4 | +import { runTerminalCommandImpl } from "./runTerminalCommand"; |
| 5 | + |
| 6 | +// Hoist mock function so it can be referenced in vi.mock factory |
| 7 | +const { mockSpawn } = vi.hoisted(() => ({ |
| 8 | + mockSpawn: vi.fn(), |
| 9 | +})); |
| 10 | + |
| 11 | +vi.mock("node:child_process", () => ({ |
| 12 | + default: { |
| 13 | + spawn: mockSpawn, |
| 14 | + }, |
| 15 | + spawn: mockSpawn, |
| 16 | +})); |
| 17 | + |
| 18 | +describe("runTerminalCommand timeout functionality", () => { |
| 19 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 20 | + let mockChildProc: any; |
| 21 | + let mockGetIdeInfo: ReturnType<typeof vi.fn>; |
| 22 | + let mockGetWorkspaceDirs: ReturnType<typeof vi.fn>; |
| 23 | + let mockOnPartialOutput: ReturnType<typeof vi.fn>; |
| 24 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 25 | + let setTimeoutSpy: any; |
| 26 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 27 | + let clearTimeoutSpy: any; |
| 28 | + |
| 29 | + beforeEach(() => { |
| 30 | + vi.resetAllMocks(); |
| 31 | + vi.clearAllTimers(); |
| 32 | + vi.useFakeTimers(); |
| 33 | + |
| 34 | + // Spy on setTimeout and clearTimeout |
| 35 | + setTimeoutSpy = vi.spyOn(global, "setTimeout"); |
| 36 | + clearTimeoutSpy = vi.spyOn(global, "clearTimeout"); |
| 37 | + |
| 38 | + // Create mock child process with EventEmitter behavior |
| 39 | + mockChildProc = new EventEmitter(); |
| 40 | + mockChildProc.stdout = new EventEmitter(); |
| 41 | + mockChildProc.stderr = new EventEmitter(); |
| 42 | + mockChildProc.killed = false; |
| 43 | + mockChildProc.kill = vi.fn((signal?: NodeJS.Signals) => { |
| 44 | + mockChildProc.killed = true; |
| 45 | + setTimeout(() => { |
| 46 | + mockChildProc.emit("close", signal === "SIGKILL" ? 137 : 143); |
| 47 | + }, 100); |
| 48 | + return true; |
| 49 | + }); |
| 50 | + |
| 51 | + mockSpawn.mockReturnValue(mockChildProc); |
| 52 | + |
| 53 | + mockGetIdeInfo = vi.fn().mockResolvedValue({ remoteName: "local" }); |
| 54 | + mockGetWorkspaceDirs = vi |
| 55 | + .fn() |
| 56 | + .mockResolvedValue(["file:///tmp/test-workspace"]); |
| 57 | + mockOnPartialOutput = vi.fn(); |
| 58 | + }); |
| 59 | + |
| 60 | + afterEach(() => { |
| 61 | + vi.restoreAllMocks(); |
| 62 | + vi.useRealTimers(); |
| 63 | + }); |
| 64 | + |
| 65 | + const createMockExtras = ( |
| 66 | + overrides: Partial<ToolExtras> = {}, |
| 67 | + ): ToolExtras => { |
| 68 | + const mockIde = { |
| 69 | + getIdeInfo: mockGetIdeInfo, |
| 70 | + getWorkspaceDirs: mockGetWorkspaceDirs, |
| 71 | + getIdeSettings: vi.fn(), |
| 72 | + getDiff: vi.fn(), |
| 73 | + readFile: vi.fn(), |
| 74 | + readRangeInFile: vi.fn(), |
| 75 | + isTelemetryEnabled: vi.fn(), |
| 76 | + getProblems: vi.fn(), |
| 77 | + subprocess: vi.fn(), |
| 78 | + getWorkspaceConfigs: vi.fn(), |
| 79 | + showToast: vi.fn(), |
| 80 | + listWorkspaceContents: vi.fn(), |
| 81 | + getTerminalContents: vi.fn(), |
| 82 | + listFolders: vi.fn(), |
| 83 | + getSessionId: vi.fn(), |
| 84 | + runCommand: vi.fn(), |
| 85 | + showLines: vi.fn(), |
| 86 | + saveFile: vi.fn(), |
| 87 | + getBranch: vi.fn(), |
| 88 | + showDiff: vi.fn(), |
| 89 | + getOpenFiles: vi.fn(), |
| 90 | + showVirtualFile: vi.fn(), |
| 91 | + openFile: vi.fn(), |
| 92 | + getRepo: vi.fn(), |
| 93 | + pathSep: vi.fn(), |
| 94 | + fileExists: vi.fn(), |
| 95 | + } as unknown as IDE; |
| 96 | + |
| 97 | + return { |
| 98 | + ide: mockIde, |
| 99 | + llm: {} as any, |
| 100 | + fetch: vi.fn() as any, |
| 101 | + tool: {} as any, |
| 102 | + config: {} as any, |
| 103 | + onPartialOutput: mockOnPartialOutput, |
| 104 | + toolCallId: "test-tool-call-id", |
| 105 | + ...overrides, |
| 106 | + }; |
| 107 | + }; |
| 108 | + |
| 109 | + it("should set up timeout when waitForCompletion is true", async () => { |
| 110 | + const extras = createMockExtras(); |
| 111 | + const args = { command: "echo test", waitForCompletion: true }; |
| 112 | + const resultPromise = runTerminalCommandImpl(args, extras); |
| 113 | + await vi.runOnlyPendingTimersAsync(); |
| 114 | + expect(setTimeoutSpy).toHaveBeenCalledWith(expect.any(Function), 120_000); |
| 115 | + mockChildProc.emit("close", 0); |
| 116 | + await resultPromise; |
| 117 | + expect(clearTimeoutSpy).toHaveBeenCalled(); |
| 118 | + }); |
| 119 | + |
| 120 | + it("should NOT set up timeout when waitForCompletion is false", async () => { |
| 121 | + const extras = createMockExtras(); |
| 122 | + const args = { command: "sleep 10", waitForCompletion: false }; |
| 123 | + const result = await runTerminalCommandImpl(args, extras); |
| 124 | + const timeoutCalls = setTimeoutSpy.mock.calls.filter( |
| 125 | + (call: any[]) => call[1] === 120_000, |
| 126 | + ); |
| 127 | + expect(timeoutCalls.length).toBe(0); |
| 128 | + expect(result[0].status).toContain("background"); |
| 129 | + }); |
| 130 | + |
| 131 | + it("should kill process when timeout fires (streaming)", async () => { |
| 132 | + const extras = createMockExtras(); |
| 133 | + const args = { command: "sleep 300", waitForCompletion: true }; |
| 134 | + const resultPromise = runTerminalCommandImpl(args, extras); |
| 135 | + await vi.runOnlyPendingTimersAsync(); |
| 136 | + await vi.advanceTimersByTimeAsync(120_000); |
| 137 | + expect(mockChildProc.kill).toHaveBeenCalledWith("SIGTERM"); |
| 138 | + await vi.advanceTimersByTimeAsync(5_000); |
| 139 | + await vi.runAllTimersAsync(); |
| 140 | + const result = await resultPromise; |
| 141 | + expect(result[0].content).toContain( |
| 142 | + "[Timeout: process killed after 2 minutes]", |
| 143 | + ); |
| 144 | + }); |
| 145 | + |
| 146 | + it("should clear timeout on normal process exit", async () => { |
| 147 | + const extras = createMockExtras(); |
| 148 | + const args = { command: "echo quick", waitForCompletion: true }; |
| 149 | + const resultPromise = runTerminalCommandImpl(args, extras); |
| 150 | + // Flush async setup (getIdeInfo, getWorkspaceDirs) without advancing timer time |
| 151 | + await vi.advanceTimersByTimeAsync(0); |
| 152 | + mockChildProc.stdout.emit("data", Buffer.from("quick\n")); |
| 153 | + mockChildProc.emit("close", 0); |
| 154 | + await resultPromise; |
| 155 | + expect(clearTimeoutSpy).toHaveBeenCalled(); |
| 156 | + expect(mockChildProc.kill).not.toHaveBeenCalled(); |
| 157 | + }); |
| 158 | + |
| 159 | + it("should clear SIGKILL timeout when process exits between SIGTERM and SIGKILL grace period", async () => { |
| 160 | + const extras = createMockExtras(); |
| 161 | + const args = { command: "sleep 300", waitForCompletion: true }; |
| 162 | + const resultPromise = runTerminalCommandImpl(args, extras); |
| 163 | + |
| 164 | + // Let initial setup complete |
| 165 | + await vi.runOnlyPendingTimersAsync(); |
| 166 | + |
| 167 | + // Advance to trigger main timeout (120s) — SIGTERM sent, SIGKILL timer started |
| 168 | + await vi.advanceTimersByTimeAsync(120_000); |
| 169 | + expect(mockChildProc.kill).toHaveBeenCalledWith("SIGTERM"); |
| 170 | + expect(mockChildProc.kill).toHaveBeenCalledTimes(1); |
| 171 | + |
| 172 | + // Process exits gracefully after 2 seconds (before 5s grace period) |
| 173 | + await vi.advanceTimersByTimeAsync(2_000); |
| 174 | + mockChildProc.emit("close", 143); // SIGTERM exit code |
| 175 | + |
| 176 | + // Wait for promise to resolve |
| 177 | + await resultPromise; |
| 178 | + |
| 179 | + // Advance past the SIGKILL grace period (3 more seconds) |
| 180 | + await vi.advanceTimersByTimeAsync(3_000); |
| 181 | + |
| 182 | + // Verify SIGKILL was NOT called (timer was cleared) |
| 183 | + expect(mockChildProc.kill).toHaveBeenCalledTimes(1); // Only SIGTERM |
| 184 | + expect(mockChildProc.kill).not.toHaveBeenCalledWith("SIGKILL"); |
| 185 | + }); |
| 186 | +}); |
0 commit comments