jiq 3.22.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
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
use assert_cmd::cargo::cargo_bin_cmd;
use predicates::prelude::*;
use std::fs;
use std::path::PathBuf;
use std::sync::mpsc;
use std::thread;
use std::time::Duration;

/// Helper to get path to fixture file
fn fixture_path(name: &str) -> PathBuf {
    PathBuf::from(env!("CARGO_MANIFEST_DIR"))
        .join("tests")
        .join("fixtures")
        .join(name)
}

/// Create a FileLoader with pre-loaded JSON for testing
///
/// This helper creates a FileLoader that immediately has the JSON available,
/// avoiding the need for actual file I/O in integration tests.
fn create_test_loader(json: String) -> jiq::input::FileLoader {
    use jiq::input::loader::LoadingState;
    use std::sync::mpsc::channel;
    let (tx, rx) = channel();
    // Send the result immediately so poll() will return it
    let _ = tx.send(Ok(json));
    jiq::input::FileLoader {
        state: LoadingState::Loading,
        rx: Some(rx),
    }
}

// Note: These tests are commented out because with Phase 2 (deferred file loading),
// the app enters TUI mode immediately and shows errors in the UI rather than
// exiting with error codes. The TUI-based error handling provides better UX
// but is harder to test with assert_cmd. The errors are still properly shown to users.
//
// #[test]
// fn test_cli_with_invalid_json_file() {
//     let fixture = fixture_path("invalid.json");
//     // With deferred loading, app enters TUI and shows error there
//     cargo_bin_cmd!().arg(&fixture).assert().success();
// }
//
// #[test]
// fn test_cli_with_nonexistent_file() {
//     // With deferred loading, app enters TUI and shows error there
//     cargo_bin_cmd!().arg("nonexistent.json").assert().success();
// }

#[test]
fn test_cli_help_flag() {
    cargo_bin_cmd!()
        .arg("--help")
        .assert()
        .success()
        .stdout(predicate::str::contains("Interactive JSON query tool"));
}

#[test]
fn test_cli_version_flag() {
    cargo_bin_cmd!()
        .arg("--version")
        .assert()
        .success()
        .stdout(predicate::str::contains("jiq"));
}

#[test]
fn test_fixture_files_exist() {
    // Verify all our test fixtures are present
    assert!(fixture_path("simple.json").exists());
    assert!(fixture_path("array.json").exists());
    assert!(fixture_path("nested.json").exists());
    assert!(fixture_path("invalid.json").exists());
}

#[test]
fn test_fixture_simple_json_content() {
    let content = fs::read_to_string(fixture_path("simple.json")).unwrap();
    assert!(content.contains("Alice"));
    assert!(content.contains("Seattle"));
}

#[test]
fn test_fixture_array_json_content() {
    let content = fs::read_to_string(fixture_path("array.json")).unwrap();
    assert!(content.contains("Alice"));
    assert!(content.contains("Bob"));
    assert!(content.contains("Charlie"));
}

#[test]
fn test_fixture_nested_json_content() {
    let content = fs::read_to_string(fixture_path("nested.json")).unwrap();
    assert!(content.contains("TechCorp"));
    assert!(content.contains("engineering"));
    assert!(content.contains("departments"));
}

// =============================================================================
// AI Integration Tests
// =============================================================================

