rmux-server 0.1.2

Tokio daemon and request dispatcher for the RMUX terminal multiplexer.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
#![cfg(unix)]

use std::error::Error;
use std::fs;
use std::io;
use std::path::Path;
use std::time::{Duration, Instant};

mod common;

use common::{session_name, start_server, ClientConnection, TestHarness, PTY_TEST_LOCK};
use rmux_proto::{
    AttachMessage, AttachSessionRequest, CapturePaneRequest, DisplayMessageRequest,
    KillSessionRequest, NewSessionRequest, PaneTarget, Request, Response, SelectPaneRequest,
    SendKeysRequest, SendKeysResponse, SplitWindowRequest, SplitWindowTarget, Target, TerminalSize,
};
use tokio::io::AsyncReadExt;

const STEP_TIMEOUT: Duration = Duration::from_secs(15);

async fn read_attach_message(
    stream: &mut tokio::net::UnixStream,
) -> Result<Option<AttachMessage>, Box<dyn Error>> {
    let mut tag = [0_u8; 1];
    let bytes_read = stream.read(&mut tag).await?;
    if bytes_read == 0 {
        return Ok(None);
    }

    match tag[0] {
        1 => {
            let mut length = [0_u8; 4];
            stream.read_exact(&mut length).await?;
            let payload_len = u32::from_le_bytes(length) as usize;
            let mut payload = vec![0_u8; payload_len];
            stream.read_exact(&mut payload).await?;
            Ok(Some(AttachMessage::Data(payload)))
        }
        2 => {
            let mut size = [0_u8; 4];
            stream.read_exact(&mut size).await?;
            Ok(Some(AttachMessage::Resize(rmux_proto::TerminalSize {
                cols: u16::from_le_bytes([size[0], size[1]]),
                rows: u16::from_le_bytes([size[2], size[3]]),
            })))
        }
        other => Err(rmux_proto::RmuxError::Decode(format!(
            "unknown attach-stream message tag {other}"
        ))
        .into()),
    }
}

async fn read_attach_until_contains(
    stream: &mut tokio::net::UnixStream,
    needle: &str,
) -> Result<String, Box<dyn Error>> {
    let deadline = Instant::now() + STEP_TIMEOUT;
    let mut output = String::new();

    while Instant::now() < deadline {
        let remaining = deadline.saturating_duration_since(Instant::now());
        let message = match tokio::time::timeout(remaining, read_attach_message(stream)).await {
            Ok(message) => message?,
            Err(_) => break,
        };

        let Some(message) = message else {
            break;
        };

        if let AttachMessage::Data(bytes) = message {
            output.push_str(&String::from_utf8_lossy(&bytes));
            if output.contains(needle) {
                return Ok(output);
            }
        }
    }

    Err(io::Error::other(format!(
        "timed out waiting for attach output containing {needle:?}: {output:?}"
    ))
    .into())
}

async fn wait_for_file_contents(
    path: &Path,
    expected: &str,
    timeout: Duration,
) -> Result<bool, Box<dyn Error>> {
    let deadline = Instant::now() + timeout;

    while Instant::now() < deadline {
        match fs::read_to_string(path) {
            Ok(contents) if contents == expected => return Ok(true),
            Ok(_) => {}
            Err(error) if error.kind() == io::ErrorKind::NotFound => {}
            Err(error) => return Err(error.into()),
        }

        tokio::time::sleep(Duration::from_millis(25)).await;
    }

    Ok(false)
}

async fn wait_for_pane_current_command(
    client: &mut ClientConnection,
    target: PaneTarget,
    expected: &str,
) -> Result<(), Box<dyn Error>> {
    let deadline = Instant::now() + STEP_TIMEOUT;
    let mut last = String::new();

    while Instant::now() < deadline {
        let response = client
            .send_request(&Request::DisplayMessage(DisplayMessageRequest {
                target: Some(Target::Pane(target.clone())),
                print: true,
                message: Some("#{pane_current_command}".to_owned()),
            }))
            .await?;
        let Response::DisplayMessage(response) = response else {
            return Err(io::Error::other("display-message returned the wrong response").into());
        };
        let output = response
            .command_output()
            .expect("display-message -p returns command output");
        last = String::from_utf8_lossy(output.stdout()).trim().to_owned();
        if last == expected {
            return Ok(());
        }

        tokio::time::sleep(Duration::from_millis(25)).await;
    }

    Err(io::Error::other(format!(
        "timed out waiting for foreground command {expected:?}; last={last:?}"
    ))
    .into())
}

