1
//! The Window menu lists the open windows, and M-n selects one in cua.
2
use super::*;
3
use crate::app::MenuId;
4
use crate::tabs::list::ListTab;
5
use crate::tabs::reports::{ReportKind, ReportsTab};
6
use crate::test_support::{alt, key, press};
7
use forms::FormValues;
8
use ratatui::crossterm::event::KeyCode;
9

            
10
/// The commands of the Window menu before the list.
11
const COMMANDS: usize = 8;
12

            
13
18
fn open_window_menu(app: &mut App) {
14
18
    app.open_menu_of(MenuId::Window);
15
18
}
16

            
17
4
fn listed(app: &App) -> Vec<String> {
18
4
    let entries = app.menu_entries();
19
4
    entries[COMMANDS..]
20
4
        .iter()
21
41
        .map(|entry| entry.label.clone())
22
        .collect()
23
4
}
24

            
25
1
fn choose(app: &mut App, label: &str) {
26
1
    let at = app
27
1
        .menu_entries()
28
1
        .iter()
29
10
        .position(|entry| entry.label == label);
30
1
    app.select_menu_item(at.unwrap_or(usize::MAX));
31
1
    press(app, key(KeyCode::Enter));
32
1
}
33

            
34
#[test]
35
1
fn the_list_has_every_window_by_number_and_the_unnumbered_last() {
36
1
    let mut app = crate::test_support::app();
37
1
    open_window_menu(&mut app);
38
1
    let expected = [
39
1
        "1 Accounts",
40
1
        "2 Transactions",
41
1
        "3 Commodities",
42
1
        "4 Balance",
43
1
        "5 Config",
44
1
        "6 SSH keys",
45
1
        "7 Tags",
46
1
        "8 Templates",
47
1
        "9 Console",
48
1
        "Scripts",
49
1
    ];
50
1
    assert_eq!(listed(&app), expected);
51
1
    app.raise_window(app.window_of(ViewId::Tags));
52
1
    assert_eq!(listed(&app), expected);
53
1
}
54

            
55
#[test]
56
1
fn a_later_window_of_a_view_keeps_its_ordinal_when_others_close() {
57
1
    let mut app = crate::test_support::app();
58
1
    app.close_window(app.window_of(ViewId::Console));
59
1
    let nine = app.open_extra(ViewId::Tags);
60
1
    let none = app.open_extra(ViewId::Tags);
61
1
    open_window_menu(&mut app);
62
1
    let labels = listed(&app);
63
1
    assert_eq!(
64
1
        labels[6..],
65
1
        ["7 Tags", "8 Templates", "9 Tags (2)", "Scripts", "Tags (3)"]
66
    );
67
1
    app.close_window(app.window_of_number(7));
68
1
    let labels = listed(&app);
69
1
    assert_eq!(
70
1
        labels[6..],
71
1
        ["8 Templates", "9 Tags (2)", "Scripts", "Tags (3)"]
72
    );
73
1
    assert_eq!(app.window_suffix(nine), " (2)");
74
1
    assert_eq!(app.window_suffix(none), " (3)");
75
1
    let fresh = app.open_extra(ViewId::Tags);
76
1
    assert_eq!(app.window_suffix(fresh), " (4)");
77
1
}
78

            
79
#[test]
80
1
fn the_commands_keep_their_mnemonics_and_the_list_has_none() {
81
1
    let mut app = crate::test_support::app();
82
1
    open_window_menu(&mut app);
83
1
    let entries = app.menu_entries();
84
1
    assert!(
85
1
        entries[..COMMANDS]
86
1
            .iter()
87
8
            .all(|entry| tvui::hotkey(&entry.label).is_some())
88
    );
89
1
    assert!(
90
1
        entries[COMMANDS..]
91
1
            .iter()
92
10
            .all(|entry| tvui::hotkey(&entry.label).is_none())
93
    );
94
1
}
95

            
96
#[tokio::test]
97
1
async fn choosing_an_entry_raises_and_loads_that_window() {
98
1
    let mut app = crate::test_support::app();
99
1
    app.attach_echo();
100
1
    open_window_menu(&mut app);
101
1
    choose(&mut app, "2 Transactions");
102
1
    let window = app.window_of(ViewId::Transactions);
103
1
    assert_eq!(app.front_window(), Some(window));
104
1
    assert!(app.menu.is_none());
105
1
    assert!(app.win_by(window).is_some_and(|win| win.view.loading()));
106
1
}
107

            
108
#[test]
109
1
fn the_list_entries_show_no_chord_column_in_any_preset() {
110
1
    for mut app in [crate::test_support::app(), crate::test_support::emacs_app()] {
111
2
        open_window_menu(&mut app);
112
2
        let entries = app.menu_entries();
113
20
        assert!(entries[COMMANDS..].iter().all(|entry| entry.key.is_empty()));
114
    }
115
1
}
116

            
117
#[test]
118
1
fn the_selection_stays_on_the_last_entry_when_the_list_shrinks() {
119
1
    let mut app = crate::test_support::app();
120
1
    open_window_menu(&mut app);
121
1
    app.menu_last();
122
1
    let last = app.menu_entries().len() - 1;
123
1
    assert_eq!(app.menu_item(), Some(last));
124
1
    app.close_window(app.window_of(ViewId::Console));
125
1
    assert_eq!(app.menu_item(), Some(last - 1));
126
1
    app.menu_next();
127
1
    assert_eq!(app.menu_item(), Some(0));
128
1
    app.menu_prev();
129
1
    assert_eq!(app.menu_item(), Some(last - 1));
130
1
}
131

            
132
#[test]
133
1
fn the_selection_keeps_its_place_when_the_list_grows() {
134
1
    let mut app = crate::test_support::app();
135
1
    open_window_menu(&mut app);
136
1
    app.menu_last();
137
1
    let before = app.menu_item();
138
1
    app.open_extra(ViewId::Tags);
139
1
    assert_eq!(app.menu_item(), before);
140
1
    app.menu_next();
141
1
    assert_eq!(app.menu_item(), before.map(|at| at + 1));
142
1
}
143

            
144
#[test]
145
1
fn alt_digit_selects_that_window_in_cua() {
146
1
    let mut app = crate::test_support::app();
147
1
    press(&mut app, alt(KeyCode::Char('4')));
148
1
    assert_eq!(app.front_kind(), Some(ViewId::Reports));
149
1
    press(&mut app, alt(KeyCode::Char('2')));
150
1
    assert_eq!(app.front_kind(), Some(ViewId::Transactions));
151
1
}
152

            
153
#[test]
154
1
fn alt_digit_without_that_window_says_so() {
155
1
    let mut app = crate::test_support::app();
156
1
    app.close_window(app.window_of(ViewId::Console));
157
1
    let front = app.front_window();
158
1
    press(&mut app, alt(KeyCode::Char('9')));
159
1
    assert_eq!(app.front_window(), front);
160
1
    assert_eq!(app.status, "no window 9");
161
1
}
162

            
163
#[test]
164
1
fn emacs_and_vim_leave_alt_digit_unbound() {
165
1
    for mut app in [
166
1
        crate::test_support::emacs_app(),
167
1
        crate::test_support::vim_app(),
168
1
    ] {
169
2
        let front = app.front_window();
170
2
        press(&mut app, alt(KeyCode::Char('3')));
171
2
        assert_eq!(app.front_window(), front);
172
    }
173
1
}
174

            
175
10
fn frame_top(app: &App) -> String {
176
10
    let (buf, _) = crate::test_support::draw_frame(app, 120, 24);
177
1200
    (0..buf.area.width).map(|x| buf[(x, 1)].symbol()).collect()
178
10
}
179

            
180
10
fn entry_title(app: &mut App, id: WindowId) -> String {
181
10
    open_window_menu(app);
182
10
    let label = app
183
10
        .menu_entries()
184
10
        .into_iter()
185
190
        .find(|entry| entry.intent == crate::event::Intent::RaiseWindow(id))
186
10
        .map(|entry| entry.label);
187
10
    let label = label.unwrap_or_default();
188
10
    label
189
10
        .trim_start_matches(|c: char| c.is_ascii_digit() || c == ' ')
190
        .to_string()
191
10
}
192

            
193
#[test]
194
1
fn the_list_entry_is_the_frame_title_of_its_window() {
195
1
    for kind in ViewId::ALL {
196
10
        let mut app = crate::test_support::app();
197
10
        let id = app.open_extra(kind);
198
10
        let title = entry_title(&mut app, id);
199
10
        app.close_menu();
200
10
        assert!(title.ends_with(" (2)"), "{title}");
201
10
        assert!(
202
10
            frame_top(&app).contains(&title),
203
            "{title}: {}",
204
            frame_top(&app)
205
        );
206
    }
207
1
}
208

            
209
#[test]
210
1
fn a_window_titled_by_its_filter_needs_no_ordinal() {
211
1
    let mut app = crate::test_support::app();
212
1
    let first = app.window_of(ViewId::Transactions);
213
1
    let second = app.open_extra(ViewId::Transactions);
214
1
    assert_eq!(app.window_suffix(second), " (2)");
215
1
    if let Some(list) = app.win_mut::<ListTab>(second) {
216
1
        list.set_filter(&FormValues::default(), "acc-1".to_string());
217
    }
218
1
    assert_eq!(app.window_suffix(second), "");
219
1
    assert_eq!(app.window_suffix(first), "");
220
1
}
221

            
222
#[test]
223
1
fn a_report_window_is_titled_by_its_report() {
224
1
    let mut app = crate::test_support::app();
225
1
    let first = app.window_of(ViewId::Reports);
226
1
    let second = app.open_extra(ViewId::Reports);
227
1
    if let Some(tab) = app.win_mut::<ReportsTab>(second) {
228
1
        tab.set_loading(1, ReportKind::Activity, &FormValues::default());
229
    }
230
2
    let title = |app: &App, id| {
231
2
        app.win_by(id)
232
2
            .map(|win| win.view.title(&app.window_suffix(id)))
233
            .unwrap_or_default()
234
2
    };
235
1
    assert_eq!(title(&app, first), "Balance");
236
1
    assert_eq!(title(&app, second), "Activity");
237
1
}
238

            
239
#[test]
240
1
fn the_frame_title_marks_the_tail_the_list_title_does_not() {
241
1
    let mut app = crate::test_support::app();
242
1
    let second = app.open_extra(ViewId::Transactions);
243
1
    let suffix = app.window_suffix(second);
244
1
    let Some(view) = app.active_view() else {
245
        panic!("a front window");
246
    };
247
1
    assert_eq!(view.title(&suffix), "Transactions (2)");
248
1
    assert_eq!(
249
1
        view.framed_title(&suffix),
250
1
        tvui::joined_title("Transactions", " (2)")
251
    );
252
1
    assert_ne!(view.framed_title(&suffix), view.title(&suffix));
253
1
}