1
//! The viewer windows: fixed, one per source, and never above a dialog.
2

            
3
use super::report_chart::env;
4
use super::transaction_detail::{TX, detail_text, front_viewer, open_detail, reply_of, started};
5
use crate::app::{App, MenuId};
6
use crate::hits::Target;
7
use crate::modal::Modal;
8
use crate::route::RouteCtx;
9
use crate::test_support::{draw_frame, flat, key, press, redraw};
10
use crate::view::ViewId;
11
use crate::viewer::Source;
12
use crate::window::WindowId;
13
use ratatui::crossterm::event::KeyCode;
14
use ratatui::layout::Position;
15
use rpc::chart::ChartBackend;
16
use tvui::Caps;
17

            
18
9
fn detail_of(id: &str) -> Source {
19
9
    Source::Detail { id: id.to_string() }
20
9
}
21

            
22
8
fn lines(count: usize) -> Vec<String> {
23
27
    (0..count).map(|n| format!("line {n}")).collect()
24
8
}
25

            
26
2
fn on_80x24(app: &mut App) {
27
2
    app.terminal.replace(env(80, 24, ChartBackend::Halfblocks));
28
2
}
29

            
30
/// The targets recorded on the cells of window `id`, bottom to top as the mouse finds them.
31
1
fn targets_of(app: &App, id: WindowId) -> Vec<Target> {
32
1
    let place = app.desktop.place_of(id, app.desktop_rect());
33
1
    let rect = flat(place.expect("the window is open"));
34
350
    let cells = (rect.y..rect.bottom()).flat_map(|y| (rect.x..rect.right()).map(move |x| (x, y)));
35
1
    cells
36
350
        .filter_map(|(x, y)| app.hits.hit_at(Position::new(x, y)))
37
        .map(|(_, target, _)| target)
38
        .collect()
39
1
}
40

            
41
#[tokio::test]
42
1
async fn a_detail_window_is_fixed_with_no_zoom_icon_or_grow_corner() {
43
1
    let (mut app, _) = started();
44
1
    on_80x24(&mut app);
45
1
    open_detail(&mut app, &detail_text("", 1));
46
1
    redraw(&mut app);
47
1
    let id = app.front_window().expect("the detail is in front");
48
1
    assert_eq!(
49
1
        app.desktop.window(id).map(|slot| slot.caps),
50
1
        Some(Caps::FIXED)
51
    );
52
1
    let targets = targets_of(&app, id);
53
1
    assert!(targets.contains(&Target::Close));
54
1
    assert!(!targets.contains(&Target::Zoom), "{targets:?}");
55
1
    assert!(!targets.contains(&Target::Grow), "{targets:?}");
56
1
}
57

            
58
#[tokio::test]
59
1
async fn a_refused_zoom_changes_and_loads_nothing() {
60
1
    let (mut app, _) = started();
61
1
    app.close_all_windows();
62
1
    app.open_extra(ViewId::Accounts);
63
1
    on_80x24(&mut app);
64
1
    app.show_viewer(detail_of(TX), lines(3));
65
1
    let (id, requests) = (app.front_window(), app.pending_routes.len());
66
1
    app.toggle_zoom();
67
1
    assert_eq!(app.front_window(), id);
68
1
    assert!(
69
1
        app.desktop
70
1
            .window(id.expect("a front window"))
71
1
            .is_some_and(|slot| !slot.zoomed)
72
    );
73
1
    assert_eq!(
74
1
        app.pending_routes.len(),
75
1
        requests,
76
1
        "the window behind loaded"
77
1
    );
78
1
}
79

            
80
#[tokio::test]
81
1
async fn a_second_detail_of_one_id_raises_the_open_one() {
82
1
    let (mut app, _) = started();
83
1
    app.show_viewer(detail_of(TX), lines(3));
84
1
    let detail = app.front_window();
85
1
    app.open_extra(ViewId::Accounts);
86
1
    app.show_viewer(detail_of(TX), lines(5));
87
1
    assert_eq!(app.windows_of(ViewId::Viewer), 1);
88
1
    assert_eq!(app.front_window(), detail);
89
1
    assert_eq!(front_viewer(&app).lines.len(), 5);
90
1
}
91

            
92
#[tokio::test]
93
1
async fn a_detail_of_another_id_opens_a_second_window() {
94
1
    let (mut app, _) = started();
95
1
    app.show_viewer(detail_of(TX), lines(3));
96
1
    app.show_viewer(detail_of("other"), lines(3));
97
1
    assert_eq!(app.windows_of(ViewId::Viewer), 2);
98
1
    let labels: Vec<String> = app
99
1
        .window_entries()
100
1
        .into_iter()
101
        .map(|(_, label)| label)
102
        .collect();
103
1
    for id in [TX, "other"] {
104
2
        assert!(labels.contains(&format!("Transaction {id}")), "{labels:?}");
105
1
    }
106
1
}
107

            
108
#[tokio::test]
109
1
async fn a_detail_asked_for_under_a_dialog_opens_behind_it() {
110
1
    let (mut app, _) = started();
111
1
    press(&mut app, key(KeyCode::Enter));
112
1
    let id = app
113
1
        .pending_routes
114
1
        .keys()
115
1
        .copied()
116
1
        .max()
117
1
        .expect("a detail request");
118
1
    crate::test_support::open_overlay(&mut app);
119
1
    app.route_reply(id, &reply_of(id, &detail_text("", 1)));
120
1
    assert!(matches!(app.overlays.top(), Some(Modal::Confirm { .. })));
121
1
    assert_eq!(app.windows_of(ViewId::Viewer), 1);
122
1
    assert!(app.status.is_empty(), "{}", app.status);
123
1
}
124

            
125
#[tokio::test]
126
1
async fn the_edit_of_a_detail_belongs_to_the_detail_window() {
127
1
    let (mut app, _) = started();
128
1
    open_detail(&mut app, &detail_text("", 1));
129
1
    press(&mut app, key(KeyCode::Char('e')));
130
1
    let route = app
131
1
        .pending_routes
132
1
        .values()
133
1
        .find(|route| matches!(route.ctx, RouteCtx::TransactionEdit));
134
1
    assert_eq!(route.and_then(|route| route.target), app.front_window());
135
1
}
136

            
137
#[test]
138
1
fn the_size_of_a_wide_line_is_not_capped() {
139
1
    assert_eq!(tvui::viewer_size(&["x".repeat(500)]).0, 502);
140
1
}
141

            
142
4
fn row_text(buffer: &ratatui::buffer::Buffer, rect: ratatui::layout::Rect, y: u16) -> String {
143
4
    (rect.x..rect.right())
144
64
        .map(|x| buffer[(x, y)].symbol())
145
        .collect()
146
4
}
147

            
148
#[test]
149
1
fn a_multi_line_note_draws_on_separate_rows_and_end_reaches_the_last_row() {
150
1
    let (mut app, _) = started();
151
30
    let mut text: Vec<String> = (0..30).map(|n| format!("line{n:02}")).collect();
152
1
    text[0] = "Note: a\nb\nc".to_string();
153
1
    app.show_viewer(detail_of(TX), text);
154
1
    let id = app.front_window().expect("the detail is in front");
155
1
    let rect = flat(app.desktop.place_of(id, app.desktop_rect()).expect("open"));
156
1
    let buffer = draw_frame(&app, 80, 24).0;
157
1
    assert!(row_text(&buffer, rect, rect.y + 1).contains("Note: a"));
158
1
    assert!(row_text(&buffer, rect, rect.y + 2).contains('b'));
159
1
    assert!(!row_text(&buffer, rect, rect.y + 2).contains("Note"));
160
1
    press(&mut app, key(KeyCode::End));
161
1
    let buffer = draw_frame(&app, 80, 24).0;
162
1
    assert!(row_text(&buffer, rect, rect.bottom() - 2).contains("line29"));
163
1
}
164

            
165
#[test]
166
1
fn the_view_menu_opens_on_the_first_entry_over_a_viewer() {
167
1
    let (mut app, _) = started();
168
1
    app.show_viewer(detail_of(TX), lines(3));
169
1
    app.open_menu();
170
1
    assert_eq!(app.menu_item(), Some(0));
171
1
    assert_eq!(app.menu_title(), Some(MenuId::View));
172
1
}
173

            
174
#[test]
175
1
fn a_new_window_over_a_fixed_viewer_follows_the_zoom_of_the_window_behind() {
176
1
    let (mut app, _) = started();
177
1
    app.show_viewer(detail_of(TX), lines(3));
178
1
    let id = app.open_extra(ViewId::Accounts);
179
1
    assert!(app.desktop.window(id).is_some_and(|slot| slot.zoomed));
180
1
}
181

            
182
#[tokio::test]
183
1
async fn a_short_detail_is_a_small_window_on_a_big_screen() {
184
1
    let (mut app, _) = started();
185
1
    app.terminal.replace(env(200, 50, ChartBackend::Halfblocks));
186
1
    app.show_viewer(detail_of(TX), lines(4));
187
1
    let id = app.front_window().expect("the detail is in front");
188
1
    let place = app.desktop.place_of(id, app.desktop_rect());
189
1
    let width = place.expect("the window is open").width;
190
1
    assert!(width < 60, "{width}");
191
1
}