par-term 0.48.0

Cross-platform GPU-accelerated terminal emulator with inline graphics support (Sixel, iTerm2, Kitty)
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
//! Profile-free par-mux session management (UX.md A16, A22, M15): attach,
//! create, switch, rename, and end a session from the picker, the palette,
//! or the command line — no profile edit required.
//!
//! Listed sessions attach through the socket that serves them
//! ([`WindowState::begin_mux_attach_at`]), never through the name-derived
//! spawn a profile uses, so a session held by a differently named daemon
//! is reached rather than shadowed by a new empty namesake.

use super::mux::MuxAttachPending;
use super::mux_directory::{self, MuxDirectory, MuxSessionRow, MuxWorkspaceRow, PendingScan};
use crate::app::window_state::WindowState;
use std::path::{Path, PathBuf};

pub(crate) use crate::session_picker_mux::MuxPickerAction as MuxSessionRequest;
use crate::session_picker_mux::MuxPickerAction;

impl WindowState {
    /// The socket directory this window scans and attaches in.
    ///
    /// Under `cfg(test)` only the override counts: a test that attaches or
    /// detaches without setting it must not list (or dial) the developer's
    /// live daemons through the refresh those paths trigger.
    pub(crate) fn mux_socket_dir(&self) -> Option<PathBuf> {
        let override_dir = self.tmux_state.mux_socket_dir_override.clone();
        if cfg!(test) {
            return override_dir;
        }
        override_dir.or_else(mux_directory::default_socket_dir)
    }

    /// The socket a new session named `name` gets: par-term's convention
    /// is one daemon per session, named after it.
    fn mux_socket_for_new(&self, name: &str) -> Option<PathBuf> {
        Some(self.mux_socket_dir()?.join(format!("par-mux-{name}.sock")))
    }

    /// Start a background rescan of the session directory unless one is
    /// already in flight. Called when the picker or palette opens and after
    /// attach, detach, and `%sessions-changed`, so the cached list the
    /// palette reads is fresh without ever blocking a frame.
    pub(crate) fn refresh_mux_directory(&mut self) {
        if self.tmux_state.mux_directory_scan.is_some() {
            return;
        }
        if let Some(dir) = self.mux_socket_dir() {
            self.tmux_state.mux_directory_scan = PendingScan::start(dir);
        }
    }

    /// Apply a finished scan. Returns true when one landed.
    pub(crate) fn poll_mux_directory(&mut self) -> bool {
        let Some(scan) = self.tmux_state.mux_directory_scan.as_ref() else {
            return false;
        };
        match scan.poll() {
            None => false,
            Some(result) => {
                self.tmux_state.mux_directory_scan = None;
                self.tmux_state.mux_directory = Some(result.unwrap_or_else(|()| MuxDirectory {
                    sessions: Vec::new(),
                    workspaces: Vec::new(),
                    errors: vec!["par-mux session scan stopped unexpectedly".to_string()],
                }));
                true
            }
        }
    }

    /// Attach this window to the session `name` served by `socket` —
    /// connect-only for a listed session (`spawn` false), connect-or-spawn
    /// for a new one. The worker thread owns the connect; the existing
    /// `poll_mux_attach` finishes on the main thread.
    pub(crate) fn begin_mux_attach_at(&mut self, socket: &Path, name: &str, spawn: bool) {
        if self.tmux_state.mux_attach_pending.is_some() {
            self.show_toast("par-mux: an attach is already in progress");
            return;
        }
        if let Some(reason) = super::mux::mux_attach_refusal(socket) {
            log::error!("par-mux attach to '{name}' refused: {reason}");
            self.record_mux_error(format!("par-mux: attach to '{name}' refused — {reason}"));
            return;
        }
        self.tmux_state.mux_daemon_rx = None;
        self.tmux_state.mux_attach_socket = Some(socket.to_path_buf());
        let (tx, rx) = std::sync::mpsc::channel();
        let socket = socket.to_path_buf();
        let spawned = std::thread::Builder::new()
            .name("mux-attach".into())
            .spawn(move || {
                let client = if spawn {
                    par_term_emu_core_rust::mux::MuxClient::connect_or_spawn_at(&socket)
                } else {
                    par_term_emu_core_rust::mux::MuxClient::connect(&socket)
                };
                let _ = tx.send(client);
            });
        match spawned {
            Ok(_) => {
                self.tmux_state.mux_attach_pending = Some(MuxAttachPending {
                    name: name.to_string(),
                    rx,
                });
            }
            Err(e) => {
                log::error!("mux attach worker could not start: {e}");
                self.tmux_state.mux_attach_socket = None;
                self.record_mux_error("par-mux: attach failed (worker thread unavailable)");
            }
        }
    }

