forked from microsoft/vscode-documentdb
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathShellCompletionProvider.test.ts
More file actions
895 lines (773 loc) · 40.5 KB
/
Copy pathShellCompletionProvider.test.ts
File metadata and controls
895 lines (773 loc) · 40.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
jest.mock('../ClustersClient');
jest.mock('../SchemaStore');
import { ClustersClient } from '../ClustersClient';
import { SchemaStore } from '../SchemaStore';
import { ShellCompletionProvider, type ShellCompletionContext } from './ShellCompletionProvider';
// ─── Helpers ─────────────────────────────────────────────────────────────────
const TEST_CONTEXT: ShellCompletionContext = {
clusterId: 'test-cluster',
databaseName: 'testdb',
};
function mockClustersClient(databases: Array<{ name: string }> = [], collections: Array<{ name: string }> = []): void {
const mockClient = {
getCachedDatabases: jest.fn().mockReturnValue(databases),
getCachedCollections: jest.fn().mockReturnValue(collections),
listDatabases: jest.fn().mockResolvedValue(databases),
listCollections: jest.fn().mockResolvedValue(collections),
};
(ClustersClient.getExistingClient as jest.Mock).mockReturnValue(mockClient);
}
function mockSchemaStore(
collections: Array<{ key: string; documentCount: number; fieldCount: number }> = [],
fields: Array<{ path: string; type: string; bsonType: string }> = [],
): void {
const mockStore = {
getStats: jest.fn().mockReturnValue({
collectionCount: collections.length,
totalDocuments: 0,
totalFields: 0,
collections,
}),
getKnownFields: jest.fn().mockReturnValue(fields),
};
(SchemaStore.getInstance as jest.Mock).mockReturnValue(mockStore);
}
// ─── Tests ───────────────────────────────────────────────────────────────────
describe('ShellCompletionProvider', () => {
let provider: ShellCompletionProvider;
beforeEach(() => {
provider = new ShellCompletionProvider();
jest.clearAllMocks();
// Default: empty caches
mockClustersClient();
mockSchemaStore();
});
describe('context detection', () => {
it('should detect empty buffer as top-level', () => {
const ctx = provider.detectContext('', 0);
expect(ctx.kind).toBe('top-level');
});
it('should detect whitespace-only buffer as top-level', () => {
const ctx = provider.detectContext(' ', 3);
expect(ctx.kind).toBe('top-level');
});
it('should detect partial top-level command', () => {
const ctx = provider.detectContext('sh', 2);
expect(ctx.kind).toBe('top-level');
if (ctx.kind === 'top-level') {
expect(ctx.prefix).toBe('sh');
}
});
it('should detect "show " as show-subcommand', () => {
const ctx = provider.detectContext('show ', 5);
expect(ctx.kind).toBe('show-subcommand');
});
it('should detect "show d" with prefix', () => {
const ctx = provider.detectContext('show d', 6);
expect(ctx.kind).toBe('show-subcommand');
if (ctx.kind === 'show-subcommand') {
expect(ctx.prefix).toBe('d');
}
});
it('should detect "use " as use-database', () => {
const ctx = provider.detectContext('use ', 4);
expect(ctx.kind).toBe('use-database');
});
it('should detect "use my" with prefix', () => {
const ctx = provider.detectContext('use my', 6);
expect(ctx.kind).toBe('use-database');
if (ctx.kind === 'use-database') {
expect(ctx.prefix).toBe('my');
}
});
it('should detect "db." as db-dot', () => {
const ctx = provider.detectContext('db.', 3);
expect(ctx.kind).toBe('db-dot');
if (ctx.kind === 'db-dot') {
expect(ctx.prefix).toBe('');
}
});
it('should detect "db.us" as db-dot with prefix', () => {
const ctx = provider.detectContext('db.us', 5);
expect(ctx.kind).toBe('db-dot');
if (ctx.kind === 'db-dot') {
expect(ctx.prefix).toBe('us');
}
});
it('should detect "db.users." as collection-method', () => {
const ctx = provider.detectContext('db.users.', 10);
expect(ctx.kind).toBe('collection-method');
if (ctx.kind === 'collection-method') {
expect(ctx.collectionName).toBe('users');
expect(ctx.prefix).toBe('');
}
});
it('should detect "db.users.fi" as collection-method with prefix', () => {
const ctx = provider.detectContext('db.users.fi', 12);
expect(ctx.kind).toBe('collection-method');
if (ctx.kind === 'collection-method') {
expect(ctx.collectionName).toBe('users');
expect(ctx.prefix).toBe('fi');
}
});
it('should detect method argument context', () => {
const ctx = provider.detectContext('db.users.find(', 15);
expect(ctx.kind).toBe('method-argument');
if (ctx.kind === 'method-argument') {
expect(ctx.collectionName).toBe('users');
expect(ctx.methodName).toBe('find');
}
});
it('should detect method argument with object content', () => {
const ctx = provider.detectContext('db.users.find({ ', 17);
expect(ctx.kind).toBe('method-argument');
});
it('should detect method argument when a string contains parentheses', () => {
const input = 'db.users.find({ note: ")" , na';
const ctx = provider.detectContext(input, input.length);
expect(ctx.kind).toBe('method-argument');
});
it('should detect method argument when a regex contains parentheses', () => {
const input = 'db.users.find({ name: /foo(bar)/, na';
const ctx = provider.detectContext(input, input.length);
expect(ctx.kind).toBe('method-argument');
});
});
describe('top-level completions', () => {
it('should return all top-level commands for empty buffer', () => {
const result = provider.getCompletions('', 0, TEST_CONTEXT);
expect(result.candidates.length).toBeGreaterThan(0);
const labels = result.candidates.map((c) => c.label);
expect(labels).toContain('show');
expect(labels).toContain('use');
expect(labels).toContain('db');
expect(labels).toContain('exit');
expect(labels).toContain('help');
});
it('should filter by prefix', () => {
const result = provider.getCompletions('sh', 2, TEST_CONTEXT);
expect(result.candidates.length).toBe(1);
expect(result.candidates[0].label).toBe('show');
});
it('should return empty for non-matching prefix', () => {
const result = provider.getCompletions('xyz', 3, TEST_CONTEXT);
expect(result.candidates.length).toBe(0);
});
});
describe('show subcommands', () => {
it('should return all show subcommands', () => {
const result = provider.getCompletions('show ', 5, TEST_CONTEXT);
const labels = result.candidates.map((c) => c.label);
expect(labels).toContain('dbs');
expect(labels).toContain('databases');
expect(labels).toContain('collections');
});
it('should filter by prefix', () => {
const result = provider.getCompletions('show d', 6, TEST_CONTEXT);
expect(result.candidates.length).toBe(2);
const labels = result.candidates.map((c) => c.label);
expect(labels).toContain('dbs');
expect(labels).toContain('databases');
});
});
describe('use database completions', () => {
it('should return database names from ClustersClient cache', () => {
mockClustersClient([{ name: 'admin' }, { name: 'mydb' }, { name: 'testdb' }]);
const result = provider.getCompletions('use ', 4, TEST_CONTEXT);
const labels = result.candidates.map((c) => c.label);
expect(labels).toContain('admin');
expect(labels).toContain('mydb');
expect(labels).toContain('testdb');
});
it('should merge database names from SchemaStore', () => {
mockClustersClient([{ name: 'admin' }]);
mockSchemaStore([{ key: 'test-cluster::otherdb::coll1', documentCount: 10, fieldCount: 5 }]);
const result = provider.getCompletions('use ', 4, TEST_CONTEXT);
const labels = result.candidates.map((c) => c.label);
expect(labels).toContain('admin');
expect(labels).toContain('otherdb');
});
it('should filter by prefix', () => {
mockClustersClient([{ name: 'admin' }, { name: 'mydb' }, { name: 'testdb' }]);
const result = provider.getCompletions('use my', 6, TEST_CONTEXT);
expect(result.candidates.length).toBe(1);
expect(result.candidates[0].label).toBe('mydb');
});
});
describe('db. completions', () => {
it('should return collection names from ClustersClient', () => {
mockClustersClient([], [{ name: 'users' }, { name: 'orders' }]);
const result = provider.getCompletions('db.', 3, TEST_CONTEXT);
const labels = result.candidates.map((c) => c.label);
expect(labels).toContain('users');
expect(labels).toContain('orders');
});
it('should include database methods', () => {
const result = provider.getCompletions('db.', 3, TEST_CONTEXT);
const labels = result.candidates.map((c) => c.label);
expect(labels).toContain('getCollection');
expect(labels).toContain('createCollection');
});
it('should merge collection names from SchemaStore', () => {
mockClustersClient([], [{ name: 'users' }]);
mockSchemaStore([{ key: 'test-cluster::testdb::logs', documentCount: 10, fieldCount: 5 }]);
const result = provider.getCompletions('db.', 3, TEST_CONTEXT);
const labels = result.candidates.map((c) => c.label);
expect(labels).toContain('users');
expect(labels).toContain('logs');
});
it('should filter by prefix', () => {
mockClustersClient([], [{ name: 'users' }, { name: 'orders' }, { name: 'uploads' }]);
const result = provider.getCompletions('db.us', 5, TEST_CONTEXT);
const labels = result.candidates.map((c) => c.label);
expect(labels).toContain('users');
expect(labels).not.toContain('orders');
});
});
describe('collection method completions', () => {
it('should return collection methods for db.<collection>.', () => {
const result = provider.getCompletions('db.users.', 10, TEST_CONTEXT);
const labels = result.candidates.map((c) => c.label);
expect(labels).toContain('find');
expect(labels).toContain('findOne');
expect(labels).toContain('insertOne');
expect(labels).toContain('updateOne');
expect(labels).toContain('deleteOne');
expect(labels).toContain('aggregate');
});
it('should filter by prefix', () => {
const result = provider.getCompletions('db.users.fi', 12, TEST_CONTEXT);
const labels = result.candidates.map((c) => c.label);
expect(labels).toContain('find');
expect(labels).toContain('findOne');
expect(labels).not.toContain('insertOne');
});
});
describe('method argument completions', () => {
it('should return field names from schema inside find()', () => {
mockSchemaStore(
[],
[
{ path: 'name', type: 'string', bsonType: 'string' },
{ path: 'age', type: 'number', bsonType: 'int32' },
{ path: '_id', type: 'string', bsonType: 'objectId' },
],
);
const result = provider.getCompletions('db.users.find({ ', 17, TEST_CONTEXT);
const labels = result.candidates.map((c) => c.label);
expect(labels).toContain('name');
expect(labels).toContain('age');
expect(labels).toContain('_id');
});
it('should not return operators inside find() without $ prefix', () => {
const result = provider.getCompletions('db.users.find({ ', 17, TEST_CONTEXT);
const operatorCandidates = result.candidates.filter((c) => c.kind === 'operator');
expect(operatorCandidates.length).toBe(0);
});
it('should return operators inside find() when $ is typed', () => {
const result = provider.getCompletions('db.users.find({ $', 18, TEST_CONTEXT);
const operatorCandidates = result.candidates.filter((c) => c.kind === 'operator');
expect(operatorCandidates.length).toBeGreaterThan(0);
});
});
describe('cursor chain completions', () => {
it('should detect cursor chain after find()', () => {
const input = 'db.users.find({}).';
const ctx = provider.detectContext(input, input.length);
expect(ctx.kind).toBe('cursor-chain');
if (ctx.kind === 'cursor-chain') {
expect(ctx.cursorType).toBe('find');
expect(ctx.prefix).toBe('');
}
});
it('should return find cursor methods after find().', () => {
const input = 'db.users.find({}).';
const result = provider.getCompletions(input, input.length, TEST_CONTEXT);
const labels = result.candidates.map((c) => c.label);
expect(labels).toContain('limit');
expect(labels).toContain('skip');
expect(labels).toContain('sort');
expect(labels).toContain('toArray');
});
it('should filter cursor methods by prefix', () => {
const input = 'db.users.find({}).li';
const result = provider.getCompletions(input, input.length, TEST_CONTEXT);
const labels = result.candidates.map((c) => c.label);
expect(labels).toContain('limit');
expect(labels).not.toContain('skip');
});
it('should detect cursor chain after chained calls', () => {
const input = 'db.users.find({}).limit(10).';
const ctx = provider.detectContext(input, input.length);
expect(ctx.kind).toBe('cursor-chain');
});
it('should return cursor methods after chained calls', () => {
const input = 'db.users.find({}).limit(10).';
const result = provider.getCompletions(input, input.length, TEST_CONTEXT);
const labels = result.candidates.map((c) => c.label);
expect(labels).toContain('sort');
expect(labels).toContain('toArray');
});
it('should detect aggregate cursor chain', () => {
const input = 'db.users.aggregate([]).';
const ctx = provider.detectContext(input, input.length);
expect(ctx.kind).toBe('cursor-chain');
if (ctx.kind === 'cursor-chain') {
expect(ctx.cursorType).toBe('aggregate');
}
});
});
describe('kind differentiation', () => {
it('should mark db. collection candidates as kind "collection"', () => {
mockClustersClient([], [{ name: 'users' }, { name: 'orders' }]);
const result = provider.getCompletions('db.', 3, TEST_CONTEXT);
const collections = result.candidates.filter((c) => c.kind === 'collection');
const methods = result.candidates.filter((c) => c.kind === 'method');
expect(collections.length).toBeGreaterThan(0);
expect(methods.length).toBeGreaterThan(0);
});
it('should mark collection methods as kind "method"', () => {
const result = provider.getCompletions('db.users.', 10, TEST_CONTEXT);
const allMethods = result.candidates.every((c) => c.kind === 'method');
expect(allMethods).toBe(true);
});
});
// ─── Operator vs field separation (Option C) ─────────────────────────────
describe('operator/field separation at key position', () => {
beforeEach(() => {
mockSchemaStore(
[],
[
{ path: 'name', type: 'string', bsonType: 'string' },
{ path: 'age', type: 'number', bsonType: 'int32' },
{ path: 'status', type: 'string', bsonType: 'string' },
],
);
});
it('should show only fields (no operators) at key position with empty prefix', () => {
const result = provider.getCompletions('db.users.find({ ', 17, TEST_CONTEXT);
const fields = result.candidates.filter((c) => c.kind === 'field');
const operators = result.candidates.filter((c) => c.kind === 'operator');
expect(fields.length).toBe(3);
expect(operators.length).toBe(0);
});
it('should show only fields when prefix is a plain identifier', () => {
const input = 'db.users.find({ na';
const result = provider.getCompletions(input, input.length, TEST_CONTEXT);
const labels = result.candidates.map((c) => c.label);
expect(labels).toContain('name');
expect(labels).not.toContain('$ne');
});
it('should show only operators when prefix starts with $', () => {
const input = 'db.users.find({ $';
const result = provider.getCompletions(input, input.length, TEST_CONTEXT);
const fields = result.candidates.filter((c) => c.kind === 'field');
const operators = result.candidates.filter((c) => c.kind === 'operator');
expect(fields.length).toBe(0);
expect(operators.length).toBeGreaterThan(0);
});
it('should filter operators by prefix after $', () => {
const input = 'db.users.find({ $g';
const result = provider.getCompletions(input, input.length, TEST_CONTEXT);
const labels = result.candidates.map((c) => c.label);
expect(labels).toContain('$gt');
expect(labels).toContain('$gte');
expect(labels).not.toContain('$lt');
});
it('should show operators at value position (after colon)', () => {
const input = 'db.users.find({ age: { $';
const result = provider.getCompletions(input, input.length, TEST_CONTEXT);
const operators = result.candidates.filter((c) => c.kind === 'operator');
expect(operators.length).toBeGreaterThan(0);
});
it('should show operators at operator position', () => {
const input = 'db.users.find({ age: { $gt: 5, $';
const result = provider.getCompletions(input, input.length, TEST_CONTEXT);
const operators = result.candidates.filter((c) => c.kind === 'operator');
expect(operators.length).toBeGreaterThan(0);
});
it('should show only fields after comma at key position', () => {
const input = 'db.users.find({ name: "alice", ';
const result = provider.getCompletions(input, input.length, TEST_CONTEXT);
const fields = result.candidates.filter((c) => c.kind === 'field');
const operators = result.candidates.filter((c) => c.kind === 'operator');
expect(fields.length).toBe(3);
expect(operators.length).toBe(0);
});
it('should show only operators after comma when $ typed', () => {
const input = 'db.users.find({ name: "alice", $';
const result = provider.getCompletions(input, input.length, TEST_CONTEXT);
const fields = result.candidates.filter((c) => c.kind === 'field');
const operators = result.candidates.filter((c) => c.kind === 'operator');
expect(fields.length).toBe(0);
expect(operators.length).toBeGreaterThan(0);
});
});
// ─── Nested field prefix extraction ──────────────────────────────────────
describe('nested field path completions', () => {
beforeEach(() => {
mockSchemaStore(
[],
[
{ path: 'name', type: 'string', bsonType: 'string' },
{ path: 'address', type: 'object', bsonType: 'object' },
{ path: 'address.city', type: 'string', bsonType: 'string' },
{ path: 'address.state', type: 'string', bsonType: 'string' },
{ path: 'address.zip', type: 'string', bsonType: 'string' },
{ path: 'toplevel', type: 'object', bsonType: 'object' },
{ path: 'toplevel.isEnabled', type: 'boolean', bsonType: 'bool' },
{ path: 'toplevel.isActive', type: 'boolean', bsonType: 'bool' },
{ path: 'toplevel.itemCount', type: 'number', bsonType: 'int32' },
{ path: 'settings.theme.color', type: 'string', bsonType: 'string' },
],
);
});
it('should match nested field by dotted prefix', () => {
const input = 'db.users.find({ address.ci';
const result = provider.getCompletions(input, input.length, TEST_CONTEXT);
const labels = result.candidates.map((c) => c.label);
expect(labels).toContain('address.city');
expect(labels).not.toContain('address.state');
});
it('should match all sub-fields of a parent path', () => {
const input = 'db.users.find({ address.';
const result = provider.getCompletions(input, input.length, TEST_CONTEXT);
const labels = result.candidates.map((c) => c.label);
expect(labels).toContain('address.city');
expect(labels).toContain('address.state');
expect(labels).toContain('address.zip');
expect(labels).not.toContain('name');
});
it('should not match ISODate for toplevel.is prefix', () => {
const input = 'db.users.find({ toplevel.is';
const result = provider.getCompletions(input, input.length, TEST_CONTEXT);
const labels = result.candidates.map((c) => c.label);
expect(labels).toContain('toplevel.isEnabled');
expect(labels).toContain('toplevel.isActive');
expect(labels).not.toContain('toplevel.itemCount');
// Must NOT match BSON constructors
const bsonCandidates = result.candidates.filter((c) => c.kind === 'bson');
expect(bsonCandidates.length).toBe(0);
});
it('should not match unrelated top-level fields for nested prefix', () => {
const input = 'db.users.find({ toplevel.';
const result = provider.getCompletions(input, input.length, TEST_CONTEXT);
const labels = result.candidates.map((c) => c.label);
expect(labels).toContain('toplevel.isEnabled');
expect(labels).toContain('toplevel.isActive');
expect(labels).toContain('toplevel.itemCount');
expect(labels).not.toContain('name');
expect(labels).not.toContain('address');
});
it('should match deeply nested fields', () => {
const input = 'db.users.find({ settings.theme.';
const result = provider.getCompletions(input, input.length, TEST_CONTEXT);
const labels = result.candidates.map((c) => c.label);
expect(labels).toContain('settings.theme.color');
});
it('should match full dotted path with partial last segment', () => {
const input = 'db.users.find({ settings.theme.co';
const result = provider.getCompletions(input, input.length, TEST_CONTEXT);
const labels = result.candidates.map((c) => c.label);
expect(labels).toContain('settings.theme.color');
});
it('should return all fields when no prefix is typed', () => {
const input = 'db.users.find({ ';
const result = provider.getCompletions(input, input.length, TEST_CONTEXT);
const fields = result.candidates.filter((c) => c.kind === 'field');
expect(fields.length).toBe(10); // all fields from mock
});
it('should handle prefix that matches top-level and nested', () => {
const input = 'db.users.find({ top';
const result = provider.getCompletions(input, input.length, TEST_CONTEXT);
const labels = result.candidates.map((c) => c.label);
expect(labels).toContain('toplevel');
expect(labels).toContain('toplevel.isEnabled');
expect(labels).toContain('toplevel.isActive');
expect(labels).toContain('toplevel.itemCount');
});
it('should produce correct replacementStart for dotted prefix', () => {
const input = 'db.users.find({ address.ci';
const result = provider.getCompletions(input, input.length, TEST_CONTEXT);
// replacementStart should point to 'a' in 'address.ci'
expect(result.prefix).toBe('address.ci');
// The replacement should cover the entire dotted path
const expectedStart = input.indexOf('address.ci');
expect(result.replacementStart).toBe(expectedStart);
});
it('should not break $ operator prefix with dot inclusion', () => {
const input = 'db.users.find({ $g';
const result = provider.getCompletions(input, input.length, TEST_CONTEXT);
expect(result.prefix).toBe('$g');
const labels = result.candidates.map((c) => c.label);
expect(labels).toContain('$gt');
expect(labels).toContain('$gte');
});
it('should not break simple field prefix', () => {
const input = 'db.users.find({ na';
const result = provider.getCompletions(input, input.length, TEST_CONTEXT);
expect(result.prefix).toBe('na');
const labels = result.candidates.map((c) => c.label);
expect(labels).toContain('name');
});
});
// ─── Dotted field quoting ────────────────────────────────────────────────
describe('dotted field quoting', () => {
beforeEach(() => {
mockSchemaStore(
[],
[
{ path: 'name', type: 'string', bsonType: 'string' },
{ path: 'address', type: 'object', bsonType: 'object' },
{ path: 'address.city', type: 'string', bsonType: 'string' },
{ path: 'address.state', type: 'string', bsonType: 'string' },
{ path: 'toplevel.isEnabled', type: 'boolean', bsonType: 'bool' },
{ path: 'settings.theme.color', type: 'string', bsonType: 'string' },
],
);
});
it('should quote dotted field paths in insertText', () => {
const input = 'db.users.find({ ';
const result = provider.getCompletions(input, input.length, TEST_CONTEXT);
const addressCity = result.candidates.find((c) => c.label === 'address.city');
expect(addressCity).toBeDefined();
expect(addressCity!.insertText).toBe('"address.city"');
});
it('should not quote simple field paths', () => {
const input = 'db.users.find({ ';
const result = provider.getCompletions(input, input.length, TEST_CONTEXT);
const name = result.candidates.find((c) => c.label === 'name');
expect(name).toBeDefined();
expect(name!.insertText).toBe('name');
});
it('should quote deeply nested field paths', () => {
const input = 'db.users.find({ ';
const result = provider.getCompletions(input, input.length, TEST_CONTEXT);
const deep = result.candidates.find((c) => c.label === 'settings.theme.color');
expect(deep).toBeDefined();
expect(deep!.insertText).toBe('"settings.theme.color"');
});
it('should filter quoted candidates by dotted prefix', () => {
const input = 'db.users.find({ address.ci';
const result = provider.getCompletions(input, input.length, TEST_CONTEXT);
expect(result.candidates.length).toBe(1);
expect(result.candidates[0].label).toBe('address.city');
expect(result.candidates[0].insertText).toBe('"address.city"');
});
it('should filter by label not insertText', () => {
// Prefix 'address.' should match fields with dotted label, not the quoted insertText
const input = 'db.users.find({ address.';
const result = provider.getCompletions(input, input.length, TEST_CONTEXT);
const labels = result.candidates.map((c) => c.label);
expect(labels).toContain('address.city');
expect(labels).toContain('address.state');
expect(labels).not.toContain('name');
});
});
// ─── Bracket notation completions ────────────────────────────────────────
describe('db[ bracket notation completions', () => {
beforeEach(() => {
mockClustersClient([], [{ name: 'restaurants' }, { name: 'orders' }, { name: 'users' }]);
});
it('should detect "db[" as db-bracket context with no quote', () => {
const ctx = provider.detectContext('db[', 3);
expect(ctx.kind).toBe('db-bracket');
if (ctx.kind === 'db-bracket') {
expect(ctx.prefix).toBe('');
expect(ctx.quote).toBe('');
}
});
it('should detect "db[\'" as db-bracket context with single quote', () => {
const ctx = provider.detectContext("db['", 4);
expect(ctx.kind).toBe('db-bracket');
if (ctx.kind === 'db-bracket') {
expect(ctx.prefix).toBe('');
expect(ctx.quote).toBe("'");
}
});
it("should detect 'db[\"' as db-bracket context with double quote", () => {
const ctx = provider.detectContext('db["', 4);
expect(ctx.kind).toBe('db-bracket');
if (ctx.kind === 'db-bracket') {
expect(ctx.prefix).toBe('');
expect(ctx.quote).toBe('"');
}
});
it('should detect "db[\'res" as db-bracket with prefix', () => {
const ctx = provider.detectContext("db['res", 7);
expect(ctx.kind).toBe('db-bracket');
if (ctx.kind === 'db-bracket') {
expect(ctx.prefix).toBe('res');
expect(ctx.quote).toBe("'");
}
});
it('should return collection names for "db["', () => {
const result = provider.getCompletions('db["', 4, TEST_CONTEXT);
const labels = result.candidates.map((c) => c.label);
expect(labels).toContain('restaurants');
expect(labels).toContain('orders');
expect(labels).toContain('users');
});
it('should not include database methods for bracket notation', () => {
const result = provider.getCompletions('db["', 4, TEST_CONTEXT);
const labels = result.candidates.map((c) => c.label);
expect(labels).not.toContain('getCollection');
expect(labels).not.toContain('createCollection');
});
it("should wrap insertText with closing quote and bracket for db['", () => {
const result = provider.getCompletions("db['", 4, TEST_CONTEXT);
const restaurants = result.candidates.find((c) => c.label === 'restaurants');
expect(restaurants).toBeDefined();
expect(restaurants!.insertText).toBe("restaurants']");
});
it('should wrap insertText with closing double quote and bracket for db["', () => {
const result = provider.getCompletions('db["', 4, TEST_CONTEXT);
const restaurants = result.candidates.find((c) => c.label === 'restaurants');
expect(restaurants).toBeDefined();
expect(restaurants!.insertText).toBe('restaurants"]');
});
it('should add opening quote when no quote typed (db[)', () => {
const result = provider.getCompletions('db[', 3, TEST_CONTEXT);
const restaurants = result.candidates.find((c) => c.label === 'restaurants');
expect(restaurants).toBeDefined();
expect(restaurants!.insertText).toBe("'restaurants']");
});
it("should filter by prefix for db['res", () => {
const result = provider.getCompletions("db['res", 7, TEST_CONTEXT);
expect(result.candidates.length).toBe(1);
expect(result.candidates[0].label).toBe('restaurants');
expect(result.candidates[0].insertText).toBe("restaurants']");
});
it('should return empty for non-matching prefix', () => {
const result = provider.getCompletions("db['xyz", 7, TEST_CONTEXT);
expect(result.candidates.length).toBe(0);
});
});
// ─── Value-position operator filtering ───────────────────────────────────
describe('value-position operator filtering', () => {
beforeEach(() => {
mockSchemaStore(
[],
[
{ path: '_id', type: 'objectId', bsonType: 'objectId' },
{ path: 'name', type: 'string', bsonType: 'string' },
{ path: 'age', type: 'number', bsonType: 'int32' },
],
);
});
it('should not show query operators in value position', () => {
const input = 'db.restaurants.find({_id: $';
const result = provider.getCompletions(input, input.length, TEST_CONTEXT);
const operators = result.candidates.filter((c) => c.kind === 'operator');
expect(operators.length).toBe(0);
});
it('should show BSON constructors in value position', () => {
const input = 'db.restaurants.find({_id: ';
const result = provider.getCompletions(input, input.length, TEST_CONTEXT);
const bson = result.candidates.filter((c) => c.kind === 'bson');
expect(bson.length).toBeGreaterThan(0);
});
it('should still show operators at operator position (inside nested object)', () => {
const input = 'db.restaurants.find({age: { $';
const result = provider.getCompletions(input, input.length, TEST_CONTEXT);
const operators = result.candidates.filter((c) => c.kind === 'operator');
expect(operators.length).toBeGreaterThan(0);
});
});
// ─── Bracket notation method completions ─────────────────────────────────
describe("db['collection']. method completions", () => {
it("should detect db['restaurants']. as collection-method", () => {
const input = "db['restaurants'].";
const ctx = provider.detectContext(input, input.length);
expect(ctx.kind).toBe('collection-method');
if (ctx.kind === 'collection-method') {
expect(ctx.collectionName).toBe('restaurants');
expect(ctx.prefix).toBe('');
}
});
it('should detect db["restaurants"]. as collection-method', () => {
const input = 'db["restaurants"].';
const ctx = provider.detectContext(input, input.length);
expect(ctx.kind).toBe('collection-method');
if (ctx.kind === 'collection-method') {
expect(ctx.collectionName).toBe('restaurants');
expect(ctx.prefix).toBe('');
}
});
it("should detect db['restaurants'].fi as collection-method with prefix", () => {
const input = "db['restaurants'].fi";
const ctx = provider.detectContext(input, input.length);
expect(ctx.kind).toBe('collection-method');
if (ctx.kind === 'collection-method') {
expect(ctx.collectionName).toBe('restaurants');
expect(ctx.prefix).toBe('fi');
}
});
it("should return collection methods for db['restaurants'].", () => {
const input = "db['restaurants'].";
const result = provider.getCompletions(input, input.length, TEST_CONTEXT);
const labels = result.candidates.map((c) => c.label);
expect(labels).toContain('find');
expect(labels).toContain('insertOne');
expect(labels).toContain('aggregate');
});
it("should detect method argument for db['restaurants'].find(", () => {
const input = "db['restaurants'].find(";
const ctx = provider.detectContext(input, input.length);
expect(ctx.kind).toBe('method-argument');
if (ctx.kind === 'method-argument') {
expect(ctx.collectionName).toBe('restaurants');
expect(ctx.methodName).toBe('find');
}
});
it("should handle special-char collection names: db['stores (10)'].", () => {
const input = "db['stores (10)'].";
const ctx = provider.detectContext(input, input.length);
expect(ctx.kind).toBe('collection-method');
if (ctx.kind === 'collection-method') {
expect(ctx.collectionName).toBe('stores (10)');
}
});
});
// ─── Special-char collection bracket notation switch ─────────────────────
describe('special-char collection auto-switch to bracket notation', () => {
beforeEach(() => {
mockClustersClient([], [{ name: 'restaurants' }, { name: 'stores (10)' }, { name: 'my-collection' }]);
});
it('should use dot notation insertText for normal collection names', () => {
const result = provider.getCompletions('db.', 3, TEST_CONTEXT);
const restaurants = result.candidates.find((c) => c.label === 'restaurants');
expect(restaurants).toBeDefined();
expect(restaurants!.insertText).toBe('restaurants');
});
it('should use bracket notation insertText for special-char names', () => {
const result = provider.getCompletions('db.', 3, TEST_CONTEXT);
const stores = result.candidates.find((c) => c.label === 'stores (10)');
expect(stores).toBeDefined();
expect(stores!.insertText).toBe("['stores (10)']");
});
it('should use bracket notation for hyphenated names', () => {
const result = provider.getCompletions('db.', 3, TEST_CONTEXT);
const coll = result.candidates.find((c) => c.label === 'my-collection');
expect(coll).toBeDefined();
expect(coll!.insertText).toBe("['my-collection']");
});
it('should filter bracket-notation candidates by label prefix', () => {
const result = provider.getCompletions('db.sto', 6, TEST_CONTEXT);
expect(result.candidates.length).toBe(1);
expect(result.candidates[0].label).toBe('stores (10)');
expect(result.candidates[0].insertText).toBe("['stores (10)']");
});
it('should escape backslashes and single quotes in bracket notation insertText', () => {
mockClustersClient([], [{ name: "a\\b'c" }]);
const result = provider.getCompletions('db.', 3, TEST_CONTEXT);
const escaped = result.candidates.find((c) => c.label === "a\\b'c");
expect(escaped).toBeDefined();
expect(escaped!.insertText).toBe("['a\\\\b\\'c']");
});
});
});