1
//! Keys inside select and splits form fields.
2

            
3
use super::tests::{ctrl, key, press};
4
use crate::app::App;
5
use crate::form::{Field, Form, FormKind};
6
use crate::modal::Modal;
7
use crate::widgets::splits::{COL_FROM, COL_TO, COL_TO_AMOUNT, COL_VALUE};
8
use crate::widgets::{Editor, SelectOption, SelectWidget, SplitsWidget, Widget};
9
use ratatui::crossterm::event::KeyCode;
10

            
11
20
fn options() -> Vec<SelectOption> {
12
20
    ["alpha", "beta", "gamma"]
13
20
        .into_iter()
14
        .map(|name| SelectOption {
15
60
            id: name.to_string(),
16
60
            label: name.to_string(),
17
60
        })
18
        .collect()
19
20
}
20

            
21
/// A form of a select over three options followed by a text field.
22
8
fn app_with_select_modal() -> App {
23
8
    let mut select = SelectWidget::new();
24
8
    select.set_options(options());
25
8
    let base = Form::config_set();
26
8
    let text = Field {
27
8
        key: None,
28
8
        label: "note",
29
8
        widget: Widget::Text(Editor::default()),
30
8
    };
31
8
    let form = Form {
32
8
        fields: vec![
33
8
            Field {
34
8
                key: None,
35
8
                label: "account",
36
8
                widget: Widget::Select(select),
37
8
            },
38
8
            text,
39
8
        ],
40
8
        focus: 0,
41
8
        kind: FormKind::ConfigSet,
42
8
        ..base
43
8
    };
44
8
    let mut a = crate::test_support::app();
45
8
    a.open_dialog_on_front(Modal::Form(form));
46
8
    a
47
8
}
48

            
49
22
fn form_of(app: &App) -> &Form {
50
22
    match app.overlays.top() {
51
22
        Some(Modal::Form(form)) => form,
52
        other => panic!("not a form: {other:?}"),
53
    }
54
22
}
55

            
56
15
fn select_of(app: &mut App) -> &mut SelectWidget {
57
15
    match app.overlays.form_mut().map(|f| &mut f.fields[0].widget) {
58
15
        Some(Widget::Select(select)) => select,
59
        _ => panic!("no select"),
60
    }
61
15
}
62

            
63
/// A submit with no console connected hands the form back and says so.
64
5
fn submitted(app: &App) -> bool {
65
5
    app.status == "console not connected" && matches!(app.overlays.top(), Some(Modal::Form(_)))
66
5
}
67

            
68
#[test]
69
1
fn an_open_select_takes_up_and_down_for_its_options() {
70
1
    let mut a = app_with_select_modal();
71
1
    press(&mut a, key(KeyCode::Enter));
72
1
    press(&mut a, key(KeyCode::Down));
73
1
    assert_eq!(select_of(&mut a).focused_in_filtered(), 1);
74
1
    press(&mut a, key(KeyCode::Up));
75
1
    assert_eq!(select_of(&mut a).focused_in_filtered(), 0);
76
1
}
77

            
78
#[test]
79
1
fn enter_opens_a_closed_select_and_the_next_enter_closes_it_without_submitting() {
80
1
    let mut a = app_with_select_modal();
81
1
    assert!(!select_of(&mut a).open);
82
1
    press(&mut a, key(KeyCode::Enter));
83
1
    assert!(select_of(&mut a).open);
84
1
    press(&mut a, key(KeyCode::Enter));
85
1
    assert!(!select_of(&mut a).open);
86
1
    assert!(!submitted(&a));
87
1
}
88

            
89
#[test]
90
1
fn space_opens_a_closed_select_and_esc_closes_the_list_not_the_form() {
91
1
    let mut a = app_with_select_modal();
92
1
    press(&mut a, key(KeyCode::Char(' ')));
93
1
    assert!(select_of(&mut a).open);
94
1
    press(&mut a, key(KeyCode::Esc));
95
1
    assert!(!select_of(&mut a).open);
96
1
    assert!(matches!(a.overlays.top(), Some(Modal::Form(_))));
97
1
}
98

            
99
#[test]
100
1
fn esc_restores_the_value_an_open_select_had_and_enter_keeps_the_new_one() {
101
1
    let mut a = app_with_select_modal();
102
1
    press(&mut a, key(KeyCode::Char(' ')));
103
1
    press(&mut a, key(KeyCode::Down));
104
1
    assert_eq!(select_of(&mut a).value(), "beta");
105
1
    press(&mut a, key(KeyCode::Esc));
106
1
    assert_eq!(select_of(&mut a).value(), "alpha");
107
1
    press(&mut a, key(KeyCode::Char(' ')));
108
1
    press(&mut a, key(KeyCode::Down));
109
1
    press(&mut a, key(KeyCode::Enter));
110
1
    assert_eq!(select_of(&mut a).value(), "beta");
111
1
    assert!(!select_of(&mut a).open);
112
1
}
113

            
114
#[test]
115
1
fn tab_out_of_an_open_select_keeps_the_highlighted_option() {
116
1
    let mut a = app_with_select_modal();
117
1
    press(&mut a, key(KeyCode::Enter));
118
1
    press(&mut a, key(KeyCode::Down));
119
1
    press(&mut a, key(KeyCode::Tab));
120
1
    assert_eq!(select_of(&mut a).value(), "beta");
121
1
    assert!(!select_of(&mut a).open);
122
1
}
123

            
124
#[test]
125
1
fn esc_restores_a_splits_select_cell_and_enter_keeps_the_new_option() {
126
1
    let mut a = app_with_splits_modal(COL_FROM);
127
1
    press(&mut a, key(KeyCode::Enter));
128
1
    press(&mut a, key(KeyCode::Down));
129
1
    assert_eq!(splits_select_index(&mut a), 1);
130
1
    press(&mut a, key(KeyCode::Esc));
131
1
    assert_eq!(splits_select_index(&mut a), 0);
132
1
    assert!(matches!(a.overlays.top(), Some(Modal::Form(_))));
133
1
    press(&mut a, key(KeyCode::Enter));
134
1
    press(&mut a, key(KeyCode::Down));
135
1
    press(&mut a, key(KeyCode::Enter));
136
1
    assert_eq!(splits_select_index(&mut a), 1);
137
1
}
138

            
139
#[test]
140
1
fn select_focused_enter_confirms_a_type_ahead_query() {
141
1
    let mut a = app_with_select_modal();
142
1
    select_of(&mut a).filter_push('g');
143
1
    press(&mut a, key(KeyCode::Enter));
144
1
    assert_eq!(select_of(&mut a).query(), "");
145
1
    assert!(!submitted(&a));
146
1
}
147

            
148
#[test]
149
1
fn text_focused_enter_still_submits() {
150
1
    let mut a = crate::test_support::app();
151
1
    a.open_dialog_on_front(Modal::Form(Form::config_set()));
152
1
    press(&mut a, key(KeyCode::Enter));
153
1
    assert!(submitted(&a));
154
1
}
155

            
156
#[test]
157
1
fn select_focused_tab_still_cycles_outer_fields() {
158
1
    let mut a = app_with_select_modal();
159
1
    press(&mut a, key(KeyCode::Tab));
160
1
    assert_eq!(form_of(&a).focus, 1);
161
1
}
162

            
163
/// An app whose topmost modal is a transaction-create form with the Splits
164
/// field focused on column `col` of a two-row editor whose selects hold options.
165
12
fn app_with_splits_modal(col: usize) -> App {
166
12
    let mut a = crate::test_support::app();
167
12
    let mut form = Form::transaction_create();
168
12
    form.focus = 3;
169
12
    if let Widget::Splits(ref mut sw) = form.fields[3].widget {
170
12
        sw.set_account_options(options());
171
12
        sw.add_row();
172
12
        sw.row_focus = 0;
173
12
        sw.col_focus = col;
174
    }
175
12
    a.open_dialog_on_front(Modal::Form(form));
176
12
    a
177
12
}
178

            
179
16
fn splits_of(app: &App) -> &SplitsWidget {
180
16
    match &form_of(app).fields[3].widget {
181
16
        Widget::Splits(sw) => sw,
182
        _ => panic!("no splits"),
183
    }
184
16
}
185

            
186
7
fn splits_select_index(app: &mut App) -> usize {
187
7
    let Some(form) = app.overlays.form_mut() else {
188
        panic!("no form");
189
    };
190
7
    match &mut form.fields[3].widget {
191
7
        Widget::Splits(sw) => match sw.focused_subwidget_mut() {
192
7
            Some(crate::widgets::FocusedSubWidget::Select(select)) => select.focused_in_filtered(),
193
            _ => panic!("no select cell"),
194
        },
195
        _ => panic!("no splits"),
196
    }
197
7
}
198

            
199
#[test]
200
1
fn an_open_splits_select_cell_takes_up_and_down_for_its_options() {
201
1
    let mut a = app_with_splits_modal(COL_FROM);
202
1
    press(&mut a, key(KeyCode::Enter));
203
1
    press(&mut a, key(KeyCode::Down));
204
1
    assert_eq!(splits_select_index(&mut a), 1);
205
1
    press(&mut a, key(KeyCode::Up));
206
1
    assert_eq!(splits_select_index(&mut a), 0);
207
1
    assert_eq!(splits_of(&a).row_focus, 0);
208
1
}
209

            
210
#[test]
211
1
fn splits_amount_col_up_and_down_move_rows() {
212
1
    let mut a = app_with_splits_modal(COL_VALUE);
213
1
    press(&mut a, key(KeyCode::Down));
214
1
    assert_eq!(splits_of(&a).row_focus, 1);
215
1
    press(&mut a, key(KeyCode::Up));
216
1
    assert_eq!(splits_of(&a).row_focus, 0);
217
1
}
218

            
219
#[test]
220
1
fn splits_tab_navigates_columns_mid_row() {
221
1
    let mut a = app_with_splits_modal(COL_TO);
222
1
    press(&mut a, key(KeyCode::Tab));
223
1
    assert_eq!(splits_of(&a).col_focus, COL_TO + 1);
224
1
    press(&mut a, key(KeyCode::BackTab));
225
1
    assert_eq!(splits_of(&a).col_focus, COL_TO);
226
1
    assert_eq!(form_of(&a).focus, 3);
227
1
}
228

            
229
#[test]
230
1
fn splits_backtab_at_first_cell_yields_to_outer_form() {
231
1
    let mut a = app_with_splits_modal(COL_FROM);
232
1
    press(&mut a, key(KeyCode::BackTab));
233
1
    assert_eq!(form_of(&a).focus, 2);
234
1
    let mut b = app_with_splits_modal(COL_FROM);
235
1
    press(&mut b, key(KeyCode::Tab));
236
1
    assert_eq!(splits_of(&b).col_focus, COL_FROM + 1);
237
1
    assert_eq!(form_of(&b).focus, 3);
238
1
}
239

            
240
#[test]
241
1
fn splits_tab_at_last_cell_yields_to_outer_form() {
242
1
    let mut a = app_with_splits_modal(COL_TO_AMOUNT);
243
    {
244
1
        let Some(form) = a.overlays.form_mut() else {
245
            panic!("no form");
246
        };
247
1
        if let Widget::Splits(ref mut sw) = form.fields[3].widget {
248
1
            sw.row_focus = 1;
249
        }
250
    }
251
1
    press(&mut a, key(KeyCode::Tab));
252
1
    assert_eq!(form_of(&a).focused_button(), Some(0));
253
1
    let mut b = app_with_splits_modal(COL_TO_AMOUNT);
254
1
    press(&mut b, key(KeyCode::BackTab));
255
1
    assert_eq!(splits_of(&b).col_focus, COL_TO_AMOUNT - 1);
256
1
    assert_eq!(form_of(&b).focus, 3);
257
1
}
258

            
259
#[test]
260
1
fn enter_on_a_splits_amount_cell_submits_the_form() {
261
1
    let mut a = app_with_splits_modal(COL_VALUE);
262
1
    press(&mut a, key(KeyCode::Enter));
263
1
    assert!(submitted(&a));
264
1
}
265

            
266
#[test]
267
1
fn enter_on_a_splits_select_cell_opens_it_and_down_then_moves_a_row() {
268
1
    let mut a = app_with_splits_modal(COL_FROM);
269
1
    press(&mut a, key(KeyCode::Down));
270
1
    assert_eq!(splits_of(&a).row_focus, 1);
271
1
    press(&mut a, key(KeyCode::Enter));
272
1
    assert!(!submitted(&a));
273
1
    press(&mut a, key(KeyCode::Down));
274
1
    assert_eq!(splits_of(&a).row_focus, 1);
275
1
    assert_eq!(splits_select_index(&mut a), 1);
276
1
}
277

            
278
#[test]
279
1
fn splits_add_and_remove_row_shortcuts() {
280
1
    let mut a = app_with_splits_modal(COL_VALUE);
281
1
    assert_eq!(splits_of(&a).rows().len(), 2);
282
1
    press(&mut a, key(KeyCode::Char('+')));
283
1
    assert_eq!(splits_of(&a).rows().len(), 3);
284
1
    press(&mut a, key(KeyCode::Char('-')));
285
1
    assert_eq!(splits_of(&a).rows().len(), 2);
286
1
    press(&mut a, ctrl(KeyCode::Char('n')));
287
1
    assert_eq!(splits_of(&a).rows().len(), 3);
288
1
}
289

            
290
#[test]
291
1
fn a_splits_select_cell_opens_on_space_and_filters_only_while_open() {
292
1
    let mut a = app_with_splits_modal(COL_FROM);
293
1
    press(&mut a, key(KeyCode::Char('b')));
294
1
    assert_eq!(splits_select_index(&mut a), 0);
295
1
    press(&mut a, key(KeyCode::Char(' ')));
296
1
    assert!(splits_of(&a).focused_select().is_some_and(|s| s.open));
297
1
    press(&mut a, key(KeyCode::Char('b')));
298
1
    assert_eq!(
299
1
        splits_of(&a).focused_select().map(SelectWidget::query),
300
1
        Some("b")
301
    );
302
1
    let grid = splits_of(&a).grid(true, None, None);
303
1
    let cell = &crate::test_support::cells(&grid, 0)[2];
304
1
    assert_eq!(
305
1
        (cell.text.as_str(), cell.focused, cell.caret),
306
1
        ("b", true, true)
307
    );
308
1
}
309

            
310
#[test]
311
1
fn an_open_select_shows_pick_and_close_list_on_the_status_bar() {
312
1
    let mut a = app_with_select_modal();
313
2
    let hints = |app: &App| crate::test_support::hint_pairs(app);
314
1
    assert!(hints(&a).contains(&("Enter".to_string(), "Open".to_string())));
315
1
    press(&mut a, key(KeyCode::Enter));
316
1
    let open = hints(&a);
317
1
    assert!(
318
1
        open.contains(&("Enter".to_string(), "Pick".to_string())),
319
        "{open:?}"
320
    );
321
1
    assert!(
322
1
        open.contains(&("Esc".to_string(), "Close list".to_string())),
323
        "{open:?}"
324
    );
325
1
}