jiq 2.21.1

Interactive JSON query tool with real-time output
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
use super::autocomplete_state::Suggestion;
use super::brace_tracker::BraceTracker;
use super::jq_functions::filter_builtins;
use super::result_analyzer::ResultAnalyzer;
use crate::query::ResultType;

/// Filter suggestions by partial match (case-insensitive)
fn filter_suggestions_by_partial(suggestions: Vec<Suggestion>, partial: &str) -> Vec<Suggestion> {
    let partial_lower = partial.to_lowercase();
    suggestions
        .into_iter()
        .filter(|s| s.text.to_lowercase().contains(&partial_lower))
        .collect()
}

/// Context information about what's being typed
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[allow(clippy::enum_variant_names)] // Context suffix is intentional for clarity
pub enum SuggestionContext {
    /// At start or after pipe/operator - suggest functions and patterns
    FunctionContext,
    /// After a dot - suggest field names
    FieldContext,
    /// Inside object literal `{}` where an object key name is expected.
    /// This context is triggered after `{` or `,` inside an object literal
    /// when the user is typing a partial identifier (not starting with `.`).
    /// Suggestions in this context do NOT have a leading dot, enabling
    /// efficient construction of object literals like `{name: .name}`.
    ObjectKeyContext,
}

/// Analyze query text and cursor position to determine what to suggest
pub fn get_suggestions(
    query: &str,
    cursor_pos: usize,
    result: Option<&str>,
    result_type: Option<ResultType>,
    brace_tracker: &BraceTracker,
) -> Vec<Suggestion> {
    // Get the text before cursor
    let before_cursor = &query[..cursor_pos.min(query.len())];

    // Determine context and get the partial word being typed
    let (context, partial) = analyze_context(before_cursor, brace_tracker);

    match context {
        SuggestionContext::FieldContext => {
            // Determine trigger context to decide if suggestions need leading dot
            let char_before_dot = find_char_before_field_access(before_cursor, &partial);

            // Check if there's a dot immediately before the partial (or at cursor if no partial)
            // This helps distinguish: ".services.ca" (continuation) vs "then .ca" (new path)
            let dot_pos = if partial.is_empty() {
                before_cursor.len().saturating_sub(1)
            } else {
                before_cursor.len().saturating_sub(partial.len() + 1)
            };
            let has_immediate_dot =
                dot_pos < before_cursor.len() && before_cursor.chars().nth(dot_pos) == Some('.');

            // Check if there's whitespace between char_before and the dot
            let has_whitespace_before_dot = if dot_pos > 0 && has_immediate_dot {
                before_cursor[..dot_pos]
                    .chars()
                    .rev()
                    .take_while(|c| c.is_whitespace())
                    .count()
                    > 0
            } else {
                false
            };

            // Include leading dot when:
            // 1. After operators (starting new path): |, ;, ,, :, (, [, {
            // 2. At start of query (None)
            // 3. After whitespace + dot (like "then .field") - new path after keyword
            let needs_leading_dot = matches!(
                char_before_dot,
                Some('|')
                    | Some(';')
                    | Some(',')
                    | Some(':')
                    | Some('(')
                    | Some('[')
                    | Some('{')
                    | None
            ) || has_whitespace_before_dot;

            // Generate type-aware suggestions (no mutation needed!)
            let suggestions = if let (Some(result), Some(typ)) = (result, result_type) {
                ResultAnalyzer::analyze_result(result, typ, needs_leading_dot)
            } else {
                Vec::new()
            };

            // Filter suggestions by partial match
            if partial.is_empty() {
                suggestions
            } else {
                filter_suggestions_by_partial(suggestions, &partial)
            }
        }
        SuggestionContext::FunctionContext => {
            // Suggest jq functions/patterns/operators
            if partial.is_empty() {
                Vec::new()
            } else {
                filter_builtins(&partial)
            }
        }
        SuggestionContext::ObjectKeyContext => {
            // Object key context: suggest field names without leading dot
            // Per requirement 1.3: return empty if partial is empty
            if partial.is_empty() {
                return Vec::new();
            }

            // Generate suggestions without leading dot (needs_leading_dot = false)
            let suggestions = if let (Some(result), Some(typ)) = (result, result_type) {
                ResultAnalyzer::analyze_result(result, typ, false)
            } else {
                Vec::new()
            };

            // Filter suggestions by partial match (same as FieldContext)
            filter_suggestions_by_partial(suggestions, &partial)
        }
    }
}

