|
| 1 | +import type { ClineProvider } from "../../../core/webview/ClineProvider" |
| 2 | +import { McpHub } from "../McpHub" |
| 3 | +import { vi, describe, it, expect, beforeEach } from "vitest" |
| 4 | +import fs from "fs/promises" |
| 5 | + |
| 6 | +// Minimal VSCode and FS mocks to keep constructor side-effects inert |
| 7 | +vi.mock("vscode", () => ({ |
| 8 | + workspace: { |
| 9 | + createFileSystemWatcher: vi.fn().mockReturnValue({ |
| 10 | + onDidChange: vi.fn(), |
| 11 | + onDidCreate: vi.fn(), |
| 12 | + onDidDelete: vi.fn(), |
| 13 | + dispose: vi.fn(), |
| 14 | + }), |
| 15 | + onDidChangeWorkspaceFolders: vi.fn(), |
| 16 | + workspaceFolders: [], |
| 17 | + }, |
| 18 | + window: { |
| 19 | + showErrorMessage: vi.fn(), |
| 20 | + showInformationMessage: vi.fn(), |
| 21 | + showWarningMessage: vi.fn(), |
| 22 | + }, |
| 23 | + Disposable: { |
| 24 | + from: vi.fn(), |
| 25 | + }, |
| 26 | +})) |
| 27 | + |
| 28 | +vi.mock("fs/promises", () => ({ |
| 29 | + default: { |
| 30 | + access: vi.fn().mockResolvedValue(undefined), |
| 31 | + writeFile: vi.fn().mockResolvedValue(undefined), |
| 32 | + readFile: vi.fn().mockResolvedValue("{}"), |
| 33 | + unlink: vi.fn().mockResolvedValue(undefined), |
| 34 | + rename: vi.fn().mockResolvedValue(undefined), |
| 35 | + lstat: vi.fn().mockResolvedValue({ isDirectory: () => true }), |
| 36 | + mkdir: vi.fn().mockResolvedValue(undefined), |
| 37 | + }, |
| 38 | + access: vi.fn().mockResolvedValue(undefined), |
| 39 | + writeFile: vi.fn().mockResolvedValue(undefined), |
| 40 | + readFile: vi.fn().mockResolvedValue("{}"), |
| 41 | + unlink: vi.fn().mockResolvedValue(undefined), |
| 42 | + rename: vi.fn().mockResolvedValue(undefined), |
| 43 | + lstat: vi.fn().mockResolvedValue({ isDirectory: () => true }), |
| 44 | + mkdir: vi.fn().mockResolvedValue(undefined), |
| 45 | +})) |
| 46 | + |
| 47 | +describe("McpHub transient-retry and readiness", () => { |
| 48 | + let mcpHub: McpHub |
| 49 | + let mockProvider: Partial<ClineProvider> |
| 50 | + |
| 51 | + beforeEach(() => { |
| 52 | + vi.clearAllMocks() |
| 53 | + |
| 54 | + // Prevent constructor from kicking off async initialization that races our injected connections |
| 55 | + vi.spyOn(McpHub.prototype as any, "initializeGlobalMcpServers").mockResolvedValue(undefined as any) |
| 56 | + vi.spyOn(McpHub.prototype as any, "initializeProjectMcpServers").mockResolvedValue(undefined as any) |
| 57 | + |
| 58 | + mockProvider = { |
| 59 | + ensureSettingsDirectoryExists: vi.fn().mockResolvedValue("/mock/settings/path"), |
| 60 | + ensureMcpServersDirectoryExists: vi.fn().mockResolvedValue("/mock/settings/path"), |
| 61 | + postMessageToWebview: vi.fn().mockResolvedValue(undefined), |
| 62 | + getState: vi.fn().mockResolvedValue({ mcpEnabled: true }), |
| 63 | + context: { |
| 64 | + subscriptions: [], |
| 65 | + workspaceState: {} as any, |
| 66 | + globalState: {} as any, |
| 67 | + secrets: {} as any, |
| 68 | + extensionUri: { fsPath: "/test/path" } as any, |
| 69 | + extensionPath: "/test/path", |
| 70 | + asAbsolutePath: (p: string) => p, |
| 71 | + globalStorageUri: { fsPath: "/test/global" } as any, |
| 72 | + globalStoragePath: "/test/global", |
| 73 | + extension: { |
| 74 | + packageJSON: { version: "1.0.0" }, |
| 75 | + } as any, |
| 76 | + } as any, |
| 77 | + } |
| 78 | + |
| 79 | + // Keep global config empty so constructor does not attempt to connect anything |
| 80 | + vi.mocked(fs.readFile).mockResolvedValue("{}") |
| 81 | + |
| 82 | + mcpHub = new McpHub(mockProvider as ClineProvider) |
| 83 | + }) |
| 84 | + |
| 85 | + it("retries once on transient -32000 'Connection closed' for tools/call", async () => { |
| 86 | + // Prepare a connected connection |
| 87 | + const firstError: any = new Error("Connection closed") |
| 88 | + firstError.code = -32000 |
| 89 | + |
| 90 | + const request = vi |
| 91 | + .fn() |
| 92 | + // First call: throw transient |
| 93 | + .mockRejectedValueOnce(firstError) |
| 94 | + // Second call: succeed |
| 95 | + .mockResolvedValueOnce({ content: [{ type: "text", text: "ok" }] }) |
| 96 | + |
| 97 | + // Inject a connected connection directly |
| 98 | + ;(mcpHub as any).connections = [ |
| 99 | + { |
| 100 | + type: "connected", |
| 101 | + server: { |
| 102 | + name: "retry-server", |
| 103 | + config: JSON.stringify({ type: "stdio", command: "node", timeout: 60 }), |
| 104 | + status: "connected", |
| 105 | + source: "global", |
| 106 | + errorHistory: [], |
| 107 | + }, |
| 108 | + client: { request } as any, |
| 109 | + transport: {} as any, |
| 110 | + }, |
| 111 | + ] |
| 112 | + |
| 113 | + const result = await mcpHub.callTool("retry-server", "do_something", { a: 1 }) |
| 114 | + expect(result).toBeTruthy() |
| 115 | + expect(request).toHaveBeenCalledTimes(2) |
| 116 | + }) |
| 117 | + |
| 118 | + it("retries once on transient -32000 'Connection closed' for resources/read", async () => { |
| 119 | + const firstError: any = new Error("Connection closed before response") |
| 120 | + firstError.code = -32000 |
| 121 | + |
| 122 | + const request = vi |
| 123 | + .fn() |
| 124 | + .mockRejectedValueOnce(firstError) |
| 125 | + .mockResolvedValueOnce({ contents: [{ type: "text", text: "resource" }] }) |
| 126 | + |
| 127 | + ;(mcpHub as any).connections = [ |
| 128 | + { |
| 129 | + type: "connected", |
| 130 | + server: { |
| 131 | + name: "retry-server", |
| 132 | + config: JSON.stringify({ type: "stdio", command: "node", timeout: 60 }), |
| 133 | + status: "connected", |
| 134 | + source: "global", |
| 135 | + errorHistory: [], |
| 136 | + }, |
| 137 | + client: { request } as any, |
| 138 | + transport: {} as any, |
| 139 | + }, |
| 140 | + ] |
| 141 | + |
| 142 | + const result = await mcpHub.readResource("retry-server", "file:///tmp/x.txt") |
| 143 | + expect(result).toBeTruthy() |
| 144 | + expect(request).toHaveBeenCalledTimes(2) |
| 145 | + }) |
| 146 | + |
| 147 | + it("waits for server to become connected before first tool call", async () => { |
| 148 | + // Start 'connecting' and then flip to 'connected' shortly after |
| 149 | + const request = vi.fn().mockResolvedValue({ content: [{ type: "text", text: "ok" }] }) |
| 150 | + |
| 151 | + const connection: any = { |
| 152 | + type: "connected", |
| 153 | + server: { |
| 154 | + name: "slow-server", |
| 155 | + config: JSON.stringify({ type: "stdio", command: "node", timeout: 60 }), |
| 156 | + status: "connecting", |
| 157 | + source: "global", |
| 158 | + errorHistory: [], |
| 159 | + }, |
| 160 | + client: { request }, |
| 161 | + transport: {}, |
| 162 | + } |
| 163 | + |
| 164 | + ;(mcpHub as any).connections = [connection] |
| 165 | + |
| 166 | + // Flip status to 'connected' after 100ms |
| 167 | + setTimeout(() => { |
| 168 | + connection.server.status = "connected" |
| 169 | + }, 100) |
| 170 | + |
| 171 | + const start = Date.now() |
| 172 | + const result = await mcpHub.callTool("slow-server", "ping", {}) |
| 173 | + const elapsed = Date.now() - start |
| 174 | + |
| 175 | + expect(result).toBeTruthy() |
| 176 | + expect(request).toHaveBeenCalledTimes(1) |
| 177 | + // Should have waited at least ~100ms before making the request |
| 178 | + expect(elapsed).toBeGreaterThanOrEqual(90) |
| 179 | + }) |
| 180 | +}) |
0 commit comments