jiq 3.21.0

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
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
//! Tests for app_state

use super::*;
use crate::test_utils::test_helpers::{create_test_loader, test_app};
use proptest::prelude::*;
use std::sync::Arc;

#[test]
fn test_app_initialization() {
    let json = r#"{"name": "Alice", "age": 30}"#;
    let app = test_app(json);

    assert_eq!(app.focus, Focus::InputField);
    assert_eq!(app.results_scroll.offset, 0);
    assert_eq!(app.output_mode, None);
    assert!(!app.should_quit);
    assert_eq!(app.query(), "");
}

#[test]
fn test_initial_query_result() {
    let json = r#"{"name": "Bob"}"#;
    let app = test_app(json);

    assert!(app.query.is_some());
    let query_state = app.query.as_ref().unwrap();
    assert!(query_state.result.is_ok());
    let result = query_state.result.as_ref().unwrap();
    assert!(result.contains("Bob"));
}

#[test]
fn test_focus_enum() {
    assert_eq!(Focus::InputField, Focus::InputField);
    assert_eq!(Focus::ResultsPane, Focus::ResultsPane);
    assert_ne!(Focus::InputField, Focus::ResultsPane);
}

#[test]
fn test_output_mode_enum() {
    assert_eq!(OutputMode::Results, OutputMode::Results);
    assert_eq!(OutputMode::Query, OutputMode::Query);
    assert_ne!(OutputMode::Results, OutputMode::Query);
}

#[test]
fn test_should_quit_getter() {
    let json = r#"{}"#;
    let mut app = test_app(json);

    assert!(!app.should_quit());

    app.should_quit = true;
    assert!(app.should_quit());
}

#[test]
fn test_output_mode_getter() {
    let json = r#"{}"#;
    let mut app = test_app(json);

    assert_eq!(app.output_mode(), None);

    app.output_mode = Some(OutputMode::Results);
    assert_eq!(app.output_mode(), Some(OutputMode::Results));

    app.output_mode = Some(OutputMode::Query);
    assert_eq!(app.output_mode(), Some(OutputMode::Query));
}

#[test]
fn test_query_getter_empty() {
    let json = r#"{"test": true}"#;
    let app = test_app(json);

    assert_eq!(app.query(), "");
}

#[test]
fn test_app_with_empty_json_object() {
    let json = "{}";
    let app = test_app(json);

    assert!(app.query.is_some());
    let query_state = app.query.as_ref().unwrap();
    assert!(query_state.result.is_ok());
}

#[test]
fn test_app_with_json_array() {
    let json = r#"[1, 2, 3]"#;
    let app = test_app(json);

    assert!(app.query.is_some());
    let query_state = app.query.as_ref().unwrap();
    assert!(query_state.result.is_ok());
    let result = query_state.result.as_ref().unwrap();
    assert!(result.contains("1"));
    assert!(result.contains("2"));
    assert!(result.contains("3"));
}

#[test]
fn test_max_scroll_large_content() {
    let json = r#"{"test": true}"#;
    let mut app = test_app(json);

    let large_result: String = (0..70000).map(|i| format!("line {}\n", i)).collect();
    let query_state = app.query.as_mut().unwrap();
    query_state.result = Ok(large_result.clone());
    query_state.last_successful_result = Some(Arc::new(large_result.clone()));
    query_state.cached_line_count = large_result.lines().count() as u32;

    let line_count = app.results_line_count_u32();
    assert!(line_count > 65535);

    app.results_scroll.update_bounds(line_count, 20);

    assert_eq!(app.results_scroll.max_offset, u16::MAX);
}

#[test]
fn test_results_line_count_large_file() {
    let json = r#"{"test": true}"#;
    let mut app = test_app(json);

    let result: String = (0..65535).map(|_| "x\n").collect();
    let query_state = app.query.as_mut().unwrap();
    query_state.result = Ok(result.clone());
    query_state.last_successful_result = Some(Arc::new(result.clone()));
    query_state.cached_line_count = result.lines().count() as u32;

    assert_eq!(app.results_line_count_u32(), 65535);

    app.results_scroll.update_bounds(65535, 10);

    assert_eq!(app.results_scroll.max_offset, 65525);
}

