11import { describe , expect , it , vi , beforeEach , afterEach } from 'vitest' ;
22
33// Import mocked modules
4- import { BrowserManager } from '../tools/browser/BrowserManager.js' ;
54import { agentStates } from '../tools/interaction/agentStart.js' ;
65import { processStates } from '../tools/system/shellStart.js' ;
76
@@ -35,14 +34,6 @@ type MockAgentState = {
3534} ;
3635
3736// 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-
4637vi . mock ( '../tools/system/shellStart.js' , ( ) => {
4738 return {
4839 processStates : new Map < string , MockProcessState > ( ) ,
@@ -58,20 +49,13 @@ vi.mock('../tools/interaction/agentStart.js', () => {
5849describe ( 'BackgroundTools cleanup' , ( ) => {
5950 let backgroundTools : BackgroundTools ;
6051
61- // Setup mocks for globalThis and process states
52+ // Setup mocks for process states
6253 beforeEach ( ( ) => {
6354 backgroundTools = new BackgroundTools ( 'test-agent' ) ;
6455
6556 // Reset mocks
6657 vi . resetAllMocks ( ) ;
6758
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-
7559 // Setup mock process states
7660 const mockProcess = {
7761 kill : vi . fn ( ) ,
@@ -113,34 +97,11 @@ describe('BackgroundTools cleanup', () => {
11397 afterEach ( ( ) => {
11498 vi . resetAllMocks ( ) ;
11599
116- // Clear global browser manager
117- (
118- globalThis as unknown as { __BROWSER_MANAGER__ ?: BrowserManager }
119- ) . __BROWSER_MANAGER__ = undefined ;
120-
121100 // Clear mock states
122101 processStates . clear ( ) ;
123102 agentStates . clear ( ) ;
124103 } ) ;
125104
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-
144105 it ( 'should clean up shell processes' , async ( ) => {
145106 // Register a shell tool
146107 const shellId = backgroundTools . registerShell ( 'echo "test"' ) ;
@@ -186,38 +147,4 @@ describe('BackgroundTools cleanup', () => {
186147 const tool = backgroundTools . getToolById ( agentId ) ;
187148 expect ( tool ?. status ) . toBe ( BackgroundToolStatus . TERMINATED ) ;
188149 } ) ;
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- } ) ;
223150} ) ;
0 commit comments