1
//! The Reports tab's chart is fetched again when its pane or the report changes.
2

            
3
use super::report_chart::{
4
    EMPTY, call_for, call_with_text, chart_requests, echoed, env, pending, png_wire, rendered,
5
    show, stripes, text, with_table,
6
};
7
use crate::app::App;
8
use crate::route::RouteCtx;
9
use crate::test_support::{ctrl, press};
10
use ratatui::crossterm::event::KeyCode;
11
use rpc::chart::ChartBackend;
12

            
13
#[tokio::test]
14
1
async fn a_resize_fetches_the_chart_at_the_new_size() {
15
1
    let (mut app, sent) = with_table(40, 20, ChartBackend::Halfblocks);
16
1
    show(&mut app);
17
1
    app.sync_terminal(env(30, 16, ChartBackend::Halfblocks));
18
1
    assert_eq!(chart_requests(&app).len(), 2);
19
1
    assert!(echoed(&mut app, &sent).await.contains(&call_for(280, 240)));
20
1
}
21

            
22
#[tokio::test]
23
1
async fn a_new_cell_size_fetches_the_chart_again() {
24
1
    let (mut app, sent) = with_table(40, 20, ChartBackend::Halfblocks);
25
1
    show(&mut app);
26
1
    let mut finer = env(40, 20, ChartBackend::Halfblocks);
27
1
    finer.cell_pixels = (8, 16);
28
1
    app.sync_terminal(finer);
29
1
    assert!(
30
1
        echoed(&mut app, &sent)
31
1
            .await
32
1
            .contains(&call_with_text(304, 256, 11))
33
1
    );
34
1
}
35

            
36
#[tokio::test]
37
1
async fn an_unchanged_terminal_does_not_refetch() {
38
1
    let (mut app, _) = with_table(40, 20, ChartBackend::Halfblocks);
39
1
    show(&mut app);
40
1
    app.sync_terminal(env(40, 20, ChartBackend::Halfblocks));
41
1
    assert_eq!(chart_requests(&app).len(), 1);
42
1
}
43

            
44
#[tokio::test]
45
1
async fn a_hidden_chart_is_not_fetched_on_resize() {
46
1
    let (mut app, _) = with_table(40, 20, ChartBackend::Halfblocks);
47
1
    app.sync_terminal(env(30, 16, ChartBackend::Halfblocks));
48
1
    assert!(chart_requests(&app).is_empty());
49
1
}
50

            
51
#[tokio::test]
52
1
async fn running_the_report_again_fetches_the_chart_again() {
53
1
    let (mut app, _) = with_table(40, 20, ChartBackend::Halfblocks);
54
1
    show(&mut app);
55
1
    assert_eq!(chart_requests(&app).len(), 1);
56
1
    press(&mut app, ctrl(KeyCode::Char('r')));
57
2
    let run = pending(&app, |ctx| matches!(ctx, RouteCtx::Reports { .. }))[0];
58
1
    app.route_reply(run, EMPTY);
59
1
    assert_eq!(chart_requests(&app).len(), 2);
60
1
}
61

            
62
#[tokio::test]
63
1
async fn the_reply_of_the_old_pane_size_is_dropped() {
64
1
    let (mut app, _) = with_table(40, 20, ChartBackend::Halfblocks);
65
1
    show(&mut app);
66
1
    let [old] = chart_requests(&app)[..] else {
67
        panic!("one chart request");
68
    };
69
1
    app.sync_terminal(env(30, 20, ChartBackend::Halfblocks));
70
1
    let [_, new] = chart_requests(&app)[..] else {
71
        panic!("two chart requests");
72
    };
73
1
    app.route_reply(old, &png_wire(old, stripes(280, 240)));
74
1
    assert!(text(&rendered(&app, 30, 20)).contains("Loading chart..."));
75
1
    app.route_reply(new, &png_wire(new, stripes(280, 240)));
76
1
    assert!(!text(&rendered(&app, 30, 20)).contains("Loading chart..."));
77
1
}
78

            
79
/// The `i=` id of the first kitty transmit drawn.
80
2
fn transmitted_id(app: &App, width: u16) -> String {
81
2
    let drawn = text(&rendered(app, width, 20));
82
2
    let after = drawn.split("\x1b_Gq=2,i=").nth(1).expect("a transmit");
83
2
    after.split(',').next().expect("an id").to_string()
84
2
}
85

            
86
#[tokio::test]
87
1
async fn every_kitty_transmit_of_a_session_carries_the_same_id() {
88
1
    let (mut app, _) = with_table(40, 20, ChartBackend::Kitty);
89
1
    show(&mut app);
90
1
    let [first] = chart_requests(&app)[..] else {
91
        panic!("one chart request");
92
    };
93
1
    app.route_reply(first, &png_wire(first, stripes(380, 240)));
94
1
    let id = transmitted_id(&app, 40);
95
1
    app.sync_terminal(env(30, 20, ChartBackend::Kitty));
96
1
    let [second] = chart_requests(&app)[..] else {
97
        panic!("one chart request");
98
    };
99
1
    app.route_reply(second, &png_wire(second, stripes(280, 240)));
100
1
    assert_eq!(transmitted_id(&app, 30), id);
101
1
}
102

            
103
#[tokio::test]
104
1
async fn a_theme_with_another_panel_colour_fetches_the_shown_chart_again() {
105
1
    let (mut app, sent) = with_table(40, 20, ChartBackend::Halfblocks);
106
1
    show(&mut app);
107
1
    let before = chart_requests(&app).len();
108
1
    app.set_theme(tvui::Theme::preset("turbo").expect("preset"));
109
1
    assert_eq!(chart_requests(&app).len(), before + 1);
110
1
    let echoed = echoed(&mut app, &sent).await;
111
1
    assert!(echoed.contains(":background 170"), "{echoed}");
112
1
}
113

            
114
#[tokio::test]
115
1
async fn a_theme_with_the_same_panel_colour_leaves_the_chart_alone() {
116
1
    let (mut app, _) = with_table(40, 20, ChartBackend::Halfblocks);
117
1
    show(&mut app);
118
1
    let before = chart_requests(&app).len();
119
1
    app.set_theme(app.theme.clone());
120
1
    assert_eq!(chart_requests(&app).len(), before);
121
1
}
122

            
123
#[tokio::test]
124
1
async fn a_depth_change_that_moves_the_panel_colour_fetches_the_chart_again() {
125
1
    let (mut app, sent) = with_table(40, 20, ChartBackend::Halfblocks);
126
1
    show(&mut app);
127
1
    let before = chart_requests(&app).len();
128
1
    app.set_theme(app.theme.clone().with_depth(tvui::ColorDepth::Ansi16));
129
1
    assert_eq!(chart_requests(&app).len(), before + 1);
130
1
    let echoed = echoed(&mut app, &sent).await;
131
1
    assert!(echoed.contains(":background 0"), "{echoed}");
132
1
}