1
//! The TUI chart over a real SSH connection: the pty-req pixel size decides
2
//! the size of the image the daemon writes into the channel.
3

            
4
use crate::eval_subsystem_tests::{
5
    insert_verified_user, random_key, register_key, setup, start_daemon,
6
};
7
use crate::tui_session::TuiSession;
8
use russh::Disconnect;
9
use russh::keys::PrivateKey;
10
use server::db::install_test_pool;
11
use sqlx::PgPool;
12
use sqlx::types::Uuid;
13
use std::net::SocketAddr;
14
use std::time::Duration;
15
use supp_macro::local_db_sqlx_test;
16

            
17
const DEADLINE: Duration = Duration::from_secs(45);
18
const KITTY_TRANSMIT: &str = "\x1b_Gq=2,i=";
19

            
20
/// A pty window change: columns, rows, pixel width and pixel height.
21
type Window = (u32, u32, u32, u32);
22

            
23
/// The `s=…,v=…` pixel size the kitty transmit of the sent chart carries.
24
5
fn transmitted_size(seen: &str) -> Option<(u32, u32)> {
25
5
    let params = seen.split(KITTY_TRANSMIT).nth(1)?.split(';').next()?;
26
10
    let number = |name: &str| {
27
10
        let (_, rest) = params.split_once(name)?;
28
10
        rest.split(',').next()?.parse().ok()
29
10
    };
30
5
    Some((number("s=")?, number("v=")?))
31
5
}
32

            
33
5
async fn next_transmit(session: &mut TuiSession) -> anyhow::Result<(u32, u32)> {
34
5
    session
35
26
        .read_until(|text| {
36
26
            text.split(KITTY_TRANSMIT)
37
26
                .nth(1)
38
5
                .is_some_and(|rest| rest.contains(';'))
39
26
        })
40
5
        .await?;
41
5
    transmitted_size(&session.seen())
42
        .ok_or_else(|| anyhow::anyhow!("no kitty transmit in the channel"))
43
5
}
44

            
45
/// The sizes of the charts sent: one for the pty, one more after `resize`.
46
4
async fn chart_sizes(
47
4
    addr: SocketAddr,
48
4
    key: PrivateKey,
49
4
    pixels: (u32, u32),
50
4
    resize: Option<Window>,
51
4
) -> anyhow::Result<Vec<(u32, u32)>> {
52
4
    let mut session = TuiSession::open(addr, key, (100, 30), pixels).await?;
53
4
    session
54
4
        .channel
55
4
        .data(&b":reports balance from=2026-01-01\r"[..])
56
4
        .await?;
57
4
    session
58
152
        .read_until(|text| text.contains("(no data)"))
59
4
        .await?;
60
4
    session.channel.data(&b"g"[..]).await?;
61
4
    let mut sizes = vec![next_transmit(&mut session).await?];
62
4
    if let Some((cols, rows, width, height)) = resize {
63
1
        session.forget();
64
1
        session
65
1
            .channel
66
1
            .window_change(cols, rows, width, height)
67
1
            .await?;
68
1
        sizes.push(next_transmit(&mut session).await?);
69
3
    }
70
4
    session
71
4
        .handle
72
4
        .disconnect(Disconnect::ByApplication, "done", "")
73
4
        .await?;
74
4
    Ok(sizes)
75
4
}
76

            
77
4
async fn sizes_for(
78
4
    pool: &PgPool,
79
4
    pixels: (u32, u32),
80
4
    resize: Option<Window>,
81
4
) -> anyhow::Result<Vec<(u32, u32)>> {
82
4
    let user_id = Uuid::new_v4();
83
4
    insert_verified_user(pool, user_id).await?;
84
4
    let key = random_key()?;
85
4
    register_key(user_id, &key).await?;
86
4
    let daemon = start_daemon().await?;
87
    let outcome =
88
4
        tokio::time::timeout(DEADLINE, chart_sizes(daemon.addr, key, pixels, resize)).await;
89
4
    daemon.stop().await?;
90
4
    outcome?
91
4
}
92

            
93
// 100x30 cells leave a 98x26 pane inside the borders.
94
#[local_db_sqlx_test]
95
async fn the_pty_cell_size_sets_the_size_of_the_sent_chart(pool: PgPool) -> anyhow::Result<()> {
96
    assert_eq!(sizes_for(&pool, (1100, 660), None).await?, [(1078, 572)]);
97
}
98

            
99
#[local_db_sqlx_test]
100
async fn a_bigger_pty_cell_sends_a_bigger_chart(pool: PgPool) -> anyhow::Result<()> {
101
    assert_eq!(sizes_for(&pool, (1200, 900), None).await?, [(1176, 780)]);
102
}
103

            
104
#[local_db_sqlx_test]
105
async fn a_pty_without_pixels_sends_the_default_cell_size(pool: PgPool) -> anyhow::Result<()> {
106
    assert_eq!(sizes_for(&pool, (0, 0), None).await?, [(980, 520)]);
107
}
108

            
109
#[local_db_sqlx_test]
110
async fn a_window_change_sends_the_chart_again_at_the_new_size(pool: PgPool) -> anyhow::Result<()> {
111
    // 100x30 cells at 11x22, then 80x24 cells at 12x30: a 78x20 pane.
112
    let sizes = sizes_for(&pool, (1100, 660), Some((80, 24, 960, 720))).await?;
113
    assert_eq!(sizes, [(1078, 572), (936, 600)]);
114
}