libtmux 0.1.0-alpha.8

Async typed tmux client and object model (alpha)
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
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
//! Integration tests for hierarchy mutations against real tmux.

#![cfg(feature = "test-support")]
// Helpers outside a test function are not covered by clippy.toml's
// in-test exemptions, and these files have them.
#![allow(clippy::expect_used, clippy::panic, clippy::unwrap_used)]

use libtmux::test::TestServer;
use libtmux::{NewSessionOptions, NewWindowOptions, SplitDirection, SplitOptions, TmuxText};

fn text(value: Option<&TmuxText>) -> Vec<u8> {
    value.expect("tmux reports the value").as_bytes().to_vec()
}

#[tokio::test]
async fn creating_an_object_returns_it_hydrated_in_one_command() {
    let guard = TestServer::builder().start().await.expect("tmux starts");
    let server = guard.server();

    // A bare name is enough for the common case.
    let session = server
        .new_session("work")
        .await
        .expect("session is created");
    assert_eq!(session.name().as_bytes().to_vec(), b"work".to_vec());
    assert_eq!(session.window_count(), 1);
    // The handle came back populated, so no follow-up listing was needed.
    assert!(session.created() > 0);

    let window = session
        .new_window(NewWindowOptions::new("editor").command("sleep 300"))
        .await
        .expect("window is created");
    assert_eq!(window.name().as_bytes().to_vec(), b"editor".to_vec());
    assert_eq!(window.session_id(), session.id());
    assert_eq!(window.pane_count(), 1);

    let pane = window
        .split(SplitOptions::new(SplitDirection::Below).command("sleep 300"))
        .await
        .expect("pane is created");
    assert_eq!(pane.window_id(), window.id());
    assert!(pane.pid() > 0);

    // The server agrees with what the creating commands reported.
    assert_eq!(server.sessions().await.expect("sessions").len(), 1);
    assert_eq!(server.windows().await.expect("windows").len(), 2);
    assert_eq!(server.panes().await.expect("panes").len(), 3);

    guard.shutdown().await.expect("tmux fixture shuts down");
}

#[tokio::test]
async fn creation_options_reach_tmux() {
    let guard = TestServer::builder().start().await.expect("tmux starts");
    let server = guard.server();
    let directory = tempfile::tempdir().expect("temporary directory");

    let session = server
        .new_session(
            NewSessionOptions::new("configured")
                .window_name("first")
                .start_directory(directory.path())
                .size(120, 40)
                .command("sleep 300"),
        )
        .await
        .expect("session is created");

    let windows = session.windows().await.expect("windows list");
    assert_eq!(windows[0].name().as_bytes().to_vec(), b"first".to_vec());

    // tmux records the requested size as the session's `default-size`, which
    // is what this option controls and is the same on every supported
    // release. Whether a window with no client attached is then drawn at that
    // size is tmux's own business and is not: 3.2a leaves it at 80x23 where
    // 3.6 uses the default. Asserting the rendered size would be asserting
    // tmux's behaviour rather than the crate's.
    assert_eq!(
        session
            .get_option("default-size")
            .await
            .expect("read")
            .expect("new-session -x -y sets it")
            .as_bytes(),
        b"120x40",
    );

    let panes = session.panes().await.expect("panes list");
    assert_eq!(
        text(panes[0].current_path()),
        directory
            .path()
            .canonicalize()
            .expect("canonical path")
            .as_os_str()
            .as_encoded_bytes(),
    );

    guard.shutdown().await.expect("tmux fixture shuts down");
}

#[tokio::test]
async fn a_new_window_does_not_steal_selection_unless_asked() {
    let guard = TestServer::builder().start().await.expect("tmux starts");
    let server = guard.server();

    let session = server
        .new_session("work")
        .await
        .expect("session is created");
    let first = session
        .active_window()
        .await
        .expect("active window resolves")
        .expect("a session always has an active window");

    session
        .new_window(NewWindowOptions::new("background").command("sleep 300"))
        .await
        .expect("window is created");

    let still_first = session
        .active_window()
        .await
        .expect("active window resolves")
        .expect("a session always has an active window");
    assert_eq!(still_first.id(), first.id(), "creation does not select");

    let selected = session
        .new_window(
            NewWindowOptions::new("foreground")
                .command("sleep 300")
                .select(),
        )
        .await
        .expect("window is created");
    let active = session
        .active_window()
        .await
        .expect("active window resolves")
        .expect("a session always has an active window");
    assert_eq!(active.id(), selected.id(), "select() makes it active");

    guard.shutdown().await.expect("tmux fixture shuts down");
}

