embers-cli 0.1.0

Interactive terminal UI and automation CLI for the Embers terminal multiplexer.
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
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
#[cfg(target_os = "macos")]
use std::ffi::CStr;
use std::ffi::{OsStr, OsString};
use std::fs;
#[cfg(target_os = "macos")]
use std::os::unix::ffi::OsStrExt;
#[cfg(unix)]
use std::os::unix::ffi::OsStringExt;
use std::path::{Path, PathBuf};
use std::time::Duration;

use embers_core::{ActivityState, BufferId, NodeId, PtySize, new_request_id};
use embers_protocol::{
    BufferRequest, ClientMessage, InputRequest, ServerResponse, SessionRequest, SessionSnapshot,
    VisibleSnapshotResponse,
};
use embers_test_support::{
    PtyHarness, TestConnection, TestServer, acquire_test_lock, cargo_bin, cargo_bin_path,
};
use filetime::FileTime;

use crate::support::{require_pty, run_cli, session_snapshot_by_name, stdout};

const STARTUP_TIMEOUT: Duration = Duration::from_secs(15);
const IO_TIMEOUT: Duration = Duration::from_secs(30);
const FILE_WAIT_POLL: Duration = Duration::from_millis(50);
const FILE_WAIT_ATTEMPTS: usize = 200;
const SCROLLBACK_SETTLE_DELAY: Duration = Duration::from_millis(750);
const QUIET_TIMEOUT: Duration = Duration::from_millis(500);
const PAGE_UP_ATTEMPTS: usize = 4;

/// A guard that owns the spawned embers process and ensures cleanup
/// of orphaned __serve processes when dropped.
struct SpawnedEmbers {
    socket_path: PathBuf,
    started_server: bool,
}

impl SpawnedEmbers {
    fn new(socket_path: PathBuf, started_server: bool) -> Self {
        Self {
            socket_path,
            started_server,
        }
    }
}

impl Drop for SpawnedEmbers {
    fn drop(&mut self) {
        if !self.started_server {
            return;
        }
        // Kill any orphaned __serve process for our socket
        kill_orphaned_server(&self.socket_path);
    }
}

/// Kill any orphaned embers __serve process for the given socket.
/// This is safe to call from Drop or any synchronous context.
fn kill_orphaned_server(socket_path: &Path) {
    let pid_path = socket_path.with_extension("pid");

    // A clean SIGTERM path returns here; failed waits fall through to the SIGKILL retry below.
    let matched_pid = try_signal_server(&pid_path, socket_path, libc::SIGTERM);
    if let Some(matched_pid) = matched_pid
        && wait_for_server_exit(matched_pid, socket_path)
    {
        if read_pid(&pid_path) == Some(matched_pid) {
            let _ = fs::remove_file(&pid_path);
        }
        return;
    }

    // Wait briefly for graceful shutdown
    std::thread::sleep(Duration::from_millis(50));

    let matched_pid = try_signal_server(&pid_path, socket_path, libc::SIGKILL);
    if let Some(matched_pid) = matched_pid
        && wait_for_server_exit(matched_pid, socket_path)
        && read_pid(&pid_path) == Some(matched_pid)
    {
        let _ = fs::remove_file(&pid_path);
    }
}

fn try_signal_server(pid_path: &Path, socket_path: &Path, signal: i32) -> Option<i32> {
    let pid = read_pid(pid_path)?;
    if !pid_matches_serve_process(pid, socket_path) {
        return None;
    }
    // SAFETY: pid was read from our pid file and verified against the active __serve command line.
    if unsafe { libc::kill(pid, signal) } != 0 {
        return None;
    }
    Some(pid)
}

fn wait_for_server_exit(pid: i32, socket_path: &Path) -> bool {
    for _ in 0..20 {
        if !pid_matches_serve_process(pid, socket_path) {
            return true;
        }
        std::thread::sleep(Duration::from_millis(25));
    }
    !pid_matches_serve_process(pid, socket_path)
}

fn read_pid(pid_path: &Path) -> Option<i32> {
    let pid = fs::read_to_string(pid_path).ok()?;
    let pid = pid.trim().parse::<i32>().ok()?;
    (pid > 0).then_some(pid)
}

fn pid_matches_serve_process(pid: i32, socket_path: &Path) -> bool {
    let expected_binary = cargo_bin_path("embers");
    let Some((exe_path, argv)) = process_exe_and_argv(pid) else {
        return false;
    };

    same_path(&exe_path, &expected_binary)
        && argv
            .first()
            .is_some_and(|arg| same_path(Path::new(arg.as_os_str()), &expected_binary))
        && argv.get(1).is_some_and(|arg| arg == OsStr::new("__serve"))
        && argv.windows(2).any(|window| {
            window[0] == OsStr::new("--socket")
                && same_path(Path::new(window[1].as_os_str()), socket_path)
        })
}

#[cfg(any(target_os = "linux", target_os = "android"))]
fn process_exe_and_argv(pid: i32) -> Option<(PathBuf, Vec<OsString>)> {
    let exe_path = fs::read_link(format!("/proc/{pid}/exe")).ok()?;
    let cmdline = fs::read(format!("/proc/{pid}/cmdline")).ok()?;
    let argv = split_nul_args(&cmdline)?;
    Some((exe_path, argv))
}

