| 1 | import { describe, expect, test, beforeEach, vi } from "vitest"; |
| 2 | |
| 3 | import { |
| 4 | BaseService, |
| 5 | hasDependencies, |
| 6 | ServiceWithDependencies, |
| 7 | } from "./BaseService.js"; |
| 8 | |
| 9 | // Test implementation of BaseService |
| 10 | interface TestState { |
| 11 | value: number; |
| 12 | message: string; |
| 13 | data?: any[]; |
| 14 | } |
| 15 | |
| 16 | class TestService extends BaseService<TestState> { |
| 17 | constructor() { |
| 18 | super("TestService", { |
| 19 | value: 0, |
| 20 | message: "initial", |
| 21 | }); |
| 22 | } |
| 23 | |
| 24 | async doInitialize(value: number, message: string): Promise<TestState> { |
| 25 | return { |
| 26 | value, |
| 27 | message, |
| 28 | }; |
| 29 | } |
| 30 | |
| 31 | // Expose setState for testing |
| 32 | updateState(newState: Partial<TestState>) { |
| 33 | this.setState(newState); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | // Test service with dependencies |
| 38 | class DependentTestService |
| 39 | extends TestService |
| 40 | implements ServiceWithDependencies |
| 41 | { |
| 42 | getDependencies(): string[] { |
| 43 | return ["auth", "config"]; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | // Test service that throws during initialization |
| 48 | class FailingTestService extends BaseService<TestState> { |
| 49 | constructor() { |
| 50 | super("FailingService", { |
| 51 | value: 0, |
| 52 | message: "initial", |
| 53 | }); |
| 54 | } |
| 55 | |
| 56 | async doInitialize(): Promise<TestState> { |
| 57 | throw new Error("Initialization failed"); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | describe("BaseService", () => { |
| 62 | describe("Basic Functionality", () => { |
| 63 | let service: TestService; |
| 64 | |
| 65 | beforeEach(() => { |
| 66 | service = new TestService(); |
| 67 | }); |
| 68 | |
| 69 | test("should have correct initial state", () => { |
| 70 | const state = service.getState(); |
| 71 | expect(state).toEqual({ |
| 72 | value: 0, |
| 73 | message: "initial", |
| 74 | }); |
| 75 | expect(service.isReady()).toBe(false); |
| 76 | }); |
| 77 | |
| 78 | test("should initialize successfully", async () => { |
| 79 | const state = await service.initialize(42, "initialized"); |
| 80 | |
| 81 | expect(state).toEqual({ |
| 82 | value: 42, |
| 83 | message: "initialized", |
| 84 | }); |
| 85 | expect(service.isReady()).toBe(true); |
| 86 | }); |
| 87 | |
| 88 | test("should emit initialization events", async () => { |
| 89 | const initializingListener = vi.fn(); |
| 90 | const initializedListener = vi.fn(); |
| 91 | |
| 92 | service.on("initializing", initializingListener); |
| 93 | service.on("initialized", initializedListener); |
| 94 | |
| 95 | await service.initialize(42, "initialized"); |
| 96 | |
| 97 | expect(initializingListener).toHaveBeenCalledTimes(1); |
| 98 | expect(initializedListener).toHaveBeenCalledTimes(1); |
| 99 | expect(initializedListener).toHaveBeenCalledWith({ |
| 100 | value: 42, |
| 101 | message: "initialized", |
| 102 | }); |
| 103 | }); |
| 104 | |
| 105 | test("should handle initialization failure", async () => { |
| 106 | const failingService = new FailingTestService(); |
| 107 | const errorListener = vi.fn(); |
| 108 | |
| 109 | failingService.on("error", errorListener); |
| 110 | |
| 111 | await expect(failingService.initialize()).rejects.toThrow( |
| 112 | "Initialization failed", |
| 113 | ); |
| 114 | expect(errorListener).toHaveBeenCalledTimes(1); |
| 115 | expect(failingService.isReady()).toBe(false); |
| 116 | }); |
| 117 | }); |
| 118 | |
| 119 | describe("State Management", () => { |
| 120 | let service: TestService; |
| 121 | |
| 122 | beforeEach(() => { |
| 123 | service = new TestService(); |
| 124 | }); |
| 125 | |
| 126 | test("should return immutable state copy", async () => { |
| 127 | await service.initialize(42, "test"); |
| 128 | |
| 129 | const state1 = service.getState(); |
| 130 | const state2 = service.getState(); |
| 131 | |
| 132 | expect(state1).toEqual(state2); |
| 133 | expect(state1).not.toBe(state2); // Different object references |
| 134 | }); |
| 135 | |
| 136 | test("should return shallow copy of state", async () => { |
| 137 | await service.initialize(42, "test"); |
| 138 | service.updateState({ data: [{ id: 1 }, { id: 2 }] }); |
| 139 | |
| 140 | const state = service.getState(); |
| 141 | const originalData = state.data; |
| 142 | |
| 143 | // Modify the returned state's nested object |
| 144 | if (state.data) { |
| 145 | state.data[0].id = 999; |
| 146 | } |
| 147 | |
| 148 | // With shallow copy, nested objects are shared |
| 149 | const newState = service.getState(); |
| 150 | expect(newState.data).toEqual([{ id: 999 }, { id: 2 }]); |
| 151 | expect(newState.data).toBe(originalData); // Same reference for nested objects |
| 152 | }); |
| 153 | |
| 154 | test("should emit state change events", async () => { |
| 155 | await service.initialize(42, "test"); |
| 156 | |
| 157 | const stateChangeListener = vi.fn(); |
| 158 | service.on("stateChanged", stateChangeListener); |
| 159 | |
| 160 | service.updateState({ value: 100 }); |
| 161 | |
| 162 | expect(stateChangeListener).toHaveBeenCalledTimes(1); |
| 163 | expect(stateChangeListener).toHaveBeenCalledWith( |
| 164 | { value: 100, message: "test" }, // new state |
| 165 | { value: 42, message: "test" }, // previous state |
| 166 | ); |
| 167 | }); |
| 168 | |
| 169 | test("should merge partial state updates", async () => { |
| 170 | await service.initialize(42, "test"); |
| 171 | |
| 172 | service.updateState({ value: 100 }); |
| 173 | expect(service.getState()).toEqual({ |
| 174 | value: 100, |
| 175 | message: "test", |
| 176 | }); |
| 177 | |
| 178 | service.updateState({ message: "updated" }); |
| 179 | expect(service.getState()).toEqual({ |
| 180 | value: 100, |
| 181 | message: "updated", |
| 182 | }); |
| 183 | }); |
| 184 | }); |
| 185 | |
| 186 | describe("Reload Functionality", () => { |
| 187 | let service: TestService; |
| 188 | |
| 189 | beforeEach(() => { |
| 190 | service = new TestService(); |
| 191 | }); |
| 192 | |
| 193 | test("should reload service with new parameters", async () => { |
| 194 | await service.initialize(42, "first"); |
| 195 | expect(service.getState()).toEqual({ |
| 196 | value: 42, |
| 197 | message: "first", |
| 198 | }); |
| 199 | |
| 200 | await service.reload(100, "second"); |
| 201 | expect(service.getState()).toEqual({ |
| 202 | value: 100, |
| 203 | message: "second", |
| 204 | }); |
| 205 | expect(service.isReady()).toBe(true); |
| 206 | }); |
| 207 | |
| 208 | test("should reset isInitialized during reload", async () => { |
| 209 | await service.initialize(42, "test"); |
| 210 | expect(service.isReady()).toBe(true); |
| 211 | |
| 212 | // Create a promise to check intermediate state |
| 213 | let wasNotReady = false; |
| 214 | const originalInit = service.initialize.bind(service); |
| 215 | service.initialize = vi.fn(async (value: number, message: string) => { |
| 216 | wasNotReady = !service.isReady(); |
| 217 | return originalInit(value, message); |
| 218 | }) as any; |
| 219 | |
| 220 | await service.reload(100, "reloaded"); |
| 221 | |
| 222 | expect(wasNotReady).toBe(true); |
| 223 | expect(service.isReady()).toBe(true); |
| 224 | }); |
| 225 | }); |
| 226 | |
| 227 | describe("Cleanup", () => { |
| 228 | let service: TestService; |
| 229 | |
| 230 | beforeEach(() => { |
| 231 | service = new TestService(); |
| 232 | }); |
| 233 | |
| 234 | test("should cleanup resources", async () => { |
| 235 | await service.initialize(42, "test"); |
| 236 | |
| 237 | const listener = vi.fn(); |
| 238 | service.on("test-event", listener); |
| 239 | |
| 240 | await service.cleanup(); |
| 241 | |
| 242 | expect(service.isReady()).toBe(false); |
| 243 | |
| 244 | // Verify listeners are removed |
| 245 | service.emit("test-event"); |
| 246 | expect(listener).not.toHaveBeenCalled(); |
| 247 | }); |
| 248 | }); |
| 249 | |
| 250 | describe("ServiceWithDependencies", () => { |
| 251 | test("should correctly identify services with dependencies", () => { |
| 252 | const regularService = new TestService(); |
| 253 | const dependentService = new DependentTestService(); |
| 254 | |
| 255 | expect(hasDependencies(regularService)).toBe(false); |
| 256 | expect(hasDependencies(dependentService)).toBe(true); |
| 257 | |
| 258 | if (hasDependencies(dependentService)) { |
| 259 | expect(dependentService.getDependencies()).toEqual(["auth", "config"]); |
| 260 | } |
| 261 | }); |
| 262 | }); |
| 263 | |
| 264 | describe("Shallow Copy Functionality", () => { |
| 265 | let service: TestService; |
| 266 | |
| 267 | beforeEach(() => { |
| 268 | service = new TestService(); |
| 269 | }); |
| 270 | |
| 271 | test("should handle null and undefined values", async () => { |
| 272 | await service.initialize(42, "test"); |
| 273 | service.updateState({ |
| 274 | data: [null, undefined, "string", 123] as any, |
| 275 | }); |
| 276 | |
| 277 | const state = service.getState(); |
| 278 | expect(state.data).toEqual([null, undefined, "string", 123]); |
| 279 | }); |
| 280 | |
| 281 | test("should handle Date objects", async () => { |
| 282 | const date = new Date("2024-01-01"); |
| 283 | await service.initialize(42, "test"); |
| 284 | service.updateState({ |
| 285 | data: [date] as any, |
| 286 | }); |
| 287 | |
| 288 | const state = service.getState(); |
| 289 | expect(state.data?.[0]).toEqual(date); |
| 290 | expect(state.data?.[0]).toBe(date); // Same instance with shallow copy |
| 291 | expect(state.data?.[0]).toBeInstanceOf(Date); |
| 292 | }); |
| 293 | |
| 294 | test("should handle complex nested structures", async () => { |
| 295 | const complexData = { |
| 296 | nested: { |
| 297 | array: [1, 2, { deep: "value" }], |
| 298 | date: new Date(), |
| 299 | bool: true, |
| 300 | null: null, |
| 301 | }, |
| 302 | }; |
| 303 | |
| 304 | await service.initialize(42, "test"); |
| 305 | service.updateState({ data: complexData as any }); |
| 306 | |
| 307 | const state = service.getState(); |
| 308 | expect(state.data).toEqual(complexData); |
| 309 | expect(state.data).toBe(complexData); // Same reference with shallow copy |
| 310 | |
| 311 | // Verify nested objects are shared (not deep copied) |
| 312 | if ( |
| 313 | state.data && |
| 314 | typeof state.data === "object" && |
| 315 | "nested" in state.data |
| 316 | ) { |
| 317 | const data = state.data as any; |
| 318 | expect(data.nested.array).toBe(complexData.nested.array); |
| 319 | expect(data.nested.array[2]).toBe(complexData.nested.array[2]); |
| 320 | } |
| 321 | }); |
| 322 | }); |
| 323 | |
| 324 | describe("Error Handling", () => { |
| 325 | test("should emit error events on setState failures", () => { |
| 326 | const service = new TestService(); |
| 327 | |
| 328 | // Override setState to throw |
| 329 | const originalSetState = Object.getOwnPropertyDescriptor( |
| 330 | Object.getPrototypeOf(service), |
| 331 | "setState", |
| 332 | ); |
| 333 | |
| 334 | Object.defineProperty(service, "setState", { |
| 335 | value: function () { |
| 336 | throw new Error("setState failed"); |
| 337 | }, |
| 338 | }); |
| 339 | |
| 340 | expect(() => service.updateState({ value: 100 })).toThrow( |
| 341 | "setState failed", |
| 342 | ); |
| 343 | |
| 344 | // Restore original |
| 345 | if (originalSetState) { |
| 346 | Object.defineProperty( |
| 347 | Object.getPrototypeOf(service), |
| 348 | "setState", |
| 349 | originalSetState, |
| 350 | ); |
| 351 | } |
| 352 | }); |
| 353 | }); |
| 354 | |
| 355 | describe("Event Emitter Integration", () => { |
| 356 | let service: TestService; |
| 357 | |
| 358 | beforeEach(() => { |
| 359 | service = new TestService(); |
| 360 | }); |
| 361 | |
| 362 | test("should support multiple listeners", async () => { |
| 363 | const listener1 = vi.fn(); |
| 364 | const listener2 = vi.fn(); |
| 365 | |
| 366 | service.on("initialized", listener1); |
| 367 | service.on("initialized", listener2); |
| 368 | |
| 369 | await service.initialize(42, "test"); |
| 370 | |
| 371 | expect(listener1).toHaveBeenCalledTimes(1); |
| 372 | expect(listener2).toHaveBeenCalledTimes(1); |
| 373 | }); |
| 374 | |
| 375 | test("should support once listeners", async () => { |
| 376 | const listener = vi.fn(); |
| 377 | |
| 378 | service.once("stateChanged", listener); |
| 379 | |
| 380 | await service.initialize(42, "test"); |
| 381 | service.updateState({ value: 100 }); |
| 382 | service.updateState({ value: 200 }); |
| 383 | |
| 384 | expect(listener).toHaveBeenCalledTimes(1); |
| 385 | }); |
| 386 | |
| 387 | test("should allow removing specific listeners", async () => { |
| 388 | const listener = vi.fn(); |
| 389 | |
| 390 | service.on("stateChanged", listener); |
| 391 | await service.initialize(42, "test"); |
| 392 | |
| 393 | service.updateState({ value: 100 }); |
| 394 | expect(listener).toHaveBeenCalledTimes(1); |
| 395 | |
| 396 | service.off("stateChanged", listener); |
| 397 | service.updateState({ value: 200 }); |
| 398 | expect(listener).toHaveBeenCalledTimes(1); // Not called again |
| 399 | }); |
| 400 | }); |
| 401 | }); |