#[tokio::test]
async fn renaming_updates_the_handle_that_performed_it() {
    let guard = TestServer::builder().start().await.expect("tmux starts");
    let server = guard.server();

    let mut session = server
        .new_session("before")
        .await
        .expect("session is created");
    session.rename("after").await.expect("rename succeeds");
    assert_eq!(session.name().as_bytes().to_vec(), b"after".to_vec());

    let mut window = session
        .new_window(NewWindowOptions::new("old").command("sleep 300"))
        .await
        .expect("window is created");
    window.rename("new").await.expect("rename succeeds");
    assert_eq!(window.name().as_bytes().to_vec(), b"new".to_vec());

    guard.shutdown().await.expect("tmux fixture shuts down");
}

#[tokio::test]
async fn killing_removes_the_object_and_strands_other_handles() {
    let guard = TestServer::builder().start().await.expect("tmux starts");
    let server = guard.server();

    let session = server
        .new_session("doomed")
        .await
        .expect("session is created");
    server
        .new_session("survivor")
        .await
        .expect("session is created");
    let mut stale = session.clone();

    session.kill().await.expect("kill succeeds");

    assert_eq!(server.sessions().await.expect("sessions").len(), 1);
    let error = stale.refresh().await.expect_err("a killed session is gone");
    assert!(matches!(
        error,
        libtmux::Error::ObjectGone {
            kind: libtmux::ObjectKind::Session,
            ..
        },
    ));

    guard.shutdown().await.expect("tmux fixture shuts down");
}

#[tokio::test]
async fn unlink_removes_one_link_while_kill_removes_the_window() {
    let guard = TestServer::builder().start().await.expect("tmux starts");
    let server = guard.server();

    let origin = server
        .new_session("origin")
        .await
        .expect("session is created");
    server
        .new_session("borrower")
        .await
        .expect("session is created");
    let shared = origin
        .new_window(NewWindowOptions::new("shared").command("sleep 300"))
        .await
        .expect("window is created");

    server
        .cmd(
            libtmux::Command::new("link-window")
                .arg("-s")
                .arg(shared.id().to_string())
                .arg("-t")
                .arg("borrower:9"),
        )
        .await
        .expect("link succeeds");

    let links: Vec<_> = server
        .windows()
        .await
        .expect("windows list")
        .into_iter()
        .filter(|window| window.id() == shared.id())
        .collect();
    assert_eq!(links.len(), 2);

    // Unlinking removes one edge; the window survives in the other session.
    links[0].clone().unlink().await.expect("unlink succeeds");
    let remaining: Vec<_> = server
        .windows()
        .await
        .expect("windows list")
        .into_iter()
        .filter(|window| window.id() == shared.id())
        .collect();
    assert_eq!(
        remaining.len(),
        1,
        "the window survives in its other session"
    );

    // Killing removes the window itself.
    remaining[0].clone().kill().await.expect("kill succeeds");
    assert!(
        server
            .windows()
            .await
            .expect("windows list")
            .iter()
            .all(|window| window.id() != shared.id()),
        "the window is gone everywhere",
    );

    guard.shutdown().await.expect("tmux fixture shuts down");
}

