jiq 3.22.1

Interactive JSON query tool with real-time output
Documentation
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
//! AI suggestion selection integration tests

use super::*;
use proptest::prelude::*;

#[test]
fn test_ai_suggestion_selection_complete_flow() {
    // Test complete flow: AI response → navigation → apply → execute
    use crate::ai::suggestion::{Suggestion, SuggestionType};

    let mut app = app_with_query(".initial");
    app.focus = Focus::InputField;
    app.input.editor_mode = EditorMode::Insert;

    // Set up AI popup with suggestions
    app.ai.visible = true;
    app.ai.enabled = true;
    app.ai.suggestions = vec![
        Suggestion {
            query: ".name".to_string(),
            description: "Get name field".to_string(),
            suggestion_type: SuggestionType::Next,
        },
        Suggestion {
            query: ".value".to_string(),
            description: "Get value field".to_string(),
            suggestion_type: SuggestionType::Optimize,
        },
    ];

    // Navigate down to select first suggestion
    app.handle_key_event(key_with_mods(KeyCode::Down, KeyModifiers::ALT));
    assert_eq!(app.ai.selection.get_selected(), Some(0));
    assert!(app.ai.selection.is_navigation_active());

    // Press Enter to apply
    app.handle_key_event(key(KeyCode::Enter));

    // Query should be replaced
    assert_eq!(app.query(), ".name");

    // Selection should be cleared
    assert!(app.ai.selection.get_selected().is_none());
    assert!(!app.ai.selection.is_navigation_active());

    // Should NOT quit
    assert!(!app.should_quit);

    // Query should have been executed (result should be updated)
    assert!(app.query.as_ref().unwrap().result.is_ok());
}

#[test]
fn test_ai_suggestion_direct_selection_alt_1() {
    // Test direct selection with Alt+1
    use crate::ai::suggestion::{Suggestion, SuggestionType};

    let mut app = app_with_query(".initial");
    app.focus = Focus::InputField;

    // Set up AI popup with suggestions
    app.ai.visible = true;
    app.ai.enabled = true;
    app.ai.suggestions = vec![
        Suggestion {
            query: ".first".to_string(),
            description: "First suggestion".to_string(),
            suggestion_type: SuggestionType::Next,
        },
        Suggestion {
            query: ".second".to_string(),
            description: "Second suggestion".to_string(),
            suggestion_type: SuggestionType::Fix,
        },
    ];

    // Press Alt+1 to select first suggestion
    app.handle_key_event(key_with_mods(KeyCode::Char('1'), KeyModifiers::ALT));

    // Query should be replaced with first suggestion
    assert_eq!(app.query(), ".first");
    assert!(!app.should_quit);
}

#[test]
fn test_ai_suggestion_direct_selection_alt_2() {
    // Test direct selection with Alt+2
    use crate::ai::suggestion::{Suggestion, SuggestionType};

    let mut app = app_with_query(".initial");
    app.focus = Focus::InputField;

    // Set up AI popup with suggestions
    app.ai.visible = true;
    app.ai.enabled = true;
    app.ai.suggestions = vec![
        Suggestion {
            query: ".first".to_string(),
            description: "First suggestion".to_string(),
            suggestion_type: SuggestionType::Next,
        },
        Suggestion {
            query: ".second".to_string(),
            description: "Second suggestion".to_string(),
            suggestion_type: SuggestionType::Fix,
        },
    ];

    // Press Alt+2 to select second suggestion
    app.handle_key_event(key_with_mods(KeyCode::Char('2'), KeyModifiers::ALT));

    // Query should be replaced with second suggestion
    assert_eq!(app.query(), ".second");
    assert!(!app.should_quit);
}