#[cfg(target_os = "macos")]
fn process_exe_and_argv(pid: i32) -> Option<(PathBuf, Vec<OsString>)> {
    let exe_path = process_exe_path(pid)?;
    let mut mib = [libc::CTL_KERN, libc::KERN_PROCARGS2, pid];
    let mut size = 0usize;
    // SAFETY: `mib` names the procargs sysctl and `size` is a valid output parameter.
    let size_result = unsafe {
        libc::sysctl(
            mib.as_mut_ptr(),
            u32::try_from(mib.len()).ok()?,
            std::ptr::null_mut(),
            &mut size,
            std::ptr::null_mut(),
            0,
        )
    };
    if size_result != 0 || size == 0 {
        return None;
    }

    let mut bytes = vec![0u8; size];
    // SAFETY: `bytes` is allocated to the kernel-reported size and all pointers remain valid.
    let read_result = unsafe {
        libc::sysctl(
            mib.as_mut_ptr(),
            u32::try_from(mib.len()).ok()?,
            bytes.as_mut_ptr().cast(),
            &mut size,
            std::ptr::null_mut(),
            0,
        )
    };
    if read_result != 0 || size == 0 {
        return None;
    }
    bytes.truncate(size);
    let argv = parse_macos_argv(&bytes)?;
    Some((exe_path, argv))
}

#[cfg(not(any(target_os = "linux", target_os = "android", target_os = "macos")))]
fn process_exe_and_argv(_pid: i32) -> Option<(PathBuf, Vec<OsString>)> {
    None
}

#[cfg(any(target_os = "linux", target_os = "android"))]
fn split_nul_args(bytes: &[u8]) -> Option<Vec<OsString>> {
    let args = bytes
        .split(|byte| *byte == 0)
        .filter(|arg| !arg.is_empty())
        .map(|arg| OsString::from_vec(arg.to_vec()))
        .collect::<Vec<_>>();
    (!args.is_empty()).then_some(args)
}

#[cfg(target_os = "macos")]
fn parse_macos_argv(bytes: &[u8]) -> Option<Vec<OsString>> {
    let argc = i32::from_ne_bytes(bytes.get(..std::mem::size_of::<i32>())?.try_into().ok()?);
    if argc < 0 {
        return None;
    }
    let argc = usize::try_from(argc).ok()?;
    if argc > bytes.len() {
        return None;
    }
    let mut index = std::mem::size_of::<i32>();
    while bytes.get(index).is_some_and(|byte| *byte != 0) {
        index += 1;
    }
    while bytes.get(index).is_some_and(|byte| *byte == 0) {
        index += 1;
    }

    let mut argv = Vec::with_capacity(argc);
    for _ in 0..argc {
        let start = index;
        while bytes.get(index).is_some_and(|byte| *byte != 0) {
            index += 1;
        }
        if start == index {
            return None;
        }
        argv.push(OsString::from_vec(bytes[start..index].to_vec()));
        while bytes.get(index).is_some_and(|byte| *byte == 0) {
            index += 1;
        }
    }

    (!argv.is_empty()).then_some(argv)
}

#[cfg(target_os = "macos")]
const PROC_PIDPATHINFO_MAXSIZE: usize = 4096;

#[cfg(target_os = "macos")]
#[link(name = "proc")]
unsafe extern "C" {
    fn proc_pidpath(pid: i32, buffer: *mut libc::c_void, buffersize: u32) -> i32;
}

#[cfg(target_os = "macos")]
fn process_exe_path(pid: i32) -> Option<PathBuf> {
    let mut buffer = vec![0u8; PROC_PIDPATHINFO_MAXSIZE];
    // SAFETY: `buffer` is a valid writable output buffer for `proc_pidpath`.
    let length = unsafe {
        proc_pidpath(
            pid,
            buffer.as_mut_ptr().cast(),
            u32::try_from(buffer.len()).ok()?,
        )
    };
    if length <= 0 {
        return None;
    }

    // SAFETY: successful `proc_pidpath` writes a NUL-terminated path into `buffer`.
    let path = unsafe { CStr::from_ptr(buffer.as_ptr().cast()) };
    Some(PathBuf::from(OsStr::from_bytes(path.to_bytes())))
}

fn same_path(left: &Path, right: &Path) -> bool {
    left == right
        || fs::canonicalize(left)
            .ok()
            .zip(fs::canonicalize(right).ok())
            .is_some_and(|(left, right)| left == right)
}

/// Spawn an embers client process with the given arguments and return a guard
/// that ensures cleanup of any orphaned __serve process when dropped.
/// The socket_path should be the path to the socket - if a server needs to be
/// spawned for it, the guard will clean it up.
fn spawn_embers(args: &[&str], socket_path: PathBuf) -> (SpawnedEmbers, PtyHarness) {
    let binary = cargo_bin_path("embers");
    let binary_dir = binary.parent().expect("binary dir");
    let started_server = !socket_path.exists() && !socket_path.with_extension("pid").exists();
    let path = format!(
        "PATH={}:{}",
        binary_dir.display(),
        std::env::var("PATH").unwrap_or_default()
    );
    let mut env_and_args = vec![
        path,
        "SHELL=/bin/sh".to_owned(),
        binary.to_string_lossy().into_owned(),
    ];
    env_and_args.extend(args.iter().map(|arg| (*arg).to_owned()));
    let argv = env_and_args.iter().map(String::as_str).collect::<Vec<_>>();
    let harness = PtyHarness::spawn("/usr/bin/env", &argv, PtySize::new(80, 24))
        .expect("spawn embers in pty");
    (SpawnedEmbers::new(socket_path, started_server), harness)
}

async fn wait_for_socket(socket_path: &Path) {
    for _ in 0..FILE_WAIT_ATTEMPTS {
        if socket_path.exists() {
            return;
        }
        tokio::time::sleep(FILE_WAIT_POLL).await;
    }

    panic!("timed out waiting for socket {}", socket_path.display());
}

