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