1
//! A window change over a real ssh connection redraws the TUI at the new size.
2
//!
3
//! The daemon image has no tty, no `tput` and no `TERM`, so nothing in the
4
//! resize path may ask the local terminal for a size. Run this under
5
//! `env -u TERM setsid -w` to prove it.
6

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

            
19
const DEADLINE: Duration = Duration::from_secs(45);
20

            
21
/// The status bar is the only row that starts with a style, so this names the window height.
22
9
fn status_row(rows: usize) -> String {
23
9
    format!("\x1b[{rows};1H\x1b[")
24
9
}
25

            
26
#[test]
27
1
fn the_status_marker_pins_the_height() {
28
1
    let frame = format!("{}status", status_row(30));
29
1
    assert!(frame.contains(&status_row(30)));
30
1
    assert!(!frame.contains(&status_row(20)));
31
1
    assert!(!"\x1b[20;1H║".contains(&status_row(20)));
32
1
}
33

            
34
1
async fn resize_session(daemon: &Daemon, key: PrivateKey) -> anyhow::Result<()> {
35
1
    let mut session = TuiSession::open(daemon.addr, key, (100, 30), (0, 0)).await?;
36
1
    session.channel.data(&b"1"[..]).await?;
37
6
    session.read_until(|text| text.contains('╤')).await?;
38
1
    session.forget();
39
1
    session.channel.window_change(60, 20, 0, 0).await?;
40
1
    session
41
5
        .read_until(|text| text.contains(&status_row(20)))
42
1
        .await?;
43
1
    session
44
1
        .handle
45
1
        .disconnect(Disconnect::ByApplication, "done", "")
46
1
        .await?;
47
1
    Ok(())
48
1
}
49

            
50
#[local_db_sqlx_test]
51
async fn a_window_change_redraws_at_the_new_size(pool: PgPool) -> anyhow::Result<()> {
52
    let user_id = Uuid::new_v4();
53
    insert_verified_user(&pool, user_id).await?;
54
    let key = random_key()?;
55
    register_key(user_id, &key).await?;
56
    let daemon = start_daemon().await?;
57
    let outcome = tokio::time::timeout(DEADLINE, resize_session(&daemon, key)).await;
58
    daemon.stop().await?;
59
    outcome??;
60
}