async fn populate_scrollback_or_wait(harness: &mut PtyHarness, lines: usize) {
    harness
        .write_all("echo READY\r")
        .expect("write ready command");
    harness
        .read_until_contains("READY", IO_TIMEOUT)
        .unwrap_or_else(|error| panic!("pane ready handshake: {error}"));

    let long_output =
        format!("i=1; while [ $i -le {lines} ]; do echo line-$i; i=$((i+1)); done; echo DONE\r");
    harness
        .write_all(&long_output)
        .expect("write scrolling command");
    harness
        .read_until_contains("DONE", IO_TIMEOUT)
        .unwrap_or_else(|error| panic!("long output completed: {error}"));
    harness
        .wait_for_quiet(QUIET_TIMEOUT, IO_TIMEOUT)
        .unwrap_or_else(|error| panic!("long output settled: {error}"));
    tokio::time::sleep(SCROLLBACK_SETTLE_DELAY).await;
}

fn page_up_until_visible(harness: &mut PtyHarness, needle: &str) {
    let mut last_err = None;
    for _ in 0..PAGE_UP_ATTEMPTS {
        harness.write_all("\x1b[5~").expect("page up");
        match harness.read_until_contains(needle, IO_TIMEOUT) {
            Ok(_) => return,
            Err(error) => last_err = Some(error),
        }
    }

    let last_err = last_err
        .map(|error| error.to_string())
        .unwrap_or_else(|| "no read error captured".to_owned());
    panic!("page up did not reveal `{needle}` within {PAGE_UP_ATTEMPTS} attempts: {last_err}");
}

fn run_pane_command(harness: &mut PtyHarness, command: &str, expected: &str) -> String {
    harness
        .write_all(&format!("{command}\r"))
        .unwrap_or_else(|error| panic!("send pane command `{command}`: {error}"));

    let output = harness
        .read_until_contains(expected, IO_TIMEOUT)
        .unwrap_or_else(|error| {
            panic!("pane command `{command}` did not print `{expected}`: {error}")
        });

    assert!(
        output.contains(expected),
        "pane command `{command}` did not print `{expected}`:\n{output}"
    );

    output
}

fn first_client_id(output: &str) -> u64 {
    output
        .lines()
        .find_map(|line| {
            let mut columns = line.split('\t');
            let client_id = columns.next()?;
            let current_session = columns.next()?;
            if current_session == "-" {
                return None;
            }
            Some(client_id)
        })
        .expect("attached client row present")
        .parse::<u64>()
        .expect("client id parses")
}

#[test]
fn first_client_id_finds_attached_row() {
    let output = "10\t-\t-\n42\t1:main\tall\n";
    assert_eq!(first_client_id(output), 42);
}

fn focused_pane_id(snapshot: &SessionSnapshot) -> u64 {
    snapshot
        .session
        .focused_leaf_id
        .map(|leaf_id| leaf_id.0)
        .expect("session has a focused pane")
}

fn pane_buffer_id(snapshot: &SessionSnapshot, pane_id: u64) -> BufferId {
    snapshot
        .nodes
        .iter()
        .find(|node| node.id == NodeId(pane_id))
        .and_then(|node| node.buffer_view.as_ref())
        .map(|view| view.buffer_id)
        .unwrap_or_else(|| panic!("pane {pane_id} buffer view exists"))
}

fn root_tab_child_id(snapshot: &SessionSnapshot, title: &str) -> NodeId {
    snapshot
        .nodes
        .iter()
        .find(|node| node.id == snapshot.session.root_node_id)
        .and_then(|node| node.tabs.as_ref())
        .and_then(|tabs| {
            tabs.tabs
                .iter()
                .find(|tab| tab.title == title)
                .map(|tab| tab.child_id)
        })
        .unwrap_or_else(|| panic!("root tab `{title}` exists"))
}

fn split_child_order(
    snapshot: &SessionSnapshot,
    first_pane_id: u64,
    second_pane_id: u64,
) -> Option<[u64; 2]> {
    snapshot.nodes.iter().find_map(|node| {
        let split = node.split.as_ref()?;
        let child_ids = split
            .child_ids
            .iter()
            .map(|child| child.0)
            .collect::<Vec<_>>();
        if child_ids.len() == 2
            && child_ids.contains(&first_pane_id)
            && child_ids.contains(&second_pane_id)
        {
            Some([child_ids[0], child_ids[1]])
        } else {
            None
        }
    })
}

async fn disable_echo_in_pane(server: &TestServer, pane_id: u64) {
    let marker = format!("__ECHO_DISABLED_{pane_id}__");
    let mut connection = TestConnection::connect(server.socket_path())
        .await
        .expect("connect protocol client");
    let snapshot = session_snapshot_containing_pane(&mut connection, pane_id).await;
    let buffer_id = pane_buffer_id(&snapshot, pane_id);
    send_buffer_input(
        &mut connection,
        buffer_id,
        format!("stty -echo; printf '{marker}\\n'\r").as_bytes(),
    )
    .await;
    connection
        .wait_for_capture_contains(buffer_id, &marker, IO_TIMEOUT)
        .await
        .expect("echo-disable marker appears");
}

async fn send_buffer_input(connection: &mut TestConnection, buffer_id: BufferId, bytes: &[u8]) {
    let response = connection
        .request(&ClientMessage::Input(InputRequest::Send {
            request_id: new_request_id(),
            buffer_id,
            bytes: bytes.to_vec(),
        }))
        .await
        .expect("send input succeeds");
    assert!(
        matches!(response, ServerResponse::Ok(_)),
        "expected ok response to input send, got {response:?}"
    );
}

