1
//! The scrollbar parts a frame records are the cells the bar is drawn in.
2

            
3
use crate::app::App;
4
use crate::hits::{Hits, Surface, Target};
5
use crate::modal::{Draft, Modal};
6
use crate::tabs::fetch::Fetch;
7
use crate::test_support::draw_frame;
8
use crate::view::Tab;
9
use ratatui::buffer::Buffer;
10
use ratatui::layout::Rect;
11
use tvui::BarPart;
12

            
13
67
fn symbol(part: BarPart) -> &'static str {
14
67
    match part {
15
4
        BarPart::Up => "\u{25B2}",
16
4
        BarPart::Down => "\u{25BC}",
17
19
        BarPart::Thumb => "\u{25A0}",
18
40
        BarPart::PageUp | BarPart::PageDown => "\u{2592}",
19
    }
20
67
}
21

            
22
8
fn parts(hits: &Hits, surface: Surface) -> Vec<(Rect, BarPart)> {
23
8
    hits.entries()
24
8
        .iter()
25
220
        .filter_map(|(rect, target)| match target {
26
24
            Target::Scroll(found, part) if *found == surface => Some((*rect, *part)),
27
196
            _ => None,
28
220
        })
29
        .collect()
30
8
}
31

            
32
/// Each recorded part covers cells drawn with its symbol; together they are one column.
33
4
fn assert_on_screen(buffer: &Buffer, parts: &[(Rect, BarPart)]) {
34
4
    assert!(!parts.is_empty());
35
4
    let column = parts[0].0.x;
36
4
    for (rect, part) in parts {
37
16
        assert_eq!(rect.x, column);
38
16
        for y in rect.top()..rect.bottom() {
39
67
            assert_eq!(buffer[(rect.x, y)].symbol(), symbol(*part), "{part:?} {y}");
40
        }
41
    }
42
8
    let ends = |first: bool| {
43
8
        let end = if first { parts.first() } else { parts.last() };
44
8
        end.map(|(_, part)| *part)
45
8
    };
46
4
    assert_eq!(ends(true), Some(BarPart::Up));
47
4
    assert_eq!(ends(false), Some(BarPart::Down));
48
4
}
49

            
50
3
fn accounts(count: usize) -> App {
51
3
    let mut app = crate::test_support::app();
52
3
    app.switch_tab(Tab::Accounts);
53
123
    let row = |i: usize| format!(r#"(:account :id "u{i}" :name "a{i}" :parent "")"#);
54
3
    let rows: Vec<String> = (0..count).map(row).collect();
55
3
    app.accounts_mut()
56
3
        .on_reply(&format!("(:id 1 :value ({}))", rows.join(" ")));
57
3
    app
58
3
}
59

            
60
#[test]
61
1
fn a_list_with_hidden_rows_records_its_bar_and_the_whole_panel_as_a_body() {
62
1
    let app = accounts(60);
63
1
    let (buffer, hits) = draw_frame(&app, 80, 24);
64
1
    assert_on_screen(&buffer, &parts(&hits, Surface::List));
65
1
    assert!(
66
1
        hits.entries()
67
1
            .iter()
68
2
            .any(|(_, target)| *target == Target::Body(Surface::List))
69
    );
70
1
}
71

            
72
#[test]
73
1
fn a_list_that_fits_records_no_bar() {
74
1
    let (_, hits) = draw_frame(&accounts(3), 80, 24);
75
1
    assert!(parts(&hits, Surface::List).is_empty());
76
1
}
77

            
78
#[test]
79
1
fn the_thumb_part_follows_the_scrolled_list() {
80
1
    let mut app = accounts(60);
81
1
    let (_, hits) = draw_frame(&app, 80, 24);
82
2
    let thumb = |hits: &Hits| {
83
2
        let found = parts(hits, Surface::List);
84
5
        found.into_iter().find(|(_, part)| *part == BarPart::Thumb)
85
2
    };
86
1
    let before = thumb(&hits).map(|(rect, _)| rect.y);
87
1
    app.accounts_mut().selected = 59;
88
1
    let (buffer, hits) = draw_frame(&app, 80, 24);
89
1
    assert_on_screen(&buffer, &parts(&hits, Surface::List));
90
1
    assert!(thumb(&hits).map(|(rect, _)| rect.y) > before);
91
1
}
92

            
93
#[test]
94
1
fn a_report_table_records_its_bar_and_body() {
95
1
    let mut app = crate::test_support::app();
96
1
    app.switch_tab(Tab::Reports);
97
60
    app.reports_mut().state = Fetch::Loaded((0..60).map(|n| n.to_string()).collect());
98
1
    let (buffer, hits) = draw_frame(&app, 80, 24);
99
1
    assert_on_screen(&buffer, &parts(&hits, Surface::Pane));
100
1
    assert!(
101
1
        hits.entries()
102
1
            .iter()
103
2
            .any(|(_, target)| *target == Target::Body(Surface::Pane))
104
    );
105
1
}
106

            
107
2
fn detail(lines: usize) -> App {
108
62
    let lines: Vec<String> = (0..lines).map(|n| n.to_string()).collect();
109
2
    let mut app = crate::test_support::app();
110
2
    app.overlays.push(Modal::Draft(Draft::new(lines)));
111
2
    app
112
2
}
113

            
114
#[test]
115
1
fn a_scrolled_modal_records_its_body_above_the_block_and_its_bar() {
116
1
    let app = detail(60);
117
1
    let (buffer, hits) = draw_frame(&app, 80, 14);
118
1
    assert_on_screen(&buffer, &parts(&hits, Surface::Modal));
119
1
    let entries = hits.entries();
120
1
    let body = entries
121
1
        .iter()
122
12
        .find(|(_, target)| *target == Target::Body(Surface::Modal));
123
1
    let (rect, _) = body.expect("the body is recorded");
124
1
    let inside = rect.as_position();
125
1
    assert_eq!(
126
1
        hits.hit_at(inside).map(|(_, target, _)| target),
127
1
        Some(Target::Body(Surface::Modal))
128
    );
129
1
    assert_eq!(
130
1
        hits.hit_at((0, 0).into()).map(|(_, target, _)| target),
131
1
        Some(Target::Block)
132
    );
133
1
}
134

            
135
#[test]
136
1
fn a_modal_that_fits_records_a_body_and_no_bar() {
137
1
    let (_, hits) = draw_frame(&detail(2), 80, 24);
138
1
    assert!(parts(&hits, Surface::Modal).is_empty());
139
1
    assert!(
140
1
        hits.entries()
141
1
            .iter()
142
12
            .any(|(_, target)| *target == Target::Body(Surface::Modal))
143
    );
144
1
}