/// Test the full AI flow: error → auto-show → request → stream → display
///
/// This test validates the AI assistant's end-to-end behavior by simulating
/// the worker thread communication pattern without making actual HTTP calls.
///
/// **Validates: Requirements 3.1, 4.1, 4.4**
#[test]
fn test_ai_full_flow_error_to_response() {
    // Create channels for request/response communication
    let (request_tx, request_rx) = mpsc::channel::<String>();
    let (response_tx, response_rx) = mpsc::channel::<String>();

    // Spawn a mock worker thread that simulates SSE streaming
    let worker_handle = thread::spawn(move || {
        // Wait for a request
        if let Ok(prompt) = request_rx.recv_timeout(Duration::from_secs(5)) {
            assert!(
                prompt.contains("error"),
                "Prompt should contain error context"
            );

            // Simulate SSE streaming response in chunks
            let chunks = vec![
                "The error ",
                "you're seeing ",
                "is because ",
                "the query ",
                "syntax is invalid.",
            ];

            for chunk in chunks {
                response_tx.send(chunk.to_string()).unwrap();
                thread::sleep(Duration::from_millis(10));
            }
        }
    });

    // Simulate the main thread behavior:
    // 1. Send a request (simulating auto-show on error)
    let error_context = "Query error: unexpected token at position 5";
    request_tx.send(error_context.to_string()).unwrap();

    // 2. Collect streaming response chunks
    let mut accumulated_response = String::new();
    let mut chunk_count = 0;

    while let Ok(chunk) = response_rx.recv_timeout(Duration::from_millis(500)) {
        accumulated_response.push_str(&chunk);
        chunk_count += 1;
    }

    // 3. Verify the response was streamed correctly
    assert!(chunk_count > 1, "Response should arrive in multiple chunks");
    assert_eq!(
        accumulated_response,
        "The error you're seeing is because the query syntax is invalid."
    );

    worker_handle.join().expect("Worker thread should complete");
}

/// Test that AI state transitions work correctly during request lifecycle
#[test]
fn test_ai_state_transitions() {
    // Simulate state transitions without actual AI module dependencies
    // This tests the logical flow of: idle → loading → streaming → complete

    #[derive(Debug, PartialEq)]
    enum State {
        Idle,
        Loading,
        Streaming,
        Complete,
    }

    let mut state = State::Idle;
    let mut response = String::new();

    // 1. Start request - should transition to Loading
    assert_eq!(state, State::Idle);
    let previous_response = if !response.is_empty() {
        Some(response.clone())
    } else {
        None
    };
    response.clear();
    state = State::Loading;
    assert_eq!(state, State::Loading);
    assert!(response.is_empty());
    assert!(previous_response.is_none());

    // 2. Receive first chunk - should transition to Streaming
    let chunk1 = "Hello ";
    response.push_str(chunk1);
    state = State::Streaming;
    assert_eq!(state, State::Streaming);
    assert_eq!(response, "Hello ");

    // 3. Receive more chunks
    let chunk2 = "World!";
    response.push_str(chunk2);
    assert_eq!(response, "Hello World!");

    // 4. Complete - should transition to Complete
    state = State::Complete;
    assert_eq!(state, State::Complete);
    assert_eq!(response, "Hello World!");
}

/// Test that previous response is preserved when starting a new request
#[test]
fn test_ai_previous_response_preservation() {
    let mut response = "Previous AI response".to_string();
    let mut previous_response: Option<String> = None;

    // Start a new request - should preserve current response
    if !response.is_empty() {
        previous_response = Some(response.clone());
    }
    response.clear();

    assert!(response.is_empty());
    assert_eq!(previous_response, Some("Previous AI response".to_string()));

    // Simulate receiving new response
    response.push_str("New response");

    // Complete request - should clear previous
    previous_response = None;

    assert_eq!(response, "New response");
    assert!(previous_response.is_none());
}

/// Test streaming concatenation property
///
/// **Validates: Property 10 - Streaming concatenation**
/// For any sequence of response chunks [c1, c2, ..., cn],
/// the final displayed response should equal c1 + c2 + ... + cn.
#[test]
fn test_ai_streaming_concatenation() {
    let chunks = vec![
        "First chunk. ",
        "Second chunk. ",
        "Third chunk. ",
        "Final chunk.",
    ];

    let mut accumulated = String::new();
    for chunk in &chunks {
        accumulated.push_str(chunk);
    }

    let expected = chunks.join("");
    assert_eq!(accumulated, expected);
    assert_eq!(
        accumulated,
        "First chunk. Second chunk. Third chunk. Final chunk."
    );
}