#[tokio::test]
async fn pane_input_and_capture_round_trip_through_a_shell() {
    let guard = TestServer::builder().start().await.expect("tmux starts");
    let server = guard.server();

    let session = server
        .new_session(NewSessionOptions::new("io").command("sh"))
        .await
        .expect("session is created");
    let mut pane = session
        .panes()
        .await
        .expect("panes list")
        .into_iter()
        .next()
        .expect("one pane");

    pane.send_keys("printf marker-8fa1\n")
        .await
        .expect("keys are sent");

    // Wait for the shell to produce the output rather than sleeping.
    let deadline = std::time::Instant::now() + std::time::Duration::from_secs(5);
    let captured = loop {
        let lines = pane.capture().await.expect("capture succeeds");
        if lines.iter().any(|line| {
            line.as_bytes()
                .windows(11)
                .any(|window| window == b"marker-8fa1")
        }) {
            break lines;
        }
        assert!(
            std::time::Instant::now() < deadline,
            "the shell did not echo the marker before the deadline",
        );
        tokio::task::yield_now().await;
    };
    assert!(!captured.is_empty());

    // A sole pane fills its window, so there is nothing for a resize to take
    // space from. Split first, then narrow the original.
    let window = pane
        .window()
        .await
        .expect("parent resolves")
        .expect("the pane's window exists");
    window
        .split(SplitOptions::new(SplitDirection::Right).command("sleep 300"))
        .await
        .expect("pane is created");

    pane.refresh().await.expect("the pane still exists");
    let full_width = pane.width();
    pane.resize(20, 8).await.expect("resize succeeds");

    // The handle reports what tmux did, not what was requested.
    assert_eq!(pane.width(), 20);
    assert!(full_width > 20, "the split left room to shrink into");

    guard.shutdown().await.expect("tmux fixture shuts down");
}

#[tokio::test]
async fn killing_the_server_leaves_nothing_running() {
    let guard = TestServer::builder().start().await.expect("tmux starts");
    let server = guard.server().clone();

    server
        .new_session("work")
        .await
        .expect("session is created");
    assert!(server.is_alive().await);

    server.kill().await.expect("kill-server succeeds");
    assert!(!server.is_alive().await);
    // Killing an already-dead server is the state the caller asked for.
    server.kill().await.expect("kill-server is idempotent");

    guard.shutdown().await.expect("tmux fixture shuts down");
}

#[tokio::test]
async fn pane_operations_reshape_the_hierarchy() {
    let guard = TestServer::builder().start().await.expect("tmux starts");
    let server = guard.server();

    let session = server.new_session("shaping").await.expect("session");
    let window = session
        .windows()
        .await
        .expect("windows")
        .into_iter()
        .next()
        .expect("one window");
    let second = window
        .split(SplitOptions::new(SplitDirection::Below).command("sleep 300"))
        .await
        .expect("pane is created");
    let mut first = window
        .panes()
        .await
        .expect("panes")
        .into_iter()
        .next()
        .expect("one pane");

    // A title belongs to the pane, and refreshing shows tmux's own view.
    first.set_title("named").await.expect("title is set");
    assert_eq!(first.title().as_bytes(), b"named",);

    first.clear_history().await.expect("history is cleared");

    // Swapping reports positions from tmux, not from the request.
    let before = first.index();
    first.swap_with(&second).await.expect("panes are swapped");
    assert_ne!(first.index(), before, "the pane moved");

    // Breaking a pane out gives it a window of its own.
    let windows_before = session.windows().await.expect("windows").len();
    second.break_out().await.expect("pane is broken out");
    assert_eq!(
        session.windows().await.expect("windows").len(),
        windows_before + 1,
    );

    guard.shutdown().await.expect("tmux fixture shuts down");
}

#[tokio::test]
async fn window_operations_move_and_resize() {
    let guard = TestServer::builder().start().await.expect("tmux starts");
    let server = guard.server();
    let session = server.new_session("moving").await.expect("session");

    let mut window = session
        .new_window(NewWindowOptions::new("mover").command("sleep 300"))
        .await
        .expect("window is created");

    window.move_to(&session, 20).await.expect("window is moved");
    assert_eq!(window.index(), 20);

    window.resize(100, 30).await.expect("window is resized");
    assert_eq!(window.width(), 100);
    assert_eq!(window.height(), 30);

    guard.shutdown().await.expect("tmux fixture shuts down");
}