/// Analyze the text before cursor to determine context and partial word
pub fn analyze_context(
    before_cursor: &str,
    brace_tracker: &BraceTracker,
) -> (SuggestionContext, String) {
    if before_cursor.is_empty() {
        return (SuggestionContext::FunctionContext, String::new());
    }

    // Find the last "word" being typed by looking backwards
    let chars: Vec<char> = before_cursor.chars().collect();
    let mut i = chars.len();

    // Skip trailing whitespace
    while i > 0 && chars[i - 1].is_whitespace() {
        i -= 1;
    }

    if i == 0 {
        return (SuggestionContext::FunctionContext, String::new());
    }

    // Check if we're in field context (after a dot)
    if i > 0 && chars[i - 1] == '.' {
        // Just typed a dot - suggest all fields
        return (SuggestionContext::FieldContext, String::new());
    }

    // Look for the start of the current token
    let mut start = i;
    while start > 0 {
        let ch = chars[start - 1];

        // Stop at delimiters
        if is_delimiter(ch) {
            break;
        }

        start -= 1;
    }

    // Extract the partial word
    let partial: String = chars[start..i].iter().collect();

    // Check if the partial starts with a dot (field access) or question mark (optional field access)
    if let Some(stripped) = partial.strip_prefix('.') {
        // Field context - return the part after the LAST dot (for nested fields like .user.na)
        let field_partial = if let Some(last_dot_pos) = partial.rfind('.') {
            partial[last_dot_pos + 1..].to_string()
        } else {
            stripped.to_string()
        };
        return (SuggestionContext::FieldContext, field_partial);
    } else if let Some(stripped) = partial.strip_prefix('?') {
        // Optional field access like []?.field
        // If after ? there's a dot, strip it and get the field name
        let field_partial = if let Some(after_dot) = stripped.strip_prefix('.') {
            after_dot.to_string()
        } else {
            // Just ? with no dot yet
            String::new()
        };
        return (SuggestionContext::FieldContext, field_partial);
    }

    // Check what comes before the partial to determine context
    if start > 0 {
        // Look backwards to see if there's a dot or question mark before this position
        let mut j = start;
        while j > 0 && chars[j - 1].is_whitespace() {
            j -= 1;
        }

        if j > 0 {
            let char_before = chars[j - 1];
            // Field context if preceded by dot or question mark
            // Examples: .field, []?.field
            if char_before == '.' || char_before == '?' {
                return (SuggestionContext::FieldContext, partial);
            }

            // Check for ObjectKeyContext: after `{` or `,` when inside an object literal
            // Only if we have a non-empty partial (per requirement 1.3)
            if !partial.is_empty() && (char_before == '{' || char_before == ',') {
                // Use BraceTracker to verify we're actually inside an object literal
                // The position to check is the cursor position (end of before_cursor)
                if brace_tracker.is_in_object(before_cursor.len()) {
                    return (SuggestionContext::ObjectKeyContext, partial);
                }
            }
        }
    }

    // Otherwise, function context
    (SuggestionContext::FunctionContext, partial)
}

/// Find the character that precedes the field access (the dot we're typing after)
/// Returns None if at the start of the query
pub fn find_char_before_field_access(before_cursor: &str, partial: &str) -> Option<char> {
    // We need to find what's before the dot that triggered FieldContext
    // If we have a partial (like "ser" in ".services | .ser"), go back past it
    // If no partial (like in ".services | ."), we're right after the dot

    let search_end = if partial.is_empty() {
        // No partial - we just typed the dot, so look before it
        before_cursor.len().saturating_sub(1)
    } else {
        // Have partial - go back past partial and the dot
        before_cursor.len().saturating_sub(partial.len() + 1)
    };

    if search_end == 0 {
        return None; // At start of query
    }

    // Search backwards for the first non-whitespace character
    let chars: Vec<char> = before_cursor[..search_end].chars().collect();
    for i in (0..chars.len()).rev() {
        let ch = chars[i];
        if !ch.is_whitespace() {
            return Some(ch);
        }
    }

    None
}

