|
| 1 | +import type { McpHub } from "../../../../services/mcp/McpHub" |
| 2 | + |
| 3 | +import { getMcpServersSection } from "../mcp-servers" |
| 4 | + |
| 5 | +const createMockMcpHub = (servers: any[]): McpHub => |
| 6 | + ({ |
| 7 | + getServers: () => servers, |
| 8 | + }) as unknown as McpHub |
| 9 | + |
| 10 | +describe("getMcpServersSection", () => { |
| 11 | + it("should return empty string when mcpHub is undefined", () => { |
| 12 | + const result = getMcpServersSection(undefined) |
| 13 | + expect(result).toBe("") |
| 14 | + }) |
| 15 | + |
| 16 | + it("should return empty string when no servers are connected", () => { |
| 17 | + const hub = createMockMcpHub([]) |
| 18 | + const result = getMcpServersSection(hub) |
| 19 | + expect(result).toBe("") |
| 20 | + }) |
| 21 | + |
| 22 | + it("should return empty string when servers exist but none are connected", () => { |
| 23 | + const hub = createMockMcpHub([ |
| 24 | + { |
| 25 | + name: "test-server", |
| 26 | + status: "disconnected", |
| 27 | + tools: [{ name: "tool1", description: "A tool" }], |
| 28 | + }, |
| 29 | + ]) |
| 30 | + const result = getMcpServersSection(hub) |
| 31 | + expect(result).toBe("") |
| 32 | + }) |
| 33 | + |
| 34 | + it("should include connected server with tools", () => { |
| 35 | + const hub = createMockMcpHub([ |
| 36 | + { |
| 37 | + name: "git", |
| 38 | + status: "connected", |
| 39 | + tools: [ |
| 40 | + { name: "git_log", description: "Show git log" }, |
| 41 | + { name: "git_status", description: "Show git status" }, |
| 42 | + ], |
| 43 | + }, |
| 44 | + ]) |
| 45 | + const result = getMcpServersSection(hub) |
| 46 | + |
| 47 | + expect(result).toContain("MCP SERVERS") |
| 48 | + expect(result).toContain("## git") |
| 49 | + expect(result).toContain("mcp--git--git_log") |
| 50 | + expect(result).toContain("mcp--git--git_status") |
| 51 | + expect(result).toContain("mcp--serverName--toolName") |
| 52 | + }) |
| 53 | + |
| 54 | + it("should filter out tools with enabledForPrompt === false", () => { |
| 55 | + const hub = createMockMcpHub([ |
| 56 | + { |
| 57 | + name: "testServer", |
| 58 | + status: "connected", |
| 59 | + tools: [ |
| 60 | + { name: "enabled_tool", description: "Enabled", enabledForPrompt: true }, |
| 61 | + { name: "disabled_tool", description: "Disabled", enabledForPrompt: false }, |
| 62 | + { name: "default_tool", description: "Default (no flag)" }, |
| 63 | + ], |
| 64 | + }, |
| 65 | + ]) |
| 66 | + const result = getMcpServersSection(hub) |
| 67 | + |
| 68 | + expect(result).toContain("mcp--testServer--enabled_tool") |
| 69 | + expect(result).not.toContain("disabled_tool") |
| 70 | + expect(result).toContain("mcp--testServer--default_tool") |
| 71 | + }) |
| 72 | + |
| 73 | + it("should include server instructions when available", () => { |
| 74 | + const hub = createMockMcpHub([ |
| 75 | + { |
| 76 | + name: "context7", |
| 77 | + status: "connected", |
| 78 | + instructions: "Always use resolve-library-id before get-library-docs.", |
| 79 | + tools: [{ name: "resolve-library-id", description: "Resolve lib" }], |
| 80 | + }, |
| 81 | + ]) |
| 82 | + const result = getMcpServersSection(hub) |
| 83 | + |
| 84 | + expect(result).toContain("## context7") |
| 85 | + expect(result).toContain("Instructions: Always use resolve-library-id before get-library-docs.") |
| 86 | + expect(result).toContain("mcp--context7--resolve-library-id") |
| 87 | + }) |
| 88 | + |
| 89 | + it("should not include instructions block when server has no instructions", () => { |
| 90 | + const hub = createMockMcpHub([ |
| 91 | + { |
| 92 | + name: "simple-server", |
| 93 | + status: "connected", |
| 94 | + tools: [{ name: "do_thing", description: "Does a thing" }], |
| 95 | + }, |
| 96 | + ]) |
| 97 | + const result = getMcpServersSection(hub) |
| 98 | + |
| 99 | + expect(result).toContain("## simple-server") |
| 100 | + expect(result).not.toContain("Instructions:") |
| 101 | + }) |
| 102 | + |
| 103 | + it("should handle server with no tools", () => { |
| 104 | + const hub = createMockMcpHub([ |
| 105 | + { |
| 106 | + name: "no-tools-server", |
| 107 | + status: "connected", |
| 108 | + tools: undefined, |
| 109 | + }, |
| 110 | + ]) |
| 111 | + const result = getMcpServersSection(hub) |
| 112 | + |
| 113 | + expect(result).toContain("## no-tools-server") |
| 114 | + expect(result).toContain("(No tools available)") |
| 115 | + }) |
| 116 | + |
| 117 | + it("should handle multiple connected servers", () => { |
| 118 | + const hub = createMockMcpHub([ |
| 119 | + { |
| 120 | + name: "git", |
| 121 | + status: "connected", |
| 122 | + tools: [{ name: "git_log", description: "Show log" }], |
| 123 | + }, |
| 124 | + { |
| 125 | + name: "context7", |
| 126 | + status: "connected", |
| 127 | + instructions: "Resolve library IDs first.", |
| 128 | + tools: [{ name: "resolve-library-id", description: "Resolve lib" }], |
| 129 | + }, |
| 130 | + { |
| 131 | + name: "offline-server", |
| 132 | + status: "disconnected", |
| 133 | + tools: [{ name: "should_not_appear", description: "Hidden" }], |
| 134 | + }, |
| 135 | + ]) |
| 136 | + const result = getMcpServersSection(hub) |
| 137 | + |
| 138 | + expect(result).toContain("## git") |
| 139 | + expect(result).toContain("## context7") |
| 140 | + expect(result).not.toContain("## offline-server") |
| 141 | + expect(result).not.toContain("should_not_appear") |
| 142 | + }) |
| 143 | + |
| 144 | + it("should only include connected servers in the section", () => { |
| 145 | + const hub = createMockMcpHub([ |
| 146 | + { |
| 147 | + name: "connecting-server", |
| 148 | + status: "connecting", |
| 149 | + tools: [{ name: "tool1", description: "Tool 1" }], |
| 150 | + }, |
| 151 | + ]) |
| 152 | + const result = getMcpServersSection(hub) |
| 153 | + |
| 154 | + // "connecting" is not "connected", so no servers section should be generated |
| 155 | + expect(result).toBe("") |
| 156 | + }) |
| 157 | + |
| 158 | + it("should handle server with all tools disabled", () => { |
| 159 | + const hub = createMockMcpHub([ |
| 160 | + { |
| 161 | + name: "all-disabled", |
| 162 | + status: "connected", |
| 163 | + tools: [ |
| 164 | + { name: "tool1", description: "Tool 1", enabledForPrompt: false }, |
| 165 | + { name: "tool2", description: "Tool 2", enabledForPrompt: false }, |
| 166 | + ], |
| 167 | + }, |
| 168 | + ]) |
| 169 | + const result = getMcpServersSection(hub) |
| 170 | + |
| 171 | + expect(result).toContain("## all-disabled") |
| 172 | + expect(result).toContain("(No tools available)") |
| 173 | + expect(result).not.toContain("mcp--all-disabled--tool1") |
| 174 | + expect(result).not.toContain("mcp--all-disabled--tool2") |
| 175 | + }) |
| 176 | +}) |
0 commit comments