#[test]
fn test_ai_suggestion_multiple_selections_in_sequence() {
    // Test multiple selections in sequence
    use crate::ai::suggestion::{Suggestion, SuggestionType};

    let mut app = app_with_query(".initial");
    app.focus = Focus::InputField;

    // Set up AI popup with suggestions
    app.ai.visible = true;
    app.ai.enabled = true;
    app.ai.suggestions = vec![
        Suggestion {
            query: ".first".to_string(),
            description: "First suggestion".to_string(),
            suggestion_type: SuggestionType::Next,
        },
        Suggestion {
            query: ".second".to_string(),
            description: "Second suggestion".to_string(),
            suggestion_type: SuggestionType::Fix,
        },
    ];

    // First selection: Alt+1
    app.handle_key_event(key_with_mods(KeyCode::Char('1'), KeyModifiers::ALT));
    assert_eq!(app.query(), ".first");

    // Second selection: Alt+2
    app.handle_key_event(key_with_mods(KeyCode::Char('2'), KeyModifiers::ALT));
    assert_eq!(app.query(), ".second");

    // Third selection via navigation: Alt+Down + Enter
    app.handle_key_event(key_with_mods(KeyCode::Down, KeyModifiers::ALT));
    app.handle_key_event(key(KeyCode::Enter));
    assert_eq!(app.query(), ".first"); // Wraps to first

    assert!(!app.should_quit);
}

#[test]
fn test_ai_suggestion_selection_hides_autocomplete() {
    // Test that selecting AI suggestion hides autocomplete
    use crate::ai::suggestion::{Suggestion, SuggestionType};

    let mut app = app_with_query(".na");
    app.focus = Focus::InputField;
    app.input.editor_mode = EditorMode::Insert;

    // Set up autocomplete
    let autocomplete_suggestions = vec![crate::autocomplete::Suggestion::new(
        "name",
        crate::autocomplete::SuggestionType::Field,
    )];
    app.autocomplete
        .update_suggestions(autocomplete_suggestions);
    assert!(app.autocomplete.is_visible());

    // Set up AI popup with suggestions
    app.ai.visible = true;
    app.ai.enabled = true;
    app.ai.suggestions = vec![Suggestion {
        query: ".name | length".to_string(),
        description: "Get name length".to_string(),
        suggestion_type: SuggestionType::Next,
    }];

    // Press Alt+1 to select AI suggestion
    app.handle_key_event(key_with_mods(KeyCode::Char('1'), KeyModifiers::ALT));

    // Query should be replaced
    assert_eq!(app.query(), ".name | length");

    // Autocomplete should be hidden
    assert!(!app.autocomplete.is_visible());
}

#[test]
fn test_ai_suggestion_selection_ignored_when_popup_hidden() {
    // Test that selection is ignored when AI popup is hidden
    use crate::ai::suggestion::{Suggestion, SuggestionType};

    let mut app = app_with_query(".initial");
    app.focus = Focus::InputField;

    // AI popup is hidden
    app.ai.visible = false;
    app.ai.enabled = true;
    app.ai.suggestions = vec![Suggestion {
        query: ".should_not_apply".to_string(),
        description: "Should not apply".to_string(),
        suggestion_type: SuggestionType::Next,
    }];

    // Press Alt+1 - should be ignored
    app.handle_key_event(key_with_mods(KeyCode::Char('1'), KeyModifiers::ALT));

    // Query should be unchanged
    assert_eq!(app.query(), ".initial");
}

#[test]
fn test_ai_suggestion_selection_ignored_when_no_suggestions() {
    // Test that selection is ignored when no suggestions
    let mut app = app_with_query(".initial");
    app.focus = Focus::InputField;

    // AI popup is visible but has no suggestions
    app.ai.visible = true;
    app.ai.enabled = true;
    app.ai.suggestions = vec![];

    // Press Alt+1 - should be ignored
    app.handle_key_event(key_with_mods(KeyCode::Char('1'), KeyModifiers::ALT));

    // Query should be unchanged
    assert_eq!(app.query(), ".initial");
}

