rmux-sdk 0.2.0

Public, daemon-backed Rust SDK for the RMUX terminal multiplexer (facade, ensure-session, snapshots, events, detach helpers).
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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
#![cfg(unix)]

use std::error::Error;
use std::path::{Path, PathBuf};
use std::process::{Child, Command, Stdio};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::OnceLock;
use std::time::Duration;

use rmux_proto::{
    encode_frame, FrameDecoder, HasSessionRequest, LinkWindowRequest, ListPanesRequest,
    ListWindowsRequest, NewWindowRequest, Request, Response, WindowListEntry, WindowTarget,
};
use rmux_sdk::{
    EnsureSession, RmuxBuilder, SessionName, SplitDirection, WindowCloseOutcome, WindowRef,
};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::UnixStream;
use tokio::sync::Mutex;
use tokio::time::Instant;

type TestResult<T = ()> = Result<T, Box<dyn Error>>;

static LIVE_DAEMON_LOCK: Mutex<()> = Mutex::const_new(());
static UNIQUE_ID: AtomicUsize = AtomicUsize::new(0);

#[tokio::test]
async fn window_split_info_pane_listing_ids_and_idempotent_close_use_daemon_paths() -> TestResult {
    let _lock = LIVE_DAEMON_LOCK.lock().await;
    let harness = Harness::start("window-basic").await?;
    let rmux = harness.rmux();
    let alpha = session_name("sdkwinalpha");
    let session = EnsureSession::named(alpha.clone())
        .create_only()
        .ensure(&rmux)
        .await?;
    raw_new_window(harness.socket_path(), alpha.clone(), 1).await?;

    let window = session.window(1);
    let window_id = window.id().await?.expect("window 1 is listed");
    let initial_panes = window.panes().await?;
    assert_eq!(initial_panes.len(), 1);
    assert_eq!(
        initial_panes[0].target,
        rmux_sdk::PaneRef::new(alpha.clone(), 1, 0)
    );

    let split_pane = session.pane(1, 0).split(SplitDirection::Down).await?;
    let split_pane_ref = split_pane.target().clone();
    assert_eq!(split_pane_ref.session_name, alpha);
    assert_eq!(split_pane_ref.window_index, 1);

    let panes = window.panes().await?;
    assert_eq!(panes.len(), 2);
    assert_eq!(
        panes.iter().filter(|pane| pane.active).count(),
        1,
        "list-panes format should identify the active pane"
    );
    assert!(panes.iter().any(|pane| pane.target == split_pane_ref));

    let info = window.info().await?;
    assert_eq!(info.sessions.len(), 1);
    assert_eq!(info.windows.len(), 1);
    assert_eq!(info.windows[0].id, window_id);
    assert_eq!(info.windows[0].index, 1);
    assert_eq!(info.panes.len(), 2);
    assert!(info
        .panes
        .iter()
        .all(|pane| pane.window_id == window_id && pane.session_id == info.sessions[0].id));

    let raw_windows = raw_list_windows(harness.socket_path(), session.name().clone()).await?;
    assert!(raw_windows.iter().any(|entry| {
        entry.target == WindowTarget::with_window(session.name().clone(), 1)
            && entry.window_id == window_id.to_string()
            && entry.pane_count == 2
    }));
    let killed_pane_ids =
        raw_list_pane_ids(harness.socket_path(), session.name().clone(), Some(1)).await?;
    assert_eq!(killed_pane_ids.len(), 2);
    assert!(window.exists().await?);

    let same_path_observer = window.clone();
    assert_eq!(
        window.close().await?,
        WindowCloseOutcome::Closed {
            active: WindowRef::first(session.name().clone())
        }
    );
    assert!(
        !raw_list_window_indices(harness.socket_path(), session.name().clone())
            .await?
            .contains(&1)
    );
    let remaining_pane_ids =
        raw_list_pane_ids(harness.socket_path(), session.name().clone(), None).await?;
    assert!(killed_pane_ids
        .iter()
        .all(|pane_id| !remaining_pane_ids.contains(pane_id)));

    assert!(!same_path_observer.exists().await?);
    assert_eq!(same_path_observer.id().await?, None);
    assert_eq!(same_path_observer.panes().await?, Vec::new());
    let stale_info = same_path_observer.info().await?;
    assert_eq!(stale_info.sessions.len(), 1);
    assert_eq!(stale_info.windows, Vec::new());
    assert_eq!(stale_info.panes, Vec::new());
    assert_eq!(
        same_path_observer.close().await?,
        WindowCloseOutcome::AlreadyClosed {
            target: WindowRef::new(session.name().clone(), 1)
        }
    );

    harness.finish().await
}

