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
use super::*;

#[tokio::test]
async fn attached_prefix_right_dispatches_select_pane_right() {
    let handler = RequestHandler::new();
    let requester_pid = std::process::id();
    let alpha = session_name("alpha");
    let _control_rx = create_attached_session(&handler, requester_pid, &alpha).await;
    assert!(matches!(
        handler
            .handle(Request::SplitWindow(SplitWindowRequest {
                target: SplitWindowTarget::Session(alpha.clone()),
                direction: rmux_proto::SplitDirection::Horizontal,
                before: false,
                environment: None,
            }))
            .await,
        Response::SplitWindow(_)
    ));
    assert!(matches!(
        handler
            .handle(Request::SelectPane(Box::new(SelectPaneRequest {
                target: PaneTarget::new(alpha.clone(), 0),
                title: None,
                style: None,
                input_disabled: None,
                preserve_zoom: false,
            })))
            .await,
        Response::SelectPane(_)
    ));

    handler
        .handle_attached_live_input_for_test(requester_pid, b"\x02\x1b[C")
        .await
        .expect("prefix right input");

    assert_eq!(active_panes(&handler, &alpha).await, "0:0\n1:1\n");
}

#[tokio::test]
async fn attached_prefix_n_dispatches_next_window() {
    let handler = RequestHandler::new();
    let requester_pid = std::process::id();
    let alpha = session_name("alpha");
    let _control_rx = create_attached_session(&handler, requester_pid, &alpha).await;
    assert!(matches!(
        handler
            .handle(Request::NewWindow(Box::new(NewWindowRequest {
                target: alpha.clone(),
                name: None,
                detached: true,
                start_directory: None,
                environment: None,
                command: None,
                process_command: None,
                target_window_index: None,
                insert_at_target: false,
            })))
            .await,
        Response::NewWindow(_)
    ));

    handler
        .handle_attached_live_input_for_test(requester_pid, b"\x02n")
        .await
        .expect("prefix n input");

    assert_eq!(active_windows(&handler, &alpha).await, "0:0\n1:1\n");
}

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

    handler
        .handle_attached_live_input_for_test(requester_pid, b"\x02n")
        .await
        .expect("prefix n should not terminate the attached client");

    let frame = recv_overlay_frame(&mut control_rx, "prefix n status message").await;
    assert!(
        frame.contains("No next window"),
        "prefix n should render tmux's attached status message, got {frame:?}"
    );
    assert!(
        frame.contains("\x1b[0;30;43m") || frame.contains("\x1b[30;43m"),
        "prefix n should render tmux's default message-style, got {frame:?}"
    );
    assert_eq!(active_windows(&handler, &alpha).await, "0:1\n");
}

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

    handler
        .handle_attached_live_input_for_test(requester_pid, b"\x02p")
        .await
        .expect("prefix p should not terminate the attached client");

    let frame = recv_overlay_frame(&mut control_rx, "prefix p status message").await;
    assert!(
        frame.contains("No previous window"),
        "prefix p should render tmux's attached status message, got {frame:?}"
    );
    assert!(
        frame.contains("\x1b[0;30;43m") || frame.contains("\x1b[30;43m"),
        "prefix p should render tmux's default message-style, got {frame:?}"
    );
    assert_eq!(active_windows(&handler, &alpha).await, "0:1\n");
}

#[tokio::test]
async fn attached_prefix_o_cycles_to_the_next_pane() {
    let handler = RequestHandler::new();
    let requester_pid = std::process::id();
    let alpha = session_name("alpha");
    let _control_rx = create_attached_session(&handler, requester_pid, &alpha).await;
    assert!(matches!(
        handler
            .handle(Request::SplitWindow(SplitWindowRequest {
                target: SplitWindowTarget::Session(alpha.clone()),
                direction: rmux_proto::SplitDirection::Horizontal,
                before: false,
                environment: None,
            }))
            .await,
        Response::SplitWindow(_)
    ));

    handler
        .handle_attached_live_input_for_test(requester_pid, b"\x02o")
        .await
        .expect("prefix o input");

    assert_eq!(active_panes(&handler, &alpha).await, "0:1\n1:0\n");
}

