@@ -115,7 +115,7 @@ describe('DataService', function () {
115115 fatal : ( ) => { } ,
116116 } ;
117117
118- let dataServiceLogTest ;
118+ let dataServiceLogTest : DataService | undefined ;
119119
120120 beforeEach ( function ( ) {
121121 logs . length = 0 ;
@@ -1557,6 +1557,24 @@ describe('DataService', function () {
15571557 } ) ;
15581558 } ) ;
15591559
1560+ describe ( '#fetchShardKey' , function ( ) {
1561+ beforeEach ( async function ( ) {
1562+ await mongoClient
1563+ . db ( testDatabaseName )
1564+ . collection ( testCollectionName )
1565+ . createIndex (
1566+ {
1567+ a : 1 ,
1568+ } ,
1569+ { }
1570+ ) ;
1571+ } ) ;
1572+
1573+ it ( 'fetches the shard key (there is none in this test)' , async function ( ) {
1574+ expect ( await dataService . fetchShardKey ( testNamespace ) ) . to . equal ( null ) ;
1575+ } ) ;
1576+ } ) ;
1577+
15601578 describe ( 'CSFLE logging' , function ( ) {
15611579 it ( 'picks a selected set of CSFLE options for logging' , function ( ) {
15621580 const fleOptions : ConnectionFleOptions = {
@@ -2010,6 +2028,122 @@ describe('DataService', function () {
20102028 } ) ;
20112029 } ) ;
20122030
2031+ context ( 'with real sharded cluster' , function ( ) {
2032+ this . slow ( 10_000 ) ;
2033+ this . timeout ( 20_000 ) ;
2034+
2035+ const cluster = mochaTestServer ( {
2036+ topology : 'sharded' ,
2037+ secondaries : 0 ,
2038+ } ) ;
2039+
2040+ let dataService : DataServiceImpl ;
2041+ let mongoClient : MongoClient ;
2042+ let connectionOptions : ConnectionOptions ;
2043+ let testCollectionName : string ;
2044+ let testDatabaseName : string ;
2045+ let testNamespace : string ;
2046+
2047+ before ( async function ( ) {
2048+ testDatabaseName = `compass-data-service-sharded-tests` ;
2049+ const connectionString = cluster ( ) . connectionString ;
2050+ connectionOptions = {
2051+ connectionString,
2052+ } ;
2053+
2054+ mongoClient = new MongoClient ( connectionOptions . connectionString ) ;
2055+ await mongoClient . connect ( ) ;
2056+
2057+ dataService = new DataServiceImpl ( connectionOptions ) ;
2058+ await dataService . connect ( ) ;
2059+ } ) ;
2060+
2061+ after ( async function ( ) {
2062+ // eslint-disable-next-line no-console
2063+ await dataService ?. disconnect ( ) . catch ( console . log ) ;
2064+ await mongoClient ?. close ( ) ;
2065+ } ) ;
2066+
2067+ beforeEach ( async function ( ) {
2068+ testCollectionName = `coll-${ new UUID ( ) . toString ( ) } ` ;
2069+ testNamespace = `${ testDatabaseName } .${ testCollectionName } ` ;
2070+
2071+ await mongoClient
2072+ . db ( testDatabaseName )
2073+ . collection ( testCollectionName )
2074+ . insertMany ( TEST_DOCS ) ;
2075+
2076+ await mongoClient
2077+ . db ( testDatabaseName )
2078+ . collection ( testCollectionName )
2079+ . createIndex (
2080+ {
2081+ a : 1 ,
2082+ } ,
2083+ { }
2084+ ) ;
2085+ } ) ;
2086+
2087+ afterEach ( async function ( ) {
2088+ sinon . restore ( ) ;
2089+
2090+ await mongoClient
2091+ . db ( testDatabaseName )
2092+ . collection ( testCollectionName )
2093+ . drop ( ) ;
2094+ } ) ;
2095+
2096+ describe ( 'with a sharded collection' , function ( ) {
2097+ beforeEach ( async function ( ) {
2098+ await runCommand ( dataService [ '_database' ] ( 'admin' , 'META' ) , {
2099+ shardCollection : testNamespace ,
2100+ key : {
2101+ a : 1 ,
2102+ } ,
2103+ // We don't run the shardCollection command outside of tests
2104+ // so it isn't part of the runCommand type.
2105+ } as unknown as Parameters < typeof runCommand > [ 1 ] ) ;
2106+ } ) ;
2107+
2108+ describe ( '#fetchShardKey' , function ( ) {
2109+ it ( 'fetches the shard key' , async function ( ) {
2110+ expect ( await dataService . fetchShardKey ( testNamespace ) ) . to . deep . equal ( {
2111+ a : 1 ,
2112+ } ) ;
2113+
2114+ // Can be cancelled.
2115+ const abortController = new AbortController ( ) ;
2116+ const abortSignal = abortController . signal ;
2117+ const promise = dataService
2118+ . fetchShardKey (
2119+ testNamespace ,
2120+ { } ,
2121+ { abortSignal : abortSignal as unknown as AbortSignal }
2122+ )
2123+ . catch ( ( err ) => err ) ;
2124+ abortController . abort ( ) ;
2125+ const error = await promise ;
2126+
2127+ expect ( dataService . isCancelError ( error ) ) . to . be . true ;
2128+ } ) ;
2129+ } ) ;
2130+
2131+ describe ( '#indexes' , function ( ) {
2132+ it ( 'includes the shard key' , async function ( ) {
2133+ const indexes = await dataService . indexes ( testNamespace ) ;
2134+
2135+ expect ( indexes . length ) . to . equal ( 2 ) ;
2136+ expect (
2137+ indexes . find ( ( index ) => index . key . _id === 1 ) ?. properties
2138+ ) . to . not . include ( 'shardKey' ) ;
2139+ expect (
2140+ indexes . find ( ( index ) => index . key . a === 1 ) ?. properties
2141+ ) . to . include ( 'shardKey' ) ;
2142+ } ) ;
2143+ } ) ;
2144+ } ) ;
2145+ } ) ;
2146+
20132147 context ( 'with mocked client' , function ( ) {
20142148 function createDataServiceWithMockedClient (
20152149 clientConfig : Partial < ClientMockOptions >
0 commit comments