|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import type { NextRequest } from 'next/server' |
| 5 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 6 | + |
| 7 | +const { mockClearCache, mockDiscoverServerTools, mockSelect, mockUpdateSet } = vi.hoisted(() => ({ |
| 8 | + mockClearCache: vi.fn(), |
| 9 | + mockDiscoverServerTools: vi.fn(), |
| 10 | + mockSelect: vi.fn(), |
| 11 | + mockUpdateSet: vi.fn(), |
| 12 | +})) |
| 13 | + |
| 14 | +vi.mock('@sim/db', () => ({ |
| 15 | + db: { |
| 16 | + select: mockSelect, |
| 17 | + update: vi.fn().mockReturnValue({ set: mockUpdateSet }), |
| 18 | + }, |
| 19 | +})) |
| 20 | + |
| 21 | +vi.mock('@/lib/core/utils/with-route-handler', () => ({ |
| 22 | + withRouteHandler: (handler: unknown) => handler, |
| 23 | +})) |
| 24 | + |
| 25 | +vi.mock('@/lib/mcp/middleware', () => ({ |
| 26 | + withMcpAuth: |
| 27 | + () => |
| 28 | + ( |
| 29 | + handler: ( |
| 30 | + request: NextRequest, |
| 31 | + context: { userId: string; workspaceId: string; requestId: string }, |
| 32 | + routeContext: { params: Promise<{ id: string }> } |
| 33 | + ) => Promise<Response> |
| 34 | + ) => |
| 35 | + (request: NextRequest, routeContext: { params: Promise<{ id: string }> }) => |
| 36 | + handler( |
| 37 | + request, |
| 38 | + { userId: 'user-1', workspaceId: 'workspace-1', requestId: 'request-1' }, |
| 39 | + routeContext |
| 40 | + ), |
| 41 | +})) |
| 42 | + |
| 43 | +vi.mock('@/lib/mcp/service', () => ({ |
| 44 | + mcpService: { |
| 45 | + clearCache: mockClearCache, |
| 46 | + discoverServerTools: mockDiscoverServerTools, |
| 47 | + }, |
| 48 | +})) |
| 49 | + |
| 50 | +import { POST } from '@/app/api/mcp/servers/[id]/refresh/route' |
| 51 | + |
| 52 | +const initialServer = { |
| 53 | + id: 'server-1', |
| 54 | + workspaceId: 'workspace-1', |
| 55 | + name: 'OAuth Server', |
| 56 | + url: 'https://example.com/mcp', |
| 57 | + connectionStatus: 'connected', |
| 58 | + lastError: null, |
| 59 | + lastConnected: new Date('2026-01-01T00:00:00.000Z'), |
| 60 | + toolCount: 4, |
| 61 | + statusConfig: { consecutiveFailures: 0, lastSuccessfulDiscovery: null }, |
| 62 | +} |
| 63 | + |
| 64 | +const persistedServer = { |
| 65 | + ...initialServer, |
| 66 | + connectionStatus: 'disconnected', |
| 67 | + lastError: null, |
| 68 | + toolCount: 0, |
| 69 | +} |
| 70 | + |
| 71 | +function selectRows(rows: unknown[]) { |
| 72 | + return { |
| 73 | + from: vi.fn().mockReturnValue({ |
| 74 | + where: vi.fn().mockReturnValue({ |
| 75 | + limit: vi.fn().mockResolvedValue(rows), |
| 76 | + }), |
| 77 | + }), |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +describe('MCP server refresh route', () => { |
| 82 | + beforeEach(() => { |
| 83 | + vi.clearAllMocks() |
| 84 | + mockSelect.mockReturnValueOnce(selectRows([initialServer])) |
| 85 | + mockUpdateSet.mockReturnValue({ |
| 86 | + where: vi.fn().mockReturnValue({ returning: vi.fn().mockResolvedValue([persistedServer]) }), |
| 87 | + }) |
| 88 | + }) |
| 89 | + |
| 90 | + it('preserves the service-persisted OAuth pending status', async () => { |
| 91 | + mockDiscoverServerTools.mockRejectedValueOnce(new Error('OAuth authorization required')) |
| 92 | + |
| 93 | + const request = new Request('http://localhost/api/mcp/servers/server-1/refresh', { |
| 94 | + method: 'POST', |
| 95 | + }) as NextRequest |
| 96 | + const response = await POST(request, { params: Promise.resolve({ id: 'server-1' }) }) |
| 97 | + const body = await response.json() |
| 98 | + |
| 99 | + expect(body.data).toEqual( |
| 100 | + expect.objectContaining({ |
| 101 | + status: 'disconnected', |
| 102 | + error: null, |
| 103 | + }) |
| 104 | + ) |
| 105 | + expect(mockUpdateSet).not.toHaveBeenCalledWith( |
| 106 | + expect.objectContaining({ connectionStatus: expect.anything() }) |
| 107 | + ) |
| 108 | + }) |
| 109 | + |
| 110 | + it('reports the discovery failure when status persistence leaves a stale connected row', async () => { |
| 111 | + const reflectedSecret = 'Bearer reflected-static-token' |
| 112 | + mockDiscoverServerTools.mockRejectedValueOnce( |
| 113 | + new Error(`Upstream reflected ${reflectedSecret}`) |
| 114 | + ) |
| 115 | + mockUpdateSet.mockReturnValueOnce({ |
| 116 | + where: vi.fn().mockReturnValue({ |
| 117 | + returning: vi.fn().mockResolvedValue([initialServer]), |
| 118 | + }), |
| 119 | + }) |
| 120 | + |
| 121 | + const request = new Request('http://localhost/api/mcp/servers/server-1/refresh', { |
| 122 | + method: 'POST', |
| 123 | + }) as NextRequest |
| 124 | + const response = await POST(request, { params: Promise.resolve({ id: 'server-1' }) }) |
| 125 | + const body = await response.json() |
| 126 | + |
| 127 | + expect(body.data).toEqual( |
| 128 | + expect.objectContaining({ |
| 129 | + status: 'disconnected', |
| 130 | + error: 'Internal server error', |
| 131 | + workflowsUpdated: 0, |
| 132 | + }) |
| 133 | + ) |
| 134 | + expect(JSON.stringify(body)).not.toContain(reflectedSecret) |
| 135 | + expect(mockClearCache).not.toHaveBeenCalled() |
| 136 | + }) |
| 137 | + |
| 138 | + it('preserves a connected status from a newer successful discovery', async () => { |
| 139 | + mockDiscoverServerTools.mockRejectedValueOnce(new Error('Connection failed')) |
| 140 | + const newerSuccessfulServer = { |
| 141 | + ...initialServer, |
| 142 | + lastConnected: new Date(Date.now() + 60_000), |
| 143 | + toolCount: 7, |
| 144 | + } |
| 145 | + mockUpdateSet.mockReturnValueOnce({ |
| 146 | + where: vi.fn().mockReturnValue({ |
| 147 | + returning: vi.fn().mockResolvedValue([newerSuccessfulServer]), |
| 148 | + }), |
| 149 | + }) |
| 150 | + |
| 151 | + const request = new Request('http://localhost/api/mcp/servers/server-1/refresh', { |
| 152 | + method: 'POST', |
| 153 | + }) as NextRequest |
| 154 | + const response = await POST(request, { params: Promise.resolve({ id: 'server-1' }) }) |
| 155 | + const body = await response.json() |
| 156 | + |
| 157 | + expect(body.data).toEqual( |
| 158 | + expect.objectContaining({ |
| 159 | + status: 'connected', |
| 160 | + error: null, |
| 161 | + toolCount: 7, |
| 162 | + workflowsUpdated: 0, |
| 163 | + }) |
| 164 | + ) |
| 165 | + expect(mockClearCache).toHaveBeenCalledWith('workspace-1') |
| 166 | + }) |
| 167 | + |
| 168 | + it('does not 500 when workflow sync fails after a successful discovery', async () => { |
| 169 | + mockDiscoverServerTools.mockResolvedValueOnce([ |
| 170 | + { |
| 171 | + name: 'search', |
| 172 | + description: 'Search tool', |
| 173 | + inputSchema: {}, |
| 174 | + serverId: 'server-1', |
| 175 | + serverName: 'OAuth Server', |
| 176 | + }, |
| 177 | + ]) |
| 178 | + // The route's server lookup consumes the first select (beforeEach). The sync's |
| 179 | + // workflow select is left unmocked, so it throws — exercising the guard that |
| 180 | + // keeps a secondary sync failure from turning a successful refresh into a 500. |
| 181 | + mockUpdateSet.mockReturnValueOnce({ |
| 182 | + where: vi.fn().mockReturnValue({ returning: vi.fn().mockResolvedValue([initialServer]) }), |
| 183 | + }) |
| 184 | + |
| 185 | + const request = new Request('http://localhost/api/mcp/servers/server-1/refresh', { |
| 186 | + method: 'POST', |
| 187 | + }) as NextRequest |
| 188 | + const response = await POST(request, { params: Promise.resolve({ id: 'server-1' }) }) |
| 189 | + const body = await response.json() |
| 190 | + |
| 191 | + expect(response.status).toBe(200) |
| 192 | + expect(body.data).toEqual( |
| 193 | + expect.objectContaining({ |
| 194 | + status: 'connected', |
| 195 | + workflowsUpdated: 0, |
| 196 | + updatedWorkflowIds: [], |
| 197 | + }) |
| 198 | + ) |
| 199 | + }) |
| 200 | +}) |
0 commit comments