async fn capture_pane_text(
    socket_path: &Path,
    target: PaneTarget,
) -> Result<String, Box<dyn Error>> {
    let response = common::send_request(
        socket_path,
        &Request::CapturePane(CapturePaneRequest {
            target,
            start: None,
            end: None,
            print: true,
            buffer_name: None,
            alternate: false,
            escape_ansi: false,
            escape_sequences: false,
            join_wrapped: false,
            use_mode_screen: false,
            preserve_trailing_spaces: false,
            do_not_trim_spaces: false,
            pending_input: false,
            quiet: false,
            start_is_absolute: false,
            end_is_absolute: false,
        }),
    )
    .await?;
    let output = response
        .command_output()
        .ok_or_else(|| io::Error::other("capture-pane -p returned no command output"))?;
    Ok(String::from_utf8_lossy(output.stdout()).into_owned())
}

async fn wait_for_pane_capture_contains(
    socket_path: &Path,
    target: PaneTarget,
    needle: &str,
) -> Result<String, Box<dyn Error>> {
    let deadline = Instant::now() + STEP_TIMEOUT;

    while Instant::now() < deadline {
        let capture = capture_pane_text(socket_path, target.clone()).await?;
        if capture.contains(needle) {
            return Ok(capture);
        }

        tokio::time::sleep(Duration::from_millis(25)).await;
    }

    Err(io::Error::other(format!(
        "timed out waiting for pane capture containing {needle:?}"
    ))
    .into())
}

fn shell_quote(path: &Path) -> String {
    format!("'{}'", path.display().to_string().replace('\'', "'\\''"))
}

#[tokio::test]
async fn send_keys_writes_to_the_correct_pane_through_the_socket() -> Result<(), Box<dyn Error>> {
    let _guard = PTY_TEST_LOCK.lock().await;
    let harness = TestHarness::new("send-keys");
    let socket_path = harness.socket_path().to_path_buf();
    let handle = start_server(&harness).await?;
    let mut client = ClientConnection::connect(&socket_path).await?;

    let created = client
        .send_request(&Request::NewSession(NewSessionRequest {
            session_name: session_name("alpha"),
            detached: true,
            size: Some(TerminalSize { cols: 80, rows: 24 }),
            environment: None,
        }))
        .await?;
    assert!(matches!(created, Response::NewSession(_)));

    let (_, mut attach_stream) = ClientConnection::connect(&socket_path)
        .await?
        .begin_attach(AttachSessionRequest {
            target: session_name("alpha"),
        })
        .await?;

    let response = client
        .send_request(&Request::SendKeys(SendKeysRequest {
            target: PaneTarget::new(session_name("alpha"), 0),
            keys: vec!["printf send-keys-ok".to_owned(), "Enter".to_owned()],
        }))
        .await?;
    assert_eq!(
        response,
        Response::SendKeys(SendKeysResponse { key_count: 2 })
    );
    let output = read_attach_until_contains(&mut attach_stream, "send-keys-ok").await?;
    assert!(output.contains("send-keys-ok"));

    let empty_response = client
        .send_request(&Request::SendKeys(SendKeysRequest {
            target: PaneTarget::new(session_name("alpha"), 0),
            keys: vec![],
        }))
        .await?;
    assert_eq!(
        empty_response,
        Response::SendKeys(SendKeysResponse { key_count: 0 })
    );

    let missing = client
        .send_request(&Request::SendKeys(SendKeysRequest {
            target: PaneTarget::new(session_name("missing"), 0),
            keys: vec!["x".to_owned()],
        }))
        .await?;
    assert!(matches!(missing, Response::Error(_)));

    let missing_pane = client
        .send_request(&Request::SendKeys(SendKeysRequest {
            target: PaneTarget::new(session_name("alpha"), 99),
            keys: vec!["x".to_owned()],
        }))
        .await?;
    assert!(matches!(missing_pane, Response::Error(_)));

    let empty_missing = client
        .send_request(&Request::SendKeys(SendKeysRequest {
            target: PaneTarget::new(session_name("nonexistent"), 0),
            keys: vec![],
        }))
        .await?;
    assert!(matches!(empty_missing, Response::Error(_)));

    drop(attach_stream);
    let removed = common::send_request(
        &socket_path,
        &Request::KillSession(KillSessionRequest {
            target: session_name("alpha"),
            kill_all_except_target: false,
            clear_alerts: false,
        }),
    )
    .await?;
    assert_eq!(
        removed,
        Response::KillSession(rmux_proto::KillSessionResponse { existed: true })
    );
    handle.shutdown().await?;
    Ok(())
}