#[test]
fn test_line_count_uses_last_result_on_error() {
    let json = r#"{"test": true}"#;
    let mut app = test_app(json);

    let valid_result: String = (0..50).map(|i| format!("line{}\n", i)).collect();
    let query_state = app.query.as_mut().unwrap();
    query_state.result = Ok(valid_result.clone());
    query_state.last_successful_result = Some(Arc::new(valid_result.clone()));
    query_state.cached_line_count = valid_result.lines().count() as u32;

    assert_eq!(app.results_line_count_u32(), 50);

    app.query.as_mut().unwrap().result = Err("syntax error\nline 2\nline 3".to_string());

    assert_eq!(app.results_line_count_u32(), 50);

    app.results_scroll.update_bounds(50, 10);
    assert_eq!(app.results_scroll.max_offset, 40);
}

#[test]
fn test_line_count_with_error_no_cached_result() {
    let json = r#"{"test": true}"#;
    let mut app = test_app(json);

    let query_state = app.query.as_mut().unwrap();
    query_state.last_successful_result = None;
    query_state.cached_line_count = 0;
    query_state.result = Err("error message".to_string());

    assert_eq!(app.results_line_count_u32(), 0);

    app.results_scroll.update_bounds(0, 10);
    assert_eq!(app.results_scroll.max_offset, 0);
}

#[test]
fn test_tooltip_initialized_enabled() {
    let json = r#"{"name": "test"}"#;
    let app = test_app(json);

    assert!(app.tooltip.enabled);
    assert!(app.tooltip.current_function.is_none());
}

// **Feature: ai-assistant-phase2, Property 10: Info popup hidden while AI visible**
// *For any* state where AI popup is visible, the info popup SHALL be hidden.
// **Validates: Requirements 9.1, 9.4**
proptest! {
    #![proptest_config(ProptestConfig::with_cases(100))]

    #[test]
    fn prop_tooltip_hidden_while_ai_visible(
        initial_tooltip_enabled: bool,
        ai_enabled: bool,
        ai_configured: bool
    ) {
        let json = r#"{"test": true}"#;
        let mut app = test_app(json);

        // Set up initial state
        app.tooltip.enabled = initial_tooltip_enabled;
        app.ai.enabled = ai_enabled;
        app.ai.configured = ai_configured;
        app.ai.visible = false;

        // Toggle AI popup to make it visible
        let was_visible = app.ai.visible;
        app.ai.toggle();

        if !was_visible && app.ai.visible {
            // Save current tooltip state and hide it
            app.saved_tooltip_visibility = app.tooltip.enabled;
            app.tooltip.enabled = false;
        }

        // When AI popup is visible, tooltip should be disabled
        if app.ai.visible {
            prop_assert!(
                !app.tooltip.enabled,
                "Tooltip should be disabled when AI popup is visible"
            );
        }
    }
}

// **Feature: ai-assistant-phase2, Property 11: Info popup state restoration**
// *For any* AI popup hide action, the info popup visibility SHALL be restored to its saved state.
// **Validates: Requirements 9.2, 9.3**
proptest! {
    #![proptest_config(ProptestConfig::with_cases(100))]

    #[test]
    fn prop_tooltip_state_restoration(
        initial_tooltip_enabled: bool,
        ai_enabled: bool,
        ai_configured: bool
    ) {
        let json = r#"{"test": true}"#;
        let mut app = test_app(json);

        // Set up initial state
        app.tooltip.enabled = initial_tooltip_enabled;
        app.ai.enabled = ai_enabled;
        app.ai.configured = ai_configured;
        app.ai.visible = false;

        let original_tooltip_state = app.tooltip.enabled;

        // Toggle AI popup to make it visible (simulating Ctrl+A press)
        let was_visible = app.ai.visible;
        app.ai.toggle();

        if !was_visible && app.ai.visible {
            // Save current tooltip state and hide it
            app.saved_tooltip_visibility = app.tooltip.enabled;
            app.tooltip.enabled = false;
        }

        // Now toggle AI popup to hide it (simulating second Ctrl+A press)
        let was_visible = app.ai.visible;
        app.ai.toggle();

        if was_visible && !app.ai.visible {
            // Restore saved tooltip state
            app.tooltip.enabled = app.saved_tooltip_visibility;
        }

        // After hiding AI popup, tooltip should be restored to original state
        prop_assert_eq!(
            app.tooltip.enabled,
            original_tooltip_state,
            "Tooltip state should be restored to original value after AI popup is hidden"
        );
    }
}

