| 1 | import { |
| 2 | markProcessAsBackgrounded, |
| 3 | isProcessBackgrounded, |
| 4 | removeBackgroundedProcess, |
| 5 | } from "./processTerminalBackgroundStates"; |
| 6 | |
| 7 | describe("processTerminalBackgroundStates", () => { |
| 8 | beforeEach(() => { |
| 9 | // Clear any backgrounded processes before each test |
| 10 | const backgroundedProcesses = Array.from( |
| 11 | Object.getOwnPropertySymbols(global), |
| 12 | ).find( |
| 13 | (symbol) => String(symbol) === "Symbol(processTerminalBackgroundStates)", |
| 14 | ); |
| 15 | |
| 16 | if (backgroundedProcesses) { |
| 17 | // Reset the map by reimporting the module |
| 18 | jest.resetModules(); |
| 19 | } |
| 20 | }); |
| 21 | |
| 22 | test("should mark a process as backgrounded", () => { |
| 23 | const toolCallId = "test-123"; |
| 24 | markProcessAsBackgrounded(toolCallId); |
| 25 | expect(isProcessBackgrounded(toolCallId)).toBe(true); |
| 26 | }); |
| 27 | |
| 28 | test("should correctly identify non-backgrounded processes", () => { |
| 29 | const toolCallId = "test-123"; |
| 30 | const anotherToolCallId = "test-456"; |
| 31 | |
| 32 | markProcessAsBackgrounded(toolCallId); |
| 33 | |
| 34 | expect(isProcessBackgrounded(toolCallId)).toBe(true); |
| 35 | expect(isProcessBackgrounded(anotherToolCallId)).toBe(false); |
| 36 | }); |
| 37 | |
| 38 | test("should remove a process from backgrounded state", () => { |
| 39 | const toolCallId = "test-123"; |
| 40 | |
| 41 | markProcessAsBackgrounded(toolCallId); |
| 42 | expect(isProcessBackgrounded(toolCallId)).toBe(true); |
| 43 | |
| 44 | removeBackgroundedProcess(toolCallId); |
| 45 | expect(isProcessBackgrounded(toolCallId)).toBe(false); |
| 46 | }); |
| 47 | |
| 48 | test("should handle removing non-existent processes", () => { |
| 49 | const toolCallId = "test-123"; |
| 50 | |
| 51 | // Should not throw an error |
| 52 | removeBackgroundedProcess(toolCallId); |
| 53 | expect(isProcessBackgrounded(toolCallId)).toBe(false); |
| 54 | }); |
| 55 | |
| 56 | test("should handle multiple processes", () => { |
| 57 | const toolCallId1 = "test-123"; |
| 58 | const toolCallId2 = "test-456"; |
| 59 | const toolCallId3 = "test-789"; |
| 60 | |
| 61 | markProcessAsBackgrounded(toolCallId1); |
| 62 | markProcessAsBackgrounded(toolCallId2); |
| 63 | |
| 64 | expect(isProcessBackgrounded(toolCallId1)).toBe(true); |
| 65 | expect(isProcessBackgrounded(toolCallId2)).toBe(true); |
| 66 | expect(isProcessBackgrounded(toolCallId3)).toBe(false); |
| 67 | |
| 68 | removeBackgroundedProcess(toolCallId1); |
| 69 | |
| 70 | expect(isProcessBackgrounded(toolCallId1)).toBe(false); |
| 71 | expect(isProcessBackgrounded(toolCallId2)).toBe(true); |
| 72 | }); |
| 73 | }); |