/// Test the full AI flow with SSE-like streaming simulation
///
/// This test validates the complete flow from error detection through
/// streaming response display, simulating the SSE streaming behavior
/// that would come from the Anthropic API.
///
/// **Validates: Requirements 3.1, 4.1, 4.4**
#[test]
fn test_ai_full_flow_with_sse_simulation() {
    use std::io::{BufRead, BufReader, Cursor};

    // Simulate SSE data as it would come from Anthropic API
    let sse_data = r#"event: message_start
data: {"type":"message_start","message":{"id":"msg_test"}}

event: content_block_start
data: {"type":"content_block_start","index":0}

event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"The error "}}

event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"in your query "}}

event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"is a syntax issue."}}

event: content_block_stop
data: {"type":"content_block_stop","index":0}

event: message_stop
data: {"type":"message_stop"}

"#;

    // Parse SSE data like the real client would
    fn parse_sse_chunks(data: &str) -> Vec<String> {
        let reader = BufReader::new(Cursor::new(data.as_bytes()));
        let mut chunks = Vec::new();

        for line in reader.lines() {
            let line = line.unwrap();
            if let Some(data) = line.strip_prefix("data: ") {
                if data == "[DONE]" {
                    break;
                }
                // Try to parse as content_block_delta
                if let Ok(json) = serde_json::from_str::<serde_json::Value>(data)
                    && json.get("type").and_then(|t| t.as_str()) == Some("content_block_delta")
                    && let Some(text) = json
                        .get("delta")
                        .and_then(|d| d.get("text"))
                        .and_then(|t| t.as_str())
                    && !text.is_empty()
                {
                    chunks.push(text.to_string());
                }
            }
        }
        chunks
    }

    // Parse the SSE data
    let chunks = parse_sse_chunks(sse_data);

    // Verify we got the expected chunks
    assert_eq!(chunks.len(), 3);
    assert_eq!(chunks[0], "The error ");
    assert_eq!(chunks[1], "in your query ");
    assert_eq!(chunks[2], "is a syntax issue.");

    // Simulate the full flow: error → auto-show → request → stream → display
    let (request_tx, request_rx) = mpsc::channel::<String>();
    let (response_tx, response_rx) = mpsc::channel::<String>();

    // Spawn worker that processes SSE-like chunks
    let worker_handle = thread::spawn(move || {
        if let Ok(_prompt) = request_rx.recv_timeout(Duration::from_secs(5)) {
            // Simulate streaming the parsed chunks
            for chunk in chunks {
                response_tx.send(chunk).unwrap();
                thread::sleep(Duration::from_millis(5));
            }
        }
    });

    // 1. Simulate error detection triggering auto-show
    let error_context = "jq: error: syntax error, unexpected IDENT";
    request_tx.send(error_context.to_string()).unwrap();

    // 2. Collect streaming response
    let mut accumulated = String::new();
    let mut chunk_count = 0;

    while let Ok(chunk) = response_rx.recv_timeout(Duration::from_millis(500)) {
        accumulated.push_str(&chunk);
        chunk_count += 1;
    }

    // 3. Verify the complete response
    assert_eq!(chunk_count, 3, "Should receive 3 chunks");
    assert_eq!(accumulated, "The error in your query is a syntax issue.");

    worker_handle.join().expect("Worker should complete");
}

/// Test visibility-based AI request behavior
#[test]
fn test_ai_visibility_configurations() {
    struct MockAiState {
        visible: bool,
        enabled: bool,
    }

    impl MockAiState {
        fn should_send_request(&self) -> bool {
            self.enabled && self.visible
        }
    }

    // Case 1: AI enabled, visible → should send request
    let state = MockAiState {
        visible: true,
        enabled: true,
    };
    assert!(state.should_send_request());

    // Case 2: AI enabled, hidden → should not send request
    let state = MockAiState {
        visible: false,
        enabled: true,
    };
    assert!(!state.should_send_request());

    // Case 3: AI disabled, visible → should not send request
    let state = MockAiState {
        visible: true,
        enabled: false,
    };
    assert!(!state.should_send_request());

    // Case 4: AI disabled, hidden → should not send request
    let state = MockAiState {
        visible: false,
        enabled: false,
    };
    assert!(!state.should_send_request());
}