async fn wait_for_buffer_activity(
    connection: &mut TestConnection,
    buffer_id: BufferId,
    expected: ActivityState,
) {
    let deadline = tokio::time::Instant::now() + IO_TIMEOUT;
    loop {
        let response = connection
            .request(&ClientMessage::Buffer(BufferRequest::Get {
                request_id: new_request_id(),
                buffer_id,
            }))
            .await
            .expect("buffer get succeeds");
        let activity = match response {
            ServerResponse::Buffer(response) => response.buffer.activity,
            other => panic!("expected buffer response, got {other:?}"),
        };
        if activity == expected {
            return;
        }

        assert!(
            tokio::time::Instant::now() < deadline,
            "timed out waiting for buffer {buffer_id} activity {expected:?}; last activity {activity:?}"
        );
        tokio::time::sleep(Duration::from_millis(50)).await;
    }
}

async fn wait_for_target_pane_buffer(
    connection: &mut TestConnection,
    session_name: &str,
    pane_id: u64,
    expected_buffer_id: BufferId,
) -> SessionSnapshot {
    let deadline = tokio::time::Instant::now() + IO_TIMEOUT;
    loop {
        let snapshot = session_snapshot_by_name(connection, session_name).await;
        if pane_buffer_id(&snapshot, pane_id) == expected_buffer_id {
            return snapshot;
        }

        assert!(
            tokio::time::Instant::now() < deadline,
            "timed out waiting for pane {pane_id} to attach buffer {expected_buffer_id}"
        );
        tokio::time::sleep(Duration::from_millis(50)).await;
    }
}

async fn session_snapshot_containing_pane(
    connection: &mut TestConnection,
    pane_id: u64,
) -> SessionSnapshot {
    let response = connection
        .request(&ClientMessage::Session(SessionRequest::List {
            request_id: new_request_id(),
        }))
        .await
        .expect("list sessions succeeds");
    let sessions = match response {
        ServerResponse::Sessions(response) => response.sessions,
        other => panic!("expected sessions response, got {other:?}"),
    };
    for session in sessions {
        let snapshot = connection
            .session_snapshot(session.id)
            .await
            .expect("session snapshot succeeds");
        if snapshot.nodes.iter().any(|node| node.id == NodeId(pane_id)) {
            return snapshot;
        }
    }
    panic!("pane {pane_id} missing from all sessions");
}

async fn wait_for_split_child_order(
    connection: &mut TestConnection,
    session_name: &str,
    pane_a: u64,
    pane_b: u64,
    expected: [u64; 2],
) -> SessionSnapshot {
    let deadline = tokio::time::Instant::now() + IO_TIMEOUT;
    loop {
        let snapshot = session_snapshot_by_name(connection, session_name).await;
        let current_order = split_child_order(&snapshot, pane_a, pane_b);
        if current_order == Some(expected) {
            return snapshot;
        }

        assert!(
            tokio::time::Instant::now() < deadline,
            "timed out waiting for split order {:?}; last order {:?}",
            expected,
            current_order
        );
        tokio::time::sleep(Duration::from_millis(50)).await;
    }
}