#[tokio::test]
async fn attached_prefix_meta_digits_select_tmux_layout_presets() {
    let handler = RequestHandler::new();
    let requester_pid = std::process::id();
    let alpha = session_name("alpha");
    let mut control_rx = create_attached_session(&handler, requester_pid, &alpha).await;
    let control_backlog = {
        let active_attach = handler.active_attach.lock().await;
        active_attach
            .by_pid
            .get(&requester_pid)
            .expect("attached client exists")
            .control_backlog
            .clone()
    };
    let control_drain = tokio::spawn(async move {
        while let Some(control) = control_rx.recv().await {
            crate::pane_io::release_attach_control_backlog(
                &control_backlog,
                control.received_backlog_units(),
            );
        }
    });
    for _ in 0..2 {
        assert!(matches!(
            handler
                .handle(Request::SplitWindow(SplitWindowRequest {
                    target: SplitWindowTarget::Session(alpha.clone()),
                    direction: rmux_proto::SplitDirection::Vertical,
                    before: false,
                    environment: None,
                }))
                .await,
            Response::SplitWindow(_)
        ));
    }

    for (bytes, expected_layout, starting_layout) in [
        (
            b"\x02\x1b1".as_slice(),
            LayoutName::EvenHorizontal,
            LayoutName::Tiled,
        ),
        (
            b"\x02\x1b2".as_slice(),
            LayoutName::EvenVertical,
            LayoutName::Tiled,
        ),
        (
            b"\x02\x1b3".as_slice(),
            LayoutName::MainHorizontal,
            LayoutName::Tiled,
        ),
        (
            b"\x02\x1b4".as_slice(),
            LayoutName::MainVertical,
            LayoutName::Tiled,
        ),
        (
            b"\x02\x1b5".as_slice(),
            LayoutName::Tiled,
            LayoutName::EvenHorizontal,
        ),
    ] {
        select_layout(&handler, &alpha, starting_layout).await;
        assert_eq!(current_layout(&handler, &alpha).await, starting_layout);
        handler
            .handle_attached_live_input_for_test(requester_pid, bytes)
            .await
            .expect("prefix meta digit input");
        assert_eq!(
            current_layout(&handler, &alpha).await,
            expected_layout,
            "prefix meta digit input {bytes:?} should select {expected_layout:?}"
        );
    }
    control_drain.abort();
}

#[tokio::test]
async fn attached_select_layout_main_horizontal_binding_command_executes() {
    let handler = RequestHandler::new();
    let requester_pid = std::process::id();
    let alpha = session_name("alpha");
    let _control_rx = create_attached_session(&handler, requester_pid, &alpha).await;
    for _ in 0..2 {
        assert!(matches!(
            handler
                .handle(Request::SplitWindow(SplitWindowRequest {
                    target: SplitWindowTarget::Session(alpha.clone()),
                    direction: rmux_proto::SplitDirection::Vertical,
                    before: false,
                    environment: None,
                }))
                .await,
            Response::SplitWindow(_)
        ));
    }
    select_layout(&handler, &alpha, LayoutName::Tiled).await;

    let commands = handler
        .parse_command_string_one_group("select-layout main-horizontal")
        .await
        .expect("binding command parses");
    handler
        .execute_parsed_commands_for_test(requester_pid, commands)
        .await
        .expect("binding command executes");

    assert_eq!(
        current_layout(&handler, &alpha).await,
        LayoutName::MainHorizontal
    );
}

#[tokio::test]
async fn attached_prefix_meta_digit_dispatch_survives_escape_split_across_reads() {
    let handler = RequestHandler::new();
    let requester_pid = std::process::id();
    let alpha = session_name("alpha");
    let _control_rx = create_attached_session(&handler, requester_pid, &alpha).await;
    for _ in 0..2 {
        assert!(matches!(
            handler
                .handle(Request::SplitWindow(SplitWindowRequest {
                    target: SplitWindowTarget::Session(alpha.clone()),
                    direction: rmux_proto::SplitDirection::Vertical,
                    before: false,
                    environment: None,
                }))
                .await,
            Response::SplitWindow(_)
        ));
    }

    select_layout(&handler, &alpha, LayoutName::Tiled).await;
    assert_eq!(current_layout(&handler, &alpha).await, LayoutName::Tiled);

    handler
        .handle_attached_live_input_for_test(requester_pid, b"\x02")
        .await
        .expect("prefix input");

    let mut pending_input = Vec::new();
    handler
        .handle_attached_live_input(requester_pid, &mut pending_input, b"\x1b")
        .await
        .expect("escape prefix fragment");
    assert_eq!(pending_input, b"\x1b");
    handler
        .handle_attached_live_input(requester_pid, &mut pending_input, b"1")
        .await
        .expect("meta digit fragment");

    assert!(
        pending_input.is_empty(),
        "meta digit should be fully consumed"
    );
    assert_eq!(
        current_layout(&handler, &alpha).await,
        LayoutName::EvenHorizontal
    );
}

