tuisage 0.2.1

TUI application for interacting with CLI commands defined by usage specs
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
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
//! Self-contained choice select overlay component.
//!
//! Manages its own open/close lifecycle, selection, filtering, scroll,
//! and text input. Reports [`ChoiceSelectAction::Selected`] or
//! [`ChoiceSelectAction::Cancelled`] — the parent interprets the result.

use crossterm::event::{KeyCode, KeyEvent, MouseEvent};
use nucleo_matcher::{Config, Matcher};
use ratatui::{buffer::Buffer, layout::Rect, widgets::Borders};
use ratatui_interact::components::InputState;

use crate::app::fuzzy_match_score;
use crate::theme::UiColors;

use super::select_list::{SelectList, SelectListScrollState};
use super::{Component, EventResult, OverlayContent, OverlayRequest};

/// Compute the scroll offset for a list with the given selected index and
/// visible item count. Matches the formula in `SelectList::render()`.
fn compute_scroll_offset(selected: Option<usize>, visible_items: usize) -> usize {
    if let Some(sel) = selected {
        if visible_items > 0 && sel >= visible_items {
            sel.saturating_sub(visible_items - 1)
        } else {
            0
        }
    } else {
        0
    }
}

// ── Actions ────────────────────────────────────────────────────────

/// Actions emitted by the choice select component.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ChoiceSelectAction {
    /// User confirmed a selection. Contains the chosen text.
    Selected(String),
    /// User cancelled (Esc). Contains the typed text to keep.
    Cancelled(String),
}

// ── Inner state ────────────────────────────────────────────────────

#[derive(Debug, Clone)]
struct ChoiceSelectInner {
    choices: Vec<String>,
    descriptions: Vec<Option<String>>,
    selected_index: Option<usize>,
    filter_active: bool,
    edit_input: InputState,
    /// Anchor point for overlay positioning (set by parent panel).
    anchor: Rect,
}

impl ChoiceSelectInner {
    /// Get filtered choices as (original_index, label) pairs.
    fn filtered_choices(&self) -> Vec<(usize, String)> {
        let filter = self.edit_input.text();
        if filter.is_empty() || !self.filter_active {
            return self
                .choices
                .iter()
                .enumerate()
                .map(|(i, c)| (i, c.clone()))
                .collect();
        }
        let mut matcher = Matcher::new(Config::DEFAULT);
        self.choices
            .iter()
            .enumerate()
            .filter(|(i, c)| {
                fuzzy_match_score(c, filter, &mut matcher) > 0
                    || self
                        .descriptions
                        .get(*i)
                        .and_then(|d| d.as_deref())
                        .map(|desc| fuzzy_match_score(desc, filter, &mut matcher) > 0)
                        .unwrap_or(false)
            })
            .map(|(i, c)| (i, c.clone()))
            .collect()
    }

    /// Resolve the value to emit: selected choice text or typed input.
    fn resolve_value(&self) -> String {
        let filtered = self.filtered_choices();
        if let Some(idx) = self.selected_index {
            if let Some((_, text)) = filtered.get(idx) {
                return text.clone();
            }
        }
        self.edit_input.text().to_string()
    }

    fn description(&self, original_index: usize) -> Option<&str> {
        self.descriptions
            .get(original_index)
            .and_then(|d| d.as_deref())
    }
}

// ── Component ──────────────────────────────────────────────────────

/// Self-contained choice/completion select overlay.
/// Owns its own open/close state, selection index, scroll, and filter.
#[derive(Debug, Clone, Default)]
pub struct ChoiceSelectComponent {
    state: Option<ChoiceSelectInner>,
    /// Mouse position from parent, used for hover highlighting.
    mouse_position: Option<(u16, u16)>,
}

impl ChoiceSelectComponent {
    pub fn new() -> Self {
        Self {
            state: None,
            mouse_position: None,
        }
    }

    /// Open with the given choices, pre-selecting the current value.
    pub fn open(&mut self, choices: Vec<String>, current_value: &str, anchor: Rect) {
        let selected_index = choices.iter().position(|c| c == current_value);
        let mut edit_input = InputState::empty();
        edit_input.set_text(current_value.to_string());
        self.state = Some(ChoiceSelectInner {
            choices,
            descriptions: Vec::new(),
            selected_index,
            filter_active: false,
            edit_input,
            anchor,
        });
    }