    /// Leave the current par-mux session (if any) so another can attach
    /// here, keeping the window alive: a window whose tabs were all
    /// daemon tabs would otherwise be left with none. The first daemon tab
    /// of the next session replaces the placeholder shell.
    fn detach_for_switch(&mut self) {
        if self.tmux_state.transport.is_none() {
            return;
        }
        self.detach_mux_session();
        // The transport is gone, so new_tab() spawns a local shell rather
        // than asking the old daemon for a window. A window that kept
        // local tabs keeps them and needs no placeholder.
        if self.tab_manager.tab_count() == 0 {
            self.new_tab();
            if let Some(tab) = self.tab_manager.tabs().first() {
                self.tmux_state.mux_restore_placeholder_tab = Some(tab.id);
            }
        }
    }

    /// Apply a session request from the picker or the palette.
    pub(crate) fn handle_mux_session_request(&mut self, request: MuxSessionRequest) {
        match request {
            MuxSessionRequest::Attach(row) => {
                let already_here = self.tmux_state.transport.is_some()
                    && self.tmux_state.tmux_session_name.as_deref() == Some(row.name.as_str())
                    && self.tmux_state.mux_daemon.as_deref() == Some(row.daemon.as_str());
                if already_here {
                    self.show_toast(format!("par-mux: already attached to '{}'", row.name));
                    return;
                }
                self.detach_for_switch();
                self.tmux_state.mux_daemon = Some(row.daemon.clone());
                self.begin_mux_attach_at(&row.socket, &row.name, false);
                if !row.survives_restore() {
                    self.show_toast(format!(
                        "par-mux: attaching to '{}' (daemon '{}'); it is not reattached on \
                         the next launch",
                        row.name, row.daemon
                    ));
                }
            }
            MuxSessionRequest::Create(name) => {
                if let Some(reason) = crate::session_picker_mux::session_name_refusal(&name) {
                    self.show_toast(format!("par-mux: {reason}"));
                    return;
                }
                let taken = self
                    .tmux_state
                    .mux_directory
                    .as_ref()
                    .is_some_and(|d| d.sessions.iter().any(|s| s.name == name));
                if taken {
                    self.show_toast(format!(
                        "par-mux: a session named '{name}' already exists — attach to it instead"
                    ));
                    return;
                }
                let Some(socket) = self.mux_socket_for_new(&name) else {
                    self.show_toast("par-mux: no socket directory is available");
                    return;
                };
                self.detach_for_switch();
                self.tmux_state.mux_daemon = Some(name.clone());
                self.begin_mux_attach_at(&socket, &name, true);
            }
            MuxSessionRequest::Rename(row, new_name) => {
                self.rename_mux_session(&row, &new_name);
            }
            MuxSessionRequest::Kill(row) => {
                self.kill_mux_session(&row);
            }
            MuxSessionRequest::Detach => {
                if !self.detach_mux_session() {
                    self.show_toast("Not attached to a par-mux session");
                }
            }
            MuxSessionRequest::NewWorkspace {
                socket,
                daemon,
                name,
            } => {
                self.new_mux_workspace(&socket, &daemon, &name);
            }
            MuxSessionRequest::SwitchWorkspace(ws) => {
                self.switch_mux_workspace(&ws);
            }
            MuxSessionRequest::RenameWorkspace(ws, new_name) => {
                self.rename_mux_workspace(&ws, &new_name);
            }
            MuxSessionRequest::KillWorkspace(ws) => {
                self.kill_mux_workspace(&ws);
            }
        }
        self.focus_state.needs_redraw = true;
        self.request_redraw();
    }