#[test]
fn test_ai_suggestion_invalid_selection_ignored() {
    // Test that invalid selection (Ctrl+3 when only 2 suggestions) is ignored
    use crate::ai::suggestion::{Suggestion, SuggestionType};

    let mut app = app_with_query(".initial");
    app.focus = Focus::InputField;

    // Set up AI popup with only 2 suggestions
    app.ai.visible = true;
    app.ai.enabled = true;
    app.ai.suggestions = vec![
        Suggestion {
            query: ".first".to_string(),
            description: "First".to_string(),
            suggestion_type: SuggestionType::Next,
        },
        Suggestion {
            query: ".second".to_string(),
            description: "Second".to_string(),
            suggestion_type: SuggestionType::Fix,
        },
    ];

    // Press Alt+3 - should be ignored (only 2 suggestions)
    app.handle_key_event(key_with_mods(KeyCode::Char('3'), KeyModifiers::ALT));

    // Query should be unchanged
    assert_eq!(app.query(), ".initial");
}

#[test]
fn test_ai_suggestion_navigation_stops_at_boundary() {
    // Test navigation boundary behavior (no wrap-around)
    use crate::ai::suggestion::{Suggestion, SuggestionType};

    let mut app = app_with_query(".initial");
    app.focus = Focus::InputField;

    // Set up AI popup with 3 suggestions
    app.ai.visible = true;
    app.ai.enabled = true;
    app.ai.suggestions = vec![
        Suggestion {
            query: ".first".to_string(),
            description: "First".to_string(),
            suggestion_type: SuggestionType::Next,
        },
        Suggestion {
            query: ".second".to_string(),
            description: "Second".to_string(),
            suggestion_type: SuggestionType::Fix,
        },
        Suggestion {
            query: ".third".to_string(),
            description: "Third".to_string(),
            suggestion_type: SuggestionType::Optimize,
        },
    ];

    // Navigate down 4 times (should stop at last: 0 -> 1 -> 2 -> 2)
    app.handle_key_event(key_with_mods(KeyCode::Down, KeyModifiers::ALT)); // 0
    app.handle_key_event(key_with_mods(KeyCode::Down, KeyModifiers::ALT)); // 1
    app.handle_key_event(key_with_mods(KeyCode::Down, KeyModifiers::ALT)); // 2
    app.handle_key_event(key_with_mods(KeyCode::Down, KeyModifiers::ALT)); // 2 (stays at last)

    assert_eq!(app.ai.selection.get_selected(), Some(2));

    // Press Enter to apply
    app.handle_key_event(key(KeyCode::Enter));
    assert_eq!(app.query(), ".third");
}

#[test]
fn test_ai_suggestion_enter_without_navigation_exits() {
    // Test that Enter without navigation exits the app
    use crate::ai::suggestion::{Suggestion, SuggestionType};

    let mut app = app_with_query(".");
    app.focus = Focus::InputField;

    // Set up AI popup with suggestions but NO navigation
    app.ai.visible = true;
    app.ai.enabled = true;
    app.ai.suggestions = vec![Suggestion {
        query: ".name".to_string(),
        description: "Get name".to_string(),
        suggestion_type: SuggestionType::Next,
    }];

    // Ensure no navigation has occurred
    assert!(!app.ai.selection.is_navigation_active());

    // Press Enter - should exit app (not apply suggestion)
    app.handle_key_event(key(KeyCode::Enter));

    // Should quit with Results output mode
    assert!(app.should_quit);
    assert_eq!(app.output_mode, Some(OutputMode::Results));

    // Query should be unchanged
    assert_eq!(app.query(), ".");
}

