@@ -7,9 +7,11 @@ import {
77 unmanageNamespace ,
88 cancelSharding ,
99 POLLING_INTERVAL ,
10+ type ShardKey ,
1011} from './reducer' ;
1112import sinon from 'sinon' ;
1213import type {
14+ AtlasShardKey ,
1315 AutomationAgentDeploymentStatusApiResponse ,
1416 AutomationAgentProcess ,
1517 ClusterDetailsApiResponse ,
@@ -74,14 +76,14 @@ function createStore({
7476 | {
7577 isNamespaceManaged ?: ( ) => boolean ;
7678 hasShardingError ?: ( ) => boolean ;
77- hasShardKey ?: ( ) => boolean ;
79+ hasShardKey ?: ( ) => boolean | AtlasShardKey ;
7880 failsOnShardingRequest ?: ( ) => boolean ;
7981 authenticatedFetchStub ?: never ;
8082 }
8183 | {
8284 isNamespaceManaged ?: never ;
8385 hasShardingError ?: never ;
84- hasShardKey ?: ( ) => boolean ;
86+ hasShardKey ?: ( ) => boolean | ShardKey ;
8587 failsOnShardingRequest ?: never ;
8688 authenticatedFetchStub ?: ( ) => void ;
8789 } = { } ) : GlobalWritesStore {
@@ -117,20 +119,24 @@ function createStore({
117119 } ) ,
118120 automationAgentAwait : ( _meta : unknown , type : string ) => {
119121 if ( type === 'getShardKey' ) {
122+ const shardKey = hasShardKey ( ) ;
120123 return {
121- response : hasShardKey ( )
122- ? [
123- {
124- key : {
125- location : 'range' ,
126- secondary : shardKeyData . isCustomShardKeyHashed
127- ? 'hashed'
128- : 'range' ,
124+ response :
125+ shardKey === true
126+ ? [
127+ {
128+ key : {
129+ location : 'range' ,
130+ secondary : shardKeyData . isCustomShardKeyHashed
131+ ? 'hashed'
132+ : 'range' ,
133+ } ,
134+ unique : true ,
129135 } ,
130- unique : true ,
131- } ,
132- ]
133- : [ ] ,
136+ ]
137+ : typeof shardKey === 'object'
138+ ? [ shardKey ]
139+ : [ ] ,
134140 } ;
135141 }
136142 } ,
@@ -340,6 +346,76 @@ describe('GlobalWritesStore Store', function () {
340346 expect ( store . getState ( ) . status ) . to . equal ( 'SHARD_KEY_CORRECT' ) ;
341347 } ) ;
342348
349+ context ( 'invalid and mismatching shard keys' , function ( ) {
350+ it ( 'there is no location' , async function ( ) {
351+ const store = createStore ( {
352+ isNamespaceManaged : ( ) => true ,
353+ hasShardKey : ( ) => ( {
354+ _id : '123' ,
355+ key : {
356+ notLocation : 'range' , // invalid
357+ secondary : 'range' ,
358+ } ,
359+ unique : true ,
360+ } ) ,
361+ } ) ;
362+ await waitFor ( ( ) => {
363+ expect ( store . getState ( ) . status ) . to . equal ( 'SHARD_KEY_INVALID' ) ;
364+ } ) ;
365+ } ) ;
366+
367+ it ( 'location is not a range' , async function ( ) {
368+ const store = createStore ( {
369+ isNamespaceManaged : ( ) => true ,
370+ hasShardKey : ( ) => ( {
371+ _id : '123' ,
372+ key : {
373+ location : 'hashed' , // invalid
374+ secondary : 'range' ,
375+ } ,
376+ unique : true ,
377+ } ) ,
378+ } ) ;
379+ await waitFor ( ( ) => {
380+ expect ( store . getState ( ) . status ) . to . equal ( 'SHARD_KEY_INVALID' ) ;
381+ } ) ;
382+ } ) ;
383+
384+ it ( 'secondary key does not match' , async function ( ) {
385+ const store = createStore ( {
386+ isNamespaceManaged : ( ) => true ,
387+ hasShardKey : ( ) => ( {
388+ _id : '123' ,
389+ key : {
390+ location : 'range' ,
391+ tertiary : 'range' , // this is a different secondary key
392+ } ,
393+ unique : true ,
394+ } ) ,
395+ } ) ;
396+ await waitFor ( ( ) => {
397+ expect ( store . getState ( ) . status ) . to . equal ( 'SHARD_KEY_MISMATCH' ) ;
398+ } ) ;
399+ } ) ;
400+
401+ it ( 'uniqueness does not match' , async function ( ) {
402+ const store = createStore ( {
403+ isNamespaceManaged : ( ) => true ,
404+ hasShardKey : ( ) => ( {
405+ _id : '123' ,
406+ key : {
407+ location : 'range' ,
408+ secondary : 'range' ,
409+ } ,
410+ unique : false , // this does not match
411+ } ) ,
412+ } ) ;
413+ await waitFor ( ( ) => {
414+ expect ( store . getState ( ) . status ) . to . equal ( 'SHARD_KEY_MISMATCH' ) ;
415+ } ) ;
416+ } ) ;
417+ } ) ;
418+
343419 it ( 'sharding error' , async function ( ) {
344420 const store = createStore ( {
345421 isNamespaceManaged : ( ) => true ,
0 commit comments