|
| 1 | +import { describe, it, expect, beforeEach, afterEach } from 'vitest'; |
| 2 | +import * as fs from 'fs'; |
| 3 | +import * as path from 'path'; |
| 4 | +import * as os from 'os'; |
| 5 | + |
| 6 | +describe('Agent Orchestrator — Behavioral', () => { |
| 7 | + describe('getOrchestrator', () => { |
| 8 | + it('should return a singleton instance', async () => { |
| 9 | + const { getOrchestrator } = await import('../src/agents/orchestrator.js'); |
| 10 | + const o1 = getOrchestrator(); |
| 11 | + const o2 = getOrchestrator(); |
| 12 | + expect(o1).toBe(o2); |
| 13 | + }); |
| 14 | + }); |
| 15 | + |
| 16 | + describe('getAvailableAgents', () => { |
| 17 | + it('should return array of agents', async () => { |
| 18 | + const { getOrchestrator } = await import('../src/agents/orchestrator.js'); |
| 19 | + const orchestrator = getOrchestrator(); |
| 20 | + const agents = orchestrator.getAvailableAgents(); |
| 21 | + expect(Array.isArray(agents)).toBe(true); |
| 22 | + }); |
| 23 | + }); |
| 24 | + |
| 25 | + describe('getCategories', () => { |
| 26 | + it('should return array of categories', async () => { |
| 27 | + const { getOrchestrator } = await import('../src/agents/orchestrator.js'); |
| 28 | + const orchestrator = getOrchestrator(); |
| 29 | + const categories = orchestrator.getCategories(); |
| 30 | + expect(Array.isArray(categories)).toBe(true); |
| 31 | + }); |
| 32 | + }); |
| 33 | + |
| 34 | + describe('searchAgents', () => { |
| 35 | + it('should return empty array for non-matching query', async () => { |
| 36 | + const { getOrchestrator } = await import('../src/agents/orchestrator.js'); |
| 37 | + const orchestrator = getOrchestrator(); |
| 38 | + const results = orchestrator.searchAgents('xyznonexistent123'); |
| 39 | + expect(Array.isArray(results)).toBe(true); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should return agents matching query', async () => { |
| 43 | + const { getOrchestrator } = await import('../src/agents/orchestrator.js'); |
| 44 | + const orchestrator = getOrchestrator(); |
| 45 | + const results = orchestrator.searchAgents('engineer'); |
| 46 | + expect(Array.isArray(results)).toBe(true); |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + describe('getStatus', () => { |
| 51 | + it('should return null when no orchestration running', async () => { |
| 52 | + const { getOrchestrator } = await import('../src/agents/orchestrator.js'); |
| 53 | + const orchestrator = getOrchestrator(); |
| 54 | + const status = orchestrator.getStatus(); |
| 55 | + expect(status).toBeNull(); |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + describe('runOrchestration', () => { |
| 60 | + it('should handle non-existent agent gracefully', async () => { |
| 61 | + const { getOrchestrator } = await import('../src/agents/orchestrator.js'); |
| 62 | + const orchestrator = getOrchestrator(); |
| 63 | + const state = await orchestrator.runOrchestration({ |
| 64 | + mode: 'micro', |
| 65 | + primaryAgent: 'nonexistent-agent', |
| 66 | + supportingAgents: [], |
| 67 | + maxRetries: 1, |
| 68 | + qualityGates: false, |
| 69 | + }, 'test task', 'ollama', {}, 'qwen2.5', os.tmpdir(), '', 5); |
| 70 | + expect(state).toBeDefined(); |
| 71 | + expect(state.phase).toBe('complete'); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should set correct initial phase', async () => { |
| 75 | + const { getOrchestrator } = await import('../src/agents/orchestrator.js'); |
| 76 | + const orchestrator = getOrchestrator(); |
| 77 | + const promise = orchestrator.runOrchestration({ |
| 78 | + mode: 'micro', |
| 79 | + primaryAgent: 'test-agent', |
| 80 | + supportingAgents: [], |
| 81 | + maxRetries: 1, |
| 82 | + qualityGates: false, |
| 83 | + }, 'test', 'ollama', {}, 'qwen2.5', os.tmpdir(), '', 1); |
| 84 | + // During execution, phase should be set |
| 85 | + expect(orchestrator.getStatus()).not.toBeNull(); |
| 86 | + await promise.catch(() => {}); |
| 87 | + }); |
| 88 | + }); |
| 89 | + |
| 90 | + describe('progress callbacks', () => { |
| 91 | + it('should call progress callback during orchestration', async () => { |
| 92 | + const { getOrchestrator } = await import('../src/agents/orchestrator.js'); |
| 93 | + const orchestrator = getOrchestrator(); |
| 94 | + const progress: any[] = []; |
| 95 | + orchestrator.setProgressCallback((state) => { progress.push(state); }); |
| 96 | + await orchestrator.runOrchestration({ |
| 97 | + mode: 'micro', |
| 98 | + primaryAgent: 'test', |
| 99 | + supportingAgents: [], |
| 100 | + maxRetries: 1, |
| 101 | + qualityGates: false, |
| 102 | + }, 'test', 'ollama', {}, 'qwen2.5', os.tmpdir(), '', 1).catch(() => {}); |
| 103 | + expect(progress.length).toBeGreaterThan(0); |
| 104 | + }); |
| 105 | + }); |
| 106 | +}); |
| 107 | + |
| 108 | +describe('Agent Orchestrator — Technical', () => { |
| 109 | + describe('orchestration state machine', () => { |
| 110 | + it('should transition through phases correctly', async () => { |
| 111 | + const { getOrchestrator } = await import('../src/agents/orchestrator.js'); |
| 112 | + const orchestrator = getOrchestrator(); |
| 113 | + await orchestrator.runOrchestration({ |
| 114 | + mode: 'micro', |
| 115 | + primaryAgent: 'test', |
| 116 | + supportingAgents: [], |
| 117 | + maxRetries: 1, |
| 118 | + qualityGates: false, |
| 119 | + }, 'test', 'ollama', {}, 'qwen2.5', os.tmpdir(), '', 1).catch(() => {}); |
| 120 | + const status = orchestrator.getStatus(); |
| 121 | + expect(status).not.toBeNull(); |
| 122 | + expect(status!.phase).toBe('complete'); |
| 123 | + expect(status!.qualityGatePassed).toBeDefined(); |
| 124 | + }); |
| 125 | + |
| 126 | + it('should track start time', async () => { |
| 127 | + const { getOrchestrator } = await import('../src/agents/orchestrator.js'); |
| 128 | + const orchestrator = getOrchestrator(); |
| 129 | + await orchestrator.runOrchestration({ |
| 130 | + mode: 'micro', |
| 131 | + primaryAgent: 'test', |
| 132 | + supportingAgents: [], |
| 133 | + maxRetries: 1, |
| 134 | + qualityGates: false, |
| 135 | + }, 'test', 'ollama', {}, 'qwen2.5', os.tmpdir(), '', 1).catch(() => {}); |
| 136 | + const status = orchestrator.getStatus(); |
| 137 | + expect(status!.startTime).toBeLessThanOrEqual(Date.now()); |
| 138 | + }); |
| 139 | + |
| 140 | + it('should track completed and failed tasks', async () => { |
| 141 | + const { getOrchestrator } = await import('../src/agents/orchestrator.js'); |
| 142 | + const orchestrator = getOrchestrator(); |
| 143 | + await orchestrator.runOrchestration({ |
| 144 | + mode: 'micro', |
| 145 | + primaryAgent: 'nonexistent', |
| 146 | + supportingAgents: [], |
| 147 | + maxRetries: 1, |
| 148 | + qualityGates: false, |
| 149 | + }, 'test', 'ollama', {}, 'qwen2.5', os.tmpdir(), '', 1).catch(() => {}); |
| 150 | + const status = orchestrator.getStatus(); |
| 151 | + expect(Array.isArray(status!.completedTasks)).toBe(true); |
| 152 | + expect(Array.isArray(status!.failedTasks)).toBe(true); |
| 153 | + }); |
| 154 | + }); |
| 155 | + |
| 156 | + describe('quality gates', () => { |
| 157 | + it('should retry failed tasks when qualityGate is true', async () => { |
| 158 | + const { getOrchestrator } = await import('../src/agents/orchestrator.js'); |
| 159 | + const orchestrator = getOrchestrator(); |
| 160 | + await orchestrator.runOrchestration({ |
| 161 | + mode: 'micro', |
| 162 | + primaryAgent: 'nonexistent', |
| 163 | + supportingAgents: [], |
| 164 | + maxRetries: 2, |
| 165 | + qualityGates: true, |
| 166 | + }, 'test', 'ollama', {}, 'qwen2.5', os.tmpdir(), '', 1).catch(() => {}); |
| 167 | + const status = orchestrator.getStatus(); |
| 168 | + expect(status).not.toBeNull(); |
| 169 | + }); |
| 170 | + |
| 171 | + it('should skip retries when qualityGate is false', async () => { |
| 172 | + const { getOrchestrator } = await import('../src/agents/orchestrator.js'); |
| 173 | + const orchestrator = getOrchestrator(); |
| 174 | + await orchestrator.runOrchestration({ |
| 175 | + mode: 'micro', |
| 176 | + primaryAgent: 'nonexistent', |
| 177 | + supportingAgents: [], |
| 178 | + maxRetries: 2, |
| 179 | + qualityGates: false, |
| 180 | + }, 'test', 'ollama', {}, 'qwen2.5', os.tmpdir(), '', 1).catch(() => {}); |
| 181 | + const status = orchestrator.getStatus(); |
| 182 | + expect(status).not.toBeNull(); |
| 183 | + }); |
| 184 | + }); |
| 185 | + |
| 186 | + describe('mode handling', () => { |
| 187 | + it('should handle micro mode (single phase)', async () => { |
| 188 | + const { getOrchestrator } = await import('../src/agents/orchestrator.js'); |
| 189 | + const orchestrator = getOrchestrator(); |
| 190 | + await orchestrator.runOrchestration({ |
| 191 | + mode: 'micro', |
| 192 | + primaryAgent: 'test', |
| 193 | + supportingAgents: [], |
| 194 | + maxRetries: 1, |
| 195 | + qualityGates: false, |
| 196 | + }, 'test', 'ollama', {}, 'qwen2.5', os.tmpdir(), '', 1).catch(() => {}); |
| 197 | + const status = orchestrator.getStatus(); |
| 198 | + expect(status!.phase).toBe('complete'); |
| 199 | + }); |
| 200 | + |
| 201 | + it('should handle sprint mode', async () => { |
| 202 | + const { getOrchestrator } = await import('../src/agents/orchestrator.js'); |
| 203 | + const orchestrator = getOrchestrator(); |
| 204 | + await orchestrator.runOrchestration({ |
| 205 | + mode: 'sprint', |
| 206 | + primaryAgent: 'test', |
| 207 | + supportingAgents: ['test2'], |
| 208 | + maxRetries: 1, |
| 209 | + qualityGates: false, |
| 210 | + }, 'test', 'ollama', {}, 'qwen2.5', os.tmpdir(), '', 1).catch(() => {}); |
| 211 | + const status = orchestrator.getStatus(); |
| 212 | + expect(status).not.toBeNull(); |
| 213 | + }); |
| 214 | + |
| 215 | + it('should handle full mode', async () => { |
| 216 | + const { getOrchestrator } = await import('../src/agents/orchestrator.js'); |
| 217 | + const orchestrator = getOrchestrator(); |
| 218 | + await orchestrator.runOrchestration({ |
| 219 | + mode: 'full', |
| 220 | + primaryAgent: 'test', |
| 221 | + supportingAgents: ['test2', 'test3'], |
| 222 | + maxRetries: 1, |
| 223 | + qualityGates: false, |
| 224 | + }, 'test', 'ollama', {}, 'qwen2.5', os.tmpdir(), '', 1).catch(() => {}); |
| 225 | + const status = orchestrator.getStatus(); |
| 226 | + expect(status).not.toBeNull(); |
| 227 | + }); |
| 228 | + }); |
| 229 | + |
| 230 | + describe('error handling', () => { |
| 231 | + it('should handle provider connection errors', async () => { |
| 232 | + const { getOrchestrator } = await import('../src/agents/orchestrator.js'); |
| 233 | + const orchestrator = getOrchestrator(); |
| 234 | + await orchestrator.runOrchestration({ |
| 235 | + mode: 'micro', |
| 236 | + primaryAgent: 'test', |
| 237 | + supportingAgents: [], |
| 238 | + maxRetries: 1, |
| 239 | + qualityGates: false, |
| 240 | + }, 'test', 'ollama', {}, 'nonexistent-model', os.tmpdir(), '', 1).catch(() => {}); |
| 241 | + // Should not throw |
| 242 | + }); |
| 243 | + |
| 244 | + it('should handle empty task string', async () => { |
| 245 | + const { getOrchestrator } = await import('../src/agents/orchestrator.js'); |
| 246 | + const orchestrator = getOrchestrator(); |
| 247 | + await orchestrator.runOrchestration({ |
| 248 | + mode: 'micro', |
| 249 | + primaryAgent: 'test', |
| 250 | + supportingAgents: [], |
| 251 | + maxRetries: 1, |
| 252 | + qualityGates: false, |
| 253 | + }, '', 'ollama', {}, 'qwen2.5', os.tmpdir(), '', 1).catch(() => {}); |
| 254 | + // Should not throw |
| 255 | + }); |
| 256 | + }); |
| 257 | + |
| 258 | + describe('synthesis', () => { |
| 259 | + it('should synthesize multiple task outputs', async () => { |
| 260 | + const { getOrchestrator } = await import('../src/agents/orchestrator.js'); |
| 261 | + const orchestrator = getOrchestrator(); |
| 262 | + await orchestrator.runOrchestration({ |
| 263 | + mode: 'micro', |
| 264 | + primaryAgent: 'test', |
| 265 | + supportingAgents: [], |
| 266 | + maxRetries: 1, |
| 267 | + qualityGates: false, |
| 268 | + }, 'test task', 'ollama', {}, 'qwen2.5', os.tmpdir(), '', 1).catch(() => {}); |
| 269 | + const status = orchestrator.getStatus(); |
| 270 | + expect(status).not.toBeNull(); |
| 271 | + }); |
| 272 | + }); |
| 273 | +}); |
0 commit comments