1
//! TurboVision frames: double-line active windows, single-line passive ones, shadowed dialogs.
2

            
3
use super::draw;
4
use crate::event::Intent;
5
use crate::modal::{ConfirmAction, Draft, Modal};
6
use crate::view::ViewId;
7
use ratatui::Terminal;
8
use ratatui::backend::TestBackend;
9
use ratatui::buffer::Buffer;
10
use ratatui::layout::Rect;
11
use tvui::{Dialog, Role};
12

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

            
15
20
fn rendered(app: &crate::app::App) -> Buffer {
16
    let mut terminal =
17
20
        Terminal::new(TestBackend::new(SCREEN.width, SCREEN.height)).expect("test terminal");
18
20
    terminal
19
20
        .draw(|frame| draw(frame, app, &mut crate::hits::Hits::default()))
20
20
        .expect("draw");
21
20
    terminal.backend().buffer().clone()
22
20
}
23

            
24
4
fn confirm_app() -> crate::app::App {
25
4
    let mut app = crate::test_support::app();
26
4
    let prompt = "Delete it?".to_string();
27
4
    app.overlays.push(Modal::Confirm {
28
4
        prompt,
29
4
        action: ConfirmAction::DiscardForm,
30
4
        focus: None,
31
4
    });
32
4
    app
33
4
}
34

            
35
#[test]
36
1
fn the_active_view_has_a_double_frame_with_a_centred_title() {
37
1
    let buffer = rendered(&crate::test_support::app());
38
1
    assert_eq!(buffer[(0, 1)].symbol(), "╔");
39
1
    assert_eq!(buffer[(79, 1)].symbol(), "╗");
40
80
    let top: String = (0..80).map(|x| buffer[(x, 1)].symbol()).collect();
41
1
    let at = top.find("Accounts").expect("the title is in the border");
42
1
    assert!((20..40).contains(&top[..at].chars().count()), "{top}");
43
1
}
44

            
45
#[test]
46
1
fn a_view_under_a_dialog_is_passive() {
47
1
    let buffer = rendered(&confirm_app());
48
1
    assert_eq!(buffer[(0, 1)].symbol(), "┌");
49
1
    assert_eq!(buffer[(79, 1)].symbol(), "┐");
50
1
}
51

            
52
/// The box between the first `╔` and the last `╝`, the only double frame of a covered view.
53
13
fn dialog_area(buffer: &Buffer) -> Rect {
54
26
    let corners = |symbol: &'static str| -> Vec<_> {
55
26
        let all = buffer.area.positions();
56
49920
        all.filter(|p| buffer[*p].symbol() == symbol).collect()
57
26
    };
58
13
    let (first, last) = (corners("╔")[0], *corners("╝").last().expect("a corner"));
59
13
    Rect::new(first.x, first.y, last.x + 1 - first.x, last.y + 1 - first.y)
