1
//! Opening and closing windows: what a close settles, and the desktop without windows.
2

            
3
use super::report_chart::{chart_requests, png_wire, rendered, show, stripes, text, with_table};
4
use crate::app::App;
5
use crate::event::{Intent, apply};
6
use crate::focus::{FocusTarget, current_focus};
7
use crate::form::Form;
8
use crate::route::{Route, RouteCtx};
9
use crate::tabs::nms::ConsoleState;
10
use crate::test_support::{alt, ctrl, draw_frame, key, press};
11
use crate::view::ViewId;
12
use crate::window::{View, WindowId};
13
use ratatui::crossterm::event::KeyCode;
14
use rpc::chart::ChartBackend;
15
use std::collections::HashSet;
16

            
17
3
fn window_ids(app: &App) -> HashSet<WindowId> {
18
31
    app.windows.iter().map(|win| win.id).collect()
19
3
}
20

            
21
2
fn desktop_ids(app: &App) -> HashSet<WindowId> {
22
2
    app.desktop
23
2
        .iter_back_to_front()
24
21
        .map(|slot| slot.id)
25
        .collect()
26
2
}
27

            
28
4
fn close_all(app: &mut App) {
29
44
    while app.front_window().is_some() {
30
40
        app.close_front_window();
31
    }
32
4
}
33

            
34
2
fn route_to(app: &mut App, request_id: i64, window: WindowId, ctx: RouteCtx) {
35
2
    app.pending_routes.insert(
36
2
        request_id,
37
2
        Route {
38
2
            target: Some(window),
39
2
            ctx,
40
2
        },
41
2
    );
42
2
}
43

            
44
#[test]
45
1
fn open_and_close_keep_the_windows_and_the_desktop_in_lockstep() {
46
1
    let mut app = crate::test_support::app();
47
1
    let opened = app.open_new_window(View::Console(ConsoleState::new()));
48
1
    assert_eq!(window_ids(&app), desktop_ids(&app));
49
1
    assert_eq!(app.front_window(), Some(opened));
50
1
    app.close_window(opened);
51
1
    assert_eq!(window_ids(&app), desktop_ids(&app));
52
1
    assert!(!window_ids(&app).contains(&opened));
53
1
    let again = app.open_new_window(View::Console(ConsoleState::new()));
54
1
    assert_ne!(again, opened, "ids are never reused");
55
1
}
56

            
57
#[tokio::test]
58
1
async fn closing_the_front_window_fronts_and_loads_the_next_one() {
59
1
    let mut app = crate::test_support::app();
60
1
    app.attach_echo();
61
1
    app.switch_tab(ViewId::Transactions);
62
1
    apply(&mut app, Intent::CloseWindow);
63
1
    assert_eq!(app.window_id(ViewId::Transactions), None);
64
1
    assert_eq!(app.front_kind(), Some(ViewId::Accounts));
65
1
    let front = app.window_of(ViewId::Accounts);
66
1
    let loads = app
67
1
        .pending_routes
68
1
        .values()
69
2
        .filter(|route| route.target == Some(front))
70
        .count();
71
1
    assert_eq!(loads, 1);
72
1
}
73

            
74
#[test]
75
1
fn closing_a_missing_window_changes_nothing() {
76
1
    let mut app = crate::test_support::app();
77
1
    app.close_window(WindowId(u32::MAX));
78
1
    assert_eq!(app.windows.len(), ViewId::ALL.len());
79
1
}
80

            
81
#[test]
82
1
fn a_pending_form_of_the_closed_window_is_released() {
83
1
    let mut app = crate::test_support::app();
84
1
    let accounts = app.window_of(ViewId::Accounts);
85
1
    route_to(&mut app, 5, accounts, RouteCtx::None);
86
1
    app.lock_pending_form(5, Form::config_set());
87
1
    assert_eq!(current_focus(&app), FocusTarget::Pending);
88
1
    app.close_window(accounts);
89
1
    assert!(app.pending_form.is_none());
90
1
    assert_ne!(current_focus(&app), FocusTarget::Pending);
91
1
    assert!(app.dialog_free("a new form"));
92
1
    assert!(app.status.contains("window closed"), "{}", app.status);
93
1
}
94

            
95
#[test]
96
1
fn a_pending_form_of_another_window_stays_locked() {
97
1
    let mut app = crate::test_support::app();
98
1
    let tags = app.window_of(ViewId::Tags);
99
1
    route_to(&mut app, 5, tags, RouteCtx::None);
100
1
    app.lock_pending_form(5, Form::config_set());
101
1
    app.close_window(app.window_of(ViewId::Accounts));
102
1
    assert!(app.pending_form.is_some());
103
1
}
104

            
105
#[tokio::test]
106
1
async fn a_bulk_delete_of_the_closed_window_is_abandoned() {
107
1
    let mut app = crate::test_support::app();
108
1
    app.attach_echo();
109
1
    app.overlays
110
1
        .set_origin(app.origin_of(app.window_of(ViewId::Transactions)));
111
1
    app.submit_delete_marked(vec!["a".into(), "b".into()]);
112
1
    assert_eq!(app.pending_routes.len(), 2);
113
1
    app.close_window(app.window_of(ViewId::Transactions));
114
1
    assert!(
115
1
        app.status.contains("outcome unknown for 2"),
116
        "{}",
117
        app.status
118
    );
119
1
    assert_eq!(app.bulk.abandon(), None);
120
1
}
121

            
122
#[tokio::test]
123
1
async fn a_bulk_delete_of_another_window_runs_on() {
124
1
    let mut app = crate::test_support::app();
125
1
    app.attach_echo();
126
1
    app.overlays
127
1
        .set_origin(app.origin_of(app.window_of(ViewId::Transactions)));
128
1
    app.submit_delete_marked(vec!["a".into()]);
129
1
    app.close_window(app.window_of(ViewId::Accounts));
130
1
    assert!(app.bulk.abandon().is_some());
131
1
}
132

            
133
#[tokio::test]
134
1
async fn a_late_reply_for_the_closed_window_is_dropped() {
135
1
    let mut app = crate::test_support::app();
136
1
    app.attach_echo();
137
1
    app.overlays
138
1
        .set_origin(app.origin_of(app.window_of(ViewId::Transactions)));
139
1
    app.submit_delete_marked(vec!["a".into()]);
140
1
    let [(&request, _)] = app.pending_routes.iter().collect::<Vec<_>>()[..] else {
141
        panic!("one bulk route");
142
    };
143
1
    app.close_window(app.window_of(ViewId::Transactions));
144
1
    let status = app.status.clone();
145
1
    app.route_reply(request, &format!("(:id {request} :value t)"));
146
1
    assert_eq!(app.status, status);
147
1
    assert!(app.pending_routes.is_empty());
148
1
}
149

            
150
#[test]
151
1
fn closing_over_a_modal_cancels_it_and_keeps_the_window() {
152
1
    let mut app = crate::test_support::app();
153
1
    crate::test_support::open_overlay(&mut app);
154
1
    apply(&mut app, Intent::CloseWindow);
155
1
    assert!(app.overlays.is_empty());
156
1
    assert_eq!(app.windows.len(), ViewId::ALL.len());
157
1
    crate::test_support::open_overlay(&mut app);
158
1
    press(&mut app, alt(KeyCode::F(3)));
159
1
    assert!(app.overlays.is_empty());
160
1
    assert_eq!(app.windows.len(), ViewId::ALL.len());
161
1
    press(&mut app, alt(KeyCode::F(3)));
162
1
    assert_eq!(app.windows.len(), ViewId::ALL.len() - 1);
163
1
}
164

            
165
#[test]
166
1
fn m_f3_and_c_f4_close_the_front_window_in_every_preset() {
167
1
    let chords = [alt(KeyCode::F(3)), ctrl(KeyCode::F(4))];
168
1
    for name in ["cua", "emacs", "vim"] {
169
3
        for chord in chords {
170
6
            let mut app = crate::test_support::app_with_preset(name);
171
6
            press(&mut app, chord);
172
6
            assert_eq!(app.windows.len(), ViewId::ALL.len() - 1, "{name} {chord:?}");
173
6
            assert_eq!(app.window_id(ViewId::Accounts), None, "{name} {chord:?}");
174
        }
175
    }
176
1
}
177

            
178
#[test]
179
1
fn m_f3_closes_a_focused_console() {
180
1
    let mut app = crate::test_support::app();
181
1
    app.focus_console();
182
1
    press(&mut app, alt(KeyCode::F(3)));
183
1
    assert_eq!(app.window_id(ViewId::Console), None);
184
1
    assert!(!app.console_input_focused());
185
1
}
186

            
187
#[test]
188
1
fn the_empty_desktop_draws_only_the_background_and_the_bars() {
189
1
    let mut app = crate::test_support::app();
190
1
    close_all(&mut app);
191
1
    assert!(app.windows.is_empty());
192
1
    assert_eq!(app.front_kind(), None);
193
1
    let (buffer, _) = draw_frame(&app, 80, 24);
194
1
    let body = &buffer.content()[80 * 3..80 * 20];
195
1360
    assert!(body.iter().all(|cell| cell.symbol() == "\u{2591}"));
196
1
}
197

            
198
#[test]
199
1
fn the_menu_opens_on_the_empty_desktop() {
200
1
    let mut app = crate::test_support::app();
201
1
    close_all(&mut app);
202
1
    press(&mut app, key(KeyCode::F(10)));
203
1
    assert_eq!(app.menu_item(), Some(0));
204
1
    press(&mut app, key(KeyCode::Enter));
205
1
    assert_eq!(app.front_kind(), Some(ViewId::Accounts));
206
1
}
207

            
208
#[test]
209
1
fn keys_on_the_empty_desktop_do_nothing_but_say_so() {
210
1
    let mut app = crate::test_support::app();
211
1
    close_all(&mut app);
212
1
    for code in [
213
1
        KeyCode::Tab,
214
1
        KeyCode::BackTab,
215
1
        KeyCode::Char('r'),
216
1
        KeyCode::Char('c'),
217
1
        KeyCode::Char('e'),
218
1
        KeyCode::Char('d'),
219
1
        KeyCode::Enter,
220
1
        KeyCode::Esc,
221
1
        KeyCode::Down,
222
1
        KeyCode::F(5),
223
1
    ] {
224
10
        app.clear_status();
225
10
        press(&mut app, key(code));
226
10
        assert!(app.windows.is_empty(), "{code:?}");
227
10
        assert!(app.overlays.is_empty(), "{code:?}");
228
    }
229
1
    press(&mut app, key(KeyCode::Tab));
230
1
    assert_eq!(app.status, "no window");
231
1
    app.clear_status();
232
1
    press(&mut app, alt(KeyCode::F(3)));
233
1
    assert_eq!(app.status, "no window");
234
1
}
235

            
236
#[test]
237
1
fn a_digit_raises_the_existing_window() {
238
1
    let mut app = crate::test_support::app();
239
1
    let transactions = app.window_of(ViewId::Transactions);
240
1
    press(&mut app, key(KeyCode::Char('2')));
241
1
    assert_eq!(app.front_window(), Some(transactions));
242
1
    assert_eq!(app.windows.len(), ViewId::ALL.len());
243
1
}
244

            
245
#[tokio::test]
246
1
async fn a_digit_reopens_a_closed_view_zoomed_and_loads_it() {
247
1
    let mut app = crate::test_support::app();
248
1
    app.attach_echo();
249
1
    let closed = app.window_of(ViewId::Transactions);
250
1
    app.close_window(closed);
251
1
    press(&mut app, key(KeyCode::Char('2')));
252
1
    let reopened = app.window_of(ViewId::Transactions);
253
1
    assert_ne!(reopened, closed);
254
1
    assert_eq!(app.front_window(), Some(reopened));
255
10
    assert!(app.desktop.iter_back_to_front().all(|slot| slot.zoomed));
256
1
    let loads = app
257
1
        .pending_routes
258
1
        .values()
259
1
        .filter(|route| route.target == Some(reopened))
260
        .count();
261
1
    assert_eq!(loads, 1);
262
1
}
263

            
264
#[test]
265
1
fn a_digit_on_the_empty_desktop_opens_the_view() {
266
1
    let mut app = crate::test_support::app();
267
1
    close_all(&mut app);
268
1
    press(&mut app, key(KeyCode::Char('4')));
269
1
    assert_eq!(app.front_kind(), Some(ViewId::Reports));
270
1
    assert_eq!(app.windows.len(), 1);
271
1
}
272

            
273
#[tokio::test]
274
1
async fn a_kitty_chart_is_deleted_when_its_window_leaves_the_front_or_closes() {
275
1
    let (mut app, _) = with_table(40, 20, ChartBackend::Kitty);
276
1
    show(&mut app);
277
1
    let [id] = chart_requests(&app)[..] else {
278
        panic!("one chart request");
279
    };
280
1
    app.route_reply(id, &png_wire(id, stripes(380, 320)));
281
1
    let reports = app.window_of(ViewId::Reports);
282
1
    let image = crate::tabs::reports::chart::kitty_image_id(reports);
283
1
    assert_eq!(
284
1
        app.win::<crate::tabs::reports::ReportsTab>(reports)
285
1
            .and_then(|tab| tab.chart.kitty_id()),
286
1
        Some(image)
287
    );
288
1
    app.take_outbound();
289
3
    let transmits = |app: &App| text(&rendered(app, 40, 20)).contains("a=T");
290
1
    assert!(transmits(&app));
291
1
    assert!(!transmits(&app), "a picture transmits once");
292
1
    let deleted = vec![tvui::kitty_delete(image)];
293
1
    app.switch_tab(ViewId::Accounts);
294
1
    assert_eq!(app.take_outbound(), deleted);
295
1
    app.switch_tab(ViewId::Reports);
296
1
    assert!(app.take_outbound().is_empty());
297
1
    assert!(transmits(&app), "the raised chart transmits again");
298
1
    app.close_window(reports);
299
1
    assert_eq!(app.take_outbound(), deleted);
300
1
}