rmux-server 0.1.0

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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
use super::super::{overlay_support::ClientOverlayState, RequestHandler};
use super::session_name;
use crate::mouse::{layout_for_session, StatusRangeType};
use crate::pane_io::AttachControl;
use rmux_proto::{
    BindKeyRequest, CapturePaneRequest, NewSessionExtRequest, NewSessionRequest, PaneTarget,
    Request, Response, SessionName, Target, TerminalSize, WindowTarget, DEFAULT_MAX_FRAME_LENGTH,
};
use tokio::sync::mpsc;
use tokio::time::{timeout, Duration};

async fn create_attached_session(
    handler: &RequestHandler,
    name: &SessionName,
    requester_pid: u32,
) -> mpsc::UnboundedReceiver<AttachControl> {
    let created = handler
        .handle(Request::NewSession(NewSessionRequest {
            session_name: name.clone(),
            detached: true,
            size: Some(TerminalSize { cols: 80, rows: 24 }),
            environment: None,
        }))
        .await;
    assert!(matches!(created, Response::NewSession(_)));

    let (control_tx, control_rx) = mpsc::unbounded_channel();
    let _attach_id = handler
        .register_attach(requester_pid, name.clone(), control_tx)
        .await;
    control_rx
}

async fn create_quiet_attached_session(
    handler: &RequestHandler,
    name: &SessionName,
    requester_pid: u32,
) -> mpsc::UnboundedReceiver<AttachControl> {
    let response = handler
        .handle(Request::NewSessionExt(NewSessionExtRequest {
            session_name: Some(name.clone()),
            working_directory: None,
            detached: true,
            size: Some(TerminalSize { cols: 80, rows: 24 }),
            environment: None,
            group_target: None,
            attach_if_exists: false,
            detach_other_clients: false,
            kill_other_clients: false,
            flags: None,
            window_name: None,
            print_session_info: false,
            print_format: None,
            command: Some(quiet_overlay_command()),
        }))
        .await;
    assert!(
        matches!(response, Response::NewSession(_)),
        "quiet overlay test session should be created, got {response:?}"
    );

    let (control_tx, control_rx) = mpsc::unbounded_channel();
    let _attach_id = handler
        .register_attach(requester_pid, name.clone(), control_tx)
        .await;
    control_rx
}

#[cfg(windows)]
fn quiet_overlay_command() -> Vec<String> {
    let system_root =
        std::env::var_os("SystemRoot").unwrap_or_else(|| std::ffi::OsString::from(r"C:\Windows"));
    let cmd = std::path::PathBuf::from(system_root)
        .join("System32")
        .join("cmd.exe");
    vec![
        cmd.to_string_lossy().into_owned(),
        "/d".to_owned(),
        "/q".to_owned(),
        "/c".to_owned(),
        "ping -n 120 127.0.0.1 >NUL".to_owned(),
    ]
}

#[cfg(unix)]
fn quiet_overlay_command() -> Vec<String> {
    ["/bin/sh", "-c", "sleep 60"]
        .into_iter()
        .map(str::to_owned)
        .collect()
}

async fn run_overlay_command(handler: &RequestHandler, requester_pid: u32, command: &str) {
    let parsed = handler
        .parse_control_commands(command)
        .await
        .expect("overlay command parses");
    let result = handler
        .execute_parsed_commands_for_test(requester_pid, parsed)
        .await
        .expect("overlay command executes");
    assert!(result.stdout().is_empty());
}

async fn next_overlay_frame(
    control_rx: &mut mpsc::UnboundedReceiver<AttachControl>,
) -> crate::pane_io::OverlayFrame {
    match timeout(Duration::from_secs(1), control_rx.recv())
        .await
        .expect("overlay control message arrives")
    {
        Some(AttachControl::Overlay(frame)) => frame,
        other => panic!("expected overlay frame, got {other:?}"),
    }
}

