1
//! The mouse over a real ssh connection: the modes are asked for on attach and given back on
2
//! quit, and SGR wheel reports and clicks move the Accounts selection.
3
//!
4
//! The cursor row is the only one a wheel step restyles, so a row's name is redrawn exactly
5
//! when the cursor enters or leaves 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::command::account::CreateAccount;
14
use server::db::install_test_pool;
15
use sqlx::PgPool;
16
use sqlx::types::Uuid;
17
use std::future::Future;
18
use std::net::SocketAddr;
19
use std::time::Duration;
20
use supp_macro::local_db_sqlx_test;
21
use tui::transport::{MOUSE_DISABLE, MOUSE_ENABLE};
22

            
23
const DEADLINE: Duration = Duration::from_secs(45);
24
const QUIT: &[u8] = b"\x1bx";
25
const CONFIRM: &[u8] = b"y";
26
const WHEEL_DOWN: &[u8] = b"\x1b[<65;5;5M";
27
const WHEEL_UP: &[u8] = b"\x1b[<64;5;5M";
28
/// Left press and release over the third and the second row of the list (1-based).
29
const CLICK_CHARLIE: &[u8] = b"\x1b[<0;5;6M\x1b[<0;5;6m";
30
const CLICK_BRAVO: &[u8] = b"\x1b[<0;5;5M\x1b[<0;5;5m";
31
/// Left press and release on the list's scrollbar (the last column, 1-based): the down arrow on
32
/// the last row of the track, then the page area below the thumb.
33
const CLICK_DOWN_ARROW: &[u8] = b"\x1b[<0;100;28M\x1b[<0;100;28m";
34
const CLICK_PAGE_DOWN: &[u8] = b"\x1b[<0;100;21M\x1b[<0;100;21m";
35
/// F5 unzooms the front window; the click is on the left column of the Accounts window behind
36
/// the Transactions one, which hides its rows until it is raised.
37
const ZOOM_KEY: &[u8] = b"\x1b[15~";
38
const CLICK_REAR_WINDOW: &[u8] = b"\x1b[<0;1;6M\x1b[<0;1;6m";
39
/// Press on the title row of the zoomed window (1-based), then motion with the left button held
40
/// and the release: the window unzooms and moves, so its rows are drawn again.
41
const TITLE_PRESS: &[u8] = b"\x1b[<0;41;2M";
42
const TITLE_MOTION: &[u8] = b"\x1b[<32;46;6M";
43
const TITLE_RELEASE: &[u8] = b"\x1b[<0;46;6m";
44
const BAR_ACCOUNTS: usize = 40;
45

            
46
3
fn text(bytes: &[u8]) -> String {
47
3
    String::from_utf8_lossy(bytes).into_owned()
48
3
}
49

            
50
/// A daemon and a registered key for a user owning the accounts `names`, and the session
51
/// `run` drives against them.
52
6
async fn with_session<F, Fut, T>(pool: &PgPool, names: &[&str], run: F) -> anyhow::Result<T>
53
6
where
54
6
    F: FnOnce(SocketAddr, PrivateKey) -> Fut,
55
6
    Fut: Future<Output = anyhow::Result<T>>,