// =============================================================================
// Task 20: AI Visibility Control Integration Tests
// =============================================================================

/// Test initial AI popup visibility based on config
///
/// **Validates: Requirements 8.1, 8.2**
/// - 8.1: WHEN AI is enabled in config THEN the AI_Popup SHALL be visible by default
/// - 8.2: WHEN AI is disabled in config THEN the AI_Popup SHALL be hidden by default
#[test]
fn test_initial_visibility_ai_enabled() {
    use jiq::app::App;
    use jiq::config::{AiConfig, AiProviderType, AnthropicConfig, Config};

    // Create config with AI enabled and provider explicitly set
    let config = Config {
        ai: AiConfig {
            enabled: true,
            provider: Some(AiProviderType::Anthropic),
            anthropic: AnthropicConfig {
                api_key: Some("test-key".to_string()),
                model: Some("claude-3-5-sonnet-20241022".to_string()),
                ..Default::default()
            },
            ..Default::default()
        },
        ..Default::default()
    };

    let json_input = r#"{"test": "data"}"#.to_string();
    let loader = create_test_loader(json_input);
    let app = App::new_with_loader(loader, &config);

    // Requirement 8.1: AI enabled → popup visible by default
    assert!(
        app.ai.visible,
        "AI popup should be visible when AI is enabled in config"
    );
    assert!(app.ai.enabled, "AI should be enabled");
    assert!(app.ai.configured, "AI should be configured with API key");
}

/// Test initial AI popup visibility when AI is disabled in config
///
/// **Validates: Requirements 8.2**
#[test]
fn test_initial_visibility_ai_disabled() {
    use jiq::app::App;
    use jiq::config::Config;

    // Create config with AI disabled (default)
    let config = Config::default();

    let json_input = r#"{"test": "data"}"#.to_string();
    let loader = create_test_loader(json_input);
    let app = App::new_with_loader(loader, &config);

    // Requirement 8.2: AI disabled → popup hidden by default
    assert!(
        !app.ai.visible,
        "AI popup should be hidden when AI is disabled in config"
    );
    assert!(!app.ai.enabled, "AI should be disabled");
}

/// Test that tooltip is hidden when AI is visible on startup
///
/// **Validates: Requirements 9.5**
/// - 9.5: WHEN jiq starts with AI enabled THEN the Info_Popup SHALL start in hidden state
#[test]
fn test_tooltip_hidden_when_ai_visible_on_startup() {
    use jiq::app::App;
    use jiq::config::{AiConfig, AiProviderType, AnthropicConfig, Config, TooltipConfig};

    // Create config with AI enabled, provider set, and tooltip auto_show enabled
    let config = Config {
        ai: AiConfig {
            enabled: true,
            provider: Some(AiProviderType::Anthropic),
            anthropic: AnthropicConfig {
                api_key: Some("test-key".to_string()),
                model: Some("claude-3-5-sonnet-20241022".to_string()),
                ..Default::default()
            },
            ..Default::default()
        },
        tooltip: TooltipConfig { auto_show: true },
        ..Default::default()
    };

    let json_input = r#"{"test": "data"}"#.to_string();
    let loader = create_test_loader(json_input);
    let app = App::new_with_loader(loader, &config);

    // AI should be visible
    assert!(app.ai.visible, "AI popup should be visible");

    // Tooltip should be hidden (even though auto_show is true in config)
    assert!(
        !app.tooltip.enabled,
        "Tooltip should be hidden when AI popup is visible on startup"
    );
}

/// Test that tooltip is visible when AI is disabled on startup
///
/// **Validates: Requirements 9.5** (inverse case)
#[test]
fn test_tooltip_visible_when_ai_disabled_on_startup() {
    use jiq::app::App;
    use jiq::config::{Config, TooltipConfig};

    // Create config with AI disabled and tooltip auto_show enabled
    let config = Config {
        tooltip: TooltipConfig { auto_show: true },
        ..Default::default()
    };

    let json_input = r#"{"test": "data"}"#.to_string();
    let loader = create_test_loader(json_input);
    let app = App::new_with_loader(loader, &config);

    // AI should be hidden
    assert!(!app.ai.visible, "AI popup should be hidden");

    // Tooltip should be visible (respecting auto_show config)
    assert!(
        app.tooltip.enabled,
        "Tooltip should be visible when AI popup is hidden on startup"
    );
}

