1
//! Tests for key handling: each key is pressed and the resulting app state asserted.
2

            
3
use crate::app::App;
4
use crate::form::{Form, FormKind};
5
use crate::keymap::VimState;
6
use crate::modal::Modal;
7
use crate::pane::PaneId;
8
use crate::tabs::fetch::Fetch;
9
use crate::view::Tab;
10
use ratatui::crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
11

            
12
pub(super) use crate::test_support::{ctrl, key, press};
13

            
14
20
pub(super) fn alt(code: KeyCode) -> KeyEvent {
15
20
    KeyEvent::new(code, KeyModifiers::ALT)
16
20
}
17

            
18
84
pub(super) fn type_text(app: &mut App, text: &str) {
19
84
    for c in text.chars() {
20
474
        press(app, key(KeyCode::Char(c)));
21
    }
22
84
}
23

            
24
/// The app-level state a stray key could disturb.
25
#[derive(Debug, PartialEq)]
26
pub(super) struct Seen {
27
    tab: Option<Tab>,
28
    overlay: bool,
29
    cmdline: bool,
30
    console_focused: bool,
31
    quit: bool,
32
    status: String,
33
}
34

            
35
20
pub(super) fn seen(app: &App) -> Seen {
36
20
    Seen {
37
20
        tab: app.front_kind(),
38
20
        overlay: !app.overlays.is_empty(),
39
20
        cmdline: app.cmdline.active,
40
20
        console_focused: app.console_input_focused(),
41
20
        quit: app.should_quit,
42
20
        status: app.status.clone(),
43
20
    }
44
20
}
45

            
46
20
pub(super) fn focused_text(app: &App) -> String {
47
20
    match app.overlays.top() {
48
20
        Some(Modal::Form(form)) => form.fields[form.focus].widget.value().to_string(),
49
        _ => String::new(),
50
    }
51
20
}
52

            
53
3
fn is_form(app: &App, kind: FormKind) -> bool {
54
3
    matches!(app.overlays.top(), Some(Modal::Form(form)) if form.kind == kind)
55
3
}
56

            
57
4
fn config_form_app() -> App {
58
4
    let mut app = crate::test_support::app();
59
4
    app.open_dialog_on_front(Modal::Form(Form::config_set()));
60
4
    app
61
4
}
62

            
63
6
fn draft_app() -> App {
64
6
    let mut app = crate::test_support::app();
65
6
    crate::test_support::open_draft(&mut app);
66
6
    app
67
6
}
68

            
69
#[test]
70
1
fn c_on_the_config_tab_opens_the_add_key_form() {
71
1
    let mut config = crate::test_support::app();
72
1
    config.switch_tab(Tab::Config);
73
1
    press(&mut config, key(KeyCode::Char('c')));
74
1
    assert!(is_form(&config, FormKind::ConfigSet));
75
1
}
76

            
77
#[test]
78
1
fn a_draft_closes_on_q_and_esc() {
79
1
    for code in [KeyCode::Char('q'), KeyCode::Esc] {
80
2
        let mut help = draft_app();
81
2
        press(&mut help, key(code));
82
2
        assert!(help.overlays.is_empty(), "{code:?}");
83
    }
84
1
}
85

            
86
#[test]
87
1
fn input_modal_types_q_and_only_esc_closes_it() {
88
1
    let mut input = config_form_app();
89
1
    press(&mut input, key(KeyCode::Char('q')));
90
1
    assert_eq!(focused_text(&input), "q");
91
1
    assert!(is_form(&input, FormKind::ConfigSet));
92
1
    let mut fresh = config_form_app();
93
1
    press(&mut fresh, key(KeyCode::Esc));
94
1
    assert!(fresh.overlays.is_empty());
95
1
}
96

            
97
#[test]
98
1
fn tab_layer_handles_digit_shortcuts() {
99
1
    let mut app = crate::test_support::app();
100
1
    app.switch_tab(Tab::Tags);
101
1
    press(&mut app, key(KeyCode::Char('1')));
102
1
    assert_eq!(app.front_kind(), Some(Tab::Accounts));
103
1
}
104

            
105
#[test]
106
1
fn a_draft_ignores_text_and_navigation_keys() {
107
1
    for code in [KeyCode::Char('1'), KeyCode::Tab, KeyCode::Backspace] {
108
3
        let mut help = draft_app();
109
3
        let before = seen(&help);
110
3
        press(&mut help, key(code));
111
3
        assert!(
112
3
            matches!(help.overlays.top(), Some(Modal::Draft(_))),
113
            "{code:?}"
114
        );
115
3
        assert_eq!(seen(&help), before, "{code:?}");
116
    }
117
1
}
118

            
119
#[test]
120
1
fn enter_submits_and_so_closes_the_draft_overlay() {
121
1
    let mut help = draft_app();
122
1
    press(&mut help, key(KeyCode::Enter));
123
1
    assert!(help.overlays.is_empty());
124
1
}
125

            
126
#[test]
127
1
fn enter_confirms_a_delete_prompt_but_keeps_the_form_on_the_discard_prompt() {
128
    use crate::modal::ConfirmAction;
129
1
    let mut delete = crate::test_support::app();
130
1
    delete.open_dialog_on_front(Modal::Confirm {
131
1
        focus: None,
132
1
        prompt: "Delete?".to_string(),
133
1
        action: ConfirmAction::DeleteTransaction("id".to_string()),
134
1
    });
135
1
    press(&mut delete, key(KeyCode::Enter));
136
1
    assert!(delete.overlays.is_empty());
137
1
    assert_eq!(delete.status, "console not connected");
138
1
    let mut discard = config_form_app();
139
1
    discard.open_dialog_on_front(Modal::Confirm {
140
1
        focus: None,
141
1
        prompt: "Discard?".to_string(),
142
1
        action: ConfirmAction::DiscardForm,
143
1
    });
144
1
    press(&mut discard, key(KeyCode::Enter));
145
1
    assert!(is_form(&discard, FormKind::ConfigSet));
146
1
}
147

            
148
#[test]
149
1
fn cmdline_insert_accepts_ctrl_a_as_home() {
150
1
    let mut a = crate::test_support::emacs_app();
151
1
    a.open_command_line();
152
1
    type_text(&mut a, "ab");
153
1
    press(&mut a, ctrl(KeyCode::Char('a')));
154
1
    type_text(&mut a, "x");
155
1
    assert_eq!(a.cmdline.editor.buffer(), "xab");
156
1
}
157

            
158
#[test]
159
1
fn cmdline_insert_accepts_plain_characters() {
160
1
    let mut a = crate::test_support::app();
161
1
    a.open_command_line();
162
1
    press(&mut a, key(KeyCode::Char('q')));
163
1
    assert_eq!(a.cmdline.editor.buffer(), "q");
164
1
    assert!(a.cmdline.active);
165
1
    assert!(!a.should_quit);
166
1
}
167

            
168
#[test]
169
1
fn cmdline_insert_tab_triggers_completion() {
170
1
    let mut a = crate::test_support::app();
171
1
    a.open_command_line();
172
1
    press(&mut a, key(KeyCode::Tab));
173
1
    assert!(!a.cmdline.completions.is_empty());
174
1
}
175

            
176
#[test]
177
1
fn cmdline_vim_normal_routes_motion_keys() {
178
1
    let mut a = crate::test_support::vim_app();
179
1
    a.open_command_line();
180
1
    type_text(&mut a, "abc");
181
1
    press(&mut a, key(KeyCode::Esc));
182
1
    assert_eq!(a.keys.vim(), VimState::Normal);
183
1
    press(&mut a, key(KeyCode::Char('0')));
184
1
    assert_eq!(a.cmdline.editor.cursor(), 0);
185
1
    press(&mut a, key(KeyCode::Char('$')));
186
1
    let end = a.cmdline.editor.cursor();
187
1
    press(&mut a, key(KeyCode::Char('h')));
188
1
    assert_eq!(a.cmdline.editor.cursor(), end - 1);
189
1
}
190

            
191
#[test]
192
1
fn unknown_key_changes_nothing() {
193
1
    let mut app = crate::test_support::app();
194
1
    let before = seen(&app);
195
1
    press(&mut app, key(KeyCode::F(12)));
196
1
    assert_eq!(seen(&app), before);
197
1
}
198

            
199
#[test]
200
1
fn console_tab_unfocused_focuses_on_i_and_enter() {
201
1
    for code in [KeyCode::Char('i'), KeyCode::Enter] {
202
2
        let mut a = crate::test_support::app();
203
2
        a.switch_tab(Tab::Console);
204
2
        press(&mut a, key(code));
205
2
        assert!(a.console_input_focused(), "{code:?}");
206
2
        assert_eq!(a.console().panes.focused(), PaneId::Prompt);
207
    }
208
1
}
209

            
210
#[test]
211
1
fn console_tab_unfocused_still_navigates() {
212
1
    let digits = [
213
1
        ('1', Tab::Accounts),
214
1
        ('6', Tab::SshKeys),
215
1
        ('7', Tab::Tags),
216
1
        ('8', Tab::Templates),
217
1
        ('9', Tab::Console),
218
1
        ('0', Tab::Scripts),
219
1
    ];
220
1
    for (digit, tab) in digits {
221
6
        let mut a = crate::test_support::app();
222
6
        a.switch_tab(Tab::Console);
223
6
        press(&mut a, key(KeyCode::Char(digit)));
224
6
        assert_eq!(a.front_kind(), Some(tab), "{digit}");
225
    }
226
1
    let mut a = crate::test_support::app();
227
1
    a.switch_tab(Tab::Console);
228
1
    press(&mut a, key(KeyCode::Char(':')));
229
1
    assert!(a.cmdline.active);
230
1
    let mut a = crate::test_support::app();
231
1
    a.switch_tab(Tab::Console);
232
1
    let front = a.front_window();
233
1
    press(&mut a, key(KeyCode::Char('q')));
234
1
    assert_ne!(a.front_window(), front);
235
1
    assert!(!a.should_quit);
236
1
}
237

            
238
#[test]
239
1
fn focus_keys_are_inert_on_other_tabs() {
240
1
    let mut a = crate::test_support::app();
241
1
    a.switch_tab(Tab::SshKeys);
242
1
    let before = seen(&a);
243
1
    press(&mut a, key(KeyCode::Char('i')));
244
1
    press(&mut a, key(KeyCode::Enter));
245
1
    assert_eq!(seen(&a), before);
246
1
}
247

            
248
#[test]
249
1
fn pending_form_swallows_everything_but_ctrl_c() {
250
    use crate::app::PendingForm;
251
2
    let pending = || {
252
2
        let mut app = crate::test_support::app();
253
2
        app.pending_form = Some(PendingForm {
254
2
            request_id: 1,
255
2
            form: Form::commodity_create(),
256
2
        });
257
2
        app
258
2
    };
259
1
    let mut quit = pending();
260
1
    press(&mut quit, ctrl(KeyCode::Char('c')));
261
1
    assert!(quit.should_quit);
262
1
    let mut other = pending();
263
1
    press(&mut other, key(KeyCode::Esc));
264
1
    press(&mut other, key(KeyCode::Char('q')));
265
1
    assert!(!other.should_quit);
266
1
    assert!(other.pending_form.is_some());
267
1
}
268

            
269
#[test]
270
1
fn modified_letters_never_insert_text_into_form_fields() {
271
1
    let mut app = config_form_app();
272
1
    press(&mut app, ctrl(KeyCode::Char('a')));
273
1
    press(&mut app, alt(KeyCode::Char('b')));
274
1
    assert_eq!(focused_text(&app), "");
275
1
    press(&mut app, key(KeyCode::Char('a')));
276
1
    assert_eq!(focused_text(&app), "a");
277
1
}
278

            
279
#[test]
280
1
fn modified_letters_never_insert_text_into_the_command_line() {
281
1
    let mut app = crate::test_support::app();
282
1
    app.open_command_line();
283
1
    press(&mut app, alt(KeyCode::Char('x')));
284
1
    press(&mut app, ctrl(KeyCode::Char('z')));
285
1
    assert_eq!(app.cmdline.editor.buffer(), "");
286
1
}
287

            
288
#[test]
289
1
fn page_keys_page_the_transactions_tab_only() {
290
1
    let page = r#"(:id 1 :value (:ROWS ((:ID "a" :DATE "2026-10-01T00:00:00Z" :DATE-TEXT "2026-10-01 00:00" :NOTE "n" :AMOUNT "5")) :HAS-MORE t :TOTAL 45))"#;
291
3
    let loaded = |app: &App| matches!(app.transactions().state, Fetch::Loaded(_));
292
1
    let mut app = crate::test_support::app();
293
1
    app.transactions_mut().on_reply(page);
294
1
    app.switch_tab(Tab::Accounts);
295
1
    press(&mut app, key(KeyCode::PageDown));
296
1
    assert!(loaded(&app));
297
1
    app.switch_tab(Tab::Transactions);
298
1
    press(&mut app, key(KeyCode::PageDown));
299
1
    assert!(!loaded(&app));
300
1
    app.transactions_mut().on_reply(page);
301
1
    press(&mut app, key(KeyCode::PageUp));
302
1
    assert!(!loaded(&app));
303
1
}
304

            
305
#[test]
306
1
fn reports_tab_routes_scroll_keys() {
307
1
    let mut a = crate::test_support::app();
308
1
    a.switch_tab(Tab::Reports);
309
40
    a.reports_mut().state = Fetch::Loaded((0..40).map(|n| n.to_string()).collect());
310
1
    crate::test_support::draw_once(&a, 80, 24);
311
1
    press(&mut a, key(KeyCode::Char('j')));
312
1
    assert_eq!(a.reports().scroll.top(), 1);
313
1
    press(&mut a, key(KeyCode::Up));
314
1
    assert_eq!(a.reports().scroll.top(), 0);
315
1
    press(&mut a, key(KeyCode::PageDown));
316
1
    let paged = a.reports().scroll.top();
317
1
    assert!(paged > 1);
318
1
    press(&mut a, key(KeyCode::PageUp));
319
1
    assert_eq!(a.reports().scroll.top(), 0);
320
1
}