sectioned-picker 0.1.0

Interactive terminal multi-select picker with non-selectable section headers
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
//! Rendering tests: verify the picker's visual output using TestBackend.
//!
//! These render the picker into an in-memory terminal buffer and assert on the
//! text content. This catches regressions in layout, styling, and scroll behavior
//! that pure state tests cannot detect.

use crate::SectionItem;
use crate::render::render_picker;
use crate::state::{PickerState, radio_section, section};
use ratatui::Terminal;
use ratatui::backend::TestBackend;
use snapbox::{assert_data_eq, str};

/// Render the picker into an in-memory buffer and return the text content.
fn render_to_string(width: u16, height: u16, title: &str, state: &mut PickerState) -> String {
    let backend = TestBackend::new(width, height);
    let mut terminal = Terminal::new(backend).unwrap();
    terminal
        .draw(|frame| render_picker(frame, title, state, &[]))
        .unwrap();
    terminal.backend().to_string()
}

// ============================================================================
// Basic rendering
// ============================================================================

#[test]
fn renders_title_and_sections() {
    let mut state = PickerState::new(vec![
        section("Features:", &[("logging", true), ("metrics", false)]),
        section("Actions:", &[("Add `ci` template", false)]),
    ]);
    let output = render_to_string(50, 12, "my-pack v1.0", &mut state);

    assert!(output.contains("my-pack v1.0"), "title missing");
    assert!(output.contains("Features:"), "features header missing");
    assert!(output.contains("Actions:"), "actions header missing");
    assert!(output.contains("[x] logging"), "checked item missing");
    assert!(output.contains("[ ] metrics"), "unchecked item missing");
    assert!(
        output.contains("Add `ci` template"),
        "template action missing"
    );
}

#[test]
fn cursor_shown_on_first_item() {
    let mut state = PickerState::new(vec![section("S:", &[("first", false), ("second", false)])]);
    let output = render_to_string(40, 8, "test", &mut state);

    assert!(output.contains("> [ ] first"), "cursor indicator missing");
    assert!(!output.contains("> [ ] second"), "cursor on wrong item");
}

#[test]
fn cursor_moves_with_navigation() {
    let mut state = PickerState::new(vec![section("S:", &[("a", false), ("b", false)])]);
    state.set_visible_height(10);
    state.move_down();

    let output = render_to_string(40, 8, "test", &mut state);

    assert!(!output.contains("> [ ] a"), "cursor still on first");
    assert!(output.contains("> [ ] b"), "cursor not on second");
}

#[test]
fn toggle_updates_checkbox() {
    let mut state = PickerState::new(vec![section("S:", &[("item", false)])]);
    state.toggle();

    let output = render_to_string(40, 8, "test", &mut state);
    assert!(output.contains("[x] item"), "toggle not reflected");
}

#[test]
fn footer_shows_keybindings() {
    let mut state = PickerState::new(vec![section("S:", &[("a", false)])]);
    let output = render_to_string(160, 8, "test", &mut state);

    assert!(output.contains("Navigate"), "navigation hint missing");
    assert!(output.contains("Toggle"), "toggle hint missing");
    assert!(output.contains("Confirm"), "confirm hint missing");
    assert!(output.contains("Cancel"), "cancel hint missing");
}

// ============================================================================
// Custom actions — footer rendering
// ============================================================================

#[test]
fn footer_includes_custom_action_labels() {
    use crate::PickerAction;

    let mut state = PickerState::new(vec![section("S:", &[("a", false)])]);
    let actions = vec![
        PickerAction {
            key: 'p',
            label: "Preview",
            handler: Box::new(|_ctx| {}),
        },
        PickerAction {
            key: 'o',
            label: "Open",
            handler: Box::new(|_ctx| {}),
        },
    ];

    let backend = TestBackend::new(160, 8);
    let mut terminal = Terminal::new(backend).unwrap();
    terminal
        .draw(|frame| render_picker(frame, "test", &mut state, &actions))
        .unwrap();
    let output = terminal.backend().to_string();

    assert!(output.contains("p Preview"), "action 'p Preview' missing");
    assert!(output.contains("o Open"), "action 'o Open' missing");
}

// ============================================================================
// Snapshot tests — full rendered output
// ============================================================================