#[tokio::test]
async fn a_session_environment_is_set_read_and_removed() {
    let guard = TestServer::builder().start().await.expect("tmux starts");
    let server = guard.server();
    let session = server.new_session("environed").await.expect("session");

    assert!(
        session.environment("EDITOR").await.expect("read").is_none(),
        "a variable nobody set is absent rather than empty",
    );

    session
        .set_environment("EDITOR", "hx")
        .await
        .expect("the variable is set");
    assert!(matches!(
        session.environment("EDITOR").await.expect("read"),
        Some(libtmux::EnvironmentEntry::Set(value)) if value.as_bytes() == b"hx",
    ));

    // A session variable reaches processes the session starts from now on,
    // not the ones already running.
    let window = session
        .new_window(NewWindowOptions::new("later").command("sleep 300"))
        .await
        .expect("window");
    let pane = window.panes().await.expect("panes").remove(0);
    let seen = server
        .format(Some(&pane), "#{?#{==:#{EDITOR},},unset,set}")
        .await
        .expect("a format expands");
    assert_eq!(seen.as_bytes(), b"set");

    session
        .unset_environment("EDITOR")
        .await
        .expect("the variable is removed");
    assert!(session.environment("EDITOR").await.expect("read").is_none());

    // Hiding is not unsetting: tmux keeps an entry marked so a process
    // started here does not inherit the name, which is a third state that
    // reporting absence would hide.
    session
        .set_environment("PAGER", "less")
        .await
        .expect("the variable is set");
    session
        .hide_environment("PAGER")
        .await
        .expect("the variable is hidden");
    assert_eq!(
        session.environment("PAGER").await.expect("read"),
        Some(libtmux::EnvironmentEntry::Removed),
    );

    guard.shutdown().await.expect("tmux fixture shuts down");
}

#[tokio::test]
async fn a_window_linked_into_two_sessions_is_one_window() {
    let guard = TestServer::builder().start().await.expect("tmux starts");
    let server = guard.server();
    let source = server.new_session("linking-from").await.expect("session");
    let target = server.new_session("linking-to").await.expect("session");

    let mut window = source
        .new_window(NewWindowOptions::new("shared").command("sleep 300"))
        .await
        .expect("window");

    window
        .link_to(&target, None)
        .await
        .expect("the window is linked");

    // One window, two winlinks: the id is the same on both sides, and the
    // link reports itself as linked rather than as a copy.
    let linked = target
        .windows()
        .await
        .expect("windows")
        .into_iter()
        .find(|other| other.id() == window.id())
        .expect("the link exists in the target session");
    assert!(linked.is_linked());

    // Renaming through one link is visible through the other, which a copy
    // would not be.
    window
        .rename("renamed")
        .await
        .expect("the window is renamed");
    assert_eq!(
        linked
            .refreshed()
            .await
            .expect("the link still exists")
            .name()
            .as_bytes(),
        b"renamed",
    );

    // Unlinking removes one winlink, and the window survives in the other.
    linked.unlink().await.expect("the link is removed");
    assert!(
        !target
            .windows()
            .await
            .expect("windows")
            .iter()
            .any(|other| other.id() == window.id()),
    );
    assert!(
        source
            .windows()
            .await
            .expect("windows")
            .iter()
            .any(|other| other.id() == window.id()),
        "the window itself is still there, only the second link is gone",
    );

    // An index another window already holds is a refusal, not an overwrite.
    let occupied = target
        .windows()
        .await
        .expect("windows")
        .first()
        .expect("a window")
        .index();
    assert!(window.link_to(&target, Some(occupied)).await.is_err());

    guard.shutdown().await.expect("tmux fixture shuts down");
}

#[tokio::test]
async fn objects_are_found_by_their_tmux_identity() {
    let guard = TestServer::builder().start().await.expect("tmux starts");
    let server = guard.server();
    let session = server.new_session("identified").await.expect("session");
    let window = session
        .new_window(NewWindowOptions::new("found").command("sleep 300"))
        .await
        .expect("window");
    let pane = window.panes().await.expect("panes").remove(0);

    assert_eq!(
        server
            .session_by_id(session.id())
            .await
            .expect("the lookup runs")
            .expect("the session exists")
            .id(),
        session.id(),
    );
    assert_eq!(
        server
            .window_by_id(window.id())
            .await
            .expect("the lookup runs")
            .expect("the window exists")
            .id(),
        window.id(),
    );
    assert_eq!(
        server
            .pane_by_id(pane.id())
            .await
            .expect("the lookup runs")
            .expect("the pane exists")
            .id(),
        pane.id(),
    );

    // An id tmux does not have is absent rather than an error: it is an
    // ordinary answer to "is this still here".
    let gone = window.id().clone();
    window.kill().await.expect("the window is killed");
    assert!(
        server
            .window_by_id(&gone)
            .await
            .expect("the lookup runs")
            .is_none(),
    );

    guard.shutdown().await.expect("tmux fixture shuts down");
}

