use crate::fuzzy_select::FuzzySelectState;
#[test]
fn test_fuzzy_select_empty_filter_shows_all() {
let state = FuzzySelectState::new(vec!["apple", "banana", "grape", "apricot"]);
assert_eq!(state.filtered().len(), 4);
}
#[test]
fn test_fuzzy_select_filter_ap_matches_three() {
let mut state = FuzzySelectState::new(vec!["apple", "banana", "grape", "apricot"]);
state.set_filter("ap");
let filtered = state.filtered();
let names: Vec<&str> = filtered
.iter()
.map(|&i| ["apple", "banana", "grape", "apricot"][i])
.collect();
assert!(names.contains(&"apple"), "filter 'ap' should match 'apple'");
assert!(
names.contains(&"grape"),
"filter 'ap' should match 'gr[ap]e'"
);
assert!(
names.contains(&"apricot"),
"filter 'ap' should match 'apricot'"
);
assert!(
!names.contains(&"banana"),
"filter 'ap' should NOT match 'banana'"
);
}
#[test]
fn test_fuzzy_select_move_down_selects_second() {
let mut state = FuzzySelectState::new(vec!["apple", "banana", "grape", "apricot"]);
state.set_filter("ap");
state.move_down();
let sel = state.selection();
assert!(sel.is_some(), "selection should be Some after move_down");
let _ = sel.unwrap(); }
#[test]
fn test_fuzzy_select_selection_no_filter() {
let state = FuzzySelectState::new(vec!["alpha", "beta", "gamma"]);
let sel = state.selection();
assert_eq!(*sel.unwrap(), "alpha");
}
#[test]
fn test_fuzzy_select_move_up_at_top_no_panic() {
let mut state = FuzzySelectState::new(vec!["a", "b", "c"]);
state.move_up(); let sel = state.selection();
assert!(sel.is_some());
}
#[test]
fn test_fuzzy_select_move_down_past_end_no_panic() {
let mut state = FuzzySelectState::new(vec!["a", "b"]);
state.move_down();
state.move_down();
state.move_down(); let sel = state.selection();
assert!(sel.is_some());
}
#[test]
fn test_fuzzy_select_clear_filter_restores_all() {
let mut state = FuzzySelectState::new(vec!["apple", "banana", "grape"]);
state.set_filter("ban");
assert_eq!(state.filtered().len(), 1);
state.set_filter("");
assert_eq!(state.filtered().len(), 3);
}
#[test]
fn test_fuzzy_select_case_insensitive() {
let mut state = FuzzySelectState::new(vec!["Apple", "BANANA", "Grape"]);
state.set_filter("ap");
let filtered = state.filtered();
assert_eq!(filtered.len(), 2);
}
#[test]
fn test_fuzzy_select_empty_list_no_selection() {
let state: FuzzySelectState<&str> = FuzzySelectState::new(vec![]);
assert_eq!(state.selection(), None);
}
#[test]
fn test_fuzzy_select_filtered_indices_valid() {
let items = vec!["cat", "car", "card", "dog"];
let mut state = FuzzySelectState::new(items.clone());
state.set_filter("ca");
for &idx in state.filtered() {
assert!(idx < items.len(), "filtered index {} out of bounds", idx);
}
}