|
| 1 | +import { describe, expect, it, vi, beforeEach } from 'vitest'; |
| 2 | + |
| 3 | +import { |
| 4 | + backgroundToolRegistry, |
| 5 | + BackgroundToolStatus, |
| 6 | + BackgroundToolType, |
| 7 | +} from './backgroundTools.js'; |
| 8 | + |
| 9 | +// Mock uuid to return predictable IDs for testing |
| 10 | +vi.mock('uuid', () => ({ |
| 11 | + v4: vi.fn().mockReturnValue('test-id-1'), // Always return the same ID for simplicity in tests |
| 12 | +})); |
| 13 | + |
| 14 | +describe('BackgroundToolRegistry', () => { |
| 15 | + beforeEach(() => { |
| 16 | + // Clear all registered tools before each test |
| 17 | + const registry = backgroundToolRegistry as any; |
| 18 | + registry.tools = new Map(); |
| 19 | + }); |
| 20 | + |
| 21 | + it('should register a shell process', () => { |
| 22 | + const id = backgroundToolRegistry.registerShell('agent-1', 'ls -la'); |
| 23 | + |
| 24 | + expect(id).toBe('test-id-1'); |
| 25 | + |
| 26 | + const tool = backgroundToolRegistry.getToolById(id); |
| 27 | + expect(tool).toBeDefined(); |
| 28 | + if (tool) { |
| 29 | + expect(tool.type).toBe(BackgroundToolType.SHELL); |
| 30 | + expect(tool.status).toBe(BackgroundToolStatus.RUNNING); |
| 31 | + expect(tool.agentId).toBe('agent-1'); |
| 32 | + if (tool.type === BackgroundToolType.SHELL) { |
| 33 | + expect(tool.metadata.command).toBe('ls -la'); |
| 34 | + } |
| 35 | + } |
| 36 | + }); |
| 37 | + |
| 38 | + it('should register a browser process', () => { |
| 39 | + const id = backgroundToolRegistry.registerBrowser( |
| 40 | + 'agent-1', |
| 41 | + 'https://example.com', |
| 42 | + ); |
| 43 | + |
| 44 | + expect(id).toBe('test-id-1'); |
| 45 | + |
| 46 | + const tool = backgroundToolRegistry.getToolById(id); |
| 47 | + expect(tool).toBeDefined(); |
| 48 | + if (tool) { |
| 49 | + expect(tool.type).toBe(BackgroundToolType.BROWSER); |
| 50 | + expect(tool.status).toBe(BackgroundToolStatus.RUNNING); |
| 51 | + expect(tool.agentId).toBe('agent-1'); |
| 52 | + if (tool.type === BackgroundToolType.BROWSER) { |
| 53 | + expect(tool.metadata.url).toBe('https://example.com'); |
| 54 | + } |
| 55 | + } |
| 56 | + }); |
| 57 | + |
| 58 | + it('should update tool status', () => { |
| 59 | + const id = backgroundToolRegistry.registerShell('agent-1', 'sleep 10'); |
| 60 | + |
| 61 | + const updated = backgroundToolRegistry.updateToolStatus( |
| 62 | + id, |
| 63 | + BackgroundToolStatus.COMPLETED, |
| 64 | + { |
| 65 | + exitCode: 0, |
| 66 | + }, |
| 67 | + ); |
| 68 | + |
| 69 | + expect(updated).toBe(true); |
| 70 | + |
| 71 | + const tool = backgroundToolRegistry.getToolById(id); |
| 72 | + expect(tool).toBeDefined(); |
| 73 | + if (tool) { |
| 74 | + expect(tool.status).toBe(BackgroundToolStatus.COMPLETED); |
| 75 | + expect(tool.endTime).toBeDefined(); |
| 76 | + if (tool.type === BackgroundToolType.SHELL) { |
| 77 | + expect(tool.metadata.exitCode).toBe(0); |
| 78 | + } |
| 79 | + } |
| 80 | + }); |
| 81 | + |
| 82 | + it('should return false when updating non-existent tool', () => { |
| 83 | + const updated = backgroundToolRegistry.updateToolStatus( |
| 84 | + 'non-existent-id', |
| 85 | + BackgroundToolStatus.COMPLETED, |
| 86 | + ); |
| 87 | + |
| 88 | + expect(updated).toBe(false); |
| 89 | + }); |
| 90 | + |
| 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 | + |
| 131 | + it('should clean up old completed tools', () => { |
| 132 | + // Create tools with specific dates |
| 133 | + const registry = backgroundToolRegistry as any; |
| 134 | + |
| 135 | + // Add a completed tool from 25 hours ago |
| 136 | + const oldTool = { |
| 137 | + id: 'old-tool', |
| 138 | + type: BackgroundToolType.SHELL, |
| 139 | + status: BackgroundToolStatus.COMPLETED, |
| 140 | + startTime: new Date(Date.now() - 25 * 60 * 60 * 1000), |
| 141 | + endTime: new Date(Date.now() - 25 * 60 * 60 * 1000), |
| 142 | + agentId: 'agent-1', |
| 143 | + metadata: { command: 'echo old' }, |
| 144 | + }; |
| 145 | + |
| 146 | + // Add a completed tool from 10 hours ago |
| 147 | + const recentTool = { |
| 148 | + id: 'recent-tool', |
| 149 | + type: BackgroundToolType.SHELL, |
| 150 | + status: BackgroundToolStatus.COMPLETED, |
| 151 | + startTime: new Date(Date.now() - 10 * 60 * 60 * 1000), |
| 152 | + endTime: new Date(Date.now() - 10 * 60 * 60 * 1000), |
| 153 | + agentId: 'agent-1', |
| 154 | + metadata: { command: 'echo recent' }, |
| 155 | + }; |
| 156 | + |
| 157 | + // Add a running tool from 25 hours ago |
| 158 | + const oldRunningTool = { |
| 159 | + id: 'old-running-tool', |
| 160 | + type: BackgroundToolType.SHELL, |
| 161 | + status: BackgroundToolStatus.RUNNING, |
| 162 | + startTime: new Date(Date.now() - 25 * 60 * 60 * 1000), |
| 163 | + agentId: 'agent-1', |
| 164 | + metadata: { command: 'sleep 100' }, |
| 165 | + }; |
| 166 | + |
| 167 | + registry.tools.set('old-tool', oldTool); |
| 168 | + registry.tools.set('recent-tool', recentTool); |
| 169 | + 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(); |
| 184 | + }); |
| 185 | +}); |
0 commit comments