|
| 1 | +import { describe, expect, it, vi, beforeEach, afterEach } from 'vitest'; |
| 2 | + |
| 3 | +// Import mocked modules |
| 4 | +import { BrowserManager } from '../tools/browser/BrowserManager.js'; |
| 5 | +import { agentStates } from '../tools/interaction/agentStart.js'; |
| 6 | +import { processStates } from '../tools/system/shellStart.js'; |
| 7 | + |
| 8 | +import { BackgroundTools, BackgroundToolStatus } from './backgroundTools'; |
| 9 | +import { Tool } from './types'; |
| 10 | + |
| 11 | +// Define types for our mocks that match the actual types |
| 12 | +type MockProcessState = { |
| 13 | + process: { kill: ReturnType<typeof vi.fn> }; |
| 14 | + state: { completed: boolean }; |
| 15 | + command?: string; |
| 16 | + stdout?: string[]; |
| 17 | + stderr?: string[]; |
| 18 | + showStdIn?: boolean; |
| 19 | + showStdout?: boolean; |
| 20 | +}; |
| 21 | + |
| 22 | +type MockAgentState = { |
| 23 | + aborted: boolean; |
| 24 | + completed: boolean; |
| 25 | + context: { |
| 26 | + backgroundTools: { |
| 27 | + cleanup: ReturnType<typeof vi.fn>; |
| 28 | + }; |
| 29 | + }; |
| 30 | + goal?: string; |
| 31 | + prompt?: string; |
| 32 | + output?: string; |
| 33 | + workingDirectory?: string; |
| 34 | + tools?: Tool[]; |
| 35 | +}; |
| 36 | + |
| 37 | +// Mock dependencies |
| 38 | +vi.mock('../tools/browser/BrowserManager.js', () => { |
| 39 | + return { |
| 40 | + BrowserManager: class MockBrowserManager { |
| 41 | + closeSession = vi.fn().mockResolvedValue(undefined); |
| 42 | + }, |
| 43 | + }; |
| 44 | +}); |
| 45 | + |
| 46 | +vi.mock('../tools/system/shellStart.js', () => { |
| 47 | + return { |
| 48 | + processStates: new Map<string, MockProcessState>(), |
| 49 | + }; |
| 50 | +}); |
| 51 | + |
| 52 | +vi.mock('../tools/interaction/agentStart.js', () => { |
| 53 | + return { |
| 54 | + agentStates: new Map<string, MockAgentState>(), |
| 55 | + }; |
| 56 | +}); |
| 57 | + |
| 58 | +describe('BackgroundTools cleanup', () => { |
| 59 | + let backgroundTools: BackgroundTools; |
| 60 | + |
| 61 | + // Setup mocks for globalThis and process states |
| 62 | + beforeEach(() => { |
| 63 | + backgroundTools = new BackgroundTools('test-agent'); |
| 64 | + |
| 65 | + // Reset mocks |
| 66 | + vi.resetAllMocks(); |
| 67 | + |
| 68 | + // Setup global browser manager |
| 69 | + ( |
| 70 | + globalThis as unknown as { __BROWSER_MANAGER__: BrowserManager } |
| 71 | + ).__BROWSER_MANAGER__ = { |
| 72 | + closeSession: vi.fn().mockResolvedValue(undefined), |
| 73 | + } as unknown as BrowserManager; |
| 74 | + |
| 75 | + // Setup mock process states |
| 76 | + const mockProcess = { |
| 77 | + kill: vi.fn(), |
| 78 | + }; |
| 79 | + |
| 80 | + const mockProcessState: MockProcessState = { |
| 81 | + process: mockProcess, |
| 82 | + state: { completed: false }, |
| 83 | + command: 'test command', |
| 84 | + stdout: [], |
| 85 | + stderr: [], |
| 86 | + showStdIn: false, |
| 87 | + showStdout: false, |
| 88 | + }; |
| 89 | + |
| 90 | + processStates.clear(); |
| 91 | + processStates.set('shell-1', mockProcessState as any); |
| 92 | + |
| 93 | + // Setup mock agent states |
| 94 | + const mockAgentState: MockAgentState = { |
| 95 | + aborted: false, |
| 96 | + completed: false, |
| 97 | + context: { |
| 98 | + backgroundTools: { |
| 99 | + cleanup: vi.fn().mockResolvedValue(undefined), |
| 100 | + }, |
| 101 | + }, |
| 102 | + goal: 'test goal', |
| 103 | + prompt: 'test prompt', |
| 104 | + output: '', |
| 105 | + workingDirectory: '/test', |
| 106 | + tools: [], |
| 107 | + }; |
| 108 | + |
| 109 | + agentStates.clear(); |
| 110 | + agentStates.set('agent-1', mockAgentState as any); |
| 111 | + }); |
| 112 | + |
| 113 | + afterEach(() => { |
| 114 | + vi.resetAllMocks(); |
| 115 | + |
| 116 | + // Clear global browser manager |
| 117 | + ( |
| 118 | + globalThis as unknown as { __BROWSER_MANAGER__?: BrowserManager } |
| 119 | + ).__BROWSER_MANAGER__ = undefined; |
| 120 | + |
| 121 | + // Clear mock states |
| 122 | + processStates.clear(); |
| 123 | + agentStates.clear(); |
| 124 | + }); |
| 125 | + |
| 126 | + it('should clean up browser sessions', async () => { |
| 127 | + // Register a browser tool |
| 128 | + const browserId = backgroundTools.registerBrowser('https://example.com'); |
| 129 | + |
| 130 | + // Run cleanup |
| 131 | + await backgroundTools.cleanup(); |
| 132 | + |
| 133 | + // Check that closeSession was called |
| 134 | + expect( |
| 135 | + (globalThis as unknown as { __BROWSER_MANAGER__: BrowserManager }) |
| 136 | + .__BROWSER_MANAGER__.closeSession, |
| 137 | + ).toHaveBeenCalledWith(browserId); |
| 138 | + |
| 139 | + // Check that tool status was updated |
| 140 | + const tool = backgroundTools.getToolById(browserId); |
| 141 | + expect(tool?.status).toBe(BackgroundToolStatus.COMPLETED); |
| 142 | + }); |
| 143 | + |
| 144 | + it('should clean up shell processes', async () => { |
| 145 | + // Register a shell tool |
| 146 | + const shellId = backgroundTools.registerShell('echo "test"'); |
| 147 | + |
| 148 | + // Get mock process state |
| 149 | + const mockProcessState = processStates.get('shell-1'); |
| 150 | + |
| 151 | + // Set the shell ID to match |
| 152 | + processStates.set(shellId, processStates.get('shell-1') as any); |
| 153 | + |
| 154 | + // Run cleanup |
| 155 | + await backgroundTools.cleanup(); |
| 156 | + |
| 157 | + // Check that kill was called |
| 158 | + expect(mockProcessState?.process.kill).toHaveBeenCalledWith('SIGTERM'); |
| 159 | + |
| 160 | + // Check that tool status was updated |
| 161 | + const tool = backgroundTools.getToolById(shellId); |
| 162 | + expect(tool?.status).toBe(BackgroundToolStatus.COMPLETED); |
| 163 | + }); |
| 164 | + |
| 165 | + it('should clean up sub-agents', async () => { |
| 166 | + // Register an agent tool |
| 167 | + const agentId = backgroundTools.registerAgent('Test goal'); |
| 168 | + |
| 169 | + // Get mock agent state |
| 170 | + const mockAgentState = agentStates.get('agent-1'); |
| 171 | + |
| 172 | + // Set the agent ID to match |
| 173 | + agentStates.set(agentId, agentStates.get('agent-1') as any); |
| 174 | + |
| 175 | + // Run cleanup |
| 176 | + await backgroundTools.cleanup(); |
| 177 | + |
| 178 | + // Check that agent was marked as aborted |
| 179 | + expect(mockAgentState?.aborted).toBe(true); |
| 180 | + expect(mockAgentState?.completed).toBe(true); |
| 181 | + |
| 182 | + // Check that cleanup was called on the agent's background tools |
| 183 | + expect(mockAgentState?.context.backgroundTools.cleanup).toHaveBeenCalled(); |
| 184 | + |
| 185 | + // Check that tool status was updated |
| 186 | + const tool = backgroundTools.getToolById(agentId); |
| 187 | + expect(tool?.status).toBe(BackgroundToolStatus.TERMINATED); |
| 188 | + }); |
| 189 | + |
| 190 | + it('should handle errors during cleanup', async () => { |
| 191 | + // Register a browser tool |
| 192 | + const browserId = backgroundTools.registerBrowser('https://example.com'); |
| 193 | + |
| 194 | + // Make closeSession throw an error |
| 195 | + ( |
| 196 | + (globalThis as unknown as { __BROWSER_MANAGER__: BrowserManager }) |
| 197 | + .__BROWSER_MANAGER__.closeSession as ReturnType<typeof vi.fn> |
| 198 | + ).mockRejectedValue(new Error('Test error')); |
| 199 | + |
| 200 | + // Run cleanup |
| 201 | + await backgroundTools.cleanup(); |
| 202 | + |
| 203 | + // Check that tool status was updated to ERROR |
| 204 | + const tool = backgroundTools.getToolById(browserId); |
| 205 | + expect(tool?.status).toBe(BackgroundToolStatus.ERROR); |
| 206 | + expect(tool?.metadata.error).toBe('Test error'); |
| 207 | + }); |
| 208 | + |
| 209 | + it('should only clean up running tools', async () => { |
| 210 | + // Register a browser tool and mark it as completed |
| 211 | + const browserId = backgroundTools.registerBrowser('https://example.com'); |
| 212 | + backgroundTools.updateToolStatus(browserId, BackgroundToolStatus.COMPLETED); |
| 213 | + |
| 214 | + // Run cleanup |
| 215 | + await backgroundTools.cleanup(); |
| 216 | + |
| 217 | + // Check that closeSession was not called |
| 218 | + expect( |
| 219 | + (globalThis as unknown as { __BROWSER_MANAGER__: BrowserManager }) |
| 220 | + .__BROWSER_MANAGER__.closeSession, |
| 221 | + ).not.toHaveBeenCalled(); |
| 222 | + }); |
| 223 | +}); |
0 commit comments