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

            
9
9
fn note_form(note: &str, focused: bool) -> Form {
10
9
    let mut values = forms::FormValues::default();
11
9
    values.set("note", note);
12
9
    let mut form = Form::transaction_edit(&values);
13
18
    let note = form.fields.iter().position(|f| f.key == Some("note"));
14
9
    form.focus = if focused { note.unwrap_or(0) } else { 0 };
15
9
    form
16
9
}
17

            
18
/// The form of [`note_form`] with the cursor moved to the end of the note, as after typing it.
19
7
fn typed_note_form(note: &str, focused: bool) -> Form {
20
7
    let mut form = note_form(note, focused);
21
7
    for field in &mut form.fields {
22
28
        if let Some(ed) = field.widget.as_text_mut().filter(|ed| ed.is_multiline()) {
23
7
            ed.replace_buffer(note);
24
21
        }
25
    }
26
7
    form
27
7
}
28

            
29
5
fn numbered(count: usize) -> String {
30
5
    (0..count)
31
100
        .map(|i| format!("row{i:02}"))
32
        .collect::<Vec<_>>()
33
5
        .join("\n")
34
5
}
35

            
36
struct Screen {
37
    buffer: Buffer,
38
    cursor: Option<Position>,
39
}
40

            
41
impl Screen {
42
56
    fn rows(&self) -> Vec<String> {
43
56
        let area = self.buffer.area;
44
56
        (0..area.height)
45
1560
            .map(|y| {
46
1560
                (0..area.width)
47
150240
                    .map(|x| self.buffer[(x, y)].symbol().to_string())
48
                    .collect()
49
1560
            })
50
            .collect()
51
56
    }
52

            
53
54
    fn find(&self, text: &str) -> Option<Position> {
54
54
        let rows = self.rows();
55
54
        let (y, row) = rows
56
54
            .iter()
57
54
            .enumerate()
58
746
            .find(|(_, row)| row.contains(text))?;
59
51
        let x = row[..row.find(text)?].chars().count();
60
51
        Some(Position::new(
61
51
            u16::try_from(x).ok()?,
62
51
            u16::try_from(y).ok()?,
63
        ))
64
54
    }
65
}
66

            
67
9
fn draw(form: Form, size: (u16, u16)) -> (Screen, crate::app::App) {
68
9
    let mut app = crate::test_support::app();
69
9
    app.overlays.push(Modal::Form(form));
70
9
    let screen = draw_app(&app, size);
71
9
    (screen, app)
72
9
}
73

            
74
29
fn draw_app(app: &crate::app::App, size: (u16, u16)) -> Screen {
75
29
    let mut terminal = Terminal::new(TestBackend::new(size.0, size.1)).expect("test terminal");
76
29
    terminal
77
29
        .draw(|frame| crate::draw::draw(frame, app, &mut crate::hits::Hits::default()))
78
29
        .expect("the frame draws");
79
29
    let backend = terminal.backend();
80
    Screen {
81
29
        buffer: backend.buffer().clone(),
82
29
        cursor: backend.cursor_visible().then(|| backend.cursor_position()),
83
    }
84
29
}
85

            
86
2
fn note_editor(app: &mut crate::app::App) -> &mut crate::widgets::Editor {
87
2
    let Some(Modal::Form(form)) = app.overlays.top_mut() else {
88
        panic!("the form is open");
89
    };
90
2
    let focus = form.focus;
91
2
    form.fields[focus]
92
2
        .widget
93
2
        .as_text_mut()
94
2
        .expect("a text field")
95
2
}
96

            
97
#[test]
98
1
fn a_focused_text_area_draws_its_lines_in_the_focused_input_colours_with_the_cursor_in_it() {
99
1
    let (screen, _) = draw(typed_note_form("first\nsecond", true), (80, 24));
100
1
    let first = screen.find("first").expect("the first line is drawn");
101
1
    let theme = crate::test_support::dn_theme();
102
1
    let want = theme.style(Role::InputFocused);
103
1
    let style = screen.buffer[(first.x, first.y)].style();
104
1
    assert_eq!((style.fg, style.bg), (want.fg, want.bg));
105
1
    let second = screen.find("second").expect("the second line is drawn");
106
1
    assert_eq!((second.x, second.y), (first.x, first.y + 1));
107
1
    assert_eq!(screen.cursor, Some(Position::new(first.x + 6, first.y + 1)));
108
1
}
109

            
110
#[test]
111
1
fn an_unfocused_text_area_draws_its_lines_in_the_input_colours_without_a_cursor() {
112
1
    let (screen, _) = draw(note_form("first\nsecond", false), (80, 24));
113
1
    let first = screen.find("first").expect("the first line is drawn");
114
1
    assert!(screen.find("second").is_some());
115
24
    assert!(!screen.rows().iter().any(|row| row.contains('\u{23ce}')));
116
1
    let want = crate::test_support::dn_theme().style(Role::Input);
117
1
    let style = screen.buffer[(first.x, first.y)].style();
118
1
    assert_eq!((style.fg, style.bg), (want.fg, want.bg));
119
1
    assert_eq!(screen.cursor.map(|at| at.y == first.y), Some(false));
120
1
}
121

            
122
18
fn dialog(screen: &Screen) -> (Position, Position) {
123
36
    let corner = |mark| screen.find(mark).expect("the dialog frame is drawn");
124
18
    (corner("\u{2554}"), corner("\u{255d}"))
125
18
}
126

            
127
#[test]
128
1
fn the_dialog_keeps_its_rect_when_the_focus_moves_in_and_out_of_a_text_area() {
129
1
    let none = forms::FormValues::default();
130
1
    for size in [(80, 24), (120, 36)] {
131
2
        for form in [Form::tag_create(), Form::transaction_edit(&none)] {
132
4
            let count = form.fields.len();
133
4
            let mut app = crate::test_support::app();
134
4
            app.overlays.push(Modal::Form(form));
135
4
            let mut seen = Vec::new();
136
4
            for focus in 0..=count {
137
18
                let Some(Modal::Form(form)) = app.overlays.top_mut() else {
138
                    panic!("the form is open");
139
                };
140
18
                form.focus = focus;
141
18
                seen.push(dialog(&draw_app(&app, size)));
142
            }
143
14
            assert!(seen.windows(2).all(|pair| pair[0] == pair[1]), "{seen:?}");
144
        }
145
    }
146
1
}
147

            
148
#[test]
149
1
fn a_loaded_text_shows_its_first_lines_from_the_first_column_when_unfocused() {
150
1
    let lines: Vec<String> = (0..9)
151
9
        .map(|i| format!("row{i} {}", "x".repeat(60)))
152
        .collect();
153
1
    let (screen, _) = draw(note_form(&lines.join("\n"), false), (80, 24));
154
1
    let first = screen.find("row0").expect("the first line is shown");
155
1
    let fifth = screen.find("row4").expect("five lines are shown");
156
1
    assert_eq!(fifth, Position::new(first.x, first.y + 4));
157
1
    assert!(screen.find("row5").is_none());
158
1
}
159

            
160
#[test]
161
1
fn no_marker_and_no_scroll_lines_remain() {
162
1
    let (screen, _) = draw(typed_note_form(&numbered(20), true), (80, 24));
163
1
    let rows = screen.rows().join("\n");
164
1
    assert!(!rows.contains('\u{25b8}'), "{rows}");
165
1
    assert!(!rows.contains("more above"), "{rows}");
166
1
    assert!(!rows.contains("more)"), "{rows}");
167
1
}
168

            
169
#[test]
170
1
fn a_long_text_scrolls_to_keep_the_cursor_visible() {
171
1
    let (screen, _) = draw(typed_note_form(&numbered(20), true), (80, 24));
172
1
    assert!(screen.find("row14").is_none());
173
1
    assert!(screen.find("row00").is_none());
174
1
    let first = screen.find("row15").expect("the window starts at row15");
175
1
    let last = screen.find("row19").expect("the last line is shown");
176
1
    assert_eq!(last.y, first.y + 4);
177
1
    assert_eq!(screen.cursor, Some(Position::new(last.x + 5, last.y)));
178
1
}
179

            
180
#[test]
181
1
fn a_long_line_scrolls_sideways_to_keep_the_cursor_visible() {
182
1
    let text = "x".repeat(60) + "END";
183
1
    let (screen, _) = draw(typed_note_form(&text, true), (80, 24));
184
1
    let end = screen.find("END").expect("the line end is shown");
185
1
    assert_eq!(screen.cursor, Some(Position::new(end.x + 3, end.y)));
186
1
}
187

            
188
#[test]
189
1
fn the_view_is_stable_while_the_cursor_moves_inside_it() {
190
1
    let (before, mut app) = draw(typed_note_form(&numbered(20), true), (80, 24));
191
1
    let at = before.find("row19").expect("the last line is shown");
192
1
    note_editor(&mut app).move_up();
193
1
    let after = draw_app(&app, (80, 24));
194
1
    assert_eq!(after.find("row19"), Some(at));
195
1
    assert_eq!(after.cursor, Some(Position::new(at.x + 5, at.y - 1)));
196
1
}
197

            
198
#[test]
199
1
fn the_text_area_fits_a_narrow_screen() {
200
1
    let (screen, _) = draw(typed_note_form(&numbered(20), true), (60, 24));
201
1
    assert!(screen.find("row19").is_some());
202
1
    assert!(screen.cursor.is_some());
203
1
}
204

            
205
#[test]
206
1
fn joining_the_last_line_while_scrolled_down_leaves_no_empty_row() {
207
1
    let (before, mut app) = draw(typed_note_form(&numbered(20), true), (80, 24));
208
1
    let top = before.find("row15").expect("the window starts at row15");
209
1
    let editor = note_editor(&mut app);
210
1
    editor.move_home();
211
1
    editor.delete_backward();
212
1
    let after = draw_app(&app, (80, 24));
213
1
    let first = after
214
1
        .find("row14")
215
1
        .expect("the window moved up by the lost line");
216
1
    assert_eq!(first.y, top.y);
217
1
    let last = after
218
1
        .find("row18row19")
219
1
        .expect("the joined line is the last");
220
1
    assert_eq!(last.y, top.y + 4);
221
1
}