|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 5 | + |
| 6 | +const { |
| 7 | + mockCreateMcpServer, |
| 8 | + mockDeleteMcpServer, |
| 9 | + mockUpdateMcpServer, |
| 10 | + mockVerifyServerConnection, |
| 11 | +} = vi.hoisted(() => ({ |
| 12 | + mockCreateMcpServer: vi.fn(), |
| 13 | + mockDeleteMcpServer: vi.fn(), |
| 14 | + mockUpdateMcpServer: vi.fn(), |
| 15 | + mockVerifyServerConnection: vi.fn(), |
| 16 | +})) |
| 17 | + |
| 18 | +vi.mock('@sim/db', () => ({ db: {} })) |
| 19 | + |
| 20 | +vi.mock('@/lib/mcp/orchestration', () => ({ |
| 21 | + performCreateMcpServer: mockCreateMcpServer, |
| 22 | + performDeleteMcpServer: mockDeleteMcpServer, |
| 23 | + performUpdateMcpServer: mockUpdateMcpServer, |
| 24 | +})) |
| 25 | + |
| 26 | +vi.mock('@/lib/mcp/service', () => ({ |
| 27 | + mcpService: { verifyServerConnection: mockVerifyServerConnection }, |
| 28 | +})) |
| 29 | + |
| 30 | +import type { ExecutionContext } from '@/lib/copilot/request/types' |
| 31 | +import { executeManageMcpTool } from './manage-mcp-tool' |
| 32 | + |
| 33 | +const context = { |
| 34 | + workspaceId: 'workspace-1', |
| 35 | + userId: 'user-1', |
| 36 | + userPermission: 'write', |
| 37 | +} as ExecutionContext |
| 38 | + |
| 39 | +describe('executeManageMcpTool verification', () => { |
| 40 | + beforeEach(() => { |
| 41 | + vi.clearAllMocks() |
| 42 | + }) |
| 43 | + |
| 44 | + it('verifies a newly added server before returning it to the agent', async () => { |
| 45 | + mockCreateMcpServer.mockResolvedValue({ |
| 46 | + success: true, |
| 47 | + serverId: 'mcp-1', |
| 48 | + updated: false, |
| 49 | + authType: 'headers', |
| 50 | + }) |
| 51 | + mockVerifyServerConnection.mockResolvedValue({ |
| 52 | + verified: true, |
| 53 | + toolCount: 3, |
| 54 | + requiresAuthorization: false, |
| 55 | + }) |
| 56 | + |
| 57 | + const result = await executeManageMcpTool( |
| 58 | + { |
| 59 | + operation: 'add', |
| 60 | + config: { name: 'Memory', url: 'https://memory.example.com/mcp' }, |
| 61 | + }, |
| 62 | + context |
| 63 | + ) |
| 64 | + |
| 65 | + expect(mockVerifyServerConnection).toHaveBeenCalledWith('user-1', 'mcp-1', 'workspace-1') |
| 66 | + expect(result).toEqual( |
| 67 | + expect.objectContaining({ |
| 68 | + success: true, |
| 69 | + output: expect.objectContaining({ |
| 70 | + serverId: 'mcp-1', |
| 71 | + verification: { |
| 72 | + verified: true, |
| 73 | + toolCount: 3, |
| 74 | + requiresAuthorization: false, |
| 75 | + }, |
| 76 | + }), |
| 77 | + }) |
| 78 | + ) |
| 79 | + }) |
| 80 | + |
| 81 | + it('retains a created server and reports a failed verification', async () => { |
| 82 | + mockCreateMcpServer.mockResolvedValue({ |
| 83 | + success: true, |
| 84 | + serverId: 'mcp-1', |
| 85 | + updated: false, |
| 86 | + authType: 'headers', |
| 87 | + }) |
| 88 | + mockVerifyServerConnection.mockResolvedValue({ |
| 89 | + verified: false, |
| 90 | + toolCount: 0, |
| 91 | + requiresAuthorization: false, |
| 92 | + error: 'Connection failed', |
| 93 | + }) |
| 94 | + |
| 95 | + const result = await executeManageMcpTool( |
| 96 | + { |
| 97 | + operation: 'add', |
| 98 | + config: { name: 'Memory', url: 'https://memory.example.com/mcp' }, |
| 99 | + }, |
| 100 | + context |
| 101 | + ) |
| 102 | + |
| 103 | + expect(result.success).toBe(true) |
| 104 | + expect(result.output).toEqual( |
| 105 | + expect.objectContaining({ |
| 106 | + verification: expect.objectContaining({ |
| 107 | + verified: false, |
| 108 | + error: 'Connection failed', |
| 109 | + }), |
| 110 | + }) |
| 111 | + ) |
| 112 | + }) |
| 113 | + |
| 114 | + it('skips verification when a newly added server is disabled', async () => { |
| 115 | + mockCreateMcpServer.mockResolvedValue({ |
| 116 | + success: true, |
| 117 | + serverId: 'mcp-1', |
| 118 | + updated: false, |
| 119 | + authType: 'headers', |
| 120 | + }) |
| 121 | + |
| 122 | + const result = await executeManageMcpTool( |
| 123 | + { |
| 124 | + operation: 'add', |
| 125 | + config: { |
| 126 | + name: 'Memory', |
| 127 | + url: 'https://memory.example.com/mcp', |
| 128 | + enabled: false, |
| 129 | + }, |
| 130 | + }, |
| 131 | + context |
| 132 | + ) |
| 133 | + |
| 134 | + expect(mockVerifyServerConnection).not.toHaveBeenCalled() |
| 135 | + expect(result.output).toEqual( |
| 136 | + expect.objectContaining({ |
| 137 | + verification: { |
| 138 | + verified: false, |
| 139 | + toolCount: 0, |
| 140 | + requiresAuthorization: false, |
| 141 | + skipped: true, |
| 142 | + reason: 'server_disabled', |
| 143 | + }, |
| 144 | + }) |
| 145 | + ) |
| 146 | + }) |
| 147 | + |
| 148 | + it('re-verifies a server after editing its connection config', async () => { |
| 149 | + mockUpdateMcpServer.mockResolvedValue({ |
| 150 | + success: true, |
| 151 | + server: { id: 'mcp-1', name: 'Memory', enabled: true }, |
| 152 | + }) |
| 153 | + mockVerifyServerConnection.mockResolvedValue({ |
| 154 | + verified: true, |
| 155 | + toolCount: 2, |
| 156 | + requiresAuthorization: false, |
| 157 | + }) |
| 158 | + |
| 159 | + const result = await executeManageMcpTool( |
| 160 | + { |
| 161 | + operation: 'edit', |
| 162 | + serverId: 'mcp-1', |
| 163 | + config: { headers: { 'X-API-Key': '{{MEMORY_KEY}}' } }, |
| 164 | + }, |
| 165 | + context |
| 166 | + ) |
| 167 | + |
| 168 | + expect(mockVerifyServerConnection).toHaveBeenCalledWith('user-1', 'mcp-1', 'workspace-1') |
| 169 | + expect(result.output).toEqual( |
| 170 | + expect.objectContaining({ |
| 171 | + verification: expect.objectContaining({ verified: true, toolCount: 2 }), |
| 172 | + }) |
| 173 | + ) |
| 174 | + }) |
| 175 | + |
| 176 | + it('skips verification after a cosmetic-only edit', async () => { |
| 177 | + mockUpdateMcpServer.mockResolvedValue({ |
| 178 | + success: true, |
| 179 | + server: { id: 'mcp-1', name: 'Renamed Memory', enabled: true }, |
| 180 | + }) |
| 181 | + |
| 182 | + const result = await executeManageMcpTool( |
| 183 | + { |
| 184 | + operation: 'edit', |
| 185 | + serverId: 'mcp-1', |
| 186 | + config: { name: 'Renamed Memory' }, |
| 187 | + }, |
| 188 | + context |
| 189 | + ) |
| 190 | + |
| 191 | + expect(mockVerifyServerConnection).not.toHaveBeenCalled() |
| 192 | + expect(result.output).toEqual( |
| 193 | + expect.objectContaining({ |
| 194 | + verification: expect.objectContaining({ |
| 195 | + verified: false, |
| 196 | + skipped: true, |
| 197 | + reason: 'connection_unchanged', |
| 198 | + }), |
| 199 | + }) |
| 200 | + ) |
| 201 | + }) |
| 202 | +}) |
0 commit comments