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
//! Content building tests for AI render module

use super::*;
use crate::ai::ai_state::lifecycle::TEST_MAX_CONTEXT_LENGTH;
use crate::ai::render::text::wrap_text;
use crate::theme;
use proptest::prelude::*;

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

#[test]
fn test_wrap_text_short() {
    let result = wrap_text("hello world", 50);
    assert_eq!(result, vec!["hello world"]);
}

#[test]
fn test_wrap_text_long() {
    let result = wrap_text("hello world this is a long line", 15);
    assert_eq!(result, vec!["hello world", "this is a long", "line"]);
}

#[test]
fn test_wrap_text_empty() {
    let result = wrap_text("", 50);
    assert_eq!(result, vec![""]);
}

#[test]
fn test_wrap_text_multiline() {
    let result = wrap_text("line one\nline two", 50);
    assert_eq!(result, vec!["line one", "line two"]);
}

#[test]
fn test_build_content_empty_state() {
    let state = AiState::new_with_config(
        true,
        true,
        "Anthropic".to_string(),
        "claude-3-5-sonnet-20241022".to_string(),
        TEST_MAX_CONTEXT_LENGTH,
    );
    let content = build_content(&state, 60);

    // Empty state shows nothing - "Thinking..." appears when loading
    assert!(content.lines.is_empty());
}

#[test]
fn test_build_content_not_configured() {
    let state = AiState::new_with_config(
        true,
        false,
        "Anthropic".to_string(),
        "claude-3-5-sonnet-20241022".to_string(),
        TEST_MAX_CONTEXT_LENGTH,
    );
    let content = build_content(&state, 60);
    let text: String = content
        .lines
        .iter()
        .flat_map(|l| l.spans.iter())
        .map(|s| s.content.as_ref())
        .collect();

    assert!(text.contains("AI provider not configured"));
    assert!(text.contains("[ai]"));
    assert!(text.contains("provider"));
    assert!(text.contains("api_key"));
    assert!(text.contains("https://github.com/bellicose100xp/jiq#configuration"));
}

#[test]
fn test_build_content_loading() {
    let mut state = AiState::new_with_config(
        true,
        true,
        "Anthropic".to_string(),
        "claude-3-5-sonnet-20241022".to_string(),
        TEST_MAX_CONTEXT_LENGTH,
    );
    state.loading = true;

    let content = build_content(&state, 60);
    let text: String = content
        .lines
        .iter()
        .flat_map(|l| l.spans.iter())
        .map(|s| s.content.as_ref())
        .collect();

    assert!(text.contains("Thinking"));
}

#[test]
fn test_build_content_error() {
    let mut state = AiState::new_with_config(
        true,
        true,
        "Anthropic".to_string(),
        "claude-3-5-sonnet-20241022".to_string(),
        TEST_MAX_CONTEXT_LENGTH,
    );
    state.error = Some("Network error".to_string());

    let content = build_content(&state, 60);
    let text: String = content
        .lines
        .iter()
        .flat_map(|l| l.spans.iter())
        .map(|s| s.content.as_ref())
        .collect();

    assert!(text.contains("Error"));
    assert!(text.contains("Network error"));
}

#[test]
fn test_build_content_response() {
    let mut state = AiState::new_with_config(
        true,
        true,
        "Anthropic".to_string(),
        "claude-3-5-sonnet-20241022".to_string(),
        TEST_MAX_CONTEXT_LENGTH,
    );
    state.response = "Try using .foo instead".to_string();

    let content = build_content(&state, 60);
    let text: String = content
        .lines
        .iter()
        .flat_map(|l| l.spans.iter())
        .map(|s| s.content.as_ref())
        .collect();

    assert!(text.contains("Try using .foo instead"));
}

#[test]
fn test_build_content_loading_with_previous() {
    let mut state = AiState::new_with_config(
        true,
        true,
        "Anthropic".to_string(),
        "claude-3-5-sonnet-20241022".to_string(),
        TEST_MAX_CONTEXT_LENGTH,
    );
    state.loading = true;
    state.previous_response = Some("Previous answer".to_string());

    let content = build_content(&state, 60);
    let text: String = content
        .lines
        .iter()
        .flat_map(|l| l.spans.iter())
        .map(|s| s.content.as_ref())
        .collect();

    assert!(text.contains("Previous answer"));
    assert!(text.contains("Thinking"));
}