#[tokio::test]
async fn a_failure_says_what_to_do_about_it() {
    use libtmux::{ErrorKind, Server};

    let guard = TestServer::builder().start().await.expect("tmux starts");
    let server = guard.server();
    let session = server.new_session("classified").await.expect("session");

    // An object that has gone is the branch a caller writes most: it means
    // look again or create, not that the request was wrong.
    let window = session
        .new_window(NewWindowOptions::new("doomed").command("sleep 300"))
        .await
        .expect("window");
    let mut stale = window.clone();
    window.kill().await.expect("the window is killed");

    let gone = stale
        .rename("gone")
        .await
        .expect_err("the window is killed");
    assert_eq!(gone.kind(), ErrorKind::ObjectGone);
    assert!(gone.is_object_gone());
    assert!(!gone.is_transient(), "looking again will not bring it back");

    // Every object kind reports the same way, through whichever command the
    // caller happened to run.
    let pane = session.panes().await.expect("panes").remove(0);
    let stale_pane = pane.clone();
    let gone_session = session.clone();
    session.kill().await.expect("the session is killed");

    for (kind, error) in [
        (
            ErrorKind::ObjectGone,
            stale_pane.capture().await.expect_err("the pane is gone"),
        ),
        (
            ErrorKind::ObjectGone,
            gone_session
                .windows()
                .await
                .expect_err("the session is gone"),
        ),
    ] {
        assert_eq!(error.kind(), kind, "{error}");
    }

    // Not everything reports a missing target. tmux expands a format against
    // one to nothing and exits zero, so this records what tmux does rather
    // than what would be tidier.
    assert!(
        server
            .format(Some(&stale_pane), "#{pane_id}")
            .await
            .expect("tmux does not refuse a format against a target that has gone")
            .as_bytes()
            .is_empty(),
    );

    // tmux answering "no" is a different thing: the arguments were wrong.
    let refused = server
        .delete_buffer("never-existed")
        .await
        .expect_err("tmux refuses to delete a buffer it does not have");
    assert_eq!(refused.kind(), ErrorKind::Refused);
    assert!(
        !refused.is_transient(),
        "tmux will answer the same way again"
    );

    // tmux not being runnable at all is neither: nothing about the request
    // will change it.
    let absent = Server::builder()
        .tmux_executable("tmux-that-is-not-installed")
        .socket_path("/tmp/libtmux-rs-unreachable.sock")
        .build()
        .expect("the configuration is valid")
        .sessions()
        .await
        .expect_err("there is no such executable");
    assert_eq!(absent.kind(), ErrorKind::Unreachable);
    assert!(!absent.is_transient());

    guard.shutdown().await.expect("tmux fixture shuts down");
}

#[tokio::test]
async fn a_name_tmux_cannot_address_is_rejected_before_it_is_created() {
    let guard = TestServer::builder().start().await.expect("tmux starts");
    let server = guard.server();

    // What the type prevents, demonstrated: tmux accepts the name, stores it,
    // and then splits it on the separator when asked to find it again. The
    // session exists and cannot be reached or killed by its own name.
    server
        .cmd(
            libtmux::Command::new("new-session")
                .arg("-d")
                .arg("-s")
                .arg("a:b"),
        )
        .await
        .expect("tmux accepts the name");

    let found = server
        .cmd(libtmux::Command::new("has-session").arg("-t").arg("a:b"))
        .await
        .expect("the command runs");
    assert!(
        !found.success(),
        "tmux cannot find the session it just made"
    );
    assert!(
        found.stderr_lossy().contains("can't find window"),
        "it split the name at the colon: {:?}",
        found.stderr_lossy(),
    );

    // The session is nonetheless there, which is what makes this worth
    // refusing rather than letting a caller discover later.
    assert_eq!(server.sessions().await.expect("sessions").len(), 1);

    // So the type refuses those names, and keeps everything tmux can address.
    assert!(libtmux::SessionName::new("a:b").is_err());
    assert!(libtmux::SessionName::new("c.d").is_err());
    assert!(libtmux::SessionName::new("").is_err());
    assert_eq!(
        libtmux::SessionName::new("has,comma")
            .expect("a comma is addressable")
            .as_str(),
        "has,comma",
    );

    guard.shutdown().await.expect("tmux fixture shuts down");
}

