Lines
100 %
Functions
Branches
//! The mouse over a real ssh connection: the modes are asked for on attach and given back on
//! quit, and SGR wheel reports and clicks move the Accounts selection.
//!
//! The cursor row is the only one a wheel step restyles, so a row's name is redrawn exactly
//! when the cursor enters or leaves it.
use crate::eval_subsystem_tests::{
Daemon, insert_verified_user, random_key, register_key, setup, start_daemon,
};
use crate::tui_session::TuiSession;
use russh::Disconnect;
use russh::keys::PrivateKey;
use server::command::account::CreateAccount;
use server::db::install_test_pool;
use sqlx::PgPool;
use sqlx::types::Uuid;
use std::future::Future;
use std::net::SocketAddr;
use std::time::Duration;
use supp_macro::local_db_sqlx_test;
use tui::transport::{MOUSE_DISABLE, MOUSE_ENABLE};
const DEADLINE: Duration = Duration::from_secs(45);
const QUIT: &[u8] = b"\x1bx";
const CONFIRM: &[u8] = b"y";
const WHEEL_DOWN: &[u8] = b"\x1b[<65;5;5M";
const WHEEL_UP: &[u8] = b"\x1b[<64;5;5M";
/// Left press and release over the third and the second row of the list (1-based).
const CLICK_CHARLIE: &[u8] = b"\x1b[<0;5;6M\x1b[<0;5;6m";
const CLICK_BRAVO: &[u8] = b"\x1b[<0;5;5M\x1b[<0;5;5m";
/// Left press and release on the list's scrollbar (the last column, 1-based): the down arrow on
/// the last row of the track, then the page area below the thumb.
const CLICK_DOWN_ARROW: &[u8] = b"\x1b[<0;100;28M\x1b[<0;100;28m";
const CLICK_PAGE_DOWN: &[u8] = b"\x1b[<0;100;21M\x1b[<0;100;21m";
/// F5 unzooms the front window; the click is on the left column of the Accounts window behind
/// the Transactions one, which hides its rows until it is raised.
const ZOOM_KEY: &[u8] = b"\x1b[15~";
const CLICK_REAR_WINDOW: &[u8] = b"\x1b[<0;1;6M\x1b[<0;1;6m";
/// Press on the title row of the zoomed window (1-based), then motion with the left button held
/// and the release: the window unzooms and moves, so its rows are drawn again.
const TITLE_PRESS: &[u8] = b"\x1b[<0;41;2M";
const TITLE_MOTION: &[u8] = b"\x1b[<32;46;6M";
const TITLE_RELEASE: &[u8] = b"\x1b[<0;46;6m";
const BAR_ACCOUNTS: usize = 40;
fn text(bytes: &[u8]) -> String {
String::from_utf8_lossy(bytes).into_owned()
}
/// A daemon and a registered key for a user owning the accounts `names`, and the session
/// `run` drives against them.
async fn with_session<F, Fut, T>(pool: &PgPool, names: &[&str], run: F) -> anyhow::Result<T>
where
F: FnOnce(SocketAddr, PrivateKey) -> Fut,
Fut: Future<Output = anyhow::Result<T>>,
{
let user_id = Uuid::new_v4();
insert_verified_user(pool, user_id).await?;
for name in names {
CreateAccount::new()
.name((*name).to_string())
.user_id(user_id)
.run()
.await?;
let key = random_key()?;
register_key(user_id, &key).await?;
let daemon: Daemon = start_daemon().await?;
let outcome = tokio::time::timeout(DEADLINE, run(daemon.addr, key)).await;
daemon.stop().await?;
outcome?
/// A session on the desktop that starts empty, with the Accounts view opened by its digit.
async fn open_accounts(addr: SocketAddr, key: PrivateKey) -> anyhow::Result<TuiSession> {
let session = TuiSession::open(addr, key, (100, 30), (0, 0)).await?;
session.channel.data(&b"1"[..]).await?;
Ok(session)
async fn quit_session(addr: SocketAddr, key: PrivateKey) -> anyhow::Result<String> {
let mut session = TuiSession::open(addr, key, (100, 30), (0, 0)).await?;
let enable = text(MOUSE_ENABLE);
session.read_until(|seen| seen.contains(&enable)).await?;
session.channel.data(QUIT).await?;
session.read_until(|seen| seen.contains("Quit?")).await?;
session.channel.data(CONFIRM).await?;
session.read_to_end().await?;
Ok(session.seen())
async fn wheel_session(addr: SocketAddr, key: PrivateKey) -> anyhow::Result<()> {
let mut session = open_accounts(addr, key).await?;
session.read_until(|seen| seen.contains("charlie")).await?;
for (bytes, redrawn) in [
(WHEEL_DOWN, "bravo"),
(WHEEL_DOWN, "charlie"),
(WHEEL_UP, "bravo"),
] {
session.forget();
session.channel.data(bytes).await?;
session.read_until(|seen| seen.contains(redrawn)).await?;
session
.handle
.disconnect(Disconnect::ByApplication, "done", "")
Ok(())
async fn click_session(addr: SocketAddr, key: PrivateKey) -> anyhow::Result<()> {
for (bytes, redrawn) in [(CLICK_CHARLIE, "charlie"), (CLICK_BRAVO, "bravo")] {
async fn raise_session(addr: SocketAddr, key: PrivateKey) -> anyhow::Result<()> {
session.channel.data(ZOOM_KEY).await?;
session.channel.data(&b"2"[..]).await?;
.read_until(|seen| seen.contains("Transactions"))
session.channel.data(CLICK_REAR_WINDOW).await?;
async fn drag_session(addr: SocketAddr, key: PrivateKey) -> anyhow::Result<()> {
session.channel.data(TITLE_PRESS).await?;
session.channel.data(TITLE_MOTION).await?;
session.channel.data(TITLE_RELEASE).await?;
async fn bar_session(addr: SocketAddr, key: PrivateKey) -> anyhow::Result<()> {
session.read_until(|seen| seen.contains("acct01")).await?;
for (bytes, redrawn) in [(CLICK_DOWN_ARROW, "acct02"), (CLICK_PAGE_DOWN, "acct27")] {
#[local_db_sqlx_test]
async fn the_mouse_modes_are_enabled_on_attach_and_disabled_on_quit(
pool: PgPool,
) -> anyhow::Result<()> {
let seen = with_session(&pool, &[], quit_session).await?;
let (enable, disable) = (text(MOUSE_ENABLE), text(MOUSE_DISABLE));
let on = seen.find(&enable);
let off = seen.rfind(&disable);
assert!(on.is_some() && off.is_some() && on < off, "{seen:?}");
async fn wheel_reports_move_the_accounts_selection(pool: PgPool) -> anyhow::Result<()> {
with_session(&pool, &["alpha", "bravo", "charlie"], wheel_session).await?;
async fn left_clicks_select_the_accounts_row_under_the_pointer(pool: PgPool) -> anyhow::Result<()> {
with_session(&pool, &["alpha", "bravo", "charlie"], click_session).await?;
async fn scrollbar_clicks_move_the_accounts_view(pool: PgPool) -> anyhow::Result<()> {
let names: Vec<String> = (1..=BAR_ACCOUNTS).map(|n| format!("acct{n:02}")).collect();
let names: Vec<&str> = names.iter().map(String::as_str).collect();
with_session(&pool, &names, bar_session).await?;
async fn a_click_on_a_rear_window_raises_it(pool: PgPool) -> anyhow::Result<()> {
with_session(&pool, &["alpha", "bravo", "charlie"], raise_session).await?;
async fn a_title_drag_moves_the_window_and_redraws_it(pool: PgPool) -> anyhow::Result<()> {
with_session(&pool, &["alpha", "bravo", "charlie"], drag_session).await?;