#[tokio::test]
async fn window_close_synchronizes_grouped_session_listings() -> TestResult {
    let _lock = LIVE_DAEMON_LOCK.lock().await;
    let harness = Harness::start("window-grouped-close").await?;
    let rmux = harness.rmux();

    let grouped_a = session_name("sdkwingroupa");
    let grouped_b = session_name("sdkwingroupb");
    let grouped_session = EnsureSession::named(grouped_a.clone())
        .create_only()
        .ensure(&rmux)
        .await?;
    raw_new_window(harness.socket_path(), grouped_a.clone(), 1).await?;
    EnsureSession::named(grouped_b.clone())
        .create_only()
        .group_target(grouped_a.clone())
        .ensure(&rmux)
        .await?;

    assert_eq!(
        raw_list_window_indices(harness.socket_path(), grouped_a.clone()).await?,
        vec![0, 1]
    );
    assert_eq!(
        raw_list_window_indices(harness.socket_path(), grouped_b.clone()).await?,
        vec![0, 1]
    );
    assert!(matches!(
        grouped_session.window(1).close().await?,
        WindowCloseOutcome::Closed { .. }
    ));
    assert_eq!(
        raw_list_window_indices(harness.socket_path(), grouped_a.clone()).await?,
        vec![0]
    );
    assert_eq!(
        raw_list_window_indices(harness.socket_path(), grouped_b.clone()).await?,
        vec![0]
    );
    assert_eq!(
        rmux.window(WindowRef::new(grouped_b.clone(), 1))
            .await?
            .close()
            .await?,
        WindowCloseOutcome::AlreadyClosed {
            target: WindowRef::new(grouped_b, 1)
        }
    );

    harness.finish().await
}

#[tokio::test]
async fn window_close_removes_linked_window_from_every_affected_listing() -> TestResult {
    let _lock = LIVE_DAEMON_LOCK.lock().await;
    let harness = Harness::start("window-linked-close").await?;
    let rmux = harness.rmux();

    let linked_a = session_name("sdkwinlinka");
    let linked_b = session_name("sdkwinlinkb");
    let linked_b_peer = session_name("sdkwinlinkc");
    let linked_session = EnsureSession::named(linked_a.clone())
        .create_only()
        .ensure(&rmux)
        .await?;
    EnsureSession::named(linked_b.clone())
        .create_only()
        .ensure(&rmux)
        .await?;
    EnsureSession::named(linked_b_peer.clone())
        .create_only()
        .group_target(linked_b.clone())
        .ensure(&rmux)
        .await?;
    raw_new_window(harness.socket_path(), linked_a.clone(), 2).await?;
    raw_link_window(
        harness.socket_path(),
        linked_a.clone(),
        0,
        linked_b.clone(),
        1,
    )
    .await?;

    let linked_a_window = raw_window(harness.socket_path(), linked_a.clone(), 0).await?;
    let linked_b_window = raw_window(harness.socket_path(), linked_b.clone(), 1).await?;
    let linked_b_peer_window = raw_window(harness.socket_path(), linked_b_peer.clone(), 1).await?;
    assert_eq!(
        linked_a_window.window_id, linked_b_window.window_id,
        "link-window should expose one stable window id through both sessions"
    );
    assert_eq!(
        linked_b_window.window_id, linked_b_peer_window.window_id,
        "grouped sessions should expose the linked window through every member"
    );
    let linked_panes = raw_list_pane_ids(harness.socket_path(), linked_a.clone(), Some(0)).await?;

    assert_eq!(
        linked_session.window(0).close().await?,
        WindowCloseOutcome::Closed {
            active: WindowRef::new(linked_a.clone(), 2)
        }
    );
    assert_eq!(
        raw_list_window_indices(harness.socket_path(), linked_a.clone()).await?,
        vec![2]
    );
    assert_eq!(
        raw_list_window_indices(harness.socket_path(), linked_b.clone()).await?,
        vec![0]
    );
    assert_eq!(
        raw_list_window_indices(harness.socket_path(), linked_b_peer.clone()).await?,
        vec![0]
    );
    let remaining_linked_panes =
        raw_list_pane_ids(harness.socket_path(), linked_a.clone(), None).await?;
    assert!(linked_panes
        .iter()
        .all(|pane_id| !remaining_linked_panes.contains(pane_id)));
    let remaining_linked_b_panes =
        raw_list_pane_ids(harness.socket_path(), linked_b.clone(), None).await?;
    assert!(linked_panes
        .iter()
        .all(|pane_id| !remaining_linked_b_panes.contains(pane_id)));
    let remaining_linked_b_peer_panes =
        raw_list_pane_ids(harness.socket_path(), linked_b_peer.clone(), None).await?;
    assert!(linked_panes
        .iter()
        .all(|pane_id| !remaining_linked_b_peer_panes.contains(pane_id)));

    let stale_linked_target = rmux.window(WindowRef::new(linked_b.clone(), 1)).await?;
    assert_eq!(stale_linked_target.panes().await?, Vec::new());
    assert_eq!(
        stale_linked_target.close().await?,
        WindowCloseOutcome::AlreadyClosed {
            target: WindowRef::new(linked_b, 1)
        }
    );

    harness.finish().await
}

