(t *testing.T)
| 353 | } |
| 354 | |
| 355 | func TestMCPConfigWatcherFiresOnFileWrite(t *testing.T) { |
| 356 | dir := t.TempDir() |
| 357 | cfgPath := filepath.Join(dir, "mcp_servers.json") |
| 358 | // Initial config: srv1 enabled, transport unsupported so startServer |
| 359 | // fails fast — we only care that Reload is invoked. |
| 360 | if err := os.WriteFile(cfgPath, []byte(`{"mcpServers":[{"name":"srv1","transport":"x","enabled":true}]}`), 0644); err != nil { |
| 361 | t.Fatal(err) |
| 362 | } |
| 363 | mgr := mcp.NewManager(zap.NewNop()) |
| 364 | if err := mgr.LoadConfig(cfgPath); err != nil { |
| 365 | t.Fatal(err) |
| 366 | } |
| 367 | ctx, cancel := context.WithCancel(context.Background()) |
| 368 | defer cancel() |
| 369 | cli := &ChatCLI{ |
| 370 | logger: zap.NewNop(), |
| 371 | mcpManager: mgr, |
| 372 | mcpCtx: ctx, |
| 373 | mcpConfigPath: cfgPath, |
| 374 | } |
| 375 | cli.startMCPConfigWatcher() |
| 376 | defer cli.stopMCPConfigWatcher() |
| 377 | if cli.mcpWatcher == nil { |
| 378 | t.Fatal("watcher did not start") |
| 379 | } |
| 380 | // Edit the file: add srv2 — Reload should pick it up. |
| 381 | if err := os.WriteFile(cfgPath, []byte(`{"mcpServers":[ |
| 382 | {"name":"srv1","transport":"x","enabled":true}, |
| 383 | {"name":"srv2","transport":"x","enabled":true} |
| 384 | ]}`), 0644); err != nil { |
| 385 | t.Fatal(err) |
| 386 | } |
| 387 | // Watcher debounces 400ms; allow a bit more for fsnotify delivery. |
| 388 | deadline := time.Now().Add(3 * time.Second) |
| 389 | for time.Now().Before(deadline) { |
| 390 | if len(mgr.GetServerStatus()) == 2 { |
| 391 | return |
| 392 | } |
| 393 | time.Sleep(50 * time.Millisecond) |
| 394 | } |
| 395 | t.Fatalf("watcher did not propagate edit; got %d servers, want 2", len(mgr.GetServerStatus())) |
| 396 | } |
| 397 | |
| 398 | // TestMCPConfigWatcherFiresOnFileCreation pins the hot-reload |
| 399 | // behavior for the "empty start" case: chatcli boots with the config |
nothing calls this directly
no test coverage detected