// =========================================================================
// No Default AI Provider Property-Based Tests
// =========================================================================

// **Feature: no-default-ai-provider, Property 2: Unconfigured state shows setup message**
// *For any* AiState where configured is false due to missing provider, the rendered content
// SHALL contain setup instructions including "provider" configuration guidance
// **Validates: Requirements 1.1, 3.1, 3.3**
proptest! {
    #![proptest_config(ProptestConfig::with_cases(100))]

    #[test]
    fn prop_unconfigured_state_shows_setup_message(
        enabled in proptest::bool::ANY,
        provider_name in "[a-zA-Z]{3,15}",
        model_name in "[a-zA-Z0-9-]{5,30}",
        max_width in 40u16..200u16
    ) {
        // Create an unconfigured state (configured = false)
        let state = AiState::new_with_config(
            enabled,
            false,  // configured = false
            provider_name,
            model_name,
            TEST_MAX_CONTEXT_LENGTH,
        );

        let content = build_content(&state, max_width);
        let text: String = content
            .lines
            .iter()
            .flat_map(|l| l.spans.iter())
            .map(|s| s.content.as_ref())
            .collect();

        // Verify setup instructions are present
        prop_assert!(
            text.contains("AI provider not configured"),
            "Content should contain 'AI provider not configured' message"
        );

        // Verify provider configuration guidance is present
        prop_assert!(
            text.contains("provider"),
            "Content should contain 'provider' configuration guidance"
        );

        // Verify example config shows provider selection
        prop_assert!(
            text.contains("anthropic") || text.contains("openai") || text.contains("gemini") || text.contains("bedrock"),
            "Content should show provider selection options"
        );
    }
}

// **Feature: no-default-ai-provider, Property 3: Unconfigured state includes README URL**
// *For any* AiState where configured is false due to missing provider, the rendered content
// SHALL contain the URL "https://github.com/bellicose100xp/jiq#configuration"
// **Validates: Requirements 1.2, 3.2**
proptest! {
    #![proptest_config(ProptestConfig::with_cases(100))]

    #[test]
    fn prop_unconfigured_state_includes_readme_url(
        enabled in proptest::bool::ANY,
        provider_name in "[a-zA-Z]{3,15}",
        model_name in "[a-zA-Z0-9-]{5,30}",
        max_width in 40u16..200u16
    ) {
        // Create an unconfigured state (configured = false)
        let state = AiState::new_with_config(
            enabled,
            false,  // configured = false
            provider_name,
            model_name,
            TEST_MAX_CONTEXT_LENGTH,
        );

        let content = build_content(&state, max_width);
        let text: String = content
            .lines
            .iter()
            .flat_map(|l| l.spans.iter())
            .map(|s| s.content.as_ref())
            .collect();

        // Verify README URL is present
        prop_assert!(
            text.contains("https://github.com/bellicose100xp/jiq#configuration"),
            "Content should contain the README configuration URL"
        );
    }
}

// =========================================================================
// Phase 3: Selection Property-Based Tests
// =========================================================================

// **Feature: ai-assistant-phase3-actionable-suggestions, Property 11: Selection number rendering**
// *For any* AI popup with N suggestions where N ≤ 5, each suggestion should be rendered
// with its selection number (1 through N) at the start of the line in a distinct color
// (dim white or gray), followed by the suggestion type and query.
// **Validates: Requirements 4.1, 4.2, 4.3**
proptest! {
    #![proptest_config(ProptestConfig::with_cases(100))]

    #[test]
    fn prop_selection_number_rendering(suggestion_count in 1usize..=5) {
        use crate::ai::ai_state::{Suggestion, SuggestionType};

        let mut state = AiState::new_with_config(true, true, "Anthropic".to_string(), "claude-3-5-sonnet-20241022".to_string(), TEST_MAX_CONTEXT_LENGTH);
        state.visible = true;
        state.response = "AI response".to_string();

        // Create N suggestions
        state.suggestions = (0..suggestion_count)
            .map(|i| Suggestion {
                query: format!(".query{}", i),
                description: format!("Description {}", i),
                suggestion_type: SuggestionType::Fix,
            })
            .collect();

        let content = build_content(&state, 80);
        let text: String = content
            .lines
            .iter()
            .flat_map(|l| l.spans.iter())
            .map(|s| s.content.as_ref())
            .collect();

        // Verify each suggestion has its number (1-N)
        for i in 1..=suggestion_count {
            prop_assert!(
                text.contains(&format!("{}.", i)),
                "Suggestion {} should have selection number '{}.'",
                i, i
            );
        }
    }
}

