@@ -2,36 +2,27 @@ import { describe, expect, it, vi, beforeEach, afterEach } from 'vitest';
22
33// Import mocked modules
44import { BrowserManager } from '../tools/browser/BrowserManager.js' ;
5- import { agentStates } from '../tools/interaction/agentStart .js' ;
5+ import { agentTracker } from '../tools/interaction/agentTracker .js' ;
66import { processStates } from '../tools/system/shellStart.js' ;
77
88import { BackgroundTools , BackgroundToolStatus } from './backgroundTools' ;
9- import { Tool } from './types' ;
9+
10+ // Import the ChildProcess type for mocking
11+ import type { ChildProcess } from 'child_process' ;
1012
1113// Define types for our mocks that match the actual types
1214type 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 ;
2920 } ;
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 ;
3526} ;
3627
3728// Mock dependencies
@@ -49,9 +40,28 @@ vi.mock('../tools/system/shellStart.js', () => {
4940 } ;
5041} ) ;
5142
52- vi . mock ( '../tools/interaction/agentStart .js' , ( ) => {
43+ vi . mock ( '../tools/interaction/agentTracker .js' , ( ) => {
5344 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+ } ,
5565 } ;
5666} ) ;
5767
@@ -75,11 +85,19 @@ describe('BackgroundTools cleanup', () => {
7585 // Setup mock process states
7686 const mockProcess = {
7787 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 > } ;
7993
8094 const mockProcessState : MockProcessState = {
8195 process : mockProcess ,
82- state : { completed : false } ,
96+ state : {
97+ completed : false ,
98+ signaled : false ,
99+ exitCode : null ,
100+ } ,
83101 command : 'test command' ,
84102 stdout : [ ] ,
85103 stderr : [ ] ,
@@ -88,26 +106,13 @@ describe('BackgroundTools cleanup', () => {
88106 } ;
89107
90108 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+ ) ;
108113
109- agentStates . clear ( ) ;
110- agentStates . set ( 'agent-1' , mockAgentState as any ) ;
114+ // Reset the agentTracker mock
115+ vi . mocked ( agentTracker . terminateAgent ) . mockClear ( ) ;
111116 } ) ;
112117
113118 afterEach ( ( ) => {
@@ -120,7 +125,6 @@ describe('BackgroundTools cleanup', () => {
120125
121126 // Clear mock states
122127 processStates . clear ( ) ;
123- agentStates . clear ( ) ;
124128 } ) ;
125129
126130 it ( 'should clean up browser sessions' , async ( ) => {
@@ -149,7 +153,10 @@ describe('BackgroundTools cleanup', () => {
149153 const mockProcessState = processStates . get ( 'shell-1' ) ;
150154
151155 // 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+ ) ;
153160
154161 // Run cleanup
155162 await backgroundTools . cleanup ( ) ;
@@ -166,21 +173,11 @@ describe('BackgroundTools cleanup', () => {
166173 // Register an agent tool
167174 const agentId = backgroundTools . registerAgent ( 'Test goal' ) ;
168175
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-
175176 // Run cleanup
176177 await backgroundTools . cleanup ( ) ;
177178
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 ) ;
184181
185182 // Check that tool status was updated
186183 const tool = backgroundTools . getToolById ( agentId ) ;
0 commit comments