    /// Open with choices and descriptions (for completions).
    pub fn open_with_descriptions(
        &mut self,
        choices: Vec<String>,
        descriptions: Vec<Option<String>>,
        current_value: &str,
        anchor: Rect,
    ) {
        self.open(choices, current_value, anchor);
        if let Some(ref mut inner) = self.state {
            inner.descriptions = descriptions;
        }
    }

    pub fn is_open(&self) -> bool {
        self.state.is_some()
    }

    pub fn close(&mut self) {
        self.state = None;
    }

    /// The current typed text (for syncing to the parent's edit state).
    pub fn typed_text(&self) -> &str {
        self.state
            .as_ref()
            .map(|s| s.edit_input.text())
            .unwrap_or("")
    }

    /// The text before the cursor (for inline edit rendering).
    pub fn text_before_cursor(&self) -> &str {
        self.state
            .as_ref()
            .map(|s| s.edit_input.text_before_cursor())
            .unwrap_or("")
    }

    /// The text after the cursor (for inline edit rendering).
    pub fn text_after_cursor(&self) -> &str {
        self.state
            .as_ref()
            .map(|s| s.edit_input.text_after_cursor())
            .unwrap_or("")
    }

    /// Compute the scroll offset for a given number of visible items.
    /// This derives the offset from the current `selected_index` and the
    /// viewport size, matching the formula used by `SelectList::render()`.
    pub fn compute_scroll_offset(&self, visible_items: usize) -> usize {
        compute_scroll_offset(
            self.state.as_ref().and_then(|s| s.selected_index),
            visible_items,
        )
    }

    /// Current selected index in the filtered list (None = text input mode).
    #[cfg(test)]
    pub fn selected_index(&self) -> Option<usize> {
        self.state.as_ref().and_then(|s| s.selected_index)
    }

    /// Filtered choices as (original_index, label) pairs.
    pub fn filtered_choices(&self) -> Vec<(usize, String)> {
        self.state
            .as_ref()
            .map(|s| s.filtered_choices())
            .unwrap_or_default()
    }

    /// Click-select: set selected index and confirm.
    #[allow(dead_code)] // used by mouse handling
    pub fn click_select(&mut self, index: usize) -> Option<ChoiceSelectAction> {
        let inner = self.state.as_mut()?;
        let filtered = inner.filtered_choices();
        if index < filtered.len() {
            inner.selected_index = Some(index);
            let value = inner.resolve_value();
            self.state = None;
            Some(ChoiceSelectAction::Selected(value))
        } else {
            None
        }
    }

    /// Update the anchor rect for overlay positioning.
    /// Called during rendering when the actual panel area is known.
    pub fn set_anchor(&mut self, anchor: Rect) {
        if let Some(ref mut inner) = self.state {
            inner.anchor = anchor;
        }
    }

    /// Update the mouse position for hover highlighting in the overlay.
    pub fn set_mouse_position(&mut self, pos: Option<(u16, u16)>) {
        self.mouse_position = pos;
    }
}

impl Component for ChoiceSelectComponent {
    type Action = ChoiceSelectAction;