#[test]
fn snapshot_single_section() {
    let mut state = PickerState::new(vec![section(
        "Dependencies:",
        &[
            ("tokio (1.38)", true),
            ("serde (1.0)", true),
            ("anyhow (1)", false),
        ],
    )]);
    let output = render_to_string(60, 10, "cli-pack v2.0", &mut state);
    assert_data_eq!(
        output,
        str![[r#"
"────────────────────────────────────────────────────────────"
" ▼ Dependencies: (2 items selected)                         "
" > [x] tokio (1.38)                                         "
"   [x] serde (1.0)                                          "
"   [ ] anyhow (1)                                           "
"                                                            "
"                                                            "
"                                                            "
"                                                            "
" cli-pack v2.0  ↑↓/jk Navigate | Space Toggle | ←/→ Collapse"

"#]]
    );
}

// ============================================================================
// Radio, collapse, descriptions, and warning banner
// ============================================================================

#[test]
fn render_radio_items_use_bullet_symbols() {
    let mut state = PickerState::new(vec![radio_section(
        "HAL:",
        &[("stm32f4", true), ("nrf52840", false)],
    )]);
    let output = render_to_string(60, 10, "embedded v0.1", &mut state);
    assert_data_eq!(
        output,
        str![[r#"
"────────────────────────────────────────────────────────────"
" ▼ HAL: (stm32f4 selected)                                  "
" > ● stm32f4                                                "
"   ○ nrf52840                                               "
"                                                            "
"                                                            "
"                                                            "
"                                                            "
"                                                            "
" embedded v0.1  ↑↓/jk Navigate | Space Toggle | ←/→ Collapse"

"#]]
    );
}

#[test]
fn render_checkbox_items_use_squares() {
    let mut state = PickerState::new(vec![section("Utils:", &[("a", true), ("b", false)])]);
    let output = render_to_string(60, 10, "pack v1", &mut state);
    assert_data_eq!(
        output,
        str![[r#"
"────────────────────────────────────────────────────────────"
" ▼ Utils: (a selected)                                      "
" > [x] a                                                    "
"   [ ] b                                                    "
"                                                            "
"                                                            "
"                                                            "
"                                                            "
"                                                            "
" pack v1  ↑↓/jk Navigate | Space Toggle | ←/→ Collapse/expan"

"#]]
    );
}

#[test]
fn render_collapsed_section_shows_chevron() {
    let mut state = PickerState::new(vec![section("Utils:", &[("a", false)]).collapsed()]);
    let output = render_to_string(60, 10, "pack v1", &mut state);
    assert_data_eq!(
        output,
        str![[r#"
"────────────────────────────────────────────────────────────"
" ▶ Utils: (pick any number)                                 "
"                                                            "
"                                                            "
"                                                            "
"                                                            "
"                                                            "
"                                                            "
"                                                            "
" pack v1  ↑↓/jk Navigate | Space Toggle | ←/→ Collapse/expan"

"#]]
    );
}

#[test]
fn render_expanded_section_shows_down_chevron() {
    let mut state = PickerState::new(vec![section("Utils:", &[("a", false)])]);
    let output = render_to_string(60, 10, "pack v1", &mut state);
    assert_data_eq!(
        output,
        str![[r#"
"────────────────────────────────────────────────────────────"
" ▼ Utils: (pick any number)                                 "
" > [ ] a                                                    "
"                                                            "
"                                                            "
"                                                            "
"                                                            "
"                                                            "
"                                                            "
" pack v1  ↑↓/jk Navigate | Space Toggle | ←/→ Collapse/expan"

"#]]
    );
}

#[test]
fn render_radio_section_header_shows_constraint() {
    let mut state = PickerState::new(vec![radio_section("HAL:", &[("a", false)])]);
    let output = render_to_string(70, 10, "embedded v0.1", &mut state);
    assert_data_eq!(
        output,
        str![[r#"
"──────────────────────────────────────────────────────────────────────"
" ▼ HAL: (pick at most one)                                            "
" > ○ a                                                                "
"                                                                      "
"                                                                      "
"                                                                      "
"                                                                      "
"                                                                      "
"                                                                      "
" embedded v0.1  ↑↓/jk Navigate | Space Toggle | ←/→ Collapse/expand | "

"#]]
    );
}

#[test]
fn render_radio_multiple_selected_shows_all_filled() {
    let mut state = PickerState::new(vec![radio_section(
        "Allocator:",
        &[("jemalloc", true), ("mimalloc", true)],
    )]);
    let output = render_to_string(60, 12, "svc v1", &mut state);
    assert_data_eq!(
        output,
        str![[r#"
" ⚠ Multiple selections in "Allocator:" — pick one to resolve"
"────────────────────────────────────────────────────────────"
" ▼ Allocator: (2 items selected)                            "
" > ● jemalloc                                               "
"   ● mimalloc                                               "
"                                                            "
"                                                            "
"                                                            "
"                                                            "
"                                                            "
"                                                            "
" svc v1  ↑↓/jk Navigate | Space Toggle | ←/→ Collapse/expand"

"#]]
    );
}

#[test]
fn render_warning_banner_for_pre_existing_conflict() {
    let mut state = PickerState::new(vec![radio_section(
        "Allocator:",
        &[("jemalloc", true), ("mimalloc", true)],
    )]);
    let output = render_to_string(70, 12, "svc v1", &mut state);
    assert_data_eq!(
        output,
        str![[r#"
" ⚠ Multiple selections in "Allocator:" — pick one to resolve          "
"──────────────────────────────────────────────────────────────────────"
" ▼ Allocator: (2 items selected)                                      "
" > ● jemalloc                                                         "
"   ● mimalloc                                                         "
"                                                                      "
"                                                                      "
"                                                                      "
"                                                                      "
"                                                                      "
"                                                                      "
" svc v1  ↑↓/jk Navigate | Space Toggle | ←/→ Collapse/expand | a Toggl"

"#]]
    );
}

#[test]
fn render_item_with_description() {
    let mut state = PickerState::new(vec![
        crate::Section::new(
            "HAL:",
            vec![SectionItem::new("stm32f4", false).with_description("STM32F4xx family")],
        )
        .radio(),
    ]);
    let output = render_to_string(70, 10, "embedded v0.1", &mut state);
    assert_data_eq!(
        output,
        str![[r#"
"──────────────────────────────────────────────────────────────────────"
" ▼ HAL: (pick at most one)                                            "
" > ○ stm32f4    STM32F4xx family                                      "
"                                                                      "
"                                                                      "
"                                                                      "
"                                                                      "
"                                                                      "
"                                                                      "
" embedded v0.1  ↑↓/jk Navigate | Space Toggle | ←/→ Collapse/expand | "

"#]]
    );
}

#[test]
fn snapshot_multiple_sections() {
    let mut state = PickerState::new(vec![
        section(
            "Features:",
            &[("observability", true), ("resilience", false)],
        ),
        section("Dependencies:", &[("tokio", true)]),
        section("Actions:", &[("Add `ci` template", false)]),
    ]);
    let output = render_to_string(60, 14, "fancy v1.0", &mut state);
    assert_data_eq!(
        output,
        str![[r#"
"────────────────────────────────────────────────────────────"
" ▼ Features: (observability selected)                       "
" > [x] observability                                        "
"   [ ] resilience                                           "
"                                                            "
" ▼ Dependencies: (tokio selected)                           "
"   [x] tokio                                                "
"                                                            "
" ▼ Actions: (pick any number)                               "
"   [ ] Add `ci` template                                    "
"                                                            "
"                                                            "
"                                                            "
" fancy v1.0  ↑↓/jk Navigate | Space Toggle | ←/→ Collapse/ex"

"#]]
    );
}

// ============================================================================
// Header hint text (selection summary)
// ============================================================================

#[test]
fn radio_header_shows_pick_at_most_one_when_nothing_selected() {
    let mut state = PickerState::new(vec![radio_section(
        "HAL:",
        &[("esp32", false), ("nrf52840", false)],
    )]);
    let output = render_to_string(60, 8, "test", &mut state);
    assert!(
        output.contains("HAL: (pick at most one)"),
        "expected 'pick at most one' hint, got:\n{output}"
    );
}

#[test]
fn radio_header_shows_selected_item_name() {
    let mut state = PickerState::new(vec![radio_section(
        "HAL:",
        &[("esp32", false), ("nrf52840", true)],
    )]);
    let output = render_to_string(60, 8, "test", &mut state);
    assert!(
        output.contains("HAL: (nrf52840 selected)"),
        "expected selected item name in header, got:\n{output}"
    );
}

#[test]
fn checkbox_header_shows_pick_any_when_nothing_selected() {
    let mut state = PickerState::new(vec![section(
        "Drivers:",
        &[("ssd1306", false), ("bme280", false)],
    )]);
    let output = render_to_string(60, 8, "test", &mut state);
    assert!(
        output.contains("Drivers: (pick any number)"),
        "expected 'pick any number' hint, got:\n{output}"
    );
}

#[test]
fn checkbox_header_shows_single_selected_item_name() {
    let mut state = PickerState::new(vec![section(
        "Drivers:",
        &[("ssd1306", true), ("bme280", false)],
    )]);
    let output = render_to_string(60, 8, "test", &mut state);
    assert!(
        output.contains("Drivers: (ssd1306 selected)"),
        "expected selected item name in header, got:\n{output}"
    );
}

#[test]
fn checkbox_header_shows_count_when_multiple_selected() {
    let mut state = PickerState::new(vec![section(
        "Drivers:",
        &[("ssd1306", true), ("bme280", true), ("lis3dh", false)],
    )]);
    let output = render_to_string(60, 8, "test", &mut state);
    assert!(
        output.contains("Drivers: (2 items selected)"),
        "expected count in header, got:\n{output}"
    );
}

// ============================================================================
// Collapsed header highlight
// ============================================================================

#[test]
fn collapsed_header_is_highlighted_when_focused() {
    // After collapsing, the cursor lands on the section header and the render
    // must apply the cursor highlight (Cyan background) to that header line.
    use ratatui::style::Color;

    let mut state = PickerState::new(vec![
        section("Utils:", &[("a", false), ("b", false)]),
        section("Other:", &[("c", false)]),
    ]);
    // Cursor starts on first item in "Utils:". Collapse moves it to the header.
    state.collapse_current();

    let backend = TestBackend::new(60, 10);
    let mut terminal = Terminal::new(backend).unwrap();
    terminal
        .draw(|frame| render_picker(frame, "test", &mut state, &[]))
        .unwrap();

    // Row 1 is the header line (row 0 is the top border). The collapsed header
    // should have the Cyan cursor highlight background.
    let buf = terminal.backend().buffer();
    let header_cell = &buf[(2, 1)]; // column 2 = start of "▶" after the padding
    assert_eq!(
        header_cell.bg,
        Color::Cyan,
        "collapsed header should be highlighted with Cyan background when focused"
    );
}