60
13
}
61

            
62
#[test]
63
1
fn a_dialog_has_a_double_frame_and_a_shadow() {
64
1
    let app = confirm_app();
65
1
    let buffer = rendered(&app);
66
1
    let area = dialog_area(&buffer);
67
1
    assert_eq!(buffer[(area.x, area.y)].symbol(), "╔");
68
1
    let shadow = app.theme.style(Role::Shadow);
69
1
    for position in [
70
1
        (area.right(), area.y + 1),
71
1
        (area.right() + 1, area.bottom()),
72
1
        (area.x + 2, area.bottom()),
73
1
    ] {
74
3
        assert_eq!(buffer[position].style().bg, shadow.bg, "{position:?}");
75
3
        assert_eq!(buffer[position].style().fg, shadow.fg, "{position:?}");
76
    }
77
1
    assert_ne!(buffer[(area.right(), area.y)].style().bg, shadow.bg);
78
1
    assert_ne!(buffer[(area.x + 1, area.bottom())].style().bg, shadow.bg);
79
1
}
80

            
81
#[test]
82
1
fn the_tab_bar_and_status_line_are_one_row_each() {
83
1
    let app = crate::test_support::app();
84
1
    let buffer = rendered(&app);
85
4
    let fill = |y: u16, roles: &[Role]| {
86
6
        let backgrounds: Vec<_> = roles.iter().map(|role| app.theme.style(*role).bg).collect();
87
162
        (0..80).all(|x| backgrounds.contains(&buffer[(x, y)].style().bg))
88
4
    };
89
1
    assert!(fill(0, &[Role::Menu, Role::MenuSelected]));
90
1
    assert!(fill(23, &[Role::Status]));
91
1
    assert!(!fill(1, &[Role::Menu, Role::MenuSelected]));
92
1
    assert!(!fill(22, &[Role::Status]));
93
1
    assert_eq!(buffer[(0, 22)].symbol(), "╚");
94
1
}
95

            
96
13
fn row_text(buffer: &Buffer, y: u16) -> String {
97
13
    (0..buffer.area.width)
98
1040
        .map(|x| buffer[(x, y)].symbol())
99
        .collect()
100
13
}
101

            
102
5
fn button_row(buffer: &Buffer) -> String {
103
5
    let area = dialog_area(buffer);
104
5
    row_text(buffer, area.bottom() - 3)
105
5
}
106

            
107
5
fn assert_labels_in_order(row: &str, labels: &[&str]) {
108
9
    let at: Vec<Option<usize>> = labels.iter().map(|label| row.find(label)).collect();
109
5
    assert!(at.iter().all(Option::is_some), "{labels:?} in {row}");
110
5
    assert!(at.is_sorted(), "{labels:?} in order in {row}");
111
5
}
112

            
113
#[test]
114
1
fn a_confirm_shows_yes_and_no_in_the_bottom_row() {
115
1
    let buffer = rendered(&confirm_app());
116
1
    assert_labels_in_order(&button_row(&buffer), &["Yes", "No"]);
117
1
}
118

            
119
#[test]
120
1
fn a_draft_shows_a_close_button() {
121
1
    let mut app = crate::test_support::app();
122
1
    app.overlays
123
1
        .push(Modal::Draft(crate::modal::Draft::new(Vec::new())));
124
1
    assert_labels_in_order(&button_row(&rendered(&app)), &["Close"]);
125
1
}
126

            
127
#[test]
128
1
fn the_transaction_form_fits_80x24_with_ok_and_cancel() {
129
1
    let mut app = crate::test_support::app();
130
1
    app.overlays
131
1
        .push(Modal::Form(crate::form::Form::transaction_create()));
132
1
    let buffer = rendered(&app);
133
1
    let area = dialog_area(&buffer);
134
1
    assert!(SCREEN.contains(ratatui::layout::Position::new(
135
1
        area.right() - 1,
136
1
        area.bottom() - 1
137
1
    )));
138
1
    assert_labels_in_order(&button_row(&buffer), &["OK", "Close"]);
139
1
    let body = Dialog::body_area(area);
140
1
    assert!(row_text(&buffer, body.y).contains("Date:"));
141
1
}
142

            
143
#[test]
144
1
fn an_eighteen_split_form_scrolls_to_the_focused_row_between_the_bars() {
145
    use crate::widgets::Widget;
146
1
    let mut form = crate::form::Form::transaction_create();
147
1
    let Widget::Splits(ref mut splits) = form.fields[3].widget else {
148
        panic!("the splits field is the fourth");
149
    };
150
1
    for _ in 0..17 {
151
17
        splits.add_row();
152
    }
153
1
    form.focus = 3;
154
1
    let mut app = crate::test_support::app();
155
1
    app.overlays.push(Modal::Form(form));
156
1
    let buffer = rendered(&app);
157
1
    let area = dialog_area(&buffer);
158
1
    assert!(area.y >= 1 && area.bottom() < SCREEN.height, "{area:?}");
159
1
    assert_labels_in_order(&button_row(&buffer), &["OK", "Close"]);
160
1
    let focus = crate::test_support::dn_theme().style(Role::InputFocused);
161
1374
    let shown = area.positions().any(|at| {
162
1374
        (buffer[at].fg, buffer[at].bg)
163
1374
            == (focus.fg.unwrap_or_default(), focus.bg.unwrap_or_default())
164
1374
    });
165
1
    assert!(shown, "the focused split cell is shown");
166
1
    assert!(row_text(&buffer, 0).contains("View"));
167
1
    assert!(row_text(&buffer, SCREEN.height - 1).contains("[cua]"));
168
1
}
169

            
170
#[test]
171
1
fn the_filter_builder_offers_apply_and_close_without_a_default() {
172
1
    let mut app = crate::test_support::app();
173
1
    let builder = crate::modal::FilterBuilder::new("(and)");
174
1
    app.overlays.push(Modal::FilterBuilder(builder));
175
1
    let row = button_row(&rendered(&app));
176
1
    assert_labels_in_order(&row, &["Apply", "Close"]);
177
1
}
178

            
179
#[test]
180
1
fn a_form_draws_its_focused_button_in_the_focus_role_and_shows_no_cursor() {
181
1
    let mut form = crate::form::Form::config_set();
182
1
    form.focus = 3;
183
1
    let mut app = crate::test_support::app();
184
1
    app.overlays.push(Modal::Form(form));
185
    let mut terminal =
186
1
        Terminal::new(TestBackend::new(SCREEN.width, SCREEN.height)).expect("test terminal");
187
1
    terminal
188
1
        .draw(|frame| draw(frame, &app, &mut crate::hits::Hits::default()))
189
1
        .expect("draw");
190
1
    let buffer = terminal.backend().buffer();
191
1
    let area = dialog_area(buffer);
192
1
    let row = area.bottom() - 3;
193
1
    let close = row_text(buffer, row)
194
1
        .find("Close")
195
1
        .expect("the Close button");
196
1
    let focus = app.theme.style(Role::ButtonFocus);
197
1
    let col = u16::try_from(row_text(buffer, row)[..close].chars().count()).unwrap_or(0);
198
1
    assert_eq!(buffer[(col, row)].style().bg, focus.bg);
199
1
    assert!(!terminal.backend().cursor_visible());
200
1
}
201

            
202
3
fn detail_app(lines: usize) -> crate::app::App {
203
3
    let mut app = crate::test_support::app();
204
123
    let lines = (0..lines).map(|i| format!("line{i:02}")).collect();
205
3
    app.overlays.push(Modal::Draft(Draft::new(lines)));
206
3
    app
207
3
}
208

            
209
20
fn page_down(app: &mut crate::app::App) {
210
20
    if let Some(Modal::Draft(detail)) = app.overlays.top_mut() {
211
20
        detail.scroll_by(Intent::ScrollPageDown);
212
    }
213
20
}
214

            
215
#[test]
216
1
fn a_long_modal_body_has_a_scrollbar_along_its_rows_on_the_right_frame() {
217
1
    let app = detail_app(60);
218
1
    let buffer = rendered(&app);
219
1
    let area = dialog_area(&buffer);
220
1
    let body = Dialog::body_area(area);
221
1
    let x = area.right() - 1;
222
1
    assert_eq!(buffer[(x, body.y)].symbol(), "\u{25B2}");
223
1
    assert_eq!(buffer[(x, body.bottom() - 1)].symbol(), "\u{25BC}");
224
1
    assert_eq!(buffer[(x, body.bottom())].symbol(), "\u{2551}");
225
1
    assert_eq!(buffer[(x, area.y)].symbol(), "\u{2557}");
226
1
}
227

            
228
#[test]
229
1
fn a_modal_body_that_fits_has_no_scrollbar() {
230
1
    let buffer = rendered(&detail_app(3));
231
1
    let area = dialog_area(&buffer);
232
1
    let body = Dialog::body_area(area);
233
1
    assert_eq!(buffer[(area.right() - 1, body.y)].symbol(), "\u{2551}");
234
1
}
235

            
236
#[test]
237
1
fn paging_past_the_end_of_a_modal_shows_the_last_page() {
238
1
    let mut app = detail_app(60);
239
1
    rendered(&app);
240
1
    for _ in 0..10 {
241
10
        page_down(&mut app);
242
    }
243
1
    let buffer = rendered(&app);
244
1
    let body = Dialog::body_area(dialog_area(&buffer));
245
2
    let row = |y: u16| -> String {
246
2
        (body.x..body.right())
247
20
            .map(|x| buffer[(x, y)].symbol())
248
            .collect()
249
2
    };
250
1
    assert!(
251
1
        row(body.bottom() - 1).contains("line59"),
252
        "{}",
253
        row(body.bottom() - 1)
254
    );
255
1
    assert!(row(body.y).contains("line"), "{}", row(body.y));
256
1
    let thumb = app.theme.style(Role::ScrollbarThumb);
257
1
    let cell = buffer[(body.right(), body.bottom() - 2)].style();
258
1
    assert_eq!((cell.fg, cell.bg), (thumb.fg, thumb.bg));
259
1
}
260

            
261
#[test]
262
1
fn a_detail_with_a_multi_line_note_scrolls_to_its_last_line() {
263
1
    let mut app = crate::test_support::app();
264
30
    let mut lines: Vec<String> = (0..30).map(|i| format!("line{i:02}")).collect();
265
1
    lines[3] = "Note: a\nb\nc\nd\ne\nf\ng\nh".to_string();
266
1
    app.overlays.push(Modal::Draft(Draft::new(lines)));
267
1
    rendered(&app);
268
1
    for _ in 0..10 {
269
10
        page_down(&mut app);
270
    }
271
1
    let buffer = rendered(&app);
272
1
    let body = Dialog::body_area(dialog_area(&buffer));
273
1
    let last: String = (body.x..body.right())
274
10
        .map(|x| buffer[(x, body.bottom() - 1)].symbol())
275
        .collect();
276
1
    assert!(last.contains("line29"), "{last}");
277
1
}
278

            
279
#[test]
280
1
fn the_zoomed_front_window_shows_unzoom_and_its_number() {
281
1
    let mut app = crate::test_support::app();
282
1
    let top = row_text(&rendered(&app), 1);
283
1
    assert!(top.starts_with("╔═[■]"), "{top}");
284
1
    assert!(top.ends_with("1═[↕]═╗"), "{top}");
285
1
    app.switch_tab(ViewId::Scripts);
286
1
    let top = row_text(&rendered(&app), 1);
287
1
    let head = top.strip_suffix("═[↕]═╗").unwrap_or_default();
288
1
    assert!(head.ends_with('═'), "{top}");
289
1
}
290

            
291
#[test]
292
1
fn a_rear_window_draws_passive_beside_the_active_front_one() {
293
1
    let mut app = crate::test_support::app();
294
1
    app.toggle_zoom_of(ViewId::Accounts);
295
1
    let buffer = rendered(&app);
296
1
    let row = row_text(&buffer, 1);
297
1
    let front: String = row.chars().take(60).collect();
298
1
    assert!(front.starts_with("╔═[■]"), "{front}");
299
1
    assert!(front.ends_with("1═[↑]═╗"), "{front}");
300
1
    assert!(row.ends_with('┐'), "{row}");
301
1
    let passive = app.theme.style(Role::PanelFramePassive);
302
1
    let active = app.theme.style(Role::PanelFrame);
303
1
    assert_eq!(buffer[(60, 1)].style().fg, passive.fg);
304
1
    assert_eq!(buffer[(0, 1)].style().fg, active.fg);
305
1
}
306

            
307
#[test]
308
1
fn a_modal_makes_the_front_window_passive_too() {
309
1
    let mut app = confirm_app();
310
1
    app.toggle_zoom_of(ViewId::Accounts);
311
1
    let buffer = rendered(&app);
312
1
    assert_eq!(buffer[(0, 1)].symbol(), "┌");
313
1
}
314

            
315
#[test]
316
1
fn the_desktop_shows_where_no_window_is() {
317
1
    let mut app = crate::test_support::app();
318
1
    for kind in ViewId::ALL {
319
10
        app.toggle_zoom_of(kind);
320
    }
321
1
    let buffer = rendered(&app);
322
1
    let shade = app.theme.style(Role::Desktop);
323
1
    assert_eq!(buffer[(79, 1)].symbol(), "░");
324
1
    assert_eq!(buffer[(79, 22)].symbol(), "░");
325
1
    assert_eq!(buffer[(79, 22)].style().bg, shade.bg);
326
1
    assert_eq!(buffer[(0, 1)].symbol(), "╔");
327
1
    assert_eq!(buffer[(0, 0)].symbol(), " ");
328
1
}