1
use super::*;
2
use crate::modal::Modal;
3
use crate::widgets::SelectOption;
4
use ratatui::Terminal;
5
use ratatui::backend::TestBackend;
6
use ratatui::buffer::Buffer;
7
use ratatui::layout::Position;
8

            
9
20
pub(super) fn options(labels: &[&str]) -> Vec<SelectOption> {
10
20
    labels
11
20
        .iter()
12
        .map(|label| SelectOption {
13
45
            id: label.to_lowercase(),
14
45
            label: (*label).to_string(),
15
45
        })
16
        .collect()
17
20
}
18

            
19
5
fn numbered(count: usize) -> Vec<SelectOption> {
20
5
    (0..count)
21
        .map(|i| SelectOption {
22
88
            id: format!("id{i}"),
23
88
            label: format!("Opt{i}"),
24
88
        })
25
        .collect()
26
5
}
27

            
28
25
pub(super) fn select_form(options: Vec<SelectOption>) -> Form {
29
25
    let mut form = Form::config_set();
30
25
    let mut select = SelectWidget::new();
31
25
    select.set_options(options);
32
25
    form.fields = vec![Field {
33
25
        key: None,
34
25
        label: "pick",
35
25
        widget: Widget::Select(select),
36
25
    }];
37
25
    form.focus = 0;
38
25
    form
39
25
}
40

            
41
295
pub(super) fn select_mut(form: &mut Form) -> &mut SelectWidget {
42
295
    match &mut form.fields[0].widget {
43
295
        Widget::Select(select) => select,
44
        _ => panic!("the first field is a select"),
45
    }
46
295
}
47

            
48
9
fn screen(form: Form, size: (u16, u16)) -> Buffer {
49
9
    let mut app = crate::test_support::app();
50
9
    app.overlays.push(Modal::Form(form));
51
9
    let mut terminal = Terminal::new(TestBackend::new(size.0, size.1)).expect("test terminal");
52
9
    terminal
53
9
        .draw(|frame| crate::draw::draw(frame, &app, &mut crate::hits::Hits::default()))
54
9
        .expect("the frame draws");
55
9
    terminal.backend().buffer().clone()
56
9
}
57

            
58
5
fn shown(terminal: &mut Terminal<TestBackend>, app: &crate::app::App) -> Buffer {
59
5
    terminal
60
5
        .draw(|frame| crate::draw::draw(frame, app, &mut crate::hits::Hits::default()))
61
5
        .expect("the frame draws");
62
5
    terminal.backend().buffer().clone()
63
5
}
64

            
65
24
fn select_of(app: &mut crate::app::App) -> &mut SelectWidget {
66
24
    match app.overlays.top_mut() {
67
24
        Some(Modal::Form(form)) => select_mut(form),
68
        _ => panic!("the form is the top overlay"),
69
    }
70
24
}
71

            
72
1081
fn row_text(buf: &Buffer, y: u16) -> String {
73
93680
    (0..buf.area.width).map(|x| buf[(x, y)].symbol()).collect()
74
1081
}
75

            
76
42
pub(super) fn finds(buf: &Buffer, text: &str) -> Vec<Position> {
77
42
    let mut found = Vec::new();
78
42
    for y in 0..buf.area.height {
79
1080
        let row = row_text(buf, y);
80
1080
        for (byte, _) in row.match_indices(text) {
81
46
            let x = u16::try_from(row[..byte].chars().count()).unwrap_or(0);
82
46
            found.push(Position::new(x, y));
83
        }
84
    }
85
42
    found
86
42
}
87

            
88
/// The first cell of the value column of the one-field select form.
89
4
fn value_at(buf: &Buffer) -> Position {
90
4
    let label = finds(buf, "pick:")[0];
91
4
    Position::new(label.x + 6, label.y)
92
4
}
93

            
94
8
fn assert_role(buf: &Buffer, at: Position, role: tvui::Role) {
95
8
    let want = crate::test_support::dn_theme().style(role);
96
8
    let got = buf[at].style();
97
8
    assert_eq!((got.fg, got.bg), (want.fg, want.bg), "{role:?} at {at:?}");
98
8
}
99

            
100
#[test]
101
1
fn an_open_select_lists_its_options_under_the_value() {
102
1
    let mut form = select_form(options(&["Apple", "Banana", "Cherry"]));
103
1
    select_mut(&mut form).open_list();
104
1
    select_mut(&mut form).next();
105
1
    let buf = screen(form, (80, 24));
106
1
    let value = value_at(&buf);
107
1
    assert_eq!(
108
1
        finds(&buf, "Banana")[0],
109
1
        Position::new(value.x + 1, value.y)
110
    );
111
1
    assert_eq!(buf[(value.x, value.y + 1)].symbol(), "┌");
112
1
    let apple = finds(&buf, "Apple")[0];
113
1
    assert_eq!((apple.x, apple.y), (value.x + 2, value.y + 2));
114
1
    let banana = finds(&buf, "Banana")[1];
115
1
    assert_eq!((banana.x, banana.y), (value.x + 2, value.y + 3));
116
1
    assert_role(&buf, apple, tvui::Role::List);
117
1
    assert_role(&buf, banana, tvui::Role::ListSelected);
118
1
}
119

            
120
#[test]
121
1
fn typing_narrows_the_list_and_shows_the_filter_in_the_field() {
122
1
    let mut form = select_form(options(&["Apple", "Banana", "Cherry"]));
123
1
    select_mut(&mut form).filter_push('c');
124
1
    let buf = screen(form, (80, 24));
125
1
    let value = value_at(&buf);
126
1
    assert_eq!(buf[(value.x + 1, value.y)].symbol(), "c");
127
1
    assert!(finds(&buf, "Apple").is_empty());
128
1
    assert!(finds(&buf, "Banana").is_empty());
129
1
    let cherry = finds(&buf, "Cherry")[0];
130
1
    assert_eq!((cherry.x, cherry.y), (value.x + 2, value.y + 2));
131
1
}
132

            
133
#[test]
134
1
fn the_list_scrolls_to_keep_the_highlight_in_view() {
135
1
    let mut form = select_form(numbered(20));
136
1
    select_mut(&mut form).open_list();
137
1
    for _ in 0..15 {
138
15
        select_mut(&mut form).next();
139
    }
140
1
    let buf = screen(form, (80, 24));
141
1
    assert!(finds(&buf, "Opt0").is_empty());
142
1
    assert!(finds(&buf, "Opt7").is_empty());
143
1
    let list = finds(&buf, "Opt15")[1];
144
1
    assert_role(&buf, list, tvui::Role::ListSelected);
145
1
    assert_role(&buf, finds(&buf, "Opt14")[0], tvui::Role::List);
146
1
    assert_eq!(finds(&buf, "Opt8")[0].y + 7, list.y);
147
1
}
148

            
149
#[test]
150
1
fn a_closed_select_is_a_combo_line_without_a_list() {
151
1
    let form = select_form(options(&["Apple", "Banana"]));
152
1
    let buf = screen(form, (80, 24));
153
1
    let value = value_at(&buf);
154
1
    assert!(row_text(&buf, value.y).contains('▐'));
155
1
    assert_eq!(finds(&buf, "▐").len(), 1);
156
1
    assert!(finds(&buf, "▼").is_empty());
157
1
    assert!(finds(&buf, "Banana").is_empty());
158
1
    assert_role(&buf, value, tvui::Role::InputFocused);
159
1
}
160

            
161
#[test]
162
1
fn opening_a_select_does_not_resize_the_dialog() {
163
2
    let filter_form = || {
164
2
        let mut form = select_form(options(&["Apple", "Banana", "Cherry"]));
165
2
        form.kind = crate::form::FormKind::TransactionFilter;
166
2
        form
167
2
    };
168
1
    let closed = finds(&screen(filter_form(), (80, 24)), "╚")[0].y;
169
1
    let mut form = filter_form();
170
1
    select_mut(&mut form).open_list();
171
1
    let open = screen(form, (80, 24));
172
1
    assert_eq!(finds(&open, "╚")[0].y, closed);
173
1
    assert!(finds(&open, "previous").is_empty());
174
1
}
175

            
176
#[test]
177
1
fn an_open_combo_has_the_cursor_at_the_end_of_its_filter_and_a_closed_one_has_none() {
178
3
    let cursor = |form: Form| {
179
3
        let mut app = crate::test_support::app();
180
3
        app.overlays.push(Modal::Form(form));
181
3
        let mut terminal = Terminal::new(TestBackend::new(80, 24)).expect("test terminal");
182
3
        terminal
183
3
            .draw(|frame| crate::draw::draw(frame, &app, &mut crate::hits::Hits::default()))
184
3
            .expect("the frame draws");
185
3
        let backend = terminal.backend();
186
3
        backend.cursor_visible().then(|| backend.cursor_position())
187
3
    };
188
1
    let form = select_form(options(&["Apple", "Banana"]));
189
1
    assert_eq!(cursor(form), None);
190
1
    let mut form = select_form(options(&["Apple", "Banana"]));
191
1
    select_mut(&mut form).open_list();
192
1
    let value = value_at(&screen(
193
1
        select_form(options(&["Apple", "Banana"])),
194
1
        (80, 24),
195
1
    ));
196
1
    assert_eq!(cursor(form), Some(Position::new(value.x + 1, value.y)));
197
1
    let mut form = select_form(options(&["Apple", "Banana"]));
198
1
    select_mut(&mut form).filter_push('b');
199
1
    select_mut(&mut form).filter_push('a');
200
1
    assert_eq!(cursor(form), Some(Position::new(value.x + 3, value.y)));
201
1
}
202

            
203
#[test]
204
1
fn the_window_stays_while_the_highlight_moves_inside_it() {
205
1
    let mut app = crate::test_support::app();
206
1
    let mut form = select_form(numbered(20));
207
1
    select_mut(&mut form).open_list();
208
1
    app.overlays.push(Modal::Form(form));
209
1
    let mut terminal = Terminal::new(TestBackend::new(80, 24)).expect("test terminal");
210
1
    shown(&mut terminal, &app);
211
1
    for _ in 0..3 {
212
3
        select_of(&mut app).next();
213
    }
214
1
    let buf = shown(&mut terminal, &app);
215
1
    assert!(!finds(&buf, "Opt0").is_empty());
216
1
    assert_role(&buf, finds(&buf, "Opt3")[1], tvui::Role::ListSelected);
217
1
    for _ in 0..5 {
218
5
        select_of(&mut app).next();
219
    }
220
1
    let buf = shown(&mut terminal, &app);
221
1
    assert!(finds(&buf, "Opt0").is_empty());
222
1
    assert_role(&buf, finds(&buf, "Opt8")[1], tvui::Role::ListSelected);
223
1
    assert_eq!(finds(&buf, "Opt1")[0].y + 7, finds(&buf, "Opt8")[1].y);
224
1
}
225

            
226
#[test]
227
1
fn a_long_list_has_a_scrollbar_on_the_box_and_a_short_one_has_none() {
228
1
    let mut long = select_form(numbered(20));
229
1
    select_mut(&mut long).open_list();
230
1
    assert_eq!(finds(&screen(long, (80, 24)), "▲").len(), 1);
231
1
    let mut short = select_form(numbered(8));
232
1
    select_mut(&mut short).open_list();
233
1
    assert!(finds(&screen(short, (80, 24)), "▲").is_empty());
234
1
}
235

            
236
#[test]
237
1
fn filtering_brings_the_highlight_back_into_the_window() {
238
1
    let mut app = crate::test_support::app();
239
1
    let mut form = select_form(numbered(20));
240
1
    select_mut(&mut form).open_list();
241
1
    app.overlays.push(Modal::Form(form));
242
1
    let mut terminal = Terminal::new(TestBackend::new(80, 24)).expect("test terminal");
243
1
    for _ in 0..15 {
244
15
        select_of(&mut app).next();
245
    }
246
1
    shown(&mut terminal, &app);
247
1
    select_of(&mut app).filter_push('1');
248
1
    let buf = shown(&mut terminal, &app);
249
1
    assert_role(&buf, finds(&buf, "Opt1")[0], tvui::Role::ListSelected);
250
1
}