|
| 1 | +import * as fs from "fs/promises" |
| 2 | +import * as os from "os" |
| 3 | +import * as path from "path" |
| 4 | +import type { McpServer } from "@roo-code/types" |
| 5 | +import type { ClineProvider } from "../../../core/webview/ClineProvider" |
| 6 | +import { GlobalFileNames } from "../../../shared/globalFileNames" |
| 7 | +import { McpHub } from "../../mcp/McpHub" |
| 8 | +import { Bm25Ranker } from "../Bm25Ranker" |
| 9 | +import { ToolRouter } from "../ToolRouter" |
| 10 | + |
| 11 | +vi.mock("../../../core/webview/ClineProvider") |
| 12 | + |
| 13 | +describe("ToolRouter with McpHub", () => { |
| 14 | + let directory: string |
| 15 | + let hub: McpHub |
| 16 | + const router = ToolRouter.getInstance() |
| 17 | + const tools = [{ serverName: "github", toolName: "issues", description: "search tickets" }] |
| 18 | + function addServer(): McpServer { |
| 19 | + const server: McpServer = { |
| 20 | + name: "github", |
| 21 | + config: JSON.stringify({ command: "node" }), |
| 22 | + status: "connecting", |
| 23 | + source: "global", |
| 24 | + } |
| 25 | + hub.connections = [{ type: "disconnected", client: null, transport: null, server }] |
| 26 | + return server |
| 27 | + } |
| 28 | + |
| 29 | + beforeEach(async () => { |
| 30 | + directory = await fs.mkdtemp(path.join(os.tmpdir(), "tool-router-")) |
| 31 | + const provider: Partial<ClineProvider> = { |
| 32 | + cwd: directory, |
| 33 | + ensureSettingsDirectoryExists: async () => directory, |
| 34 | + postMessageToWebview: vi.fn().mockResolvedValue(undefined), |
| 35 | + getState: vi.fn().mockResolvedValue({ mcpEnabled: false }), |
| 36 | + } |
| 37 | + // The hub uses only cwd, settings directory, state, and webview posting. |
| 38 | + hub = new McpHub(provider as ClineProvider) |
| 39 | + await hub.waitUntilReady() |
| 40 | + }) |
| 41 | + |
| 42 | + afterEach(async () => { |
| 43 | + await hub.dispose() |
| 44 | + await fs.rm(directory, { recursive: true, force: true }) |
| 45 | + vi.restoreAllMocks() |
| 46 | + }) |
| 47 | + |
| 48 | + it("returns empty before any servers are configured", async () => { |
| 49 | + expect(hub.getServers()).toEqual([]) |
| 50 | + expect(await router.attachInitial("tickets", [], 5)).toEqual([]) |
| 51 | + expect(await router.search("tickets", [], 5)).toEqual([]) |
| 52 | + }) |
| 53 | + |
| 54 | + it("invalidates the lazy index when a server is disabled and enabled", async () => { |
| 55 | + await fs.writeFile( |
| 56 | + path.join(directory, GlobalFileNames.mcpSettings), |
| 57 | + JSON.stringify({ |
| 58 | + mcpServers: { github: { command: "node" } }, |
| 59 | + }), |
| 60 | + ) |
| 61 | + // An in-flight connection allows toggling without starting an external MCP process. |
| 62 | + addServer() |
| 63 | + const changed = vi.fn() |
| 64 | + const subscription = hub.onServerListChanged(changed) |
| 65 | + const rank = vi.spyOn(Bm25Ranker.prototype, "rank") |
| 66 | + expect(await router.search("tickets", tools, 5)).toEqual(["mcp--github--issues"]) |
| 67 | + const firstIndex = rank.mock.calls[0][1] |
| 68 | + |
| 69 | + await hub.toggleServerDisabled("github", true) |
| 70 | + expect(changed).toHaveBeenCalledTimes(1) |
| 71 | + expect(hub.getServers()).toEqual([]) |
| 72 | + expect(router["indexedTools"]).toBeUndefined() |
| 73 | + expect(await router.search("tickets", [], 5)).toEqual([]) |
| 74 | + |
| 75 | + await hub.toggleServerDisabled("github", false) |
| 76 | + expect(changed).toHaveBeenCalledTimes(2) |
| 77 | + expect(hub.getServers()).toHaveLength(1) |
| 78 | + expect(await router.attachInitial("tickets", tools, 5)).toEqual(["mcp--github--issues"]) |
| 79 | + expect(rank.mock.calls[1][1]).not.toBe(firstIndex) |
| 80 | + const enabledIndex = rank.mock.calls[1][1] |
| 81 | + await hub["notifyWebviewOfServerChanges"]() |
| 82 | + expect(changed).toHaveBeenCalledTimes(2) |
| 83 | + await router.search("tickets", tools, 5) |
| 84 | + expect(rank.mock.calls[2][1]).toBe(enabledIndex) |
| 85 | + subscription.dispose() |
| 86 | + await hub.dispose() |
| 87 | + expect(changed).toHaveBeenCalledTimes(2) |
| 88 | + expect(router["indexedTools"]).toBeUndefined() |
| 89 | + }) |
| 90 | + |
| 91 | + it.each<[string, Partial<McpServer>]>([ |
| 92 | + ["name", { name: "jira" }], |
| 93 | + ["source", { source: "project" }], |
| 94 | + ["config", { config: JSON.stringify({ command: "python" }) }], |
| 95 | + ["status", { status: "connected" }], |
| 96 | + ["disabled", { disabled: true }], |
| 97 | + ["tools", { tools: [{ name: "issues", description: "tickets" }] }], |
| 98 | + ])("invalidates once when server %s changes", async (_field, update) => { |
| 99 | + const server = addServer() |
| 100 | + await hub["notifyWebviewOfServerChanges"]() |
| 101 | + const changed = vi.fn() |
| 102 | + hub.onServerListChanged(changed) |
| 103 | + await router.search("tickets", tools, 5) |
| 104 | + Object.assign(server, update) |
| 105 | + await hub["notifyWebviewOfServerChanges"]() |
| 106 | + expect(changed).toHaveBeenCalledTimes(1) |
| 107 | + expect(router["indexedTools"]).toBeUndefined() |
| 108 | + await hub["notifyWebviewOfServerChanges"]() |
| 109 | + expect(changed).toHaveBeenCalledTimes(1) |
| 110 | + }) |
| 111 | + |
| 112 | + it("isolates throwing listeners during notifications and disposal", async () => { |
| 113 | + const failure = new Error("listener failed") |
| 114 | + const log = vi.spyOn(console, "error").mockImplementation(() => {}) |
| 115 | + hub.onServerListChanged(() => { |
| 116 | + throw failure |
| 117 | + }) |
| 118 | + const changed = vi.fn() |
| 119 | + hub.onServerListChanged(changed) |
| 120 | + addServer() |
| 121 | + const post = vi.spyOn(hub["providerRef"].deref()!, "postMessageToWebview") |
| 122 | + await expect(hub["notifyWebviewOfServerChanges"]()).resolves.toBeUndefined() |
| 123 | + expect(post).toHaveBeenCalled() |
| 124 | + expect(changed).toHaveBeenCalledTimes(1) |
| 125 | + expect(log).toHaveBeenCalledWith("[McpHub] Server-list change listener failed:", failure) |
| 126 | + await expect(hub.dispose()).resolves.toBeUndefined() |
| 127 | + expect(changed).toHaveBeenCalledTimes(2) |
| 128 | + expect(hub["serverListEvents"].listenerCount("changed")).toBe(0) |
| 129 | + }) |
| 130 | +}) |
0 commit comments