    fn handle_key(&mut self, key: KeyEvent) -> EventResult<ChoiceSelectAction> {
        let Some(ref mut inner) = self.state else {
            return EventResult::NotHandled;
        };

        match key.code {
            KeyCode::Enter => {
                let value = inner.resolve_value();
                self.state = None;
                EventResult::Action(ChoiceSelectAction::Selected(value))
            }
            KeyCode::Esc => {
                let typed = inner.edit_input.text().to_string();
                self.state = None;
                EventResult::Action(ChoiceSelectAction::Cancelled(typed))
            }
            KeyCode::Up => {
                match inner.selected_index {
                    Some(0) | None => inner.selected_index = None,
                    Some(idx) => inner.selected_index = Some(idx - 1),
                }
                EventResult::Consumed
            }
            KeyCode::Down => {
                let filtered_len = inner.filtered_choices().len();
                if filtered_len > 0 {
                    match inner.selected_index {
                        None => inner.selected_index = Some(0),
                        Some(idx) if idx + 1 < filtered_len => {
                            inner.selected_index = Some(idx + 1);
                        }
                        _ => {}
                    }
                }
                EventResult::Consumed
            }
            KeyCode::Backspace => {
                inner.edit_input.delete_char_backward();
                inner.filter_active = true;
                inner.selected_index = None;
                EventResult::Consumed
            }
            KeyCode::Left => {
                inner.edit_input.move_left();
                EventResult::Consumed
            }
            KeyCode::Right => {
                inner.edit_input.move_right();
                EventResult::Consumed
            }
            KeyCode::Char(c) => {
                inner.edit_input.insert_char(c);
                inner.filter_active = true;
                inner.selected_index = None;
                EventResult::Consumed
            }
            _ => EventResult::NotHandled,
        }
    }

    fn handle_mouse(&mut self, _event: MouseEvent, _area: Rect) -> EventResult<ChoiceSelectAction> {
        EventResult::NotHandled
    }

    fn collect_overlays(&mut self) -> Vec<OverlayRequest> {
        let Some(ref inner) = self.state else {
            return Vec::new();
        };

        let filtered = inner.filtered_choices();
        let anchor = inner.anchor;

        // Compute preferred size
        let max_choice_len = filtered
            .iter()
            .map(|(_, c)| c.chars().count())
            .max()
            .unwrap_or(10) as u16;

        let descriptions: Vec<Option<String>> = filtered
            .iter()
            .map(|(orig_idx, _)| inner.description(*orig_idx).map(|s| s.to_string()))
            .collect();

        let max_desc_len = descriptions
            .iter()
            .map(|d| d.as_ref().map(|s| s.chars().count() + 2).unwrap_or(0))
            .max()
            .unwrap_or(0) as u16;

        let width = max_choice_len + max_desc_len + 4;
        let max_visible = 10u16;
        let height = if filtered.is_empty() {
            2 // "(no matches)" + bottom border
        } else {
            (filtered.len() as u16).min(max_visible) + 1 // items + bottom border
        };

        // Build the snapshot of data needed to render
        let labels: Vec<String> = filtered.iter().map(|(_, c)| c.clone()).collect();
        let selected_index = inner.selected_index;

        vec![OverlayRequest {
            anchor,
            size: (width, height),
            content: Box::new(ChoiceSelectOverlay {
                labels,
                descriptions,
                selected_index,
                mouse_position: self.mouse_position,
            }),
        }]
    }
}

// ── OverlayContent implementation ──────────────────────────────────

/// Snapshot of choice select data needed to render the overlay.
/// Created during collect_overlays(), rendered by the coordinator.
struct ChoiceSelectOverlay {
    labels: Vec<String>,
    descriptions: Vec<Option<String>>,
    selected_index: Option<usize>,
    mouse_position: Option<(u16, u16)>,
}

impl ChoiceSelectOverlay {
    /// Compute hovered item index from mouse position relative to the overlay area.
    fn hovered_index(&self, area: Rect) -> Option<usize> {
        let (col, row) = self.mouse_position?;
        let inner_top = area.y; // no top border
        let inner_bottom = area.y + area.height.saturating_sub(1);
        if col >= area.x
            && col < area.x + area.width
            && row >= inner_top
            && row < inner_bottom
        {
            let visible_items = area.height.saturating_sub(1) as usize; // minus bottom border
            let scroll = compute_scroll_offset(self.selected_index, visible_items);
            let idx = (row - inner_top) as usize + scroll;
            if idx < self.labels.len() {
                Some(idx)
            } else {
                None
            }
        } else {
            None
        }
    }
}

