use crate::tui::onboarding_render::visible_window;
const MAX: usize = 8;
#[test]
fn a_list_that_fits_is_shown_whole() {
assert_eq!(visible_window(5, 0, MAX), (0, 5));
assert_eq!(visible_window(5, 4, MAX), (0, 5));
assert_eq!(visible_window(MAX, 3, MAX), (0, MAX));
}
#[test]
fn an_empty_list_is_an_empty_window() {
assert_eq!(visible_window(0, 0, MAX), (0, 0));
}
#[test]
fn a_long_list_is_capped_at_the_window() {
let (start, end) = visible_window(20, 0, MAX);
assert_eq!(end - start, MAX, "never more rows than the cap");
}
#[test]
fn the_selection_is_always_inside_the_window() {
for total in [1usize, 7, 8, 9, 18, 50] {
for selected in 0..total {
let (start, end) = visible_window(total, selected, MAX);
assert!(
selected >= start && selected < end,
"selection {selected} fell outside [{start},{end}) for total {total}"
);
assert!(end <= total, "window ran past the end for total {total}");
assert!(end - start <= MAX, "window exceeded the cap");
}
}
}
#[test]
fn the_last_item_does_not_leave_a_window_of_blanks() {
let (start, end) = visible_window(18, 17, MAX);
assert_eq!(end, 18, "the window must stop at the end of the list");
assert_eq!(end - start, MAX, "and still be full");
}
#[test]
fn the_first_item_keeps_the_window_at_the_top() {
let (start, end) = visible_window(18, 0, MAX);
assert_eq!((start, end), (0, MAX));
}
#[test]
fn the_selection_is_centred_in_the_middle_of_a_long_list() {
let (start, end) = visible_window(18, 9, MAX);
assert!(start > 0 && end < 18, "expected truncation both ways");
assert_eq!(start, 9 - MAX / 2);
}
#[test]
fn a_zero_width_window_asks_for_nothing() {
assert_eq!(visible_window(10, 5, 0), (0, 10));
}
#[test]
fn a_stock_provider_list_leaves_room_for_the_rest_of_the_form() {
const PROVIDERS: usize = 18;
let (start, end) = visible_window(PROVIDERS, PROVIDERS / 2, MAX);
let hints = usize::from(start > 0) + usize::from(end < PROVIDERS);
let rows = (end - start) + hints;
assert_eq!(hints, 2, "expected truncation both ways at the midpoint");
assert!(
rows <= 10,
"the list plus its scroll hints took {rows} of ~20 usable rows in an \
80x24 box, leaving nothing for the model and key fields below"
);
}