rmux-server 0.10.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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
use super::*;

#[tokio::test]
async fn attached_session_mutations_emit_refresh_switches() {
    let handler = RequestHandler::new();
    let requester_pid = std::process::id();
    let alpha = session_name("alpha");

    let created = handler
        .handle(Request::NewSession(NewSessionRequest {
            session_name: alpha.clone(),
            detached: true,
            size: Some(TerminalSize {
                cols: 120,
                rows: 40,
            }),
            environment: None,
        }))
        .await;
    assert!(matches!(created, Response::NewSession(_)));

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

    let split = handler
        .handle(Request::SplitWindow(SplitWindowRequest {
            target: SplitWindowTarget::Session(alpha.clone()),
            direction: rmux_proto::SplitDirection::Horizontal,
            before: false,
            environment: None,
        }))
        .await;
    assert_eq!(
        split,
        Response::SplitWindow(rmux_proto::SplitWindowResponse {
            pane: PaneTarget::new(alpha.clone(), 1),
        })
    );
    let split_frame = recv_render_frame(&mut control_rx, "split refresh").await;
    assert!(split_frame.contains('│'));

    let resized = handler
        .handle(Request::ResizePane(rmux_proto::ResizePaneRequest {
            target: PaneTarget::new(alpha.clone(), 0),
            adjustment: ResizePaneAdjustment::AbsoluteWidth { columns: 34 },
        }))
        .await;
    assert_eq!(
        resized,
        Response::ResizePane(rmux_proto::ResizePaneResponse {
            target: PaneTarget::new(alpha.clone(), 0),
            adjustment: ResizePaneAdjustment::AbsoluteWidth { columns: 34 },
        })
    );
    let resize_frame = recv_render_frame(&mut control_rx, "resize refresh").await;
    assert!(resize_frame.contains('│'));

    let selected_layout = handler
        .handle(Request::SelectLayout(SelectLayoutRequest {
            target: SelectLayoutTarget::Session(alpha.clone()),
            layout: LayoutName::MainVertical,
        }))
        .await;
    assert_eq!(
        selected_layout,
        Response::SelectLayout(rmux_proto::SelectLayoutResponse {
            layout: LayoutName::MainVertical,
        })
    );
    let layout_frame = recv_render_frame(&mut control_rx, "layout refresh").await;
    assert!(layout_frame.contains('│'));

    let selected_pane = handler
        .handle(Request::SelectPane(Box::new(SelectPaneRequest {
            target: PaneTarget::new(alpha, 1),
            title: None,
            style: None,
            input_disabled: None,
            preserve_zoom: false,
        })))
        .await;
    assert_eq!(
        selected_pane,
        Response::SelectPane(rmux_proto::SelectPaneResponse {
            target: PaneTarget::new(session_name("alpha"), 1),
        })
    );
    let select_frame = recv_render_frame(&mut control_rx, "pane refresh").await;
    assert!(select_frame.contains('│'));
    assert!(matches!(control_rx.try_recv(), Err(TryRecvError::Empty)));
}