/// Check if a character is a delimiter
fn is_delimiter(ch: char) -> bool {
    matches!(
        ch,
        '|' | ';' | '(' | ')' | '[' | ']' | '{' | '}' | ',' | ' ' | '\t' | '\n' | '\r'
    )
}

#[cfg(test)]
mod tests {
    use super::*;
    use proptest::prelude::*;

    /// Helper to create a BraceTracker initialized with the given query
    fn tracker_for(query: &str) -> BraceTracker {
        let mut tracker = BraceTracker::new();
        tracker.rebuild(query);
        tracker
    }

    #[test]
    fn test_empty_query() {
        let tracker = tracker_for("");
        let (ctx, partial) = analyze_context("", &tracker);
        assert_eq!(ctx, SuggestionContext::FunctionContext);
        assert_eq!(partial, "");
    }

    #[test]
    fn test_function_context() {
        let tracker = tracker_for("ma");
        let (ctx, partial) = analyze_context("ma", &tracker);
        assert_eq!(ctx, SuggestionContext::FunctionContext);
        assert_eq!(partial, "ma");

        let tracker = tracker_for("select");
        let (ctx, partial) = analyze_context("select", &tracker);
        assert_eq!(ctx, SuggestionContext::FunctionContext);
        assert_eq!(partial, "select");
    }

    #[test]
    fn test_field_context_with_dot() {
        let tracker = tracker_for(".na");
        let (ctx, partial) = analyze_context(".na", &tracker);
        assert_eq!(ctx, SuggestionContext::FieldContext);
        assert_eq!(partial, "na");

        let tracker = tracker_for(".name");
        let (ctx, partial) = analyze_context(".name", &tracker);
        assert_eq!(ctx, SuggestionContext::FieldContext);
        assert_eq!(partial, "name");
    }

    #[test]
    fn test_just_dot() {
        let tracker = tracker_for(".");
        let (ctx, partial) = analyze_context(".", &tracker);
        assert_eq!(ctx, SuggestionContext::FieldContext);
        assert_eq!(partial, "");
    }

    #[test]
    fn test_after_pipe() {
        let tracker = tracker_for(".name | ma");
        let (ctx, partial) = analyze_context(".name | ma", &tracker);
        assert_eq!(ctx, SuggestionContext::FunctionContext);
        assert_eq!(partial, "ma");
    }

    #[test]
    fn test_nested_field() {
        let tracker = tracker_for(".user.na");
        let (ctx, partial) = analyze_context(".user.na", &tracker);
        assert_eq!(ctx, SuggestionContext::FieldContext);
        assert_eq!(partial, "na");
    }

    #[test]
    fn test_array_access() {
        let tracker = tracker_for(".items[0].na");
        let (ctx, partial) = analyze_context(".items[0].na", &tracker);
        assert_eq!(ctx, SuggestionContext::FieldContext);
        assert_eq!(partial, "na");
    }

    #[test]
    fn test_in_function_call() {
        let tracker = tracker_for("map(.na");
        let (ctx, partial) = analyze_context("map(.na", &tracker);
        assert_eq!(ctx, SuggestionContext::FieldContext);
        assert_eq!(partial, "na");
    }

    // Tests for find_char_before_field_access helper
    #[test]
    fn test_char_before_field_after_pipe() {
        // `.services | .` - should find '|'
        assert_eq!(
            find_char_before_field_access(".services | .", ""),
            Some('|')
        );
        // `.services | .ser` - should find '|' (go back past partial)
        assert_eq!(
            find_char_before_field_access(".services | .ser", "ser"),
            Some('|')
        );
    }

    #[test]
    fn test_char_before_field_after_dot() {
        // `.services.` - should find 's' (last char of identifier)
        assert_eq!(find_char_before_field_access(".services.", ""), Some('s'));
        // `.services.na` - should find 's' (go back past partial and dot)
        assert_eq!(
            find_char_before_field_access(".services.na", "na"),
            Some('s')
        );
    }

    #[test]
    fn test_char_before_field_after_brackets() {
        // `.services[].` - should find ']'
        assert_eq!(find_char_before_field_access(".services[].", ""), Some(']'));
        // `.services[0].` - should find ']'
        assert_eq!(
            find_char_before_field_access(".services[0].", ""),
            Some(']')
        );
    }