#[tokio::test]
async fn window_split_through_linked_target_updates_every_linked_view() -> TestResult {
    let _lock = LIVE_DAEMON_LOCK.lock().await;
    let harness = Harness::start("window-linked-split").await?;
    let rmux = harness.rmux();

    let linked_a = session_name("sdkwinsplita");
    let linked_b = session_name("sdkwinsplitb");
    let linked_b_peer = session_name("sdkwinsplitc");
    EnsureSession::named(linked_a.clone())
        .create_only()
        .ensure(&rmux)
        .await?;
    EnsureSession::named(linked_b.clone())
        .create_only()
        .ensure(&rmux)
        .await?;
    EnsureSession::named(linked_b_peer.clone())
        .create_only()
        .group_target(linked_b.clone())
        .ensure(&rmux)
        .await?;
    raw_link_window(
        harness.socket_path(),
        linked_a.clone(),
        0,
        linked_b.clone(),
        1,
    )
    .await?;

    let linked_target = rmux.window(WindowRef::new(linked_b.clone(), 1)).await?;
    let window_id = linked_target.id().await?.expect("linked target is listed");
    assert_eq!(linked_target.panes().await?.len(), 1);

    let split_pane = rmux
        .pane(rmux_sdk::PaneRef::new(linked_b.clone(), 1, 0))
        .await?
        .split(SplitDirection::Right)
        .await?;
    let split_ref = split_pane.target().clone();
    assert_eq!(split_ref.session_name, linked_b);
    assert_eq!(split_ref.window_index, 1);

    let linked_target_panes = linked_target.panes().await?;
    assert_eq!(linked_target_panes.len(), 2);
    assert!(linked_target_panes
        .iter()
        .any(|pane| pane.target == split_ref));

    let linked_a_window = raw_window(harness.socket_path(), linked_a.clone(), 0).await?;
    let linked_b_window = raw_window(harness.socket_path(), linked_b.clone(), 1).await?;
    let linked_b_peer_window = raw_window(harness.socket_path(), linked_b_peer.clone(), 1).await?;
    assert_eq!(linked_a_window.window_id, window_id.to_string());
    assert_eq!(linked_b_window.window_id, window_id.to_string());
    assert_eq!(linked_b_peer_window.window_id, window_id.to_string());
    assert_eq!(linked_a_window.pane_count, 2);
    assert_eq!(linked_b_window.pane_count, 2);
    assert_eq!(linked_b_peer_window.pane_count, 2);

    let info = linked_target.info().await?;
    assert_eq!(info.sessions.len(), 1);
    assert_eq!(info.windows.len(), 1);
    assert_eq!(info.windows[0].id, window_id);
    assert_eq!(info.windows[0].index, 1);
    assert_eq!(info.panes.len(), 2);

    harness.finish().await
}

fn session_name(value: &str) -> SessionName {
    SessionName::new(value).expect("valid session name")
}

async fn raw_new_window(socket_path: &Path, target: SessionName, index: u32) -> TestResult {
    match framed_request(
        socket_path,
        Request::NewWindow(NewWindowRequest {
            target: target.clone(),
            name: None,
            detached: true,
            environment: None,
            command: None,
            start_directory: None,
            target_window_index: Some(index),
            insert_at_target: false,
        }),
    )
    .await?
    {
        Response::NewWindow(response) => {
            assert_eq!(response.target, WindowTarget::with_window(target, index));
            Ok(())
        }
        response => Err(format!("unexpected new-window response: {response:?}").into()),
    }
}

