1
//! The windows live on the desktop: its order is the active view, the draw order and the lookup order.
2

            
3
use super::report_chart::{
4
    EMPTY, NO_OVERRIDE, call_for, chart_requests, echoed, env, overrides, png_wire, rendered, show,
5
    stripes, text, with_table,
6
};
7
use super::*;
8
use crate::focus::{FocusTarget, current_focus};
9
use crate::tabs::reports::{ReportKind, ReportsTab};
10
use crate::test_support::{key, press};
11
use forms::FormValues;
12
use ratatui::crossterm::event::KeyCode;
13
use ratatui::layout::Rect;
14
use rpc::chart::ChartBackend;
15

            
16
const AREA: Rect = Rect::new(0, 0, 80, 22);
17

            
18
#[test]
19
1
fn every_startup_window_is_on_the_desktop_zoomed_with_the_first_view_in_front() {
20
1
    let app = crate::test_support::app();
21
1
    let slots: Vec<_> = app.desktop.iter_back_to_front().collect();
22
1
    assert_eq!(slots.len(), ViewId::ALL.len());
23
10
    assert!(slots.iter().all(|slot| slot.zoomed));
24
1
    assert_eq!(app.desktop.front(), Some(app.window_of(ViewId::ALL[0])));
25
1
    assert_eq!(app.front_kind(), Some(ViewId::ALL[0]));
26
1
}
27

            
28
#[test]
29
1
fn switching_a_tab_raises_its_window_on_the_desktop() {
30
1
    let mut app = crate::test_support::app();
31
1
    app.switch_tab(ViewId::Tags);
32
1
    app.switch_tab(ViewId::Config);
33
1
    let order: Vec<_> = app
34
1
        .desktop
35
1
        .iter_back_to_front()
36
10
        .map(|slot| slot.id)
37
        .collect();
38
1
    let [.., tags, config] = order[..] else {
39
        panic!("two windows or more");
40
    };
41
1
    assert_eq!(tags, app.window_of(ViewId::Tags));
42
1
    assert_eq!(config, app.window_of(ViewId::Config));
43
1
    assert_eq!(app.front_kind(), Some(ViewId::Config));
44
1
}
45

            
46
#[test]
47
1
fn the_drawn_body_is_the_front_window_alone() {
48
1
    let mut app = crate::test_support::app();
49
1
    for kind in [ViewId::Accounts, ViewId::Scripts, ViewId::Console] {
50
3
        app.switch_tab(kind);
51
3
        let drawn: Vec<_> = app
52
3
            .visible_windows(AREA)
53
3
            .into_iter()
54
3
            .map(|(id, view, place)| (id, view.kind(), place.visible_in(AREA)))
55
            .collect();
56
3
        assert_eq!(drawn, [(app.window_of(kind), kind, AREA)]);
57
    }
58
1
}
59

            
60
#[test]
61
1
fn an_unzoomed_window_shows_the_one_behind_it() {
62
1
    let mut app = crate::test_support::app();
63
1
    let front = app.window_of(ViewId::Accounts);
64
1
    app.desktop.toggle_zoom(front);
65
1
    let drawn: Vec<_> = app
66
1
        .visible_windows(AREA)
67
1
        .iter()
68
2
        .map(|(id, _, _)| *id)
69
        .collect();
70
1
    assert_eq!(drawn.len(), 2);
71
1
    assert_eq!(drawn.last(), Some(&front));
72
1
}
73

            
74
#[test]
75
1
fn the_console_prompt_focus_follows_the_front_window() {
76
1
    let mut app = crate::test_support::app();
77
1
    app.switch_tab(ViewId::Console);
78
1
    app.set_console_focused(true);
79
1
    assert!(app.console_input_focused());
80
1
    assert_eq!(current_focus(&app), FocusTarget::ConsoleInput);
81
1
    let accounts = app.window_of(ViewId::Accounts);
82
1
    app.desktop.raise(accounts);
83
1
    assert!(!app.console_input_focused());
84
1
    assert_eq!(current_focus(&app), FocusTarget::ViewPane);
85
1
}
86

            
87
#[test]
88
1
fn a_lookup_by_kind_walks_the_desktop_front_first() {
89
1
    let mut app = crate::test_support::app();
90
1
    let first = app.window_of(ViewId::Console);
91
1
    let second = app.open_new_window(View::Console(ConsoleState::new()));
92
1
    assert_eq!(app.window_id(ViewId::Console), Some(second));
93
1
    app.desktop.raise(first);
94
1
    assert_eq!(app.window_id(ViewId::Console), Some(first));
95
1
    assert_eq!(app.window_ids(ViewId::Console), [first, second]);
96
1
}
97

            
98
#[test]
99
1
fn a_form_submitted_to_the_console_goes_to_the_front_console() {
100
1
    let mut app = crate::test_support::app();
101
1
    let first = app.window_of(ViewId::Console);
102
1
    let second = app.open_new_window(View::Console(ConsoleState::new()));
103
1
    app.desktop.raise(first);
104
1
    app.submit_to_console("(x)".to_string());
105
2
    let echoed = |id| {
106
2
        app.win::<ConsoleState>(id)
107
2
            .is_some_and(|console| console.scrollback.iter().any(|line| line == "> (x)"))
108
2
    };
109
1
    assert!(echoed(first));
110
1
    assert!(!echoed(second));
111
1
}
112

            
113
/// Opens a second Reports window with a loaded table, on top.
114
2
fn open_loaded_reports(app: &mut App) -> WindowId {
115
2
    let mut reports = ReportsTab::new();
116
2
    reports.on_reply(EMPTY, ReportKind::Balance, FormValues::default());
117
2
    app.open_new_window(View::Reports(reports))
118
2
}
119

            
120
#[tokio::test]
121
1
async fn two_windows_toggling_the_chart_both_settle() {
122
1
    let (mut app, _) = with_table(40, 20, ChartBackend::Halfblocks);
123
1
    let first = app.window_of(ViewId::Reports);
124
1
    press(&mut app, key(KeyCode::Char('g')));
125
1
    let second = open_loaded_reports(&mut app);
126
1
    press(&mut app, key(KeyCode::Char('g')));
127
1
    let [a, b] = overrides(&app)[..] else {
128
        panic!("one override read per window");
129
    };
130
1
    app.route_reply(a, NO_OVERRIDE);
131
1
    app.route_reply(b, NO_OVERRIDE);
132
1
    let targets: Vec<_> = chart_requests(&app)
133
1
        .iter()
134
2
        .filter_map(|id| app.pending_routes.get(id)?.target)
135
        .collect();
136
1
    assert!(targets.contains(&first), "the first window kept Loading");
137
1
    assert!(targets.contains(&second));
138
1
}
139

            
140
#[tokio::test]
141
1
async fn every_reports_window_adopts_the_terminal() {
142
1
    let (mut app, _) = with_table(40, 20, ChartBackend::Halfblocks);
143
1
    show(&mut app);
144
1
    let first = app.window_of(ViewId::Reports);
145
1
    let second = open_loaded_reports(&mut app);
146
1
    if let Some(reports) = app.win_mut::<ReportsTab>(second) {
147
1
        reports.chart.toggle()
148
    }
149
1
    app.sync_terminal(env(30, 16, ChartBackend::Halfblocks));
150
1
    let targets: Vec<_> = chart_requests(&app)
151
1
        .iter()
152
3
        .filter_map(|id| app.pending_routes.get(id)?.target)
153
        .collect();
154
3
    assert_eq!(targets.iter().filter(|id| **id == first).count(), 2);
155
3
    assert_eq!(targets.iter().filter(|id| **id == second).count(), 1);
156
1
}
157

            
158
#[tokio::test]
159
1
async fn the_chart_is_sized_to_its_window_not_the_screen() {
160
1
    let (mut app, sent) = with_table(80, 24, ChartBackend::Halfblocks);
161
1
    app.toggle_zoom_of(ViewId::Reports);
162
1
    show(&mut app);
163
1
    let echoed = echoed(&mut app, &sent).await;
164
1
    assert!(echoed.contains(&call_for(580, 280)), "{echoed}");
165
1
}
166

            
167
/// A Reports window behind an unzoomed Accounts one, with its chart ready.
168
2
fn rear_chart(backend: ChartBackend) -> App {
169
2
    let (mut app, _) = with_table(80, 24, backend);
170
2
    show(&mut app);
171
2
    let [id] = chart_requests(&app)[..] else {
172
        panic!("one chart request");
173
    };
174
2
    app.route_reply(id, &png_wire(id, stripes(780, 400)));
175
2
    app.switch_tab(ViewId::Accounts);
176
2
    app.toggle_zoom_of(ViewId::Accounts);
177
2
    app
178
2
}
179

            
180
#[tokio::test]
181
1
async fn a_halfblocks_chart_draws_in_a_rear_window() {
182
1
    let app = rear_chart(ChartBackend::Halfblocks);
183
1
    let terminal = rendered(&app, 80, 24);
184
1
    assert_ne!(terminal.backend().buffer()[(70, 10)].symbol(), " ");
185
1
}
186

            
187
#[tokio::test]
188
1
async fn a_kitty_chart_waits_until_its_window_is_in_front() {
189
1
    let mut app = rear_chart(ChartBackend::Kitty);
190
1
    assert!(!text(&rendered(&app, 80, 24)).contains("\x1b_G"));
191
1
    app.switch_tab(ViewId::Reports);
192
1
    assert!(text(&rendered(&app, 80, 24)).contains("\x1b_G"));
193
1
}