    #[test]
    fn test_char_before_field_after_question() {
        // `.services?.` - should find '?'
        assert_eq!(find_char_before_field_access(".services?.", ""), Some('?'));
        // `.services?.na` - should find '?'
        assert_eq!(
            find_char_before_field_access(".services?.na", "na"),
            Some('?')
        );
    }

    #[test]
    fn test_char_before_field_in_constructors() {
        // `[.` - should find '['
        assert_eq!(find_char_before_field_access("[.", ""), Some('['));
        // `[.a, .` - should find ','
        assert_eq!(find_char_before_field_access("[.a, .", ""), Some(','));
        // `{name: .` - should find ':'
        assert_eq!(find_char_before_field_access("{name: .", ""), Some(':'));
        // `{.` - should find '{'
        assert_eq!(find_char_before_field_access("{.", ""), Some('{'));
    }

    #[test]
    fn test_char_before_field_in_functions() {
        // `map(.` - should find '('
        assert_eq!(find_char_before_field_access("map(.", ""), Some('('));
        // `select(.active).` - should find ')'
        assert_eq!(
            find_char_before_field_access("select(.active).", ""),
            Some(')')
        );
    }

    #[test]
    fn test_char_before_field_with_semicolon() {
        // `.a; .` - should find ';'
        assert_eq!(find_char_before_field_access(".a; .", ""), Some(';'));
    }

    #[test]
    fn test_char_before_field_at_start() {
        // `.` at start - should return None
        assert_eq!(find_char_before_field_access(".", ""), None);
        // `.na` at start - should return None
        assert_eq!(find_char_before_field_access(".na", "na"), None);
    }

    #[test]
    fn test_analyze_context_after_optional_array() {
        // After []?. should be FieldContext
        let query = ".services[].capacityProviderStrategy[]?.";
        let tracker = tracker_for(query);
        let (ctx, partial) = analyze_context(query, &tracker);
        assert_eq!(ctx, SuggestionContext::FieldContext);
        assert_eq!(partial, "");

        // After []?.b should be FieldContext with partial "b"
        let query = ".services[].capacityProviderStrategy[]?.b";
        let tracker = tracker_for(query);
        let (ctx, partial) = analyze_context(query, &tracker);
        assert_eq!(ctx, SuggestionContext::FieldContext);
        assert_eq!(partial, "b");
    }

    #[test]
    fn test_analyze_context_jq_keywords() {
        // jq keywords like "if", "then", "else" should be FunctionContext
        let tracker = tracker_for("if");
        let (ctx, partial) = analyze_context("if", &tracker);
        assert_eq!(ctx, SuggestionContext::FunctionContext);
        assert_eq!(partial, "if");

        let tracker = tracker_for("then");
        let (ctx, partial) = analyze_context("then", &tracker);
        assert_eq!(ctx, SuggestionContext::FunctionContext);
        assert_eq!(partial, "then");

        let tracker = tracker_for("else");
        let (ctx, partial) = analyze_context("else", &tracker);
        assert_eq!(ctx, SuggestionContext::FunctionContext);
        assert_eq!(partial, "else");

        // Partial keywords
        let tracker = tracker_for("i");
        let (ctx, partial) = analyze_context("i", &tracker);
        assert_eq!(ctx, SuggestionContext::FunctionContext);
        assert_eq!(partial, "i");
    }

    // ========== ObjectKeyContext Unit Tests ==========

    #[test]
    fn test_object_key_context_after_open_brace() {
        // `{na` should return ObjectKeyContext
        let query = "{na";
        let tracker = tracker_for(query);
        let (ctx, partial) = analyze_context(query, &tracker);
        assert_eq!(ctx, SuggestionContext::ObjectKeyContext);
        assert_eq!(partial, "na");
    }

    #[test]
    fn test_object_key_context_after_comma() {
        // `{name: .name, ag` should return ObjectKeyContext
        let query = "{name: .name, ag";
        let tracker = tracker_for(query);
        let (ctx, partial) = analyze_context(query, &tracker);
        assert_eq!(ctx, SuggestionContext::ObjectKeyContext);
        assert_eq!(partial, "ag");
    }

    #[test]
    fn test_array_context_not_object_key() {
        // `[1, na` should NOT return ObjectKeyContext
        let query = "[1, na";
        let tracker = tracker_for(query);
        let (ctx, partial) = analyze_context(query, &tracker);
        assert_ne!(ctx, SuggestionContext::ObjectKeyContext);
        assert_eq!(partial, "na");
        // Should be FunctionContext since it's not a field access
        assert_eq!(ctx, SuggestionContext::FunctionContext);
    }