    /// Run one command against the daemon at `socket`/`daemon` — through
    /// this window's transport when that is the daemon, else a short-lived
    /// connect (never a spawn).
    fn send_to_daemon(
        &mut self,
        socket: &Path,
        daemon: &str,
        command: &str,
    ) -> Result<Vec<String>, String> {
        let ours = self.tmux_state.transport.is_some()
            && self.tmux_state.mux_daemon.as_deref() == Some(daemon);
        let reply = if ours {
            self.tmux_state
                .transport
                .as_ref()
                .map(|t| t.send_command(command))
                .unwrap_or_else(|| Err(std::io::Error::other("no transport")))
        } else {
            par_term_mux::MuxSessionClient::connect(socket).and_then(|mut c| c.send(command))
        };
        match reply {
            Ok(body) if body.iter().any(|l| l.starts_with("%error")) => Err(body.join(" ")),
            Ok(body) => Ok(body),
            Err(e) => Err(e.to_string()),
        }
    }

    /// Run one command against the daemon serving `row`.
    fn send_to_session_daemon(&mut self, row: &MuxSessionRow, command: &str) -> Result<(), String> {
        self.send_to_daemon(&row.socket, &row.daemon, command)
            .map(|_| ())
    }

    /// Create a workspace on a daemon (`new-workspace -n`): the daemon
    /// makes it active and spawns its first session, so the new row is
    /// landable the moment it exists.
    fn new_mux_workspace(&mut self, socket: &Path, daemon: &str, name: &str) {
        if let Some(reason) = crate::session_picker_mux::workspace_name_refusal(name) {
            self.show_toast(format!("par-mux: {reason}"));
            return;
        }
        let command = format!("new-workspace -n {}", par_term_mux::quote_env_value(name));
        match self.send_to_daemon(socket, daemon, &command) {
            Ok(_) => {
                self.show_toast(format!("par-mux: created workspace '{name}'"));
                self.refresh_mux_directory();
            }
            Err(e) => self.record_mux_error(format!("par-mux: new-workspace failed — {e}")),
        }
    }

    /// Land on a listed daemon's workspace (`select-workspace -t +N`). For
    /// this window's own daemon the view follows: the daemon resumes the
    /// workspace's active session as the displayed one, and this window
    /// reattaches to it through the ordinary session-switch flow.
    fn switch_mux_workspace(&mut self, ws: &MuxWorkspaceRow) {
        let ours = self.tmux_state.transport.is_some()
            && self.tmux_state.mux_daemon.as_deref() == Some(ws.daemon.as_str());
        match self.send_to_daemon(
            &ws.socket,
            &ws.daemon,
            &format!("select-workspace -t +{}", ws.id),
        ) {
            Ok(_) => {
                self.show_toast(format!("par-mux: switched to workspace '{}'", ws.name));
                self.refresh_mux_directory();
                if ours {
                    self.follow_displayed_mux_session();
                }
            }
            Err(e) => self.record_mux_error(format!("par-mux: select-workspace failed — {e}")),
        }
    }

