File tree Expand file tree Collapse file tree 4 files changed +31
-8
lines changed Expand file tree Collapse file tree 4 files changed +31
-8
lines changed Original file line number Diff line number Diff line change @@ -20,6 +20,7 @@ import {
20
20
tick ,
21
21
useTmpdir ,
22
22
waitEval ,
23
+ waitMongoshCompletionResults ,
23
24
} from '../test/repl-helpers' ;
24
25
import type { MongoshIOProvider , MongoshNodeReplOptions } from './mongosh-repl' ;
25
26
import MongoshNodeRepl from './mongosh-repl' ;
@@ -364,12 +365,7 @@ describe('MongoshNodeRepl', function () {
364
365
const tabtab = async ( ) => {
365
366
await tab ( ) ;
366
367
if ( process . env . USE_NEW_AUTOCOMPLETE ) {
367
- // TODO: This is because autocomplete() is async and will either list
368
- // databases or collections or sample documents, any of which takes time
369
- // to complete and can time out. There is probably a better way.
370
- await new Promise ( ( resolve ) => {
371
- setTimeout ( resolve , 210 ) ;
372
- } ) ;
368
+ await waitMongoshCompletionResults ( bus ) ;
373
369
}
374
370
await tab ( ) ;
375
371
} ;
Original file line number Diff line number Diff line change @@ -548,7 +548,11 @@ class MongoshNodeRepl implements EvaluationListener {
548
548
}
549
549
} ) ( ) ,
550
550
] ) ;
551
- this . bus . emit ( 'mongosh:autocompletion-complete' ) ; // For testing.
551
+ this . bus . emit (
552
+ 'mongosh:autocompletion-complete' ,
553
+ replResults ,
554
+ mongoshResults
555
+ ) ; // For testing.
552
556
553
557
// Sometimes the mongosh completion knows that what it is doing is right,
554
558
// and that autocompletion based on inspecting the actual objects that
Original file line number Diff line number Diff line change @@ -75,6 +75,25 @@ async function waitCompletion(bus: MongoshBus) {
75
75
await tick ( ) ;
76
76
}
77
77
78
+ async function waitMongoshCompletionResults ( bus : MongoshBus ) {
79
+ // Waiting for the completion results can "time out" if an async action such
80
+ // as listing the databases or collections or loading the schema takes longer
81
+ // than 200ms (at the time of writing), but by the next try or at least
82
+ // eventually the action should complete and then the next autocomplete call
83
+ // will return the cached result.
84
+ let found = false ;
85
+ while ( ! found ) {
86
+ const [ , mongoshResults ] = await waitBus (
87
+ bus ,
88
+ 'mongosh:autocompletion-complete'
89
+ ) ;
90
+ if ( mongoshResults . length === 0 ) {
91
+ found = true ;
92
+ }
93
+ }
94
+ await tick ( ) ;
95
+ }
96
+
78
97
const fakeTTYProps : Partial < ReadStream & WriteStream > = {
79
98
isTTY : true ,
80
99
isRaw : true ,
@@ -106,6 +125,7 @@ export {
106
125
waitBus ,
107
126
waitEval ,
108
127
waitCompletion ,
128
+ waitMongoshCompletionResults ,
109
129
fakeTTYProps ,
110
130
readReplLogFile ,
111
131
} ;
Original file line number Diff line number Diff line change @@ -335,7 +335,10 @@ export interface MongoshBusEventsMap extends ConnectEventMap {
335
335
* Signals the completion of the autocomplete suggestion providers.
336
336
* _ONLY AVAILABLE FOR TESTING._
337
337
*/
338
- 'mongosh:autocompletion-complete' : ( ) => void ;
338
+ 'mongosh:autocompletion-complete' : (
339
+ resplResults : string [ ] ,
340
+ mongoshResults : string [ ]
341
+ ) => void ;
339
342
/**
340
343
* Signals the completion of the asynchronous interrupt handler in MongoshRepl. Not fired for interrupts of _synchronous_ code.
341
344
* _ONLY AVAILABLE FOR TESTING._
You can’t perform that action at this time.
0 commit comments