#[test]
fn test_trigger_ai_request_sends_request_when_configured() {
    // Test that trigger_ai_request sends a request when AI is configured
    let json_input = r#"{"name": "test", "value": 42}"#.to_string();
    let config = Config::default();
    let loader = create_test_loader(json_input);
    let mut app = App::new_with_loader(loader, &config);

    // Poll the loader to initialize query state
    app.poll_file_loader();

    // Configure AI with channel
    app.ai.configured = true;
    app.ai.visible = true; // Must be visible for requests to be sent
    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");

    // Set a different query
    app.input.textarea.insert_str(".name");
    if let Some(query_state) = &mut app.query {
        query_state.execute(".name");
    }

    // Trigger AI request
    app.trigger_ai_request();

    // Verify 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 configured");
}

#[test]
fn test_trigger_ai_request_noop_when_not_configured() {
    // Test that trigger_ai_request does nothing when AI is not configured
    let json_input = r#"{"name": "test"}"#.to_string();
    let config = Config::default();
    let loader = create_test_loader(json_input);
    let mut app = App::new_with_loader(loader, &config);

    // Poll the loader to initialize query state
    app.poll_file_loader();

    // AI is NOT configured
    app.ai.configured = false;
    app.ai.request_tx = None;

    // Set a query
    app.input.textarea.insert_str(".name");

    // This should not panic even without channel
    app.trigger_ai_request();

    // Test passes if no panic occurred
}

#[test]
fn test_trigger_ai_request_includes_query_context() {
    // Test that trigger_ai_request includes the current query context
    let json_input = r#"{"name": "test", "age": 30}"#.to_string();
    let config = Config::default();
    let loader = create_test_loader(json_input);
    let mut app = App::new_with_loader(loader, &config);

    // Poll the loader to initialize query state
    app.poll_file_loader();

    // Configure AI
    app.ai.configured = true;
    app.ai.visible = true; // Must be visible for requests to be sent
    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");

    // Set a query with error
    app.input.textarea.insert_str(".invalid");
    if let Some(query_state) = &mut app.query {
        query_state.execute(".invalid");
    }

    // Trigger AI request
    app.trigger_ai_request();

    // Verify request contains the query
    if let Ok(crate::ai::ai_state::AiRequest::Query { prompt, .. }) = rx.try_recv() {
        assert!(
            prompt.contains(".invalid"),
            "Prompt should contain the query"
        );
    } else {
        panic!("Expected Query request");
    }
}