    #[test]
    fn test_function_call_context_not_object_key() {
        // `select(.a, na` should NOT return ObjectKeyContext
        let query = "select(.a, na";
        let tracker = tracker_for(query);
        let (ctx, partial) = analyze_context(query, &tracker);
        assert_ne!(ctx, SuggestionContext::ObjectKeyContext);
        assert_eq!(partial, "na");
        // Should be FunctionContext since it's inside a function call
        assert_eq!(ctx, SuggestionContext::FunctionContext);
    }

    #[test]
    fn test_nested_object_in_array() {
        // `[{na` should return ObjectKeyContext (innermost is object)
        let query = "[{na";
        let tracker = tracker_for(query);
        let (ctx, partial) = analyze_context(query, &tracker);
        assert_eq!(ctx, SuggestionContext::ObjectKeyContext);
        assert_eq!(partial, "na");
    }

    #[test]
    fn test_nested_array_in_object() {
        // `{items: [na` should NOT return ObjectKeyContext (innermost is array)
        let query = "{items: [na";
        let tracker = tracker_for(query);
        let (ctx, partial) = analyze_context(query, &tracker);
        assert_ne!(ctx, SuggestionContext::ObjectKeyContext);
        assert_eq!(partial, "na");
        // Should be FunctionContext since innermost context is array
        assert_eq!(ctx, SuggestionContext::FunctionContext);
    }

    #[test]
    fn test_object_key_empty_partial_no_suggestions() {
        // `{` alone should NOT return ObjectKeyContext (no partial)
        let query = "{";
        let tracker = tracker_for(query);
        let (ctx, partial) = analyze_context(query, &tracker);
        // With empty partial, we don't trigger ObjectKeyContext per requirement 1.3
        assert_ne!(ctx, SuggestionContext::ObjectKeyContext);
        assert_eq!(partial, "");
    }

    #[test]
    fn test_object_key_after_comma_empty_partial() {
        // `{name: .name, ` should NOT return ObjectKeyContext (no partial)
        let query = "{name: .name, ";
        let tracker = tracker_for(query);
        let (ctx, _partial) = analyze_context(query, &tracker);
        // With empty partial, we don't trigger ObjectKeyContext
        assert_ne!(ctx, SuggestionContext::ObjectKeyContext);
    }

    #[test]
    fn test_dot_after_brace_is_field_context() {
        // `{.na` should return FieldContext (not ObjectKeyContext)
        let query = "{.na";
        let tracker = tracker_for(query);
        let (ctx, partial) = analyze_context(query, &tracker);
        assert_eq!(ctx, SuggestionContext::FieldContext);
        assert_eq!(partial, "na");
    }

    #[test]
    fn test_object_key_with_complex_value() {
        // `{name: .name | map(.), ag` should return ObjectKeyContext
        let query = "{name: .name | map(.), ag";
        let tracker = tracker_for(query);
        let (ctx, partial) = analyze_context(query, &tracker);
        assert_eq!(ctx, SuggestionContext::ObjectKeyContext);
        assert_eq!(partial, "ag");
    }

    #[test]
    fn test_deeply_nested_object_context() {
        // `{a: {b: {c` should return ObjectKeyContext
        let query = "{a: {b: {c";
        let tracker = tracker_for(query);
        let (ctx, partial) = analyze_context(query, &tracker);
        assert_eq!(ctx, SuggestionContext::ObjectKeyContext);
        assert_eq!(partial, "c");
    }

    // ========== Regression Tests for Existing Behavior ==========
    // These tests verify that the ObjectKeyContext feature doesn't break
    // existing FieldContext and FunctionContext behavior.
    // Requirements: 4.1, 4.2, 4.3, 4.4, 4.5

    #[test]
    fn test_regression_field_context_at_start() {
        // `.na` at start should return FieldContext (not ObjectKeyContext)
        // Validates: Requirement 4.1
        let query = ".na";
        let tracker = tracker_for(query);
        let (ctx, partial) = analyze_context(query, &tracker);
        assert_eq!(ctx, SuggestionContext::FieldContext);
        assert_ne!(ctx, SuggestionContext::ObjectKeyContext);
        assert_eq!(partial, "na");
    }

