Lines
98.41 %
Functions
91.67 %
Branches
100 %
//! The TUI chart over a real SSH connection: the pty-req pixel size decides
//! the size of the image the daemon writes into the channel.
use crate::eval_subsystem_tests::{
insert_verified_user, random_key, register_key, setup, start_daemon,
};
use crate::tui_session::TuiSession;
use russh::Disconnect;
use russh::keys::PrivateKey;
use server::db::install_test_pool;
use sqlx::PgPool;
use sqlx::types::Uuid;
use std::net::SocketAddr;
use std::time::Duration;
use supp_macro::local_db_sqlx_test;
const DEADLINE: Duration = Duration::from_secs(45);
const KITTY_TRANSMIT: &str = "\x1b_Gq=2,i=";
/// A pty window change: columns, rows, pixel width and pixel height.
type Window = (u32, u32, u32, u32);
/// The `s=…,v=…` pixel size the kitty transmit of the sent chart carries.
fn transmitted_size(seen: &str) -> Option<(u32, u32)> {
let params = seen.split(KITTY_TRANSMIT).nth(1)?.split(';').next()?;
let number = |name: &str| {
let (_, rest) = params.split_once(name)?;
rest.split(',').next()?.parse().ok()
Some((number("s=")?, number("v=")?))
}
async fn next_transmit(session: &mut TuiSession) -> anyhow::Result<(u32, u32)> {
session
.read_until(|text| {
text.split(KITTY_TRANSMIT)
.nth(1)
.is_some_and(|rest| rest.contains(';'))
})
.await?;
transmitted_size(&session.seen())
.ok_or_else(|| anyhow::anyhow!("no kitty transmit in the channel"))
/// The sizes of the charts sent: one for the pty, one more after `resize`.
async fn chart_sizes(
addr: SocketAddr,
key: PrivateKey,
pixels: (u32, u32),
resize: Option<Window>,
) -> anyhow::Result<Vec<(u32, u32)>> {
let mut session = TuiSession::open(addr, key, (100, 30), pixels).await?;
.channel
.data(&b":reports balance from=2026-01-01\r"[..])
.read_until(|text| text.contains("(no data)"))
session.channel.data(&b"g"[..]).await?;
let mut sizes = vec![next_transmit(&mut session).await?];
if let Some((cols, rows, width, height)) = resize {
session.forget();
.window_change(cols, rows, width, height)
sizes.push(next_transmit(&mut session).await?);
.handle
.disconnect(Disconnect::ByApplication, "done", "")
Ok(sizes)
async fn sizes_for(
pool: &PgPool,
let user_id = Uuid::new_v4();
insert_verified_user(pool, user_id).await?;
let key = random_key()?;
register_key(user_id, &key).await?;
let daemon = start_daemon().await?;
let outcome =
tokio::time::timeout(DEADLINE, chart_sizes(daemon.addr, key, pixels, resize)).await;
daemon.stop().await?;
outcome?
// 100x30 cells leave a 98x26 pane inside the borders.
#[local_db_sqlx_test]
async fn the_pty_cell_size_sets_the_size_of_the_sent_chart(pool: PgPool) -> anyhow::Result<()> {
assert_eq!(sizes_for(&pool, (1100, 660), None).await?, [(1078, 572)]);
async fn a_bigger_pty_cell_sends_a_bigger_chart(pool: PgPool) -> anyhow::Result<()> {
assert_eq!(sizes_for(&pool, (1200, 900), None).await?, [(1176, 780)]);
async fn a_pty_without_pixels_sends_the_default_cell_size(pool: PgPool) -> anyhow::Result<()> {
assert_eq!(sizes_for(&pool, (0, 0), None).await?, [(980, 520)]);
async fn a_window_change_sends_the_chart_again_at_the_new_size(pool: PgPool) -> anyhow::Result<()> {
// 100x30 cells at 11x22, then 80x24 cells at 12x30: a 78x20 pane.
let sizes = sizes_for(&pool, (1100, 660), Some((80, 24, 960, 720))).await?;
assert_eq!(sizes, [(1078, 572), (936, 600)]);