#[tokio::test]
async fn send_keys_targets_the_correct_pane_in_a_multi_pane_session() -> Result<(), Box<dyn Error>>
{
    let _guard = PTY_TEST_LOCK.lock().await;
    let harness = TestHarness::new("send-keys-multi-pane");
    let socket_path = harness.socket_path().to_path_buf();
    let handle = start_server(&harness).await?;
    let mut client = ClientConnection::connect(&socket_path).await?;

    let created = client
        .send_request(&Request::NewSession(NewSessionRequest {
            session_name: session_name("beta"),
            detached: true,
            size: Some(TerminalSize {
                cols: 120,
                rows: 40,
            }),
            environment: None,
        }))
        .await?;
    assert!(matches!(created, Response::NewSession(_)));

    let split = client
        .send_request(&Request::SplitWindow(SplitWindowRequest {
            target: SplitWindowTarget::Session(session_name("beta")),
            direction: rmux_proto::SplitDirection::Vertical,
            before: false,
            environment: None,
        }))
        .await?;
    assert!(matches!(split, Response::SplitWindow(_)));

    let pane0_response = client
        .send_request(&Request::SendKeys(SendKeysRequest {
            target: PaneTarget::new(session_name("beta"), 0),
            keys: vec!["printf pane-zero".to_owned(), "Enter".to_owned()],
        }))
        .await?;
    assert_eq!(
        pane0_response,
        Response::SendKeys(SendKeysResponse { key_count: 2 })
    );
    let pane0_output = wait_for_pane_capture_contains(
        &socket_path,
        PaneTarget::new(session_name("beta"), 0),
        "pane-zero",
    )
    .await?;
    assert!(pane0_output.contains("pane-zero"));

    let selected = client
        .send_request(&Request::SelectPane(SelectPaneRequest {
            target: PaneTarget::new(session_name("beta"), 1),
            title: None,
        }))
        .await?;
    assert!(matches!(selected, Response::SelectPane(_)));

    let pane1_response = client
        .send_request(&Request::SendKeys(SendKeysRequest {
            target: PaneTarget::new(session_name("beta"), 1),
            keys: vec!["printf pane-one".to_owned(), "Enter".to_owned()],
        }))
        .await?;
    assert_eq!(
        pane1_response,
        Response::SendKeys(SendKeysResponse { key_count: 2 })
    );
    let pane1_output = wait_for_pane_capture_contains(
        &socket_path,
        PaneTarget::new(session_name("beta"), 1),
        "pane-one",
    )
    .await?;
    assert!(pane1_output.contains("pane-one"));
    let pane0_after_pane1 =
        capture_pane_text(&socket_path, PaneTarget::new(session_name("beta"), 0)).await?;
    assert!(
        !pane0_after_pane1.contains("pane-one"),
        "pane-one output should remain isolated to pane 1, got pane 0 capture {pane0_after_pane1:?}"
    );

    let removed = common::send_request(
        &socket_path,
        &Request::KillSession(KillSessionRequest {
            target: session_name("beta"),
            kill_all_except_target: false,
            clear_alerts: false,
        }),
    )
    .await?;
    assert_eq!(
        removed,
        Response::KillSession(rmux_proto::KillSessionResponse { existed: true })
    );
    handle.shutdown().await?;
    Ok(())
}