    /// After a daemon-side workspace switch, land this window on the
    /// session the daemon now displays (bare `switch-client` answers its
    /// id): resolve the id against a fresh directory and run the ordinary
    /// attach flow. A listing that misses the id keeps the toast's switch
    /// and skips the follow rather than attaching blind.
    fn follow_displayed_mux_session(&mut self) {
        let Some(transport) = self.tmux_state.transport.as_ref() else {
            return;
        };
        let displayed = transport
            .send_command("switch-client")
            .ok()
            .and_then(|lines| lines.into_iter().find(|l| !l.trim().is_empty()))
            .and_then(|l| {
                l.trim()
                    .strip_prefix('$')
                    .and_then(|r| r.parse::<u64>().ok())
            });
        let Some(displayed) = displayed else {
            return;
        };
        // The cached directory may predate the switch: resolve the id
        // against a live listing of this window's daemon. A session the
        // listing does not carry keeps the toast's switch and skips the
        // follow rather than attaching blind.
        let daemon = self.tmux_state.mux_daemon.clone().unwrap_or_default();
        let socket = self.tmux_state.mux_attach_socket.clone().or_else(|| {
            self.mux_socket_dir()
                .map(|dir| dir.join(format!("par-mux-{daemon}.sock")))
        });
        let Some(socket) = socket else {
            return;
        };
        let row = par_term_mux::MuxSessionClient::connect(&socket)
            .and_then(|mut c| c.list_sessions())
            .ok()
            .and_then(|sessions| {
                sessions.into_iter().find(|s| s.id == displayed).map(|s| {
                    crate::session_picker_mux::MuxSessionRow {
                        socket: socket.clone(),
                        daemon: daemon.clone(),
                        id: s.id,
                        name: s.name,
                        workspace_name: s.workspace_name,
                    }
                })
            });
        let Some(row) = row else {
            return;
        };
        self.handle_mux_session_request(MuxPickerAction::Attach(row));
    }

    /// Rename a listed workspace (`rename-workspace -t +N`).
    fn rename_mux_workspace(&mut self, ws: &MuxWorkspaceRow, new_name: &str) {
        if let Some(reason) = crate::session_picker_mux::workspace_name_refusal(new_name) {
            self.show_toast(format!("par-mux: {reason}"));
            return;
        }
        let command = format!(
            "rename-workspace -t +{} {}",
            ws.id,
            par_term_mux::quote_env_value(new_name)
        );
        match self.send_to_daemon(&ws.socket, &ws.daemon, &command) {
            Ok(_) => {
                self.show_toast(format!(
                    "par-mux: renamed workspace '{}' to '{new_name}'",
                    ws.name
                ));
                self.refresh_mux_directory();
            }
            Err(e) => self.record_mux_error(format!("par-mux: rename-workspace failed — {e}")),
        }
    }

    /// End a listed workspace (`kill-workspace -t +N`). When this window's
    /// own session dies with it, the daemon's `%window-close` /
    /// `%sessions-changed` pushes tear the view down, as for End session.
    fn kill_mux_workspace(&mut self, ws: &MuxWorkspaceRow) {
        match self.send_to_daemon(
            &ws.socket,
            &ws.daemon,
            &format!("kill-workspace -t +{}", ws.id),
        ) {
            Ok(_) => {
                self.show_toast(format!("par-mux: ended workspace '{}'", ws.name));
                self.refresh_mux_directory();
            }
            Err(e) => self.record_mux_error(format!("par-mux: kill-workspace failed — {e}")),
        }
    }

    /// Rename a listed session (UX.md UP1, `rename-session`).
    fn rename_mux_session(&mut self, row: &MuxSessionRow, new_name: &str) {
        if let Some(reason) = crate::session_picker_mux::session_name_refusal(new_name) {
            self.show_toast(format!("par-mux: {reason}"));
            return;
        }
        let command = format!(
            "rename-session -t ${} {}",
            row.id,
            par_term_mux::quote_env_value(new_name)
        );
        match self.send_to_session_daemon(row, &command) {
            Ok(()) => {
                self.show_toast(format!("par-mux: renamed '{}' to '{new_name}'", row.name));
                self.refresh_mux_directory();
            }
            Err(e) => self.record_mux_error(format!("par-mux: rename failed — {e}")),
        }
    }

