1
//! F6, S-F6 and F5: the stack of windows rotates and zooming uncovers what is behind.
2

            
3
use super::report_chart::{
4
    call_for, chart_requests, echoed, env, png_wire, rendered, show, stripes, text, with_table,
5
};
6
use crate::app::App;
7
use crate::event::{Intent, apply};
8
use crate::test_support::{ctrl, key, press};
9
use crate::view::ViewId;
10
use crate::window::WindowId;
11
use ratatui::crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
12
use rpc::chart::ChartBackend;
13

            
14
8
fn shift(code: KeyCode) -> KeyEvent {
15
8
    KeyEvent::new(code, KeyModifiers::SHIFT)
16
8
}
17

            
18
7
fn loads_of(app: &App, window: WindowId) -> usize {
19
7
    app.pending_routes
20
7
        .values()
21
9
        .filter(|route| route.target == Some(window))
22
        .count()
23
7
}
24

            
25
3
fn is_zoomed(app: &App, window: WindowId) -> bool {
26
3
    app.desktop
27
3
        .iter_back_to_front()
28
30
        .any(|slot| slot.id == window && slot.zoomed)
29
3
}
30

            
31
#[test]
32
1
fn f6_from_the_start_walks_the_views_in_their_order() {
33
1
    let mut app = crate::test_support::app();
34
1
    let mut seen = Vec::new();
35
1
    for _ in 0..ViewId::ALL.len() {
36
10
        press(&mut app, key(KeyCode::F(6)));
37
10
        seen.extend(app.front_kind());
38
    }
39
1
    assert_eq!(
40
1
        seen,
41
1
        [
42
1
            ViewId::Transactions,
43
1
            ViewId::Commodities,
44
1
            ViewId::Reports,
45
1
            ViewId::Config,
46
1
            ViewId::SshKeys,
47
1
            ViewId::Tags,
48
1
            ViewId::Templates,
49
1
            ViewId::Console,
50
1
            ViewId::Scripts,
51
1
            ViewId::Accounts,
52
1
        ]
53
    );
54
1
}
55

            
56
#[test]
57
1
fn f6_raises_the_rearmost_window_and_the_stack_rotates() {
58
1
    let mut app = crate::test_support::app();
59
1
    let rearmost = app.desktop.rearmost();
60
1
    let front = app.front_window();
61
1
    press(&mut app, key(KeyCode::F(6)));
62
1
    assert_eq!(app.front_window(), rearmost);
63
1
    assert_eq!(
64
1
        app.desktop.iter_back_to_front().last().map(|w| w.id),
65
1
        rearmost
66
    );
67
1
    assert_eq!(app.desktop.iter_back_to_front().nth(8).map(|w| w.id), front);
68
1
}
69

            
70
#[test]
71
1
fn shift_f6_sends_the_front_window_to_the_back() {
72
1
    let mut app = crate::test_support::app();
73
1
    let accounts = app.window_of(ViewId::Accounts);
74
1
    press(&mut app, shift(KeyCode::F(6)));
75
1
    assert_eq!(app.desktop.rearmost(), Some(accounts));
76
1
    assert_eq!(app.front_kind(), Some(ViewId::Scripts));
77
1
    press(&mut app, shift(KeyCode::F(6)));
78
1
    assert_eq!(app.front_kind(), Some(ViewId::Console));
79
1
}
80

            
81
#[test]
82
1
fn tab_and_shift_tab_no_longer_rotate_the_windows() {
83
1
    let mut app = crate::test_support::app();
84
10
    let order: Vec<WindowId> = app.desktop.iter_back_to_front().map(|w| w.id).collect();
85
1
    press(&mut app, key(KeyCode::Tab));
86
1
    press(&mut app, shift(KeyCode::BackTab));
87
10
    let after: Vec<WindowId> = app.desktop.iter_back_to_front().map(|w| w.id).collect();
88
1
    assert_eq!(after, order);
89
1
    assert_eq!(app.front_kind(), Some(ViewId::Accounts));
90
1
}
91

            
92
#[test]
93
1
fn next_and_previous_with_a_single_window_stay_on_it() {
94
1
    let mut app = crate::test_support::app();
95
1
    for kind in ViewId::ALL.into_iter().skip(1) {
96
9
        app.close_window(app.window_of(kind));
97
    }
98
1
    press(&mut app, key(KeyCode::F(6)));
99
1
    press(&mut app, shift(KeyCode::F(6)));
100
1
    assert_eq!(app.front_kind(), Some(ViewId::Accounts));
101
1
}
102

            
103
#[test]
104
1
fn next_and_previous_do_nothing_on_the_empty_desktop() {
105
1
    let mut app = crate::test_support::app();
106
11
    while app.front_window().is_some() {
107
10
        app.close_front_window();
108
    }
109
1
    press(&mut app, key(KeyCode::F(6)));
110
1
    press(&mut app, shift(KeyCode::F(6)));
111
1
    press(&mut app, key(KeyCode::F(5)));
112
1
    assert_eq!(app.front_window(), None);
113
1
}
114

            
115
#[test]
116
1
fn both_leave_the_console_prompt() {
117
1
    for code in [key(KeyCode::F(6)), shift(KeyCode::F(6))] {
118
2
        let mut app = crate::test_support::app();
119
2
        app.focus_console();
120
2
        press(&mut app, code);
121
2
        assert!(!app.console_input_focused(), "{code:?}");
122
2
        assert_ne!(app.front_kind(), Some(ViewId::Console), "{code:?}");
123
    }
124
1
}
125

            
126
#[tokio::test]
127
1
async fn next_loads_the_window_it_raises() {
128
1
    let mut app = crate::test_support::app();
129
1
    app.attach_echo();
130
1
    let transactions = app.window_of(ViewId::Transactions);
131
1
    assert_eq!(loads_of(&app, transactions), 0);
132
1
    press(&mut app, key(KeyCode::F(6)));
133
1
    assert_eq!(loads_of(&app, transactions), 1);
134
1
}
135

            
136
#[tokio::test]
137
1
async fn previous_loads_the_window_that_comes_to_the_front() {
138
1
    let mut app = crate::test_support::app();
139
1
    app.attach_echo();
140
1
    app.switch_tab(ViewId::Scripts);
141
1
    let accounts = app.window_of(ViewId::Accounts);
142
1
    assert_eq!(loads_of(&app, accounts), 0);
143
1
    press(&mut app, shift(KeyCode::F(6)));
144
1
    assert_eq!(app.front_window(), Some(accounts));
145
1
    assert_eq!(loads_of(&app, accounts), 1);
146
1
}
147

            
148
#[test]
149
1
fn f5_zooms_and_restores_the_front_window() {
150
1
    let mut app = crate::test_support::app();
151
1
    let accounts = app.window_of(ViewId::Accounts);
152
1
    assert!(is_zoomed(&app, accounts));
153
1
    press(&mut app, key(KeyCode::F(5)));
154
1
    assert!(!is_zoomed(&app, accounts));
155
1
    press(&mut app, key(KeyCode::F(5)));
156
1
    assert!(is_zoomed(&app, accounts));
157
1
}
158

            
159
#[tokio::test]
160
1
async fn unzooming_loads_the_window_it_uncovers() {
161
1
    let mut app = crate::test_support::app();
162
1
    app.attach_echo();
163
1
    app.sync_terminal(env(80, 24, ChartBackend::Halfblocks));
164
1
    app.switch_tab(ViewId::Commodities);
165
1
    let accounts = app.window_of(ViewId::Accounts);
166
1
    assert_eq!(loads_of(&app, accounts), 0);
167
1
    press(&mut app, key(KeyCode::F(5)));
168
1
    assert_eq!(loads_of(&app, accounts), 1);
169
1
}
170

            
171
#[tokio::test]
172
1
async fn zooming_again_does_not_load_a_window_twice() {
173
1
    let mut app = crate::test_support::app();
174
1
    app.attach_echo();
175
1
    app.sync_terminal(env(80, 24, ChartBackend::Halfblocks));
176
1
    app.switch_tab(ViewId::Commodities);
177
1
    let accounts = app.window_of(ViewId::Accounts);
178
1
    for _ in 0..4 {
179
4
        press(&mut app, key(KeyCode::F(5)));
180
1
    }
181
1
    assert_eq!(loads_of(&app, accounts), 1);
182
1
}
183

            
184
#[tokio::test]
185
1
async fn zooming_a_reports_window_fetches_its_chart_at_the_new_size() {
186
1
    let (mut app, sent) = with_table(40, 20, ChartBackend::Halfblocks);
187
1
    show(&mut app);
188
1
    assert_eq!(chart_requests(&app).len(), 1);
189
1
    press(&mut app, key(KeyCode::F(5)));
190
1
    assert_eq!(chart_requests(&app).len(), 2);
191
    // 40x20 screen: the 60x16 start rect at (3, 3) is cut to the desktop's width, 40x16 cells in
192
    // full (it hangs 3 columns off the right), 38x14 inside the border.
193
1
    assert!(echoed(&mut app, &sent).await.contains(&call_for(380, 280)));
194
1
}
195

            
196
#[test]
197
1
fn control_r_refreshes_and_plain_r_does_not() {
198
1
    let mut app = crate::test_support::app();
199
1
    app.switch_tab(ViewId::Accounts);
200
1
    app.set_status("old");
201
1
    press(&mut app, ctrl(KeyCode::Char('r')));
202
1
    assert_ne!(app.status, "old");
203
1
    app.set_status("old");
204
1
    press(&mut app, key(KeyCode::Char('r')));
205
1
    assert_eq!(app.status, "old");
206
1
}
207

            
208
/// A Reports window showing a kitty chart that has been drawn once; returns its image id.
209
9
pub(super) fn drawn_kitty_chart() -> (App, u32) {
210
9
    let (mut app, _) = with_table(40, 20, ChartBackend::Kitty);
211
9
    show(&mut app);
212
9
    answer_chart(&mut app);
213
9
    let image = crate::tabs::reports::chart::kitty_image_id(app.window_of(ViewId::Reports));
214
9
    app.take_outbound();
215
9
    assert!(text(&rendered(&app, 40, 20)).contains("a=T"));
216
9
    (app, image)
217
9
}
218

            
219
16
pub(super) fn answer_chart(app: &mut App) {
220
16
    let Some(id) = chart_requests(app).last().copied() else {
221
        panic!("a chart request");
222
    };
223
16
    app.route_reply(id, &png_wire(id, stripes(380, 320)));
224
16
}
225

            
226
#[tokio::test]
227
1
async fn zooming_and_unzooming_delete_the_kitty_image_before_it_is_drawn_again() {
228
1
    let (mut app, image) = drawn_kitty_chart();
229
1
    let deleted = vec![tvui::kitty_delete(image)];
230
1
    for _ in 0..2 {
231
2
        press(&mut app, key(KeyCode::F(5)));
232
2
        assert_eq!(app.take_outbound(), deleted);
233
2
        answer_chart(&mut app);
234
2
        assert!(text(&rendered(&app, 40, 20)).contains("a=T"));
235
2
        app.take_outbound();
236
1
    }
237
1
}
238

            
239
#[tokio::test]
240
1
async fn tiling_and_cascading_delete_the_kitty_image_and_fetch_the_chart_again() {
241
1
    for intent in [Intent::Tile, Intent::Cascade] {
242
2
        let (mut app, image) = drawn_kitty_chart();
243
2
        let before = chart_requests(&app).len();
244
2
        apply(&mut app, intent);
245
2
        assert_eq!(
246
2
            app.take_outbound(),
247
2
            vec![tvui::kitty_delete(image)],
248
1
            "{intent:?}"
249
1
        );
250
2
        assert_eq!(chart_requests(&app).len(), before + 1, "{intent:?}");
251
1
    }
252
1
}
253

            
254
#[tokio::test]
255
1
async fn zooming_again_fetches_the_chart_again() {
256
1
    let (mut app, _) = with_table(40, 20, ChartBackend::Halfblocks);
257
1
    show(&mut app);
258
1
    for expected in [2, 3] {
259
2
        press(&mut app, key(KeyCode::F(5)));
260
2
        assert_eq!(chart_requests(&app).len(), expected);
261
1
    }
262
1
}
263

            
264
#[tokio::test]
265
1
async fn previous_with_a_single_window_keeps_its_kitty_image() {
266
1
    let (mut app, _) = drawn_kitty_chart();
267
1
    for kind in ViewId::ALL {
268
10
        if kind != ViewId::Reports {
269
9
            app.close_window(app.window_of(kind));
270
1
        }
271
    }
272
1
    app.take_outbound();
273
1
    press(&mut app, shift(KeyCode::F(6)));
274
1
    assert!(app.take_outbound().is_empty());
275
1
    assert!(!text(&rendered(&app, 40, 20)).contains("a=T"));
276
1
}