#[tokio::test]
async fn switch_client_updates_the_tracked_session_for_follow_up_refreshes() {
    let handler = RequestHandler::new();
    let requester_pid = std::process::id();
    let alpha = session_name("alpha");
    let beta = session_name("beta");

    for session_name in [alpha.clone(), beta.clone()] {
        let created = handler
            .handle(Request::NewSession(NewSessionRequest {
                session_name,
                detached: true,
                size: Some(TerminalSize { cols: 80, rows: 24 }),
                environment: None,
            }))
            .await;
        assert!(matches!(created, Response::NewSession(_)));
    }

    let split = handler
        .handle(Request::SplitWindow(SplitWindowRequest {
            target: SplitWindowTarget::Session(beta.clone()),
            direction: rmux_proto::SplitDirection::Horizontal,
            before: false,
            environment: None,
        }))
        .await;
    assert!(matches!(split, Response::SplitWindow(_)));

    let (control_tx, mut control_rx) = mpsc::unbounded_channel();
    let _attach_id = handler
        .register_attach(requester_pid, alpha, control_tx)
        .await;

    let switched = handler
        .handle(Request::SwitchClient(SwitchClientRequest {
            target: beta.clone(),
        }))
        .await;
    assert_eq!(
        switched,
        Response::SwitchClient(rmux_proto::SwitchClientResponse {
            session_name: beta.clone(),
        })
    );
    let switch_frame = recv_render_frame(&mut control_rx, "switch refresh").await;
    assert!(switch_frame.contains('│'));

    let global_border = handler
        .handle(Request::SetOption(SetOptionRequest {
            scope: ScopeSelector::Global,
            option: OptionName::PaneActiveBorderStyle,
            value: "red".to_owned(),
            mode: SetOptionMode::Replace,
        }))
        .await;
    assert!(matches!(global_border, Response::SetOption(_)));
    let global_frame = recv_render_frame(&mut control_rx, "global refresh").await;
    assert!(global_frame.contains("\u{1b}[31m"));

    let beta_window = WindowTarget::with_window(beta.clone(), 0);
    let session_border = handler
        .handle(Request::SetOption(SetOptionRequest {
            scope: ScopeSelector::Window(beta_window),
            option: OptionName::PaneActiveBorderStyle,
            value: "blue".to_owned(),
            mode: SetOptionMode::Replace,
        }))
        .await;
    assert!(matches!(session_border, Response::SetOption(_)));
    let session_frame = recv_render_frame(&mut control_rx, "session refresh").await;
    assert!(session_frame.contains("\u{1b}[34m"));

    let alpha_window = WindowTarget::with_window(session_name("alpha"), 0);
    let other_session = handler
        .handle(Request::SetOption(SetOptionRequest {
            scope: ScopeSelector::Window(alpha_window),
            option: OptionName::PaneBorderStyle,
            value: "green".to_owned(),
            mode: SetOptionMode::Replace,
        }))
        .await;
    assert!(matches!(other_session, Response::SetOption(_)));
    assert!(matches!(control_rx.try_recv(), Err(TryRecvError::Empty)));
}

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

    let created = handler
        .handle(Request::NewSession(NewSessionRequest {
            session_name: beta.clone(),
            detached: true,
            size: Some(TerminalSize { cols: 80, rows: 24 }),
            environment: None,
        }))
        .await;
    assert!(matches!(created, Response::NewSession(_)));
    drain_attach_controls(&mut control_rx);

    let switched = handler
        .dispatch(
            requester_pid,
            Request::SwitchClient(SwitchClientRequest {
                target: beta.clone(),
            }),
        )
        .await
        .response;
    assert_eq!(
        switched,
        Response::SwitchClient(rmux_proto::SwitchClientResponse { session_name: beta })
    );
    let _ = recv_matching_attach_control(&mut control_rx, "switch to beta", |control| {
        matches!(control, AttachControl::Switch(_))
    })
    .await;
    drain_attach_controls(&mut control_rx);

    handler
        .handle_attached_live_input_for_test(requester_pid, b"\x02s")
        .await
        .expect("prefix s opens choose-tree after switch-client");

    let overlay = recv_overlay_frame(&mut control_rx, "choose-tree after switch-client").await;
    assert!(
        overlay.contains("sort:") && overlay.contains("alpha") && overlay.contains("beta"),
        "choose-tree should render sessions after CLI switch-client, got: {overlay:?}"
    );
}

#[tokio::test]
async fn terminal_feature_mutations_refresh_attached_targets_with_client_context() {
    let handler = RequestHandler::new();
    let requester_pid = 42;
    let alpha = session_name("alpha");

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

    let (control_tx, mut control_rx) = mpsc::unbounded_channel();
    let _attach_id = handler
        .register_attach_with_terminal_context(
            requester_pid,
            alpha,
            control_tx,
            OuterTerminalContext::from_pairs(&[("TERM", "xterm-256color")]),
        )
        .await;

    let set = handler
        .handle(Request::SetOption(SetOptionRequest {
            scope: ScopeSelector::Global,
            option: OptionName::TerminalFeatures,
            value: "xterm*:sync".to_owned(),
            mode: SetOptionMode::Append,
        }))
        .await;
    assert!(matches!(set, Response::SetOption(_)));

    let target = recv_switch_target(&mut control_rx, "terminal feature refresh").await;
    assert!(target.outer_terminal.features_string().contains("sync"));
    assert!(target
        .outer_terminal
        .wrap_render_frame(&target.render_frame)
        .starts_with(b"\x1b[?2026h"));
}