// **Feature: ai-assistant-phase3, Property 7: Mode independence**
// *For any* editor mode (Normal, Insert, Operator) and any suggestion, applying the
// suggestion should replace the query input and preserve the current editor mode unchanged.
// **Validates: Requirements 6.1, 6.2, 6.3, 6.4**
proptest! {
    #![proptest_config(ProptestConfig::with_cases(100))]

    #[test]
    fn prop_mode_independence_for_ai_suggestion_selection(
        // Test with different editor modes
        mode_idx in 0usize..3,
        // Test with different selection methods (Alt+1-5)
        selection_digit in 1usize..=5,
        // Test with different suggestion queries
        suggestion_query in prop_oneof![
            Just(".name"),
            Just(".value"),
            Just(".items[]"),
            Just(".users | length"),
        ],
    ) {
        use crate::ai::suggestion::{Suggestion, SuggestionType};

        let mut app = app_with_query(".existing");
        app.focus = Focus::InputField;

        // Set editor mode based on index
        let mode = match mode_idx {
            0 => EditorMode::Normal,
            1 => EditorMode::Insert,
            _ => EditorMode::Operator('d'),
        };
        app.input.editor_mode = mode;

        // Set up AI popup with suggestions
        app.ai.visible = true;
        app.ai.enabled = true;
        app.ai.suggestions = (0..5).map(|i| Suggestion {
            query: if i == selection_digit - 1 {
                suggestion_query.to_string()
            } else {
                format!(".field{}", i)
            },
            description: format!("Description {}", i),
            suggestion_type: SuggestionType::Next,
        }).collect();

        // Only test if selection is valid
        prop_assume!(selection_digit <= app.ai.suggestions.len());

        // Press Alt+N to select suggestion
        let key = key_with_mods(
            KeyCode::Char(char::from_digit(selection_digit as u32, 10).unwrap()),
            KeyModifiers::ALT,
        );
        app.handle_key_event(key);

        // Property 1: Query should be replaced with suggestion
        prop_assert_eq!(
            app.query(),
            suggestion_query,
            "Query should be replaced with selected suggestion"
        );

        // Property 2: Editor mode should be preserved
        prop_assert_eq!(
            app.input.editor_mode,
            mode,
            "Editor mode should be preserved after suggestion selection"
        );

        // Property 3: Should NOT quit
        prop_assert!(
            !app.should_quit,
            "Should not quit after selecting AI suggestion"
        );
    }

    #[test]
    fn prop_mode_independence_for_ai_navigation_selection(
        // Test with different editor modes
        mode_idx in 0usize..3,
        // Test with different navigation steps
        nav_steps in 1usize..5,
        // Test with different suggestion queries
        suggestion_query in prop_oneof![
            Just(".name"),
            Just(".value"),
            Just(".items[]"),
        ],
    ) {
        use crate::ai::suggestion::{Suggestion, SuggestionType};

        let mut app = app_with_query(".existing");
        app.focus = Focus::InputField;

        // Set editor mode based on index
        let mode = match mode_idx {
            0 => EditorMode::Normal,
            1 => EditorMode::Insert,
            _ => EditorMode::Operator('d'),
        };
        app.input.editor_mode = mode;

        // Set up AI popup with suggestions
        app.ai.visible = true;
        app.ai.enabled = true;
        app.ai.suggestions = vec![
            Suggestion {
                query: suggestion_query.to_string(),
                description: "First suggestion".to_string(),
                suggestion_type: SuggestionType::Next,
            },
            Suggestion {
                query: ".other".to_string(),
                description: "Second suggestion".to_string(),
                suggestion_type: SuggestionType::Fix,
            },
        ];

        // Navigate with Alt+Down
        for _ in 0..nav_steps {
            app.handle_key_event(key_with_mods(KeyCode::Down, KeyModifiers::ALT));
        }

        // Press Enter to apply
        app.handle_key_event(key(KeyCode::Enter));

        // Property 1: Query should be replaced with one of the suggestions
        let query = app.query();
        prop_assert!(
            query == suggestion_query || query == ".other",
            "Query '{}' should be one of the suggestions",
            query
        );

        // Property 2: Editor mode should be preserved
        prop_assert_eq!(
            app.input.editor_mode,
            mode,
            "Editor mode should be preserved after navigation selection"
        );

        // Property 3: Should NOT quit
        prop_assert!(
            !app.should_quit,
            "Should not quit after selecting AI suggestion via navigation"
        );
    }
}