async fn raw_link_window(
    socket_path: &Path,
    source_session: SessionName,
    source_index: u32,
    target_session: SessionName,
    target_index: u32,
) -> TestResult {
    match framed_request(
        socket_path,
        Request::LinkWindow(LinkWindowRequest {
            source: WindowTarget::with_window(source_session, source_index),
            target: WindowTarget::with_window(target_session.clone(), target_index),
            after: false,
            before: false,
            kill_destination: false,
            detached: true,
        }),
    )
    .await?
    {
        Response::LinkWindow(response) => {
            assert_eq!(
                response.target,
                WindowTarget::with_window(target_session, target_index)
            );
            Ok(())
        }
        response => Err(format!("unexpected link-window response: {response:?}").into()),
    }
}

async fn raw_window(
    socket_path: &Path,
    target: SessionName,
    index: u32,
) -> TestResult<WindowListEntry> {
    raw_list_windows(socket_path, target)
        .await?
        .into_iter()
        .find(|entry| entry.target.window_index() == index)
        .ok_or_else(|| format!("window {index} was not listed").into())
}

async fn raw_list_window_indices(socket_path: &Path, target: SessionName) -> TestResult<Vec<u32>> {
    Ok(raw_list_windows(socket_path, target)
        .await?
        .into_iter()
        .map(|entry| entry.target.window_index())
        .collect())
}

async fn raw_list_windows(
    socket_path: &Path,
    target: SessionName,
) -> TestResult<Vec<WindowListEntry>> {
    match framed_request(
        socket_path,
        Request::ListWindows(ListWindowsRequest {
            target,
            format: Some("#{window_index}:#{window_id}:#{window_panes}".to_owned()),
        }),
    )
    .await?
    {
        Response::ListWindows(response) => Ok(response.windows),
        response => Err(format!("unexpected list-windows response: {response:?}").into()),
    }
}

async fn raw_list_pane_ids(
    socket_path: &Path,
    target: SessionName,
    target_window_index: Option<u32>,
) -> TestResult<Vec<String>> {
    match framed_request(
        socket_path,
        Request::ListPanes(ListPanesRequest {
            target,
            target_window_index,
            format: Some("#{pane_id}".to_owned()),
        }),
    )
    .await?
    {
        Response::ListPanes(response) => Ok(String::from_utf8_lossy(response.output.stdout())
            .lines()
            .map(str::to_owned)
            .collect()),
        response => Err(format!("unexpected list-panes response: {response:?}").into()),
    }
}

async fn framed_request(socket_path: &Path, request: Request) -> TestResult<Response> {
    let mut stream = UnixStream::connect(socket_path).await?;
    let frame = encode_frame(&request)?;
    stream.write_all(&frame).await?;
    read_response(&mut stream).await
}

async fn read_response(stream: &mut UnixStream) -> TestResult<Response> {
    let mut decoder = FrameDecoder::new();
    let mut read_buffer = [0_u8; 8192];

    loop {
        if let Some(response) = decoder.next_frame::<Response>()? {
            return Ok(response);
        }

        let bytes_read = stream.read(&mut read_buffer).await?;
        if bytes_read == 0 {
            return Err("connection closed before response frame".into());
        }
        decoder.push_bytes(&read_buffer[..bytes_read]);
    }
}

struct Harness {
    _root: TestRoot,
    socket_path: PathBuf,
    child: Option<Child>,
}

impl Harness {
    async fn start(label: &str) -> TestResult<Self> {
        let root = TestRoot::new(label);
        let socket_path = root.path().join("daemon.sock");
        let mut child = Command::new(rmux_binary()?)
            .arg("--__internal-daemon")
            .arg(&socket_path)
            .stdin(Stdio::null())
            .stdout(Stdio::null())
            .stderr(Stdio::null())
            .spawn()?;
        wait_for_daemon_ready(&socket_path, &mut child).await?;

        Ok(Self {
            _root: root,
            socket_path,
            child: Some(child),
        })
    }

    fn socket_path(&self) -> &Path {
        &self.socket_path
    }

    fn rmux(&self) -> rmux_sdk::Rmux {
        RmuxBuilder::new().unix_socket(&self.socket_path).build()
    }

    async fn finish(self) -> TestResult {
        let shutdown = self.rmux().shutdown().await;
        wait_for_child_exit(self, "server did not exit during cleanup").await?;
        if let Err(error) = shutdown {
            let rendered = error.to_string();
            assert!(
                rendered.contains("connect to rmux daemon")
                    || rendered.contains("rmux daemon closed the transport")
                    || rendered.contains("rmux transport actor is closed")
                    || rendered.contains("Connection reset by peer"),
                "unexpected cleanup shutdown error: {rendered}"
            );
        }
        Ok(())
    }
}

