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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
//! Autocomplete suggestion insertion logic
//!
//! This module handles inserting autocomplete suggestions into the query,
//! managing cursor positioning, and executing the updated query.
use tui_textarea::{CursorMove, TextArea};
use crate::app::App;
use crate::autocomplete::autocomplete_state::Suggestion;
use crate::autocomplete::{SuggestionContext, analyze_context, find_char_before_field_access};
use crate::query::{CharType, QueryState};
#[cfg(debug_assertions)]
use log::debug;
/// Insert an autocomplete suggestion from App context
///
/// This function delegates to the existing `insert_suggestion()` function and
/// updates related app state (hide autocomplete, reset scroll, clear error overlay).
/// This pattern allows the App to delegate feature-specific logic to the autocomplete module.
///
/// # Arguments
/// * `app` - Mutable reference to the App struct
/// * `suggestion` - The suggestion to insert
pub fn insert_suggestion_from_app(app: &mut App, suggestion: &Suggestion) {
// Delegate to existing insert_suggestion function
insert_suggestion(&mut app.input.textarea, &mut app.query, suggestion);
// Hide autocomplete and reset scroll/error state
app.autocomplete.hide();
app.results_scroll.reset();
app.error_overlay_visible = false; // Auto-hide error overlay on query change
}
/// Insert an autocomplete suggestion at the current cursor position
/// Uses explicit state-based formulas for each context type
///
/// Returns the new query string after insertion
pub fn insert_suggestion(
textarea: &mut TextArea<'_>,
query_state: &mut QueryState,
suggestion: &Suggestion,
) {
let suggestion_text = &suggestion.text;
// Get base query that produced these suggestions
let base_query = match &query_state.base_query_for_suggestions {
Some(b) => b.clone(),
None => {
// Fallback to current query if no base (shouldn't happen)
textarea.lines()[0].clone()
}
};
let query = textarea.lines()[0].clone();
let cursor_pos = textarea.cursor().1;
let before_cursor = &query[..cursor_pos.min(query.len())];
#[cfg(debug_assertions)]
debug!(
"insert_suggestion: current_query='{}' base_query='{}' suggestion='{}' cursor_pos={}",
query, base_query, suggestion_text, cursor_pos
);
// Determine the trigger context
// Note: For insertion, we create a temporary BraceTracker since we only need
// to distinguish FunctionContext from FieldContext here. ObjectKeyContext
// handling will be added in task 9.
let mut temp_tracker = crate::autocomplete::BraceTracker::new();
temp_tracker.rebuild(before_cursor);
let (context, partial) = analyze_context(before_cursor, &temp_tracker);
#[cfg(debug_assertions)]
debug!(
"context_analysis: context={:?} partial='{}'",
context, partial
);
// For function/operator context (jq keywords like then, else, end, etc.),
// we should do simple replacement without adding dots or complex formulas
if context == SuggestionContext::FunctionContext {
// Simple replacement: remove the partial and insert the suggestion
let replacement_start = cursor_pos.saturating_sub(partial.len());
// Append opening parenthesis if the function requires arguments
let insert_text = if suggestion.needs_parens {
format!("{}(", suggestion_text)
} else {
suggestion_text.to_string()
};
let new_query = format!(
"{}{}{}",
&query[..replacement_start],
insert_text,
&query[cursor_pos..]
);
#[cfg(debug_assertions)]
debug!(
"function_context_replacement: partial='{}' new_query='{}'",
partial, new_query
);
// Replace the entire line with new query
textarea.delete_line_by_head();
textarea.insert_str(&new_query);
// Move cursor to end of inserted suggestion (including parenthesis if added)
let target_pos = replacement_start + insert_text.len();
move_cursor_to_column(textarea, target_pos);
// Execute query
execute_query_and_update(textarea, query_state);
return;
}
// For ObjectKeyContext (object key names inside `{}`),
// we do simple replacement similar to FunctionContext
// This handles cases like `{na` -> `{name` or `{name: .name, ag` -> `{name: .name, age`
if context == SuggestionContext::ObjectKeyContext {
// Simple replacement: remove the partial and insert the suggestion
let replacement_start = cursor_pos.saturating_sub(partial.len());
let new_query = format!(
"{}{}{}",
&query[..replacement_start],
suggestion_text,
&query[cursor_pos..]
);
#[cfg(debug_assertions)]
debug!(
"object_key_context_replacement: partial='{}' new_query='{}'",
partial, new_query
);
// Replace the entire line with new query
textarea.delete_line_by_head();
textarea.insert_str(&new_query);
// Move cursor to end of inserted suggestion
let target_pos = replacement_start + suggestion_text.len();
move_cursor_to_column(textarea, target_pos);
// Execute query
execute_query_and_update(textarea, query_state);
return;
}
// For field context, continue with the existing complex logic
let char_before = find_char_before_field_access(before_cursor, &partial);
let trigger_type = QueryState::classify_char(char_before);
// Extract middle_query: everything between base and current field being typed
// This preserves complex expressions like if/then/else, functions, etc.
let mut middle_query = extract_middle_query(&query, &base_query, before_cursor, &partial);
let mut adjusted_base = base_query.clone();
let mut adjusted_suggestion = suggestion_text.to_string();
#[cfg(debug_assertions)]
debug!(
"field_context: partial='{}' char_before={:?} trigger_type={:?} middle_query='{}'",
partial, char_before, trigger_type, middle_query
);
// Special handling for CloseBracket trigger with [] in middle_query
// This handles nested arrays like: .services[].capacityProviderStrategy[].field
// When user types [], it becomes part of middle_query, but should be part of base
if trigger_type == CharType::CloseBracket && middle_query == "[]" {
#[cfg(debug_assertions)]
debug!("nested_array_adjustment: detected [] in middle_query, moving to base");
// Move [] from middle to base
adjusted_base = format!("{}{}", base_query, middle_query);
middle_query = String::new();
// Strip [] prefix from suggestion if present (it's already in the query)
// Also strip the leading dot since CloseBracket formula will add it
if let Some(stripped) = adjusted_suggestion.strip_prefix("[]") {
// Strip leading dot if present (e.g., "[].base" -> "base")
adjusted_suggestion = stripped.strip_prefix('.').unwrap_or(stripped).to_string();
#[cfg(debug_assertions)]
debug!("nested_array_adjustment: stripped [] and leading dot from suggestion");
}
#[cfg(debug_assertions)]
debug!(
"nested_array_adjustment: adjusted_base='{}' adjusted_suggestion='{}' middle_query='{}'",
adjusted_base, adjusted_suggestion, middle_query
);
}
// Special case: if base is root "." and suggestion starts with ".",
// replace the base entirely instead of appending
// This prevents: "." + ".services" = "..services"
let new_query = if adjusted_base == "."
&& adjusted_suggestion.starts_with('.')
&& middle_query.is_empty()
{
#[cfg(debug_assertions)]
debug!("formula: root_replacement (special case for root '.')");
adjusted_suggestion.to_string()
} else {
// Apply insertion formula: base + middle + (operator) + suggestion
// The middle preserves complex expressions between base and current field
let formula_result = match trigger_type {
CharType::NoOp => {
// NoOp means continuing a path, but we need to check if suggestion needs a dot
// - If suggestion starts with special char like [, {, etc., don't add dot
// - If base is root ".", don't add another dot
// - Otherwise, add dot for path continuation (like .user.name)
let needs_dot = !adjusted_suggestion.starts_with('[')
&& !adjusted_suggestion.starts_with('{')
&& !adjusted_suggestion.starts_with('.')
&& adjusted_base != ".";
if needs_dot {
#[cfg(debug_assertions)]
debug!("formula: NoOp -> base + middle + '.' + suggestion");
format!("{}{}.{}", adjusted_base, middle_query, adjusted_suggestion)
} else {
#[cfg(debug_assertions)]
debug!("formula: NoOp -> base + middle + suggestion (no dot added)");
format!("{}{}{}", adjusted_base, middle_query, adjusted_suggestion)
}
}
CharType::CloseBracket => {
#[cfg(debug_assertions)]
debug!("formula: CloseBracket -> base + middle + '.' + suggestion");
// Formula: base + middle + "." + suggestion
format!("{}{}.{}", adjusted_base, middle_query, adjusted_suggestion)
}
CharType::PipeOperator | CharType::Semicolon | CharType::Comma | CharType::Colon => {
#[cfg(debug_assertions)]
debug!("formula: Separator -> base + middle + ' ' + suggestion");
// Formula: base + middle + " " + suggestion
// Trim trailing space from middle to avoid double spaces
let trimmed_middle = middle_query.trim_end();
format!(
"{}{} {}",
adjusted_base, trimmed_middle, adjusted_suggestion
)
}
CharType::OpenParen => {
#[cfg(debug_assertions)]
debug!(
"formula: OpenParen -> base + middle + suggestion (paren already in middle)"
);
// Formula: base + middle + suggestion
// The ( is already in middle_query, don't add it again
format!("{}{}{}", adjusted_base, middle_query, adjusted_suggestion)
}
CharType::OpenBracket => {
#[cfg(debug_assertions)]
debug!(
"formula: OpenBracket -> base + middle + suggestion (bracket already in middle)"
);
// Formula: base + middle + suggestion
// The [ is already in middle_query, don't add it again
format!("{}{}{}", adjusted_base, middle_query, adjusted_suggestion)
}
CharType::OpenBrace => {
#[cfg(debug_assertions)]
debug!(
"formula: OpenBrace -> base + middle + suggestion (brace already in middle)"
);
// Formula: base + middle + suggestion
// The { is already in middle_query, don't add it again
format!("{}{}{}", adjusted_base, middle_query, adjusted_suggestion)
}
CharType::QuestionMark => {
#[cfg(debug_assertions)]
debug!("formula: QuestionMark -> base + middle + '.' + suggestion");
// Formula: base + middle + "." + suggestion
format!("{}{}.{}", adjusted_base, middle_query, adjusted_suggestion)
}
CharType::Dot => {
#[cfg(debug_assertions)]
debug!("formula: Dot -> base + middle + suggestion");
// Formula: base + middle + suggestion
format!("{}{}{}", adjusted_base, middle_query, adjusted_suggestion)
}
CharType::CloseParen | CharType::CloseBrace => {
#[cfg(debug_assertions)]
debug!("formula: CloseParen/CloseBrace -> base + middle + '.' + suggestion");
// Formula: base + middle + "." + suggestion
format!("{}{}.{}", adjusted_base, middle_query, adjusted_suggestion)
}
};
#[cfg(debug_assertions)]
debug!(
"formula_components: base='{}' middle='{}' suggestion='{}'",
adjusted_base, middle_query, adjusted_suggestion
);
formula_result
};
#[cfg(debug_assertions)]
debug!("new_query_constructed: '{}'", new_query);
// Replace the entire line with new query
textarea.delete_line_by_head();
textarea.insert_str(&new_query);
#[cfg(debug_assertions)]
debug!("query_after_insertion: '{}'", textarea.lines()[0]);
// Move cursor to end of query
let target_pos = new_query.len();
move_cursor_to_column(textarea, target_pos);
// Execute query
execute_query_and_update(textarea, query_state);
}
/// Extract middle query: everything between base and current field being typed
///
/// Examples:
/// - Query: ".services | if has(...) then .ca", base: ".services"
/// → middle: " | if has(...) then "
/// - Query: ".services | .ca", base: ".services"
/// → middle: " | "
/// - Query: ".services.ca", base: ".services"
/// → middle: ""
pub fn extract_middle_query(
current_query: &str,
base_query: &str,
before_cursor: &str,
partial: &str,
) -> String {
// Find where base ends in current query
if !current_query.starts_with(base_query) {
// Base is not a prefix of current query (shouldn't happen, but handle gracefully)
return String::new();
}
// Find where the trigger char is in before_cursor
// Middle should be: everything after base, up to but not including trigger char
// Examples:
// Query: ".services | .ca", partial: "ca", base: ".services"
// → trigger is the dot at position 11
// → middle = query[9..11] = " | " (with trailing space, no dot)
let trigger_pos_in_before_cursor = if partial.is_empty() {
// Just typed trigger char - it's the last char
before_cursor.len().saturating_sub(1)
} else {
// Partial being typed - trigger is one char before partial
before_cursor.len().saturating_sub(partial.len() + 1)
};
#[cfg(debug_assertions)]
debug!(
"extract_middle_query: current_query='{}' before_cursor='{}' partial='{}' trigger_pos={} base_len={}",
current_query,
before_cursor,
partial,
trigger_pos_in_before_cursor,
base_query.len()
);
// Middle is everything from end of base to (but not including) trigger
let base_len = base_query.len();
if trigger_pos_in_before_cursor <= base_len {
// Trigger at or before base ends - no middle
return String::new();
}
// Extract middle - preserve all whitespace as it may be significant
// (e.g., "then " needs the space before the field access)
let middle = current_query[base_len..trigger_pos_in_before_cursor].to_string();
#[cfg(debug_assertions)]
debug!("extract_middle_query: extracted_middle='{}'", middle);
middle
}
/// Move cursor to a specific column position
pub fn move_cursor_to_column(textarea: &mut TextArea<'_>, target_col: usize) {
let current_col = textarea.cursor().1;
match target_col.cmp(¤t_col) {
std::cmp::Ordering::Less => {
// Move backward
for _ in 0..(current_col - target_col) {
textarea.move_cursor(CursorMove::Back);
}
}
std::cmp::Ordering::Greater => {
// Move forward
for _ in 0..(target_col - current_col) {
textarea.move_cursor(CursorMove::Forward);
}
}
std::cmp::Ordering::Equal => {
// Already at target position
}
}
}
/// Execute query and update results
pub fn execute_query_and_update(textarea: &TextArea<'_>, query_state: &mut QueryState) {
let query_text = textarea.lines()[0].clone();
query_state.execute(&query_text);
}
#[cfg(test)]
mod tests {
use super::*;
use crate::autocomplete::autocomplete_state::{Suggestion, SuggestionType};
use crate::autocomplete::jq_functions::JQ_FUNCTION_METADATA;
use proptest::prelude::*;
use tui_textarea::TextArea;
// ============================================================================
// Property-Based Tests for Insertion Behavior
// ============================================================================
// Helper function to get functions requiring arguments
fn get_functions_requiring_args() -> Vec<&'static crate::autocomplete::jq_functions::JqFunction>
{
JQ_FUNCTION_METADATA
.iter()
.filter(|f| f.needs_parens)
.collect()
}
// Helper function to get functions not requiring arguments
fn get_functions_not_requiring_args()
-> Vec<&'static crate::autocomplete::jq_functions::JqFunction> {
JQ_FUNCTION_METADATA
.iter()
.filter(|f| !f.needs_parens)
.collect()
}
// Helper to create a test environment for insertion
fn setup_insertion_test(initial_query: &str) -> (TextArea<'static>, crate::query::QueryState) {
let mut textarea = TextArea::default();
textarea.insert_str(initial_query);
let query_state = crate::query::QueryState::new(r#"{"test": true}"#.to_string());
(textarea, query_state)
}
// **Feature: enhanced-autocomplete, Property 1: Functions requiring arguments get parenthesis appended**
// *For any* jq function marked with `needs_parens = true`, when that function is inserted
// via Tab completion, the resulting query text SHALL end with the function name followed
// by an opening parenthesis `(`.
// **Validates: Requirements 1.1**
proptest! {
#![proptest_config(ProptestConfig::with_cases(100))]
#[test]
fn prop_functions_requiring_args_get_parenthesis(index in 0usize..100) {
let funcs = get_functions_requiring_args();
if funcs.is_empty() {
return Ok(());
}
let func = funcs[index % funcs.len()];
// Create a suggestion with needs_parens = true
let suggestion = Suggestion::new(func.name, SuggestionType::Function)
.with_needs_parens(true)
.with_signature(func.signature);
// Set up test environment with a partial query that would trigger function context
// e.g., typing "sel" should complete to "select("
let partial = &func.name[..func.name.len().min(3)];
let (mut textarea, mut query_state) = setup_insertion_test(partial);
// Insert the suggestion
insert_suggestion(&mut textarea, &mut query_state, &suggestion);
// Verify the result ends with function name followed by (
let result = textarea.lines()[0].clone();
let expected_suffix = format!("{}(", func.name);
prop_assert!(
result.ends_with(&expected_suffix),
"Function '{}' with needs_parens=true should result in '{}' but got '{}'",
func.name,
expected_suffix,
result
);
}
}
// **Feature: enhanced-autocomplete, Property 2: Functions not requiring arguments get no parenthesis**
// *For any* jq function marked with `needs_parens = false`, when that function is inserted
// via Tab completion, the resulting query text SHALL contain only the function name
// without any trailing parenthesis.
// **Validates: Requirements 1.2**
proptest! {
#![proptest_config(ProptestConfig::with_cases(100))]
#[test]
fn prop_functions_not_requiring_args_get_no_parenthesis(index in 0usize..100) {
let funcs = get_functions_not_requiring_args();
if funcs.is_empty() {
return Ok(());
}
let func = funcs[index % funcs.len()];
// Create a suggestion with needs_parens = false
let suggestion = Suggestion::new(func.name, SuggestionType::Function)
.with_needs_parens(false)
.with_signature(func.signature);
// Set up test environment with a partial query
let partial = &func.name[..func.name.len().min(3)];
let (mut textarea, mut query_state) = setup_insertion_test(partial);
// Insert the suggestion
insert_suggestion(&mut textarea, &mut query_state, &suggestion);
// Verify the result ends with function name (no parenthesis)
let result = textarea.lines()[0].clone();
prop_assert!(
result.ends_with(func.name),
"Function '{}' with needs_parens=false should end with '{}' but got '{}'",
func.name,
func.name,
result
);
// Also verify it does NOT end with (
prop_assert!(
!result.ends_with(&format!("{}(", func.name)),
"Function '{}' with needs_parens=false should NOT have '(' appended, but got '{}'",
func.name,
result
);
}
}
// **Feature: enhanced-autocomplete, Property 3: Cursor positioned after parenthesis for argument functions**
// *For any* jq function marked with `needs_parens = true`, after Tab insertion, the cursor
// position SHALL equal the length of the inserted text (function name + opening parenthesis).
// **Validates: Requirements 1.3**
proptest! {
#![proptest_config(ProptestConfig::with_cases(100))]
#[test]
fn prop_cursor_positioned_after_parenthesis(index in 0usize..100) {
let funcs = get_functions_requiring_args();
if funcs.is_empty() {
return Ok(());
}
let func = funcs[index % funcs.len()];
// Create a suggestion with needs_parens = true
let suggestion = Suggestion::new(func.name, SuggestionType::Function)
.with_needs_parens(true)
.with_signature(func.signature);
// Set up test environment with a partial query
let partial = &func.name[..func.name.len().min(3)];
let (mut textarea, mut query_state) = setup_insertion_test(partial);
// Insert the suggestion
insert_suggestion(&mut textarea, &mut query_state, &suggestion);
// Verify cursor position is at the end of the inserted text
let result = textarea.lines()[0].clone();
let cursor_col = textarea.cursor().1;
let expected_cursor_pos = result.len();
prop_assert_eq!(
cursor_col,
expected_cursor_pos,
"Cursor should be at position {} (end of '{}') but was at {}",
expected_cursor_pos,
result,
cursor_col
);
}
}
// **Feature: object-key-autocomplete, Property 5: ObjectKeyContext insertion replaces partial correctly**
// *For any* ObjectKeyContext suggestion accepted via Tab, the resulting query SHALL contain
// the suggestion text at the position where the partial was, with no duplicate characters.
// **Validates: Requirements 1.5**
proptest! {
#![proptest_config(ProptestConfig::with_cases(100))]
#[test]
fn prop_object_key_context_insertion_replaces_partial(
has_prefix in prop::bool::ANY, // Whether to include existing key-value pairs
prefix_key in "[a-z]{2,6}", // Key name for prefix (if used)
partial in "[a-z]{1,4}", // Partial being typed
suffix in "[a-z]{1,6}", // Suffix to append to partial to form suggestion
) {
// Build the suggestion by appending suffix to partial (ensures suggestion starts with partial)
let suggestion = format!("{}{}", partial, suffix);
// Build initial query: `{` or `{key: .key, ` followed by partial
let prefix = if has_prefix {
format!("{{{}: .{}, ", prefix_key, prefix_key)
} else {
"{".to_string()
};
let initial_query = format!("{}{}", prefix, partial);
let (mut textarea, mut query_state) = setup_insertion_test(&initial_query);
// Create a field suggestion (ObjectKeyContext suggestions are field names without dots)
let suggestion_obj = Suggestion::new(&suggestion, SuggestionType::Field);
// Insert the suggestion
insert_suggestion(&mut textarea, &mut query_state, &suggestion_obj);
// Get the result
let result = textarea.lines()[0].clone();
// Verify: the result should contain the suggestion at the right position
// The partial should be replaced by the suggestion, not duplicated
let expected = format!("{}{}", prefix, suggestion);
prop_assert_eq!(
result.clone(),
expected.clone(),
"ObjectKeyContext insertion should replace partial '{}' with suggestion '{}'. Initial: '{}', Expected: '{}', Got: '{}'",
partial,
suggestion,
initial_query,
expected,
result
);
// Verify: cursor should be positioned after the inserted suggestion
let cursor_col = textarea.cursor().1;
let expected_cursor_pos = expected.len();
prop_assert_eq!(
cursor_col,
expected_cursor_pos,
"Cursor should be at position {} (end of '{}') but was at {}",
expected_cursor_pos,
result,
cursor_col
);
}
}
// ============================================================================
// Middle Query Extraction Tests
// ============================================================================
#[test]
fn test_extract_middle_query_simple_path() {
// Simple path: no middle
let result = extract_middle_query(".services.ca", ".services", ".services.ca", "ca");
assert_eq!(result, "", "Simple path should have empty middle");
}
#[test]
fn test_extract_middle_query_after_pipe() {
// After pipe with identity - preserves trailing space
let result = extract_middle_query(".services | .ca", ".services", ".services | .ca", "ca");
assert_eq!(
result, " | ",
"Middle: pipe with trailing space (before dot)"
);
}
#[test]
fn test_extract_middle_query_with_if_then() {
// Complex: if/then between base and current field - preserves trailing space
let query = ".services | if has(\"x\") then .ca";
let before_cursor = query;
let result = extract_middle_query(query, ".services", before_cursor, "ca");
assert_eq!(
result, " | if has(\"x\") then ",
"Middle with trailing space (important for 'then ')"
);
}
#[test]
fn test_extract_middle_query_with_select() {
// With select function - preserves trailing space
let query = ".items | select(.active) | .na";
let result = extract_middle_query(query, ".items", query, "na");
assert_eq!(
result, " | select(.active) | ",
"Middle: includes pipe with trailing space"
);
}
#[test]
fn test_extract_middle_query_no_partial() {
// Just typed dot, no partial yet - preserves trailing space
let result = extract_middle_query(".services | .", ".services", ".services | .", "");
assert_eq!(
result, " | ",
"Middle with trailing space before trigger dot"
);
}
#[test]
fn test_extract_middle_query_base_not_prefix() {
// Edge case: base is not prefix of current query (shouldn't happen)
let result = extract_middle_query(".items.ca", ".services", ".items.ca", "ca");
assert_eq!(result, "", "Should return empty if base not a prefix");
}
#[test]
fn test_extract_middle_query_nested_pipes() {
// Multiple pipes and functions - preserves trailing space
let query = ".a | .b | map(.c) | .d";
let result = extract_middle_query(query, ".a", query, "d");
assert_eq!(result, " | .b | map(.c) | ", "Middle with trailing space");
}
// ============================================================================
// App Integration Tests for Autocomplete Insertion
// ============================================================================
// These tests verify the full autocomplete insertion flow through the App struct
use crate::query::ResultType;
use crate::test_utils::test_helpers::test_app;
/// Helper to create a test suggestion from text (for backward compatibility with existing tests)
fn test_suggestion(text: &str) -> Suggestion {
Suggestion::new(text, SuggestionType::Field)
}
#[test]
fn test_array_suggestion_appends_to_path() {
// When accepting [].field suggestion for .services, should produce .services[].field
let json = r#"{"services": [{"name": "alice"}, {"name": "bob"}, {"name": "charlie"}]}"#;
let mut app = test_app(json);
// Step 1: Execute ".services" to cache base
app.input.textarea.insert_str(".services");
app.query.execute(".services");
// Validate cached state after ".services"
assert_eq!(
app.query.base_query_for_suggestions,
Some(".services".to_string()),
"base_query should be '.services'"
);
assert_eq!(
app.query.base_type_for_suggestions,
Some(ResultType::ArrayOfObjects),
"base_type should be ArrayOfObjects"
);
// Step 2: Accept autocomplete suggestion "[].name" (no leading dot since after NoOp)
insert_suggestion_from_app(&mut app, &test_suggestion("[].name"));
// Should produce .services[].name (append, not replace)
assert_eq!(app.input.query(), ".services[].name");
// CRITICAL: Verify the query EXECUTES correctly and returns ALL array elements
let result = app.query.result.as_ref().unwrap();
assert!(result.contains("alice"), "Should contain first element");
assert!(result.contains("bob"), "Should contain second element");
assert!(result.contains("charlie"), "Should contain third element");
// Verify it does NOT just return nulls or single value
let line_count = result.lines().count();
assert!(
line_count >= 3,
"Should return at least 3 lines for 3 array elements"
);
}
#[test]
fn test_simple_path_continuation_with_dot() {
// Test simple path continuation: .object.field
// This is the bug: .services[0].deploymentConfiguration.alarms becomes deploymentConfigurationalarms
let json = r#"{"user": {"name": "Alice", "age": 30, "address": {"city": "NYC"}}}"#;
let mut app = test_app(json);
// Step 1: Execute base query
app.input.textarea.insert_str(".user");
app.query.execute(".user");
// Validate cached state
assert_eq!(
app.query.base_query_for_suggestions,
Some(".user".to_string())
);
assert_eq!(
app.query.base_type_for_suggestions,
Some(ResultType::Object)
);
// Step 2: Type ".na" (partial field access)
app.input.textarea.insert_str(".na");
// Step 3: Accept suggestion "name" (no leading dot since continuing path)
insert_suggestion_from_app(&mut app, &test_suggestion("name"));
// Should produce: .user.name
// NOT: .username (missing dot)
assert_eq!(app.input.query(), ".user.name");
// Verify execution
let result = app.query.result.as_ref().unwrap();
assert!(result.contains("Alice"));
}
#[test]
fn test_array_suggestion_replaces_partial_field() {
// When user types partial field after array name, accepting [] suggestion should replace partial
let json = r#"{"services": [{"serviceArn": "arn1"}, {"serviceArn": "arn2"}, {"serviceArn": "arn3"}]}"#;
let mut app = test_app(json);
// Step 1: Execute ".services" to cache base
app.input.textarea.insert_str(".services");
app.query.execute(".services");
// Validate cached state
assert_eq!(
app.query.base_query_for_suggestions,
Some(".services".to_string())
);
assert_eq!(
app.query.base_type_for_suggestions,
Some(ResultType::ArrayOfObjects)
);
// Step 2: Type ".s" (partial)
app.input.textarea.insert_char('.');
app.input.textarea.insert_char('s');
// Step 3: Accept autocomplete suggestion "[].serviceArn"
insert_suggestion_from_app(&mut app, &test_suggestion("[].serviceArn"));
// Should produce .services[].serviceArn (replace ".s" with "[].serviceArn")
assert_eq!(app.input.query(), ".services[].serviceArn");
// CRITICAL: Verify execution returns ALL serviceArns
let result = app.query.result.as_ref().unwrap();
assert!(result.contains("arn1"), "Should contain first serviceArn");
assert!(result.contains("arn2"), "Should contain second serviceArn");
assert!(result.contains("arn3"), "Should contain third serviceArn");
// Should NOT have nulls (would indicate query failed to iterate array)
let null_count = result.matches("null").count();
assert_eq!(
null_count, 0,
"Should not have any null values - query should iterate all array elements"
);
}
#[test]
fn test_array_suggestion_replaces_trailing_dot() {
// When user types ".services." (trailing dot, no partial), array suggestion should replace the dot
let json = r#"{"services": [{"deploymentConfiguration": {"x": 1}}, {"deploymentConfiguration": {"x": 2}}]}"#;
let mut app = test_app(json);
// Step 1: Execute ".services" to cache base query and type
app.input.textarea.insert_str(".services");
app.query.execute(".services");
// Validate cached state
assert_eq!(
app.query.base_query_for_suggestions,
Some(".services".to_string()),
"base_query should be '.services'"
);
assert_eq!(
app.query.base_type_for_suggestions,
Some(ResultType::ArrayOfObjects),
"base_type should be ArrayOfObjects"
);
// Step 2: Type a dot (syntax error, doesn't update base)
app.input.textarea.insert_char('.');
// Step 3: Accept autocomplete suggestion "[].deploymentConfiguration"
insert_suggestion_from_app(&mut app, &test_suggestion("[].deploymentConfiguration"));
// Should produce .services[].deploymentConfiguration (NOT .services.[].deploymentConfiguration)
assert_eq!(app.input.query(), ".services[].deploymentConfiguration");
// Verify the query executes correctly
let result = app.query.result.as_ref().unwrap();
assert!(result.contains("x"));
assert!(result.contains("1"));
assert!(result.contains("2"));
}
#[test]
fn test_nested_array_suggestion_replaces_trailing_dot() {
// Test deeply nested arrays: .services[].capacityProviderStrategy[].
let json = r#"{"services": [{"capacityProviderStrategy": [{"base": 0, "weight": 1}]}]}"#;
let mut app = test_app(json);
// Step 1: Execute base query to cache state
app.input
.textarea
.insert_str(".services[].capacityProviderStrategy[]");
app.query.execute(".services[].capacityProviderStrategy[]");
// Validate cached state
assert_eq!(
app.query.base_query_for_suggestions,
Some(".services[].capacityProviderStrategy[]".to_string())
);
// With only 1 service, this returns a single object, not destructured
assert_eq!(
app.query.base_type_for_suggestions,
Some(ResultType::Object)
);
// Step 2: Type trailing dot
app.input.textarea.insert_char('.');
// Step 3: Accept autocomplete suggestion "base"
// Note: suggestion is "base" (no prefix) since Object after CloseBracket
insert_suggestion_from_app(&mut app, &test_suggestion("base"));
// Should produce .services[].capacityProviderStrategy[].base
assert_eq!(
app.input.query(),
".services[].capacityProviderStrategy[].base"
);
// Verify the query executes and returns the base values
let result = app.query.result.as_ref().unwrap();
assert!(result.contains("0"));
}
#[test]
fn test_array_suggestion_after_pipe() {
// After pipe, array suggestions should include leading dot
let json = r#"{"services": [{"name": "svc1"}]}"#;
let mut app = test_app(json);
// Step 1: Execute base query
app.input.textarea.insert_str(".services");
app.query.execute(".services");
// Validate cached state
assert_eq!(
app.query.base_query_for_suggestions,
Some(".services".to_string())
);
assert_eq!(
app.query.base_type_for_suggestions,
Some(ResultType::ArrayOfObjects)
);
// Step 2: Type " | ."
app.input.textarea.insert_str(" | .");
// Step 3: Accept autocomplete suggestion ".[].name" (WITH leading dot after pipe)
insert_suggestion_from_app(&mut app, &test_suggestion(".[].name"));
// Should produce .services | .[].name (NOT .services | . | .[].name)
assert_eq!(app.input.query(), ".services | .[].name");
// Verify execution
let result = app.query.result.as_ref().unwrap();
assert!(result.contains("svc1"));
}
#[test]
fn test_array_suggestion_after_pipe_exact_user_flow() {
// Replicate exact user flow: type partial, select, then pipe
let json = r#"{"services": [{"capacityProviderStrategy": [{"base": 0}]}]}"#;
let mut app = test_app(json);
// Step 1: Type ".ser" (partial)
app.input.textarea.insert_str(".ser");
// Note: .ser returns null, base stays at "."
// Step 2: Select ".services" from autocomplete
// In real app, user would Tab here with suggestion ".services"
insert_suggestion_from_app(&mut app, &test_suggestion(".services"));
// Validate: should now be ".services"
assert_eq!(app.input.query(), ".services");
// Step 3: Verify base is now cached after successful execution
assert_eq!(
app.query.base_query_for_suggestions,
Some(".services".to_string()),
"base should be '.services' after insertion executed it"
);
assert_eq!(
app.query.base_type_for_suggestions,
Some(ResultType::ArrayOfObjects)
);
// Step 4: Type " | ."
app.input.textarea.insert_str(" | .");
// Step 5: Select ".[].capacityProviderStrategy"
insert_suggestion_from_app(&mut app, &test_suggestion(".[].capacityProviderStrategy"));
// Should produce: .services | .[].capacityProviderStrategy
// NOT: .services | . | .[].capacityProviderStrategy
assert_eq!(
app.input.query(),
".services | .[].capacityProviderStrategy"
);
}
#[test]
fn test_pipe_after_typing_space() {
// Test typing space then pipe character by character
let json = r#"{"services": [{"name": "svc1"}]}"#;
let mut app = test_app(json);
// Step 1: Type and execute ".services"
app.input.textarea.insert_str(".services");
app.query.execute(".services");
assert_eq!(
app.query.base_query_for_suggestions,
Some(".services".to_string())
);
// Step 2: Type space (executes ".services ")
app.input.textarea.insert_char(' ');
app.query.execute(".services ");
// Step 3: Type | (executes ".services |" - syntax error, base stays at ".services")
app.input.textarea.insert_char('|');
app.query.execute(".services |");
// Step 4: Type space then dot
app.input.textarea.insert_str(" .");
// Step 5: Accept suggestion
insert_suggestion_from_app(&mut app, &test_suggestion(".[].name"));
// Should be: base + " | " + suggestion
// Base is trimmed, so: ".services" + " | " + ".[].name" = ".services | .[].name" ✅
assert_eq!(app.input.query(), ".services | .[].name");
}
#[test]
fn test_suggestions_persist_when_typing_partial_after_array() {
// Critical: When typing partial field after [], suggestions should persist
let json = r#"{"services": [{"capacityProviderStrategy": [{"base": 0, "weight": 1, "capacityProvider": "x"}]}]}"#;
let mut app = test_app(json);
// Step 1: Type the full path up to the last array
app.input
.textarea
.insert_str(".services[].capacityProviderStrategy[]");
app.query.execute(".services[].capacityProviderStrategy[]");
app.update_autocomplete();
// Cache should have the array element objects with fields: base, weight, capacityProvider
assert!(app.query.last_successful_result_unformatted.is_some());
let cached = app.query.last_successful_result_unformatted.clone();
// Step 2: Type a dot - should still have cached result
app.input.textarea.insert_char('.');
// Query is now ".services[].capacityProviderStrategy[]." which is syntax error
app.query.execute(".services[].capacityProviderStrategy[].");
// Cache should NOT be cleared (syntax error doesn't update cache)
assert_eq!(app.query.last_successful_result_unformatted, cached);
// Step 3: Type a partial "b" - query returns multiple nulls
app.input.textarea.insert_char('b');
// Query is now ".services[].capacityProviderStrategy[].b" which returns multiple nulls
app.query
.execute(".services[].capacityProviderStrategy[].b");
// CRITICAL: Cache should STILL not be cleared (multiple nulls shouldn't overwrite)
assert_eq!(app.query.last_successful_result_unformatted, cached);
// Step 4: Update autocomplete - should still show suggestions based on cached result
app.update_autocomplete();
// Should have suggestions for the cached object fields
let suggestions = app.autocomplete.suggestions();
assert!(
!suggestions.is_empty(),
"Suggestions should persist when typing partial that returns null"
);
// Should have "base" suggestion (filtered by partial "b")
assert!(
suggestions.iter().any(|s| s.text.contains("base")),
"Should suggest 'base' field when filtering by 'b'"
);
}
#[test]
fn test_suggestions_persist_with_optional_chaining_and_partial() {
// Critical: When typing partial after []?, suggestions should persist
// Realistic scenario: some services have capacityProviderStrategy, some don't
let json = r#"{
"services": [
{
"serviceName": "service1",
"capacityProviderStrategy": [
{"base": 0, "weight": 1, "capacityProvider": "FARGATE"},
{"base": 0, "weight": 2, "capacityProvider": "FARGATE_SPOT"}
]
},
{
"serviceName": "service2"
},
{
"serviceName": "service3",
"capacityProviderStrategy": [
{"base": 1, "weight": 3, "capacityProvider": "EC2"}
]
}
]
}"#;
let mut app = test_app(json);
// Step 1: Execute query with optional chaining up to the array
app.input
.textarea
.insert_str(".services[].capacityProviderStrategy[]?");
app.query.execute(".services[].capacityProviderStrategy[]?");
// This should return the object with base, weight, capacityProvider fields
let cached_before_partial = app.query.last_successful_result_unformatted.clone();
assert!(cached_before_partial.is_some());
assert!(cached_before_partial.as_ref().unwrap().contains("base"));
// Step 2: Type a dot
app.input.textarea.insert_char('.');
app.query
.execute(".services[].capacityProviderStrategy[]?.");
// Syntax error - cache should remain
assert_eq!(
app.query.last_successful_result_unformatted,
cached_before_partial
);
// Step 3: Type partial "b"
app.input.textarea.insert_char('b');
app.query
.execute(".services[].capacityProviderStrategy[]?.b");
// This returns single "null" (not multiple) due to optional chaining
// Cache should NOT be updated
assert_eq!(
app.query.last_successful_result_unformatted, cached_before_partial,
"Cache should not be overwritten by null result from partial field"
);
// Step 4: Update autocomplete
app.update_autocomplete();
// Should have suggestions based on the cached object
let suggestions = app.autocomplete.suggestions();
assert!(
!suggestions.is_empty(),
"Suggestions should persist when typing partial after []?"
);
// Should suggest "base" (filtered by partial "b")
assert!(
suggestions.iter().any(|s| s.text.contains("base")),
"Should suggest 'base' field when filtering by 'b' after []?"
);
}
#[test]
fn test_jq_keyword_autocomplete_no_dot_prefix() {
// Test that jq keywords like "then", "else", "end" don't get a dot prefix
let json = r#"{"services": [{"capacityProviderStrategy": [{"base": 0}]}]}"#;
let mut app = test_app(json);
// Step 1: Type the beginning of an if statement
app.input
.textarea
.insert_str(".services | if has(\"capacityProviderStrategy\")");
app.query
.execute(".services | if has(\"capacityProviderStrategy\")");
// Step 2: Type partial "the" to trigger autocomplete for "then"
app.input.textarea.insert_str(" the");
// Step 3: Accept "then" from autocomplete
// This should NOT add a dot before "then"
insert_suggestion_from_app(&mut app, &test_suggestion("then"));
// Should produce: .services | if has("capacityProviderStrategy") then
// NOT: .services | if has("capacityProviderStrategy") .then
assert_eq!(
app.input.query(),
".services | if has(\"capacityProviderStrategy\") then"
);
// Verify no extra dot was added
assert!(
!app.input.query().contains(" .then"),
"Should not have dot before 'then' keyword"
);
}
#[test]
fn test_jq_keyword_else_autocomplete() {
// Test "else" keyword autocomplete
let json = r#"{"value": 42}"#;
let mut app = test_app(json);
// Type an if-then statement
app.input
.textarea
.insert_str("if .value > 10 then \"high\" el");
// Accept "else" from autocomplete
insert_suggestion_from_app(&mut app, &test_suggestion("else"));
// Should produce: if .value > 10 then "high" else
// NOT: if .value > 10 then "high" .else
assert_eq!(app.input.query(), "if .value > 10 then \"high\" else");
assert!(
!app.input.query().contains(".else"),
"Should not have dot before 'else' keyword"
);
}
#[test]
fn test_jq_keyword_end_autocomplete() {
// Test "end" keyword autocomplete
let json = r#"{"value": 42}"#;
let mut app = test_app(json);
// Type a complete if-then-else statement
app.input
.textarea
.insert_str("if .value > 10 then \"high\" else \"low\" en");
// Accept "end" from autocomplete
insert_suggestion_from_app(&mut app, &test_suggestion("end"));
// Should produce: if .value > 10 then "high" else "low" end
// NOT: if .value > 10 then "high" else "low" .end
assert_eq!(
app.input.query(),
"if .value > 10 then \"high\" else \"low\" end"
);
assert!(
!app.input.query().contains(".end"),
"Should not have dot before 'end' keyword"
);
}
#[test]
fn test_field_access_after_jq_keyword_preserves_space() {
// Test that field access after "then" preserves the space
// Bug: ".services[] | if has(\"x\") then .field" becomes "then.field" (no space)
let json = r#"{"services": [{"capacityProviderStrategy": [{"base": 0}]}]}"#;
let mut app = test_app(json);
// Step 1: Execute base query
app.input.textarea.insert_str(".services[]");
app.query.execute(".services[]");
// Step 2: Type if-then with field access
app.input
.textarea
.insert_str(" | if has(\"capacityProviderStrategy\") then .ca");
// Step 3: Accept field suggestion (with leading dot as it would come from get_suggestions)
insert_suggestion_from_app(&mut app, &test_suggestion(".capacityProviderStrategy"));
// Should produce: .services[] | if has("capacityProviderStrategy") then .capacityProviderStrategy
// NOT: .services[] | if has("capacityProviderStrategy") thencapacityProviderStrategy
assert_eq!(
app.input.query(),
".services[] | if has(\"capacityProviderStrategy\") then .capacityProviderStrategy"
);
// Verify there's a space before the field name
assert!(
app.input.query().contains("then .capacityProviderStrategy"),
"Should have space between 'then' and field name"
);
assert!(
!app.input.query().contains("thencapacityProviderStrategy"),
"Should NOT concatenate 'then' with field name"
);
}
#[test]
fn test_field_access_after_else_preserves_space() {
// Test that field access after "else" preserves the space
let json = r#"{"services": [{"name": "test"}]}"#;
let mut app = test_app(json);
// Execute base query
app.input.textarea.insert_str(".services[]");
app.query.execute(".services[]");
// Type if-then-else with field access
app.input
.textarea
.insert_str(" | if has(\"name\") then .name else .na");
// Accept field suggestion (with leading dot as it would come from get_suggestions)
insert_suggestion_from_app(&mut app, &test_suggestion(".name"));
// Should have space between "else" and field
assert!(
app.input.query().contains("else .name"),
"Should have space between 'else' and field name"
);
assert!(
!app.input.query().contains("elsename"),
"Should NOT concatenate 'else' with field name"
);
}
#[test]
fn test_autocomplete_inside_if_statement() {
// Autocomplete inside complex query should only replace the local part
let json = r#"{"services": [{"capacityProviderStrategy": [{"base": 0}]}]}"#;
let mut app = test_app(json);
// User types complex query with if/then
app.input
.textarea
.insert_str(".services | if has(\"capacityProviderStrategy\") then .ca");
// Execute to cache state (this will likely error due to incomplete query)
app.query
.execute(".services | if has(\"capacityProviderStrategy\") then .ca");
// The issue: when Tab is pressed, entire query gets replaced with base + suggestion
// Expected: only ".ca" should be replaced
// Actual: entire query replaced with ".services[].capacityProviderStrategy"
// TODO: This test documents the bug - we need smarter insertion
// For now, this is a known limitation when using autocomplete inside complex expressions
}
#[test]
fn test_root_field_suggestion() {
// At root, typing "." and selecting field should replace "." with ".field"
let json = r#"{"services": [{"name": "test"}], "status": "active"}"#;
let mut app = test_app(json);
// Validate initial state
assert_eq!(
app.query.base_query_for_suggestions,
Some(".".to_string()),
"base_query should be '.' initially"
);
assert_eq!(
app.query.base_type_for_suggestions,
Some(ResultType::Object),
"base_type should be Object"
);
// User types "."
app.input.textarea.insert_str(".");
// Accept suggestion ".services" (with leading dot since at root after NoOp)
insert_suggestion_from_app(&mut app, &test_suggestion(".services"));
// Should produce ".services" NOT "..services"
assert_eq!(app.input.query(), ".services");
// Verify query executes correctly
let result = app.query.result.as_ref().unwrap();
assert!(result.contains("name"));
}
#[test]
fn test_field_suggestion_replaces_from_dot() {
// When accepting .field suggestion at root, should replace from last dot
let json = r#"{"name": "test", "age": 30}"#;
let mut app = test_app(json);
// Initial state: "." was executed during App::new()
// Validate initial state
assert_eq!(
app.query.base_query_for_suggestions,
Some(".".to_string()),
"base_query should be '.' initially"
);
assert_eq!(
app.query.base_type_for_suggestions,
Some(ResultType::Object),
"base_type should be Object for root"
);
// Simulate: user typed ".na" and cursor is at end
app.input.textarea.insert_str(".na");
// Accept autocomplete suggestion "name" (no leading dot since after Dot)
insert_suggestion_from_app(&mut app, &test_suggestion("name"));
// Should produce .name (replace from the dot)
assert_eq!(app.input.query(), ".name");
}
#[test]
fn test_autocomplete_with_real_ecs_like_data() {
// Test with data structure similar to AWS ECS services
let json = r#"{
"services": [
{"serviceArn": "arn:aws:ecs:region:account:service/cluster/svc1", "serviceName": "service1"},
{"serviceArn": "arn:aws:ecs:region:account:service/cluster/svc2", "serviceName": "service2"},
{"serviceArn": "arn:aws:ecs:region:account:service/cluster/svc3", "serviceName": "service3"},
{"serviceArn": "arn:aws:ecs:region:account:service/cluster/svc4", "serviceName": "service4"},
{"serviceArn": "arn:aws:ecs:region:account:service/cluster/svc5", "serviceName": "service5"}
]
}"#;
let mut app = test_app(json);
// Step 1: Execute ".services" to cache base
app.input.textarea.insert_str(".services");
app.query.execute(".services");
// Validate cached state
assert_eq!(
app.query.base_query_for_suggestions,
Some(".services".to_string()),
"base_query should be '.services'"
);
assert_eq!(
app.query.base_type_for_suggestions,
Some(ResultType::ArrayOfObjects),
"base_type should be ArrayOfObjects"
);
// Step 2: Type ".s" (partial)
app.input.textarea.insert_str(".s");
// Step 3: Accept "[].serviceArn" (no leading dot since after NoOp)
insert_suggestion_from_app(&mut app, &test_suggestion("[].serviceArn"));
let query_text = app.input.query();
assert_eq!(query_text, ".services[].serviceArn");
// Verify execution returns ALL 5 serviceArns
let result = app.query.result.as_ref().unwrap();
// Check for all service ARNs
assert!(result.contains("svc1"));
assert!(result.contains("svc2"));
assert!(result.contains("svc3"));
assert!(result.contains("svc4"));
assert!(result.contains("svc5"));
// Count non-null values
let lines: Vec<&str> = result.lines().collect();
let non_null_lines: Vec<&str> = lines
.iter()
.filter(|line| !line.trim().contains("null"))
.copied()
.collect();
assert!(
non_null_lines.len() >= 5,
"Should have at least 5 non-null results, got {}",
non_null_lines.len()
);
}
// ============================================================================
// ObjectKeyContext Insertion Unit Tests
// ============================================================================
// These tests verify ObjectKeyContext insertion behavior per Requirements 1.5
#[test]
fn test_object_key_context_insertion_simple() {
// Test: `{na` + accept "name" → `{name`
// This tests the basic ObjectKeyContext insertion after opening brace
let initial_query = "{na";
let (mut textarea, mut query_state) = setup_insertion_test(initial_query);
// Create a field suggestion (ObjectKeyContext suggestions are field names without dots)
let suggestion = Suggestion::new("name", SuggestionType::Field);
// Insert the suggestion
insert_suggestion(&mut textarea, &mut query_state, &suggestion);
// Verify the result
let result = textarea.lines()[0].clone();
assert_eq!(
result, "{name",
"ObjectKeyContext insertion should replace 'na' with 'name'. Got: '{}'",
result
);
// Verify cursor position is at the end
let cursor_col = textarea.cursor().1;
assert_eq!(
cursor_col, 5,
"Cursor should be at position 5 (end of '{{name')"
);
}
#[test]
fn test_object_key_context_insertion_after_comma() {
// Test: `{name: .name, ag` + accept "age" → `{name: .name, age`
// This tests ObjectKeyContext insertion after comma in object literal
let initial_query = "{name: .name, ag";
let (mut textarea, mut query_state) = setup_insertion_test(initial_query);
// Create a field suggestion
let suggestion = Suggestion::new("age", SuggestionType::Field);
// Insert the suggestion
insert_suggestion(&mut textarea, &mut query_state, &suggestion);
// Verify the result
let result = textarea.lines()[0].clone();
assert_eq!(
result, "{name: .name, age",
"ObjectKeyContext insertion should replace 'ag' with 'age'. Got: '{}'",
result
);
// Verify cursor position is at the end
let cursor_col = textarea.cursor().1;
assert_eq!(
cursor_col, 17,
"Cursor should be at position 17 (end of '{{name: .name, age')"
);
}
#[test]
fn test_object_key_context_insertion_with_space_after_comma() {
// Test: `{name: .name, ag` (with space after comma) + accept "age" → `{name: .name, age`
// This tests that spaces are preserved correctly
let initial_query = "{name: .name, ag";
let (mut textarea, mut query_state) = setup_insertion_test(initial_query);
let suggestion = Suggestion::new("age", SuggestionType::Field);
insert_suggestion(&mut textarea, &mut query_state, &suggestion);
let result = textarea.lines()[0].clone();
assert_eq!(result, "{name: .name, age");
}
#[test]
fn test_object_key_context_insertion_nested_object() {
// Test: `{outer: {in` + accept "inner" → `{outer: {inner`
// This tests ObjectKeyContext in nested object
let initial_query = "{outer: {in";
let (mut textarea, mut query_state) = setup_insertion_test(initial_query);
let suggestion = Suggestion::new("inner", SuggestionType::Field);
insert_suggestion(&mut textarea, &mut query_state, &suggestion);
let result = textarea.lines()[0].clone();
assert_eq!(
result, "{outer: {inner",
"ObjectKeyContext insertion in nested object should work. Got: '{}'",
result
);
}
#[test]
fn test_object_key_context_insertion_longer_partial() {
// Test: `{servi` + accept "services" → `{services`
// This tests with a longer partial
let initial_query = "{servi";
let (mut textarea, mut query_state) = setup_insertion_test(initial_query);
let suggestion = Suggestion::new("services", SuggestionType::Field);
insert_suggestion(&mut textarea, &mut query_state, &suggestion);
let result = textarea.lines()[0].clone();
assert_eq!(
result, "{services",
"ObjectKeyContext insertion with longer partial should work. Got: '{}'",
result
);
}
#[test]
fn test_object_key_context_insertion_single_char_partial() {
// Test: `{n` + accept "name" → `{name`
// This tests with a single character partial
let initial_query = "{n";
let (mut textarea, mut query_state) = setup_insertion_test(initial_query);
let suggestion = Suggestion::new("name", SuggestionType::Field);
insert_suggestion(&mut textarea, &mut query_state, &suggestion);
let result = textarea.lines()[0].clone();
assert_eq!(
result, "{name",
"ObjectKeyContext insertion with single char partial should work. Got: '{}'",
result
);
}
}