| 1 | import { ServiceContainer } from "./ServiceContainer.js"; |
| 2 | |
| 3 | import { initializeServices } from "./index.js"; |
| 4 | |
| 5 | describe("Service Circular Dependencies", () => { |
| 6 | it("should not have circular dependencies in service setup", async () => { |
| 7 | // Create a fresh container and initialize services to get real dependencies |
| 8 | const testContainer = new ServiceContainer(); |
| 9 | |
| 10 | // Monkey patch the global serviceContainer to capture registrations |
| 11 | const originalRegister = testContainer.register.bind(testContainer); |
| 12 | const serviceDependencies: Record<string, string[]> = {}; |
| 13 | |
| 14 | testContainer.register = function <T>( |
| 15 | serviceName: string, |
| 16 | factory: () => Promise<T>, |
| 17 | deps: string[] = [], |
| 18 | ) { |
| 19 | serviceDependencies[serviceName] = [...deps]; // Capture actual dependencies |
| 20 | return originalRegister(serviceName, factory, deps); |
| 21 | }; |
| 22 | |
| 23 | // Import and run the real service initialization to capture dependencies |
| 24 | const { serviceContainer } = await import("./index.js"); |
| 25 | const originalContainer = serviceContainer; |
| 26 | |
| 27 | // Temporarily replace the global container |
| 28 | (global as any).serviceContainer = testContainer; |
| 29 | |
| 30 | try { |
| 31 | // This will register services with their real dependencies |
| 32 | await initializeServices({ headless: true }); |
| 33 | } catch (error) { |
| 34 | // Ignore initialization errors - we just want the registration data |
| 35 | } finally { |
| 36 | // Restore original container |
| 37 | (global as any).serviceContainer = originalContainer; |
| 38 | } |
| 39 | |
| 40 | // Detect circular dependencies using topological sort |
| 41 | const detectCircularDependencies = ( |
| 42 | dependencies: Record<string, string[]>, |
| 43 | ): string[] => { |
| 44 | const visited = new Set<string>(); |
| 45 | const recStack = new Set<string>(); |
| 46 | const cycle: string[] = []; |
| 47 | |
| 48 | const hasCycle = (node: string, path: string[]): boolean => { |
| 49 | if (recStack.has(node)) { |
| 50 | // Found a cycle - capture the cycle path |
| 51 | const cycleStart = path.indexOf(node); |
| 52 | cycle.push(...path.slice(cycleStart), node); |
| 53 | return true; |
| 54 | } |
| 55 | |
| 56 | if (visited.has(node)) { |
| 57 | return false; |
| 58 | } |
| 59 | |
| 60 | visited.add(node); |
| 61 | recStack.add(node); |
| 62 | |
| 63 | const deps = dependencies[node] || []; |
| 64 | for (const dep of deps) { |
| 65 | if (hasCycle(dep, [...path, node])) { |
| 66 | return true; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | recStack.delete(node); |
| 71 | return false; |
| 72 | }; |
| 73 | |
| 74 | // Check each service for cycles |
| 75 | for (const service of Object.keys(dependencies)) { |
| 76 | if (!visited.has(service)) { |
| 77 | if (hasCycle(service, [])) { |
| 78 | return cycle; |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | return []; |
| 84 | }; |
| 85 | |
| 86 | const circularDeps = detectCircularDependencies(serviceDependencies); |
| 87 | |
| 88 | if (circularDeps.length > 0) { |
| 89 | throw new Error( |
| 90 | `Circular dependency detected: ${circularDeps.join(" -> ")}`, |
| 91 | ); |
| 92 | } |
| 93 | |
| 94 | // Also verify that all services can be theoretically loaded without circular issues |
| 95 | const canResolveAll = () => { |
| 96 | const resolved = new Set<string>(); |
| 97 | const maxIterations = Object.keys(serviceDependencies).length * 2; |
| 98 | let iterations = 0; |
| 99 | |
| 100 | while ( |
| 101 | resolved.size < Object.keys(serviceDependencies).length && |
| 102 | iterations < maxIterations |
| 103 | ) { |
| 104 | for (const [service, deps] of Object.entries(serviceDependencies)) { |
| 105 | if ( |
| 106 | !resolved.has(service) && |
| 107 | deps.every((dep) => resolved.has(dep)) |
| 108 | ) { |
| 109 | resolved.add(service); |
| 110 | } |
| 111 | } |
| 112 | iterations++; |
| 113 | } |
| 114 | |
| 115 | return resolved.size === Object.keys(serviceDependencies).length; |
| 116 | }; |
| 117 | |
| 118 | expect(canResolveAll()).toBe(true); |
| 119 | }); |
| 120 | }); |