56
6
{
57
6
    let user_id = Uuid::new_v4();
58
6
    insert_verified_user(pool, user_id).await?;
59
6
    for name in names {
60
52
        CreateAccount::new()
61
52
            .name((*name).to_string())
62
52
            .user_id(user_id)
63
52
            .run()
64
52
            .await?;
65
    }
66
6
    let key = random_key()?;
67
6
    register_key(user_id, &key).await?;
68
6
    let daemon: Daemon = start_daemon().await?;
69
6
    let outcome = tokio::time::timeout(DEADLINE, run(daemon.addr, key)).await;
70
6
    daemon.stop().await?;
71
6
    outcome?
72
6
}
73

            
74
/// A session on the desktop that starts empty, with the Accounts view opened by its digit.
75
5
async fn open_accounts(addr: SocketAddr, key: PrivateKey) -> anyhow::Result<TuiSession> {
76
5
    let session = TuiSession::open(addr, key, (100, 30), (0, 0)).await?;
77
5
    session.channel.data(&b"1"[..]).await?;
78
5
    Ok(session)
79
5
}
80

            
81
1
async fn quit_session(addr: SocketAddr, key: PrivateKey) -> anyhow::Result<String> {
82
1
    let mut session = TuiSession::open(addr, key, (100, 30), (0, 0)).await?;
83
1
    let enable = text(MOUSE_ENABLE);
84
3
    session.read_until(|seen| seen.contains(&enable)).await?;
85
1
    session.channel.data(QUIT).await?;
86
3
    session.read_until(|seen| seen.contains("Quit?")).await?;
87
1
    session.channel.data(CONFIRM).await?;
88
1
    session.read_to_end().await?;
89
1
    Ok(session.seen())
90
1
}
91

            
92
1
async fn wheel_session(addr: SocketAddr, key: PrivateKey) -> anyhow::Result<()> {
93
1
    let mut session = open_accounts(addr, key).await?;
94
6
    session.read_until(|seen| seen.contains("charlie")).await?;
95
1
    for (bytes, redrawn) in [
96
1
        (WHEEL_DOWN, "bravo"),
97
1
        (WHEEL_DOWN, "charlie"),
98
1
        (WHEEL_UP, "bravo"),
99
1
    ] {
100
3
        session.forget();
101
3
        session.channel.data(bytes).await?;
102
7
        session.read_until(|seen| seen.contains(redrawn)).await?;
103
    }
104
1
    session
105
1
        .handle
106
1
        .disconnect(Disconnect::ByApplication, "done", "")
107
1
        .await?;
108
1
    Ok(())
109
1
}
110

            
111
1
async fn click_session(addr: SocketAddr, key: PrivateKey) -> anyhow::Result<()> {
112
1
    let mut session = open_accounts(addr, key).await?;
113
6
    session.read_until(|seen| seen.contains("charlie")).await?;
114
1
    for (bytes, redrawn) in [(CLICK_CHARLIE, "charlie"), (CLICK_BRAVO, "bravo")] {
115
2
        session.forget();
116
2
        session.channel.data(bytes).await?;
117
5
        session.read_until(|seen| seen.contains(redrawn)).await?;
118
    }
119
1
    session
120
1
        .handle
121
1
        .disconnect(Disconnect::ByApplication, "done", "")
122
1
        .await?;
123
1
    Ok(())
124
1
}
125

            
126
1
async fn raise_session(addr: SocketAddr, key: PrivateKey) -> anyhow::Result<()> {
127
1
    let mut session = open_accounts(addr, key).await?;
128
6
    session.read_until(|seen| seen.contains("charlie")).await?;
129
1
    session.channel.data(ZOOM_KEY).await?;
130
1
    session.channel.data(&b"2"[..]).await?;
131
1
    session
132
1
        .read_until(|seen| seen.contains("Transactions"))
133
1
        .await?;
134
1
    session.forget();
135
1
    session.channel.data(CLICK_REAR_WINDOW).await?;
136
5
    session.read_until(|seen| seen.contains("charlie")).await?;
137
1
    session
138
1
        .handle
139
1
        .disconnect(Disconnect::ByApplication, "done", "")
140
1
        .await?;
141
1
    Ok(())
142
1
}
143

            
144
1
async fn drag_session(addr: SocketAddr, key: PrivateKey) -> anyhow::Result<()> {
145
1
    let mut session = open_accounts(addr, key).await?;
146
6
    session.read_until(|seen| seen.contains("charlie")).await?;
147
1
    session.channel.data(TITLE_PRESS).await?;
148
1
    session.forget();
149
1
    session.channel.data(TITLE_MOTION).await?;
150
4
    session.read_until(|seen| seen.contains("charlie")).await?;
151
1
    session.channel.data(TITLE_RELEASE).await?;
152
1
    session
153
1
        .handle
154
1
        .disconnect(Disconnect::ByApplication, "done", "")
155
1
        .await?;
156
1
    Ok(())
157
1
}
158

            
159
1
async fn bar_session(addr: SocketAddr, key: PrivateKey) -> anyhow::Result<()> {
160
1
    let mut session = open_accounts(addr, key).await?;
161
6
    session.read_until(|seen| seen.contains("acct01")).await?;
162
1
    for (bytes, redrawn) in [(CLICK_DOWN_ARROW, "acct02"), (CLICK_PAGE_DOWN, "acct27")] {
163
2
        session.forget();
164
2
        session.channel.data(bytes).await?;
165
6
        session.read_until(|seen| seen.contains(redrawn)).await?;
166
    }
167
1
    session
168
1
        .handle
169
1
        .disconnect(Disconnect::ByApplication, "done", "")
170
1
        .await?;
171
1
    Ok(())
172
1
}
173

            
174
#[local_db_sqlx_test]
175
async fn the_mouse_modes_are_enabled_on_attach_and_disabled_on_quit(
176
    pool: PgPool,
177
) -> anyhow::Result<()> {
178
    let seen = with_session(&pool, &[], quit_session).await?;
179
    let (enable, disable) = (text(MOUSE_ENABLE), text(MOUSE_DISABLE));
180
    let on = seen.find(&enable);
181
    let off = seen.rfind(&disable);
182
    assert!(on.is_some() && off.is_some() && on < off, "{seen:?}");
183
}
184

            
185
#[local_db_sqlx_test]
186
async fn wheel_reports_move_the_accounts_selection(pool: PgPool) -> anyhow::Result<()> {
187
    with_session(&pool, &["alpha", "bravo", "charlie"], wheel_session).await?;
188
}
189

            
190
#[local_db_sqlx_test]
191
async fn left_clicks_select_the_accounts_row_under_the_pointer(pool: PgPool) -> anyhow::Result<()> {
192
    with_session(&pool, &["alpha", "bravo", "charlie"], click_session).await?;
193
}
194

            
195
#[local_db_sqlx_test]
196
async fn scrollbar_clicks_move_the_accounts_view(pool: PgPool) -> anyhow::Result<()> {
197
40
    let names: Vec<String> = (1..=BAR_ACCOUNTS).map(|n| format!("acct{n:02}")).collect();
198
    let names: Vec<&str> = names.iter().map(String::as_str).collect();
199
    with_session(&pool, &names, bar_session).await?;
200
}
201

            
202
#[local_db_sqlx_test]
203
async fn a_click_on_a_rear_window_raises_it(pool: PgPool) -> anyhow::Result<()> {
204
    with_session(&pool, &["alpha", "bravo", "charlie"], raise_session).await?;
205
}
206

            
207
#[local_db_sqlx_test]
208
async fn a_title_drag_moves_the_window_and_redraws_it(pool: PgPool) -> anyhow::Result<()> {
209
    with_session(&pool, &["alpha", "bravo", "charlie"], drag_session).await?;
210
}