#[test]
fn test_initial_needs_render_true() {
    let app = test_app(r#"{"test": true}"#);
    assert!(
        app.needs_render,
        "New app should have needs_render=true for initial render"
    );
}

#[test]
fn test_poll_file_loader_marks_dirty_on_success() {
    let json_input = r#"{"test": true}"#.to_string();
    let config = Config::default();
    let loader = create_test_loader(json_input);
    let mut app = App::new_with_loader(loader, &config);

    app.clear_dirty();

    app.poll_file_loader();

    assert!(
        app.needs_render,
        "poll_file_loader should mark dirty on successful load"
    );
}

#[test]
fn test_poll_file_loader_marks_dirty_on_error() {
    let config = Config::default();
    let loader = crate::input::FileLoader::spawn_load(std::path::PathBuf::from("/nonexistent"));
    let mut app = App::new_with_loader(loader, &config);

    std::thread::sleep(std::time::Duration::from_millis(100));

    app.clear_dirty();

    app.poll_file_loader();

    if app.notification.current().is_some() {
        assert!(
            app.needs_render,
            "poll_file_loader should mark dirty when error notification is shown"
        );
    }
}

// **Feature: deferred-file-loading, Property 2: Successful loading initializes QueryState**
// *For any* valid JSON string returned by FileLoader, after poll_file_loader processes the result,
// the App's query field should be Some and contain a QueryState initialized with that JSON
// **Validates: Requirements 1.3, 2.2, 4.4**
proptest! {
    #![proptest_config(ProptestConfig::with_cases(100))]

    #[test]
    fn prop_successful_loading_initializes_query(
        json_value in prop::collection::vec(any::<u8>(), 1..100)
    ) {
        // Generate a valid JSON string
        let json = format!(r#"{{"data": {:?}}}"#, json_value);

        // Validate it's actually valid JSON
        if serde_json::from_str::<serde_json::Value>(&json).is_err() {
            return Ok(());
        }

        let config = Config::default();

        // Create a mock FileLoader that has completed successfully
        // We'll simulate this by creating an app with loader, then manually setting the result
        let loader = crate::input::FileLoader::spawn_load(std::path::PathBuf::from("/nonexistent"));
        let mut app = App::new_with_loader(loader, &config);

        // Manually simulate successful loading by removing loader and setting query
        app.file_loader = None;
        app.query = Some(QueryState::new(json.clone()));

        // Verify query is initialized
        prop_assert!(app.query.is_some(), "Query should be Some after successful loading");

        let query_state = app.query.as_ref().unwrap();
        prop_assert_eq!(query_state.executor.json_input(), &json, "Query should contain the loaded JSON");
    }
}

// **Feature: deferred-file-loading, Property 3: Error loading preserves None QueryState**
// *For any* error returned by FileLoader, after poll_file_loader processes the error,
// the App's query field should remain None
// **Validates: Requirements 1.4, 2.3, 4.5**
proptest! {
    #![proptest_config(ProptestConfig::with_cases(100))]

    #[test]
    fn prop_error_loading_preserves_none_query(
        _error_msg in ".*"
    ) {
        let config = Config::default();

        // Create app with loader
        let loader = crate::input::FileLoader::spawn_load(std::path::PathBuf::from("/nonexistent"));
        let app = App::new_with_loader(loader, &config);

        // Verify query starts as None
        prop_assert!(app.query.is_none(), "Query should start as None with loader");

        // Simulate error by keeping loader but not setting query
        // (In real scenario, poll_file_loader would handle this)
        // For this test, we just verify the invariant holds

        prop_assert!(app.query.is_none(), "Query should remain None after error");
    }
}

// **Feature: deferred-file-loading, Property 5: Loading invariant maintained**
// *For any* App state where file_loader is Some and in Loading state, the query field must be None
// **Validates: Requirements 4.3**
proptest! {
    #![proptest_config(ProptestConfig::with_cases(100))]

    #[test]
    fn prop_loading_invariant_maintained(
        _dummy in any::<u8>()
    ) {
        let config = Config::default();

        // Create app with loader in Loading state
        let loader = crate::input::FileLoader::spawn_load(std::path::PathBuf::from("/nonexistent"));
        let app = App::new_with_loader(loader, &config);

        // Verify invariant: if file_loader is Some and Loading, query must be None
        if let Some(loader) = &app.file_loader
            && loader.is_loading() {
                prop_assert!(app.query.is_none(), "Query must be None when file_loader is Loading");
            }
    }
}

#[test]
fn test_new_with_bedrock_provider() {
    use crate::config::ai_types::{AiConfig, AiProviderType, BedrockConfig};

    let config = Config {
        ai: AiConfig {
            enabled: true,
            provider: Some(AiProviderType::Bedrock),
            bedrock: BedrockConfig {
                region: Some("us-east-1".to_string()),
                model: Some("anthropic.claude-3-sonnet".to_string()),
                ..Default::default()
            },
            ..Default::default()
        },
        ..Default::default()
    };

    let loader = create_test_loader("{}".to_string());
    let app = App::new_with_loader(loader, &config);

    assert!(app.ai.configured);
}

#[test]
fn test_new_with_openai_provider() {
    use crate::config::ai_types::{AiConfig, AiProviderType, OpenAiConfig};

    let config = Config {
        ai: AiConfig {
            enabled: true,
            provider: Some(AiProviderType::Openai),
            openai: OpenAiConfig {
                api_key: Some("test-key".to_string()),
                model: Some("gpt-4".to_string()),
                base_url: None,
            },
            ..Default::default()
        },
        ..Default::default()
    };

    let loader = create_test_loader("{}".to_string());
    let app = App::new_with_loader(loader, &config);

    assert!(app.ai.configured);
}

#[test]
fn test_new_with_gemini_provider() {
    use crate::config::ai_types::{AiConfig, AiProviderType, GeminiConfig};

    let config = Config {
        ai: AiConfig {
            enabled: true,
            provider: Some(AiProviderType::Gemini),
            gemini: GeminiConfig {
                api_key: Some("test-key".to_string()),
                model: Some("gemini-pro".to_string()),
            },
            ..Default::default()
        },
        ..Default::default()
    };

    let loader = create_test_loader("{}".to_string());
    let app = App::new_with_loader(loader, &config);

    assert!(app.ai.configured);
}

#[test]
fn test_trigger_ai_request_when_query_none() {
    let json = r#"{"test": true}"#;
    let mut app = test_app(json);
    app.query = None;
    app.ai.configured = true;

    app.trigger_ai_request();

    // Should return early without error
}

#[test]
fn test_trigger_ai_request_strips_ansi_from_success_output() {
    let json_input = r#"{"name": "test", "age": 30}"#.to_string();
    let config = Config::default();
    let loader = create_test_loader(json_input);
    let mut app = App::new_with_loader(loader, &config);

    app.poll_file_loader();

    app.ai.configured = true;
    app.ai.visible = true;
    app.ai.max_context_length = crate::ai::context::MAX_JSON_SAMPLE_LENGTH;
    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);

    app.ai.set_last_query_hash(".initial");

    app.input.textarea.insert_str(".name");
    if let Some(query_state) = &mut app.query {
        query_state.result = Ok("\x1b[0;32m\"test\"\x1b[0m\n".to_string());
        query_state.last_successful_result =
            Some(Arc::new("\x1b[0;32m\"test\"\x1b[0m\n".to_string()));
        query_state.last_successful_result_unformatted = Some(Arc::new("\"test\"\n".to_string()));
        query_state.last_successful_result_for_context = Some(Arc::new(
            crate::ai::context::prepare_json_for_context("\"test\"\n", app.ai.max_context_length),
        ));
        query_state.base_query_for_suggestions = Some(".name".to_string());
    }

    app.trigger_ai_request();

    if let Ok(crate::ai::ai_state::AiRequest::Query { prompt, .. }) = rx.try_recv() {
        assert!(
            !prompt.contains("\x1b["),
            "Prompt should not contain ANSI escape codes"
        );
        assert!(
            prompt.contains("\"test\""),
            "Prompt should contain the unformatted output"
        );
    } else {
        panic!("Expected Query request");
    }
}

#[test]
fn test_trigger_ai_request_strips_ansi_from_base_query_result() {
    let json_input = r#"{"name": "test", "age": 30}"#.to_string();
    let config = Config::default();
    let loader = create_test_loader(json_input);
    let mut app = App::new_with_loader(loader, &config);

    app.poll_file_loader();

    app.ai.configured = true;
    app.ai.visible = 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);

    app.ai.set_last_query_hash(".name");

    app.input.textarea.insert_str(".invalid");
    if let Some(query_state) = &mut app.query {
        query_state.result = Err("field not found".to_string());
        query_state.last_successful_result =
            Some(Arc::new("\x1b[0;32m\"test\"\x1b[0m\n".to_string()));
        query_state.last_successful_result_unformatted = Some(Arc::new("\"test\"\n".to_string()));
        query_state.base_query_for_suggestions = Some(".name".to_string());
    }

    app.trigger_ai_request();

    if let Ok(crate::ai::ai_state::AiRequest::Query { prompt, .. }) = rx.try_recv() {
        assert!(
            !prompt.contains("\x1b["),
            "Prompt should not contain ANSI escape codes in base_query_result"
        );
        if prompt.contains("Its Output") {
            assert!(
                prompt.contains("\"test\""),
                "Prompt should contain unformatted base query result"
            );
        }
    } else {
        panic!("Expected Query request");
    }
}