    /// End a listed session (UX.md UP2, `kill-session`). When it is this
    /// window's own session, the daemon's `%window-close` /
    /// `%sessions-changed` pushes tear the view down, as for End session
    /// in the last-tab dialog.
    fn kill_mux_session(&mut self, row: &MuxSessionRow) {
        match self.send_to_session_daemon(row, &format!("kill-session -t ${}", row.id)) {
            Ok(()) => {
                self.show_toast(format!("par-mux: ended session '{}'", row.name));
                self.refresh_mux_directory();
            }
            Err(e) => self.record_mux_error(format!("par-mux: kill-session failed — {e}")),
        }
    }

    /// M15 / A22: attach this window to `name` without a profile — the CLI
    /// `--attach` and the `mux_auto_attach` config key land here. Which
    /// daemon serves `name` is decided on the attach worker, off the event
    /// loop: a listed session attaches through its socket; an unknown name
    /// creates the session in its own daemon (par-term's
    /// one-daemon-per-session convention).
    pub(crate) fn attach_mux_session_by_name(&mut self, name: &str) {
        self.attach_by_name(name, true);
    }

    /// [`Self::attach_mux_session_by_name`]'s worker, with the create
    /// fallback optional: a hand-bound `attach_mux_session:<name>` finds a
    /// running session this way when the cached list is not filled yet,
    /// and must never create one.
    fn attach_by_name(&mut self, name: &str, create_missing: bool) {
        if let Some(reason) = crate::session_picker_mux::session_name_refusal(name) {
            self.show_toast(format!("par-mux: cannot attach to '{name}' — {reason}"));
            return;
        }
        if self.tmux_state.mux_attach_pending.is_some() {
            self.show_toast("par-mux: an attach is already in progress");
            return;
        }
        let Some(dir) = self.mux_socket_dir() else {
            self.show_toast("par-mux: no socket directory is available");
            return;
        };
        self.detach_for_switch();
        self.tmux_state.mux_attach_socket = None;
        let (tx, rx) = std::sync::mpsc::channel();
        let (daemon_tx, daemon_rx) = std::sync::mpsc::channel();
        let wanted = name.to_string();
        let spawned = std::thread::Builder::new()
            .name("mux-attach".into())
            .spawn(move || {
                let listed = mux_directory::scan(&dir, mux_directory::DAEMON_QUERY_DEADLINE)
                    .sessions
                    .into_iter()
                    .find(|s| s.name == wanted);
                let client = match listed {
                    Some(row) => {
                        let _ = daemon_tx.send((row.daemon.clone(), row.socket.clone()));
                        par_term_emu_core_rust::mux::MuxClient::connect(&row.socket)
                    }
                    None if create_missing => {
                        let socket = dir.join(format!("par-mux-{wanted}.sock"));
                        let _ = daemon_tx.send((wanted.clone(), socket.clone()));
                        par_term_emu_core_rust::mux::MuxClient::connect_or_spawn_at(&socket)
                    }
                    None => Err(std::io::Error::new(
                        std::io::ErrorKind::NotFound,
                        format!("no running session named '{wanted}'"),
                    )),
                };
                let _ = tx.send(client);
            });
        match spawned {
            Ok(_) => {
                self.tmux_state.mux_daemon_rx = Some(daemon_rx);
                self.tmux_state.mux_attach_pending = Some(MuxAttachPending {
                    name: name.to_string(),
                    rx,
                });
            }
            Err(e) => {
                log::error!("mux attach worker could not start: {e}");
                self.record_mux_error("par-mux: attach failed (worker thread unavailable)");
            }
        }
    }

    /// Dispatch an `attach_mux_session:<daemon>/<name>` palette row or
    /// binding against the cached directory. A name no daemon lists is
    /// refused with a toast rather than silently creating a session.
    pub(crate) fn dispatch_attach_mux_session(&mut self, action: &str) -> bool {
        let resolved =
            resolve_attach_id(action, self.tmux_state.mux_directory.as_ref()).map(|r| r.cloned());
        match resolved {
            Some(Ok(row)) => {
                self.handle_mux_session_request(MuxSessionRequest::Attach(row));
                true
            }
            Some(Err(name)) => {
                // Not in the cached list — which may simply be unfilled (a
                // binding pressed right after launch). Look it up on the
                // attach worker; a name no daemon lists fails there with a
                // chip error, and nothing is created.
                self.attach_by_name(&name, false);
                true
            }
            None => false,
        }
    }

