1
//! Tests for key-event routing.
2

            
3
mod account_merge;
4
mod completion;
5
mod config_edit;
6
mod confirm;
7
mod convert;
8
mod create;
9
mod palette_console;
10
mod pending;
11
mod quit;
12
mod select;
13
mod split_tag_side;
14
mod transaction;
15
mod transaction_create;
16

            
17
use super::*;
18
use crate::view::ViewId;
19
use sqlx::types::Uuid;
20

            
21
#[test]
22
1
fn quit_now_intent_sets_quit_flag() {
23
1
    let mut app = crate::test_support::app();
24
1
    apply(&mut app, Intent::QuitNow);
25
1
    assert!(app.should_quit);
26
1
}
27

            
28
#[test]
29
1
fn select_tab_intent_jumps_directly() {
30
1
    let mut app = crate::test_support::app();
31
1
    apply(&mut app, Intent::SelectTab(Tab::Reports));
32
1
    assert_eq!(app.front_kind(), Some(Tab::Reports));
33
1
}
34

            
35
#[test]
36
1
fn open_command_line_activates_and_accepts_input() {
37
1
    let mut app = crate::test_support::app();
38
1
    apply(&mut app, Intent::OpenCommandLine);
39
1
    assert!(app.cmdline.active);
40
1
    apply(&mut app, Intent::InsertChar('v'));
41
1
    apply(&mut app, Intent::InsertChar('x'));
42
1
    assert_eq!(app.cmdline.editor.buffer(), "vx");
43
1
}
44

            
45
#[test]
46
1
fn submit_command_line_records_buffer_and_closes() {
47
1
    let mut app = crate::test_support::app();
48
1
    apply(&mut app, Intent::OpenCommandLine);
49
1
    for c in "version".chars() {
50
7
        apply(&mut app, Intent::InsertChar(c));
51
    }
52
1
    apply(&mut app, Intent::Submit);
53
1
    assert!(!app.cmdline.active, "command line must close after submit");
54
    // version is an eval leaf — it submits the form and switches to Console.
55
1
    assert_eq!(app.front_kind(), Some(Tab::Console));
56
1
    assert!(
57
1
        app.console()
58
1
            .scrollback
59
1
            .iter()
60
1
            .any(|l| l.contains("(get-version)")),
61
        "form should appear in console scrollback"
62
    );
63
1
}
64

            
65
#[test]
66
1
fn submit_command_line_with_unknown_path_surfaces_error() {
67
1
    let mut app = crate::test_support::app();
68
1
    apply(&mut app, Intent::OpenCommandLine);
69
1
    for c in "bogus-cmd".chars() {
70
9
        apply(&mut app, Intent::InsertChar(c));
71
    }
72
1
    apply(&mut app, Intent::Submit);
73
1
    assert!(app.status.contains("unknown"));
74
1
}
75

            
76
#[test]
77
1
fn report_modal_accepts_typing_and_focus_cycle() {
78
    // `reports activity` with no dates opens the params modal; the user must be
79
    // able to type into it, Tab to cycle focus, and have the buffers captured.
80
1
    let mut app = crate::test_support::app();
81
1
    apply(&mut app, Intent::OpenCommandLine);
82
1
    for c in "reports activity".chars() {
83
16
        apply(&mut app, Intent::InsertChar(c));
84
    }
85
1
    apply(&mut app, Intent::Submit);
86
1
    assert!(
87
1
        matches!(app.overlays.top(), Some(Modal::Form(_))),
88
        "missing dates should open the report-params modal"
89
    );
90
1
    apply(&mut app, Intent::NextField); // Target → From
91
1
    for c in "2024-01-01".chars() {
92
10
        apply(&mut app, Intent::InsertChar(c));
93
    }
94
1
    apply(&mut app, Intent::NextField); // To
95
1
    for c in "2024-12-31".chars() {
96
10
        apply(&mut app, Intent::InsertChar(c));
97
    }
98
1
    match app.overlays.top() {
99
        Some(Modal::Form(f)) => {
100
1
            assert_eq!(f.fields[1].widget.value(), "2024-01-01");
101
1
            assert_eq!(f.fields[2].widget.value(), "2024-12-31");
102
        }
103
        other => panic!("expected Form modal, got {other:?}"),
104
    }
105
1
}
106

            
107
#[test]
108
1
fn submit_config_set_opens_form_modal() {
109
1
    let mut app = crate::test_support::app();
110
1
    apply(&mut app, Intent::OpenCommandLine);
111
1
    for c in "config set key=locale value=en".chars() {
112
30
        apply(&mut app, Intent::InsertChar(c));
113
    }
114
1
    apply(&mut app, Intent::Submit);
115
1
    assert!(!app.overlays.is_empty());
116
1
    match app.overlays.top() {
117
        Some(Modal::Form(f)) => {
118
1
            assert_eq!(f.fields[0].widget.value(), "locale");
119
1
            assert_eq!(f.fields[1].widget.value(), "en");
120
        }
121
        other => panic!("expected Form modal, got {other:?}"),
122
    }
123
1
}
124

            
125
#[test]
126
1
fn close_topmost_pops_modal_before_touching_tabs() {
127
1
    let mut app = crate::test_support::app();
128
1
    crate::test_support::open_overlay(&mut app);
129
1
    apply(&mut app, Intent::Quit);
130
1
    assert!(
131
1
        !app.should_quit,
132
        "quit should be swallowed by the modal layer"
133
    );
134
1
    apply(&mut app, Intent::CloseTopmost);
135
1
    assert!(app.overlays.is_empty());
136
1
    apply(&mut app, Intent::Quit);
137
1
    apply(&mut app, Intent::ConfirmYes);
138
1
    assert!(app.should_quit);
139
1
}
140

            
141
#[test]
142
1
fn console_focus_sets_flag_and_blur_clears_it() {
143
1
    let mut app = crate::test_support::app();
144
1
    app.switch_tab(Tab::Console);
145
1
    apply(&mut app, Intent::ConsoleFocus);
146
1
    assert!(app.console_input_focused());
147
1
    apply(&mut app, Intent::ConsoleBlur);
148
1
    assert!(!app.console_input_focused());
149
1
}
150

            
151
#[test]
152
1
fn console_editing_intents_mutate_input_editor() {
153
1
    let mut app = crate::test_support::app();
154
1
    app.focus_console();
155
1
    apply(&mut app, Intent::InsertChar('('));
156
1
    apply(&mut app, Intent::InsertChar('a'));
157
1
    assert_eq!(app.console().input.buffer(), "(a");
158
1
    apply(&mut app, Intent::DeleteBackward);
159
1
    assert_eq!(app.console().input.buffer(), "(");
160
1
}
161

            
162
#[test]
163
1
fn console_submit_incomplete_form_keeps_buffering() {
164
1
    let mut app = crate::test_support::app();
165
1
    app.focus_console();
166
1
    for c in "(list".chars() {
167
5
        apply(&mut app, Intent::InsertChar(c));
168
    }
169
1
    apply(&mut app, Intent::ConsoleSubmit);
170
1
    assert_eq!(app.console().pending, "(list");
171
1
    assert!(app.console().input.buffer().is_empty());
172
1
}
173

            
174
#[tokio::test]
175
1
async fn console_submit_routes_complete_form_to_echo_eval() {
176
1
    let mut app = crate::test_support::app();
177
1
    let sent = app.attach_echo();
178
1
    app.focus_console();
179
1
    for c in "(+ 1 2)".chars() {
180
7
        apply(&mut app, Intent::InsertChar(c));
181
    }
182
1
    apply(&mut app, Intent::ConsoleSubmit);
183
1
    tokio::task::yield_now().await;
184
1
    app.drain_eval();
185
1
    assert!(app.console().scrollback.iter().any(|l| l == "> (+ 1 2)"));
186
1
    assert!(
187
1
        sent.frames()
188
1
            .iter()
189
1
            .any(|l| l.contains("(:id 1 :stream 1 :form (+ 1 2))"))
190
1
    );
191
1
}
192

            
193
#[test]
194
1
fn console_history_keys_navigate_prior_submissions() {
195
1
    let mut app = crate::test_support::app();
196
1
    app.focus_console();
197
1
    for form in ["(a)", "(b)"] {
198
2
        for c in form.chars() {
199
6
            apply(&mut app, Intent::InsertChar(c));
200
        }
201
2
        apply(&mut app, Intent::ConsoleSubmit);
202
    }
203
1
    apply(&mut app, Intent::ConsoleHistoryPrev);
204
1
    assert_eq!(app.console().input.buffer(), "(b)");
205
1
    apply(&mut app, Intent::ConsoleHistoryPrev);
206
1
    assert_eq!(app.console().input.buffer(), "(a)");
207
1
    apply(&mut app, Intent::ConsoleHistoryNext);
208
1
    assert_eq!(app.console().input.buffer(), "(b)");
209
1
}
210

            
211
#[test]
212
1
fn console_interrupt_without_eval_does_not_panic() {
213
1
    let mut app = crate::test_support::app();
214
1
    app.focus_console();
215
1
    apply(&mut app, Intent::ConsoleInterrupt);
216
1
}
217

            
218
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
219
1
async fn console_interrupt_intent_reaches_attached_eval() {
220
    use crate::tabs::nms_eval::ConsoleEval;
221
    use rpc::{ScriptCtx, ScriptLimits};
222
    use std::time::Duration;
223
    use tokio::time::sleep;
224

            
225
1
    let ctx = ScriptCtx::new(Uuid::nil()).with_limits(ScriptLimits {
226
1
        fuel: u64::MAX,
227
1
        ..ScriptLimits::default()
228
1
    });
229
1
    let eval = ConsoleEval::connect(&tokio::runtime::Handle::current(), ctx, rpc::Session::new)
230
1
        .expect("spawn");
231
1
    let mut app = crate::test_support::app();
232
1
    app.attach_console(eval);
233
1
    assert!(
234
1
        app.await_ui_config_read().await,
235
        "the theme read was never answered"
236
    );
237
1
    app.focus_console();
238
1
    for c in "(do ((i 0 (+ i 1))) ((>= i 1073741823) i))".chars() {
239
42
        apply(&mut app, Intent::InsertChar(c));
240
    }
241
1
    apply(&mut app, Intent::ConsoleSubmit);
242
1
    sleep(Duration::from_millis(40)).await;
243
1
    apply(&mut app, Intent::ConsoleInterrupt);
244

            
245
1
    let mut interrupted = false;
246
1
    for _ in 0..200 {
247
2
        app.drain_eval();
248
2
        if app
249
2
            .console()
250
2
            .scrollback
251
2
            .iter()
252
3
            .any(|l| l.contains("[error] interrupted:"))
253
1
        {
254
1
            interrupted = true;
255
1
            break;
256
1
        }
257
1
        sleep(Duration::from_millis(20)).await;
258
1
    }
259
1
    assert!(interrupted, "interrupt did not reach the eval");
260
1
}
261

            
262
#[tokio::test]
263
1
async fn palette_version_submits_form_and_switches_console() {
264
1
    let mut app = crate::test_support::app();
265
1
    app.attach_echo();
266
1
    apply(&mut app, Intent::OpenCommandLine);
267
1
    for c in "version".chars() {
268
7
        apply(&mut app, Intent::InsertChar(c));
269
    }
270
1
    apply(&mut app, Intent::Submit);
271
1
    assert_eq!(
272
1
        app.front_kind(),
273
1
        Some(Tab::Console),
274
        "should switch to Console tab"
275
    );
276
1
    assert!(
277
1
        app.console()
278
1
            .scrollback
279
1
            .iter()
280
1
            .any(|l| l.contains("(get-version)")),
281
1
        "form should appear in scrollback; scrollback: {:?}",
282
1
        app.console().scrollback
283
1
    );
284
1
}
285

            
286
#[test]
287
1
fn palette_config_set_opens_modal_not_console() {
288
1
    let mut app = crate::test_support::app();
289
1
    apply(&mut app, Intent::OpenCommandLine);
290
1
    for c in "config set".chars() {
291
10
        apply(&mut app, Intent::InsertChar(c));
292
    }
293
1
    apply(&mut app, Intent::Submit);
294
1
    assert!(!app.overlays.is_empty(), "config set should open a modal");
295
1
    assert_eq!(
296
1
        app.front_kind(),
297
1
        Some(Tab::Config),
298
        "the dialog's window comes to the front"
299
    );
300
1
}
301

            
302
#[test]
303
1
fn console_cycle_pane_toggles_focus() {
304
1
    let mut app = crate::test_support::app();
305
1
    app.focus_console();
306
    use crate::pane::PaneId;
307
1
    assert_eq!(app.console().panes.focused(), PaneId::Prompt);
308
1
    apply(&mut app, Intent::ConsoleCyclePane);
309
1
    assert_eq!(app.console().panes.focused(), PaneId::Scrollback);
310
1
    apply(&mut app, Intent::ConsoleCyclePane);
311
1
    assert_eq!(app.console().panes.focused(), PaneId::Prompt);
312
1
}
313

            
314
#[test]
315
1
fn console_refocus_resets_pane_to_prompt() {
316
    use crate::pane::PaneId;
317
1
    let mut app = crate::test_support::app();
318
1
    app.focus_console();
319
1
    apply(&mut app, Intent::ConsoleCyclePane);
320
1
    assert_eq!(app.console().panes.focused(), PaneId::Scrollback);
321
1
    apply(&mut app, Intent::ConsoleBlur);
322
1
    apply(&mut app, Intent::ConsoleFocus);
323
1
    assert_eq!(app.console().panes.focused(), PaneId::Prompt);
324
1
}
325

            
326
#[test]
327
1
fn scroll_line_up_down_adjusts_offset() {
328
1
    let mut app = crate::test_support::app();
329
1
    app.focus_console();
330
1
    for i in 0..20 {
331
20
        app.console_mut().push_scrollback(format!("line {i}"));
332
    }
333
1
    apply(&mut app, Intent::ScrollLineUp);
334
1
    assert_eq!(app.console().scroll, 1);
335
1
    apply(&mut app, Intent::ScrollLineUp);
336
1
    assert_eq!(app.console().scroll, 2);
337
1
    apply(&mut app, Intent::ScrollLineDown);
338
1
    assert_eq!(app.console().scroll, 1);
339
1
    apply(&mut app, Intent::ScrollLineDown);
340
1
    assert_eq!(app.console().scroll, 0);
341
1
}
342

            
343
#[test]
344
1
fn scroll_page_up_down_moves_by_ten() {
345
1
    let mut app = crate::test_support::app();
346
1
    app.focus_console();
347
1
    for i in 0..30 {
348
30
        app.console_mut().push_scrollback(format!("line {i}"));
349
    }
350
1
    apply(&mut app, Intent::ScrollPageUp);
351
1
    assert_eq!(app.console().scroll, 10);
352
1
    apply(&mut app, Intent::ScrollPageDown);
353
1
    assert_eq!(app.console().scroll, 0);
354
1
}
355

            
356
#[test]
357
1
fn scroll_up_clamped_at_scrollback_len() {
358
1
    let mut app = crate::test_support::app();
359
1
    app.focus_console();
360
1
    for i in 0..5 {
361
5
        app.console_mut().push_scrollback(format!("line {i}"));
362
    }
363
1
    apply(&mut app, Intent::ScrollPageUp);
364
1
    assert_eq!(app.console().scroll, 5);
365
1
}
366

            
367
#[test]
368
1
fn submit_console_form_resets_scroll() {
369
1
    let mut app = crate::test_support::app();
370
1
    for i in 0..20 {
371
20
        app.console_mut().push_scrollback(format!("line {i}"));
372
    }
373
1
    app.console_mut().scroll_up(10);
374
1
    assert_eq!(app.console().scroll, 10);
375
1
    let console = app.window_of(ViewId::Console);
376
1
    app.submit_console_form(console, "(+ 1 2)".to_string());
377
1
    assert_eq!(app.console().scroll, 0);
378
1
}
379

            
380
#[test]
381
1
fn palette_account_balance_without_an_account_surfaces_error_status() {
382
1
    let mut app = crate::test_support::app();
383
1
    apply(&mut app, Intent::OpenCommandLine);
384
1
    for c in "account balance".chars() {
385
15
        apply(&mut app, Intent::InsertChar(c));
386
    }
387
1
    apply(&mut app, Intent::Submit);
388
1
    assert!(
389
1
        app.status.contains("command error"),
390
        "a missing account should surface error in status; got: {}",
391
        app.status
392
    );
393
1
}
394

            
395
#[test]
396
1
fn amount_widget_in_form_keeps_every_typed_char() {
397
    use crate::form::{Field, Form, FormKind};
398
    use crate::widgets::{AmountWidget, Widget};
399

            
400
1
    let mut app = crate::test_support::app();
401
1
    let form = Form {
402
1
        fields: vec![Field {
403
1
            key: None,
404
1
            label: "amount",
405
1
            widget: Widget::Amount(AmountWidget::default()),
406
1
        }],
407
1
        focus: 0,
408
1
        kind: FormKind::ConfigSet,
409
1
        ..Form::config_set()
410
1
    };
411
1
    app.open_dialog_on_front(Modal::Form(form));
412
1
    apply(&mut app, Intent::InsertChar('a'));
413
1
    apply(&mut app, Intent::InsertChar('5'));
414
1
    apply(&mut app, Intent::InsertChar(','));
415
1
    match app.overlays.top() {
416
1
        Some(Modal::Form(f)) => assert_eq!(f.fields[0].widget.value(), "a5,"),
417
        _ => panic!("expected form modal"),
418
    }
419
1
}