async fn capture_pane_print(handler: &RequestHandler, target: PaneTarget) -> String {
    let response = handler
        .handle(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 Response::CapturePane(response) = response else {
        panic!("expected capture-pane response, got {response:?}");
    };
    let output = response
        .output
        .expect("capture-pane -p should return command output");
    String::from_utf8(output.stdout().to_vec()).expect("capture-pane stdout is utf-8")
}

fn sgr_mouse(button: u16, x: u16, y: u16) -> Vec<u8> {
    format!(
        "\x1b[<{button};{};{}M",
        x.saturating_add(1),
        y.saturating_add(1)
    )
    .into_bytes()
}

#[tokio::test]
async fn display_menu_keyboard_navigation_wraps_around_separators() {
    let handler = RequestHandler::new();
    let alpha = session_name("alpha");
    let requester_pid = std::process::id();
    let mut control_rx = create_attached_session(&handler, &alpha, requester_pid).await;

    run_overlay_command(
        &handler,
        requester_pid,
        r#"display-menu -T Menu "First" "f" "display-message first" "" "" "" "Second" "s" "display-message second""#,
    )
    .await;

    let frame = next_overlay_frame(&mut control_rx).await;
    assert!(frame.persistent);
    let rendered = String::from_utf8(frame.frame).expect("menu frame is utf-8");
    assert!(rendered.contains("First"));
    assert!(rendered.contains("Second"));

    {
        let active_attach = handler.active_attach.lock().await;
        let active = active_attach
            .by_pid
            .get(&requester_pid)
            .expect("attached client");
        let Some(ClientOverlayState::Menu(menu)) = active.overlay.as_ref() else {
            panic!("expected a root menu overlay");
        };
        assert_eq!(menu.choice, Some(0));
    }

    handler
        .handle_attached_live_input_for_test(requester_pid, b"\x0e")
        .await
        .expect("menu navigation");
    {
        let active_attach = handler.active_attach.lock().await;
        let active = active_attach
            .by_pid
            .get(&requester_pid)
            .expect("attached client");
        let Some(ClientOverlayState::Menu(menu)) = active.overlay.as_ref() else {
            panic!("expected a root menu overlay");
        };
        assert_eq!(menu.choice, Some(2));
    }

    handler
        .handle_attached_live_input_for_test(requester_pid, b"\x0e")
        .await
        .expect("menu wrap");
    {
        let active_attach = handler.active_attach.lock().await;
        let active = active_attach
            .by_pid
            .get(&requester_pid)
            .expect("attached client");
        let Some(ClientOverlayState::Menu(menu)) = active.overlay.as_ref() else {
            panic!("expected a root menu overlay");
        };
        assert_eq!(menu.choice, Some(0));
    }

    handler
        .handle_attached_live_input_for_test(requester_pid, b"\r")
        .await
        .expect("menu choose");
    let active_attach = handler.active_attach.lock().await;
    let active = active_attach
        .by_pid
        .get(&requester_pid)
        .expect("attached client");
    assert!(active.overlay.is_none());
}

#[tokio::test]
async fn display_menu_unterminated_sgr_mouse_input_is_bounded() {
    let handler = RequestHandler::new();
    let alpha = session_name("alpha");
    let requester_pid = std::process::id();
    let mut control_rx = create_attached_session(&handler, &alpha, requester_pid).await;

    run_overlay_command(
        &handler,
        requester_pid,
        r#"display-menu -T Menu "First" "f" "display-message first""#,
    )
    .await;
    let _ = next_overlay_frame(&mut control_rx).await;

    let mut pending_input = Vec::new();
    handler
        .handle_attached_live_input(requester_pid, &mut pending_input, b"\x1b[<")
        .await
        .expect("partial menu mouse is retained");
    assert_eq!(pending_input, b"\x1b[<");

    let overflow = vec![b'9'; DEFAULT_MAX_FRAME_LENGTH - pending_input.len() + 1];
    let err = handler
        .handle_attached_live_input(requester_pid, &mut pending_input, &overflow)
        .await
        .expect_err("unterminated menu mouse should be bounded");
    assert_eq!(err.kind(), std::io::ErrorKind::InvalidData);
    assert!(err.to_string().contains("menu overlay mouse"));
    assert!(pending_input.is_empty());
}

#[tokio::test]
async fn display_menu_partial_utf8_input_is_retained_and_recovered() {
    let handler = RequestHandler::new();
    let alpha = session_name("alpha");
    let requester_pid = std::process::id();
    let mut control_rx = create_quiet_attached_session(&handler, &alpha, requester_pid).await;
    let target = PaneTarget::new(alpha.clone(), 0);
    let before_capture = capture_pane_print(&handler, target.clone()).await;

    run_overlay_command(
        &handler,
        requester_pid,
        r#"display-menu -T Menu "First" "f" "display-message first""#,
    )
    .await;
    let _ = next_overlay_frame(&mut control_rx).await;

    let mut pending_input = Vec::new();
    handler
        .handle_attached_live_input(requester_pid, &mut pending_input, &[0xe6])
        .await
        .expect("partial menu UTF-8 is retained");
    assert_eq!(
        pending_input,
        vec![0xe6],
        "menu overlay should retain only the partial UTF-8 fragment"
    );
    assert_eq!(
        capture_pane_print(&handler, target.clone()).await,
        before_capture,
        "partial menu prompt UTF-8 must not leak to the pane"
    );

    handler
        .handle_attached_live_input(requester_pid, &mut pending_input, b"\x97\xa5")
        .await
        .expect("completed menu UTF-8 is handled");
    assert!(
        pending_input.is_empty(),
        "completed menu UTF-8 input should leave no retained bytes"
    );
    let active_attach = handler.active_attach.lock().await;
    let active = active_attach
        .by_pid
        .get(&requester_pid)
        .expect("attached client");
    assert!(
        matches!(active.overlay.as_ref(), Some(ClientOverlayState::Menu(_))),
        "completed non-matching menu input should not leave retained bytes or collapse the menu"
    );
}

#[tokio::test]
async fn display_menu_extended_key_partial_is_bounded_without_pane_leak() {
    let handler = RequestHandler::new();
    let alpha = session_name("alpha");
    let requester_pid = std::process::id();
    let mut control_rx = create_quiet_attached_session(&handler, &alpha, requester_pid).await;
    let target = PaneTarget::new(alpha.clone(), 0);
    let before_capture = capture_pane_print(&handler, target.clone()).await;

    run_overlay_command(
        &handler,
        requester_pid,
        r#"display-menu -T Menu "First" "f" "display-message first""#,
    )
    .await;
    let _ = next_overlay_frame(&mut control_rx).await;

    let mut pending_input = Vec::new();
    handler
        .handle_attached_live_input(requester_pid, &mut pending_input, b"\x1b[27;2;65")
        .await
        .expect("partial menu extended key is retained");
    assert_eq!(pending_input, b"\x1b[27;2;65");
    assert_eq!(
        capture_pane_print(&handler, target.clone()).await,
        before_capture,
        "partial menu prompt extended key must not leak to the pane"
    );

    let oversized = vec![b'9'; DEFAULT_MAX_FRAME_LENGTH - pending_input.len() + 1];
    let err = handler
        .handle_attached_live_input(requester_pid, &mut pending_input, &oversized)
        .await
        .expect_err("oversized partial menu extended key should be bounded");
    assert_eq!(err.kind(), std::io::ErrorKind::InvalidData);
    assert!(err.to_string().contains("menu overlay prompt input"));
    assert!(
        pending_input.is_empty(),
        "overflowing menu prompt input should be cleared after rejection"
    );
    assert_eq!(
        capture_pane_print(&handler, target.clone()).await,
        before_capture,
        "rejected oversized menu prompt input must not leak to the pane"
    );

    handler
        .handle_attached_live_input(requester_pid, &mut pending_input, b"9")
        .await
        .expect("menu remains usable after partial-input rejection");
    assert!(pending_input.is_empty());
    assert_eq!(
        capture_pane_print(&handler, target).await,
        before_capture,
        "post-rejection menu input must not leak to the pane"
    );
}

#[tokio::test]
async fn popup_right_click_opens_nested_menu_and_escape_closes_layers() {
    let handler = RequestHandler::new();
    let alpha = session_name("alpha");
    let requester_pid = std::process::id();
    let mut control_rx = create_attached_session(&handler, &alpha, requester_pid).await;

    run_overlay_command(
        &handler,
        requester_pid,
        r#"display-popup -N -T Popup -w 20 -h 6 -x C -y C"#,
    )
    .await;

    let frame = next_overlay_frame(&mut control_rx).await;
    assert!(frame.persistent);
    let rendered = String::from_utf8(frame.frame).expect("popup frame is utf-8");
    assert!(rendered.contains("Popup"));

    let rect = {
        let active_attach = handler.active_attach.lock().await;
        let active = active_attach
            .by_pid
            .get(&requester_pid)
            .expect("attached client");
        let Some(ClientOverlayState::Popup(popup)) = active.overlay.as_ref() else {
            panic!("expected popup overlay");
        };
        popup.rect
    };

    handler
        .handle_attached_live_input_for_test(requester_pid, &sgr_mouse(2, rect.x, rect.y))
        .await
        .expect("popup menu mouse");
    {
        let active_attach = handler.active_attach.lock().await;
        let active = active_attach
            .by_pid
            .get(&requester_pid)
            .expect("attached client");
        let Some(ClientOverlayState::Popup(popup)) = active.overlay.as_ref() else {
            panic!("expected popup overlay");
        };
        assert!(popup.nested_menu.is_some());
    }

    handler
        .handle_attached_live_input_for_test(requester_pid, b"\x1b")
        .await
        .expect("close nested menu");
    {
        let active_attach = handler.active_attach.lock().await;
        let active = active_attach
            .by_pid
            .get(&requester_pid)
            .expect("attached client");
        let Some(ClientOverlayState::Popup(popup)) = active.overlay.as_ref() else {
            panic!("expected popup overlay");
        };
        assert!(popup.nested_menu.is_none());
    }

    handler
        .handle_attached_live_input_for_test(requester_pid, b"\x1b")
        .await
        .expect("close popup");
    let active_attach = handler.active_attach.lock().await;
    let active = active_attach
        .by_pid
        .get(&requester_pid)
        .expect("attached client");
    assert!(active.overlay.is_none());
}

#[tokio::test]
async fn status_right_click_routes_window_menu_to_clicked_window_target() {
    let handler = RequestHandler::new();
    let alpha = session_name("alpha");
    let requester_pid = std::process::id();
    let mut control_rx = create_attached_session(&handler, &alpha, requester_pid).await;
    let rebound = handler
        .handle(Request::BindKey(BindKeyRequest {
            table_name: "root".to_owned(),
            key: "MouseDown3Status".to_owned(),
            note: Some("overlay-status-menu".to_owned()),
            repeat: false,
            command: Some(vec![
                "display-menu".to_owned(),
                "-x".to_owned(),
                "W".to_owned(),
                "-y".to_owned(),
                "W".to_owned(),
                "-T".to_owned(),
                "#{window_index}:#{window_name}".to_owned(),
                "Inspect".to_owned(),
                "i".to_owned(),
                "display-message inspect".to_owned(),
            ]),
        }))
        .await;
    assert!(matches!(rebound, Response::BindKey(_)));

    let (click_x, click_y) = {
        let state = handler.state.lock().await;
        let layout = layout_for_session(&state, &alpha, 1).expect("mouse layout");
        let status = layout.status.as_ref().expect("status layout");
        let range = status
            .ranges
            .iter()
            .find(|range| matches!(range.kind, StatusRangeType::Window(_)))
            .expect("window status range");
        (
            *range.x.start(),
            layout.status_at.expect("status line position"),
        )
    };

    handler
        .handle_attached_live_input_for_test(requester_pid, &sgr_mouse(2, click_x, click_y))
        .await
        .expect("status mouse input");

    let frame = next_overlay_frame(&mut control_rx).await;
    assert!(frame.persistent);
    let rendered = String::from_utf8(frame.frame).expect("window menu frame is utf-8");
    assert!(rendered.contains("Inspect"));

    let active_attach = handler.active_attach.lock().await;
    let active = active_attach
        .by_pid
        .get(&requester_pid)
        .expect("attached client");
    let Some(ClientOverlayState::Menu(menu)) = active.overlay.as_ref() else {
        panic!("expected a status menu overlay");
    };
    assert_eq!(
        menu.current_target,
        Target::Window(WindowTarget::with_window(alpha, 0))
    );
}