1
1
import { describe , expect , it , vi , beforeEach , afterEach } from 'vitest' ;
2
2
3
3
// Import mocked modules
4
- import { BrowserManager } from '../tools/browser/BrowserManager.js' ;
5
4
import { agentStates } from '../tools/interaction/agentStart.js' ;
6
5
import { processStates } from '../tools/system/shellStart.js' ;
7
6
@@ -35,14 +34,6 @@ type MockAgentState = {
35
34
} ;
36
35
37
36
// 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
37
vi . mock ( '../tools/system/shellStart.js' , ( ) => {
47
38
return {
48
39
processStates : new Map < string , MockProcessState > ( ) ,
@@ -58,20 +49,13 @@ vi.mock('../tools/interaction/agentStart.js', () => {
58
49
describe ( 'BackgroundTools cleanup' , ( ) => {
59
50
let backgroundTools : BackgroundTools ;
60
51
61
- // Setup mocks for globalThis and process states
52
+ // Setup mocks for process states
62
53
beforeEach ( ( ) => {
63
54
backgroundTools = new BackgroundTools ( 'test-agent' ) ;
64
55
65
56
// Reset mocks
66
57
vi . resetAllMocks ( ) ;
67
58
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
59
// Setup mock process states
76
60
const mockProcess = {
77
61
kill : vi . fn ( ) ,
@@ -113,34 +97,11 @@ describe('BackgroundTools cleanup', () => {
113
97
afterEach ( ( ) => {
114
98
vi . resetAllMocks ( ) ;
115
99
116
- // Clear global browser manager
117
- (
118
- globalThis as unknown as { __BROWSER_MANAGER__ ?: BrowserManager }
119
- ) . __BROWSER_MANAGER__ = undefined ;
120
-
121
100
// Clear mock states
122
101
processStates . clear ( ) ;
123
102
agentStates . clear ( ) ;
124
103
} ) ;
125
104
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
105
it ( 'should clean up shell processes' , async ( ) => {
145
106
// Register a shell tool
146
107
const shellId = backgroundTools . registerShell ( 'echo "test"' ) ;
@@ -186,38 +147,4 @@ describe('BackgroundTools cleanup', () => {
186
147
const tool = backgroundTools . getToolById ( agentId ) ;
187
148
expect ( tool ?. status ) . toBe ( BackgroundToolStatus . TERMINATED ) ;
188
149
} ) ;
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
150
} ) ;
0 commit comments