Skip to content

Commit 0d62a1d

Browse files
chore: tests for connection error handler
1 parent 40c2293 commit 0d62a1d

File tree

3 files changed

+102
-5
lines changed

3 files changed

+102
-5
lines changed

src/mcp/mcpConnectionErrorHandler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export const createMCPConnectionErrorHandler = (
5353
content: [
5454
{
5555
type: 'text',
56-
text: `MCP server is having trouble connecting to the ${activeConnectionName ?? 'selected connection in the MongoDB VSCode extension'}.`,
56+
text: `MCP server is having trouble connecting to ${activeConnectionName ? activeConnectionName : 'the selected connection in the MongoDB VSCode extension'}.`,
5757
},
5858
...resolutionGuidance.map<{ type: 'text'; text: string }>(
5959
(text) => ({ type: 'text', text }),

src/mcp/mcpConnectionManager.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ export class MCPConnectionManager extends ConnectionManager {
3131
connect(): Promise<AnyConnectionState> {
3232
return Promise.reject(
3333
new Error(
34-
[
35-
'MongoDB MCP Server in MongoDB VSCode extension makes use of the connection that the MongoDB VSCode extension is connected to.',
36-
"To connect, choose a connection from MongoDB VSCode extensions's sidepanel - https://www.mongodb.com/docs/mongodb-vscode/connect/#connect-to-your-mongodb-deployment",
37-
].join(' '),
34+
// MCP runs in node process
35+
// eslint-disable-next-line no-multi-str
36+
"MongoDB MCP Server in MongoDB VSCode extension makes use of the connection that the MongoDB VSCode extension is connected to.\
37+
To connect, choose a connection from MongoDB VSCode extensions's sidepanel - https://www.mongodb.com/docs/mongodb-vscode/connect/#connect-to-your-mongodb-deployment",
3838
),
3939
);
4040
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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

Comments
 (0)