// =============================================================================
// Task 20.2: Documentation of Existing Ctrl+A Toggle Tests
// =============================================================================
//
// The following tests for Ctrl+A toggle functionality already exist in the codebase:
//
// **Unit Tests** (in src/ai/ai_events.rs):
// - `test_ctrl_a_toggles_visibility_on`: Tests that Ctrl+A toggles visibility from hidden to visible
// - `test_ctrl_a_toggles_visibility_off`: Tests that Ctrl+A toggles visibility from visible to hidden
// - `test_plain_a_not_handled`: Tests that plain 'a' key without Ctrl is not handled
// - `test_ctrl_other_key_not_handled`: Tests that other Ctrl+key combinations are not handled
//
// **Property-Based Test** (in src/ai/ai_state.rs):
// - `prop_toggle_visibility`: Property test that verifies toggle() flips visibility state
//   for any initial visibility state, enabled state, and debounce_ms value.
//   Tests both single toggle and double toggle (round-trip).
//
// **Validates: Requirements 8.3**
// - 8.3: WHEN the user presses Ctrl+A THEN the AI_Popup visibility SHALL toggle
//
// These tests comprehensively cover the Ctrl+A toggle functionality at both the
// unit level (specific key combinations) and property level (state transitions).
// =============================================================================

// =============================================================================
// Task 20.3: Verify No Other Visibility Control Mechanisms
// =============================================================================

/// Test that handle_execution_result does NOT change visibility on error
///
/// **Validates: Requirements 8.1, 8.2, 8.3**
/// Confirms that only config initialization and Ctrl+A toggle control visibility,
/// not execution results.
#[test]
fn test_handle_execution_result_does_not_change_visibility_on_error() {
    use jiq::ai::ai_events::handle_execution_result;
    use jiq::ai::ai_state::AiState;

    // Test with visibility = false
    let mut ai_state = AiState {
        visible: false,
        ..Default::default()
    };
    let initial_visibility = ai_state.visible;

    let error_result: Result<String, String> = Err("syntax error".to_string());
    handle_execution_result(
        &mut ai_state,
        &error_result,
        ".invalid",
        0,
        jiq::ai::context::ContextParams {
            input_schema: None,
            base_query: None,
            base_query_result: None,
            is_empty_result: false,
        },
    );

    assert_eq!(
        ai_state.visible, initial_visibility,
        "handle_execution_result should NOT change visibility on error"
    );

    // Test with visibility = true
    let mut ai_state = AiState {
        visible: true,
        ..Default::default()
    };
    let initial_visibility = ai_state.visible;

    let error_result: Result<String, String> = Err("syntax error".to_string());
    handle_execution_result(
        &mut ai_state,
        &error_result,
        ".invalid2",
        0,
        jiq::ai::context::ContextParams {
            input_schema: None,
            base_query: None,
            base_query_result: None,
            is_empty_result: false,
        },
    );

    assert_eq!(
        ai_state.visible, initial_visibility,
        "handle_execution_result should NOT change visibility on error (even when visible)"
    );
}

