@@ -3,6 +3,7 @@ import { MongoDBAutocompleter } from './index';
3
3
import type { AutocompletionContext } from './autocompletion-context' ;
4
4
import { analyzeDocuments } from 'mongodb-schema' ;
5
5
import { expect } from 'chai' ;
6
+ import { relativeNodePath } from '@mongodb-js/ts-autocomplete' ;
6
7
7
8
/*
8
9
This is intended as deliberately diabolical database and collection names to
@@ -25,6 +26,7 @@ describe('MongoDBAutocompleter', function () {
25
26
let fallbackServiceHost : ts . LanguageServiceHost ;
26
27
let autocompleterContext : AutocompletionContext ;
27
28
let autocompleter : MongoDBAutocompleter ;
29
+ let autocompleterWithFallback : MongoDBAutocompleter ;
28
30
let encounteredPaths : EncounteredPaths ;
29
31
30
32
beforeEach ( function ( ) {
@@ -51,19 +53,19 @@ describe('MongoDBAutocompleter', function () {
51
53
ts . sys . readFile ( fileName ) || '' ,
52
54
) ;
53
55
54
- encounteredPaths . getScriptSnapshot . push ( fileName ) ;
56
+ encounteredPaths . getScriptSnapshot . push ( relativeNodePath ( fileName ) ) ;
55
57
return result ;
56
58
} ,
57
59
fileExists : ( fileName : string ) => {
58
60
const result = ts . sys . fileExists ( fileName ) ;
59
61
if ( result ) {
60
- encounteredPaths . fileExists . push ( fileName ) ;
62
+ encounteredPaths . fileExists . push ( relativeNodePath ( fileName ) ) ;
61
63
}
62
64
return result ;
63
65
} ,
64
66
readFile : ( fileName : string ) => {
65
67
const result = ts . sys . readFile ( fileName ) ;
66
- encounteredPaths . readFile . push ( fileName ) ;
68
+ encounteredPaths . readFile . push ( relativeNodePath ( fileName ) ) ;
67
69
return result ;
68
70
} ,
69
71
readDirectory : ( ...args ) => {
@@ -126,11 +128,32 @@ describe('MongoDBAutocompleter', function () {
126
128
127
129
autocompleter = new MongoDBAutocompleter ( {
128
130
context : autocompleterContext ,
131
+ } ) ;
132
+
133
+ autocompleterWithFallback = new MongoDBAutocompleter ( {
134
+ context : autocompleterContext ,
129
135
fallbackServiceHost,
130
136
} ) ;
131
137
} ) ;
132
138
133
- afterEach ( function ( ) {
139
+ /*
140
+ This test can be used to recreate the list of deps in extract-types.ts.
141
+
142
+ ie. if you comment out the deps structure so it is an empty object, run
143
+ extract-types (so it is just everything except the node types and Javascript
144
+ lib) and then run this test, then it will essentially print what that structure
145
+ needs to be.
146
+
147
+ The other tests would fail at the same time because they don't use the fallback
148
+ service host, so typescript wouldn't load all the dependencies.
149
+ */
150
+ it ( 'autocompletes' , async function ( ) {
151
+ await autocompleterWithFallback . autocomplete ( 'db.foo.find({ fo' ) ;
152
+
153
+ encounteredPaths . fileExists . sort ( ) ;
154
+ encounteredPaths . getScriptSnapshot . sort ( ) ;
155
+ encounteredPaths . readFile . sort ( ) ;
156
+
134
157
// this is what tells us what we're missing in extract-types.ts
135
158
expect ( encounteredPaths ) . to . deep . equal ( {
136
159
fileExists : [ ] ,
@@ -139,33 +162,6 @@ describe('MongoDBAutocompleter', function () {
139
162
} ) ;
140
163
} ) ;
141
164
142
- it ( 'deals with no connection' , async function ( ) {
143
- // The body of tests are all wrapped in loops so that we exercise the
144
- // caching logic in the autocompleter.
145
- for ( let i = 0 ; i < 2 ; i ++ ) {
146
- autocompleterContext . currentDatabaseAndConnection = ( ) => {
147
- return undefined ;
148
- } ;
149
-
150
- const completions = await autocompleter . autocomplete ( 'db.' ) ;
151
- expect ( completions ) . to . deep . equal ( [ ] ) ;
152
- }
153
- } ) ;
154
-
155
- it ( 'does not leak the bson package' , async function ( ) {
156
- for ( let i = 0 ; i < 2 ; i ++ ) {
157
- const completions = await autocompleter . autocomplete ( 'bson.' ) ;
158
- expect ( completions ) . to . deep . equal ( [ ] ) ;
159
- }
160
- } ) ;
161
-
162
- it ( 'does not leak the ShellAPI package' , async function ( ) {
163
- for ( let i = 0 ; i < 2 ; i ++ ) {
164
- const completions = await autocompleter . autocomplete ( 'ShellAPI.' ) ;
165
- expect ( completions ) . to . deep . equal ( [ ] ) ;
166
- }
167
- } ) ;
168
-
169
165
it ( 'completes a bson expression' , async function ( ) {
170
166
for ( let i = 0 ; i < 2 ; i ++ ) {
171
167
const completions = await autocompleter . autocomplete ( 'Ob' ) ;
@@ -285,6 +281,33 @@ describe('MongoDBAutocompleter', function () {
285
281
] ) ;
286
282
} ) ;
287
283
284
+ it ( 'deals with no connection' , async function ( ) {
285
+ // The body of tests are all wrapped in loops so that we exercise the
286
+ // caching logic in the autocompleter.
287
+ for ( let i = 0 ; i < 2 ; i ++ ) {
288
+ autocompleterContext . currentDatabaseAndConnection = ( ) => {
289
+ return undefined ;
290
+ } ;
291
+
292
+ const completions = await autocompleter . autocomplete ( 'db.' ) ;
293
+ expect ( completions ) . to . deep . equal ( [ ] ) ;
294
+ }
295
+ } ) ;
296
+
297
+ it ( 'does not leak the bson package' , async function ( ) {
298
+ for ( let i = 0 ; i < 2 ; i ++ ) {
299
+ const completions = await autocompleter . autocomplete ( 'bson.' ) ;
300
+ expect ( completions ) . to . deep . equal ( [ ] ) ;
301
+ }
302
+ } ) ;
303
+
304
+ it ( 'does not leak the ShellAPI package' , async function ( ) {
305
+ for ( let i = 0 ; i < 2 ; i ++ ) {
306
+ const completions = await autocompleter . autocomplete ( 'ShellAPI.' ) ;
307
+ expect ( completions ) . to . deep . equal ( [ ] ) ;
308
+ }
309
+ } ) ;
310
+
288
311
describe ( 'getConnectionSchemaCode' , function ( ) {
289
312
it ( 'generates code for a connection' , async function ( ) {
290
313
const docs = [
0 commit comments