#[tokio::test]
async fn a_taken_session_name_is_classified_rather_than_a_bare_refusal() {
    let guard = TestServer::builder().start().await.expect("tmux starts");
    let server = guard.server();

    server.new_session("taken").await.expect("first session");

    // "pick another name" is the one creation failure a caller routinely
    // handles, so it arrives as its own variant rather than as text to
    // match on. Checking with has-session first would race: another process
    // can take the name between the check and the create.
    let error = server
        .new_session("taken")
        .await
        .map(|_| ())
        .expect_err("tmux refuses a duplicate name");

    assert!(
        matches!(&error, libtmux::Error::SessionExists { name } if name == "taken"),
        "the refusal names what was taken: {error:?}",
    );
    assert_eq!(error.kind(), libtmux::ErrorKind::Refused);

    // The first session is untouched by the refusal.
    assert_eq!(server.sessions().await.expect("sessions").len(), 1);

    guard.shutdown().await.expect("tmux fixture shuts down");
}

#[tokio::test]
async fn directional_focus_follows_the_layout_and_wraps_at_the_edge() {
    use libtmux::{NewWindowOptions, PaneDirection, SplitDirection};

    let guard = TestServer::builder().start().await.expect("tmux starts");
    let session = guard
        .server()
        .new_session("directions")
        .await
        .expect("session");

    // Two panes, one above the other. Kept to two because `split` divides
    // whichever pane is active, and splitting is detached by default, so a
    // third pane's position depends on focus rather than on the call.
    let stacked = session
        .active_window()
        .await
        .expect("windows")
        .expect("a session has a window");
    let mut top = stacked
        .active_pane()
        .await
        .expect("panes")
        .expect("a window has a pane");
    let bottom = stacked
        .split(SplitDirection::Below)
        .await
        .expect("split below");

    top.select().await.expect("focus the top pane");
    assert_eq!(
        stacked
            .focus_direction(PaneDirection::Below)
            .await
            .expect("focus moves")
            .id(),
        bottom.id(),
        "down from the top",
    );
    assert_eq!(
        stacked
            .focus_direction(PaneDirection::Above)
            .await
            .expect("focus moves")
            .id(),
        top.id(),
        "up from the bottom",
    );

    // tmux wraps rather than refusing, so this lands on the other pane
    // instead of reporting that there is nothing above.
    assert_eq!(
        stacked
            .focus_direction(PaneDirection::Above)
            .await
            .expect("the edge is not an error")
            .id(),
        bottom.id(),
        "up from the top wraps to the bottom",
    );

    // Side by side, in a window of its own for the same reason.
    let side = session
        .new_window(NewWindowOptions::new("side").command("sleep 300"))
        .await
        .expect("window created");
    let mut left = side
        .active_pane()
        .await
        .expect("panes")
        .expect("a window has a pane");
    let right = side
        .split(SplitDirection::Right)
        .await
        .expect("split right");

    left.select().await.expect("focus the left pane");
    assert_eq!(
        side.focus_direction(PaneDirection::Right)
            .await
            .expect("focus moves")
            .id(),
        right.id(),
        "right from the left",
    );
    assert_eq!(
        side.focus_direction(PaneDirection::Left)
            .await
            .expect("focus moves")
            .id(),
        left.id(),
        "left from the right",
    );

    guard.shutdown().await.expect("tmux fixture shuts down");
}