async fn wait_for_visible_snapshot<F>(
    connection: &mut TestConnection,
    buffer_id: BufferId,
    mut predicate: F,
) -> VisibleSnapshotResponse
where
    F: FnMut(&VisibleSnapshotResponse) -> bool,
{
    let deadline = tokio::time::Instant::now() + IO_TIMEOUT;
    loop {
        let snapshot = connection
            .capture_visible_buffer(buffer_id)
            .await
            .expect("visible capture succeeds");
        if predicate(&snapshot) {
            return snapshot;
        }

        assert!(
            tokio::time::Instant::now() < deadline,
            "timed out waiting for visible snapshot predicate; last snapshot: {snapshot:?}"
        );
        tokio::time::sleep(Duration::from_millis(50)).await;
    }
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn embers_without_subcommand_starts_server_and_client() {
    let _guard = acquire_test_lock().await.expect("acquire test lock");
    if !require_pty() {
        return;
    }
    let tempdir = tempfile::tempdir().expect("tempdir");
    let socket_path = tempdir.path().join("embers.sock");
    let socket_arg = socket_path.to_string_lossy().into_owned();
    let (_spawned, mut harness) = spawn_embers(&["--socket", &socket_arg], socket_path.clone());

    harness
        .read_until_contains("[main]", STARTUP_TIMEOUT)
        .expect("client starts and renders");

    let output = run_pane_command(&mut harness, "embers list-sessions", "1\tmain");
    assert!(
        output.contains("1\tmain"),
        "expected list-sessions output in pane:\n{output}"
    );

    wait_for_socket(&socket_path).await;

    let output = cargo_bin("embers")
        .arg("list-sessions")
        .arg("--socket")
        .arg(&socket_path)
        .output()
        .expect("cli command runs");
    assert!(
        output.status.success(),
        "list-sessions failed after client exit:\nstdout:\n{}\nstderr:\n{}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );
    assert!(String::from_utf8_lossy(&output.stdout).contains("1\tmain"));

    harness.write_all("\x11").expect("quit client");
    harness.wait().expect("client exits");

    // spawned.drop() will clean up the orphaned __serve process
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn attach_subcommand_connects_to_running_server() {
    let _guard = acquire_test_lock().await.expect("acquire test lock");
    if !require_pty() {
        return;
    }
    let server = TestServer::start().await.expect("start server");
    let binary = cargo_bin_path("embers");
    let binary_dir = binary.parent().expect("binary dir");
    let shell_path = format!(
        "{}:{}",
        binary_dir.display(),
        std::env::var("PATH").unwrap_or_default()
    );

    run_cli(&server, ["new-session", "main"]);
    run_cli(
        &server,
        vec![
            "new-window".to_owned(),
            "-t".to_owned(),
            "main".to_owned(),
            "--title".to_owned(),
            "shell".to_owned(),
            "--".to_owned(),
            "/usr/bin/env".to_owned(),
            format!("PATH={shell_path}"),
            "/bin/sh".to_owned(),
        ],
    );

    let socket_arg = server.socket_path().to_string_lossy().into_owned();
    let socket_path = server.socket_path().to_path_buf();
    let (_spawned, mut harness) = spawn_embers(&["attach", "--socket", &socket_arg], socket_path);
    harness
        .read_until_contains("[main]", STARTUP_TIMEOUT)
        .expect("attach client renders");

    let output = run_pane_command(&mut harness, "embers list-sessions", "1\tmain");
    assert!(
        output.contains("1\tmain"),
        "expected list-sessions output in attached pane:\n{output}"
    );

    harness.write_all("\x11").expect("quit attached client");
    harness.wait().expect("client exits");
    server.shutdown().await.expect("shutdown server");
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn client_commands_can_switch_and_detach_a_live_attached_client() {
    let _guard = acquire_test_lock().await.expect("acquire test lock");
    if !require_pty() {
        return;
    }
    let server = TestServer::start().await.expect("start server");

    run_cli(&server, ["new-session", "main"]);
    run_cli(
        &server,
        [
            "new-window",
            "-t",
            "main",
            "--title",
            "shell",
            "--",
            "/bin/sh",
        ],
    );
    run_cli(&server, ["new-session", "ops"]);
    run_cli(
        &server,
        [
            "new-window",
            "-t",
            "ops",
            "--title",
            "shell",
            "--",
            "/bin/sh",
        ],
    );

    let socket_arg = server.socket_path().to_string_lossy().into_owned();
    let socket_path = server.socket_path().to_path_buf();
    let (_spawned, mut harness) = spawn_embers(
        &["attach", "--socket", &socket_arg, "-t", "main"],
        socket_path,
    );
    harness
        .read_until_contains("[main]", STARTUP_TIMEOUT)
        .expect("attach client renders main");

    let clients = run_cli(&server, ["list-clients"]);
    let client_id = first_client_id(&stdout(&clients));

    run_cli(
        &server,
        ["switch-client", &client_id.to_string(), "-t", "ops"],
    );
    harness
        .read_until_contains("[ops]", IO_TIMEOUT)
        .expect("switch-client retargets the live client");

    run_cli(&server, ["detach-client", &client_id.to_string()]);
    harness.wait().expect("client exits after detach");

    server.shutdown().await.expect("shutdown server");
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn buffer_reveal_switches_the_attached_client_to_the_buffer_session() {
    let _guard = acquire_test_lock().await.expect("acquire test lock");
    if !require_pty() {
        return;
    }
    let server = TestServer::start().await.expect("start server");

    run_cli(&server, ["new-session", "main"]);
    run_cli(
        &server,
        [
            "new-window",
            "-t",
            "main",
            "--title",
            "shell",
            "--",
            "/bin/sh",
        ],
    );
    run_cli(&server, ["new-session", "ops"]);
    run_cli(
        &server,
        [
            "new-window",
            "-t",
            "ops",
            "--title",
            "logs",
            "--",
            "/bin/sh",
        ],
    );

    let mut connection = TestConnection::connect(server.socket_path())
        .await
        .expect("connect protocol client");
    let ops_snapshot = session_snapshot_by_name(&mut connection, "ops").await;
    let ops_buffer_id = ops_snapshot
        .session
        .focused_leaf_id
        .and_then(|leaf_id| {
            ops_snapshot
                .nodes
                .iter()
                .find(|node| node.id == leaf_id)
                .and_then(|node| node.buffer_view.as_ref())
                .map(|view| view.buffer_id.0)
        })
        .expect("ops focused buffer id exists");

    let socket_arg = server.socket_path().to_string_lossy().into_owned();
    let socket_path = server.socket_path().to_path_buf();
    let (_spawned, mut harness) = spawn_embers(
        &["attach", "--socket", &socket_arg, "-t", "main"],
        socket_path,
    );
    harness
        .read_until_contains("[main]", STARTUP_TIMEOUT)
        .expect("attach client renders main");

    run_cli(&server, ["buffer", "reveal", &ops_buffer_id.to_string()]);
    harness
        .read_until_contains("[ops]", IO_TIMEOUT)
        .expect("buffer reveal retargets the live client");

    harness.write_all("\x11").expect("quit attached client");
    harness.wait().expect("client exits");
    server.shutdown().await.expect("shutdown server");
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn page_up_enters_local_scrollback() {
    let _guard = acquire_test_lock().await.expect("acquire test lock");
    if !require_pty() {
        return;
    }
    let tempdir = tempfile::tempdir().expect("tempdir");
    let socket_path = tempdir.path().join("embers.sock");
    let socket_arg = socket_path.to_string_lossy().into_owned();
    let (_spawned, mut harness) = spawn_embers(&["--socket", &socket_arg], socket_path.clone());

    harness
        .read_until_contains("[main]", STARTUP_TIMEOUT)
        .expect("client starts and renders");
    populate_scrollback_or_wait(&mut harness, 40).await;

    page_up_until_visible(&mut harness, "line-1");

    harness.write_all("\x11").expect("quit client");
    harness.wait().expect("client exits");

    // spawned.drop() will clean up the orphaned __serve process
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn local_selection_yank_emits_osc52_clipboard_sequence() {
    let _guard = acquire_test_lock().await.expect("acquire test lock");
    if !require_pty() {
        return;
    }
    let tempdir = tempfile::tempdir().expect("tempdir");
    let socket_path = tempdir.path().join("embers.sock");
    let socket_arg = socket_path.to_string_lossy().into_owned();
    let (_spawned, mut harness) = spawn_embers(&["--socket", &socket_arg], socket_path.clone());

    harness
        .read_until_contains("[main]", STARTUP_TIMEOUT)
        .expect("client starts and renders");
    populate_scrollback_or_wait(&mut harness, 40).await;

    page_up_until_visible(&mut harness, "line-1");
    harness
        .wait_for_quiet(Duration::from_millis(200), IO_TIMEOUT)
        .unwrap_or_else(|error| panic!("scrollback render settled: {error}"));
    harness.write_all("vly").expect("select and yank");
    let output = harness
        .read_until_contains("]52;c;", IO_TIMEOUT)
        .expect("osc52 emitted");
    assert!(output.contains("]52;c;"));

    harness.write_all("\x11").expect("quit client");
    harness.wait().expect("client exits");

    // spawned.drop() will clean up the orphaned __serve process
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn scripted_input_bindings_reach_the_live_terminal_in_pty() {
    let _guard = acquire_test_lock().await.expect("acquire test lock");
    if !require_pty() {
        return;
    }
    let server = TestServer::start().await.expect("start server");

    run_cli(&server, ["new-session", "main"]);
    run_cli(
        &server,
        [
            "new-window",
            "-t",
            "main",
            "--title",
            "shell",
            "--",
            "/bin/sh",
        ],
    );

    let tempdir = tempfile::tempdir().expect("tempdir");
    let config_path = tempdir.path().join("config.rhai");
    fs::write(
        &config_path,
        r#"bind("normal", "<C-g>", action.send_bytes_current("echo scripted-pty\r"))"#,
    )
    .expect("write config");

    let socket_arg = server.socket_path().to_string_lossy().into_owned();
    let socket_path = server.socket_path().to_path_buf();
    let config_arg = config_path.to_string_lossy().into_owned();
    let (_spawned, mut harness) = spawn_embers(
        &[
            "attach",
            "--socket",
            &socket_arg,
            "--config",
            &config_arg,
            "-t",
            "main",
        ],
        socket_path,
    );
    harness
        .read_until_contains("[main]", STARTUP_TIMEOUT)
        .expect("attach client renders");
    harness
        .write_all("stty -echo\r")
        .expect("disable shell echo in focused pane");
    harness
        .wait_for_quiet(QUIET_TIMEOUT, IO_TIMEOUT)
        .expect("focused shell settles");

    let mut connection = TestConnection::connect(server.socket_path())
        .await
        .expect("connect protocol client");
    let snapshot = session_snapshot_by_name(&mut connection, "main").await;
    let buffer_id = pane_buffer_id(&snapshot, focused_pane_id(&snapshot));

    harness.write_all("\x07").expect("trigger scripted binding");
    harness
        .read_until_contains("scripted-pty", IO_TIMEOUT)
        .expect("scripted output renders");
    connection
        .wait_for_capture_contains(buffer_id, "scripted-pty", IO_TIMEOUT)
        .await
        .expect("scripted output reaches focused buffer");

    harness.write_all("\x11").expect("quit attached client");
    harness.wait().expect("client exits");
    server.shutdown().await.expect("shutdown server");
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn config_reload_updates_live_bindings_without_breaking_terminal_io() {
    let _guard = acquire_test_lock().await.expect("acquire test lock");
    if !require_pty() {
        return;
    }
    let server = TestServer::start().await.expect("start server");

    run_cli(&server, ["new-session", "main"]);
    run_cli(
        &server,
        [
            "new-window",
            "-t",
            "main",
            "--title",
            "shell",
            "--",
            "/bin/sh",
        ],
    );

    let tempdir = tempfile::tempdir().expect("tempdir");
    let config_path = tempdir.path().join("config.rhai");
    fs::write(
        &config_path,
        r#"bind("normal", "<C-g>", action.send_bytes_current("echo before-reload\r"))"#,
    )
    .expect("write initial config");

    let socket_arg = server.socket_path().to_string_lossy().into_owned();
    let socket_path = server.socket_path().to_path_buf();
    let config_arg = config_path.to_string_lossy().into_owned();
    let (_spawned, mut harness) = spawn_embers(
        &[
            "attach",
            "--socket",
            &socket_arg,
            "--config",
            &config_arg,
            "-t",
            "main",
        ],
        socket_path,
    );
    harness
        .read_until_contains("[main]", STARTUP_TIMEOUT)
        .expect("attach client renders");
    harness
        .write_all("stty -echo\r")
        .expect("disable shell echo in focused pane");
    harness
        .wait_for_quiet(QUIET_TIMEOUT, IO_TIMEOUT)
        .expect("focused shell settles");

    harness.write_all("\x07").expect("trigger initial binding");
    harness
        .read_until_contains("before-reload", IO_TIMEOUT)
        .expect("initial binding renders");

    fs::write(
        &config_path,
        r#"bind("normal", "<C-g>", action.send_bytes_current("echo after-reload\r"))"#,
    )
    .expect("write reloaded config");
    let previous_mtime = FileTime::from_last_modification_time(
        &fs::metadata(&config_path).expect("read reloaded config metadata"),
    );
    let next_mtime = FileTime::from_unix_time(
        previous_mtime.unix_seconds() + 1,
        previous_mtime.nanoseconds(),
    );
    filetime::set_file_mtime(&config_path, next_mtime).expect("bump reloaded config mtime");
    let reload_deadline = tokio::time::Instant::now() + IO_TIMEOUT;
    loop {
        harness.write_all("\x07").expect("trigger reloaded binding");
        if harness
            .read_until_contains("after-reload", Duration::from_millis(200))
            .is_ok()
        {
            break;
        }
        assert!(
            tokio::time::Instant::now() < reload_deadline,
            "timed out waiting for reloaded binding to activate"
        );
    }

    let output = run_pane_command(&mut harness, "echo still-live", "still-live");
    assert!(
        output.contains("still-live"),
        "regular terminal input must still work after reload:\n{output}"
    );

    harness.write_all("\x11").expect("quit attached client");
    harness.wait().expect("client exits");
    server.shutdown().await.expect("shutdown server");
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn live_pty_client_preserves_buffers_across_layout_and_attachment_changes() {
    let _guard = acquire_test_lock().await.expect("acquire test lock");
    if !require_pty() {
        return;
    }
    let server = TestServer::start().await.expect("start server");

    run_cli(&server, ["new-session", "main"]);
    run_cli(
        &server,
        [
            "new-window",
            "-t",
            "main",
            "--title",
            "shell",
            "--",
            "/bin/sh",
        ],
    );

    let socket_arg = server.socket_path().to_string_lossy().into_owned();
    let socket_path = server.socket_path().to_path_buf();
    let (_spawned, mut harness) = spawn_embers(
        &["attach", "--socket", &socket_arg, "-t", "main"],
        socket_path,
    );
    harness
        .read_until_contains("[main]", STARTUP_TIMEOUT)
        .expect("attach client renders");

    let split = run_cli(&server, ["split-window", "--", "/bin/sh"]);
    let moving_pane_id = stdout(&split)
        .trim()
        .parse::<u64>()
        .expect("split-window returns new pane id");
    disable_echo_in_pane(&server, moving_pane_id).await;

    let mut connection = TestConnection::connect(server.socket_path())
        .await
        .expect("connect protocol client");
    let snapshot = session_snapshot_by_name(&mut connection, "main").await;
    let anchor_pane_id = snapshot
        .nodes
        .iter()
        .filter(|node| node.buffer_view.is_some())
        .map(|node| node.id.0)
        .find(|pane_id| *pane_id != moving_pane_id)
        .expect("anchor pane exists");
    let moving_buffer_id = pane_buffer_id(&snapshot, moving_pane_id);

    run_cli(
        &server,
        [
            "send-keys",
            "-t",
            &moving_pane_id.to_string(),
            "--enter",
            "echo",
            "split-live",
        ],
    );
    connection
        .wait_for_capture_contains(moving_buffer_id, "split-live", IO_TIMEOUT)
        .await
        .expect("split pane keeps running");
    harness
        .read_until_contains("split-live", IO_TIMEOUT)
        .expect("split output renders in attached client");

    let initial_order = split_child_order(&snapshot, anchor_pane_id, moving_pane_id)
        .expect("split containing anchor and moving panes exists");
    let expected_order = if initial_order == [anchor_pane_id, moving_pane_id] {
        run_cli(
            &server,
            [
                "node",
                "move-before",
                &moving_pane_id.to_string(),
                &anchor_pane_id.to_string(),
            ],
        );
        [moving_pane_id, anchor_pane_id]
    } else {
        run_cli(
            &server,
            [
                "node",
                "move-after",
                &moving_pane_id.to_string(),
                &anchor_pane_id.to_string(),
            ],
        );
        [anchor_pane_id, moving_pane_id]
    };
    let _moved_snapshot = wait_for_split_child_order(
        &mut connection,
        "main",
        anchor_pane_id,
        moving_pane_id,
        expected_order,
    )
    .await;

    run_cli(
        &server,
        [
            "send-keys",
            "-t",
            &moving_pane_id.to_string(),
            "--enter",
            "echo",
            "moved-live",
        ],
    );
    connection
        .wait_for_capture_contains(moving_buffer_id, "moved-live", IO_TIMEOUT)
        .await
        .expect("moved pane keeps running");
    harness
        .read_until_contains("moved-live", IO_TIMEOUT)
        .expect("moved pane output still renders");

    let response = connection
        .request(&ClientMessage::Buffer(BufferRequest::Detach {
            request_id: new_request_id(),
            buffer_id: moving_buffer_id,
        }))
        .await
        .expect("detach buffer succeeds");
    assert!(
        matches!(response, ServerResponse::Ok(_)),
        "expected ok response to buffer detach, got {response:?}"
    );

    send_buffer_input(&mut connection, moving_buffer_id, b"echo detached-live\r").await;
    connection
        .wait_for_capture_contains(moving_buffer_id, "detached-live", IO_TIMEOUT)
        .await
        .expect("detached buffer continues receiving output");

    run_cli(
        &server,
        [
            "attach-buffer",
            &moving_buffer_id.to_string(),
            "-t",
            &anchor_pane_id.to_string(),
        ],
    );
    let _reattached_snapshot =
        wait_for_target_pane_buffer(&mut connection, "main", anchor_pane_id, moving_buffer_id)
            .await;

    send_buffer_input(&mut connection, moving_buffer_id, b"echo reattach-live\r").await;
    connection
        .wait_for_capture_contains(moving_buffer_id, "reattach-live", IO_TIMEOUT)
        .await
        .expect("reattached buffer continues receiving output");
    harness
        .read_until_contains("reattach-live", IO_TIMEOUT)
        .expect("reattached buffer output renders in attached client");

    harness.write_all("\x11").expect("quit attached client");
    harness.wait().expect("client exits");
    server.shutdown().await.expect("shutdown server");
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn hidden_buffer_bells_surface_in_the_attached_client_and_reveal_buffered_output() {
    let _guard = acquire_test_lock().await.expect("acquire test lock");
    if !require_pty() {
        return;
    }
    let server = TestServer::start().await.expect("start server");

    run_cli(&server, ["new-session", "main"]);
    run_cli(
        &server,
        [
            "new-window",
            "-t",
            "main",
            "--title",
            "shell",
            "--",
            "/bin/sh",
        ],
    );
    run_cli(
        &server,
        ["new-window", "-t", "main", "--title", "bg", "--", "/bin/sh"],
    );
    run_cli(&server, ["select-window", "-t", "main:0"]);

    let mut connection = TestConnection::connect(server.socket_path())
        .await
        .expect("connect protocol client");
    let snapshot = session_snapshot_by_name(&mut connection, "main").await;
    let hidden_pane_id = root_tab_child_id(&snapshot, "bg").0;
    let hidden_buffer_id = pane_buffer_id(&snapshot, hidden_pane_id);
    disable_echo_in_pane(&server, hidden_pane_id).await;

    let socket_arg = server.socket_path().to_string_lossy().into_owned();
    let socket_path = server.socket_path().to_path_buf();
    let (_spawned, mut harness) = spawn_embers(
        &["attach", "--socket", &socket_arg, "-t", "main"],
        socket_path,
    );
    harness
        .read_until_contains("[main]", STARTUP_TIMEOUT)
        .expect("attach client renders");

    send_buffer_input(
        &mut connection,
        hidden_buffer_id,
        b"printf 'hidden-bell\\n\\a'; sleep 0.5\r",
    )
    .await;
    connection
        .wait_for_capture_contains(hidden_buffer_id, "hidden-bell", IO_TIMEOUT)
        .await
        .expect("hidden buffer output accumulates");
    wait_for_buffer_activity(&mut connection, hidden_buffer_id, ActivityState::Bell).await;
    harness
        .read_until_contains("!bg", IO_TIMEOUT)
        .expect("hidden bell updates tab marker in attached client");

    run_cli(&server, ["select-window", "-t", "main:bg"]);
    harness
        .read_until_contains("hidden-bell", IO_TIMEOUT)
        .expect("revealed hidden buffer shows accumulated output");

    harness.write_all("\x11").expect("quit attached client");
    harness.wait().expect("client exits");
    server.shutdown().await.expect("shutdown server");
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn fullscreen_terminal_transitions_render_in_the_live_client_pty() {
    let _guard = acquire_test_lock().await.expect("acquire test lock");
    if !require_pty() {
        return;
    }
    let server = TestServer::start().await.expect("start server");

    run_cli(&server, ["new-session", "main"]);
    run_cli(
        &server,
        [
            "new-window",
            "-t",
            "main",
            "--title",
            "shell",
            "--",
            "/bin/sh",
        ],
    );

    let mut connection = TestConnection::connect(server.socket_path())
        .await
        .expect("connect protocol client");
    let snapshot = session_snapshot_by_name(&mut connection, "main").await;
    let buffer_id = pane_buffer_id(&snapshot, focused_pane_id(&snapshot));

    let socket_arg = server.socket_path().to_string_lossy().into_owned();
    let socket_path = server.socket_path().to_path_buf();
    let (_spawned, mut harness) = spawn_embers(
        &["attach", "--socket", &socket_arg, "-t", "main"],
        socket_path,
    );
    harness
        .read_until_contains("[main]", STARTUP_TIMEOUT)
        .expect("attach client renders");
    harness
        .write_all("stty -echo\r")
        .expect("disable shell echo in focused pane");
    harness
        .wait_for_quiet(QUIET_TIMEOUT, IO_TIMEOUT)
        .expect("focused shell settles");

    harness
        .write_all(
            "printf '\\033[?1049h\\033[2J\\033[HPTY-FULLSCREEN'; sleep 1; printf '\\033[?1049lPTY-RESTORED\\n'\r",
        )
        .expect("run fullscreen fixture");
    harness
        .read_until_contains("PTY-FULLSCREEN", IO_TIMEOUT)
        .expect("fullscreen output renders in live client");

    let live = wait_for_visible_snapshot(&mut connection, buffer_id, |snapshot| {
        snapshot.alternate_screen
            && snapshot
                .lines
                .iter()
                .any(|line| line.contains("PTY-FULLSCREEN"))
    })
    .await;
    assert!(live.alternate_screen);

    harness
        .read_until_contains("PTY-RESTORED", IO_TIMEOUT)
        .expect("primary screen restoration renders in live client");
    let restored = wait_for_visible_snapshot(&mut connection, buffer_id, |snapshot| {
        !snapshot.alternate_screen
            && snapshot
                .lines
                .iter()
                .any(|line| line.contains("PTY-RESTORED"))
    })
    .await;
    assert!(!restored.alternate_screen);

    harness.write_all("\x11").expect("quit attached client");
    harness.wait().expect("client exits");
    server.shutdown().await.expect("shutdown server");
}