#[tokio::test]
async fn send_keys_ctrl_c_interrupts_a_real_pane_process() -> Result<(), Box<dyn Error>> {
    let _guard = PTY_TEST_LOCK.lock().await;
    let harness = TestHarness::new("send-keys-ctrl-c");
    let socket_path = harness.socket_path().to_path_buf();
    let root = socket_path
        .parent()
        .expect("socket path must have a parent");
    let recovery_path = root.join("ctrl-c-recovered.txt");
    let handle = start_server(&harness).await?;
    let mut client = ClientConnection::connect(&socket_path).await?;

    let created = client
        .send_request(&Request::NewSession(NewSessionRequest {
            session_name: session_name("gamma"),
            detached: true,
            size: Some(TerminalSize { cols: 80, rows: 24 }),
            environment: None,
        }))
        .await?;
    assert!(matches!(created, Response::NewSession(_)));

    let (_, mut attach_stream) = ClientConnection::connect(&socket_path)
        .await?
        .begin_attach(AttachSessionRequest {
            target: session_name("gamma"),
        })
        .await?;

    let start_sleep = client
        .send_request(&Request::SendKeys(SendKeysRequest {
            target: PaneTarget::new(session_name("gamma"), 0),
            keys: vec!["sleep 5".to_owned(), "Enter".to_owned()],
        }))
        .await?;
    assert_eq!(
        start_sleep,
        Response::SendKeys(SendKeysResponse { key_count: 2 })
    );
    let sleep_output = read_attach_until_contains(&mut attach_stream, "sleep 5").await?;
    assert!(sleep_output.contains("sleep 5"));
    wait_for_pane_current_command(
        &mut client,
        PaneTarget::new(session_name("gamma"), 0),
        "sleep",
    )
    .await?;

    let interrupt = client
        .send_request(&Request::SendKeys(SendKeysRequest {
            target: PaneTarget::new(session_name("gamma"), 0),
            keys: vec!["C-c".to_owned()],
        }))
        .await?;
    assert_eq!(
        interrupt,
        Response::SendKeys(SendKeysResponse { key_count: 1 })
    );
    let interrupt_output = read_attach_until_contains(&mut attach_stream, "^C").await?;
    assert!(
        interrupt_output.contains("^C"),
        "attach output should include the interrupt echo before recovery, got {interrupt_output:?}"
    );
    tokio::time::sleep(Duration::from_millis(50)).await;

    let recovery_command = format!("printf ctrl-c-recovered > {}", shell_quote(&recovery_path));
    let recovery_deadline = Instant::now() + STEP_TIMEOUT;
    while Instant::now() < recovery_deadline {
        let recovery_response = client
            .send_request(&Request::SendKeys(SendKeysRequest {
                target: PaneTarget::new(session_name("gamma"), 0),
                keys: vec![recovery_command.clone(), "Enter".to_owned()],
            }))
            .await?;
        assert!(matches!(recovery_response, Response::SendKeys(_)));
        if wait_for_file_contents(
            &recovery_path,
            "ctrl-c-recovered",
            Duration::from_millis(500),
        )
        .await?
        {
            break;
        }
    }
    assert_eq!(
        fs::read_to_string(&recovery_path).ok().as_deref(),
        Some("ctrl-c-recovered"),
        "shell should accept input again after ctrl-c"
    );

    drop(attach_stream);
    let removed = common::send_request(
        &socket_path,
        &Request::KillSession(KillSessionRequest {
            target: session_name("gamma"),
            kill_all_except_target: false,
            clear_alerts: false,
        }),
    )
    .await?;
    assert_eq!(
        removed,
        Response::KillSession(rmux_proto::KillSessionResponse { existed: true })
    );
    handle.shutdown().await?;
    Ok(())
}