impl Drop for Harness {
    fn drop(&mut self) {
        if let Some(child) = self.child.as_mut() {
            let _ = child.kill();
        }
    }
}

async fn wait_for_child_exit(mut harness: Harness, timeout_message: &'static str) -> TestResult {
    let mut child = harness.child.take().expect("harness owns daemon child");
    let deadline = Instant::now() + Duration::from_secs(5);

    loop {
        if let Some(status) = child.try_wait()? {
            assert!(status.success(), "daemon exited with status {status}");
            return Ok(());
        }

        if Instant::now() >= deadline {
            let _ = child.kill();
            return Err(timeout_message.into());
        }

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

async fn wait_for_daemon_ready(socket_path: &Path, child: &mut Child) -> TestResult {
    let deadline = Instant::now() + Duration::from_secs(5);
    let probe = session_name("sdkprobe");

    loop {
        if let Some(status) = child.try_wait()? {
            return Err(format!("daemon exited before accepting RPC: {status}").into());
        }

        if matches!(
            framed_request(
                socket_path,
                Request::HasSession(HasSessionRequest {
                    target: probe.clone()
                })
            )
            .await,
            Ok(Response::HasSession(_))
        ) {
            return Ok(());
        }

        if Instant::now() >= deadline {
            return Err(format!(
                "daemon at '{}' did not accept RPC before timeout",
                socket_path.display()
            )
            .into());
        }
        tokio::time::sleep(Duration::from_millis(25)).await;
    }
}

fn rmux_binary() -> TestResult<&'static Path> {
    static RMUX_BINARY: OnceLock<Result<PathBuf, String>> = OnceLock::new();
    match RMUX_BINARY.get_or_init(|| resolve_rmux_binary().map_err(|error| error.to_string())) {
        Ok(path) => Ok(path.as_path()),
        Err(error) => Err(std::io::Error::other(error.clone()).into()),
    }
}

fn resolve_rmux_binary() -> TestResult<PathBuf> {
    if let Some(path) = option_env!("CARGO_BIN_EXE_rmux") {
        let path = PathBuf::from(path);
        if path.is_file() {
            return Ok(path);
        }
    }

    let target_dir = target_dir()?;
    let candidate = target_dir.join("debug").join("rmux");
    let status =
        std::process::Command::new(std::env::var_os("CARGO").unwrap_or_else(|| "cargo".into()))
            .arg("build")
            .arg("--bin")
            .arg("rmux")
            .arg("--locked")
            .arg("--manifest-path")
            .arg(workspace_root().join("Cargo.toml"))
            .env("CARGO_TARGET_DIR", &target_dir)
            .status()?;
    if !status.success() {
        return Err(format!("failed to build rmux binary for daemon tests: {status}").into());
    }
    if !candidate.is_file() {
        return Err(format!(
            "rmux daemon build succeeded but '{}' was not created",
            candidate.display()
        )
        .into());
    }

    Ok(candidate)
}

fn target_dir() -> TestResult<PathBuf> {
    if let Some(target_dir) = std::env::var_os("CARGO_TARGET_DIR") {
        return Ok(PathBuf::from(target_dir));
    }

    let current = std::env::current_exe()?;
    current
        .parent()
        .and_then(Path::parent)
        .map(Path::to_path_buf)
        .ok_or_else(|| "test executable is not under a target directory".into())
}

fn workspace_root() -> PathBuf {
    PathBuf::from(env!("CARGO_MANIFEST_DIR"))
        .parent()
        .and_then(Path::parent)
        .expect("rmux-sdk manifest lives under crates/rmux-sdk")
        .to_path_buf()
}

struct TestRoot {
    path: PathBuf,
}

impl TestRoot {
    fn new(label: &str) -> Self {
        let unique_id = UNIQUE_ID.fetch_add(1, Ordering::Relaxed);
        let path = PathBuf::from("/tmp").join(format!(
            "rmux-sdk-window-{}-{}-{unique_id}",
            compact_label(label),
            std::process::id()
        ));
        Self { path }
    }

    fn path(&self) -> &Path {
        &self.path
    }
}

impl Drop for TestRoot {
    fn drop(&mut self) {
        let _ = std::fs::remove_dir_all(&self.path);
    }
}

fn compact_label(label: &str) -> String {
    let compact = label
        .chars()
        .filter(|character| character.is_ascii_alphanumeric())
        .take(16)
        .collect::<String>();
    if compact.is_empty() {
        "x".to_owned()
    } else {
        compact
    }
}