1
//! The status-bar hints of the focused form field, which the form footers no longer carry.
2

            
3
use crate::app::App;
4
use crate::form::Form;
5
use crate::modal::Modal;
6
use crate::tabs::reports::ReportKind;
7
use crate::test_support::hint_pairs;
8
use crate::widgets::Widget;
9
use ratatui::Terminal;
10
use ratatui::backend::TestBackend;
11

            
12
const PRESETS: [&str; 3] = ["cua", "emacs", "vim"];
13

            
14
13
fn shown_labels(preset: &str, mut form: Form, field: impl Fn(&Widget) -> bool) -> Vec<String> {
15
13
    form.focus = form
16
13
        .fields
17
13
        .iter()
18
31
        .position(|f| field(&f.widget))
19
13
        .expect("the field is in the form");
20
13
    let mut app: App = crate::test_support::app_with_preset(preset);
21
13
    app.overlays.push(Modal::Form(form));
22
13
    hint_pairs(&app)
23
13
        .into_iter()
24
        .map(|(_, label)| label)
25
        .collect()
26
13
}
27

            
28
12
fn assert_shows(preset: &str, labels: &[String], wanted: &[&str]) {
29
12
    for label in wanted {
30
30
        assert!(
31
85
            labels.iter().any(|l| l == label),
32
            "{preset}: {label} in {labels:?}"
33
        );
34
    }
35
12
}
36

            
37
#[test]
38
1
fn a_split_cell_hints_its_row_and_tag_keys() {
39
1
    for preset in PRESETS {
40
12
        let labels = shown_labels(preset, Form::transaction_create(), |w| {
41
12
            matches!(w, Widget::Splits(_))
42
12
        });
43
3
        assert_shows(
44
3
            preset,
45
3
            &labels,
46
3
            &["Add row", "Remove row", "Tags", "Suggest"],
47
3
        );
48
    }
49
1
}
50

            
51
#[test]
52
1
fn a_rows_cell_hints_its_row_keys_and_suggestion() {
53
1
    let none = forms::FormValues::default();
54
1
    for preset in PRESETS {
55
9
        let labels = shown_labels(preset, Form::account_edit(&none), |w| {
56
9
            matches!(w, Widget::Rows(_))
57
9
        });
58
3
        assert_shows(preset, &labels, &["Add row", "Remove row", "Suggest"]);
59
    }
60
1
}
61

            
62
#[test]
63
1
fn a_text_area_hints_its_submit_and_newline_keys() {
64
1
    for preset in PRESETS {
65
3
        let labels = shown_labels(
66
3
            preset,
67
3
            Form::transaction_create(),
68
6
            |w| matches!(w, Widget::Text(ed) if ed.is_multiline()),
69
        );
70
3
        assert_shows(preset, &labels, &["Newline"]);
71
    }
72
1
}
73

            
74
#[test]
75
1
fn a_date_field_hints_the_day_steps() {
76
1
    for preset in PRESETS {
77
3
        let labels = shown_labels(preset, Form::transaction_create(), |w| {
78
3
            matches!(w, Widget::Date(_))
79
3
        });
80
3
        assert_shows(preset, &labels, &["Day back", "Day forward"]);
81
    }
82
1
}
83

            
84
#[test]
85
1
fn the_hints_stay_off_a_plain_field() {
86
1
    let labels = shown_labels("cua", Form::config_set(), |w| matches!(w, Widget::Text(_)));
87
1
    for label in ["Newline", "Add row", "Tags", "Suggest"] {
88
16
        assert!(!labels.iter().any(|l| l == label), "{label} in {labels:?}");
89
    }
90
1
}
91

            
92
7
fn status_row(preset: &str, mut form: Form, focus: usize) -> String {
93
7
    form.focus = focus;
94
7
    let mut app: App = crate::test_support::app_with_preset(preset);
95
7
    app.overlays.push(Modal::Form(form));
96
7
    let mut terminal = Terminal::new(TestBackend::new(80, 24)).expect("a test terminal");
97
7
    terminal
98
7
        .draw(|frame| crate::draw::draw(frame, &app, &mut crate::hits::Hits::default()))
99
7
        .expect("the frame draws");
100
7
    let buffer = terminal.backend().buffer();
101
560
    (0..80).map(|x| buffer[(x, 23)].symbol()).collect()
102
7
}
103

            
104
3
fn field_of(form: &Form, key: &str) -> usize {
105
3
    form.fields
106
3
        .iter()
107
7
        .position(|f| f.key == Some(key))
108
3
        .expect("the field is in the form")
109
3
}
110

            
111
#[test]
112
1
fn a_filter_field_shows_the_builder_key_and_other_fields_do_not() {
113
3
    let form = || Form::report(ReportKind::Balance, &forms::FormValues::default());
114
1
    let on = field_of(&form(), "filter");
115
1
    let row = status_row("cua", form(), on);
116
1
    assert!(row.contains("C-b Builder"), "{row}");
117
1
    let row = status_row("cua", form(), 0);
118
1
    assert!(!row.contains("Builder"), "{row}");
119
1
}
120

            
121
#[test]
122
1
fn the_template_form_shows_test_not_tags_on_each_field() {
123
1
    for key in ["name", "source"] {
124
2
        let form = Form::template_create();
125
2
        let focus = field_of(&form, key);
126
2
        let row = status_row("cua", form, focus);
127
2
        assert!(row.contains("C-t Test"), "{key}: {row}");
128
2
        assert!(!row.contains("Tags"), "{key}: {row}");
129
    }
130
1
}
131

            
132
#[test]
133
1
fn a_split_cell_shows_the_tags_key_at_80_columns() {
134
1
    for preset in PRESETS {
135
3
        let form = Form::transaction_create();
136
3
        let focus = form
137
3
            .fields
138
3
            .iter()
139
12
            .position(|f| matches!(f.widget, Widget::Splits(_)))
140
3
            .expect("the splits field");
141
3
        let row = status_row(preset, form, focus);
142
3
        assert!(row.contains("Tags"), "{preset}: {row}");
143
    }
144
1
}
145

            
146
2
fn splits_app(overrides: &str) -> (App, usize) {
147
2
    let mut app: App = crate::test_support::app();
148
2
    app.keys = app.keys.overridden(overrides).expect("the override parses");
149
2
    let mut form = Form::transaction_create();
150
2
    form.focus = form
151
2
        .fields
152
2
        .iter()
153
8
        .position(|f| matches!(f.widget, Widget::Splits(_)))
154
2
        .expect("the splits field");
155
2
    let focus = form.focus;
156
2
    app.overlays.push(Modal::Form(form));
157
2
    (app, focus)
158
2
}
159

            
160
2
fn hint_keys(app: &App) -> Vec<String> {
161
2
    hint_pairs(app)
162
2
        .into_iter()
163
16
        .map(|(key, label)| format!("{key} {label}"))
164
        .collect()
165
2
}
166

            
167
2
fn tags_open(app: &App, focus: usize) -> bool {
168
2
    let Some(Modal::Form(form)) = app.overlays.top() else {
169
        panic!("the form is open");
170
    };
171
2
    matches!(&form.fields[focus].widget, Widget::Splits(sw) if sw.tags_open())
172
2
}
173

            
174
#[test]
175
1
fn the_tags_hint_comes_before_the_row_keys_on_a_split_cell() {
176
1
    let (app, _) = splits_app("");
177
1
    let shown = hint_keys(&app);
178
2
    let at = |hint: &str| {
179
2
        shown
180
2
            .iter()
181
7
            .position(|s| s == hint)
182
2
            .expect("the hint shows")
183
2
    };
184
1
    assert!(at("C-t Tags") < at("+ Add row"), "{shown:?}");
185
1
}
186

            
187
#[test]
188
1
fn rebinding_the_overlay_chord_moves_the_split_hint_and_the_key() {
189
1
    let (mut app, focus) = splits_app("overlay: C-t = none; overlay: C-g = toggle-tags");
190
1
    let shown = hint_keys(&app);
191
1
    assert!(shown.contains(&"C-g Tags".to_string()), "{shown:?}");
192
8
    assert!(!shown.iter().any(|s| s.starts_with("C-t")), "{shown:?}");
193
1
    crate::test_support::press(
194
1
        &mut app,
195
1
        crate::test_support::ctrl(ratatui::crossterm::event::KeyCode::Char('t')),
196
1
    );
197
1
    assert!(!tags_open(&app, focus));
198
1
    crate::test_support::press(
199
1
        &mut app,
200
1
        crate::test_support::ctrl(ratatui::crossterm::event::KeyCode::Char('g')),
201
1
    );
202
1
    assert!(tags_open(&app, focus));
203
1
}