| 1 | import { ChildProcess } from "child_process"; |
| 2 | import { |
| 3 | clearAllBackgroundProcesses, |
| 4 | getAllBackgroundedProcessIds, |
| 5 | getAllRunningProcessIds, |
| 6 | getRunningProcess, |
| 7 | isProcessBackgrounded, |
| 8 | isProcessRunning, |
| 9 | killAllRunningTerminalProcesses, |
| 10 | killMultipleTerminalProcesses, |
| 11 | killTerminalProcess, |
| 12 | markProcessAsBackgrounded, |
| 13 | markProcessAsRunning, |
| 14 | removeBackgroundedProcess, |
| 15 | removeRunningProcess, |
| 16 | updateProcessOutput, |
| 17 | } from "./processTerminalStates"; |
| 18 | |
| 19 | // Mock ChildProcess |
| 20 | const createMockProcess = ( |
| 21 | pid: number = 123, |
| 22 | killed: boolean = false, |
| 23 | ): jest.Mocked<ChildProcess> => { |
| 24 | const mockProcess = { |
| 25 | pid, |
| 26 | killed, |
| 27 | kill: jest.fn(), |
| 28 | } as unknown as jest.Mocked<ChildProcess>; |
| 29 | |
| 30 | // Make kill() update the killed property |
| 31 | mockProcess.kill.mockImplementation(() => { |
| 32 | (mockProcess as any).killed = true; |
| 33 | return true; |
| 34 | }); |
| 35 | |
| 36 | return mockProcess; |
| 37 | }; |
| 38 | |
| 39 | describe("processTerminalStates", () => { |
| 40 | beforeEach(() => { |
| 41 | // Clear all running processes |
| 42 | const runningIds = getAllRunningProcessIds(); |
| 43 | runningIds.forEach((id) => removeRunningProcess(id)); |
| 44 | |
| 45 | // Clear all backgrounded processes |
| 46 | const backgroundedIds = getAllBackgroundedProcessIds(); |
| 47 | backgroundedIds.forEach((id) => removeBackgroundedProcess(id)); |
| 48 | |
| 49 | // Clear all timers |
| 50 | jest.clearAllTimers(); |
| 51 | }); |
| 52 | |
| 53 | test("should mark a process as backgrounded", () => { |
| 54 | const toolCallId = "test-123"; |
| 55 | markProcessAsBackgrounded(toolCallId); |
| 56 | expect(isProcessBackgrounded(toolCallId)).toBe(true); |
| 57 | }); |
| 58 | |
| 59 | test("should correctly identify non-backgrounded processes", () => { |
| 60 | const toolCallId = "test-123"; |
| 61 | const anotherToolCallId = "test-456"; |
| 62 | |
| 63 | markProcessAsBackgrounded(toolCallId); |
| 64 | |
| 65 | expect(isProcessBackgrounded(toolCallId)).toBe(true); |
| 66 | expect(isProcessBackgrounded(anotherToolCallId)).toBe(false); |
| 67 | }); |
| 68 | |
| 69 | test("should remove a process from backgrounded state", () => { |
| 70 | const toolCallId = "test-123"; |
| 71 | |
| 72 | markProcessAsBackgrounded(toolCallId); |
| 73 | expect(isProcessBackgrounded(toolCallId)).toBe(true); |
| 74 | |
| 75 | removeBackgroundedProcess(toolCallId); |
| 76 | expect(isProcessBackgrounded(toolCallId)).toBe(false); |
| 77 | }); |
| 78 | |
| 79 | test("should handle removing non-existent processes", () => { |
| 80 | const toolCallId = "test-123"; |
| 81 | |
| 82 | // Should not throw an error |
| 83 | removeBackgroundedProcess(toolCallId); |
| 84 | expect(isProcessBackgrounded(toolCallId)).toBe(false); |
| 85 | }); |
| 86 | |
| 87 | test("should handle multiple processes", () => { |
| 88 | const toolCallId1 = "test-123"; |
| 89 | const toolCallId2 = "test-456"; |
| 90 | const toolCallId3 = "test-789"; |
| 91 | |
| 92 | markProcessAsBackgrounded(toolCallId1); |
| 93 | markProcessAsBackgrounded(toolCallId2); |
| 94 | |
| 95 | expect(isProcessBackgrounded(toolCallId1)).toBe(true); |
| 96 | expect(isProcessBackgrounded(toolCallId2)).toBe(true); |
| 97 | expect(isProcessBackgrounded(toolCallId3)).toBe(false); |
| 98 | |
| 99 | removeBackgroundedProcess(toolCallId1); |
| 100 | |
| 101 | expect(isProcessBackgrounded(toolCallId1)).toBe(false); |
| 102 | expect(isProcessBackgrounded(toolCallId2)).toBe(true); |
| 103 | }); |
| 104 | |
| 105 | describe("foreground process functions", () => { |
| 106 | test("should mark a process as running", () => { |
| 107 | const toolCallId = "test-123"; |
| 108 | const mockProcess = createMockProcess(); |
| 109 | const mockCallback = jest.fn(); |
| 110 | const initialOutput = "initial output"; |
| 111 | |
| 112 | markProcessAsRunning( |
| 113 | toolCallId, |
| 114 | mockProcess, |
| 115 | mockCallback, |
| 116 | initialOutput, |
| 117 | ); |
| 118 | |
| 119 | expect(isProcessRunning(toolCallId)).toBe(true); |
| 120 | expect(getRunningProcess(toolCallId)).toBe(mockProcess); |
| 121 | }); |
| 122 | |
| 123 | test("should handle process without callback or initial output", () => { |
| 124 | const toolCallId = "test-123"; |
| 125 | const mockProcess = createMockProcess(); |
| 126 | |
| 127 | markProcessAsRunning(toolCallId, mockProcess); |
| 128 | |
| 129 | expect(isProcessRunning(toolCallId)).toBe(true); |
| 130 | expect(getRunningProcess(toolCallId)).toBe(mockProcess); |
| 131 | }); |
| 132 | |
| 133 | test("should correctly identify non-running processes", () => { |
| 134 | const toolCallId = "test-123"; |
| 135 | const anotherToolCallId = "test-456"; |
| 136 | const mockProcess = createMockProcess(); |
| 137 | |
| 138 | markProcessAsRunning(toolCallId, mockProcess); |
| 139 | |
| 140 | expect(isProcessRunning(toolCallId)).toBe(true); |
| 141 | expect(isProcessRunning(anotherToolCallId)).toBe(false); |
| 142 | }); |
| 143 | |
| 144 | test("should update process output", () => { |
| 145 | const toolCallId = "test-123"; |
| 146 | const mockProcess = createMockProcess(); |
| 147 | const initialOutput = "initial"; |
| 148 | const updatedOutput = "updated output"; |
| 149 | |
| 150 | markProcessAsRunning(toolCallId, mockProcess, undefined, initialOutput); |
| 151 | updateProcessOutput(toolCallId, updatedOutput); |
| 152 | |
| 153 | // We can't directly test the output value, but we can ensure the function doesn't throw |
| 154 | expect(() => |
| 155 | updateProcessOutput(toolCallId, updatedOutput), |
| 156 | ).not.toThrow(); |
| 157 | }); |
| 158 | |
| 159 | test("should handle updating output for non-existent process", () => { |
| 160 | const toolCallId = "non-existent"; |
| 161 | |
| 162 | expect(() => |
| 163 | updateProcessOutput(toolCallId, "some output"), |
| 164 | ).not.toThrow(); |
| 165 | }); |
| 166 | |
| 167 | test("should remove a process from running state", () => { |
| 168 | const toolCallId = "test-123"; |
| 169 | const mockProcess = createMockProcess(); |
| 170 | |
| 171 | markProcessAsRunning(toolCallId, mockProcess); |
| 172 | expect(isProcessRunning(toolCallId)).toBe(true); |
| 173 | |
| 174 | removeRunningProcess(toolCallId); |
| 175 | expect(isProcessRunning(toolCallId)).toBe(false); |
| 176 | expect(getRunningProcess(toolCallId)).toBeUndefined(); |
| 177 | }); |
| 178 | |
| 179 | test("should handle removing non-existent running processes", () => { |
| 180 | const toolCallId = "test-123"; |
| 181 | |
| 182 | expect(() => removeRunningProcess(toolCallId)).not.toThrow(); |
| 183 | expect(isProcessRunning(toolCallId)).toBe(false); |
| 184 | }); |
| 185 | |
| 186 | test("should return undefined for non-existent running process", () => { |
| 187 | const toolCallId = "non-existent"; |
| 188 | |
| 189 | expect(getRunningProcess(toolCallId)).toBeUndefined(); |
| 190 | }); |
| 191 | }); |
| 192 | |
| 193 | describe("terminal command cancellation", () => { |
| 194 | beforeEach(() => { |
| 195 | jest.useFakeTimers(); |
| 196 | }); |
| 197 | |
| 198 | afterEach(() => { |
| 199 | jest.useRealTimers(); |
| 200 | }); |
| 201 | |
| 202 | test("should cancel a running terminal command", async () => { |
| 203 | const toolCallId = "test-123"; |
| 204 | const mockProcess = createMockProcess(); |
| 205 | |
| 206 | markProcessAsRunning(toolCallId, mockProcess); |
| 207 | expect(isProcessRunning(toolCallId)).toBe(true); |
| 208 | |
| 209 | await killTerminalProcess(toolCallId); |
| 210 | |
| 211 | expect(mockProcess.kill).toHaveBeenCalledWith("SIGTERM"); |
| 212 | expect(isProcessRunning(toolCallId)).toBe(false); |
| 213 | }); |
| 214 | |
| 215 | test("should handle cancelling non-existent process", async () => { |
| 216 | const toolCallId = "non-existent"; |
| 217 | |
| 218 | await expect(killTerminalProcess(toolCallId)).resolves.not.toThrow(); |
| 219 | }); |
| 220 | |
| 221 | test("should handle cancelling already killed process", async () => { |
| 222 | const toolCallId = "test-123"; |
| 223 | const mockProcess = createMockProcess(123, true); // Already killed |
| 224 | |
| 225 | markProcessAsRunning(toolCallId, mockProcess); |
| 226 | |
| 227 | await killTerminalProcess(toolCallId); |
| 228 | |
| 229 | expect(mockProcess.kill).not.toHaveBeenCalled(); |
| 230 | }); |
| 231 | |
| 232 | test("should send SIGKILL after timeout if process not killed", async () => { |
| 233 | const toolCallId = "test-123"; |
| 234 | const mockProcess = createMockProcess(); |
| 235 | |
| 236 | // Mock kill to not actually set killed=true for SIGTERM |
| 237 | mockProcess.kill.mockImplementation((signal) => { |
| 238 | if (signal === "SIGKILL") { |
| 239 | (mockProcess as any).killed = true; |
| 240 | } |
| 241 | return true; |
| 242 | }); |
| 243 | |
| 244 | markProcessAsRunning(toolCallId, mockProcess); |
| 245 | |
| 246 | const cancelPromise = killTerminalProcess(toolCallId); |
| 247 | |
| 248 | // Fast-forward time to trigger the timeout |
| 249 | jest.advanceTimersByTime(5000); |
| 250 | |
| 251 | await cancelPromise; |
| 252 | |
| 253 | expect(mockProcess.kill).toHaveBeenCalledWith("SIGTERM"); |
| 254 | expect(mockProcess.kill).toHaveBeenCalledWith("SIGKILL"); |
| 255 | }); |
| 256 | |
| 257 | test("should not send SIGKILL if process was already killed", async () => { |
| 258 | const toolCallId = "test-123"; |
| 259 | const mockProcess = createMockProcess(); |
| 260 | |
| 261 | markProcessAsRunning(toolCallId, mockProcess); |
| 262 | |
| 263 | const cancelPromise = killTerminalProcess(toolCallId); |
| 264 | |
| 265 | // Fast-forward time to trigger the timeout |
| 266 | jest.advanceTimersByTime(5000); |
| 267 | |
| 268 | await cancelPromise; |
| 269 | |
| 270 | expect(mockProcess.kill).toHaveBeenCalledWith("SIGTERM"); |
| 271 | expect(mockProcess.kill).toHaveBeenCalledTimes(1); // Only SIGTERM, no SIGKILL |
| 272 | }); |
| 273 | }); |
| 274 | |
| 275 | describe("utility functions", () => { |
| 276 | test("should get all running process IDs", () => { |
| 277 | const toolCallId1 = "test-123"; |
| 278 | const toolCallId2 = "test-456"; |
| 279 | const mockProcess1 = createMockProcess(123); |
| 280 | const mockProcess2 = createMockProcess(456); |
| 281 | |
| 282 | markProcessAsRunning(toolCallId1, mockProcess1); |
| 283 | markProcessAsRunning(toolCallId2, mockProcess2); |
| 284 | |
| 285 | const runningIds = getAllRunningProcessIds(); |
| 286 | expect(runningIds).toContain(toolCallId1); |
| 287 | expect(runningIds).toContain(toolCallId2); |
| 288 | expect(runningIds).toHaveLength(2); |
| 289 | }); |
| 290 | |
| 291 | test("should get all backgrounded process IDs", () => { |
| 292 | const toolCallId1 = "test-123"; |
| 293 | const toolCallId2 = "test-456"; |
| 294 | |
| 295 | markProcessAsBackgrounded(toolCallId1); |
| 296 | markProcessAsBackgrounded(toolCallId2); |
| 297 | |
| 298 | const backgroundedIds = getAllBackgroundedProcessIds(); |
| 299 | expect(backgroundedIds).toContain(toolCallId1); |
| 300 | expect(backgroundedIds).toContain(toolCallId2); |
| 301 | expect(backgroundedIds).toHaveLength(2); |
| 302 | }); |
| 303 | |
| 304 | test("should return empty arrays when no processes", () => { |
| 305 | expect(getAllRunningProcessIds()).toEqual([]); |
| 306 | expect(getAllBackgroundedProcessIds()).toEqual([]); |
| 307 | }); |
| 308 | |
| 309 | test("should cancel multiple terminal commands", async () => { |
| 310 | const toolCallId1 = "test-123"; |
| 311 | const toolCallId2 = "test-456"; |
| 312 | const mockProcess1 = createMockProcess(123); |
| 313 | const mockProcess2 = createMockProcess(456); |
| 314 | |
| 315 | markProcessAsRunning(toolCallId1, mockProcess1); |
| 316 | markProcessAsRunning(toolCallId2, mockProcess2); |
| 317 | |
| 318 | await killMultipleTerminalProcesses([toolCallId1, toolCallId2]); |
| 319 | |
| 320 | expect(mockProcess1.kill).toHaveBeenCalledWith("SIGTERM"); |
| 321 | expect(mockProcess2.kill).toHaveBeenCalledWith("SIGTERM"); |
| 322 | expect(isProcessRunning(toolCallId1)).toBe(false); |
| 323 | expect(isProcessRunning(toolCallId2)).toBe(false); |
| 324 | }); |
| 325 | |
| 326 | test("should cancel all running terminal commands", async () => { |
| 327 | const toolCallId1 = "test-123"; |
| 328 | const toolCallId2 = "test-456"; |
| 329 | const mockProcess1 = createMockProcess(123); |
| 330 | const mockProcess2 = createMockProcess(456); |
| 331 | |
| 332 | markProcessAsRunning(toolCallId1, mockProcess1); |
| 333 | markProcessAsRunning(toolCallId2, mockProcess2); |
| 334 | |
| 335 | const cancelledIds = await killAllRunningTerminalProcesses(); |
| 336 | |
| 337 | expect(cancelledIds).toContain(toolCallId1); |
| 338 | expect(cancelledIds).toContain(toolCallId2); |
| 339 | expect(cancelledIds).toHaveLength(2); |
| 340 | expect(mockProcess1.kill).toHaveBeenCalledWith("SIGTERM"); |
| 341 | expect(mockProcess2.kill).toHaveBeenCalledWith("SIGTERM"); |
| 342 | expect(isProcessRunning(toolCallId1)).toBe(false); |
| 343 | expect(isProcessRunning(toolCallId2)).toBe(false); |
| 344 | }); |
| 345 | |
| 346 | test("should return empty array when no running commands to cancel", async () => { |
| 347 | const cancelledIds = await killAllRunningTerminalProcesses(); |
| 348 | expect(cancelledIds).toEqual([]); |
| 349 | }); |
| 350 | |
| 351 | test("should clear all background processes", () => { |
| 352 | const toolCallId1 = "test-123"; |
| 353 | const toolCallId2 = "test-456"; |
| 354 | |
| 355 | markProcessAsBackgrounded(toolCallId1); |
| 356 | markProcessAsBackgrounded(toolCallId2); |
| 357 | |
| 358 | expect(getAllBackgroundedProcessIds()).toHaveLength(2); |
| 359 | |
| 360 | clearAllBackgroundProcesses(); |
| 361 | |
| 362 | expect(getAllBackgroundedProcessIds()).toEqual([]); |
| 363 | expect(isProcessBackgrounded(toolCallId1)).toBe(false); |
| 364 | expect(isProcessBackgrounded(toolCallId2)).toBe(false); |
| 365 | }); |
| 366 | }); |
| 367 | }); |