1
//! The emacs preset: its shape, the scope chain cuts and the exact chords.
2

            
3
use super::tests::{alt, ctrl, focused_text, key, press, seen, type_text};
4
use super::{BASE, EMACS, Keys, PRESETS, VIM, VimState};
5
use crate::app::App;
6
use crate::form::Form;
7
use crate::modal::{ConfirmAction, Modal};
8
use crate::view::Tab;
9
use ratatui::crossterm::event::KeyCode;
10

            
11
1
fn chords_of_the_preset() -> Vec<String> {
12
1
    let lines = [BASE, EMACS, VIM]
13
1
        .into_iter()
14
3
        .flat_map(|(_, text)| text.lines())
15
288
        .filter(|line| !line.starts_with('#'));
16
271
    let entries = lines.flat_map(|line| line.split(';'));
17
328
    let bindings = entries.filter_map(|entry| entry.split_once(':')?.1.split_once('='));
18
286
    let chords = bindings.map(|(chords, _)| chords.split_whitespace().map(str::to_string));
19
1
    chords.flatten().collect()
20
1
}
21

            
22
2
fn bound(text: &str) -> Keys {
23
2
    match Keys::layered("test", VimState::Off, &[("test.keys", text)]) {
24
2
        Ok(keys) => keys,
25
        Err(err) => panic!("should parse: {err}"),
26
    }
27
2
}
28

            
29
8
fn form_app(form: Form) -> App {
30
8
    let mut app = crate::test_support::emacs_app();
31
8
    app.open_dialog_on_front(Modal::Form(form));
32
8
    app
33
8
}
34

            
35
4
fn note_form_app(note: &str) -> App {
36
4
    let mut values = forms::FormValues::default();
37
4
    values.set("note", note);
38
4
    let mut form = Form::transaction_edit(&values);
39
4
    form.focus = form
40
4
        .fields
41
4
        .iter()
42
8
        .position(|field| field.key == Some("note"))
43
4
        .unwrap_or(0);
44
4
    if let Some(note) = form.fields[form.focus].widget.as_text_mut() {
45
4
        note.replace_buffer(note.buffer().to_string());
46
    }
47
4
    form_app(form)
48
4
}
49

            
50
3
fn submitted(app: &App) -> bool {
51
3
    app.status == "console not connected" && matches!(app.overlays.top(), Some(Modal::Form(_)))
52
3
}
53

            
54
#[test]
55
1
fn every_preset_parses_and_names_itself() {
56
1
    for preset in PRESETS {
57
3
        match Keys::preset(preset.name) {
58
3
            Ok(keys) => assert_eq!(keys.name, preset.name),
59
            Err(err) => panic!("{err}"),
60
        }
61
    }
62
1
    assert_eq!(
63
1
        Keys::preset("emacs").map(|keys| keys.indicator()),
64
1
        Ok("emacs")
65
    );
66
1
    assert_eq!(
67
1
        Keys::preset("vim").map(|keys| keys.indicator()),
68
1
        Ok("vim:insert")
69
    );
70
1
}
71

            
72
#[test]
73
1
fn a_layer_error_names_its_file_and_line() {
74
1
    let layers = [
75
1
        ("a.keys", "global-view: q = quit"),
76
1
        ("b.keys", "\nbad-scope: q = quit"),
77
1
    ];
78
1
    let err = Keys::layered("test", VimState::Off, &layers).err();
79
1
    assert_eq!(
80
1
        err.as_deref(),
81
1
        Some("b.keys: line 2: unknown scope `bad-scope`")
82
    );
83
1
}
84

            
85
#[test]
86
1
fn a_later_layer_overrides_an_earlier_one() {
87
1
    let layers = [
88
1
        ("a.keys", "global-view: q = quit"),
89
1
        ("b.keys", "global-view: q = open-help"),
90
1
    ];
91
1
    let mut app = crate::test_support::emacs_app();
92
1
    app.keys = match Keys::layered("test", VimState::Off, &layers) {
93
1
        Ok(keys) => keys,
94
        Err(err) => panic!("{err}"),
95
    };
96
1
    press(&mut app, key(KeyCode::Char('q')));
97
1
    assert!(!app.should_quit);
98
1
    assert!(crate::test_support::help_open(&app));
99
1
}
100

            
101
#[test]
102
1
fn no_binding_uses_a_chord_that_never_arrives_over_ssh() {
103
1
    let chords = chords_of_the_preset();
104
1
    assert!(chords.len() > 100, "{}", chords.len());
105
1
    for chord in ["C-h", "C-i", "C-j", "C-m"] {
106
1156
        assert!(!chords.iter().any(|bound| bound == chord), "{chord}");
107
    }
108
1
}
109

            
110
#[test]
111
1
fn global_view_keys_are_typed_in_the_console_prompt() {
112
1
    let mut app = crate::test_support::emacs_app();
113
1
    app.focus_console();
114
1
    type_text(&mut app, "q:?1 ");
115
1
    assert_eq!(app.console().input.buffer(), "q:?1 ");
116
1
    assert!(!app.should_quit);
117
1
    assert!(!app.cmdline.active);
118
1
    assert!(app.overlays.is_empty());
119
1
}
120

            
121
#[test]
122
1
fn global_view_keys_are_typed_in_the_command_line() {
123
1
    let mut app = crate::test_support::emacs_app();
124
1
    app.open_command_line();
125
1
    type_text(&mut app, "q?9");
126
1
    assert_eq!(app.cmdline.editor.buffer(), "q?9");
127
1
    assert!(!app.should_quit);
128
1
}
129

            
130
#[test]
131
1
fn digits_are_typed_in_a_form_text_field() {
132
1
    let mut app = form_app(Form::config_set());
133
1
    press(&mut app, key(KeyCode::Char('1')));
134
1
    assert_eq!(focused_text(&app), "1");
135
1
    assert!(app.overlays.top().is_some());
136
1
}
137

            
138
#[test]
139
1
fn a_chain_change_clears_the_half_typed_binding() {
140
1
    let mut app = crate::test_support::emacs_app();
141
1
    app.keys = bound("global-view: g g = quit\nmodal-view: g = close");
142
1
    press(&mut app, key(KeyCode::Char('g')));
143
1
    crate::test_support::open_draft(&mut app);
144
1
    press(&mut app, key(KeyCode::Char('g')));
145
1
    assert!(app.overlays.is_empty());
146
1
    assert!(!app.should_quit);
147
1
}
148

            
149
#[test]
150
1
fn a_half_typed_binding_completes_within_one_chain() {
151
1
    let mut app = crate::test_support::emacs_app();
152
1
    app.keys = bound("global-view: g g = quit");
153
1
    press(&mut app, key(KeyCode::Char('g')));
154
1
    assert!(!app.should_quit);
155
1
    press(&mut app, key(KeyCode::Char('g')));
156
1
    assert!(matches!(
157
1
        app.overlays.top(),
158
        Some(Modal::Confirm {
159
            action: ConfirmAction::Quit,
160
            ..
161
        })
162
    ));
163
1
}
164

            
165
#[test]
166
1
fn close_or_quit_is_the_plain_q_only() {
167
1
    for event in [ctrl(KeyCode::Char('q')), alt(KeyCode::Char('q'))] {
168
2
        let mut app = crate::test_support::emacs_app();
169
2
        let front = app.front_window();
170
2
        press(&mut app, event);
171
2
        assert_eq!(app.front_window(), front, "{event:?}");
172
    }
173
1
    let mut app = crate::test_support::emacs_app();
174
1
    let front = app.front_window();
175
1
    press(&mut app, key(KeyCode::Char('q')));
176
1
    assert_ne!(app.front_window(), front);
177
1
}
178

            
179
#[test]
180
1
fn a_modified_row_key_does_nothing() {
181
1
    let mut app = crate::test_support::emacs_app();
182
1
    app.switch_tab(Tab::Tags);
183
1
    press(&mut app, ctrl(KeyCode::Char('d')));
184
1
    assert!(app.overlays.is_empty());
185
1
}
186

            
187
#[test]
188
1
fn plus_is_typed_text_outside_the_splits() {
189
1
    let mut app = form_app(Form::config_set());
190
1
    press(&mut app, key(KeyCode::Char('+')));
191
1
    assert_eq!(focused_text(&app), "+");
192
1
}
193

            
194
#[test]
195
1
fn alt_enter_and_ctrl_s_submit_a_text_area_and_enter_breaks_the_line() {
196
1
    for event in [alt(KeyCode::Enter), ctrl(KeyCode::Char('s'))] {
197
2
        let mut app = note_form_app("a");
198
2
        press(&mut app, event);
199
2
        assert!(submitted(&app), "{event:?}");
200
    }
201
1
    let mut app = note_form_app("a");
202
1
    press(&mut app, key(KeyCode::Enter));
203
1
    assert!(!submitted(&app));
204
1
    assert_eq!(focused_text(&app), "a\n");
205
1
}
206

            
207
#[test]
208
1
fn the_cursor_moves_in_a_text_area_and_in_a_single_line_field() {
209
1
    let mut area = note_form_app("ab");
210
1
    press(&mut area, key(KeyCode::Left));
211
1
    press(&mut area, key(KeyCode::Char('x')));
212
1
    assert_eq!(focused_text(&area), "axb");
213
1
    let mut field = form_app(Form::config_set());
214
1
    type_text(&mut field, "ab");
215
1
    press(&mut field, key(KeyCode::Left));
216
1
    press(&mut field, key(KeyCode::Char('x')));
217
1
    assert_eq!(focused_text(&field), "axb");
218
1
}
219

            
220
#[test]
221
1
fn tab_moves_between_form_fields() {
222
1
    let mut app = form_app(Form::config_set());
223
1
    press(&mut app, key(KeyCode::Tab));
224
1
    assert_eq!(app.overlays.form_mut().map(|form| form.focus), Some(1));
225
1
}
226

            
227
#[test]
228
1
fn c_creates_a_row_on_the_tabs_that_have_one() {
229
1
    for tab in [
230
1
        Tab::Transactions,
231
1
        Tab::Commodities,
232
1
        Tab::Config,
233
1
        Tab::SshKeys,
234
1
        Tab::Tags,
235
1
        Tab::Templates,
236
1
        Tab::Scripts,
237
1
    ] {
238
7
        let mut app = crate::test_support::emacs_app();
239
7
        app.switch_tab(tab);
240
7
        press(&mut app, key(KeyCode::Char('c')));
241
7
        assert!(
242
7
            matches!(app.overlays.top(), Some(Modal::Form(_))),
243
            "{tab:?}"
244
        );
245
    }
246
1
}
247

            
248
#[test]
249
1
fn c_and_d_do_nothing_on_the_tabs_without_that_row_action() {
250
1
    let quiet = [
251
1
        ('d', Tab::Accounts),
252
1
        ('d', Tab::Commodities),
253
1
        ('d', Tab::Config),
254
1
    ];
255
1
    for (letter, tab) in quiet {
256
3
        let mut app = crate::test_support::emacs_app();
257
3
        app.switch_tab(tab);
258
3
        let before = seen(&app);
259
3
        press(&mut app, key(KeyCode::Char(letter)));
260
3
        assert_eq!(seen(&app), before, "{letter} on {tab:?}");
261
    }
262
1
}
263

            
264
#[test]
265
1
fn preset_names_list_every_preset_that_builds() {
266
1
    let names: Vec<_> = Keys::preset_names().collect();
267
1
    assert_eq!(names, ["cua", "emacs", "vim"]);
268
1
    for name in names {
269
3
        assert!(Keys::preset(name).is_ok(), "{name}");
270
    }
271
1
}