    #[test]
    fn test_regression_field_context_after_pipe() {
        // `.services | .na` should return FieldContext
        // Validates: Requirement 4.2
        let query = ".services | .na";
        let tracker = tracker_for(query);
        let (ctx, partial) = analyze_context(query, &tracker);
        assert_eq!(ctx, SuggestionContext::FieldContext);
        assert_ne!(ctx, SuggestionContext::ObjectKeyContext);
        assert_eq!(partial, "na");
    }

    #[test]
    fn test_regression_field_context_in_map() {
        // `map(.na` should return FieldContext
        // Validates: Requirement 4.3
        let query = "map(.na";
        let tracker = tracker_for(query);
        let (ctx, partial) = analyze_context(query, &tracker);
        assert_eq!(ctx, SuggestionContext::FieldContext);
        assert_ne!(ctx, SuggestionContext::ObjectKeyContext);
        assert_eq!(partial, "na");
    }

    #[test]
    fn test_regression_function_context_at_start() {
        // `sel` at start should return FunctionContext
        // Validates: Requirement 4.4
        let query = "sel";
        let tracker = tracker_for(query);
        let (ctx, partial) = analyze_context(query, &tracker);
        assert_eq!(ctx, SuggestionContext::FunctionContext);
        assert_ne!(ctx, SuggestionContext::ObjectKeyContext);
        assert_eq!(partial, "sel");
    }

    #[test]
    fn test_regression_function_context_after_pipe() {
        // `.services | sel` should return FunctionContext
        // Validates: Requirement 4.5
        let query = ".services | sel";
        let tracker = tracker_for(query);
        let (ctx, partial) = analyze_context(query, &tracker);
        assert_eq!(ctx, SuggestionContext::FunctionContext);
        assert_ne!(ctx, SuggestionContext::ObjectKeyContext);
        assert_eq!(partial, "sel");
    }

    // ========== Property-Based Tests ==========

