| 1 | import { beforeEach, describe, expect, test, vi } from "vitest"; |
| 2 | |
| 3 | // Mock modules |
| 4 | vi.mock("../auth/workos.js"); |
| 5 | vi.mock("../configLoader.js"); |
| 6 | vi.mock("../configEnhancer.js"); |
| 7 | vi.mock("./ServiceContainer.js"); |
| 8 | |
| 9 | import * as workos from "../auth/workos.js"; |
| 10 | import { configEnhancer } from "../configEnhancer.js"; |
| 11 | import * as configLoader from "../configLoader.js"; |
| 12 | |
| 13 | import { ConfigService } from "./ConfigService.js"; |
| 14 | import { serviceContainer } from "./ServiceContainer.js"; |
| 15 | import { SERVICE_NAMES } from "./types.js"; |
| 16 | |
| 17 | describe("ConfigService", () => { |
| 18 | let service: ConfigService; |
| 19 | const mockConfig = { |
| 20 | name: "test-assistant", |
| 21 | version: "1.0.0", |
| 22 | models: [], |
| 23 | systemMessage: "Test system message", |
| 24 | } as any; |
| 25 | const mockApiClient = { get: vi.fn(), post: vi.fn() }; |
| 26 | |
| 27 | beforeEach(() => { |
| 28 | vi.clearAllMocks(); |
| 29 | service = new ConfigService(); |
| 30 | }); |
| 31 | |
| 32 | describe("State Management", () => { |
| 33 | test("should initialize with config from path", async () => { |
| 34 | vi.mocked(configLoader.loadConfiguration).mockResolvedValue({ |
| 35 | config: mockConfig as any, |
| 36 | source: { type: "cli-flag", path: "/path/to/config.yaml" } as any, |
| 37 | }); |
| 38 | |
| 39 | const state = await service.initialize( |
| 40 | { accessToken: "token" } as any, |
| 41 | "/path/to/config.yaml", |
| 42 | "org-123", |
| 43 | mockApiClient as any, |
| 44 | ); |
| 45 | |
| 46 | expect(state).toEqual({ |
| 47 | config: mockConfig as any, |
| 48 | configPath: "/path/to/config.yaml", |
| 49 | }); |
| 50 | }); |
| 51 | |
| 52 | test("should initialize with undefined config path", async () => { |
| 53 | vi.mocked(configLoader.loadConfiguration).mockResolvedValue({ |
| 54 | config: mockConfig as any, |
| 55 | source: { type: "default-agent" } as any, |
| 56 | }); |
| 57 | |
| 58 | const state = await service.initialize( |
| 59 | { accessToken: "token" } as any, |
| 60 | undefined, |
| 61 | "org-123", |
| 62 | mockApiClient as any, |
| 63 | ); |
| 64 | |
| 65 | expect(state).toEqual({ |
| 66 | config: mockConfig as any, |
| 67 | configPath: undefined, |
| 68 | }); |
| 69 | }); |
| 70 | |
| 71 | test("should inject rules into config", async () => { |
| 72 | vi.mocked(configLoader.loadConfiguration).mockResolvedValue({ |
| 73 | config: mockConfig as any, |
| 74 | source: { type: "cli-flag", path: "/config.yaml" } as any, |
| 75 | }); |
| 76 | |
| 77 | const expectedConfig = { |
| 78 | ...mockConfig, |
| 79 | systemMessage: |
| 80 | "Test system message\n\nProcessed: rule1\n\nProcessed: rule2", |
| 81 | }; |
| 82 | |
| 83 | vi.mocked(configEnhancer.enhanceConfig).mockResolvedValue(expectedConfig); |
| 84 | |
| 85 | const state = await service.initialize( |
| 86 | { accessToken: "token" } as any, |
| 87 | "/config.yaml", |
| 88 | "org-123", |
| 89 | mockApiClient as any, |
| 90 | { rule: ["rule1", "rule2"] }, |
| 91 | ); |
| 92 | |
| 93 | // Verify configEnhancer was called with the right parameters |
| 94 | expect(vi.mocked(configEnhancer.enhanceConfig)).toHaveBeenCalledWith( |
| 95 | mockConfig, |
| 96 | { rule: ["rule1", "rule2"] }, |
| 97 | ); |
| 98 | |
| 99 | expect(state.config).toEqual(expectedConfig); |
| 100 | }); |
| 101 | }); |
| 102 | |
| 103 | describe("switchConfig()", () => { |
| 104 | test("should switch to new configuration", async () => { |
| 105 | // Initialize first |
| 106 | vi.mocked(configLoader.loadConfiguration).mockResolvedValue({ |
| 107 | config: mockConfig as any, |
| 108 | source: { type: "cli-flag", path: "/old.yaml" } as any, |
| 109 | }); |
| 110 | await service.initialize( |
| 111 | { accessToken: "token" } as any, |
| 112 | "/old.yaml", |
| 113 | "org-123", |
| 114 | mockApiClient as any, |
| 115 | ); |
| 116 | |
| 117 | // Switch to new config |
| 118 | const newConfig = { ...mockConfig, name: "new-assistant" } as any; |
| 119 | vi.mocked(configLoader.loadConfiguration).mockResolvedValue({ |
| 120 | config: newConfig, |
| 121 | source: { type: "cli-flag", path: "/new.yaml" } as any, |
| 122 | }); |
| 123 | |
| 124 | const state = await service.switchConfig( |
| 125 | "/new.yaml", |
| 126 | { accessToken: "token" } as any, |
| 127 | "org-123", |
| 128 | mockApiClient as any, |
| 129 | ); |
| 130 | |
| 131 | expect(state).toEqual({ |
| 132 | config: newConfig, |
| 133 | configPath: "/new.yaml", |
| 134 | }); |
| 135 | expect(service.getState()).toEqual(state); |
| 136 | }); |
| 137 | |
| 138 | test("should handle switch config errors", async () => { |
| 139 | await service.initialize( |
| 140 | { accessToken: "token" } as any, |
| 141 | "/old.yaml", |
| 142 | "org-123", |
| 143 | mockApiClient as any, |
| 144 | ); |
| 145 | |
| 146 | vi.mocked(configLoader.loadConfiguration).mockRejectedValue( |
| 147 | new Error("Config not found"), |
| 148 | ); |
| 149 | |
| 150 | await expect( |
| 151 | service.switchConfig( |
| 152 | "/bad.yaml", |
| 153 | { accessToken: "token" } as any, |
| 154 | "org-123", |
| 155 | mockApiClient as any, |
| 156 | ), |
| 157 | ).rejects.toThrow("Config not found"); |
| 158 | }); |
| 159 | }); |
| 160 | |
| 161 | describe("reload()", () => { |
| 162 | test("should reload current configuration", async () => { |
| 163 | // Initialize with a config |
| 164 | vi.mocked(configLoader.loadConfiguration).mockResolvedValue({ |
| 165 | config: mockConfig as any, |
| 166 | source: { type: "cli-flag", path: "/config.yaml" } as any, |
| 167 | }); |
| 168 | await service.initialize( |
| 169 | { accessToken: "token" } as any, |
| 170 | "/config.yaml", |
| 171 | "org-123", |
| 172 | mockApiClient as any, |
| 173 | ); |
| 174 | |
| 175 | // Modify mock to return updated config |
| 176 | const updatedConfig = { ...mockConfig, name: "updated-assistant" } as any; |
| 177 | vi.mocked(configLoader.loadConfiguration).mockResolvedValue({ |
| 178 | config: updatedConfig, |
| 179 | source: { type: "cli-flag", path: "/config.yaml" } as any, |
| 180 | }); |
| 181 | |
| 182 | const state = await service.reload( |
| 183 | { accessToken: "token" } as any, |
| 184 | "org-123", |
| 185 | mockApiClient as any, |
| 186 | ); |
| 187 | |
| 188 | expect(state).toEqual({ |
| 189 | config: updatedConfig, |
| 190 | configPath: "/config.yaml", |
| 191 | }); |
| 192 | }); |
| 193 | |
| 194 | test("should throw error if no config path available", async () => { |
| 195 | // Initialize without config path |
| 196 | vi.mocked(configLoader.loadConfiguration).mockResolvedValue({ |
| 197 | config: mockConfig as any, |
| 198 | source: { type: "default-agent" } as any, |
| 199 | }); |
| 200 | await service.initialize( |
| 201 | { accessToken: "token" } as any, |
| 202 | undefined, |
| 203 | "org-123", |
| 204 | mockApiClient as any, |
| 205 | ); |
| 206 | |
| 207 | await expect( |
| 208 | service.reload( |
| 209 | { accessToken: "token" } as any, |
| 210 | "org-123", |
| 211 | mockApiClient as any, |
| 212 | ), |
| 213 | ).rejects.toThrow("No configuration path available for reload"); |
| 214 | }); |
| 215 | }); |
| 216 | |
| 217 | describe("updateConfigPath()", () => { |
| 218 | test("should update config path and reload dependent services", async () => { |
| 219 | // Initialize service first |
| 220 | vi.mocked(configLoader.loadConfiguration).mockResolvedValue({ |
| 221 | config: mockConfig as any, |
| 222 | source: { type: "cli-flag", path: "/old.yaml" } as any, |
| 223 | }); |
| 224 | await service.initialize( |
| 225 | { accessToken: "token" } as any, |
| 226 | "/old.yaml", |
| 227 | "org-123", |
| 228 | mockApiClient as any, |
| 229 | ); |
| 230 | |
| 231 | // Mock service container |
| 232 | vi.mocked(workos.loadAuthConfig).mockReturnValue({ |
| 233 | accessToken: "token", |
| 234 | organizationId: "org-123", |
| 235 | } as any); |
| 236 | vi.mocked(serviceContainer.get).mockResolvedValue({ |
| 237 | apiClient: mockApiClient, |
| 238 | }); |
| 239 | |
| 240 | // Mock new config load |
| 241 | const newConfig = { ...mockConfig, name: "new-assistant" } as any; |
| 242 | vi.mocked(configLoader.loadConfiguration).mockResolvedValue({ |
| 243 | config: newConfig, |
| 244 | source: { type: "cli-flag", path: "/new.yaml" } as any, |
| 245 | }); |
| 246 | |
| 247 | await service.updateConfigPath("/new.yaml"); |
| 248 | |
| 249 | expect(service.getState()).toEqual({ |
| 250 | config: newConfig, |
| 251 | configPath: "/new.yaml", |
| 252 | }); |
| 253 | expect(vi.mocked(serviceContainer.set)).toHaveBeenCalledWith( |
| 254 | SERVICE_NAMES.CONFIG, |
| 255 | expect.objectContaining({ config: newConfig }), |
| 256 | ); |
| 257 | expect(vi.mocked(serviceContainer.reload)).toHaveBeenCalledWith( |
| 258 | SERVICE_NAMES.MODEL, |
| 259 | ); |
| 260 | expect(vi.mocked(serviceContainer.reload)).toHaveBeenCalledWith( |
| 261 | SERVICE_NAMES.MCP, |
| 262 | ); |
| 263 | }); |
| 264 | |
| 265 | test("should handle missing API client", async () => { |
| 266 | await service.initialize( |
| 267 | { accessToken: "token" } as any, |
| 268 | "/old.yaml", |
| 269 | "org-123", |
| 270 | mockApiClient as any, |
| 271 | ); |
| 272 | |
| 273 | vi.mocked(workos.loadAuthConfig).mockReturnValue({ |
| 274 | accessToken: "token", |
| 275 | organizationId: "org-123", |
| 276 | } as any); |
| 277 | vi.mocked(serviceContainer.get).mockResolvedValue({ |
| 278 | apiClient: null, |
| 279 | }); |
| 280 | |
| 281 | await expect(service.updateConfigPath("/new.yaml")).rejects.toThrow( |
| 282 | "API client not available", |
| 283 | ); |
| 284 | }); |
| 285 | }); |
| 286 | |
| 287 | describe("getDependencies()", () => { |
| 288 | test("should declare auth and apiClient dependencies", () => { |
| 289 | expect(service.getDependencies()).toEqual(["auth", "apiClient"]); |
| 290 | }); |
| 291 | }); |
| 292 | |
| 293 | describe("Event Emission", () => { |
| 294 | test("should emit stateChanged when switching config", async () => { |
| 295 | vi.mocked(configLoader.loadConfiguration).mockResolvedValue({ |
| 296 | config: mockConfig as any, |
| 297 | source: { type: "cli-flag", path: "/old.yaml" } as any, |
| 298 | }); |
| 299 | await service.initialize( |
| 300 | { accessToken: "token" } as any, |
| 301 | "/old.yaml", |
| 302 | "org-123", |
| 303 | mockApiClient as any, |
| 304 | ); |
| 305 | |
| 306 | const listener = vi.fn(); |
| 307 | service.on("stateChanged", listener); |
| 308 | |
| 309 | const newConfig = { ...mockConfig, name: "new-assistant" } as any; |
| 310 | vi.mocked(configLoader.loadConfiguration).mockResolvedValue({ |
| 311 | config: newConfig, |
| 312 | source: { type: "cli-flag", path: "/new.yaml" } as any, |
| 313 | }); |
| 314 | |
| 315 | await service.switchConfig( |
| 316 | "/new.yaml", |
| 317 | { accessToken: "token" } as any, |
| 318 | "org-123", |
| 319 | mockApiClient as any, |
| 320 | ); |
| 321 | |
| 322 | expect(listener).toHaveBeenCalledWith( |
| 323 | expect.objectContaining({ config: newConfig, configPath: "/new.yaml" }), |
| 324 | expect.objectContaining({ |
| 325 | config: mockConfig, |
| 326 | configPath: "/old.yaml", |
| 327 | }), |
| 328 | ); |
| 329 | }); |
| 330 | |
| 331 | test("should emit error on switch failure", async () => { |
| 332 | await service.initialize( |
| 333 | { accessToken: "token" } as any, |
| 334 | "/old.yaml", |
| 335 | "org-123", |
| 336 | mockApiClient as any, |
| 337 | ); |
| 338 | |
| 339 | const errorListener = vi.fn(); |
| 340 | service.on("error", errorListener); |
| 341 | |
| 342 | const error = new Error("Config load failed"); |
| 343 | vi.mocked(configLoader.loadConfiguration).mockRejectedValue(error); |
| 344 | |
| 345 | await expect( |
| 346 | service.switchConfig( |
| 347 | "/bad.yaml", |
| 348 | { accessToken: "token" } as any, |
| 349 | "org-123", |
| 350 | mockApiClient as any, |
| 351 | ), |
| 352 | ).rejects.toThrow(); |
| 353 | expect(errorListener).toHaveBeenCalledWith(error); |
| 354 | }); |
| 355 | }); |
| 356 | |
| 357 | describe("Legacy Compatibility", () => { |
| 358 | test("updateConfigPath method exists and has correct signature", () => { |
| 359 | // Test that the updateConfigPath method exists and is a function |
| 360 | expect(typeof service.updateConfigPath).toBe("function"); |
| 361 | |
| 362 | // Test that it accepts the correct parameter types |
| 363 | const method = service.updateConfigPath; |
| 364 | expect(method).toBeInstanceOf(Function); |
| 365 | expect(method.length).toBe(1); // Should accept 1 parameter |
| 366 | }); |
| 367 | |
| 368 | test("ConfigService has reactive pattern implementation", () => { |
| 369 | // Verify that ConfigService has the updateConfigPath method |
| 370 | // which is the key method for reactive config switching |
| 371 | expect(service).toHaveProperty("updateConfigPath"); |
| 372 | expect(typeof service.updateConfigPath).toBe("function"); |
| 373 | |
| 374 | // The method signature should accept string | undefined |
| 375 | // This confirms the reactive pattern is implemented |
| 376 | expect(service.updateConfigPath.length).toBe(1); |
| 377 | }); |
| 378 | }); |
| 379 | }); |