@@ -7,10 +7,6 @@ import { config } from "../../src/config.js";
77import { jest } from "@jest/globals" ;
88import logger , { LogId } from "../../src/logger.js" ;
99import { createHmac } from "crypto" ;
10- import { NodeDriverServiceProvider } from "@mongosh/service-provider-node-driver" ;
11-
12- jest . mock ( "@mongosh/service-provider-node-driver" ) ;
13- const MockNodeDriverServiceProvider = NodeDriverServiceProvider as jest . MockedClass < typeof NodeDriverServiceProvider > ;
1410
1511// Mock the ApiClient to avoid real API calls
1612jest . mock ( "../../src/common/atlas/apiClient.js" ) ;
@@ -323,104 +319,4 @@ describe("Telemetry", () => {
323319 } ) ;
324320 } ) ;
325321 } ) ;
326-
327- describe ( "connectToMongoDB" , ( ) => {
328- beforeEach ( ( ) => {
329- session = new Session ( {
330- apiClientId : "test-client-id" ,
331- apiBaseUrl : "https://api.test.com" ,
332- } ) ;
333-
334- MockNodeDriverServiceProvider . connect = jest . fn ( ( ) =>
335- Promise . resolve ( { } as unknown as NodeDriverServiceProvider )
336- ) ;
337- } ) ;
338-
339- afterEach ( ( ) => {
340- config . telemetry = "enabled" ;
341- } ) ;
342-
343- const testCases : {
344- connectionString : string ;
345- isAtlas : boolean ;
346- expectAppName : boolean ;
347- expectTelemetryId : boolean ;
348- name : string ;
349- disableTelemetry ?: boolean ;
350- } [ ] = [
351- {
352- connectionString : "mongodb://localhost:27017" ,
353- isAtlas : false ,
354- expectAppName : true ,
355- expectTelemetryId : false ,
356- name : "local db" ,
357- } ,
358- {
359- connectionString : "mongodb+srv://test.mongodb.net/test?retryWrites=true&w=majority" ,
360- isAtlas : true ,
361- expectAppName : true ,
362- expectTelemetryId : true ,
363- name : "atlas db" ,
364- } ,
365- {
366- connectionString : "mongodb://localhost:27017?appName=CustomAppName" ,
367- isAtlas : false ,
368- expectAppName : false ,
369- expectTelemetryId : false ,
370- name : "local db with custom appName" ,
371- } ,
372- {
373- connectionString :
374- "mongodb+srv://test.mongodb.net/test?retryWrites=true&w=majority&appName=CustomAppName" ,
375- isAtlas : true ,
376- expectAppName : false ,
377- expectTelemetryId : false ,
378- name : "atlas db with custom appName" ,
379- } ,
380-
381- {
382- connectionString : "mongodb+srv://test.mongodb.net/test?retryWrites=true&w=majority" ,
383- isAtlas : true ,
384- expectAppName : true ,
385- expectTelemetryId : false ,
386- name : "atlas db with telemetry disabled" ,
387- disableTelemetry : true ,
388- } ,
389- ] ;
390-
391- for ( const testCase of testCases ) {
392- it ( `should update connection string for ${ testCase . name } ` , async ( ) => {
393- if ( testCase . disableTelemetry ) {
394- config . telemetry = "disabled" ;
395- telemetry = Telemetry . create ( session , config , {
396- getRawMachineId : ( ) => Promise . resolve ( machineId ) ,
397- } ) ;
398- }
399-
400- await session . connectToMongoDB ( testCase . connectionString , config . connectOptions , telemetry ) ;
401- expect ( session . serviceProvider ) . toBeDefined ( ) ;
402-
403- // eslint-disable-next-line @typescript-eslint/unbound-method
404- const connectMock = MockNodeDriverServiceProvider . connect as jest . Mock ;
405- expect ( connectMock ) . toHaveBeenCalledOnce ( ) ;
406- const connectionString = connectMock . mock . calls [ 0 ] [ 0 ] ;
407- if ( testCase . expectAppName ) {
408- expect ( connectionString ) . toContain ( "appName=MongoDB+MCP+Server" ) ;
409- } else {
410- expect ( connectionString ) . not . toContain ( "appName=MongoDB+MCP+Server" ) ;
411- }
412-
413- if ( testCase . disableTelemetry ) {
414- expect ( connectionString ) . not . toMatch ( / a p p N a m e = [ ^ - ] * - [ ^ & ] * / ) ;
415- } else {
416- const telemetryId = await telemetry . deviceIdPromise ;
417- if ( testCase . isAtlas && testCase . expectTelemetryId ) {
418- expect ( connectionString ) . toContain ( telemetryId ) ;
419- } else {
420- expect ( connectionString ) . not . toContain ( telemetryId ) ;
421- }
422- }
423- } ) ;
424- }
425- } ) ;
426322} ) ;
0 commit comments