    proptest! {
        /// **Feature: object-key-autocomplete, Property 1: ObjectKeyContext suggestions have no leading dot**
        /// **Validates: Requirements 1.1, 1.2**
        ///
        /// For any query where the cursor is in ObjectKeyContext (after `{` or `,` inside
        /// an object literal with a partial typed), all returned suggestions SHALL NOT
        /// start with a `.` character.
        #[test]
        fn prop_object_key_context_suggestions_no_leading_dot(
            partial in "[a-z]{1,10}",
            field_names in prop::collection::vec("[a-z]{1,10}", 1..5),
        ) {
            use crate::query::ResultType;

            // Build a query that triggers ObjectKeyContext: `{<partial>`
            let query = format!("{{{}", partial);
            let tracker = tracker_for(&query);

            // Build a mock JSON result with the field names
            let json_fields: Vec<String> = field_names
                .iter()
                .map(|name| format!("\"{}\": \"value\"", name))
                .collect();
            let json_result = format!("{{{}}}", json_fields.join(", "));

            // Get suggestions
            let suggestions = get_suggestions(
                &query,
                query.len(),
                Some(&json_result),
                Some(ResultType::Object),
                &tracker,
            );

            // All suggestions should NOT start with a dot
            for suggestion in &suggestions {
                prop_assert!(
                    !suggestion.text.starts_with('.'),
                    "ObjectKeyContext suggestion '{}' should NOT start with '.', query: '{}'",
                    suggestion.text,
                    query
                );
            }
        }

        /// **Feature: object-key-autocomplete, Property 1: ObjectKeyContext suggestions have no leading dot**
        /// **Validates: Requirements 1.1, 1.2**
        ///
        /// For any query with comma inside object context, suggestions should not have leading dot.
        #[test]
        fn prop_object_key_context_after_comma_no_leading_dot(
            first_key in "[a-z]{1,8}",
            partial in "[a-z]{1,10}",
            field_names in prop::collection::vec("[a-z]{1,10}", 1..5),
        ) {
            use crate::query::ResultType;

            // Build a query that triggers ObjectKeyContext after comma: `{key: .key, <partial>`
            let query = format!("{{{}: .{}, {}", first_key, first_key, partial);
            let tracker = tracker_for(&query);

            // Build a mock JSON result with the field names
            let json_fields: Vec<String> = field_names
                .iter()
                .map(|name| format!("\"{}\": \"value\"", name))
                .collect();
            let json_result = format!("{{{}}}", json_fields.join(", "));

            // Get suggestions
            let suggestions = get_suggestions(
                &query,
                query.len(),
                Some(&json_result),
                Some(ResultType::Object),
                &tracker,
            );

            // All suggestions should NOT start with a dot
            for suggestion in &suggestions {
                prop_assert!(
                    !suggestion.text.starts_with('.'),
                    "ObjectKeyContext suggestion '{}' should NOT start with '.', query: '{}'",
                    suggestion.text,
                    query
                );
            }
        }

        /// **Feature: object-key-autocomplete, Property 2: Non-object contexts never return ObjectKeyContext**
        /// **Validates: Requirements 2.1, 2.2**
        ///
        /// For any query where the innermost unclosed brace is `[` (array) or `(` (paren),
        /// the analyze_context() function shall NOT return ObjectKeyContext.
        #[test]
        fn prop_non_object_contexts_never_return_object_key_context(
            prefix in "[a-z.| ]*",
            partial in "[a-z]{1,10}",
            brace_type in prop_oneof![Just('['), Just('(')],
        ) {
            // Build a query that ends with an array or paren context followed by a partial
            // Examples: "[na", "select(na", ".items | [na", "map(na"
            let query = format!("{}{}{}", prefix, brace_type, partial);

            let tracker = tracker_for(&query);
            let (ctx, _) = analyze_context(&query, &tracker);

            // Should never be ObjectKeyContext when inside array or paren
            prop_assert_ne!(
                ctx,
                SuggestionContext::ObjectKeyContext,
                "Query '{}' with innermost brace '{}' should NOT return ObjectKeyContext, got {:?}",
                query,
                brace_type,
                ctx
            );
        }

        /// **Feature: object-key-autocomplete, Property 2: Non-object contexts never return ObjectKeyContext**
        /// **Validates: Requirements 2.1, 2.2**
        ///
        /// For any query with comma inside array or paren context,
        /// the analyze_context() function shall NOT return ObjectKeyContext.
        #[test]
        fn prop_comma_in_non_object_context_not_object_key(
            prefix in "[a-z.| ]*",
            inner in "[a-z0-9., ]{0,20}",
            partial in "[a-z]{1,10}",
            brace_type in prop_oneof![Just('['), Just('(')],
        ) {
            // Build a query with comma inside array or paren
            // Examples: "[1, na", "select(.a, na", ".items | [.x, na"
            let query = format!("{}{}{}, {}", prefix, brace_type, inner, partial);

            let tracker = tracker_for(&query);
            let (ctx, _) = analyze_context(&query, &tracker);

            // Should never be ObjectKeyContext when comma is inside array or paren
            prop_assert_ne!(
                ctx,
                SuggestionContext::ObjectKeyContext,
                "Query '{}' with comma inside '{}' should NOT return ObjectKeyContext, got {:?}",
                query,
                brace_type,
                ctx
            );
        }

        /// **Feature: object-key-autocomplete, Property 6: Existing FieldContext behavior preserved**
        /// **Validates: Requirements 4.1, 4.2, 4.3**
        ///
        /// For any query starting with `.` followed by a partial (e.g., `.na`),
        /// the analyze_context() function SHALL return FieldContext, not ObjectKeyContext.
        #[test]
        fn prop_field_context_preserved_at_start(
            partial in "[a-z]{1,10}",
        ) {
            // Build a query that starts with dot followed by partial: `.na`
            let query = format!(".{}", partial);
            let tracker = tracker_for(&query);
            let (ctx, returned_partial) = analyze_context(&query, &tracker);

            // Should always be FieldContext, never ObjectKeyContext
            prop_assert_eq!(
                ctx,
                SuggestionContext::FieldContext,
                "Query '{}' starting with '.' should return FieldContext, got {:?}",
                query,
                ctx
            );

            // The partial should match what we typed
            prop_assert!(
                returned_partial == partial,
                "Query '{}' should return partial '{}', got '{}'",
                query,
                partial,
                returned_partial
            );
        }

        /// **Feature: object-key-autocomplete, Property 6: Existing FieldContext behavior preserved**
        /// **Validates: Requirements 4.1, 4.2, 4.3**
        ///
        /// For any query with pipe followed by dot and partial (e.g., `.services | .na`),
        /// the analyze_context() function SHALL return FieldContext.
        #[test]
        fn prop_field_context_preserved_after_pipe(
            field1 in "[a-z]{1,8}",
            partial in "[a-z]{1,10}",
        ) {
            // Build a query like `.services | .na`
            let query = format!(".{} | .{}", field1, partial);
            let tracker = tracker_for(&query);
            let (ctx, returned_partial) = analyze_context(&query, &tracker);

            // Should always be FieldContext
            prop_assert_eq!(
                ctx,
                SuggestionContext::FieldContext,
                "Query '{}' with pipe and dot should return FieldContext, got {:?}",
                query,
                ctx
            );

            // The partial should match what we typed after the last dot
            prop_assert!(
                returned_partial == partial,
                "Query '{}' should return partial '{}', got '{}'",
                query,
                partial,
                returned_partial
            );
        }

        /// **Feature: object-key-autocomplete, Property 6: Existing FieldContext behavior preserved**
        /// **Validates: Requirements 4.1, 4.2, 4.3**
        ///
        /// For any query with function call containing dot field access (e.g., `map(.na`),
        /// the analyze_context() function SHALL return FieldContext.
        #[test]
        fn prop_field_context_preserved_in_function_call(
            func in "(map|select|sort_by|group_by|unique_by|min_by|max_by)",
            partial in "[a-z]{1,10}",
        ) {
            // Build a query like `map(.na`
            let query = format!("{}(.{}", func, partial);
            let tracker = tracker_for(&query);
            let (ctx, returned_partial) = analyze_context(&query, &tracker);

            // Should always be FieldContext
            prop_assert_eq!(
                ctx,
                SuggestionContext::FieldContext,
                "Query '{}' with function call and dot should return FieldContext, got {:?}",
                query,
                ctx
            );

            // The partial should match what we typed after the dot
            prop_assert!(
                returned_partial == partial,
                "Query '{}' should return partial '{}', got '{}'",
                query,
                partial,
                returned_partial
            );
        }

        /// **Feature: object-key-autocomplete, Property 7: Existing FunctionContext behavior preserved**
        /// **Validates: Requirements 4.4, 4.5**
        ///
        /// For any query with a partial identifier not preceded by `.` and not inside object braces
        /// (e.g., `sel`), the analyze_context() function SHALL return FunctionContext.
        #[test]
        fn prop_function_context_preserved_at_start(
            partial in "[a-z]{1,10}",
        ) {
            // Build a query that is just a partial function name: `sel`
            let query = partial.clone();
            let tracker = tracker_for(&query);
            let (ctx, returned_partial) = analyze_context(&query, &tracker);

            // Should always be FunctionContext
            prop_assert_eq!(
                ctx,
                SuggestionContext::FunctionContext,
                "Query '{}' (bare identifier) should return FunctionContext, got {:?}",
                query,
                ctx
            );

            // The partial should match what we typed
            prop_assert!(
                returned_partial == partial,
                "Query '{}' should return partial '{}', got '{}'",
                query,
                partial,
                returned_partial
            );
        }

        /// **Feature: object-key-autocomplete, Property 7: Existing FunctionContext behavior preserved**
        /// **Validates: Requirements 4.4, 4.5**
        ///
        /// For any query with pipe followed by a partial identifier (e.g., `.services | sel`),
        /// the analyze_context() function SHALL return FunctionContext.
        #[test]
        fn prop_function_context_preserved_after_pipe(
            field in "[a-z]{1,8}",
            partial in "[a-z]{1,10}",
        ) {
            // Build a query like `.services | sel`
            let query = format!(".{} | {}", field, partial);
            let tracker = tracker_for(&query);
            let (ctx, returned_partial) = analyze_context(&query, &tracker);

            // Should always be FunctionContext
            prop_assert_eq!(
                ctx,
                SuggestionContext::FunctionContext,
                "Query '{}' with pipe and bare identifier should return FunctionContext, got {:?}",
                query,
                ctx
            );

            // The partial should match what we typed after the pipe
            prop_assert!(
                returned_partial == partial,
                "Query '{}' should return partial '{}', got '{}'",
                query,
                partial,
                returned_partial
            );
        }
    }
}