#[test]
fn test_trigger_ai_request_empty_result_uses_unformatted() {
    let json_input = r#"{"items": []}"#.to_string();
    let config = Config::default();
    let loader = create_test_loader(json_input);
    let mut app = App::new_with_loader(loader, &config);

    app.poll_file_loader();

    app.ai.configured = true;
    app.ai.visible = true;
    app.ai.max_context_length = crate::ai::context::MAX_JSON_SAMPLE_LENGTH;
    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);

    app.ai.set_last_query_hash(".previous");

    app.input.textarea.insert_str(".empty");
    if let Some(query_state) = &mut app.query {
        query_state.result = Ok("null\n".to_string());
        query_state.is_empty_result = true;
        query_state.last_successful_result =
            Some(Arc::new("\x1b[0;32m\"previous\"\x1b[0m\n".to_string()));
        query_state.last_successful_result_unformatted =
            Some(Arc::new("\"previous\"\n".to_string()));
        query_state.last_successful_result_for_context =
            Some(Arc::new(crate::ai::context::prepare_json_for_context(
                "\"previous\"\n",
                app.ai.max_context_length,
            )));
        query_state.base_query_for_suggestions = Some(".previous".to_string());
    }

    app.trigger_ai_request();

    if let Ok(crate::ai::ai_state::AiRequest::Query { prompt, .. }) = rx.try_recv() {
        assert!(
            !prompt.contains("\x1b["),
            "Prompt should not contain ANSI codes even with empty result"
        );
        assert!(
            prompt.contains("\"previous\""),
            "Prompt should contain unformatted previous result"
        );
    } else {
        panic!("Expected Query request");
    }
}

#[cfg(test)]
#[path = "app_state_tests/dirty_flag_tests.rs"]
mod dirty_flag_tests;