Elffuss BraidHub

kikocis/continue public

406467d04db9 · 5.7 KiBRawPermalink
1import { describe, expect, test, beforeEach, vi } from "vitest";
2
3import { ServiceContainer } from "./ServiceContainer.js";
4
5describe("ServiceContainer", () => {
6 let container: ServiceContainer;
7
8 beforeEach(() => {
9 container = new ServiceContainer();
10 vi.clearAllMocks();
11 });
12
13 describe("Dependency Cascade Reloading", () => {
14 test("should automatically reload all transitive dependents", async () => {
15 // Setup service dependency chain: auth -> apiClient -> config -> model
16 const authValue = { auth: "auth-value" };
17 const apiClientValue = { apiClient: "api-client-value" };
18 const configValue = { config: "config-value" };
19 const modelValue = { model: "model-value" };
20
21 let authReloadCount = 0;
22 let apiClientReloadCount = 0;
23 let configReloadCount = 0;
24 let modelReloadCount = 0;
25
26 // Register services with mock factories that track reload count
27 container.register(
28 "auth",
29 async () => {
30 authReloadCount++;
31 return authValue;
32 },
33 [],
34 );
35
36 container.register(
37 "apiClient",
38 async () => {
39 apiClientReloadCount++;
40 return apiClientValue;
41 },
42 ["auth"],
43 );
44
45 container.register(
46 "config",
47 async () => {
48 configReloadCount++;
49 return configValue;
50 },
51 ["auth", "apiClient"],
52 );
53
54 container.register(
55 "model",
56 async () => {
57 modelReloadCount++;
58 return modelValue;
59 },
60 ["config"],
61 );
62
63 // Initial load of all services
64 await container.get("model");
65
66 // Verify initial load counts
67 expect(authReloadCount).toBe(1);
68 expect(apiClientReloadCount).toBe(1);
69 expect(configReloadCount).toBe(1);
70 expect(modelReloadCount).toBe(1);
71
72 // Reload auth service - should cascade to all dependents
73 await container.reload("auth");
74
75 // Verify cascade reload occurred - all dependent services should reload
76 expect(authReloadCount).toBe(2); // auth reloaded
77 expect(apiClientReloadCount).toBe(2); // apiClient depends on auth
78 expect(configReloadCount).toBe(2); // config depends on auth and apiClient
79 expect(modelReloadCount).toBe(2); // model depends on config
80 });
81
82 test("should find all transitive dependents correctly", async () => {
83 // Setup complex dependency graph
84 container.register("serviceA", async () => ({}), []);
85 container.register("serviceB", async () => ({}), ["serviceA"]);
86 container.register("serviceC", async () => ({}), ["serviceA"]);
87 container.register("serviceD", async () => ({}), [
88 "serviceB",
89 "serviceC",
90 ]);
91 container.register("serviceE", async () => ({}), ["serviceD"]);
92
93 // Use reflection to access private method for testing
94 const findAllDependents = (container as any).findAllDependents.bind(
95 container,
96 );
97
98 const dependentsOfA = findAllDependents("serviceA");
99
100 // serviceA should have all others as transitive dependents
101 expect(dependentsOfA.sort()).toEqual([
102 "serviceB",
103 "serviceC",
104 "serviceD",
105 "serviceE",
106 ]);
107 });
108
109 test("should handle circular dependencies gracefully", async () => {
110 // This test ensures the findAllDependents doesn't get stuck in infinite loops
111
112 // Setup a circular dependency (which shouldn't happen in practice but we handle it)
113 container.register("serviceX", async () => ({}), ["serviceY"]);
114 container.register("serviceY", async () => ({}), ["serviceX"]);
115
116 // Use reflection to access private method for testing
117 const findAllDependents = (container as any).findAllDependents.bind(
118 container,
119 );
120
121 // Should not hang and should return both services as circular dependents
122 const dependentsOfX = findAllDependents("serviceX");
123 const dependentsOfY = findAllDependents("serviceY");
124
125 // Both should contain each other due to circular dependency
126 expect(dependentsOfX).toContain("serviceY");
127 expect(dependentsOfY).toContain("serviceX");
128 });
129
130 test("should emit events during cascade reload", async () => {
131 const events: string[] = [];
132
133 container.register("parent", async () => ({ parent: true }), []);
134 container.register("child", async () => ({ child: true }), ["parent"]);
135
136 // Listen for events
137 container.on("parent:loading", () => events.push("parent:loading"));
138 container.on("parent:ready", () => events.push("parent:ready"));
139 container.on("child:loading", () => events.push("child:loading"));
140 container.on("child:ready", () => events.push("child:ready"));
141
142 // Initial load
143 await container.get("child");
144 events.length = 0; // Clear events
145
146 // Reload parent - should cascade to child
147 await container.reload("parent");
148
149 // Verify both services went through loading cycle
150 expect(events).toContain("parent:loading");
151 expect(events).toContain("parent:ready");
152 expect(events).toContain("child:loading");
153 expect(events).toContain("child:ready");
154 });
155
156 test("should preserve service state when no dependents exist", async () => {
157 let reloadCount = 0;
158
159 container.register(
160 "standalone",
161 async () => {
162 reloadCount++;
163 return { standalone: true };
164 },
165 [],
166 );
167
168 // Load initially
169 await container.get("standalone");
170 expect(reloadCount).toBe(1);
171
172 // Reload - only this service should reload
173 await container.reload("standalone");
174 expect(reloadCount).toBe(2);
175
176 // Should still be ready
177 expect(container.isReady("standalone")).toBe(true);
178 });
179 });
180});