impl OverlayContent for ChoiceSelectOverlay {
    fn render(&self, area: Rect, buf: &mut Buffer, colors: &UiColors) {
        let hovered = self.hovered_index(area);
        let widget = SelectList::new(
            String::new(),
            &self.labels,
            self.selected_index,
            colors.choice,
            colors.choice,
            colors,
        )
        .with_descriptions(&self.descriptions)
        .with_borders(Borders::LEFT | Borders::RIGHT | Borders::BOTTOM)
        .with_hovered(hovered);

        let mut scroll_state = SelectListScrollState::default();
        ratatui::widgets::StatefulWidget::render(widget, area, buf, &mut scroll_state);
    }
}

// ── Tests ──────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;

    fn make_choices() -> Vec<String> {
        vec![
            "alpha".to_string(),
            "beta".to_string(),
            "gamma".to_string(),
            "delta".to_string(),
        ]
    }

    #[test]
    fn test_open_close_lifecycle() {
        let mut cs = ChoiceSelectComponent::new();
        assert!(!cs.is_open());

        cs.open(make_choices(), "beta", Rect::new(0, 0, 20, 1));
        assert!(cs.is_open());

        cs.close();
        assert!(!cs.is_open());
    }

    #[test]
    fn test_open_preselects_matching_value() {
        let mut cs = ChoiceSelectComponent::new();
        cs.open(make_choices(), "gamma", Rect::new(0, 0, 20, 1));

        // Should have pre-selected index 2 (gamma)
        let inner = cs.state.as_ref().unwrap();
        assert_eq!(inner.selected_index, Some(2));
    }

    #[test]
    fn test_open_no_match_preselects_none() {
        let mut cs = ChoiceSelectComponent::new();
        cs.open(make_choices(), "nonexistent", Rect::new(0, 0, 20, 1));

        let inner = cs.state.as_ref().unwrap();
        assert_eq!(inner.selected_index, None);
    }

    #[test]
    fn test_enter_confirms_selected() {
        let mut cs = ChoiceSelectComponent::new();
        cs.open(make_choices(), "beta", Rect::new(0, 0, 20, 1));

        let key = KeyEvent::from(KeyCode::Enter);
        let result = cs.handle_key(key);

        assert_eq!(
            result,
            EventResult::Action(ChoiceSelectAction::Selected("beta".to_string()))
        );
        assert!(!cs.is_open());
    }

    #[test]
    fn test_enter_with_no_selection_uses_typed_text() {
        let mut cs = ChoiceSelectComponent::new();
        cs.open(make_choices(), "custom", Rect::new(0, 0, 20, 1));
        // No selection, typed text is "custom"

        let key = KeyEvent::from(KeyCode::Enter);
        let result = cs.handle_key(key);

        assert_eq!(
            result,
            EventResult::Action(ChoiceSelectAction::Selected("custom".to_string()))
        );
    }

    #[test]
    fn test_esc_cancels_with_typed_text() {
        let mut cs = ChoiceSelectComponent::new();
        cs.open(make_choices(), "beta", Rect::new(0, 0, 20, 1));

        let key = KeyEvent::from(KeyCode::Esc);
        let result = cs.handle_key(key);

        assert_eq!(
            result,
            EventResult::Action(ChoiceSelectAction::Cancelled("beta".to_string()))
        );
        assert!(!cs.is_open());
    }

    #[test]
    fn test_down_navigates_selection() {
        let mut cs = ChoiceSelectComponent::new();
        cs.open(make_choices(), "", Rect::new(0, 0, 20, 1));
        // No selection initially

        let down = KeyEvent::from(KeyCode::Down);
        assert_eq!(cs.handle_key(down), EventResult::Consumed);
        assert_eq!(cs.state.as_ref().unwrap().selected_index, Some(0));

        let down = KeyEvent::from(KeyCode::Down);
        assert_eq!(cs.handle_key(down), EventResult::Consumed);
        assert_eq!(cs.state.as_ref().unwrap().selected_index, Some(1));
    }

    #[test]
    fn test_up_from_first_deselects() {
        let mut cs = ChoiceSelectComponent::new();
        cs.open(make_choices(), "alpha", Rect::new(0, 0, 20, 1));
        // Pre-selected at index 0

        let up = KeyEvent::from(KeyCode::Up);
        assert_eq!(cs.handle_key(up), EventResult::Consumed);
        assert_eq!(cs.state.as_ref().unwrap().selected_index, None);
    }

    #[test]
    fn test_typing_activates_filter_and_clears_selection() {
        let mut cs = ChoiceSelectComponent::new();
        cs.open(make_choices(), "beta", Rect::new(0, 0, 20, 1));
        assert_eq!(cs.state.as_ref().unwrap().selected_index, Some(1));
        assert!(!cs.state.as_ref().unwrap().filter_active);

        let key = KeyEvent::from(KeyCode::Char('a'));
        assert_eq!(cs.handle_key(key), EventResult::Consumed);

        let inner = cs.state.as_ref().unwrap();
        assert!(inner.filter_active);
        assert_eq!(inner.selected_index, None);
        assert_eq!(inner.edit_input.text(), "betaa");
    }

    #[test]
    fn test_filtering_narrows_choices() {
        let mut cs = ChoiceSelectComponent::new();
        cs.open(make_choices(), "", Rect::new(0, 0, 20, 1));

        // Type "al" to filter
        cs.handle_key(KeyEvent::from(KeyCode::Char('a')));
        cs.handle_key(KeyEvent::from(KeyCode::Char('l')));

        let filtered = cs.filtered_choices();
        // "alpha" matches "al"
        assert!(filtered.iter().any(|(_, c)| c == "alpha"));
        // "beta" and "gamma" should not match "al"
        assert!(!filtered.iter().any(|(_, c)| c == "beta"));
        assert!(!filtered.iter().any(|(_, c)| c == "gamma"));
        // Result set is narrowed from the original 4
        assert!(filtered.len() < 4);
    }

    #[test]
    fn test_backspace_activates_filter() {
        let mut cs = ChoiceSelectComponent::new();
        cs.open(make_choices(), "beta", Rect::new(0, 0, 20, 1));

        let key = KeyEvent::from(KeyCode::Backspace);
        assert_eq!(cs.handle_key(key), EventResult::Consumed);

        let inner = cs.state.as_ref().unwrap();
        assert!(inner.filter_active);
        assert_eq!(inner.selected_index, None);
        assert_eq!(inner.edit_input.text(), "bet");
    }

    #[test]
    fn test_not_handled_when_closed() {
        let mut cs = ChoiceSelectComponent::new();
        let key = KeyEvent::from(KeyCode::Enter);
        assert_eq!(cs.handle_key(key), EventResult::NotHandled);
    }

    #[test]
    fn test_collect_overlays_when_closed() {
        let mut cs = ChoiceSelectComponent::new();
        assert!(cs.collect_overlays().is_empty());
    }

    #[test]
    fn test_collect_overlays_when_open() {
        let mut cs = ChoiceSelectComponent::new();
        cs.open(make_choices(), "", Rect::new(5, 10, 20, 1));

        let overlays = cs.collect_overlays();
        assert_eq!(overlays.len(), 1);

        let req = &overlays[0];
        assert_eq!(req.anchor, Rect::new(5, 10, 20, 1));
        // Width should accommodate labels + padding
        assert!(req.size.0 > 0);
        // Height = 4 items (capped at max 10) + 1 bottom border
        assert_eq!(req.size.1, 5);
    }

    #[test]
    fn test_click_select() {
        let mut cs = ChoiceSelectComponent::new();
        cs.open(make_choices(), "", Rect::new(0, 0, 20, 1));

        let result = cs.click_select(2);
        assert_eq!(
            result,
            Some(ChoiceSelectAction::Selected("gamma".to_string()))
        );
        assert!(!cs.is_open());
    }

    #[test]
    fn test_click_select_out_of_range() {
        let mut cs = ChoiceSelectComponent::new();
        cs.open(make_choices(), "", Rect::new(0, 0, 20, 1));

        let result = cs.click_select(100);
        assert_eq!(result, None);
        assert!(cs.is_open()); // stays open
    }

    #[test]
    fn test_open_with_descriptions() {
        let mut cs = ChoiceSelectComponent::new();
        let descs = vec![
            Some("First letter".to_string()),
            None,
            Some("Third letter".to_string()),
            None,
        ];
        cs.open_with_descriptions(make_choices(), descs.clone(), "", Rect::new(0, 0, 20, 1));

        let inner = cs.state.as_ref().unwrap();
        assert_eq!(inner.descriptions, descs);
    }

    #[test]
    fn test_filtering_matches_description() {
        let mut cs = ChoiceSelectComponent::new();
        let descs = vec![
            Some("Authentication plugin".to_string()),
            Some("Usage analytics".to_string()),
            Some("Redis cache layer".to_string()),
        ];
        cs.open_with_descriptions(
            vec!["auth".to_string(), "analytics".to_string(), "cache".to_string()],
            descs,
            "",
            Rect::new(0, 0, 40, 1),
        );

        // Type "redis" — matches the description of "cache", not its name
        cs.handle_key(KeyEvent::from(KeyCode::Char('r')));
        cs.handle_key(KeyEvent::from(KeyCode::Char('e')));
        cs.handle_key(KeyEvent::from(KeyCode::Char('d')));
        cs.handle_key(KeyEvent::from(KeyCode::Char('i')));
        cs.handle_key(KeyEvent::from(KeyCode::Char('s')));

        let filtered = cs.filtered_choices();
        assert_eq!(filtered.len(), 1, "Only 'cache' (whose description contains 'redis') should match");
        assert_eq!(filtered[0].1, "cache");
    }

    #[test]
    fn test_filtering_matches_both_name_and_description() {
        let mut cs = ChoiceSelectComponent::new();
        let descs = vec![
            Some("Authentication plugin".to_string()),
            Some("Analytics module".to_string()),
            Some("Redis cache layer".to_string()),
        ];
        cs.open_with_descriptions(
            vec!["auth".to_string(), "analytics".to_string(), "cache".to_string()],
            descs,
            "",
            Rect::new(0, 0, 40, 1),
        );

        // Type "an" — matches "analytics" by name AND "Authentication plugin" by description
        cs.handle_key(KeyEvent::from(KeyCode::Char('a')));
        cs.handle_key(KeyEvent::from(KeyCode::Char('n')));

        let filtered = cs.filtered_choices();
        // Should include items matching by name or description
        assert!(
            filtered.iter().any(|(_, c)| c == "auth"),
            "auth should match via description 'Authentication plugin'"
        );
        assert!(
            filtered.iter().any(|(_, c)| c == "analytics"),
            "analytics should match via its name"
        );
    }

    #[test]
    fn test_down_does_not_exceed_filtered_count() {
        let mut cs = ChoiceSelectComponent::new();
        cs.open(vec!["a".to_string(), "b".to_string()], "", Rect::new(0, 0, 20, 1));

        // Navigate to last item
        cs.handle_key(KeyEvent::from(KeyCode::Down)); // -> 0
        cs.handle_key(KeyEvent::from(KeyCode::Down)); // -> 1
        cs.handle_key(KeyEvent::from(KeyCode::Down)); // stays at 1

        assert_eq!(cs.state.as_ref().unwrap().selected_index, Some(1));
    }

    #[test]
    fn test_overlay_renders_without_panic() {
        let mut cs = ChoiceSelectComponent::new();
        cs.open(make_choices(), "beta", Rect::new(0, 0, 20, 1));

        let overlays = cs.collect_overlays();
        assert_eq!(overlays.len(), 1);

        let req = &overlays[0];
        let area = Rect::new(0, 0, req.size.0, req.size.1);
        let mut buf = Buffer::empty(area);
        let palette = ratatui_themes::Theme::default().palette();
        let colors = UiColors::from_palette(&palette);
        req.content.render(area, &mut buf, &colors);
    }

    /// Helper: create 15 choices (enough to scroll with max_visible=10).
    fn make_many_choices() -> Vec<String> {
        (0..15).map(|i| format!("item_{i:02}")).collect()
    }

    #[test]
    fn test_click_select_with_scroll_offset() {
        // When the list is scrolled (selected item past visible area),
        // click_select should select based on the filtered index, NOT visual row.
        // The caller (App) must add scroll_offset to the visual row before calling.
        let mut cs = ChoiceSelectComponent::new();
        cs.open(make_many_choices(), "", Rect::new(0, 0, 20, 1));

        // Navigate down to item 12 so the list scrolls
        let down = KeyEvent::from(KeyCode::Down);
        for _ in 0..13 {
            cs.handle_key(down);
        }
        assert_eq!(cs.selected_index(), Some(12));

        // The scroll_offset should account for the selected item.
        // With 10 visible items and selected=12: offset = 12 - (10-1) = 3
        // So clicking on visual row 0 should map to item 3.
        // The caller must compute: clicked_index = visual_row + scroll_offset(visible_items)
        let visible = 10usize;
        let scroll = cs.compute_scroll_offset(visible);
        assert_eq!(scroll, 3, "scroll_offset should be 3 when selected=12, visible=10");

        // Clicking visual row 5 → actual index 5 + 3 = 8
        let clicked_index = 5 + scroll;
        let result = cs.click_select(clicked_index);
        assert_eq!(
            result,
            Some(ChoiceSelectAction::Selected("item_08".to_string())),
            "Should select item_08 (visual row 5 + scroll 3)"
        );
    }

    #[test]
    fn test_scroll_offset_computation() {
        let mut cs = ChoiceSelectComponent::new();
        cs.open(make_many_choices(), "", Rect::new(0, 0, 20, 1));

        // No selection → offset 0
        assert_eq!(cs.compute_scroll_offset(10), 0);

        // Select item 5 (visible within 10 items) → offset 0
        let down = KeyEvent::from(KeyCode::Down);
        for _ in 0..6 {
            cs.handle_key(down);
        }
        assert_eq!(cs.selected_index(), Some(5));
        assert_eq!(cs.compute_scroll_offset(10), 0);

        // Select item 10 → offset = 10 - 9 = 1
        for _ in 0..5 {
            cs.handle_key(down);
        }
        assert_eq!(cs.selected_index(), Some(10));
        assert_eq!(cs.compute_scroll_offset(10), 1);

        // Select item 14 → offset = 14 - 9 = 5
        for _ in 0..4 {
            cs.handle_key(down);
        }
        assert_eq!(cs.selected_index(), Some(14));
        assert_eq!(cs.compute_scroll_offset(10), 5);
    }

    #[test]
    fn test_hover_accounts_for_scroll() {
        // When the choice list is scrolled, hovering over the 4th visible row
        // should highlight item at index (4 + scroll_offset), not item 4.
        let mut cs = ChoiceSelectComponent::new();
        cs.open(make_many_choices(), "", Rect::new(0, 0, 20, 1));

        // Navigate to item 12 to cause scroll
        let down = KeyEvent::from(KeyCode::Down);
        for _ in 0..13 {
            cs.handle_key(down);
        }
        assert_eq!(cs.selected_index(), Some(12));

        // Set mouse position at visual row 3 (within overlay area starting at y=1)
        // Overlay area: x=0, y=1, width=20, height=11 (10 items + 1 bottom border)
        cs.set_mouse_position(Some((5, 4))); // row 4, within overlay at y=1 → visual row 3

        let overlays = cs.collect_overlays();
        assert_eq!(overlays.len(), 1);

        let req = &overlays[0];
        // Simulate the actual overlay area
        let area = Rect::new(0, 1, req.size.0, req.size.1);
        let mut buf = Buffer::empty(Rect::new(0, 0, 30, 20));
        let palette = ratatui_themes::Theme::default().palette();
        let colors = UiColors::from_palette(&palette);
        req.content.render(area, &mut buf, &colors);

        // With scroll_offset=3, visual row 3 should highlight item 6 (3+3),
        // not item 3. Verify by checking the buffer for hover highlight on the correct row.
        // The hover highlight uses colors.choice.hover_bg.
        // Visual row 3 in the overlay (row=4 absolute) should have hover_bg.
        let hover_row = 4u16; // absolute row where we set the mouse
        let cell = buf.cell((1, hover_row)).unwrap(); // col 1 to skip border
        assert_ne!(
            cell.bg,
            ratatui::style::Color::Reset,
            "Hovered row should have a background highlight"
        );
    }
}