-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautocomplete.test.ts
More file actions
3028 lines (2766 loc) · 113 KB
/
autocomplete.test.ts
File metadata and controls
3028 lines (2766 loc) · 113 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
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { describe, it, expect } from "vitest"
import {
createAutocompleteProvider,
SuggestionKind,
SuggestionPriority,
} from "../src/index"
import type {
AutocompleteProvider,
Suggestion,
} from "../src/autocomplete/types"
// =============================================================================
// Test Utility: Autocomplete Walkthrough
// =============================================================================
// Tests that autocomplete provides relevant suggestions at each word boundary
// in a SQL statement. This ensures the parser is responsive and provides
// correct context-aware suggestions as the user types.
interface WalkthroughStep {
/** The partial SQL up to this point (what user has typed so far) */
typed: string
/** Token names or keyword labels expected in suggestions */
expects: string[]
/** Token names or keyword labels that should NOT appear */
rejects?: string[]
}
/**
* Assert autocomplete suggestions at each step of typing a SQL statement.
* Each step represents a word boundary where the user pauses and expects suggestions.
*/
function assertSuggestionsWalkthrough(
provider: AutocompleteProvider,
steps: WalkthroughStep[],
) {
for (const step of steps) {
const suggestions = provider.getSuggestions(step.typed, step.typed.length)
const labels = suggestions.map((s) => s.label)
for (const expected of step.expects) {
expect(labels).toContain(expected)
}
if (step.rejects) {
for (const rejected of step.rejects) {
expect(labels).not.toContain(rejected)
}
}
}
}
/**
* Helper: get suggestion labels at a given position
*/
function getLabelsAt(
provider: AutocompleteProvider,
sql: string,
offset?: number,
): string[] {
return provider.getSuggestions(sql, offset ?? sql.length).map((s) => s.label)
}
/**
* Helper: get suggestions of a specific kind at a given position
*/
function getSuggestionsOfKind(
provider: AutocompleteProvider,
sql: string,
kind: SuggestionKind,
offset?: number,
): Suggestion[] {
return provider
.getSuggestions(sql, offset ?? sql.length)
.filter((s) => s.kind === kind)
}
// =============================================================================
// Test Schema
// =============================================================================
const schema = {
tables: [
{ name: "trades", designatedTimestamp: "timestamp" },
{ name: "orders" },
{ name: "users" },
],
columns: {
trades: [
{ name: "symbol", type: "STRING" },
{ name: "price", type: "DOUBLE" },
{ name: "amount", type: "DOUBLE" },
{ name: "timestamp", type: "TIMESTAMP" },
],
orders: [
{ name: "id", type: "LONG" },
{ name: "status", type: "STRING" },
],
users: [
{ name: "name", type: "STRING" },
{ name: "email", type: "STRING" },
],
},
}
const provider = createAutocompleteProvider(schema)
// =============================================================================
// Tests
// =============================================================================
describe("createAutocompleteProvider", () => {
describe("keyword suggestions", () => {
it("suggests TABLE after CREATE", () => {
const labels = getLabelsAt(provider, "CREATE ")
expect(labels).toContain("TABLE")
expect(labels).not.toContain("SELECT")
})
it("suggests valid actions after ALTER TABLE tableName", () => {
const labels = getLabelsAt(provider, "ALTER TABLE trades ")
expect(labels).toContain("ADD")
expect(labels).toContain("DROP")
expect(labels).toContain("RENAME")
})
it("suggests FROM after SELECT * and does NOT suggest columns", () => {
const suggestions = provider.getSuggestions("SELECT * ", 9)
const labels = suggestions.map((s) => s.label)
expect(labels).toContain("FROM")
// After SELECT *, all columns are already selected — suggesting
// column names makes no sense. Only keywords should appear.
const columns = suggestions.filter(
(s) => s.kind === SuggestionKind.Column,
)
expect(columns).toHaveLength(0)
})
it("suggests FROM when typing 'FR' after SELECT * (mid-word)", () => {
const suggestions = provider.getSuggestions("SELECT * FR", 11)
const labels = suggestions.map((s) => s.label)
expect(labels).toContain("FROM")
// No columns, tables, or functions should appear — only keywords
const nonKeywords = suggestions.filter(
(s) => s.kind !== SuggestionKind.Keyword,
)
expect(nonKeywords).toHaveLength(0)
})
it("suggests BY after ORDER", () => {
const labels = getLabelsAt(provider, "SELECT * FROM trades ORDER ")
expect(labels).toContain("BY")
})
})
describe("column suggestions", () => {
it("suggests columns after SELECT (with FROM clause)", () => {
const columns = getSuggestionsOfKind(
provider,
"SELECT FROM trades",
SuggestionKind.Column,
7,
)
expect(columns.length).toBeGreaterThan(0)
const columnLabels = columns.map((s) => s.label)
expect(columnLabels).toContain("symbol")
expect(columnLabels).toContain("price")
expect(columnLabels).toContain("timestamp")
expect(columns.every((s) => s.priority === SuggestionPriority.High)).toBe(
true,
)
})
it("suggests columns in WHERE clause", () => {
const columns = getSuggestionsOfKind(
provider,
"SELECT * FROM trades WHERE ",
SuggestionKind.Column,
)
expect(columns.length).toBeGreaterThan(0)
expect(columns.map((s) => s.label)).toContain("symbol")
})
})
describe("table suggestions", () => {
it("suggests tables after FROM", () => {
const tables = getSuggestionsOfKind(
provider,
"SELECT * FROM ",
SuggestionKind.Table,
)
expect(tables.length).toBeGreaterThan(0)
const tableLabels = tables.map((s) => s.label)
expect(tableLabels).toContain("trades")
expect(tableLabels).toContain("orders")
})
it("suggests tables after JOIN", () => {
const tables = getSuggestionsOfKind(
provider,
"SELECT * FROM trades JOIN ",
SuggestionKind.Table,
)
expect(tables.length).toBeGreaterThan(0)
})
it("suggests tables after FROM when select items are missing", () => {
const tables = getSuggestionsOfKind(
provider,
"SELECT FROM ",
SuggestionKind.Table,
)
expect(tables.length).toBe(3)
const tableLabels = tables.map((s) => s.label).sort()
expect(tableLabels).toContain("trades")
expect(tableLabels).toContain("orders")
expect(tableLabels).toContain("users")
})
it("suggests tables after FROM when typing partial table name without select items", () => {
const tables = getSuggestionsOfKind(
provider,
"SELECT FROM t",
SuggestionKind.Table,
)
expect(tables.length).toBe(3)
})
it("should NOT suggest the partial word itself as a table (phantom suggestion)", () => {
// Typing "FROM te" — "te" is not a real table but gets captured by
// extractTables because it follows FROM. It must not be suggested back.
const tables = getSuggestionsOfKind(
provider,
"SELECT amount FROM te",
SuggestionKind.Table,
)
const labels = tables.map((s) => s.label)
expect(labels).not.toContain("te")
// Real schema tables should still be present
expect(labels).toContain("trades")
expect(labels).toContain("orders")
expect(labels).toContain("users")
})
it("should NOT suggest longer partial words as tables", () => {
const tables = getSuggestionsOfKind(
provider,
"SELECT amount FROM tes",
SuggestionKind.Table,
)
const labels = tables.map((s) => s.label)
expect(labels).not.toContain("tes")
})
it("should NOT suggest partial word after JOIN", () => {
const tables = getSuggestionsOfKind(
provider,
"SELECT * FROM trades JOIN or",
SuggestionKind.Table,
)
const labels = tables.map((s) => s.label)
expect(labels).not.toContain("or")
expect(labels).toContain("orders")
})
it("should still suggest real table when partial matches schema table", () => {
// Typing "trades" mid-word — it IS a real table, should be kept
const tables = getSuggestionsOfKind(
provider,
"SELECT * FROM trades",
SuggestionKind.Table,
)
const labels = tables.map((s) => s.label)
expect(labels).toContain("trades")
})
it("should still suggest CTE name when partial matches CTE", () => {
const sql =
"WITH my_cte AS (SELECT symbol FROM trades) SELECT * FROM my_cte"
const tables = getSuggestionsOfKind(provider, sql, SuggestionKind.Table)
const labels = tables.map((s) => s.label)
expect(labels).toContain("my_cte")
})
})
describe("suggestion details", () => {
it("includes column details with table and type", () => {
const suggestions = provider.getSuggestions("SELECT FROM trades", 7)
const symbolSuggestion = suggestions.find((s) => s.label === "symbol")
expect(symbolSuggestion).toBeDefined()
expect(symbolSuggestion?.detail).toContain("trades")
expect(symbolSuggestion?.description).toBe("STRING")
})
})
describe("priority ordering", () => {
it("columns have higher priority than tables", () => {
const suggestions = provider.getSuggestions("SELECT FROM trades", 7)
const columnSuggestion = suggestions.find(
(s) => s.kind === SuggestionKind.Column,
)
const tableSuggestion = suggestions.find(
(s) => s.kind === SuggestionKind.Table,
)
expect(columnSuggestion).toBeDefined()
expect(tableSuggestion).toBeDefined()
expect(columnSuggestion!.priority).toBeLessThan(tableSuggestion!.priority)
})
})
})
// =============================================================================
// Autocomplete Walkthrough Tests
// =============================================================================
describe("Autocomplete walkthrough", () => {
describe("SELECT * FROM trades WHERE price > 100", () => {
it("should provide correct suggestions at each word boundary", () => {
assertSuggestionsWalkthrough(provider, [
{
typed: "SELECT ",
expects: ["DISTINCT", "CASE", "CAST"],
},
{
typed: "SELECT * ",
expects: ["FROM"],
},
{
typed: "SELECT * FROM ",
expects: ["trades", "orders", "users"],
},
{
typed: "SELECT * FROM trades ",
expects: ["WHERE", "ORDER", "GROUP", "LIMIT", "JOIN"],
},
{
typed: "SELECT * FROM trades WHERE ",
expects: ["symbol", "price", "amount", "timestamp"],
},
])
})
})
describe("SELECT symbol, price FROM trades ORDER BY price DESC", () => {
it("should provide correct suggestions at each word boundary", () => {
assertSuggestionsWalkthrough(provider, [
{
typed: "SELECT ",
expects: ["CASE", "CAST"],
},
{
typed: "SELECT symbol, ",
expects: ["CASE", "CAST"],
},
{
typed: "SELECT symbol, price ",
expects: ["FROM", "AS"],
},
{
typed: "SELECT symbol, price FROM ",
expects: ["trades", "orders"],
},
{
typed: "SELECT symbol, price FROM trades ",
expects: ["WHERE", "ORDER", "GROUP", "JOIN"],
},
{
typed: "SELECT symbol, price FROM trades ORDER ",
expects: ["BY"],
},
])
})
})
describe("INSERT INTO trades (symbol, price) VALUES ('BTC', 100)", () => {
it("should provide correct suggestions at each word boundary", () => {
assertSuggestionsWalkthrough(provider, [
{
typed: "INSERT ",
expects: ["INTO"],
},
{
typed: "INSERT INTO ",
expects: ["trades", "orders"],
},
])
})
})
describe("CREATE TABLE new_table (id INT, name STRING) TIMESTAMP(id) PARTITION BY DAY", () => {
it("should provide correct suggestions at each word boundary", () => {
assertSuggestionsWalkthrough(provider, [
{
typed: "CREATE ",
expects: ["TABLE"],
},
{
typed:
"CREATE TABLE new_table (id INT, name STRING) TIMESTAMP(id) PARTITION ",
expects: ["BY"],
},
])
})
})
// Expression-related walkthroughs
describe("SELECT * FROM trades WHERE symbol ILIKE '%btc%'", () => {
it("should suggest ILIKE and LIKE after column name in WHERE", () => {
assertSuggestionsWalkthrough(provider, [
{
typed: "SELECT * FROM trades WHERE symbol ",
expects: ["LIKE", "ILIKE", "IN", "BETWEEN", "IS"],
},
])
})
})
describe("SELECT * FROM trades WHERE price BETWEEN 100 AND 200", () => {
it("should suggest BETWEEN and IN after column in WHERE", () => {
assertSuggestionsWalkthrough(provider, [
{
typed: "SELECT * FROM trades WHERE price ",
expects: ["BETWEEN", "IN", "LIKE", "ILIKE", "IS"],
},
])
})
})
})
// =============================================================================
// Documentation Example Tests
// =============================================================================
describe("Documentation examples - autocomplete", () => {
// From /query/operators/text.md
describe("text operators", () => {
it("should suggest operators after string literal", () => {
// SELECT 'a' || 'b' - after 'a' we should get valid next tokens
const labels = getLabelsAt(provider, "SELECT 'a' ")
expect(labels).toContain("FROM")
expect(labels).toContain("AS")
})
it("should suggest LIKE and ILIKE in WHERE context", () => {
const labels = getLabelsAt(provider, "SELECT * FROM trades WHERE symbol ")
expect(labels).toContain("LIKE")
expect(labels).toContain("ILIKE")
})
})
// From /query/sql/where.md
describe("WHERE clause operators", () => {
it("should suggest IS after column name", () => {
const labels = getLabelsAt(provider, "SELECT * FROM trades WHERE price ")
expect(labels).toContain("IS")
})
it("should suggest NOT after IS", () => {
const labels = getLabelsAt(
provider,
"SELECT * FROM trades WHERE price IS ",
)
expect(labels).toContain("NOT")
})
it("should suggest IN after column name", () => {
const labels = getLabelsAt(provider, "SELECT * FROM trades WHERE symbol ")
expect(labels).toContain("IN")
})
it("should suggest BETWEEN after column name", () => {
const labels = getLabelsAt(provider, "SELECT * FROM trades WHERE price ")
expect(labels).toContain("BETWEEN")
})
})
// From /query/sql/sample-by.md
describe("SAMPLE BY autocomplete", () => {
it("should suggest SAMPLE after FROM table clause", () => {
const labels = getLabelsAt(provider, "SELECT avg(price) FROM trades ")
expect(labels).toContain("SAMPLE")
})
it("should suggest BY after SAMPLE", () => {
const labels = getLabelsAt(
provider,
"SELECT avg(price) FROM trades SAMPLE ",
)
expect(labels).toContain("BY")
})
it("should suggest FILL after SAMPLE BY duration", () => {
const labels = getLabelsAt(
provider,
"SELECT avg(price) FROM trades SAMPLE BY 1h ",
)
expect(labels).toContain("FILL")
})
it("should suggest ALIGN after SAMPLE BY duration", () => {
const labels = getLabelsAt(
provider,
"SELECT avg(price) FROM trades SAMPLE BY 1h ",
)
expect(labels).toContain("ALIGN")
})
it("should suggest FROM after SAMPLE BY duration (for FROM/TO range)", () => {
const labels = getLabelsAt(
provider,
"SELECT avg(price) FROM trades SAMPLE BY 1h ",
)
expect(labels).toContain("FROM")
})
it("should suggest ALIGN after FILL clause", () => {
const labels = getLabelsAt(
provider,
"SELECT avg(price) FROM trades SAMPLE BY 1h FILL(PREV) ",
)
expect(labels).toContain("ALIGN")
})
it("should suggest TO after ALIGN", () => {
const labels = getLabelsAt(
provider,
"SELECT avg(price) FROM trades SAMPLE BY 1h ALIGN ",
)
expect(labels).toContain("TO")
})
it("should suggest CALENDAR and FIRST after ALIGN TO", () => {
const labels = getLabelsAt(
provider,
"SELECT avg(price) FROM trades SAMPLE BY 1h ALIGN TO ",
)
expect(labels).toContain("CALENDAR")
expect(labels).toContain("FIRST")
})
it("should suggest OBSERVATION after FIRST", () => {
const labels = getLabelsAt(
provider,
"SELECT avg(price) FROM trades SAMPLE BY 1h ALIGN TO FIRST ",
)
expect(labels).toContain("OBSERVATION")
})
it("should suggest TIME and WITH after CALENDAR", () => {
const labels = getLabelsAt(
provider,
"SELECT avg(price) FROM trades SAMPLE BY 1h ALIGN TO CALENDAR ",
)
expect(labels).toContain("TIME")
expect(labels).toContain("WITH")
})
it("should suggest ZONE after TIME", () => {
const labels = getLabelsAt(
provider,
"SELECT avg(price) FROM trades SAMPLE BY 1h ALIGN TO CALENDAR TIME ",
)
expect(labels).toContain("ZONE")
})
})
})
// =============================================================================
// SAMPLE BY Autocomplete Walkthrough
// =============================================================================
describe("SAMPLE BY walkthrough", () => {
describe("SELECT ts, count() FROM trades SAMPLE BY 1d FILL(NULL) ALIGN TO CALENDAR", () => {
it("should provide correct suggestions at each word boundary", () => {
assertSuggestionsWalkthrough(provider, [
{
typed: "SELECT ts, count() FROM trades ",
expects: ["SAMPLE", "WHERE", "ORDER", "GROUP", "JOIN"],
},
{
typed: "SELECT ts, count() FROM trades SAMPLE ",
expects: ["BY"],
},
{
typed: "SELECT ts, count() FROM trades SAMPLE BY 1d ",
expects: ["FILL", "ALIGN", "FROM"],
},
{
typed: "SELECT ts, count() FROM trades SAMPLE BY 1d FILL(NULL) ",
expects: ["ALIGN"],
},
{
typed:
"SELECT ts, count() FROM trades SAMPLE BY 1d FILL(NULL) ALIGN ",
expects: ["TO"],
},
{
typed:
"SELECT ts, count() FROM trades SAMPLE BY 1d FILL(NULL) ALIGN TO ",
expects: ["CALENDAR", "FIRST"],
},
])
})
})
describe("SELECT ts, count() FROM trades SAMPLE BY 1h ALIGN TO CALENDAR TIME ZONE 'UTC' WITH OFFSET '00:15'", () => {
it("should provide correct suggestions at each word boundary", () => {
assertSuggestionsWalkthrough(provider, [
{
typed:
"SELECT ts, count() FROM trades SAMPLE BY 1h ALIGN TO CALENDAR ",
expects: ["TIME", "WITH"],
},
{
typed:
"SELECT ts, count() FROM trades SAMPLE BY 1h ALIGN TO CALENDAR TIME ",
expects: ["ZONE"],
},
])
})
})
})
// =============================================================================
// JOIN Autocomplete Tests (Chunk 3)
// =============================================================================
describe("JOIN autocomplete", () => {
it("should suggest JOIN types after FROM table", () => {
const labels = getLabelsAt(provider, "SELECT * FROM trades ")
expect(labels).toContain("JOIN")
expect(labels).toContain("INNER")
expect(labels).toContain("LEFT")
expect(labels).toContain("CROSS")
expect(labels).toContain("ASOF")
})
it("should suggest JOIN after LEFT/RIGHT/FULL", () => {
const labels = getLabelsAt(provider, "SELECT * FROM trades LEFT ")
expect(labels).toContain("JOIN")
expect(labels).toContain("OUTER")
})
it("should suggest ON after JOIN table", () => {
const labels = getLabelsAt(
provider,
"SELECT * FROM trades t JOIN orders o ",
)
expect(labels).toContain("ON")
})
it("should suggest TOLERANCE after ON condition in ASOF JOIN", () => {
const labels = getLabelsAt(
provider,
"SELECT * FROM trades t ASOF JOIN quotes q ON t.ts = q.ts ",
)
expect(labels).toContain("TOLERANCE")
})
describe("ASOF JOIN walkthrough", () => {
it("should provide correct suggestions at each word boundary", () => {
assertSuggestionsWalkthrough(provider, [
{
typed: "SELECT * FROM trades ",
expects: ["JOIN", "ASOF", "LEFT", "CROSS"],
},
{
typed: "SELECT * FROM trades ASOF ",
expects: ["JOIN"],
},
{
typed: "SELECT * FROM trades ASOF JOIN quotes ",
expects: ["ON"],
},
])
})
it("should NOT suggest OUTER after ASOF", () => {
const labels = getLabelsAt(provider, "SELECT * FROM trades ASOF ")
expect(labels).not.toContain("OUTER")
})
it("should suggest OUTER after LEFT", () => {
const labels = getLabelsAt(provider, "SELECT * FROM trades LEFT ")
expect(labels).toContain("OUTER")
expect(labels).toContain("JOIN")
})
it("should suggest join types after ON condition (chained joins)", () => {
const labels = getLabelsAt(
provider,
"SELECT * FROM trades t ASOF JOIN quotes q ON (symbol) ",
)
expect(labels).toContain("ASOF")
expect(labels).toContain("JOIN")
expect(labels).toContain("CROSS")
expect(labels).toContain("LEFT")
})
})
describe("join-type-specific postamble suggestions", () => {
it("ASOF JOIN: should suggest ON and TOLERANCE, not INCLUDE/EXCLUDE/RANGE", () => {
const labels = getLabelsAt(
provider,
"SELECT * FROM trades t ASOF JOIN quotes q ",
)
expect(labels).toContain("ON")
expect(labels).toContain("TOLERANCE")
expect(labels).not.toContain("INCLUDE")
expect(labels).not.toContain("EXCLUDE")
expect(labels).not.toContain("RANGE")
})
it("LT JOIN: should suggest ON and TOLERANCE, not INCLUDE/EXCLUDE/RANGE", () => {
const labels = getLabelsAt(
provider,
"SELECT * FROM trades t LT JOIN quotes q ",
)
expect(labels).toContain("ON")
expect(labels).toContain("TOLERANCE")
expect(labels).not.toContain("INCLUDE")
expect(labels).not.toContain("EXCLUDE")
expect(labels).not.toContain("RANGE")
})
it("SPLICE JOIN: should suggest ON, not TOLERANCE/INCLUDE/EXCLUDE/RANGE", () => {
const labels = getLabelsAt(
provider,
"SELECT * FROM trades t SPLICE JOIN quotes q ",
)
expect(labels).toContain("ON")
expect(labels).not.toContain("TOLERANCE")
expect(labels).not.toContain("INCLUDE")
expect(labels).not.toContain("EXCLUDE")
expect(labels).not.toContain("RANGE")
})
it("WINDOW JOIN: should suggest ON, RANGE, INCLUDE, EXCLUDE, not TOLERANCE", () => {
const labels = getLabelsAt(
provider,
"SELECT * FROM trades t WINDOW JOIN quotes q ",
)
expect(labels).toContain("ON")
expect(labels).toContain("RANGE")
expect(labels).toContain("INCLUDE")
expect(labels).toContain("EXCLUDE")
expect(labels).not.toContain("TOLERANCE")
})
it("INNER JOIN: should suggest ON, not TOLERANCE/INCLUDE/EXCLUDE/RANGE", () => {
const labels = getLabelsAt(
provider,
"SELECT * FROM trades t INNER JOIN quotes q ",
)
expect(labels).toContain("ON")
expect(labels).not.toContain("TOLERANCE")
expect(labels).not.toContain("INCLUDE")
expect(labels).not.toContain("EXCLUDE")
expect(labels).not.toContain("RANGE")
})
it("LEFT JOIN: should suggest ON, not TOLERANCE/INCLUDE/EXCLUDE/RANGE", () => {
const labels = getLabelsAt(
provider,
"SELECT * FROM trades t LEFT JOIN quotes q ",
)
expect(labels).toContain("ON")
expect(labels).not.toContain("TOLERANCE")
expect(labels).not.toContain("INCLUDE")
expect(labels).not.toContain("EXCLUDE")
expect(labels).not.toContain("RANGE")
})
})
})
// =============================================================================
// CREATE TABLE Autocomplete Tests (Chunk 4)
// =============================================================================
describe("CREATE TABLE autocomplete", () => {
it("should suggest TABLE after CREATE", () => {
const labels = getLabelsAt(provider, "CREATE ")
expect(labels).toContain("TABLE")
})
it("should suggest IF and AS after table name and columns", () => {
const labels = getLabelsAt(provider, "CREATE TABLE trades (ts TIMESTAMP) ")
expect(labels).toContain("TIMESTAMP")
expect(labels).toContain("PARTITION")
})
it("should suggest BY after PARTITION", () => {
const labels = getLabelsAt(
provider,
"CREATE TABLE trades (ts TIMESTAMP) TIMESTAMP(ts) PARTITION ",
)
expect(labels).toContain("BY")
})
it("should suggest partition units after PARTITION BY", () => {
const labels = getLabelsAt(
provider,
"CREATE TABLE trades (ts TIMESTAMP) TIMESTAMP(ts) PARTITION BY ",
)
expect(labels).toContain("DAY")
expect(labels).toContain("HOUR")
expect(labels).toContain("MONTH")
expect(labels).toContain("YEAR")
})
it("should suggest WAL and TTL after PARTITION BY UNIT", () => {
const labels = getLabelsAt(
provider,
"CREATE TABLE trades (ts TIMESTAMP) TIMESTAMP(ts) PARTITION BY DAY ",
)
expect(labels).toContain("WAL")
expect(labels).toContain("TTL")
})
describe("PIVOT walkthrough", () => {
it("should provide correct suggestions at each word boundary", () => {
assertSuggestionsWalkthrough(provider, [
{
typed: "trades ",
expects: ["PIVOT"],
},
{
typed: "trades PIVOT (sum(amount) ",
expects: ["FOR"],
},
{
typed: "trades PIVOT (sum(amount) FOR category ",
expects: ["IN"],
},
])
})
})
describe("CREATE VIEW walkthrough", () => {
it("should provide correct suggestions at each word boundary", () => {
assertSuggestionsWalkthrough(provider, [
{
typed: "CREATE ",
expects: ["TABLE", "VIEW"],
},
{
typed: "CREATE VIEW my_view ",
expects: ["AS"],
},
{
typed: "CREATE OR ",
expects: ["REPLACE"],
},
{
typed: "CREATE OR REPLACE ",
expects: ["VIEW"],
},
])
})
})
describe("DROP VIEW walkthrough", () => {
it("should provide correct suggestions at each word boundary", () => {
assertSuggestionsWalkthrough(provider, [
{
typed: "DROP ",
expects: ["TABLE", "VIEW"],
},
{
typed: "DROP VIEW ",
expects: ["IF"],
},
])
})
})
describe("SHOW walkthrough", () => {
it("should provide correct suggestions at each word boundary", () => {
assertSuggestionsWalkthrough(provider, [
{
typed: "SHOW ",
expects: ["TABLES", "COLUMNS", "CREATE", "PARAMETERS"],
},
{
typed: "SHOW CREATE ",
expects: ["TABLE", "VIEW", "MATERIALIZED"],
},
])
})
})
describe("CREATE TABLE walkthrough", () => {
it("should provide correct suggestions at each word boundary", () => {
assertSuggestionsWalkthrough(provider, [
{
typed: "CREATE ",
expects: ["TABLE"],
rejects: ["SELECT"],
},
{
typed:
"CREATE TABLE trades (ts TIMESTAMP, symbol SYMBOL, price DOUBLE) ",
expects: ["TIMESTAMP", "PARTITION"],
},
{
typed: "CREATE TABLE trades (ts TIMESTAMP) TIMESTAMP(ts) ",
expects: ["PARTITION"],
},
{
typed: "CREATE TABLE trades (ts TIMESTAMP) TIMESTAMP(ts) PARTITION ",
expects: ["BY"],
},
{
typed:
"CREATE TABLE trades (ts TIMESTAMP) TIMESTAMP(ts) PARTITION BY ",
expects: ["DAY", "HOUR", "MONTH", "YEAR", "WEEK", "NONE"],
},
])
})
})
})
// =============================================================================
// Materialized View Autocomplete Tests (Chunk 9)
// =============================================================================
describe("Materialized View autocomplete", () => {
describe("CREATE MATERIALIZED VIEW walkthrough", () => {
it("should provide correct suggestions at each word boundary", () => {
assertSuggestionsWalkthrough(provider, [
{
typed: "CREATE ",
expects: ["TABLE", "VIEW", "MATERIALIZED"],
},
{
typed: "CREATE MATERIALIZED ",
expects: ["VIEW"],
},
])
})
})
describe("DROP MATERIALIZED VIEW walkthrough", () => {
it("should provide correct suggestions at each word boundary", () => {
assertSuggestionsWalkthrough(provider, [
{
typed: "DROP ",
expects: ["TABLE", "VIEW", "MATERIALIZED"],
},
{
typed: "DROP MATERIALIZED ",
expects: ["VIEW"],
},
])
})
})
describe("REFRESH MATERIALIZED VIEW walkthrough", () => {
it("should provide correct suggestions at each word boundary", () => {
assertSuggestionsWalkthrough(provider, [
{
typed: "REFRESH ",
expects: ["MATERIALIZED"],
},
{
typed: "REFRESH MATERIALIZED ",
expects: ["VIEW"],
},
])
})
})
})
// =============================================================================
// User/Auth Autocomplete Tests (Chunk 10)
// =============================================================================
describe("User/Auth autocomplete", () => {
describe("CREATE USER walkthrough", () => {
it("should provide correct suggestions at each word boundary", () => {
assertSuggestionsWalkthrough(provider, [
{
typed: "CREATE ",
expects: ["USER", "GROUP", "SERVICE"],
},
{
typed: "CREATE SERVICE ",
expects: ["ACCOUNT"],
},
])
})
})
describe("GRANT walkthrough", () => {
it("should provide correct suggestions at each word boundary", () => {
assertSuggestionsWalkthrough(provider, [
{
typed: "GRANT ",
expects: ["SELECT", "INSERT", "UPDATE", "ALL"],
},
])