1
//! The `▐↓▌` history mark on form select fields, in both presets, and `▼` kept in grid cells.
2

            
3
use ratatui::buffer::Buffer;
4
use ratatui::layout::{Position, Rect};
5
use ratatui::style::Color;
6
use tvui::{HISTORY_CELLS, Place, Role, Theme};
7

            
8
use super::body::FormBody;
9
use super::render_field;
10
use super::select_tests::{finds, options, select_form, select_mut};
11
use crate::app::App;
12
use crate::form::Form;
13
use crate::hits::{Hits, Surface, Target};
14
use crate::modal::Modal;
15
use crate::overlay::Placement;
16
use crate::widgets::Widget;
17

            
18
const PRESETS: [&str; 2] = ["dn", "turbo"];
19
const SIZE: (u16, u16) = (100, 30);
20

            
21
12
fn app_with(preset: &str, form: Form) -> (App, Theme) {
22
12
    let mut app = crate::test_support::app();
23
12
    let Ok(theme) = Theme::preset(preset) else {
24
        panic!("the {preset} preset parses");
25
    };
26
12
    app.theme = theme.clone();
27
12
    app.overlays.push(Modal::Form(form));
28
12
    (app, theme)
29
12
}
30

            
31
18
fn drawn(app: &App) -> (Buffer, Hits) {
32
18
    crate::test_support::draw_frame(app, SIZE.0, SIZE.1)
33
18
}
34

            
35
2
fn look(buf: &Buffer, at: Position) -> (Option<Color>, Option<Color>) {
36
2
    let style = buf[at].style();
37
2
    (style.fg, style.bg)
38
2
}
39

            
40
2
fn want(theme: &Theme, role: Role) -> (Option<Color>, Option<Color>) {
41
2
    let style = theme.style(role);
42
2
    (style.fg, style.bg)
43
2
}
44

            
45
12
fn mark_rect(hits: &Hits) -> Rect {
46
12
    let found = hits
47
12
        .entries()
48
12
        .into_iter()
49
180
        .find(|(_, target)| matches!(target, Target::Mark { .. }));
50
12
    found.map(|(rect, _)| rect).expect("a mark was drawn")
51
12
}
52

            
53
4
fn long_filter() -> Form {
54
4
    let mut form = select_form(options(&["Apple", "Banana"]));
55
4
    for _ in 0..60 {
56
240
        select_mut(&mut form).filter_push('x');
57
    }
58
4
    form
59
4
}
60

            
61
#[test]
62
1
fn a_select_field_ends_in_the_history_mark_cell_by_cell() {
63
1
    for preset in PRESETS {
64
2
        let (app, theme) = app_with(preset, select_form(options(&["Apple", "Banana"])));
65
2
        let (buf, hits) = drawn(&app);
66
2
        let start = finds(&buf, "▐")[0];
67
2
        for (dx, glyph) in ["▐", "↓", "▌"].into_iter().enumerate() {
68
6
            let at = Position::new(start.x + u16::try_from(dx).unwrap_or(0), start.y);
69
6
            assert_eq!(buf[at].symbol(), glyph, "{preset} {dx}");
70
        }
71
2
        assert_eq!(
72
2
            mark_rect(&hits),
73
2
            Rect::new(start.x, start.y, HISTORY_CELLS, 1),
74
            "{preset}"
75
        );
76
2
        let before = Position::new(start.x - 1, start.y);
77
2
        assert_eq!(look(&buf, before), want(&theme, Role::InputFocused));
78
2
        assert!(finds(&buf, "▼").is_empty(), "{preset}");
79
    }
80
1
}
81

            
82
#[test]
83
1
fn the_input_line_of_a_select_is_shorter_by_the_mark_and_keeps_its_arrow() {
84
1
    for preset in PRESETS {
85
2
        let (app, theme) = app_with(preset, long_filter());
86
2
        let (buf, hits) = drawn(&app);
87
2
        let mark = mark_rect(&hits);
88
2
        let field = hits
89
2
            .entries()
90
2
            .into_iter()
91
28
            .find_map(|(rect, target)| (target == Target::Field(0)).then_some(rect));
92
2
        let field = field.expect("the field was drawn");
93
2
        assert_eq!(mark.right() + 1, field.right(), "{preset}");
94
2
        let left = finds(&buf, "◄");
95
2
        assert_eq!(left.len(), 1, "{preset}");
96
2
        assert_eq!(left[0].y, mark.y);
97
2
        assert!(left[0].x < mark.x, "{preset}");
98
2
        assert_eq!(
99
2
            buf[left[0]].style().fg,
100
2
            theme.style(Role::InputArrow).fg,
101
            "{preset}"
102
        );
103
2
        assert!(finds(&buf, "►").is_empty(), "{preset}");
104
    }
105
1
}
106

            
107
#[test]
108
1
fn a_grid_cell_keeps_its_triangle_and_has_no_history_mark() {
109
1
    for preset in PRESETS {
110
2
        let form = Form::transaction_create();
111
2
        let selects = form
112
2
            .fields
113
2
            .iter()
114
8
            .filter(|field| matches!(field.widget, Widget::Select(_)))
115
            .count();
116
2
        let (app, _) = app_with(preset, form);
117
2
        let (buf, _) = drawn(&app);
118
2
        assert!(!finds(&buf, "▼").is_empty(), "{preset}");
119
2
        assert_eq!(finds(&buf, "▐").len(), selects, "{preset}");
120
    }
121
1
}
122

            
123
4
fn moved_by(app: &mut App, dx: i32, dy: i32) {
124
4
    let (_, hits) = drawn(app);
125
4
    let auto = hits.dialog().expect("a dialog was drawn").auto;
126
4
    let place = Place {
127
4
        x: i32::from(auto.x) + dx,
128
4
        y: i32::from(auto.y) + dy,
129
4
        ..Place::from(auto)
130
4
    };
131
4
    app.overlays.set_placement(Placement::of(place, auto));
132
4
}
133

            
134
#[test]
135
1
fn the_mark_moves_with_a_moved_dialog() {
136
1
    for preset in PRESETS {
137
2
        let (mut app, _) = app_with(preset, select_form(options(&["Apple", "Banana"])));
138
2
        let before = mark_rect(&drawn(&app).1);
139
2
        moved_by(&mut app, -6, 3);
140
2
        let (buf, hits) = drawn(&app);
141
2
        let after = mark_rect(&hits);
142
2
        assert_eq!((after.x + 6, after.y), (before.x, before.y + 3), "{preset}");
143
2
        assert_eq!(buf[(after.x, after.y)].symbol(), "▐", "{preset}");
144
    }
145
1
}
146

            
147
#[test]
148
1
fn a_list_opened_in_a_dialog_moved_to_the_edge_stays_on_the_screen_below_the_mark() {
149
1
    for preset in PRESETS {
150
2
        let wide = "W".repeat(60);
151
2
        let mut form = select_form(options(&["Apple", &wide, "Cherry"]));
152
2
        select_mut(&mut form).open_list();
153
2
        let (mut app, _) = app_with(preset, form);
154
2
        let auto = drawn(&app).1.dialog().expect("a dialog was drawn").auto;
155
2
        moved_by(&mut app, i32::from(SIZE.0 - auto.right()) + 2, 4);
156
2
        let (_, hits) = drawn(&app);
157
2
        let mark = mark_rect(&hits);
158
2
        let list = hits
159
2
            .entries()
160
2
            .into_iter()
161
32
            .find_map(|(rect, target)| (target == Target::Body(Surface::Options)).then_some(rect));
162
2
        let list = list.expect("the list was drawn");
163
2
        assert_eq!(list.y, mark.y + 1, "{preset}");
164
2
        assert!(list.right() <= SIZE.0, "{preset} {list:?}");
165
2
        assert!(list.x < mark.right(), "{preset} {list:?}");
166
    }
167
1
}
168

            
169
#[test]
170
1
fn the_cursor_of_an_open_select_stays_left_of_the_mark() {
171
1
    for preset in PRESETS {
172
2
        let mut form = long_filter();
173
2
        select_mut(&mut form).open_list();
174
2
        let (app, _) = app_with(preset, form);
175
        let mut terminal =
176
2
            ratatui::Terminal::new(ratatui::backend::TestBackend::new(SIZE.0, SIZE.1))
177
2
                .expect("test terminal");
178
2
        let mut hits = Hits::default();
179
2
        terminal
180
2
            .draw(|frame| crate::draw::draw(frame, &app, &mut hits))
181
2
            .expect("the frame draws");
182
2
        let cursor = terminal.backend().cursor_position();
183
2
        assert!(cursor.x < mark_rect(&hits).x, "{preset} {cursor:?}");
184
    }
185
1
}
186

            
187
2
fn widest_of(field: &crate::form::Field) -> usize {
188
2
    let body = FormBody {
189
2
        title: "",
190
2
        notices: Vec::new(),
191
2
        blocks: vec![render_field(0, field, 0, 4, None, None)],
192
2
        tail: Vec::new(),
193
2
        focus: 0,
194
2
        button: None,
195
2
        pressed: None,
196
2
        dragging: false,
197
2
        active: true,
198
2
        popup: None,
199
2
    };
200
2
    body.widest()
201
2
}
202

            
203
#[test]
204
1
fn a_select_is_wider_than_a_text_field_by_its_mark() {
205
1
    let mut form = select_form(options(&["Apple"]));
206
1
    let select = form.fields.remove(0);
207
1
    let text = crate::form::Field {
208
1
        key: None,
209
1
        label: "pick",
210
1
        widget: Widget::Text(crate::widgets::Editor::with_buffer("")),
211
1
    };
212
1
    assert_eq!(
213
1
        widest_of(&select),
214
1
        widest_of(&text) + usize::from(HISTORY_CELLS)
215
    );
216
1
}