#[tokio::test]
async fn allow_passthrough_mutations_refresh_attached_targets() {
    let handler = RequestHandler::new();
    let requester_pid = 43;
    let alpha = session_name("alpha");

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

    let (control_tx, mut control_rx) = mpsc::unbounded_channel();
    let _attach_id = handler
        .register_attach_with_terminal_context(
            requester_pid,
            alpha,
            control_tx,
            OuterTerminalContext::from_pairs(&[("TERM", "xterm-kitty")]),
        )
        .await;

    let set = handler
        .handle(Request::SetOption(SetOptionRequest {
            scope: ScopeSelector::Global,
            option: OptionName::AllowPassthrough,
            value: "on".to_owned(),
            mode: SetOptionMode::Replace,
        }))
        .await;
    assert!(matches!(set, Response::SetOption(_)));

    let target = recv_switch_target(&mut control_rx, "passthrough refresh").await;
    assert!(
        target.kitty_graphics_passthrough,
        "allow-passthrough changes must recompute the attach target gate"
    );
    assert!(
        !target.sixel_passthrough,
        "kitty-only terminals must not enable sixel passthrough"
    );
}

#[tokio::test]
async fn allow_passthrough_enables_sixel_for_sixel_terminals() {
    let handler = RequestHandler::new();
    let requester_pid = 43;
    let alpha = session_name("alpha");

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

    let (control_tx, mut control_rx) = mpsc::unbounded_channel();
    let _attach_id = handler
        .register_attach_with_terminal_context(
            requester_pid,
            alpha,
            control_tx,
            OuterTerminalContext::from_pairs(&[("TERM", "foot")]),
        )
        .await;

    let set = handler
        .handle(Request::SetOption(SetOptionRequest {
            scope: ScopeSelector::Global,
            option: OptionName::AllowPassthrough,
            value: "on".to_owned(),
            mode: SetOptionMode::Replace,
        }))
        .await;
    assert!(matches!(set, Response::SetOption(_)));

    let target = recv_switch_target(&mut control_rx, "passthrough refresh").await;
    assert!(
        target.sixel_passthrough,
        "allow-passthrough should enable sixel passthrough on sixel terminals"
    );
}

#[tokio::test]
async fn allow_passthrough_all_shares_the_active_pane_gate() {
    let handler = RequestHandler::new();
    let requester_pid = 43;
    let alpha = session_name("alpha");

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

    let (control_tx, mut control_rx) = mpsc::unbounded_channel();
    let _attach_id = handler
        .register_attach_with_terminal_context(
            requester_pid,
            alpha,
            control_tx,
            OuterTerminalContext::from_pairs(&[("TERM", "xterm-kitty")]),
        )
        .await;

    let set = handler
        .handle(Request::SetOption(SetOptionRequest {
            scope: ScopeSelector::Global,
            option: OptionName::AllowPassthrough,
            value: "all".to_owned(),
            mode: SetOptionMode::Replace,
        }))
        .await;
    assert!(matches!(set, Response::SetOption(_)));

    let target = recv_switch_target(&mut control_rx, "passthrough refresh").await;
    assert!(
        target.kitty_graphics_passthrough,
        "all is accepted and shares the active-pane passthrough path, since RMUX \
         renders the attached pane and has no unattached-pane passthrough to gate"
    );
}

#[tokio::test]
async fn kitty_passthrough_is_disabled_while_active_pane_is_in_copy_mode() {
    let handler = RequestHandler::new();
    let requester_pid = 43;
    let alpha = session_name("alpha");

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

    let (control_tx, mut control_rx) = mpsc::unbounded_channel();
    let _attach_id = handler
        .register_attach_with_terminal_context(
            requester_pid,
            alpha.clone(),
            control_tx,
            OuterTerminalContext::from_pairs(&[("TERM", "xterm-kitty")]),
        )
        .await;

    let set = handler
        .handle(Request::SetOption(SetOptionRequest {
            scope: ScopeSelector::Global,
            option: OptionName::AllowPassthrough,
            value: "on".to_owned(),
            mode: SetOptionMode::Replace,
        }))
        .await;
    assert!(matches!(set, Response::SetOption(_)));
    let target = recv_switch_target(&mut control_rx, "passthrough refresh").await;
    assert!(
        target.kitty_graphics_passthrough,
        "kitty passthrough should be available before modal pane modes"
    );

    let copied = handler
        .handle(Request::CopyMode(CopyModeRequest {
            target: Some(PaneTarget::new(alpha, 0)),
            page_down: false,
            exit_on_scroll: false,
            hide_position: false,
            mouse_drag_start: false,
            cancel_mode: false,
            scrollbar_scroll: false,
            source: None,
            page_up: false,
        }))
        .await;
    assert!(matches!(copied, Response::CopyMode(_)));

    let target = recv_switch_target(&mut control_rx, "copy-mode refresh").await;
    assert!(
        !target.kitty_graphics_passthrough,
        "modal pane modes must suppress live kitty passthrough"
    );
}

