Elffuss BraidHub

kikocis/continue public

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