|
| 1 | +import type { SinonStub } from 'sinon'; |
| 2 | +import sinon from 'sinon'; |
| 3 | +import { expect } from 'chai'; |
| 4 | +import { afterEach, beforeEach } from 'mocha'; |
| 5 | +import * as vscode from 'vscode'; |
| 6 | +import type { ExtensionContext } from 'vscode'; |
| 7 | +import * as MCPServer from 'mongodb-mcp-server'; |
| 8 | +import { ExtensionContextStub } from '../stubs'; |
| 9 | +import type { MCPServerInfo } from '../../../mcp/mcpController'; |
| 10 | +import { MCPController } from '../../../mcp/mcpController'; |
| 11 | +import ConnectionController from '../../../connectionController'; |
| 12 | +import { StatusView } from '../../../views'; |
| 13 | +import { StorageController } from '../../../storage'; |
| 14 | +import { TelemetryService } from '../../../telemetry'; |
| 15 | +import { TEST_DATABASE_URI } from '../dbTestHelper'; |
| 16 | + |
| 17 | +const sandbox = sinon.createSandbox(); |
| 18 | +suite('MCPController test suite', function () { |
| 19 | + this.timeout(300000); |
| 20 | + let extensionContext: ExtensionContext; |
| 21 | + let connectionController: ConnectionController; |
| 22 | + let mcpController: MCPController; |
| 23 | + |
| 24 | + beforeEach(() => { |
| 25 | + extensionContext = new ExtensionContextStub(); |
| 26 | + const testStorageController = new StorageController(extensionContext); |
| 27 | + const testTelemetryService = new TelemetryService( |
| 28 | + testStorageController, |
| 29 | + extensionContext, |
| 30 | + ); |
| 31 | + connectionController = new ConnectionController({ |
| 32 | + statusView: new StatusView(extensionContext), |
| 33 | + storageController: testStorageController, |
| 34 | + telemetryService: testTelemetryService, |
| 35 | + }); |
| 36 | + |
| 37 | + mcpController = new MCPController(extensionContext, connectionController); |
| 38 | + }); |
| 39 | + |
| 40 | + afterEach(async () => { |
| 41 | + sandbox.restore(); |
| 42 | + sandbox.reset(); |
| 43 | + connectionController.clearAllConnections(); |
| 44 | + await vscode.workspace.getConfiguration('mdb').update('mcp.server', null); |
| 45 | + }); |
| 46 | + |
| 47 | + test('should register mcp server definition provider', function () { |
| 48 | + // At-least one from our mcp controller |
| 49 | + expect(extensionContext.subscriptions.length).to.be.greaterThanOrEqual(1); |
| 50 | + }); |
| 51 | + |
| 52 | + suite('#activate', function () { |
| 53 | + test('should subscribe to ACTIVE_CONNECTION_CHANGED event', async function () { |
| 54 | + const addEventListenerSpy = sandbox.spy( |
| 55 | + connectionController, |
| 56 | + 'addEventListener', |
| 57 | + ); |
| 58 | + await mcpController.activate(); |
| 59 | + expect(addEventListenerSpy).to.be.called; |
| 60 | + expect(addEventListenerSpy.args[0]).to.contain( |
| 61 | + 'ACTIVE_CONNECTION_CHANGED', |
| 62 | + ); |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + suite('#startServer', function () { |
| 67 | + test('should initialize HTTP transport and start it', async function () { |
| 68 | + await mcpController.startServer(); |
| 69 | + const serverInfo = (mcpController as any).server as |
| 70 | + | MCPServerInfo |
| 71 | + | undefined; |
| 72 | + expect(serverInfo).to.not.be.undefined; |
| 73 | + expect(serverInfo?.runner).to.be.instanceOf( |
| 74 | + MCPServer.StreamableHttpRunner, |
| 75 | + ); |
| 76 | + expect(serverInfo?.headers?.authorization).to.not.be.undefined; |
| 77 | + }); |
| 78 | + }); |
| 79 | + |
| 80 | + suite('when mcp server start is enabled from config', function () { |
| 81 | + test('it should start mcp server without any confirmation', async function () { |
| 82 | + await vscode.workspace |
| 83 | + .getConfiguration('mdb') |
| 84 | + .update('mcp.server', 'enabled'); |
| 85 | + |
| 86 | + const showInformationSpy = sandbox.spy( |
| 87 | + vscode.window, |
| 88 | + 'showInformationMessage', |
| 89 | + ); |
| 90 | + const startServerSpy = sandbox.spy(mcpController, 'startServer'); |
| 91 | + // listen to connection events |
| 92 | + await mcpController.activate(); |
| 93 | + // add a new connection to trigger connection change |
| 94 | + await connectionController.addNewConnectionStringAndConnect({ |
| 95 | + connectionString: TEST_DATABASE_URI, |
| 96 | + }); |
| 97 | + |
| 98 | + expect(showInformationSpy).to.not.be.called; |
| 99 | + expect(startServerSpy).to.be.calledOnce; |
| 100 | + }); |
| 101 | + }); |
| 102 | + |
| 103 | + suite('when mcp server start is disabled from config', function () { |
| 104 | + test('it should not start mcp server and ask for no confirmation', async function () { |
| 105 | + await vscode.workspace |
| 106 | + .getConfiguration('mdb') |
| 107 | + .update('mcp.server', 'disabled'); |
| 108 | + |
| 109 | + const showInformationSpy = sandbox.spy( |
| 110 | + vscode.window, |
| 111 | + 'showInformationMessage', |
| 112 | + ); |
| 113 | + const startServerSpy = sandbox.spy(mcpController, 'startServer'); |
| 114 | + // listen to connection events |
| 115 | + await mcpController.activate(); |
| 116 | + // add a new connection to trigger connection change |
| 117 | + await connectionController.addNewConnectionStringAndConnect({ |
| 118 | + connectionString: TEST_DATABASE_URI, |
| 119 | + }); |
| 120 | + |
| 121 | + expect(showInformationSpy).to.not.be.called; |
| 122 | + expect(startServerSpy).to.not.be.called; |
| 123 | + }); |
| 124 | + }); |
| 125 | + |
| 126 | + suite('when mcp server start is not configured', function () { |
| 127 | + test('it should ask before starting the mcp server, and update the configuration with the chosen value', async function () { |
| 128 | + const updateStub = sandbox.stub(); |
| 129 | + const fakeGetConfiguration = sandbox.fake.returns({ |
| 130 | + get: () => null, |
| 131 | + update: updateStub, |
| 132 | + }); |
| 133 | + sandbox.replace( |
| 134 | + vscode.workspace, |
| 135 | + 'getConfiguration', |
| 136 | + fakeGetConfiguration, |
| 137 | + ); |
| 138 | + |
| 139 | + const showInformationStub: SinonStub = sandbox.stub( |
| 140 | + vscode.window, |
| 141 | + 'showInformationMessage', |
| 142 | + ); |
| 143 | + showInformationStub.resolves('Yes'); |
| 144 | + const startServerSpy = sandbox.spy(mcpController, 'startServer'); |
| 145 | + // listen to connection events |
| 146 | + await mcpController.activate(); |
| 147 | + // add a new connection to trigger connection change |
| 148 | + await connectionController.addNewConnectionStringAndConnect({ |
| 149 | + connectionString: TEST_DATABASE_URI, |
| 150 | + }); |
| 151 | + expect(showInformationStub).to.be.calledOnce; |
| 152 | + expect(updateStub).to.be.calledWith('mcp.server', 'enabled', true); |
| 153 | + expect(startServerSpy).to.be.called; |
| 154 | + }); |
| 155 | + |
| 156 | + test('it should ask before starting the mcp server, and when denied, should not start the server', async function () { |
| 157 | + const updateStub = sandbox.stub(); |
| 158 | + const fakeGetConfiguration = sandbox.fake.returns({ |
| 159 | + get: () => null, |
| 160 | + update: updateStub, |
| 161 | + }); |
| 162 | + sandbox.replace( |
| 163 | + vscode.workspace, |
| 164 | + 'getConfiguration', |
| 165 | + fakeGetConfiguration, |
| 166 | + ); |
| 167 | + |
| 168 | + const showInformationStub: SinonStub = sandbox.stub( |
| 169 | + vscode.window, |
| 170 | + 'showInformationMessage', |
| 171 | + ); |
| 172 | + showInformationStub.resolves('No'); |
| 173 | + const startServerSpy = sandbox.spy(mcpController, 'startServer'); |
| 174 | + // listen to connection events |
| 175 | + await mcpController.activate(); |
| 176 | + // add a new connection to trigger connection change |
| 177 | + await connectionController.addNewConnectionStringAndConnect({ |
| 178 | + connectionString: TEST_DATABASE_URI, |
| 179 | + }); |
| 180 | + expect(showInformationStub).to.be.calledOnce; |
| 181 | + expect(updateStub).to.be.calledWith('mcp.server', 'disabled', true); |
| 182 | + expect(startServerSpy).to.not.be.called; |
| 183 | + }); |
| 184 | + }); |
| 185 | + |
| 186 | + suite('when an MCP server is already running', function () { |
| 187 | + test('it should notify the connection manager of the connection changed event', async function () { |
| 188 | + // We want to connect as soon as connection changes |
| 189 | + await vscode.workspace |
| 190 | + .getConfiguration('mdb') |
| 191 | + .update('mcp.server', 'enabled'); |
| 192 | + |
| 193 | + // Start the controller and list to events |
| 194 | + await mcpController.activate(); |
| 195 | + |
| 196 | + // Add a connection |
| 197 | + await connectionController.addNewConnectionStringAndConnect({ |
| 198 | + connectionString: TEST_DATABASE_URI, |
| 199 | + }); |
| 200 | + |
| 201 | + const connectionChangedSpy = sandbox.spy( |
| 202 | + mcpController as any, |
| 203 | + 'onActiveConnectionChanged', |
| 204 | + ); |
| 205 | + |
| 206 | + await connectionController.disconnect(); |
| 207 | + expect(connectionChangedSpy).to.be.calledOnce; |
| 208 | + }); |
| 209 | + }); |
| 210 | +}); |
0 commit comments