|
| 1 | +import sinon from 'sinon'; |
| 2 | +import { expect } from 'chai'; |
| 3 | +import { afterEach, beforeEach } from 'mocha'; |
| 4 | +import { createMCPConnectionErrorHandler } from '../../../mcp/mcpConnectionErrorHandler'; |
| 5 | +import ConnectionController from '../../../connectionController'; |
| 6 | +import { ExtensionContextStub } from '../stubs'; |
| 7 | +import { StorageController } from '../../../storage'; |
| 8 | +import { TelemetryService } from '../../../telemetry'; |
| 9 | +import { StatusView } from '../../../views'; |
| 10 | +import type { |
| 11 | + ConnectionErrorHandled, |
| 12 | + ConnectionErrorHandlerContext, |
| 13 | +} from '@himanshusinghs/mongodb-mcp-server'; |
| 14 | +import { ErrorCodes } from '@himanshusinghs/mongodb-mcp-server'; |
| 15 | + |
| 16 | +class MongoDBError extends Error { |
| 17 | + constructor( |
| 18 | + public code: |
| 19 | + | ErrorCodes.NotConnectedToMongoDB |
| 20 | + | ErrorCodes.MisconfiguredConnectionString, |
| 21 | + message: string, |
| 22 | + ) { |
| 23 | + super(message); |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +const sandbox = sinon.createSandbox(); |
| 28 | +suite('mcpConnectionErrorHandler suite', () => { |
| 29 | + let connectionController: ConnectionController; |
| 30 | + beforeEach(() => { |
| 31 | + const extensionContext = new ExtensionContextStub(); |
| 32 | + const testStorageController = new StorageController(extensionContext); |
| 33 | + const testTelemetryService = new TelemetryService( |
| 34 | + testStorageController, |
| 35 | + extensionContext, |
| 36 | + ); |
| 37 | + connectionController = new ConnectionController({ |
| 38 | + statusView: new StatusView(extensionContext), |
| 39 | + storageController: testStorageController, |
| 40 | + telemetryService: testTelemetryService, |
| 41 | + }); |
| 42 | + }); |
| 43 | + |
| 44 | + afterEach(() => { |
| 45 | + sandbox.reset(); |
| 46 | + sandbox.restore(); |
| 47 | + }); |
| 48 | + |
| 49 | + test('should handle NotConnectedToMongoDB error', () => { |
| 50 | + const handler = createMCPConnectionErrorHandler(connectionController); |
| 51 | + const result = handler( |
| 52 | + new MongoDBError( |
| 53 | + ErrorCodes.NotConnectedToMongoDB, |
| 54 | + 'Not connected to MongoDB', |
| 55 | + ), |
| 56 | + {} as ConnectionErrorHandlerContext, |
| 57 | + ) as ConnectionErrorHandled; |
| 58 | + |
| 59 | + expect(result.errorHandled).to.be.true; |
| 60 | + expect(result.result.content).to.deep.contain({ |
| 61 | + type: 'text', |
| 62 | + text: 'You need to connect to a MongoDB instance before you can access its data.', |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + test('should handle MisconfiguredConnectionString error', () => { |
| 67 | + const handler = createMCPConnectionErrorHandler(connectionController); |
| 68 | + const result = handler( |
| 69 | + new MongoDBError( |
| 70 | + ErrorCodes.MisconfiguredConnectionString, |
| 71 | + 'Misconfigured MongoDB string', |
| 72 | + ), |
| 73 | + {} as ConnectionErrorHandlerContext, |
| 74 | + ) as ConnectionErrorHandled; |
| 75 | + |
| 76 | + expect(result.errorHandled).to.be.true; |
| 77 | + expect(result.result.content).to.deep.contain({ |
| 78 | + type: 'text', |
| 79 | + text: 'MCP server is having trouble connecting to the selected connection in the MongoDB VSCode extension.', |
| 80 | + }); |
| 81 | + }); |
| 82 | + |
| 83 | + test('should not handle any other errors', () => { |
| 84 | + const handler = createMCPConnectionErrorHandler(connectionController); |
| 85 | + expect( |
| 86 | + handler( |
| 87 | + new MongoDBError(ErrorCodes.ForbiddenCollscan as any, 'Some error'), |
| 88 | + {} as any, |
| 89 | + ), |
| 90 | + ).to.deep.equal({ |
| 91 | + errorHandled: false, |
| 92 | + }); |
| 93 | + expect(handler(new Error('Some error') as any, {} as any)).to.deep.equal({ |
| 94 | + errorHandled: false, |
| 95 | + }); |
| 96 | + }); |
| 97 | +}); |
0 commit comments