/// Test that handle_execution_result does NOT change visibility on success
///
/// **Validates: Requirements 8.1, 8.2, 8.3**
/// Confirms that only config initialization and Ctrl+A toggle control visibility,
/// not execution results.
#[test]
fn test_handle_execution_result_does_not_change_visibility_on_success() {
    use jiq::ai::ai_events::handle_execution_result;
    use jiq::ai::ai_state::AiState;

    // Test with visibility = false
    let mut ai_state = AiState {
        visible: false,
        ..Default::default()
    };
    let initial_visibility = ai_state.visible;

    let success_result: Result<String, String> = Ok(r#"{"result": "value"}"#.to_string());
    handle_execution_result(
        &mut ai_state,
        &success_result,
        ".test",
        0,
        jiq::ai::context::ContextParams {
            input_schema: None,
            base_query: None,
            base_query_result: None,
            is_empty_result: false,
        },
    );

    assert_eq!(
        ai_state.visible, initial_visibility,
        "handle_execution_result should NOT change visibility on success"
    );

    // Test with visibility = true
    let mut ai_state = AiState {
        visible: true,
        ..Default::default()
    };
    let initial_visibility = ai_state.visible;

    let success_result: Result<String, String> = Ok(r#"{"result": "value2"}"#.to_string());
    handle_execution_result(
        &mut ai_state,
        &success_result,
        ".test2",
        0,
        jiq::ai::context::ContextParams {
            input_schema: None,
            base_query: None,
            base_query_result: None,
            is_empty_result: false,
        },
    );

    assert_eq!(
        ai_state.visible, initial_visibility,
        "handle_execution_result should NOT change visibility on success (even when visible)"
    );
}

/// Test that only config and Ctrl+A control visibility
///
/// **Validates: Requirements 8.1, 8.2, 8.3**
/// This test documents the complete visibility control mechanism:
/// 1. Initial visibility is set by config (8.1, 8.2)
/// 2. Ctrl+A toggle is the only runtime control (8.3)
/// 3. Execution results do NOT affect visibility
#[test]
fn test_visibility_control_mechanisms_complete() {
    use jiq::ai::ai_events::handle_execution_result;
    use jiq::ai::ai_state::AiState;
    use jiq::app::App;
    use jiq::config::{AiConfig, AiProviderType, AnthropicConfig, Config};

    // Mechanism 1: Config controls initial visibility
    let config_enabled = Config {
        ai: AiConfig {
            enabled: true,
            provider: Some(AiProviderType::Anthropic),
            anthropic: AnthropicConfig {
                api_key: Some("test-key".to_string()),
                model: Some("claude-3-5-sonnet-20241022".to_string()),
                ..Default::default()
            },
            ..Default::default()
        },
        ..Default::default()
    };

    let loader_enabled = create_test_loader(r#"{"test": "data"}"#.to_string());
    let app_enabled = App::new_with_loader(loader_enabled, &config_enabled);
    assert!(
        app_enabled.ai.visible,
        "Config with AI enabled should set initial visibility to true"
    );

    let config_disabled = Config::default();
    let loader_disabled = create_test_loader(r#"{"test": "data"}"#.to_string());
    let app_disabled = App::new_with_loader(loader_disabled, &config_disabled);
    assert!(
        !app_disabled.ai.visible,
        "Config with AI disabled should set initial visibility to false"
    );

    // Mechanism 2: Ctrl+A toggle is the only runtime control
    let mut ai_state = AiState {
        visible: false,
        ..Default::default()
    };

    ai_state.toggle(); // Simulates Ctrl+A
    assert!(ai_state.visible, "Toggle should change visibility");

    ai_state.toggle(); // Simulates Ctrl+A again
    assert!(!ai_state.visible, "Toggle should change visibility back");

    // Mechanism 3: Execution results do NOT change visibility
    let mut ai_state = AiState {
        visible: false,
        ..Default::default()
    };

    // Try error result
    let error_result: Result<String, String> = Err("error".to_string());
    handle_execution_result(
        &mut ai_state,
        &error_result,
        ".err",
        0,
        jiq::ai::context::ContextParams {
            input_schema: None,
            base_query: None,
            base_query_result: None,
            is_empty_result: false,
        },
    );
    assert!(
        !ai_state.visible,
        "Error result should NOT change visibility"
    );

    // Try success result
    let success_result: Result<String, String> = Ok(r#"{"ok": true}"#.to_string());
    handle_execution_result(
        &mut ai_state,
        &success_result,
        ".ok",
        0,
        jiq::ai::context::ContextParams {
            input_schema: None,
            base_query: None,
            base_query_result: None,
            is_empty_result: false,
        },
    );
    assert!(
        !ai_state.visible,
        "Success result should NOT change visibility"
    );

    // Summary: Only config (initial) and toggle (runtime) control visibility
}