    /// Palette rows for the cached directory (A22): one attach row per
    /// listed session plus New Session. Reads the cache only — the palette
    /// never waits on a daemon.
    pub(crate) fn mux_session_palette_rows(
        tmux_state: &crate::app::tmux_handler::tmux_state::TmuxState,
    ) -> Vec<crate::command_palette::catalog::PaletteEntry> {
        let mut rows = Vec::new();
        let attached = tmux_state
            .transport
            .as_ref()
            .and(tmux_state.tmux_session_name.as_deref());
        if let Some(dir) = &tmux_state.mux_directory {
            for session in &dir.sessions {
                if attached == Some(session.name.as_str())
                    && tmux_state.mux_daemon.as_deref() == Some(session.daemon.as_str())
                {
                    continue;
                }
                let label = if session.survives_restore() {
                    format!("Attach par-mux Session: {}", session.name)
                } else {
                    format!(
                        "Attach par-mux Session: {} (daemon {})",
                        session.name, session.daemon
                    )
                };
                rows.push(crate::command_palette::catalog::PaletteEntry {
                    action_id: attach_action_id(session),
                    label,
                    chord: None,
                    priority: 0,
                });
            }
        }
        rows
    }
}

/// The runtime palette id that attaches `row`: `attach_mux_session:<daemon>/<name>`.
pub(crate) fn attach_action_id(row: &MuxSessionRow) -> String {
    format!("attach_mux_session:{}/{}", row.daemon, row.name)
}

/// Resolve an `attach_mux_session:` id against the cached directory.
/// `<name>` alone (no daemon) is accepted for hand-written bindings and
/// resolves by session name.
pub(crate) fn resolve_attach_id<'a>(
    id: &str,
    directory: Option<&'a MuxDirectory>,
) -> Option<Result<&'a MuxSessionRow, String>> {
    let spec = id.strip_prefix("attach_mux_session:")?;
    let (daemon, name) = match spec.split_once('/') {
        Some((daemon, name)) => (Some(daemon), name),
        None => (None, spec),
    };
    let found = directory.and_then(|d| {
        d.sessions
            .iter()
            .find(|s| s.name == name && daemon.is_none_or(|dn| dn == s.daemon))
    });
    Some(found.ok_or_else(|| name.to_string()))
}

#[cfg(test)]
mod tests {
    use super::*;

    fn row(daemon: &str, name: &str) -> MuxSessionRow {
        MuxSessionRow {
            socket: PathBuf::from(format!("/s/par-mux-{daemon}.sock")),
            daemon: daemon.to_string(),
            id: 0,
            name: name.to_string(),
            workspace_name: String::new(),
        }
    }

    #[test]
    fn attach_ids_round_trip_through_the_directory() {
        let dir = MuxDirectory {
            sessions: vec![row("work", "work"), row("default", "scratch")],
            workspaces: vec![],
            errors: vec![],
        };
        let id = attach_action_id(&dir.sessions[1]);
        assert_eq!(id, "attach_mux_session:default/scratch");
        assert_eq!(
            resolve_attach_id(&id, Some(&dir)),
            Some(Ok(&dir.sessions[1]))
        );
        assert_eq!(
            resolve_attach_id("attach_mux_session:work", Some(&dir)),
            Some(Ok(&dir.sessions[0])),
            "a bare name resolves by session name"
        );
        assert_eq!(
            resolve_attach_id("attach_mux_session:gone", Some(&dir)),
            Some(Err("gone".to_string()))
        );
        assert_eq!(resolve_attach_id("new_tab", Some(&dir)), None);
    }
}