// **Feature: ai-assistant-phase3-actionable-suggestions, Property 12: Selection number limit**
// *For any* AI popup with N suggestions where N > 5, only the first 5 suggestions should be
// rendered with selection numbers (1-5), and suggestions 6 through N should be rendered
// without selection numbers.
// **Validates: Requirements 4.4**
proptest! {
    #![proptest_config(ProptestConfig::with_cases(100))]

    #[test]
    fn prop_selection_number_limit(suggestion_count in 6usize..15) {
        use crate::ai::ai_state::{Suggestion, SuggestionType};

        let mut state = AiState::new_with_config(true, true, "Anthropic".to_string(), "claude-3-5-sonnet-20241022".to_string(), TEST_MAX_CONTEXT_LENGTH);
        state.visible = true;
        state.response = "AI response".to_string();

        // Create N suggestions (N > 5)
        state.suggestions = (0..suggestion_count)
            .map(|i| Suggestion {
                query: format!(".query{}", i),
                description: format!("Description {}", i),
                suggestion_type: SuggestionType::Fix,
            })
            .collect();

        let content = build_content(&state, 80);

        // Check each line to see if it starts with a number
        let mut numbered_suggestions = 0;
        for line in &content.lines {
            let line_text: String = line.spans.iter()
                .map(|s| s.content.as_ref())
                .collect();

            // Check if line starts with "N. " where N is 1-5
            for i in 1..=5 {
                if line_text.trim_start().starts_with(&format!("{}. ", i)) {
                    numbered_suggestions += 1;
                    break;
                }
            }
        }

        // Should have exactly 5 numbered suggestions
        prop_assert_eq!(
            numbered_suggestions, 5,
            "Should have exactly 5 numbered suggestions, found {}",
            numbered_suggestions
        );
    }
}

// **Feature: ai-assistant-phase3-actionable-suggestions, Property 6: Selection highlight visibility**
// *For any* suggestion selected via Alt+Up/Down navigation, the selected suggestion should be
// rendered with a distinct background color or visual indicator that differs from unselected suggestions.
// **Validates: Requirements 4.5, 8.5**
proptest! {
    #![proptest_config(ProptestConfig::with_cases(100))]

    #[test]
    fn prop_selection_highlight_visibility(
        suggestion_count in 1usize..10,
        selected_index in 0usize..10
    ) {
        use crate::ai::ai_state::{Suggestion, SuggestionType};
        use ratatui::Terminal;
        use ratatui::backend::TestBackend;
        use ratatui::layout::Rect;

        prop_assume!(selected_index < suggestion_count);

        let mut state = AiState::new_with_config(true, true, "Anthropic".to_string(), "claude-3-5-sonnet-20241022".to_string(), TEST_MAX_CONTEXT_LENGTH);
        state.visible = true;
        state.response = "AI response".to_string();

        // Create N suggestions
        state.suggestions = (0..suggestion_count)
            .map(|i| Suggestion {
                query: format!(".query{}", i),
                description: format!("Description {}", i),
                suggestion_type: SuggestionType::Fix,
            })
            .collect();

        // Select a suggestion via navigation
        for _ in 0..=selected_index {
            state.selection.navigate_next(suggestion_count);
        }

        // Render to TestBackend to check widget-level background styling
        // Use taller terminal to ensure space for all suggestions (each needs ~2 lines + 1 spacing)
        let terminal_height = 30 + (suggestion_count as u16 * 3);
        let backend = TestBackend::new(100, terminal_height);
        let mut terminal = Terminal::new(backend).unwrap();
        let mut state_mut = state; // Move state to make it mutable
        terminal.draw(|f| {
            let input_area = Rect {
                x: 0,
                y: terminal_height - 4,
                width: 100,
                height: 3,
            };
            render_popup(&mut state_mut, f, input_area);
        }).unwrap();

        let buffer = terminal.backend().buffer();

        // Check that at least one cell has the selected background (widget-level styling)
        let expected_bg = theme::ai::SUGGESTION_SELECTED_BG;
        let has_background = buffer.content.iter().any(|cell| {
            cell.bg == expected_bg
        });

        prop_assert!(
            has_background,
            "Selected suggestion should have cells with selected background color"
        );
    }
}