#[tokio::test]
async fn attached_prefix_space_cycles_next_layout_using_current_window_target() {
    let handler = RequestHandler::new();
    let requester_pid = std::process::id();
    let alpha = session_name("alpha");
    let _control_rx = create_attached_session(&handler, requester_pid, &alpha).await;
    for _ in 0..2 {
        assert!(matches!(
            handler
                .handle(Request::SplitWindow(SplitWindowRequest {
                    target: SplitWindowTarget::Session(alpha.clone()),
                    direction: rmux_proto::SplitDirection::Vertical,
                    before: false,
                    environment: None,
                }))
                .await,
            Response::SplitWindow(_)
        ));
    }

    select_layout(&handler, &alpha, LayoutName::Tiled).await;
    assert_eq!(current_layout(&handler, &alpha).await, LayoutName::Tiled);

    handler
        .handle_attached_live_input_for_test(requester_pid, b"\x02 ")
        .await
        .expect("prefix space input");

    assert_eq!(
        current_layout(&handler, &alpha).await,
        LayoutName::EvenHorizontal
    );
}

#[tokio::test]
async fn attached_prefix_q_emits_a_display_panes_overlay() {
    let handler = RequestHandler::new();
    let requester_pid = std::process::id();
    let alpha = session_name("alpha");
    let mut control_rx = create_attached_session(&handler, requester_pid, &alpha).await;
    assert!(matches!(
        handler
            .handle(Request::SplitWindow(SplitWindowRequest {
                target: SplitWindowTarget::Session(alpha.clone()),
                direction: rmux_proto::SplitDirection::Vertical,
                before: false,
                environment: None,
            }))
            .await,
        Response::SplitWindow(_)
    ));
    drain_attach_controls(&mut control_rx);

    handler
        .handle_attached_live_input_for_test(requester_pid, b"\x02q")
        .await
        .expect("prefix q input");

    let overlay = tokio::time::timeout(std::time::Duration::from_secs(1), async {
        loop {
            let next = control_rx
                .recv()
                .await
                .expect("display-panes overlay control");
            if matches!(next, AttachControl::Overlay(_)) {
                break next;
            }
        }
    })
    .await
    .expect("display-panes overlay should arrive");
    assert!(
        matches!(overlay, AttachControl::Overlay(_)),
        "expected display-panes overlay, got {overlay:?}"
    );
}

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

    let mut pending_input = Vec::new();
    let forwarded = handler
        .handle_attached_live_input_inner(requester_pid, &mut pending_input, b"\x02?")
        .await
        .expect("prefix help input");

    assert!(
        !forwarded,
        "prefix help should be consumed by the attach UI"
    );
    assert!(
        pending_input.is_empty(),
        "prefix help should not leave pending input"
    );
    let help_frame = recv_overlay_frame(&mut control_rx, "prefix help overlay").await;
    assert!(
        help_frame.contains("list-keys") || help_frame.contains("List key bindings"),
        "prefix help should render list-keys content, got {help_frame:?}"
    );

    let forwarded = handler
        .handle_attached_live_input_inner(requester_pid, &mut pending_input, b"q")
        .await
        .expect("prefix help dismiss");

    assert!(
        !forwarded,
        "dismiss key should not be forwarded to the pane"
    );
    let clear_frame = recv_overlay_frame(&mut control_rx, "prefix help clear").await;
    assert!(
        clear_frame.is_empty(),
        "dismissing prefix help should clear the overlay, got {clear_frame:?}"
    );
}