1
//! The vim preset drives list views and read-only dialogs with its motions.
2

            
3
use super::tests::{key, press, type_text};
4
use super::{in_modal, in_view};
5
use crate::app::App;
6
use crate::event::Intent;
7
use crate::modal::{Draft, Modal};
8
use crate::view::Tab;
9
use ratatui::crossterm::event::KeyCode;
10

            
11
4
fn scripts(app: App) -> App {
12
4
    let mut app = app;
13
4
    app.switch_tab(Tab::Scripts);
14
4
    app.scripts_mut().on_reply(
15
4
        r#"(:id 1 :value "((:id \"s1\" :name \"one\" :enabled t :has-source t) (:id \"s2\" :name \"two\" :enabled nil :has-source t) (:id \"s3\" :name \"three\" :enabled nil :has-source t))")"#,
16
4
    );
17
4
    app
18
4
}
19

            
20
2
fn accounts(app: App) -> App {
21
2
    let mut app = app;
22
2
    app.switch_tab(Tab::Accounts);
23
6
    let row = |id: &str| format!(r#"(:account :id "{id}" :name "{id}" :parent "")"#);
24
6
    let rows: Vec<String> = ["u1", "u2", "u3"].iter().map(|id| row(id)).collect();
25
2
    app.accounts_mut()
26
2
        .on_reply(&format!("(:id 1 :value ({}))", rows.join(" ")));
27
2
    app
28
2
}
29

            
30
#[test]
31
1
fn j_and_k_step_the_rows_of_every_list_kind() {
32
1
    let mut select = scripts(crate::test_support::vim_app());
33
1
    press(&mut select, key(KeyCode::Char('j')));
34
1
    assert_eq!(select.scripts().selected, 1);
35
1
    press(&mut select, key(KeyCode::Char('k')));
36
1
    assert_eq!(select.scripts().selected, 0);
37
1
    let mut list = accounts(crate::test_support::vim_app());
38
1
    press(&mut list, key(KeyCode::Char('j')));
39
1
    assert_eq!(list.accounts().selected, 1);
40
1
}
41

            
42
#[test]
43
1
fn capital_g_selects_the_last_row_and_gg_the_first() {
44
1
    let mut select = scripts(crate::test_support::vim_app());
45
1
    press(&mut select, key(KeyCode::Char('G')));
46
1
    assert_eq!(select.scripts().selected, 2);
47
1
    type_text(&mut select, "gg");
48
1
    assert_eq!(select.scripts().selected, 0);
49
1
    let mut list = accounts(crate::test_support::vim_app());
50
1
    press(&mut list, key(KeyCode::Char('G')));
51
1
    assert_eq!(list.accounts().selected, 2);
52
1
    type_text(&mut list, "gg");
53
1
    assert_eq!(list.accounts().selected, 0);
54
1
}
55

            
56
#[test]
57
1
fn control_d_and_u_page_the_lists_and_scroll_the_read_only_dialogs() {
58
1
    let mut app = crate::test_support::vim_app();
59
1
    app.switch_tab(Tab::Reports);
60
1
    assert_eq!(in_view(&app).bound("C-d"), Some(Intent::ScrollPageDown));
61
1
    app.switch_tab(Tab::Transactions);
62
1
    let list = in_view(&app);
63
1
    assert_eq!(list.bound("C-d"), Some(Intent::ListPageNext));
64
1
    assert_eq!(list.bound("C-u"), Some(Intent::ListPagePrev));
65
1
    let help = Modal::Draft(Draft::new(Vec::new()));
66
1
    let keys = in_modal(&app, &help);
67
1
    assert_eq!(keys.bound("C-d"), Some(Intent::ScrollPageDown));
68
1
    assert_eq!(keys.bound("C-u"), Some(Intent::ScrollPageUp));
69
1
}
70

            
71
#[test]
72
1
fn emacs_and_cua_do_not_get_the_vim_motions() {
73
1
    for app in [crate::test_support::app(), crate::test_support::emacs_app()] {
74
2
        let mut app = scripts(app);
75
2
        press(&mut app, key(KeyCode::Char('G')));
76
2
        assert_eq!(app.scripts().selected, 0);
77
2
        assert_eq!(in_view(&app).bound("C-d"), None);
78
    }
79
1
}
80

            
81
#[test]
82
1
fn paging_keys_show_and_act_only_on_transactions() {
83
1
    let mut app = crate::test_support::vim_app();
84
1
    app.switch_tab(Tab::Accounts);
85
1
    let shown = |app: &App| {
86
1
        crate::test_support::hint_pairs(app)
87
1
            .iter()
88
8
            .any(|(key, _)| key == "C-d")
89
1
    };
90
1
    assert_eq!(in_view(&app).bound("C-d"), None);
91
1
    assert!(
92
1
        !crate::keymap::help_lines(&app)
93
1
            .iter()
94
49
            .any(|l| l.contains("C-d"))
95
    );
96
1
    assert!(!shown(&app));
97
1
    app.switch_tab(Tab::Transactions);
98
1
    assert_eq!(in_view(&app).bound("C-d"), Some(Intent::ListPageNext));
99
1
}
100

            
101
#[test]
102
1
fn home_and_end_scroll_a_read_only_dialog_to_its_ends_in_every_preset() {
103
1
    for (preset, app) in [
104
1
        ("cua", crate::test_support::app()),
105
1
        ("emacs", crate::test_support::emacs_app()),
106
1
        ("vim", crate::test_support::vim_app()),
107
1
    ] {
108
3
        let help = Modal::Draft(Draft::new(Vec::new()));
109
3
        let keys = in_modal(&app, &help);
110
3
        assert_eq!(keys.bound("Home"), Some(Intent::ScrollTop), "{preset}");
111
3
        assert_eq!(keys.bound("End"), Some(Intent::ScrollEnd), "{preset}");
112
    }
113
1
}
114

            
115
#[test]
116
1
fn vim_binds_gg_and_capital_g_to_the_top_and_end_of_a_dialog() {
117
1
    let app = crate::test_support::vim_app();
118
1
    let help = Modal::Draft(Draft::new(Vec::new()));
119
1
    let keys = in_modal(&app, &help);
120
1
    assert_eq!(keys.bound("g g"), Some(Intent::ScrollTop));
121
1
    assert_eq!(keys.bound("G"), Some(Intent::ScrollEnd));
122
1
}
123

            
124
#[test]
125
1
fn home_and_end_move_a_draft_dialog_through_the_key_handler() {
126
1
    let mut app = crate::test_support::app();
127
60
    let lines = (0..60).map(|n| format!("line{n}")).collect();
128
1
    app.overlays.push(Modal::Draft(Draft::new(lines)));
129
1
    crate::test_support::draw_once(&app, 80, 24);
130
2
    let top = |app: &App| match app.overlays.top() {
131
2
        Some(Modal::Draft(help)) => help.scroll.top(),
132
        _ => usize::MAX,
133
2
    };
134
1
    press(&mut app, key(KeyCode::End));
135
1
    let end = top(&app);
136
1
    assert!(end > 0);
137
1
    press(&mut app, key(KeyCode::Home));
138
1
    assert_eq!(top(&app), 0);
139
1
}