1
use super::*;
2
use crate::app::App;
3
use crate::hits::Hits;
4
use ratatui::Terminal;
5
use ratatui::backend::TestBackend;
6
use ratatui::layout::Rect;
7
use tvui::Dialog;
8

            
9
/// The rows of the body of `form` drawn in a dialog on a roomy screen.
10
16
pub(in crate::draw) fn form_rows(form: &Form, pending: bool, app: &App) -> Vec<String> {
11
16
    let body = form_body(form, (pending, true), app);
12
16
    let screen = Rect::new(0, 0, 100, 60);
13
    let mut terminal =
14
16
        Terminal::new(TestBackend::new(screen.width, screen.height)).expect("a test terminal");
15
16
    terminal
16
16
        .draw(|frame| {
17
16
            let at = tvui::Place::from(screen);
18
16
            let mut hits = Hits::default();
19
16
            crate::draw::canvas::draw_placed(frame, at, screen, &mut hits, |canvas, area, hits| {
20
16
                draw_form(canvas, area, &body, &[], &app.theme, false, hits);
21
16
            });
22
16
        })
23
16
        .expect("the form draws");
24
16
    let buffer = terminal.backend().buffer();
25
16
    let inner = Dialog::body_area(screen);
26
16
    (inner.y..inner.y + u16::try_from(body.height()).unwrap_or(0))
27
171
        .map(|y| {
28
171
            let mut row = String::new();
29
171
            let mut x = inner.x;
30
16929
            while x < inner.right() {
31
16758
                let symbol = buffer[(x, y)].symbol();
32
16758
                row.push_str(symbol);
33
16758
                x += u16::try_from(Line::from(symbol).width().max(1)).unwrap_or(1);
34
            }
35
171
            row.trim_end().to_string()
36
171
        })
37
        .collect()
38
16
}
39

            
40
11
fn texts(app: &App, form: &Form) -> Vec<String> {
41
11
    form_rows(form, false, app)
42
11
}
43

            
44
#[test]
45
1
fn the_transaction_footer_is_one_note_wherever_the_focus_is() {
46
1
    let mut form = Form::transaction_create();
47
1
    for focus in 0..=form.fields.len() {
48
5
        form.focus = focus;
49
5
        let texts = texts(&crate::test_support::app(), &form);
50
65
        let notes: Vec<&String> = texts.iter().filter(|l| l.starts_with("Date:")).collect();
51
5
        assert_eq!(notes, ["Date: YYYY-MM-DDTHH:MM"], "{texts:?}");
52
65
        assert!(!texts.iter().any(|l| l.contains("C-t")), "{texts:?}");
53
    }
54
1
}
55

            
56
#[test]
57
1
fn a_form_whose_keys_are_all_on_the_bar_has_no_footer() {
58
1
    let texts = texts(&crate::test_support::app(), &Form::config_set());
59
1
    assert_eq!(texts, ["  Key:", "  Value:"]);
60
1
}
61

            
62
#[test]
63
1
fn footer_separated_from_fields_by_single_blank_line() {
64
1
    let form = Form::ssh_key_add();
65
1
    let texts = form_rows(&form, false, &crate::test_support::app());
66
1
    let footer = texts
67
1
        .iter()
68
4
        .position(|l| l.contains("Paste one public key"))
69
1
        .expect("footer line present");
70
1
    assert!(
71
1
        footer >= 1 && texts[footer - 1].trim().is_empty(),
72
        "exactly one blank line must precede the footer: {texts:?}"
73
    );
74
1
    assert!(
75
1
        footer < 2 || !texts[footer - 2].trim().is_empty(),
76
        "no double blank line before the footer: {texts:?}"
77
    );
78
1
}
79

            
80
#[test]
81
1
fn pending_form_shows_submitting_indicator_instead_of_the_key_hints() {
82
1
    let form = Form::config_set();
83
1
    let texts = form_rows(&form, true, &crate::test_support::app());
84
4
    assert!(texts.iter().any(|l| l.contains("Submitting")), "{texts:?}");
85
1
    assert!(
86
4
        !texts.iter().any(|l| l.contains("Paste one public key")),
87
        "{texts:?}"
88
    );
89
1
}
90

            
91
#[test]
92
1
fn unlocked_form_shows_its_error_messages() {
93
1
    let mut form = Form::config_set();
94
1
    form.errors = vec!["value: too long".to_string()];
95
1
    let texts = form_rows(&form, false, &crate::test_support::app());
96
3
    assert!(texts.iter().any(|l| l == "! value: too long"), "{texts:?}");
97
1
}
98

            
99
#[test]
100
1
fn a_multiline_note_shows_its_lines_on_separate_rows() {
101
1
    let mut values = forms::FormValues::default();
102
1
    values.set("note", "first\nsecond");
103
1
    let form = Form::transaction_edit(&values);
104
1
    let texts = form_rows(&form, false, &crate::test_support::app());
105
2
    assert!(texts.iter().any(|l| l.contains("first")), "{texts:?}");
106
3
    assert!(texts.iter().any(|l| l.contains("second")), "{texts:?}");
107
13
    assert!(!texts.iter().any(|l| l.contains('\u{23ce}')), "{texts:?}");
108
1
    assert_eq!(form.values().text("note"), Some("first\nsecond"));
109
1
}
110

            
111
3
fn transaction_rows(app: &App) -> Vec<String> {
112
3
    texts(app, &Form::transaction_create())
113
3
}
114

            
115
2
fn tags_block(rows: &[String]) -> Vec<&String> {
116
2
    let at = rows
117
2
        .iter()
118
14
        .position(|l| l.trim_start().starts_with("Tags:"))
119
2
        .expect("the Tags field is drawn");
120
2
    rows[at..].iter().collect()
121
2
}
122

            
123
#[test]
124
1
fn an_empty_tags_field_says_how_to_add_a_tag() {
125
1
    let rows = transaction_rows(&crate::test_support::app());
126
1
    let block = tags_block(&rows);
127
1
    assert!(
128
3
        block.iter().any(|l| l.contains("(no tags: C-n adds one)")),
129
        "{rows:?}"
130
    );
131
1
}
132

            
133
#[test]
134
1
fn the_empty_tags_note_names_the_key_of_the_keymap() {
135
1
    let mut app = crate::test_support::app();
136
1
    app.keys = app
137
1
        .keys
138
1
        .overridden("widget-cells: C-n = none; widget-cells: F9 = add-row")
139
1
        .expect("the override parses");
140
1
    let rows = transaction_rows(&app);
141
1
    let block = tags_block(&rows);
142
1
    assert!(
143
3
        block.iter().any(|l| l.contains("(no tags: F9 adds one)")),
144
        "{rows:?}"
145
    );
146
13
    assert!(!rows.iter().any(|l| l.contains("C-n adds one")), "{rows:?}");
147
1
}
148

            
149
2
fn open_split_tags(form: &mut Form, focus: usize) {
150
2
    form.focus = 3;
151
2
    let Some(Widget::Splits(splits)) = form.fields.get_mut(3).map(|f| &mut f.widget) else {
152
        panic!("splits field");
153
    };
154
2
    splits.open_tags();
155
2
    form.focus = focus;
156
2
}
157

            
158
#[test]
159
1
fn an_empty_open_split_tag_list_names_the_add_key_with_or_without_focus() {
160
1
    let app = crate::test_support::app();
161
1
    for focus in [3, 0] {
162
2
        let mut form = Form::transaction_create();
163
2
        open_split_tags(&mut form, focus);
164
2
        let rows = texts(&app, &form);
165
2
        let Some(from) = rows
166
2
            .iter()
167
20
            .position(|l| l.trim_start().starts_with("Splits:"))
168
        else {
169
            panic!("the splits field is drawn: {rows:?}");
170
        };
171
2
        let note = rows[from..]
172
2
            .iter()
173
8
            .any(|l| l.contains("(no tags: C-n adds one)"));
174
2
        assert!(note, "focus {focus}: {rows:?}");
175
    }
176
1
}
177

            
178
#[test]
179
1
fn a_split_line_shows_its_tag_count_even_when_zero() {
180
1
    let rows = transaction_rows(&crate::test_support::app());
181
11
    assert!(rows.iter().any(|l| l.contains("#0")), "{rows:?}");
182
1
}