#[tokio::test]
async fn different_requester_pids_can_control_the_sole_active_attach() {
    let handler = RequestHandler::new();
    let owner_pid = 101;
    let intruder_pid = 202;
    let alpha = session_name("alpha");
    let beta = session_name("beta");

    for session_name in [alpha.clone(), beta.clone()] {
        let created = handler
            .handle(Request::NewSession(NewSessionRequest {
                session_name,
                detached: true,
                size: Some(TerminalSize { cols: 80, rows: 24 }),
                environment: None,
            }))
            .await;
        assert!(matches!(created, Response::NewSession(_)));
    }

    let (control_tx, mut control_rx) = mpsc::unbounded_channel();
    let _attach_id = handler
        .register_attach(owner_pid, alpha.clone(), control_tx)
        .await;

    let switched = handler
        .dispatch(
            intruder_pid,
            Request::SwitchClient(SwitchClientRequest {
                target: beta.clone(),
            }),
        )
        .await
        .response;
    assert_eq!(
        switched,
        Response::SwitchClient(rmux_proto::SwitchClientResponse {
            session_name: beta.clone(),
        })
    );
    let _ = recv_matching_attach_control(&mut control_rx, "switch refresh", |control| {
        matches!(control, AttachControl::Switch(_))
    })
    .await;

    let detached = handler
        .dispatch(
            intruder_pid,
            Request::DetachClient(rmux_proto::DetachClientRequest),
        )
        .await
        .response;
    assert_eq!(
        detached,
        Response::DetachClient(rmux_proto::DetachClientResponse)
    );
    let _ = recv_matching_attach_control(&mut control_rx, "detach control", |control| {
        matches!(control, AttachControl::Detach)
    })
    .await;
}

#[tokio::test]
async fn rename_session_preserves_ambiguity_rules_for_switch_and_detach() {
    let handler = RequestHandler::new();
    let alpha = session_name("alpha");
    let beta = session_name("beta");
    let gamma = session_name("gamma");

    for session_name in [alpha.clone(), beta.clone()] {
        let created = handler
            .handle(Request::NewSession(NewSessionRequest {
                session_name,
                detached: true,
                size: Some(TerminalSize { cols: 80, rows: 24 }),
                environment: None,
            }))
            .await;
        assert!(matches!(created, Response::NewSession(_)));
    }

    let (first_tx, _first_rx) = mpsc::unbounded_channel();
    let (second_tx, _second_rx) = mpsc::unbounded_channel();
    let _first_attach = handler.register_attach(101, alpha.clone(), first_tx).await;
    let _second_attach = handler.register_attach(202, alpha.clone(), second_tx).await;

    let renamed = handler
        .handle(Request::RenameSession(RenameSessionRequest {
            target: alpha,
            new_name: gamma,
        }))
        .await;
    assert!(matches!(renamed, Response::RenameSession(_)));

    let switched = handler
        .dispatch(
            303,
            Request::SwitchClient(SwitchClientRequest {
                target: beta.clone(),
            }),
        )
        .await
        .response;
    assert_eq!(
        switched,
        Response::Error(ErrorResponse {
            error: RmuxError::Server(
                "switch-client requires an unambiguous attached client".to_owned(),
            ),
        })
    );

    let detached = handler
        .dispatch(303, Request::DetachClient(DetachClientRequest))
        .await
        .response;
    assert_eq!(
        detached,
        Response::Error(ErrorResponse {
            error: RmuxError::Server(
                "detach-client requires an unambiguous attached client".to_owned(),
            ),
        })
    );
}