@@ -2,36 +2,27 @@ import { describe, expect, it, vi, beforeEach, afterEach } from 'vitest';
2
2
3
3
// Import mocked modules
4
4
import { BrowserManager } from '../tools/browser/BrowserManager.js' ;
5
- import { agentStates } from '../tools/interaction/agentStart .js' ;
5
+ import { agentTracker } from '../tools/interaction/agentTracker .js' ;
6
6
import { processStates } from '../tools/system/shellStart.js' ;
7
7
8
8
import { BackgroundTools , BackgroundToolStatus } from './backgroundTools' ;
9
- import { Tool } from './types' ;
9
+
10
+ // Import the ChildProcess type for mocking
11
+ import type { ChildProcess } from 'child_process' ;
10
12
11
13
// Define types for our mocks that match the actual types
12
14
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
- } ;
15
+ process : ChildProcess & { kill : ReturnType < typeof vi . fn > } ;
16
+ state : {
17
+ completed : boolean ;
18
+ signaled : boolean ;
19
+ exitCode : number | null ;
29
20
} ;
30
- goal ? : string ;
31
- prompt ? : string ;
32
- output ? : string ;
33
- workingDirectory ?: string ;
34
- tools ?: Tool [ ] ;
21
+ command : string ;
22
+ stdout : string [ ] ;
23
+ stderr : string [ ] ;
24
+ showStdIn : boolean ;
25
+ showStdout : boolean ;
35
26
} ;
36
27
37
28
// Mock dependencies
@@ -49,9 +40,28 @@ vi.mock('../tools/system/shellStart.js', () => {
49
40
} ;
50
41
} ) ;
51
42
52
- vi . mock ( '../tools/interaction/agentStart .js' , ( ) => {
43
+ vi . mock ( '../tools/interaction/agentTracker .js' , ( ) => {
53
44
return {
54
- agentStates : new Map < string , MockAgentState > ( ) ,
45
+ agentTracker : {
46
+ terminateAgent : vi . fn ( ) . mockResolvedValue ( undefined ) ,
47
+ getAgentState : vi . fn ( ) . mockImplementation ( ( id : string ) => {
48
+ return {
49
+ id,
50
+ aborted : false ,
51
+ completed : false ,
52
+ context : {
53
+ backgroundTools : {
54
+ cleanup : vi . fn ( ) . mockResolvedValue ( undefined ) ,
55
+ } ,
56
+ } ,
57
+ goal : 'test goal' ,
58
+ prompt : 'test prompt' ,
59
+ output : '' ,
60
+ workingDirectory : '/test' ,
61
+ tools : [ ] ,
62
+ } ;
63
+ } ) ,
64
+ } ,
55
65
} ;
56
66
} ) ;
57
67
@@ -75,11 +85,19 @@ describe('BackgroundTools cleanup', () => {
75
85
// Setup mock process states
76
86
const mockProcess = {
77
87
kill : vi . fn ( ) ,
78
- } ;
88
+ stdin : null ,
89
+ stdout : null ,
90
+ stderr : null ,
91
+ stdio : null ,
92
+ } as unknown as ChildProcess & { kill : ReturnType < typeof vi . fn > } ;
79
93
80
94
const mockProcessState : MockProcessState = {
81
95
process : mockProcess ,
82
- state : { completed : false } ,
96
+ state : {
97
+ completed : false ,
98
+ signaled : false ,
99
+ exitCode : null ,
100
+ } ,
83
101
command : 'test command' ,
84
102
stdout : [ ] ,
85
103
stderr : [ ] ,
@@ -88,26 +106,13 @@ describe('BackgroundTools cleanup', () => {
88
106
} ;
89
107
90
108
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
- } ;
109
+ processStates . set (
110
+ 'shell-1' ,
111
+ mockProcessState as unknown as MockProcessState ,
112
+ ) ;
108
113
109
- agentStates . clear ( ) ;
110
- agentStates . set ( 'agent-1' , mockAgentState as any ) ;
114
+ // Reset the agentTracker mock
115
+ vi . mocked ( agentTracker . terminateAgent ) . mockClear ( ) ;
111
116
} ) ;
112
117
113
118
afterEach ( ( ) => {
@@ -120,7 +125,6 @@ describe('BackgroundTools cleanup', () => {
120
125
121
126
// Clear mock states
122
127
processStates . clear ( ) ;
123
- agentStates . clear ( ) ;
124
128
} ) ;
125
129
126
130
it ( 'should clean up browser sessions' , async ( ) => {
@@ -149,7 +153,10 @@ describe('BackgroundTools cleanup', () => {
149
153
const mockProcessState = processStates . get ( 'shell-1' ) ;
150
154
151
155
// Set the shell ID to match
152
- processStates . set ( shellId , processStates . get ( 'shell-1' ) as any ) ;
156
+ processStates . set (
157
+ shellId ,
158
+ processStates . get ( 'shell-1' ) as unknown as MockProcessState ,
159
+ ) ;
153
160
154
161
// Run cleanup
155
162
await backgroundTools . cleanup ( ) ;
@@ -166,21 +173,11 @@ describe('BackgroundTools cleanup', () => {
166
173
// Register an agent tool
167
174
const agentId = backgroundTools . registerAgent ( 'Test goal' ) ;
168
175
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
176
// Run cleanup
176
177
await backgroundTools . cleanup ( ) ;
177
178
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 ( ) ;
179
+ // Check that terminateAgent was called with the agent ID
180
+ expect ( agentTracker . terminateAgent ) . toHaveBeenCalledWith ( agentId ) ;
184
181
185
182
// Check that tool status was updated
186
183
const tool = backgroundTools . getToolById ( agentId ) ;
0 commit comments