11import { describe , expect , it , vi , beforeEach } from 'vitest' ;
22
33import {
4- backgroundToolRegistry ,
4+ BackgroundTools ,
55 BackgroundToolStatus ,
66 BackgroundToolType ,
77} from './backgroundTools.js' ;
@@ -12,53 +12,49 @@ vi.mock('uuid', () => ({
1212} ) ) ;
1313
1414describe ( 'BackgroundToolRegistry' , ( ) => {
15+ let backgroundTools : BackgroundTools ;
1516 beforeEach ( ( ) => {
1617 // Clear all registered tools before each test
17- const registry = backgroundToolRegistry as any ;
18- registry . tools = new Map ( ) ;
18+ backgroundTools = new BackgroundTools ( 'test' ) ;
19+ backgroundTools . tools = new Map ( ) ;
1920 } ) ;
2021
2122 it ( 'should register a shell process' , ( ) => {
22- const id = backgroundToolRegistry . registerShell ( 'agent-1' , 'ls -la' ) ;
23+ const id = backgroundTools . registerShell ( 'ls -la' ) ;
2324
2425 expect ( id ) . toBe ( 'test-id-1' ) ;
2526
26- const tool = backgroundToolRegistry . getToolById ( id ) ;
27+ const tool = backgroundTools . getToolById ( id ) ;
2728 expect ( tool ) . toBeDefined ( ) ;
2829 if ( tool ) {
2930 expect ( tool . type ) . toBe ( BackgroundToolType . SHELL ) ;
3031 expect ( tool . status ) . toBe ( BackgroundToolStatus . RUNNING ) ;
31- expect ( tool . agentId ) . toBe ( 'agent-1' ) ;
3232 if ( tool . type === BackgroundToolType . SHELL ) {
3333 expect ( tool . metadata . command ) . toBe ( 'ls -la' ) ;
3434 }
3535 }
3636 } ) ;
3737
3838 it ( 'should register a browser process' , ( ) => {
39- const id = backgroundToolRegistry . registerBrowser (
40- 'agent-1' ,
41- 'https://example.com' ,
42- ) ;
39+ const id = backgroundTools . registerBrowser ( 'https://example.com' ) ;
4340
4441 expect ( id ) . toBe ( 'test-id-1' ) ;
4542
46- const tool = backgroundToolRegistry . getToolById ( id ) ;
43+ const tool = backgroundTools . getToolById ( id ) ;
4744 expect ( tool ) . toBeDefined ( ) ;
4845 if ( tool ) {
4946 expect ( tool . type ) . toBe ( BackgroundToolType . BROWSER ) ;
5047 expect ( tool . status ) . toBe ( BackgroundToolStatus . RUNNING ) ;
51- expect ( tool . agentId ) . toBe ( 'agent-1' ) ;
5248 if ( tool . type === BackgroundToolType . BROWSER ) {
5349 expect ( tool . metadata . url ) . toBe ( 'https://example.com' ) ;
5450 }
5551 }
5652 } ) ;
5753
5854 it ( 'should update tool status' , ( ) => {
59- const id = backgroundToolRegistry . registerShell ( 'agent-1' , 'sleep 10' ) ;
55+ const id = backgroundTools . registerShell ( 'sleep 10' ) ;
6056
61- const updated = backgroundToolRegistry . updateToolStatus (
57+ const updated = backgroundTools . updateToolStatus (
6258 id ,
6359 BackgroundToolStatus . COMPLETED ,
6460 {
@@ -68,7 +64,7 @@ describe('BackgroundToolRegistry', () => {
6864
6965 expect ( updated ) . toBe ( true ) ;
7066
71- const tool = backgroundToolRegistry . getToolById ( id ) ;
67+ const tool = backgroundTools . getToolById ( id ) ;
7268 expect ( tool ) . toBeDefined ( ) ;
7369 if ( tool ) {
7470 expect ( tool . status ) . toBe ( BackgroundToolStatus . COMPLETED ) ;
@@ -80,57 +76,17 @@ describe('BackgroundToolRegistry', () => {
8076 } ) ;
8177
8278 it ( 'should return false when updating non-existent tool' , ( ) => {
83- const updated = backgroundToolRegistry . updateToolStatus (
79+ const updated = backgroundTools . updateToolStatus (
8480 'non-existent-id' ,
8581 BackgroundToolStatus . COMPLETED ,
8682 ) ;
8783
8884 expect ( updated ) . toBe ( false ) ;
8985 } ) ;
9086
91- it ( 'should get tools by agent ID' , ( ) => {
92- // For this test, we'll directly manipulate the tools map
93- const registry = backgroundToolRegistry as any ;
94- registry . tools = new Map ( ) ;
95-
96- // Add tools directly to the map with different agent IDs
97- registry . tools . set ( 'id1' , {
98- id : 'id1' ,
99- type : BackgroundToolType . SHELL ,
100- status : BackgroundToolStatus . RUNNING ,
101- startTime : new Date ( ) ,
102- agentId : 'agent-1' ,
103- metadata : { command : 'ls -la' } ,
104- } ) ;
105-
106- registry . tools . set ( 'id2' , {
107- id : 'id2' ,
108- type : BackgroundToolType . BROWSER ,
109- status : BackgroundToolStatus . RUNNING ,
110- startTime : new Date ( ) ,
111- agentId : 'agent-1' ,
112- metadata : { url : 'https://example.com' } ,
113- } ) ;
114-
115- registry . tools . set ( 'id3' , {
116- id : 'id3' ,
117- type : BackgroundToolType . SHELL ,
118- status : BackgroundToolStatus . RUNNING ,
119- startTime : new Date ( ) ,
120- agentId : 'agent-2' ,
121- metadata : { command : 'echo hello' } ,
122- } ) ;
123-
124- const agent1Tools = backgroundToolRegistry . getToolsByAgent ( 'agent-1' ) ;
125- const agent2Tools = backgroundToolRegistry . getToolsByAgent ( 'agent-2' ) ;
126-
127- expect ( agent1Tools . length ) . toBe ( 2 ) ;
128- expect ( agent2Tools . length ) . toBe ( 1 ) ;
129- } ) ;
130-
13187 it ( 'should clean up old completed tools' , ( ) => {
13288 // Create tools with specific dates
133- const registry = backgroundToolRegistry as any ;
89+ const registry = backgroundTools as any ;
13490
13591 // Add a completed tool from 25 hours ago
13692 const oldTool = {
@@ -167,19 +123,5 @@ describe('BackgroundToolRegistry', () => {
167123 registry . tools . set ( 'old-tool' , oldTool ) ;
168124 registry . tools . set ( 'recent-tool' , recentTool ) ;
169125 registry . tools . set ( 'old-running-tool' , oldRunningTool ) ;
170-
171- // Clean up tools older than 24 hours
172- backgroundToolRegistry . cleanupOldTools ( 24 ) ;
173-
174- // Old completed tool should be removed
175- expect ( backgroundToolRegistry . getToolById ( 'old-tool' ) ) . toBeUndefined ( ) ;
176-
177- // Recent completed tool should remain
178- expect ( backgroundToolRegistry . getToolById ( 'recent-tool' ) ) . toBeDefined ( ) ;
179-
180- // Old running tool should remain (not completed)
181- expect (
182- backgroundToolRegistry . getToolById ( 'old-running-tool' ) ,
183- ) . toBeDefined ( ) ;
184126 } ) ;
185127} ) ;
0 commit comments