1
use super::*;
2
use crate::event::{Intent, apply};
3
use crate::modal::Modal;
4
use crate::view::Tab;
5
use ratatui::Terminal;
6
use ratatui::backend::TestBackend;
7
use ratatui::buffer::Cell;
8
use ratatui::style::{Color, Style};
9
use sqlx::types::Uuid;
10

            
11
7
fn buffer_text(terminal: &Terminal<TestBackend>) -> String {
12
7
    terminal
13
7
        .backend()
14
7
        .buffer()
15
7
        .content()
16
7
        .iter()
17
17280
        .map(|cell| cell.symbol())
18
        .collect()
19
7
}
20

            
21
5
fn render_console(app: &App) -> String {
22
5
    render_console_at(app, 80)
23
5
}
24

            
25
6
fn render_console_at(app: &App, width: u16) -> String {
26
6
    let backend = TestBackend::new(width, 24);
27
6
    let mut terminal = Terminal::new(backend).expect("test terminal");
28
6
    terminal
29
6
        .draw(|frame| draw(frame, app, &mut crate::hits::Hits::default()))
30
6
        .expect("draw must not panic");
31
6
    buffer_text(&terminal)
32
6
}
33

            
34
6
fn console_app() -> App {
35
6
    let mut app = crate::test_support::app_for(Uuid::nil());
36
6
    app.switch_tab(Tab::Console);
37
6
    app
38
6
}
39

            
40
#[test]
41
1
fn transaction_modal_renders_split_row_details() {
42
    use crate::form::Form;
43
    use crate::modal::Modal;
44
    use crate::widgets::splits::{COL_FROM, COL_VALUE};
45
    use crate::widgets::{FocusedSubWidget, SelectOption, Widget};
46

            
47
1
    let mut app = crate::test_support::app_for(Uuid::nil());
48
1
    let mut form = Form::transaction_create();
49
1
    if let Widget::Splits(ref mut sw) = form.fields[3].widget {
50
1
        sw.set_account_options(vec![
51
1
            SelectOption {
52
1
                id: "acc-0".to_string(),
53
1
                label: "Cash".to_string(),
54
1
            },
55
1
            SelectOption {
56
1
                id: "acc-1".to_string(),
57
1
                label: "Food".to_string(),
58
1
            },
59
1
        ]);
60
1
        sw.col_focus = COL_VALUE;
61
1
        if let Some(FocusedSubWidget::Amount(aw)) = sw.focused_subwidget_mut() {
62
1
            aw.insert_char('5');
63
1
            aw.insert_char('0');
64
        }
65
1
        sw.col_focus = COL_FROM;
66
    }
67
1
    app.overlays.push(Modal::Form(form));
68

            
69
1
    let backend = TestBackend::new(120, 40);
70
1
    let mut terminal = Terminal::new(backend).expect("test terminal");
71
1
    terminal
72
1
        .draw(|frame| draw(frame, &app, &mut crate::hits::Hits::default()))
73
1
        .expect("draw must not panic");
74
1
    let text = buffer_text(&terminal);
75
1
    assert!(
76
1
        text.contains("Cash"),
77
        "the selected from-account label must render"
78
    );
79
1
    assert!(text.contains("50"), "typed amount must render: not found");
80
1
}
81

            
82
#[test]
83
1
fn console_tab_renders_scrollback_and_prompt() {
84
1
    let mut app = console_app();
85
1
    app.console_mut().push_scrollback("(:id 0 :value 42)");
86
1
    app.console_mut().input.insert_char('(');
87

            
88
1
    let text = render_console(&app);
89
1
    assert!(text.contains("(:id 0 :value 42)"), "scrollback missing");
90
1
    assert!(text.contains("nms>"), "prompt missing");
91
1
}
92

            
93
#[test]
94
1
fn console_prompt_shows_continuation_marker_when_pending() {
95
1
    let mut app = console_app();
96
1
    for c in "(list".chars() {
97
5
        app.console_mut().input.insert_char(c);
98
    }
99
1
    app.console_mut().take_complete_form();
100
1
    assert!(!app.console().pending.is_empty(), "form must be pending");
101

            
102
1
    let text = render_console(&app);
103
1
    assert!(text.contains("...>"), "continuation marker missing");
104
1
    assert!(!text.contains("nms>"), "primary prompt should be hidden");
105
1
}
106

            
107
#[test]
108
1
fn console_hint_switches_with_focus() {
109
1
    let mut app = console_app();
110
1
    let unfocused = render_console(&app);
111
1
    assert!(unfocused.contains("i Focus"), "blurred hint missing");
112

            
113
1
    app.focus_console();
114
1
    let focused = render_console(&app);
115
1
    assert!(focused.contains("Esc Leave"), "focused hint missing");
116
1
    let wide = render_console_at(&app, 120);
117
1
    assert!(wide.contains("C-c Interrupt"), "interrupt hint missing");
118
1
}
119

            
120
#[test]
121
1
fn console_prompt_uses_the_prompt_role_only_when_focused() {
122
1
    let theme = crate::test_support::dn_theme();
123
1
    let app = console_app();
124
1
    let backend = TestBackend::new(80, 24);
125
1
    let mut terminal = Terminal::new(backend).expect("test terminal");
126
1
    terminal
127
1
        .draw(|frame| draw(frame, &app, &mut crate::hits::Hits::default()))
128
1
        .expect("draw must not panic");
129
1
    let prompt_cell = prompt_marker_cell(&terminal);
130
1
    assert!(
131
1
        shows(&prompt_cell, theme.style(Role::PanelDim)),
132
        "blurred prompt must use the dim role"
133
    );
134

            
135
1
    let mut focused = console_app();
136
1
    focused.focus_console();
137
1
    let backend = TestBackend::new(80, 24);
138
1
    let mut terminal = Terminal::new(backend).expect("test terminal");
139
1
    terminal
140
1
        .draw(|frame| draw(frame, &focused, &mut crate::hits::Hits::default()))
141
1
        .expect("draw must not panic");
142
1
    let prompt_cell = prompt_marker_cell(&terminal);
143
1
    assert!(
144
1
        shows(&prompt_cell, theme.style(Role::Prompt)),
145
        "focused prompt must use the prompt role"
146
    );
147
1
}
148

            
149
4
fn shows(cell: &Cell, style: Style) -> bool {
150
4
    Some(cell.fg) == style.fg && Some(cell.bg) == style.bg && cell.modifier == style.add_modifier
151
4
}
152

            
153
2
fn prompt_marker_cell(terminal: &Terminal<TestBackend>) -> ratatui::buffer::Cell {
154
2
    let buffer = terminal.backend().buffer();
155
2
    for y in 0..buffer.area.height {
156
44
        if buffer[(1, y)].symbol() == "n" && buffer[(2, y)].symbol() == "m" {
157
2
            return buffer[(1, y)].clone();
158
42
        }
159
    }
160
    panic!("nms> prompt marker not found in rendered buffer");
161
2
}
162

            
163
#[test]
164
1
fn form_focused_field_uses_the_focused_input_role_unfocused_the_input_role() {
165
1
    let theme = crate::test_support::dn_theme();
166
    use crate::form::Form;
167

            
168
1
    let mut app = crate::test_support::app_for(Uuid::nil());
169
1
    let form = Form::config_set();
170
1
    assert_eq!(form.focus, 0, "focus must start on first field");
171
1
    app.overlays.push(Modal::Form(form));
172

            
173
1
    let backend = TestBackend::new(120, 40);
174
1
    let mut terminal = Terminal::new(backend).expect("test terminal");
175
1
    terminal
176
1
        .draw(|frame| draw(frame, &app, &mut crate::hits::Hits::default()))
177
1
        .expect("draw must not panic");
178

            
179
1
    let cursor = terminal.get_cursor_position().expect("the cursor position");
180
1
    let buffer = terminal.backend().buffer();
181
1
    assert!(
182
1
        shows(
183
1
            &buffer[(cursor.x, cursor.y)],
184
1
            theme.style(Role::InputFocused)
185
1
        ),
186
        "focused field must use the focused input role"
187
    );
188
1
    assert!(
189
1
        shows(&buffer[(cursor.x, cursor.y + 1)], theme.style(Role::Input)),
190
        "unfocused field must use the input role"
191
    );
192
1
}
193

            
194
#[test]
195
1
fn scrollback_shows_only_the_tail_when_longer_than_viewport() {
196
1
    let mut app = console_app();
197
1
    for i in 0..100 {
198
100
        app.console_mut().push_scrollback(format!("line-{i:03}"));
199
    }
200
1
    let text = render_console(&app);
201
1
    assert!(
202
1
        text.contains(&format!("line-{:03}", 99)),
203
        "newest line must be visible"
204
    );
205
1
    assert!(
206
1
        !text.contains("line-000"),
207
        "oldest line must be scrolled out of the viewport"
208
    );
209
1
}
210

            
211
4
fn render_app(app: &App) -> String {
212
4
    let backend = TestBackend::new(120, 40);
213
4
    let mut terminal = Terminal::new(backend).expect("test terminal");
214
4
    terminal
215
4
        .draw(|frame| draw(frame, app, &mut crate::hits::Hits::default()))
216
4
        .expect("draw must not panic");
217
4
    terminal
218
4
        .backend()
219
4
        .buffer()
220
4
        .content()
221
4
        .iter()
222
19200
        .map(|cell| cell.symbol())
223
        .collect()
224
4
}
225

            
226
#[test]
227
1
fn help_window_contains_command_palette_key() {
228
1
    let mut app = crate::test_support::app();
229
1
    crate::event::apply(&mut app, crate::event::Intent::OpenHelp);
230
1
    let text = render_app(&app);
231
1
    assert!(text.contains(':'), "help must mention the ':' key");
232
1
}
233

            
234
#[test]
235
1
fn help_window_contains_list_view_keys() {
236
1
    let mut app = crate::test_support::app();
237
1
    crate::event::apply(&mut app, crate::event::Intent::OpenHelp);
238
1
    let text = render_app(&app);
239
1
    assert!(text.contains('d'), "help must mention 'd' for delete");
240
1
    assert!(text.contains('e'), "help must mention 'e' for edit/tag");
241
1
}
242

            
243
#[test]
244
1
fn help_modal_names_the_window_keys() {
245
1
    let app = crate::test_support::app();
246
1
    let text = crate::keymap::help_lines(&app).join("\n");
247
1
    assert!(
248
1
        text.contains("F6"),
249
        "help must mention F6 for the next window"
250
    );
251
1
}
252

            
253
2
fn screen_rows(app: &App) -> Vec<String> {
254
2
    let mut terminal = Terminal::new(TestBackend::new(80, 24)).expect("test terminal");
255
2
    terminal
256
2
        .draw(|frame| draw(frame, app, &mut crate::hits::Hits::default()))
257
2
        .expect("draw");
258
2
    let buffer = terminal.backend().buffer();
259
2
    buffer
260
2
        .content()
261
2
        .chunks(usize::from(buffer.area.width))
262
3840
        .map(|row| row.iter().map(|cell| cell.symbol()).collect())
263
        .collect()
264
2
}
265

            
266
#[test]
267
1
fn help_shows_its_top_lines_on_80x24_and_scrolls_to_the_rest() {
268
1
    let mut app = crate::test_support::app();
269
1
    crate::event::apply(&mut app, crate::event::Intent::OpenHelp);
270
1
    let Some(help) = app.front::<crate::viewer::Viewer>() else {
271
        panic!("help is open");
272
    };
273
1
    let lines = help.lines.clone();
274
1
    let top = screen_rows(&app);
275
3
    assert!(top.iter().any(|row| row.contains(lines[0].trim_end())));
276
1
    let last = lines.last().expect("help has lines").trim_end();
277
24
    assert!(!top.iter().any(|row| row.contains(last)), "{last:?}");
278
1
    for _ in 0..lines.len() {
279
60
        crate::event::apply(&mut app, crate::event::Intent::ScrollPageDown);
280
    }
281
1
    let bottom = screen_rows(&app);
282
22
    assert!(bottom.iter().any(|row| row.contains(last)), "{last:?}");
283
1
}
284

            
285
#[test]
286
1
fn cmdline_active_with_completions_shows_them_in_status() {
287
1
    let mut app = crate::test_support::app();
288
1
    apply(&mut app, Intent::OpenCommandLine);
289
1
    for c in "account ".chars() {
290
8
        apply(&mut app, Intent::InsertChar(c));
291
    }
292
1
    apply(&mut app, Intent::CompleteCommandLine);
293
1
    let text = render_app(&app);
294
1
    assert!(
295
1
        text.contains("create"),
296
        "completion candidates must appear in the status area"
297
    );
298
1
}
299

            
300
#[test]
301
1
fn cmdline_active_without_completions_shows_the_typed_text() {
302
1
    let mut app = crate::test_support::app();
303
1
    apply(&mut app, Intent::OpenCommandLine);
304
1
    for c in "ver".chars() {
305
3
        apply(&mut app, Intent::InsertChar(c));
306
    }
307
1
    let text = render_app(&app);
308
1
    assert!(text.contains(":ver"), "the typed text must be shown");
309
1
    assert!(!text.contains("cursor"), "no debug cursor position");
310
1
}
311

            
312
12
fn assert_no_default_background(app: &App) {
313
12
    let mut terminal = Terminal::new(TestBackend::new(80, 24)).expect("test terminal");
314
12
    terminal
315
12
        .draw(|frame| draw(frame, app, &mut crate::hits::Hits::default()))
316
12
        .expect("draw");
317
12
    let buffer = terminal.backend().buffer();
318
12
    for y in 0..buffer.area.height {
319
288
        for x in 0..buffer.area.width {
320
23040
            assert_ne!(buffer[(x, y)].bg, Color::Reset, "({x}, {y})");
321
        }
322
    }
323
12
}
324

            
325
#[test]
326
1
fn the_whole_default_screen_is_filled() {
327
1
    assert_no_default_background(&crate::test_support::app());
328
1
}
329

            
330
#[test]
331
1
fn the_whole_screen_is_filled_under_a_modal() {
332
1
    let mut app = crate::test_support::app();
333
1
    crate::test_support::open_overlay(&mut app);
334
1
    assert_no_default_background(&app);
335
1
}
336

            
337
#[test]
338
1
fn every_tab_fills_the_screen() {
339
1
    for tab in Tab::ALL {
340
10
        let mut app = crate::test_support::app();
341
10
        app.switch_tab(tab);
342
10
        assert_no_default_background(&app);
343
    }
344
1
}
345

            
346
3
fn bottom_row(app: &App, width: u16) -> String {
347
3
    let mut terminal = Terminal::new(TestBackend::new(width, 24)).expect("test terminal");
348
3
    terminal
349
3
        .draw(|frame| draw(frame, app, &mut crate::hits::Hits::default()))
350
3
        .expect("draw");
351
3
    let buffer = terminal.backend().buffer();
352
280
    (0..width).map(|x| buffer[(x, 23)].symbol()).collect()
353
3
}
354

            
355
#[test]
356
1
fn the_status_bar_shows_the_hints_and_the_keymap_indicator() {
357
1
    let cua = bottom_row(&crate::test_support::app(), 80);
358
1
    assert!(cua.contains("F1 Help  M-x Quit"), "{cua:?}");
359
1
    assert!(cua.trim_end().ends_with("[cua]"), "{cua:?}");
360
1
    let emacs = bottom_row(&crate::test_support::emacs_app(), 120);
361
1
    assert!(emacs.contains("q Close/Quit  ? Help"), "{emacs:?}");
362
1
    assert!(emacs.trim_end().ends_with("[emacs]"), "{emacs:?}");
363
1
}
364

            
365
#[test]
366
1
fn a_status_message_takes_the_whole_row() {
367
1
    let mut app = crate::test_support::app();
368
1
    app.status = "x".repeat(200);
369
1
    let row = bottom_row(&app, 80);
370
1
    assert!(row.starts_with("[cua] xxx"), "{row:?}");
371
1
    assert!(!row.contains("F1 Help"), "{row:?}");
372
1
}
373

            
374
#[test]
375
1
fn the_status_hotkeys_use_the_hotkey_role() {
376
1
    let app = crate::test_support::app();
377
1
    let mut terminal = Terminal::new(TestBackend::new(80, 24)).expect("test terminal");
378
1
    terminal
379
1
        .draw(|frame| draw(frame, &app, &mut crate::hits::Hits::default()))
380
1
        .expect("draw");
381
1
    let row: String = (0..80)
382
80
        .map(|x| terminal.backend().buffer()[(x, 23)].symbol())
383
        .collect();
384
1
    let at = row.find("F1").expect("the F1 hint is shown");
385
1
    let cell = &terminal.backend().buffer()[(u16::try_from(at).expect("fits"), 23)];
386
1
    assert_eq!(cell.fg, app.theme.style(Role::StatusHotkey).fg.expect("fg"));
387
1
}