#[test]
fn test_ctrl_a_triggers_ai_request_when_becoming_visible() {
    // Test that pressing Ctrl+A triggers an AI request when popup becomes visible
    // This validates the fix for enabled=false but configured=true scenario
    let mut app = app_with_query(".");
    app.focus = Focus::InputField;

    // Start with AI hidden but configured (simulating enabled=false in config)
    app.ai.visible = false;
    app.ai.enabled = false;
    app.ai.configured = true;

    // Set up AI channel (simulating worker is running because configured=true)
    let (tx, rx) = std::sync::mpsc::channel();
    let (_response_tx, response_rx) = std::sync::mpsc::channel();
    app.ai.request_tx = Some(tx);
    app.ai.response_rx = Some(response_rx);

    // Set initial query hash to ensure query appears changed
    app.ai.set_last_query_hash(".initial");

    // Press Ctrl+A to show AI box
    app.handle_key_event(key_with_mods(KeyCode::Char('a'), KeyModifiers::CONTROL));

    // Verify popup is now visible
    assert!(app.ai.visible, "AI popup should be visible after Ctrl+A");

    // Verify AI request was sent
    let mut found_request = false;
    while let Ok(msg) = rx.try_recv() {
        if matches!(msg, crate::ai::ai_state::AiRequest::Query { .. }) {
            found_request = true;
            break;
        }
    }
    assert!(
        found_request,
        "Should have sent AI request when popup became visible"
    );
}

#[test]
fn test_ctrl_a_no_request_when_not_configured() {
    // Test that pressing Ctrl+A does NOT trigger AI request when not configured
    let mut app = app_with_query(".");
    app.focus = Focus::InputField;

    // Start with AI hidden and NOT configured
    app.ai.visible = false;
    app.ai.enabled = false;
    app.ai.configured = false;

    // No channel setup (no worker running)
    app.ai.request_tx = None;
    app.ai.response_rx = None;

    // Press Ctrl+A to show AI box
    app.handle_key_event(key_with_mods(KeyCode::Char('a'), KeyModifiers::CONTROL));

    // Verify popup is visible (toggle still works)
    assert!(app.ai.visible, "AI popup should be visible after Ctrl+A");

    // No request was sent (because not configured, so no crash)
    // This test mainly verifies no panic occurs
}

#[test]
fn test_ctrl_a_toggles_off_no_request() {
    // Test that toggling AI popup OFF does not trigger a request
    let mut app = app_with_query(".");
    app.focus = Focus::InputField;

    // Start with AI visible and configured
    app.ai.visible = true;
    app.ai.enabled = true;
    app.ai.configured = true;

    // Set up AI channel
    let (tx, rx) = std::sync::mpsc::channel();
    let (_response_tx, response_rx) = std::sync::mpsc::channel();
    app.ai.request_tx = Some(tx);
    app.ai.response_rx = Some(response_rx);

    // Clear any pending messages
    while rx.try_recv().is_ok() {}

    // Press Ctrl+A to HIDE AI box
    app.handle_key_event(key_with_mods(KeyCode::Char('a'), KeyModifiers::CONTROL));

    // Verify popup is now hidden
    assert!(!app.ai.visible, "AI popup should be hidden after Ctrl+A");

    // Verify NO AI request was sent when hiding
    let mut found_request = false;
    while let Ok(msg) = rx.try_recv() {
        if matches!(msg, crate::ai::ai_state::AiRequest::Query { .. }) {
            found_request = true;
            break;
        }
    }
    assert!(
        !found_request,
        "Should NOT send AI request when hiding popup"
    );
}