1
//! The areas a frame records are the ones it drew.
2

            
3
use super::body_area;
4
use crate::app::App;
5
use crate::hits::{Hits, Target};
6
use crate::modal::{ConfirmAction, Modal};
7
use crate::test_support::draw_frame;
8
use crate::view::Tab;
9
use ratatui::buffer::Buffer;
10
use ratatui::layout::{Position, Rect};
11
use tvui::{MenuBar, MenuItem, MenuPopup, Panel, StatusBar, plain};
12

            
13
const SCREEN: Rect = Rect::new(0, 0, 80, 24);
14

            
15
19
fn text(buffer: &Buffer, rect: Rect) -> String {
16
19
    (rect.x..rect.right())
17
656
        .map(|x| buffer[(x, rect.y)].symbol())
18
        .collect()
19
19
}
20

            
21
11
fn targets(hits: &Hits, pick: impl Fn(Target) -> bool) -> Vec<(Rect, Target)> {
22
11
    hits.entries()
23
11
        .iter()
24
244
        .filter(|(_, target)| pick(*target))
25
        .copied()
26
        .collect()
27
11
}
28

            
29
7
fn accounts() -> App {
30
7
    let mut app = crate::test_support::app();
31
7
    app.switch_tab(Tab::Accounts);
32
21
    let row = |i: usize| format!(r#"(:account :id "u{i}" :name "acc{i}" :parent "")"#);
33
7
    let rows: Vec<String> = (0..3).map(row).collect();
34
7
    app.accounts_mut()
35
7
        .on_reply(&format!("(:id 1 :value ({}))", rows.join(" ")));
36
7
    app
37
7
}
38

            
39
20
fn is_row(target: Target) -> bool {
40
20
    matches!(target, Target::Row(_))
41
20
}
42

            
43
#[test]
44
1
fn a_list_records_each_row_where_it_draws_it() {
45
1
    let (buffer, hits) = draw_frame(&accounts(), SCREEN.width, SCREEN.height);
46
1
    let body = Panel::rows(body_area(SCREEN));
47
1
    let rows = targets(&hits, is_row);
48
1
    assert_eq!(rows.len(), 3);
49
1
    for (i, (rect, target)) in rows.into_iter().enumerate() {
50
3
        let y = body.y + u16::try_from(i).expect("small");
51
3
        assert_eq!(
52
3
            (rect, target),
53
3
            (Rect::new(body.x, y, body.width, 1), Target::Row(i))
54
        );
55
3
        assert!(text(&buffer, rect).contains(&format!("acc{i}")), "{i}");
56
    }
57
1
}
58

            
59
#[test]
60
1
fn the_menu_bar_records_its_title_and_a_closed_menu_no_backdrop() {
61
1
    let (buffer, hits) = draw_frame(&accounts(), SCREEN.width, SCREEN.height);
62
1
    let bar = Rect::new(0, 0, SCREEN.width, 1);
63
17
    let titles = targets(&hits, |t| matches!(t, Target::MenuTitle(_)));
64
1
    assert_eq!(
65
1
        titles,
66
1
        MenuBar::title_areas(bar, &["~V~iew", "~W~indow", "~O~ptions"])
67
1
            .into_iter()
68
1
            .enumerate()
69
3
            .map(|(index, area)| (area, Target::MenuTitle(index)))
70
            .collect::<Vec<_>>()
71
    );
72
1
    assert_eq!(text(&buffer, titles[0].0), " View ");
73
1
    assert_eq!(text(&buffer, titles[1].0), " Window ");
74
17
    assert!(targets(&hits, |t| t == Target::MenuBackdrop).is_empty());
75
1
}
76

            
77
#[test]
78
1
fn an_open_menu_records_a_backdrop_and_each_item_on_top_of_the_view() {
79
1
    let mut app = accounts();
80
1
    app.open_menu();
81
1
    let (buffer, hits) = draw_frame(&app, SCREEN.width, SCREEN.height);
82
1
    assert_eq!(
83
24
        targets(&hits, |t| t == Target::MenuBackdrop),
84
1
        vec![(SCREEN, Target::MenuBackdrop)]
85
    );
86
1
    let entries = app.menu_entries();
87
1
    let items: Vec<MenuItem> = entries
88
1
        .iter()
89
        .map(|entry| MenuItem {
90
10
            label: &entry.label,
91
10
            key: &entry.key,
92
10
            marked: entry.marked,
93
10
            enabled: entry.enabled,
94
10
        })
95
        .collect();
96
1
    let anchor = MenuBar::title_areas(Rect::new(0, 0, SCREEN.width, 1), &["~V~iew", "~W~indow"])[0];
97
24
    let found = targets(&hits, |t| matches!(t, Target::MenuItem(_)));
98
1
    let want: Vec<_> = MenuPopup::item_areas(anchor, &items)
99
1
        .into_iter()
100
1
        .enumerate()
101
10
        .map(|(i, rect)| (rect, Target::MenuItem(i)))
102
        .collect();
103
1
    assert_eq!(found, want);
104
1
    assert_eq!(found.len(), Tab::ALL.len());
105
1
    assert!(text(&buffer, found[0].0).contains("Accounts"));
106
1
    let over_a_row = found[0].0;
107
1
    let top = hits.hit_at(Position::new(over_a_row.x, over_a_row.y));
108
1
    assert_eq!(top.map(|(_, target, _)| target), Some(Target::MenuItem(0)));
109
1
}
110

            
111
#[test]
112
1
fn the_status_bar_records_each_hint_it_draws_with_its_intent() {
113
1
    let app = accounts();
114
1
    let (buffer, hits) = draw_frame(&app, SCREEN.width, SCREEN.height);
115
1
    let hinted = crate::keymap::hinted(&app);
116
1
    let pairs: Vec<_> = hinted.iter().map(crate::keymap::Hint::pair).collect();
117
17
    let found = targets(&hits, |t| matches!(t, Target::Hint(_)));
118
1
    assert!(!found.is_empty());
119
1
    let bar = Rect::new(0, SCREEN.bottom() - 1, SCREEN.width, 1);
120
1
    let areas = StatusBar::item_areas(bar, &pairs);
121
1
    assert!(found.len() <= areas.len());
122
1
    for ((rect, target), (index, area)) in found.iter().zip(areas) {
123
5
        assert_eq!((*rect, *target), (area, Target::Hint(hinted[index].intent)));
124
5
        let (key, label) = &pairs[index];
125
5
        assert_eq!(text(&buffer, *rect), format!("{key} {label}"));
126
    }
127
1
}
128

            
129
#[test]
130
1
fn a_status_message_replaces_the_hints() {
131
1
    let mut app = accounts();
132
1
    app.set_status("saved");
133
1
    let (_, hits) = draw_frame(&app, SCREEN.width, SCREEN.height);
134
12
    assert!(targets(&hits, |t| matches!(t, Target::Hint(_))).is_empty());
135
1
}
136

            
137
1
fn confirm() -> App {
138
1
    let mut app = accounts();
139
1
    app.overlays.push(Modal::Confirm {
140
1
        prompt: "Delete it?".to_string(),
141
1
        action: ConfirmAction::DiscardForm,
142
1
        focus: None,
143
1
    });
144
1
    app
145
1
}
146

            
147
#[test]
148
1
fn a_confirm_blocks_the_screen_and_records_yes_and_no_where_drawn() {
149
1
    let (buffer, hits) = draw_frame(&confirm(), SCREEN.width, SCREEN.height);
150
21
    let buttons = targets(&hits, |t| matches!(t, Target::Button(_)));
151
1
    assert_eq!(buttons.len(), 2);
152
1
    assert!(text(&buffer, buttons[0].0).contains("Yes"));
153
1
    assert!(text(&buffer, buttons[1].0).contains("No"));
154
1
    assert_eq!(buttons[1].1, Target::Button(1));
155
21
    let blocks = targets(&hits, |t| t == Target::Block);
156
1
    assert_eq!(blocks.last(), Some(&(SCREEN, Target::Block)));
157
1
    let entries = hits.entries();
158
3
    let first_row = entries.iter().position(|(_, t)| is_row(*t)).expect("rows");
159
1
    let block = entries
160
1
        .iter()
161
13
        .position(|entry| *entry == (SCREEN, Target::Block))
162
1
        .expect("block");
163
1
    assert!(first_row < block, "the modal lies over the view");
164
1
}
165

            
166
#[test]
167
1
fn a_form_records_its_fields_and_buttons_where_drawn() {
168
1
    let mut app = accounts();
169
1
    let form = crate::form::Form::transaction_create();
170
4
    let labels: Vec<&str> = form.fields.iter().map(|f| f.label).collect();
171
1
    app.overlays.push(Modal::Form(form));
172
1
    let (buffer, hits) = draw_frame(&app, SCREEN.width, SCREEN.height);
173
37
    let fields = targets(&hits, |t| matches!(t, Target::Field(_)));
174
1
    assert!(fields.len() >= 3, "{fields:?}");
175
1
    for (rect, target) in &fields {
176
4
        let Target::Field(i) = target else { continue };
177
4
        let drawn = text(&buffer, Rect { height: 1, ..*rect });
178
4
        assert!(
179
4
            drawn.contains(plain(labels[*i]).trim_end_matches(':')),
180
            "{i}: {drawn}"
181
        );
182
    }
183
37
    let buttons = targets(&hits, |t| matches!(t, Target::Button(_)));
184
1
    assert!(text(&buffer, buttons[0].0).contains("OK"));
185
1
    assert!(text(&buffer, buttons[1].0).contains("Close"));
186
1
    let at = hits.hit_at(Position::new(buttons[0].0.x, buttons[0].0.y));
187
1
    assert_eq!(at.map(|(_, t, _)| t), Some(Target::Button(0)));
188
1
}