croft-software 0.2.132

VSCode-style TUI written in Rust
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
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
use assert_cmd::Command;

#[test]
fn help_flag_prints_usage_and_exits_zero() {
    let out = Command::cargo_bin("croft").unwrap().arg("--help").assert();
    let out = out.success();
    let stdout = String::from_utf8(out.get_output().stdout.clone()).unwrap();
    assert!(stdout.contains("Usage:"), "stdout was: {stdout}");
    assert!(stdout.contains("setup-terminal"));
}

#[test]
fn version_flag_prints_version() {
    let out = Command::cargo_bin("croft")
        .unwrap()
        .arg("--version")
        .assert();
    let out = out.success();
    let stdout = String::from_utf8(out.get_output().stdout.clone()).unwrap();
    assert!(stdout.contains("croft"));
}

/// #282 end to end, against the real binary: `--version` is a bare `x.y.z`
/// and the provenance is reachable behind `--build-info`. The unit test pins
/// the constants and clap's rendering; this pins that the `--build-info`
/// handler actually runs and prints the verbose form.
#[test]
fn build_info_carries_provenance_and_version_does_not() {
    let out = Command::cargo_bin("croft")
        .unwrap()
        .arg("--build-info")
        .assert();
    let out = out.success();
    let build_info = String::from_utf8(out.get_output().stdout.clone()).unwrap();
    assert!(
        build_info.contains(env!("CARGO_PKG_VERSION")) && build_info.contains("built "),
        "--build-info carries version and build time: {build_info}"
    );

    let out = Command::cargo_bin("croft")
        .unwrap()
        .arg("--version")
        .assert();
    let out = out.success();
    let version = String::from_utf8(out.get_output().stdout.clone()).unwrap();
    assert_eq!(
        version.trim(),
        format!("croft {}", env!("CARGO_PKG_VERSION")),
        "--version stays bare"
    );
}

#[test]
fn setup_terminal_help_works() {
    Command::cargo_bin("croft")
        .unwrap()
        .args(["setup-terminal", "--help"])
        .assert()
        .success();
}

/// The remote launch script runs `croft session-host --probe` (and the
/// collab-relay twin) over SSH on every connect as a pure liveness check.
/// A remote macOS host hands SSH sessions the stripped launchd PATH, so if
/// the GUI-PATH repair ran before the probe early-out, every attach would
/// stall on a login-shell probe — against the "remote attach never waits"
/// invariant. The probe subcommands must answer without consulting a shell.
#[test]
fn probe_subcommands_answer_instantly_even_with_a_stripped_path() {
    let dir = tempfile::tempdir().unwrap();
    let slow_shell = dir.path().join("slow-shell");
    std::fs::write(&slow_shell, "#!/bin/sh\nsleep 30\n").unwrap();
    let mut perm = std::fs::metadata(&slow_shell).unwrap().permissions();
    std::os::unix::fs::PermissionsExt::set_mode(&mut perm, 0o755);
    std::fs::set_permissions(&slow_shell, perm).unwrap();
    for sub in ["session-host", "collab-relay"] {
        let start = std::time::Instant::now();
        Command::cargo_bin("croft")
            .unwrap()
            .args([sub, "--probe"])
            .env("PATH", "/usr/bin:/bin:/usr/sbin:/sbin")
            .env("SHELL", &slow_shell)
            .assert()
            .success();
        // The discriminant is the 30s sleeping shell: a probe that runs the
        // PATH repair blocks on it, one that early-outs answers in well
        // under a second. The measured span also covers the cargo_bin
        // spawn, which a suite-saturated box once pushed past a 5s ceiling
        // (#50) — half the sleep keeps the failure mode unreachable while
        // giving load an order of magnitude more headroom.
        assert!(
            start.elapsed() < std::time::Duration::from_secs(15),
            "{sub} --probe took {:?} with a stripped PATH",
            start.elapsed()
        );
    }
}

#[test]
fn nonexistent_path_fails_cleanly() {
    Command::cargo_bin("croft")
        .unwrap()
        .arg("/nonexistent/path/that/does/not/exist/abc123")
        .assert()
        .failure();
}

/// #917: `croft pr` without a usable GitHub CLI says so in the shell, in
/// one line naming the fix, instead of opening an editor whose review tab
/// can never load.
#[test]
fn pr_without_a_usable_gh_says_how_to_fix_it_before_starting() {
    use std::os::unix::fs::PermissionsExt;
    let tmp = tempfile::tempdir().unwrap();
    let bin = tmp.path().join("bin");
    std::fs::create_dir(&bin).unwrap();
    let pr = |expected: &str| {
        let out = Command::cargo_bin("croft")
            .unwrap()
            .args(["pr", "42"])
            .current_dir(tmp.path())
            .env("PATH", &bin)
            .env("HOME", tmp.path())
            // A token in the environment would vouch for the stand-in.
            .env_remove("GH_TOKEN")
            .env_remove("GITHUB_TOKEN")
            .env_remove("GH_ENTERPRISE_TOKEN")
            .env_remove("GITHUB_ENTERPRISE_TOKEN")
            .timeout(std::time::Duration::from_secs(30))
            .assert()
            .failure()
            .code(1);
        let stderr = String::from_utf8(out.get_output().stderr.clone()).unwrap();
        assert!(stderr.starts_with("croft pr: "), "{stderr}");
        assert!(stderr.contains(expected), "{stderr}");
        assert_eq!(stderr.trim().lines().count(), 1, "{stderr}");
    };
    pr("install it from https://cli.github.com, then run gh auth login");
    let gh = bin.join("gh");
    std::fs::write(&gh, "#!/bin/sh\necho 'no oauth token found' >&2\nexit 1\n").unwrap();
    std::fs::set_permissions(&gh, std::fs::Permissions::from_mode(0o755)).unwrap();
    pr("not signed in; run gh auth login");
}

/// A workspace with a devcontainer.json, and a stand-in `docker` that logs
/// each call to `docker.log` and knows one container for it, made from an
/// older config with its workspace at `/workspaces/before-the-edit`.
/// `docker top` lists `$DOCKER_TOP` as its processes, or fails for `fail`.
#[cfg(unix)]
fn devcontainer_with_a_stale_container() -> tempfile::TempDir {
    use std::os::unix::fs::PermissionsExt;
    let tmp = tempfile::tempdir().unwrap();
    let ws = tmp.path().join("ws/.devcontainer");
    std::fs::create_dir_all(&ws).unwrap();
    std::fs::write(
        ws.join("devcontainer.json"),
        "// A comment added since.\n{ \"image\": \"ubuntu:24.04\" }\n",
    )
    .unwrap();
    let bin = tmp.path().join("bin");
    std::fs::create_dir(&bin).unwrap();
    let docker = bin.join("docker");
    std::fs::write(
        &docker,
        r#"#!/bin/sh
echo "$*" >> "$DOCKER_LOG"
case "$1" in
  ps) echo c0ffee000001 ;;
  inspect) case "$3" in
    *Running*) echo true ;;
    *Labels*) echo older-config ;;
    *WorkingDir*) echo /workspaces/before-the-edit ;;
  esac ;;
  top) [ "$DOCKER_TOP" = fail ] && exit 1
    echo 'UID PID PPID C STIME TTY TIME CMD'; echo "$DOCKER_TOP" ;;
  exec) case "$*" in *uname*) echo x86_64 ;; esac ;;
esac
"#,
    )
    .unwrap();
    std::fs::set_permissions(&docker, std::fs::Permissions::from_mode(0o755)).unwrap();
    tmp
}

#[cfg(unix)]
fn run_devcontainer(tmp: &std::path::Path, args: &[&str], top: &str) -> assert_cmd::assert::Assert {
    Command::cargo_bin("croft")
        .unwrap()
        .arg("devcontainer")
        .arg(tmp.join("ws"))
        .args(args)
        .env("PATH", tmp.join("bin"))
        .env("HOME", tmp)
        .env("DOCKER_LOG", tmp.join("docker.log"))
        .env("DOCKER_TOP", top)
        .env("CROFT_DEVCONTAINER_BINARY", "/bin/true")
        .timeout(std::time::Duration::from_secs(30))
        .assert()
}

/// #1110: an edited devcontainer.json used to `docker rm -f` the container
/// on the next run, killing a croft still running in it with its unsaved
/// edits and everything made inside it. The container is kept, and the run
/// says how to replace it. Until then, croft opens in the workspace folder
/// the container was made with.
#[cfg(unix)]
#[test]
fn devcontainer_keeps_a_container_whose_config_changed() {
    let tmp = devcontainer_with_a_stale_container();
    let out = run_devcontainer(tmp.path(), &[], "root 42 0 0 10:01 pts/0 00:00:01 sh").success();
    let stderr = String::from_utf8(out.get_output().stderr.clone()).unwrap();
    assert!(
        stderr.contains(
            "devcontainer.json changed since this container was created; run \
             `croft devcontainer --rebuild` to recreate it"
        ),
        "{stderr}"
    );
    let calls = std::fs::read_to_string(tmp.path().join("docker.log")).unwrap();
    assert!(!calls.contains("rm "), "{calls}");
    let attach = calls.lines().find(|l| l.starts_with("exec -it")).unwrap();
    assert!(
        attach.ends_with("-w /workspaces/before-the-edit c0ffee000001 /usr/local/bin/croft /workspaces/before-the-edit"),
        "{attach}"
    );
}

/// #1110: `--rebuild` refuses while a croft runs in the container from
/// another terminal, or when docker can't say whether one does, and
/// replaces it once none does.
#[cfg(unix)]
#[test]
fn devcontainer_rebuild_waits_for_the_croft_running_in_it() {
    let tmp = devcontainer_with_a_stale_container();
    let attached = "root 42 0 0 10:01 pts/0 00:00:01 /usr/local/bin/croft /workspaces/ws";
    let out = run_devcontainer(tmp.path(), &["--rebuild"], attached).failure();
    let stderr = String::from_utf8(out.get_output().stderr.clone()).unwrap();
    assert!(
        stderr.contains("croft is still running in this container, in another terminal"),
        "{stderr}"
    );
    let log = tmp.path().join("docker.log");
    let out = run_devcontainer(tmp.path(), &["--rebuild"], "fail").failure();
    let stderr = String::from_utf8(out.get_output().stderr.clone()).unwrap();
    assert!(
        stderr.contains("can't tell whether croft is running in the container"),
        "{stderr}"
    );
    assert!(!std::fs::read_to_string(&log).unwrap().contains("rm "));
    run_devcontainer(tmp.path(), &["--rebuild"], "root 7 0 0 10:01 ? 00:00:00 sh").success();
    let calls = std::fs::read_to_string(&log).unwrap();
    assert!(calls.contains("rm -f c0ffee000001"), "{calls}");
}

/// The persisted pair record is the baseline for every `croft pair`
/// invocation: `--off` (with or without provider flags) and a plain
/// re-activation must both keep a custom ollama endpoint. Resolving bare
/// flags in isolation used to clobber the custom URL with the default and
/// silently converted a disabled ollama record into a cloud claude seat.
#[test]
fn pair_off_and_reactivation_keep_the_recorded_backend() {
    // A SHORT home: the activation path binds a relay socket under
    // $HOME/.cache/croft/sessions, and the default tempdir base pushes the
    // path past the AF_UNIX 104-byte limit.
    let home = tempfile::Builder::new()
        .prefix("h")
        .tempdir_in("/tmp")
        .unwrap();
    let ws = tempfile::tempdir().unwrap();
    let run = |args: &[&str]| {
        Command::cargo_bin("croft")
            .unwrap()
            .args(args)
            .arg("--workspace")
            .arg(ws.path())
            .env("HOME", home.path())
            .assert()
            .success();
    };
    run(&[
        "pair",
        "--provider",
        "ollama",
        "--model",
        "qwen3-coder:30b",
        "--base-url",
        "http://localhost:9999",
    ]);
    let record_path = || {
        let dir = home.path().join(".cache/croft/sessions");
        std::fs::read_dir(&dir)
            .unwrap()
            .filter_map(|e| e.ok())
            .map(|e| e.path())
            .find(|p| p.to_string_lossy().ends_with(".pair.json"))
            .expect("a pair record exists")
    };
    let read = || std::fs::read_to_string(record_path()).unwrap();
    assert!(read().contains("http://localhost:9999"));
    // Deactivate with provider flags but no URL: the custom URL survives.
    run(&["pair", "--provider", "ollama", "--off"]);
    let r = read();
    assert!(r.contains("\"enabled\":false"), "{r}");
    assert!(
        r.contains("http://localhost:9999"),
        "the default endpoint must not clobber the custom one: {r}"
    );
    // Plain re-activation: the whole recorded backend comes back.
    run(&["pair"]);
    let r = read();
    assert!(r.contains("\"enabled\":true"), "{r}");
    assert!(r.contains("\"provider\":\"ollama\""), "{r}");
    assert!(r.contains("http://localhost:9999"), "{r}");
    assert!(r.contains("qwen3-coder:30b"), "{r}");
    // An explicit provider SWITCH starts fresh: the other backend's
    // endpoint and model must not ride along into the claude record.
    run(&["pair", "--provider", "claude"]);
    let r = read();
    assert!(r.contains("\"provider\":\"claude\""), "{r}");
    assert!(
        !r.contains("http://localhost:9999"),
        "the ollama endpoint must not survive a provider switch: {r}"
    );
    assert!(!r.contains("qwen3-coder:30b"), "{r}");
    reap_pair_relays(home.path());
}

/// Activation detaches a relay for the workspace; reap the ones under
/// `home` so test runs do not accumulate idle processes.
fn reap_pair_relays(home: &std::path::Path) {
    let sessions = home.join(".cache/croft/sessions");
    if let Ok(entries) = std::fs::read_dir(&sessions) {
        for e in entries.filter_map(|e| e.ok()) {
            let p = e.path();
            if p.to_string_lossy().ends_with(".collab.sock") {
                let _ = std::process::Command::new("pkill")
                    .args(["-f", &p.to_string_lossy()])
                    .status();
            }
        }
    }
}

/// #1907: the navigator's name persists like its model. A plain `croft pair`
/// or `croft pair --off` keeps the recorded name instead of renaming the
/// seat back to `claude`; only an explicit `--name` changes it.
#[test]
fn pair_rerun_without_name_keeps_the_recorded_name() {
    // A SHORT home, for the relay socket's AF_UNIX path limit (see above).
    let home = tempfile::Builder::new()
        .prefix("h")
        .tempdir_in("/tmp")
        .unwrap();
    let ws = tempfile::tempdir().unwrap();
    let run = |args: &[&str]| {
        let out = Command::cargo_bin("croft")
            .unwrap()
            .args(args)
            .arg("--workspace")
            .arg(ws.path())
            .env("HOME", home.path())
            .assert()
            .success();
        String::from_utf8(out.get_output().stdout.clone()).unwrap()
    };
    let read = || {
        let dir = home.path().join(".cache/croft/sessions");
        let path = std::fs::read_dir(&dir)
            .unwrap()
            .filter_map(|e| e.ok())
            .map(|e| e.path())
            .find(|p| p.to_string_lossy().ends_with(".pair.json"))
            .expect("a pair record exists");
        std::fs::read_to_string(path).unwrap()
    };
    run(&[
        "pair",
        "--name",
        "navigator",
        "--model",
        "claude-haiku-4-5-20251001",
    ]);
    assert!(read().contains("\"name\":\"navigator\""));
    run(&["pair", "--off"]);
    let r = read();
    assert!(r.contains("\"enabled\":false"), "{r}");
    assert!(
        r.contains("\"name\":\"navigator\""),
        "--off keeps the name: {r}"
    );
    let out = run(&["pair"]);
    let r = read();
    assert!(r.contains("\"enabled\":true"), "{r}");
    assert!(
        r.contains("\"name\":\"navigator\""),
        "re-activation keeps the name: {r}"
    );
    assert!(r.contains("claude-haiku-4-5-20251001"), "{r}");
    assert!(out.contains("navigator 'navigator' active"), "{out}");
    run(&["pair", "--name", "other"]);
    assert!(read().contains("\"name\":\"other\""));
    reap_pair_relays(home.path());
}

/// #362 acceptance criterion 3, against the real binary: outside croft the
/// command exits 1 with a one-line explanation rather than trying to render.
///
/// `env_remove` rather than a bare run: this test suite is itself often
/// launched from a croft pane, where `CROFT_VIEW_SOCK` is set and inherited,
/// and the test would then quietly exercise the connected path instead.
#[test]
fn view_outside_croft_exits_one_with_an_explanation() {
    let out = Command::cargo_bin("croft")
        .unwrap()
        .env_remove("CROFT_VIEW_SOCK")
        .args(["view", "Cargo.toml"])
        .assert();
    let out = out.failure().code(1);
    let stderr = String::from_utf8(out.get_output().stderr.clone()).unwrap();
    assert!(
        stderr.contains("croft view needs a croft"),
        "stderr must say why, was: {stderr}"
    );
    assert_eq!(
        stderr.lines().filter(|l| !l.trim().is_empty()).count(),
        1,
        "one line, not a backtrace: {stderr}"
    );
}

/// An empty `CROFT_VIEW_SOCK` must read as absent, not as a socket at "".
#[test]
fn view_treats_an_empty_socket_variable_as_no_croft_at_all() {
    let out = Command::cargo_bin("croft")
        .unwrap()
        .env("CROFT_VIEW_SOCK", "")
        .args(["view", "Cargo.toml"])
        .assert();
    let out = out.failure().code(1);
    let stderr = String::from_utf8(out.get_output().stderr.clone()).unwrap();
    assert!(
        stderr.contains("croft view needs a croft"),
        "stderr was: {stderr}"
    );
}

/// A stale socket path (the croft that set it has exited) must name that
/// situation rather than surfacing a raw connect error.
#[test]
fn view_against_a_vanished_croft_says_the_croft_is_gone() {
    let tmp = tempfile::tempdir().unwrap();
    let out = Command::cargo_bin("croft")
        .unwrap()
        .env("CROFT_VIEW_SOCK", tmp.path().join("nobody.sock"))
        .args(["view", "Cargo.toml"])
        .assert();
    let out = out.failure().code(1);
    let stderr = String::from_utf8(out.get_output().stderr.clone()).unwrap();
    assert!(
        stderr.contains("is gone"),
        "stderr must name the situation, was: {stderr}"
    );
}

/// #362 end to end against the REAL binary: `croft view <file>` connects to
/// the socket named by the environment, sends the resolved path, and exits 0
/// on an ok reply.
///
/// The unit tests drive the client and server halves in one process, where
/// they can agree with each other. This stands up a socket the binary knows
/// nothing about and makes the shipped `croft view` talk to it.
#[test]
fn view_sends_the_resolved_path_to_the_socket_and_exits_zero() {
    use std::io::{BufRead, BufReader, Write};
    let tmp = tempfile::tempdir().unwrap();
    let sock = tmp.path().join("v.sock");
    let target = tmp.path().join("report.txt");
    std::fs::write(&target, b"hello").unwrap();
    let listener = std::os::unix::net::UnixListener::bind(&sock).unwrap();

    let server = std::thread::spawn(move || {
        let (mut stream, _) = listener.accept().unwrap();
        let mut line = String::new();
        BufReader::new(&stream).read_line(&mut line).unwrap();
        stream.write_all(b"{\"status\":\"ok\"}\n").unwrap();
        line
    });

    // Run from a DIFFERENT directory with a RELATIVE argument: the client is
    // the side that resolves, and running from the file's own directory
    // would pass even if it did not.
    Command::cargo_bin("croft")
        .unwrap()
        .env("CROFT_VIEW_SOCK", &sock)
        .current_dir(tmp.path())
        .args(["view", "report.txt"])
        .assert()
        .success();

    // The wire carries the path as raw bytes (a filename is bytes, not
    // UTF-8), so decode rather than substring-match: asserting on the text
    // form would pass only for paths that happen to be valid UTF-8, which is
    // the case the byte encoding exists to stop relying on.
    let request = server.join().unwrap();
    let bytes: Vec<u8> = request
        .trim()
        .trim_start_matches("{\"path\":[")
        .trim_end_matches("]}")
        .split(',')
        .map(|n| n.trim().parse::<u8>().expect("the wire is a byte array"))
        .collect();
    // Canonicalised on both sides: macOS hands a process a cwd under
    // `/private/var` for a `/var` tempdir, so the client resolves against a
    // path that is the same directory by a different name and a raw compare
    // fails there while passing on Linux.
    let received = std::path::PathBuf::from(String::from_utf8(bytes).unwrap());
    assert_eq!(
        received.canonicalize().unwrap_or(received),
        target.canonicalize().unwrap_or(target.clone()),
        "the server must receive the path resolved against the client's cwd"
    );
}

/// A file that does not exist is refused by the client, before any croft is
/// asked to open it: the message can then name the path as the user typed it,
/// resolved against the cwd they typed it in.
#[test]
fn view_refuses_a_missing_file_without_bothering_the_socket() {
    let tmp = tempfile::tempdir().unwrap();
    let sock = tmp.path().join("v.sock");
    let _listener = std::os::unix::net::UnixListener::bind(&sock).unwrap();
    let out = Command::cargo_bin("croft")
        .unwrap()
        .env("CROFT_VIEW_SOCK", &sock)
        .current_dir(tmp.path())
        .args(["view", "nope.pdf"])
        .assert();
    let out = out.failure().code(1);
    let stderr = String::from_utf8(out.get_output().stderr.clone()).unwrap();
    assert!(
        stderr.contains("no such file") && stderr.contains("nope.pdf"),
        "stderr must name the missing path, was: {stderr}"
    );
}

/// The wire carries a path as raw bytes so a filename that is not UTF-8
/// survives. That is only true if the CLI takes the argument as an
/// `OsString`: a `String` parameter throws the bytes away one call before
/// the encoding that preserves them, which is where this started.
///
/// Not on macOS: APFS rejects a filename that is not valid UTF-8, so the
/// fixture cannot be created there and the test would fail for a reason
/// that has nothing to do with the wire.
#[cfg(all(unix, not(target_os = "macos")))]
#[test]
fn view_transmits_a_non_utf8_filename_byte_for_byte() {
    use std::ffi::OsStr;
    use std::io::{BufRead, BufReader, Write};
    use std::os::unix::ffi::OsStrExt;

    let tmp = tempfile::tempdir().unwrap();
    let sock = tmp.path().join("v.sock");
    let name = OsStr::from_bytes(b"od\xffd.txt");
    let target = tmp.path().join(name);
    std::fs::write(&target, b"x").unwrap();
    assert!(
        target.to_str().is_none(),
        "fixture must be invalid UTF-8, or this test proves nothing"
    );
    let listener = std::os::unix::net::UnixListener::bind(&sock).unwrap();

    let server = std::thread::spawn(move || {
        let (mut stream, _) = listener.accept().unwrap();
        let mut line = String::new();
        BufReader::new(&stream).read_line(&mut line).unwrap();
        stream.write_all(b"{\"status\":\"ok\"}\n").unwrap();
        line
    });

    Command::cargo_bin("croft")
        .unwrap()
        .env("CROFT_VIEW_SOCK", &sock)
        .current_dir(tmp.path())
        .arg("view")
        .arg(name)
        .assert()
        .success();

    let request = server.join().unwrap();
    let bytes: Vec<u8> = request
        .trim()
        .trim_start_matches("{\"path\":[")
        .trim_end_matches("]}")
        .split(',')
        .map(|n| n.trim().parse::<u8>().expect("the wire is a byte array"))
        .collect();
    assert!(
        bytes.contains(&0xff),
        "the 0xff byte must reach the server intact, got {bytes:?}"
    );
    assert_eq!(std::path::PathBuf::from(OsStr::from_bytes(&bytes)), target);
}

/// An `--as` value that cannot become a filename is refused, not ignored:
/// falling back to the sniff would stage the bytes under a name the user did
/// not ask for and still report success.
#[test]
fn view_refuses_an_as_flag_that_cannot_be_a_file_extension() {
    let tmp = tempfile::tempdir().unwrap();
    let sock = tmp.path().join("v.sock");
    let _listener = std::os::unix::net::UnixListener::bind(&sock).unwrap();
    let home = tmp.path().join("home");
    std::fs::create_dir(&home).unwrap();
    let out = Command::cargo_bin("croft")
        .unwrap()
        .env("CROFT_VIEW_SOCK", &sock)
        .env("HOME", &home)
        .current_dir(tmp.path())
        .write_stdin("a,b\n1,2\n")
        .args(["view", "-", "--as", "../x"])
        .assert();
    let out = out.failure().code(1);
    let stderr = String::from_utf8(out.get_output().stderr.clone()).unwrap();
    assert!(
        stderr.contains("--as"),
        "the message must name the flag, was: {stderr}"
    );
    // And it refuses without leaving anything behind. `stage_stdin` used to
    // create `view-stdin/` before it validated the hint, so this refusal
    // built a directory in the user's real cache dir on its way out. HOME is
    // redirected here precisely so that is observable rather than invisible.
    assert!(
        !home.join(".cache/croft/view-stdin").exists(),
        "a refused --as must not create the staging directory"
    );
}

/// `croft view -` end to end against the shipped binary (#362).
///
/// The rejection test above never reaches the staging write, so nothing
/// covered the success path at the binary level: the file appearing on disk
/// under a sniffed extension the editor routes on, at 0600, with THAT path
/// being what the server is told to open. HOME is redirected so the staging
/// lands in a tempdir rather than the developer's own cache.
#[test]
fn view_from_a_pipe_stages_an_owner_only_file_and_sends_its_path() {
    use std::io::{BufRead, BufReader, Write};
    use std::os::unix::fs::PermissionsExt;
    let tmp = tempfile::tempdir().unwrap();
    let home = tmp.path().join("home");
    std::fs::create_dir(&home).unwrap();
    let sock = tmp.path().join("v.sock");
    let listener = std::os::unix::net::UnixListener::bind(&sock).unwrap();

    let server = std::thread::spawn(move || {
        let (mut stream, _) = listener.accept().unwrap();
        let mut line = String::new();
        BufReader::new(&stream).read_line(&mut line).unwrap();
        stream.write_all(b"{\"status\":\"ok\"}\n").unwrap();
        line
    });

    Command::cargo_bin("croft")
        .unwrap()
        .env("CROFT_VIEW_SOCK", &sock)
        .env("HOME", &home)
        .current_dir(tmp.path())
        .write_stdin("a,b\n1,2\n")
        .args(["view", "-"])
        .assert()
        .success();

    // Same byte-array decode as the on-disk case: the wire carries a filename
    // as bytes, and a substring match would only work for UTF-8 paths.
    let request = server.join().unwrap();
    let bytes: Vec<u8> = request
        .trim()
        .trim_start_matches("{\"path\":[")
        .trim_end_matches("]}")
        .split(',')
        .map(|n| n.trim().parse::<u8>().expect("the wire is a byte array"))
        .collect();
    let staged = std::path::PathBuf::from(String::from_utf8(bytes).unwrap());

    assert_eq!(
        staged.extension().and_then(|e| e.to_str()),
        Some("csv"),
        "delimited text must be staged under the extension the sheet viewer \
         routes on, or it arrives as plain text: {staged:?}"
    );
    assert!(
        staged.starts_with(home.join(".cache").join("croft")),
        "the staged file belongs under the cache dir, not beside the user's \
         own files: {staged:?}"
    );
    let meta = std::fs::metadata(&staged)
        .expect("the staged file must exist by the time the server is told about it");
    assert_eq!(
        meta.permissions().mode() & 0o777,
        0o600,
        "piped content is owner-only: `vault read ... | croft view -` is the \
         case this command invites"
    );
    assert_eq!(
        std::fs::read(&staged).unwrap(),
        b"a,b\n1,2\n",
        "and it is the bytes that came down the pipe, unaltered"
    );
}

/// `--as` on a NAMED file is refused, not ignored (#362).
///
/// The flag only ever reached `stage_stdin`, which only runs for `-`, so
/// `croft view data.log --as csv` opened a text tab and exited 0. The only
/// signal that the flag did nothing was the file opening in the viewer the
/// user was trying to override, which is precisely the reasoning the module
/// already gives for refusing an unusable `--as` value rather than sniffing
/// past it.
#[test]
fn view_refuses_an_as_flag_on_a_named_file_rather_than_ignoring_it() {
    let tmp = tempfile::tempdir().unwrap();
    let sock = tmp.path().join("v.sock");
    let target = tmp.path().join("data.log");
    std::fs::write(&target, b"a,b\n1,2\n").unwrap();
    // A listener that would ACCEPT, so a pass here cannot come from the
    // socket being unreachable.
    let _listener = std::os::unix::net::UnixListener::bind(&sock).unwrap();
    let out = Command::cargo_bin("croft")
        .unwrap()
        .env("CROFT_VIEW_SOCK", &sock)
        .current_dir(tmp.path())
        .args(["view", "data.log", "--as", "csv"])
        .assert();
    let out = out.failure().code(1);
    let stderr = String::from_utf8(out.get_output().stderr.clone()).unwrap();
    assert!(
        stderr.contains("--as"),
        "the message must name the flag that was refused, was: {stderr}"
    );
    assert!(
        stderr.contains("piped"),
        "and say what it applies to, or the user cannot tell what to do \
         instead, was: {stderr}"
    );
}

/// An empty pipe is refused by name, and stages nothing (#362).
#[test]
fn view_from_an_empty_pipe_says_nothing_arrived() {
    let tmp = tempfile::tempdir().unwrap();
    let home = tmp.path().join("home");
    std::fs::create_dir(&home).unwrap();
    let sock = tmp.path().join("v.sock");
    let _listener = std::os::unix::net::UnixListener::bind(&sock).unwrap();
    let out = Command::cargo_bin("croft")
        .unwrap()
        .env("CROFT_VIEW_SOCK", &sock)
        .env("HOME", &home)
        .current_dir(tmp.path())
        .write_stdin("")
        .args(["view", "-"])
        .assert();
    let out = out.failure().code(1);
    let stderr = String::from_utf8(out.get_output().stderr.clone()).unwrap();
    assert!(
        stderr.contains("nothing arrived on stdin"),
        "an empty pipe must say so rather than staging a zero-byte file and \
         reporting success, was: {stderr}"
    );
    assert!(
        !home.join(".cache/croft/view-stdin").exists(),
        "and it must not create the staging directory either"
    );
}

/// #848 end to end: `croft edit --wait` on a file that does not exist yet
/// creates it empty, asks the croft to open it, and returns once a probe
/// says its tab closed. That is what `EDITOR="croft edit --wait"` needs from
/// a tool that names a file it has not written, and it used to be refused.
#[test]
fn edit_wait_creates_a_new_file_opens_it_and_returns_once_it_closes() {
    use std::io::{BufRead, BufReader, Write};
    let tmp = tempfile::tempdir().unwrap();
    let sock = tmp.path().join("v.sock");
    let target = tmp.path().join("TODO.md");
    let listener = std::os::unix::net::UnixListener::bind(&sock).unwrap();

    // The open, answered ok, then the wait's first probe, answered closed.
    let server = std::thread::spawn(move || {
        let replies: [&[u8]; 2] = [
            b"{\"status\":\"ok\"}\n",
            b"{\"status\":\"err\",\"message\":\"closed\"}\n",
        ];
        let mut requests = Vec::new();
        for reply in replies {
            let (mut stream, _) = listener.accept().unwrap();
            let mut line = String::new();
            BufReader::new(&stream).read_line(&mut line).unwrap();
            stream.write_all(reply).unwrap();
            requests.push(line);
        }
        requests
    });

    Command::cargo_bin("croft")
        .unwrap()
        .env("CROFT_VIEW_SOCK", &sock)
        .current_dir(tmp.path())
        .args(["edit", "--wait", "TODO.md"])
        .assert()
        .success();

    let requests = server.join().unwrap();
    assert!(
        !requests[0].contains("probe"),
        "first the open: {requests:?}"
    );
    assert!(
        requests[1].contains("\"probe\":true"),
        "then the wait's probe: {requests:?}"
    );
    assert_eq!(
        std::fs::read(&target).unwrap(),
        b"",
        "the new file was created, empty"
    );
}

/// #848: every `croft edit` message said `croft view`. Outside croft it now
/// names itself, and leaves no empty file behind for a path it cannot open.
#[test]
fn edit_outside_croft_names_itself_and_creates_nothing() {
    let tmp = tempfile::tempdir().unwrap();
    let out = Command::cargo_bin("croft")
        .unwrap()
        .env_remove("CROFT_VIEW_SOCK")
        .current_dir(tmp.path())
        .args(["edit", "new.txt"])
        .assert();
    let out = out.failure().code(1);
    let stderr = String::from_utf8(out.get_output().stderr.clone()).unwrap();
    assert!(
        stderr.contains("croft edit needs a croft") && !stderr.contains("croft view"),
        "stderr must name the command the user typed, was: {stderr}"
    );
    assert!(!tmp.path().join("new.txt").exists());
}

/// #848 guard: a new file's folder must exist. `croft edit` into a missing
/// one is refused before any croft is asked to open it, and creates neither
/// the file nor the folders above it.
#[test]
fn edit_refuses_a_missing_folder_without_bothering_the_socket() {
    let tmp = tempfile::tempdir().unwrap();
    let sock = tmp.path().join("v.sock");
    let listener = std::os::unix::net::UnixListener::bind(&sock).unwrap();
    listener.set_nonblocking(true).unwrap();
    let out = Command::cargo_bin("croft")
        .unwrap()
        .env("CROFT_VIEW_SOCK", &sock)
        .current_dir(tmp.path())
        .args(["edit", "no/such/TODO.md"])
        .assert();
    let out = out.failure().code(1);
    let stderr = String::from_utf8(out.get_output().stderr.clone()).unwrap();
    assert!(
        stderr.starts_with("croft edit: ") && stderr.contains("no such directory"),
        "stderr must name the command and the reason, was: {stderr}"
    );
    assert!(
        matches!(listener.accept(), Err(e) if e.kind() == std::io::ErrorKind::WouldBlock),
        "no croft was asked to open anything"
    );
    assert!(!tmp.path().join("no").exists(), "no folders were created");
}

/// #848: with a stale `CROFT_VIEW_SOCK` (the croft that set it has exited),
/// `croft edit` created the new file, failed to open it, and left the empty
/// file behind. A failed open takes back the file this call created.
#[test]
fn edit_against_a_vanished_croft_leaves_no_new_file_behind() {
    let tmp = tempfile::tempdir().unwrap();
    let out = Command::cargo_bin("croft")
        .unwrap()
        .env("CROFT_VIEW_SOCK", tmp.path().join("nobody.sock"))
        .current_dir(tmp.path())
        .args(["edit", "new.txt"])
        .assert();
    let out = out.failure().code(1);
    let stderr = String::from_utf8(out.get_output().stderr.clone()).unwrap();
    assert!(
        stderr.contains("croft edit: the croft that opened this pane is gone"),
        "stderr must name the command and the situation, was: {stderr}"
    );
    assert!(
        !tmp.path().join("new.txt").exists(),
        "the empty file this call created was left behind"
    );
}

/// #848: a croft that answers but refuses to open the new file leaves
/// nothing behind either.
#[test]
fn edit_refused_by_the_croft_leaves_no_new_file_behind() {
    use std::io::{BufRead, BufReader, Write};
    let tmp = tempfile::tempdir().unwrap();
    let sock = tmp.path().join("v.sock");
    let listener = std::os::unix::net::UnixListener::bind(&sock).unwrap();
    let server = std::thread::spawn(move || {
        let (mut stream, _) = listener.accept().unwrap();
        let mut line = String::new();
        BufReader::new(&stream).read_line(&mut line).unwrap();
        stream
            .write_all(b"{\"status\":\"err\",\"message\":\"cannot open that here\"}\n")
            .unwrap();
    });
    let out = Command::cargo_bin("croft")
        .unwrap()
        .env("CROFT_VIEW_SOCK", &sock)
        .current_dir(tmp.path())
        .args(["edit", "new.txt"])
        .assert();
    server.join().unwrap();
    let out = out.failure().code(1);
    let stderr = String::from_utf8(out.get_output().stderr.clone()).unwrap();
    assert!(
        stderr.contains("croft edit: cannot open that here"),
        "stderr was: {stderr}"
    );
    assert!(
        !tmp.path().join("new.txt").exists(),
        "the empty file this call created was left behind"
    );
}

/// #848 guard: a failed open takes back only a file THIS call created. A
/// file that was already there, even an empty one, is never removed.
#[test]
fn edit_against_a_vanished_croft_never_removes_an_existing_file() {
    let tmp = tempfile::tempdir().unwrap();
    for (name, body) in [("keep.txt", "keep me"), ("empty.txt", "")] {
        let path = tmp.path().join(name);
        std::fs::write(&path, body).unwrap();
        Command::cargo_bin("croft")
            .unwrap()
            .env("CROFT_VIEW_SOCK", tmp.path().join("nobody.sock"))
            .current_dir(tmp.path())
            .args(["edit", name])
            .assert()
            .failure()
            .code(1);
        assert_eq!(
            std::fs::read_to_string(&path).ok().as_deref(),
            Some(body),
            "{name} must survive a failed open untouched"
        );
    }
}

/// #682 end to end: croft over a pty with an image protocol on (iTerm2's,
/// forced) must not stream images on every keystroke. Before the fix, 20
/// keystrokes in a file cost about 175 KB of escape output (the minimap and
/// chrome images re-sent each frame); now they cost a few KB. The budget
/// sits between the two so a regression fails loudly without being flaky.
#[cfg(unix)]
#[test]
fn typing_with_images_on_does_not_stream_images_per_keystroke() {
    use std::io::Read;
    use std::os::fd::{FromRawFd, OwnedFd};
    use std::os::unix::process::CommandExt;
    use std::time::{Duration, Instant};

    let home = tempfile::tempdir().unwrap();
    let file = home.path().join("a.rs");
    std::fs::write(&file, "fn main() {\n    println!(\"hi\");\n}\n".repeat(300)).unwrap();

    let (mut master, mut slave) = (0, 0);
    let mut ws = libc::winsize {
        ws_row: 50,
        ws_col: 160,
        ws_xpixel: 160 * 9,
        ws_ypixel: 50 * 18,
    };
    assert_eq!(
        unsafe {
            libc::openpty(
                &mut master,
                &mut slave,
                std::ptr::null_mut(),
                std::ptr::null_mut(),
                &raw mut ws,
            )
        },
        0
    );
    // Neither end may leak into croft or what it starts: a leaked master
    // keeps the terminal open after the test closes it, and macOS then
    // holds a killed croft in its exit until its output is read.
    for fd in [master, slave] {
        assert_ne!(
            unsafe { libc::fcntl(fd, libc::F_SETFD, libc::FD_CLOEXEC) },
            -1
        );
    }
    let slave = unsafe { OwnedFd::from_raw_fd(slave) };
    let bin = assert_cmd::cargo::cargo_bin("croft");
    let mut cmd = std::process::Command::new(bin);
    cmd.arg("a.rs")
        .current_dir(home.path())
        .env("HOME", home.path())
        .env("TERM", "xterm-256color")
        .env("TERM_PROGRAM", "iTerm.app")
        .env("CROFT_FORCE_INLINE_IMAGES", "1")
        .stdin(slave.try_clone().unwrap())
        .stdout(slave.try_clone().unwrap())
        .stderr(slave.try_clone().unwrap());
    unsafe {
        cmd.pre_exec(|| {
            libc::setsid();
            libc::ioctl(0, libc::TIOCSCTTY as _, 0);
            Ok(())
        });
    }
    let mut child = cmd.spawn().unwrap();
    drop(slave);
    unsafe {
        let flags = libc::fcntl(master, libc::F_GETFL);
        libc::fcntl(master, libc::F_SETFL, flags | libc::O_NONBLOCK);
    }
    let mut pty = unsafe { std::fs::File::from_raw_fd(master) };
    let mut tty = pty.try_clone().unwrap();
    let mut drain = |for_: Duration| {
        let mut n = 0usize;
        let mut buf = [0u8; 1 << 16];
        let end = Instant::now() + for_;
        while Instant::now() < end {
            match pty.read(&mut buf) {
                Ok(0) => break,
                Ok(k) => n += k,
                Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => {
                    std::thread::sleep(Duration::from_millis(10))
                }
                Err(_) => break,
            }
        }
        n
    };
    // Let it start, then dismiss any first-run popup and let that settle.
    let started = drain(Duration::from_secs(8));
    assert!(started > 1000, "croft drew nothing ({started} bytes)");
    use std::io::Write;
    tty.write_all(b"\x1b").unwrap();
    drain(Duration::from_secs(2));
    let mut typed = 0;
    for _ in 0..20 {
        tty.write_all(b"x").unwrap();
        typed += drain(Duration::from_millis(80));
    }
    typed += drain(Duration::from_secs(1));
    let _ = child.kill();
    // macOS holds a killed process in its exit while output sits unread on
    // its terminal, so the terminal closes before the wait.
    drop((pty, tty));
    let _ = child.wait();
    assert!(
        typed < 30_000,
        "20 keystrokes wrote {typed} bytes: images are being re-sent per frame"
    );
}

/// #862 hot exit end to end, on the real binary: croft killed (SIGKILL, so
/// no quit runs, as in an OOM kill) a few seconds after an edit it never
/// saved brings the edit back on the next launch of the workspace, as an
/// unsaved tab, and the file itself is untouched. Before hot exit the text
/// was gone.
#[cfg(unix)]
#[test]
fn unsaved_edits_survive_a_kill_and_come_back_on_the_next_launch() {
    use std::io::{Read, Write};
    use std::os::fd::{FromRawFd, OwnedFd};
    use std::os::unix::process::CommandExt;
    use std::time::{Duration, Instant};

    /// croft on a fresh pty in `dir` (also its HOME), and the pty's master.
    fn spawn(dir: &std::path::Path, args: &[&str]) -> (std::process::Child, std::fs::File) {
        let (mut master, mut slave) = (0, 0);
        let mut ws = libc::winsize {
            ws_row: 40,
            ws_col: 140,
            ws_xpixel: 0,
            ws_ypixel: 0,
        };
        let opened = unsafe {
            libc::openpty(
                &mut master,
                &mut slave,
                std::ptr::null_mut(),
                std::ptr::null_mut(),
                &raw mut ws,
            )
        };
        assert_eq!(opened, 0);
        // Neither end may leak into croft or what it starts: a leaked master
        // keeps the terminal open after the test closes it, and macOS then
        // holds a killed croft in its exit until its output is read.
        for fd in [master, slave] {
            assert_ne!(
                unsafe { libc::fcntl(fd, libc::F_SETFD, libc::FD_CLOEXEC) },
                -1
            );
        }
        let slave = unsafe { OwnedFd::from_raw_fd(slave) };
        let mut cmd = std::process::Command::new(assert_cmd::cargo::cargo_bin("croft"));
        cmd.args(args)
            .current_dir(dir)
            .env("HOME", dir)
            .env("TERM", "xterm-256color")
            .env_remove("TERM_PROGRAM")
            .env_remove("XDG_STATE_HOME")
            .stdin(slave.try_clone().unwrap())
            .stdout(slave.try_clone().unwrap())
            .stderr(slave.try_clone().unwrap());
        // A child that cannot take the pty as its controlling terminal
        // fails to spawn here, rather than running detached from it.
        unsafe {
            cmd.pre_exec(|| {
                if libc::setsid() == -1 || libc::ioctl(0, libc::TIOCSCTTY as _, 0) == -1 {
                    return Err(std::io::Error::last_os_error());
                }
                Ok(())
            });
        }
        let child = cmd.spawn().unwrap();
        drop(slave);
        unsafe {
            let flags = libc::fcntl(master, libc::F_GETFL);
            libc::fcntl(master, libc::F_SETFL, flags | libc::O_NONBLOCK);
        }
        (child, unsafe { std::fs::File::from_raw_fd(master) })
    }
    /// What croft writes until `done` holds on everything read so far, or
    /// until `limit` (a bound for a slow machine, not a wait). The end of
    /// the terminal (croft exited) stops the read; any other read error
    /// fails the test rather than passing for the end.
    fn read_until(
        pty: &mut std::fs::File,
        limit: Duration,
        mut done: impl FnMut(&str) -> bool,
    ) -> String {
        let mut out = String::new();
        let mut buf = [0u8; 1 << 16];
        let end = Instant::now() + limit;
        while !done(&out) && Instant::now() < end {
            match pty.read(&mut buf) {
                Ok(0) => break,
                Ok(k) => out.push_str(&String::from_utf8_lossy(&buf[..k])),
                Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => {
                    std::thread::sleep(Duration::from_millis(10))
                }
                // Linux reports the slave side closing as EIO.
                Err(e) if e.raw_os_error() == Some(libc::EIO) => break,
                Err(e) => panic!("reading croft's terminal: {e}"),
            }
        }
        out
    }
    /// Whether a hot-exit backup under `dir` holds `text`.
    fn a_backup_holds(dir: &std::path::Path, text: &str) -> bool {
        let Ok(workspaces) = std::fs::read_dir(dir) else {
            return false;
        };
        workspaces
            .flatten()
            .filter_map(|ws| std::fs::read_dir(ws.path()).ok())
            .flat_map(|files| files.flatten())
            .filter(|f| f.path().extension().is_some_and(|x| x == "json"))
            .any(|f| std::fs::read_to_string(f.path()).is_ok_and(|t| t.contains(text)))
    }
    let limit = Duration::from_secs(20);

    let home = tempfile::tempdir().unwrap();
    let file = home.path().join("a.txt");
    std::fs::write(&file, "alpha\n").unwrap();
    // In the state directory, not the cache that cleaners delete (#1578).
    let backups = home.path().join(".local/state/croft/hot-exit");
    let (mut child, mut pty) = spawn(home.path(), &["a.txt"]);
    let started = read_until(&mut pty, limit, |out| out.contains("alpha"));
    assert!(
        started.contains("alpha"),
        "croft never drew a.txt: {started:?}"
    );
    // Dismiss anything a first launch shows over the editor. A terminal
    // reads ESC as Escape only when nothing follows it at once, so this
    // pause is the protocol's, not a guess at how long croft takes.
    pty.write_all(b"\x1b").unwrap();
    read_until(&mut pty, Duration::from_millis(300), |_| false);
    pty.write_all(b"HOTEXIT").unwrap();
    // The backup lands 5 s after the last edit; croft is killed outright
    // once it has. The terminal is read meanwhile, so croft never blocks
    // on a full one.
    let end = Instant::now() + limit;
    while !a_backup_holds(&backups, "HOTEXIT") && Instant::now() < end {
        read_until(&mut pty, Duration::from_millis(100), |_| false);
    }
    assert!(
        a_backup_holds(&backups, "HOTEXIT"),
        "a backup holds the edit"
    );
    child.kill().unwrap();
    // macOS holds a killed process in its exit while output sits unread on
    // its terminal, so the terminal closes before the wait.
    drop(pty);
    child.wait().unwrap();
    assert_eq!(
        std::fs::read_to_string(&file).unwrap(),
        "alpha\n",
        "never saved"
    );

    let (mut child, mut pty) = spawn(home.path(), &[]);
    let drawn = read_until(&mut pty, limit, |out| {
        out.contains("HOTEXITalpha") && out.contains("restored 1 unsaved tab")
    });
    let _ = child.kill();
    drop(pty);
    let _ = child.wait();
    assert!(
        drawn.contains("HOTEXITalpha"),
        "the unsaved edit is back on the next launch"
    );
    assert!(
        drawn.contains("restored 1 unsaved tab"),
        "and croft says so"
    );
    assert_eq!(std::fs::read_to_string(&file).unwrap(), "alpha\n");
}

/// #621: a locale as `$LANG` spells it (`de_DE.UTF-8`) seeds the template
/// from the German catalog and names the file croft will load.
#[test]
fn locale_template_accepts_a_full_locale() {
    let out = Command::cargo_bin("croft")
        .unwrap()
        .args(["locale-template", "de_DE.UTF-8"])
        .assert()
        .success();
    let stdout = String::from_utf8(out.get_output().stdout.clone()).unwrap();
    let stderr = String::from_utf8(out.get_output().stderr.clone()).unwrap();
    let catalog: serde_json::Value = serde_json::from_str(&stdout).unwrap();
    let filled = catalog
        .as_object()
        .unwrap()
        .values()
        .filter(|v| v.as_str().is_some_and(|s| !s.is_empty()))
        .count();
    assert!(filled > 0, "the built-in German entries seed it");
    assert!(stderr.contains("locales/de.json"), "{stderr}");
}

/// The text a Rust string literal's body (between its quotes) spells: one
/// pass over its escapes (`\\`, `\"`, `\'`, `\n`, `\r`, `\t`, `\0`,
/// `\xNN`, `\u{...}`), as the compiler reads them.
fn unescape_rust_literal(body: &str) -> String {
    let mut out = String::with_capacity(body.len());
    let mut chars = body.chars();
    while let Some(c) = chars.next() {
        if c != '\\' {
            out.push(c);
            continue;
        }
        match chars.next() {
            Some('n') => out.push('\n'),
            Some('r') => out.push('\r'),
            Some('t') => out.push('\t'),
            Some('0') => out.push('\0'),
            Some('x') => {
                let hex: String = chars.by_ref().take(2).collect();
                out.push(char::from(u8::from_str_radix(&hex, 16).unwrap()));
            }
            Some('u') => {
                let hex: String = chars.by_ref().skip(1).take_while(|&c| c != '}').collect();
                out.push(char::from_u32(u32::from_str_radix(&hex, 16).unwrap()).unwrap());
            }
            Some(other) => out.push(other),
            None => out.push('\\'),
        }
    }
    out
}

/// #849 review: the scan reads translated strings out of Rust literals, so
/// it has to spell every escape a literal can hold the way the compiler
/// does, or a string with a newline or tab would be looked up by the wrong
/// key.
#[test]
fn unescape_rust_literal_reads_every_rust_escape() {
    for (body, text) in [
        (r"plain", "plain"),
        (r#"a \"quoted\" word"#, "a \"quoted\" word"),
        (r"back\\slash", "back\\slash"),
        (r"line\nbreak", "line\nbreak"),
        (r"tab\there", "tab\there"),
        (r"cr\r", "cr\r"),
        (r"nul\0", "nul\0"),
        (r"it\'s", "it's"),
        (r"hex\x41", "hexA"),
        (r"\u{2026}\u{1F600}", "\u{2026}\u{1F600}"),
        (
            r"\\n stays a backslash and an n",
            "\\n stays a backslash and an n",
        ),
    ] {
        assert_eq!(unescape_rust_literal(body), text, "{body}");
    }
}

/// Every string literal croft hands to `tr` at runtime, read from its own
/// sources, with the file each came from: `tr("…")` calls, and context-menu
/// labels, which `ContextMenu::localize` runs through `tr` wholesale. Test
/// code is skipped: its strings are never shown.
fn strings_croft_translates() -> std::collections::BTreeMap<String, String> {
    let lit = r#""((?:[^"\\]|\\.)*)""#;
    let patterns: Vec<regex::Regex> = [
        format!(r"\btr\(\s*{lit}\s*\)"),
        format!(r"MenuEntry::(?:item|header)\(\s*{lit}"),
        format!(r"MenuEntry::item\(\s*if [^{{]*\{{\s*{lit}\s*\}}\s*else\s*\{{\s*{lit}\s*\}}"),
        format!(r"MenuEntry::Submenu\s*\{{\s*label:\s*(?:String::from\(\s*)?{lit}"),
        format!(r"\(\s*String::from\(\s*{lit}\s*\)\s*,\s*MenuAction::"),
        format!(r"\(\s*{lit}\s*\.(?:to_string|to_owned|into)\(\)\s*,\s*MenuAction::"),
    ]
    .iter()
    .map(|p| regex::Regex::new(p).unwrap())
    .collect();
    let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("src");
    let mut dirs = vec![root.clone()];
    let mut found = std::collections::BTreeMap::new();
    while let Some(dir) = dirs.pop() {
        for entry in std::fs::read_dir(&dir).unwrap() {
            let path = entry.unwrap().path();
            if path.is_dir() {
                if !path.ends_with("app/tests") {
                    dirs.push(path);
                }
                continue;
            }
            if path.extension().is_none_or(|e| e != "rs") {
                continue;
            }
            let text = std::fs::read_to_string(&path).unwrap();
            let text = text
                .find("#[cfg(test)]\nmod tests")
                .map_or(text.as_str(), |i| &text[..i]);
            let file = path.strip_prefix(&root).unwrap().display().to_string();
            for re in &patterns {
                for caps in re.captures_iter(text) {
                    for m in caps.iter().skip(1).flatten() {
                        found.insert(unescape_rust_literal(m.as_str()), file.clone());
                    }
                }
            }
        }
    }
    found
}

/// #849: `croft locale-template` must offer every string croft translates,
/// not only the palette titles and whatever a built-in catalog happens to
/// list. Context-menu labels (Close to the Right, Keep Open, the Customize
/// Layout headers, ...) and `tr("CodeQL")` were missing from every
/// language's template, so no translator could ever reach them.
#[test]
fn locale_template_lists_every_string_croft_translates() {
    let found = strings_croft_translates();
    for known in [
        "Close to the Right",
        "Panel Alignment",
        "CodeQL",
        "Pin",
        "Unpin",
    ] {
        assert!(
            found.contains_key(known),
            "the source scan still sees {known:?}; update it with the code"
        );
    }
    let out = Command::cargo_bin("croft")
        .unwrap()
        .args(["locale-template", "xx"])
        .assert()
        .success();
    let template: std::collections::HashMap<String, String> =
        serde_json::from_slice(&out.get_output().stdout).unwrap();
    let missing: Vec<String> = found
        .iter()
        .filter(|(s, _)| !template.contains_key(*s))
        .map(|(s, file)| format!("{s:?} ({file})"))
        .collect();
    assert!(
        missing.is_empty(),
        "{} string(s) croft translates are missing from the template; add them to \
         i18n::TRANSLATABLE:\n{}",
        missing.len(),
        missing.join("\n")
    );
}

/// A French translation in progress, as `locales/fr.json` under a scratch
/// config directory.
fn config_with_french_started() -> (tempfile::TempDir, std::path::PathBuf) {
    let cfg = tempfile::tempdir().unwrap();
    let file = cfg.path().join("croft/locales/fr.json");
    std::fs::create_dir_all(file.parent().unwrap()).unwrap();
    std::fs::write(
        &file,
        "{\n  \"File: Save\": \"Enregistrer\",\n  \"View: Toggle Terminal\": \"Afficher le terminal\"\n}\n",
    )
    .unwrap();
    (cfg, file)
}

fn json_object(text: &str) -> serde_json::Map<String, serde_json::Value> {
    serde_json::from_str::<serde_json::Value>(text)
        .unwrap()
        .as_object()
        .unwrap()
        .clone()
}

/// #1148: refreshing a translation in place keeps every value already
/// translated and adds the strings it lacks.
#[test]
fn locale_template_write_keeps_the_translations_already_in_the_file() {
    let (cfg, file) = config_with_french_started();
    let out = Command::cargo_bin("croft")
        .unwrap()
        .env("XDG_CONFIG_HOME", cfg.path())
        .args(["locale-template", "fr", "--write"])
        .assert()
        .success();
    let stderr = String::from_utf8(out.get_output().stderr.clone()).unwrap();
    let map = json_object(&std::fs::read_to_string(&file).unwrap());
    assert_eq!(map["File: Save"], "Enregistrer");
    assert_eq!(map["View: Toggle Terminal"], "Afficher le terminal");
    assert!(map.len() > 2, "the missing strings are added");
    assert!(stderr.contains("locales/fr.json"), "{stderr}");
}

/// #1148: the printed template carries the user's own translations too, so
/// saving it elsewhere doesn't drop them.
#[test]
fn locale_template_prints_the_users_own_translations() {
    let (cfg, _file) = config_with_french_started();
    let out = Command::cargo_bin("croft")
        .unwrap()
        .env("XDG_CONFIG_HOME", cfg.path())
        .args(["locale-template", "fr"])
        .assert()
        .success();
    let map = json_object(&String::from_utf8(out.get_output().stdout.clone()).unwrap());
    assert_eq!(map["File: Save"], "Enregistrer");
}

/// With no file yet, `--write` starts one, creating `locales/`.
#[test]
fn locale_template_write_starts_a_new_translation() {
    let cfg = tempfile::tempdir().unwrap();
    Command::cargo_bin("croft")
        .unwrap()
        .env("XDG_CONFIG_HOME", cfg.path())
        .args(["locale-template", "fr", "--write"])
        .assert()
        .success();
    let map =
        json_object(&std::fs::read_to_string(cfg.path().join("croft/locales/fr.json")).unwrap());
    assert_eq!(map["File: Save"], "");
}

/// A file that isn't a JSON object of strings is left as it is: `--write`
/// refuses rather than replacing what it can't read.
#[test]
fn locale_template_write_leaves_an_unreadable_file_alone() {
    let (cfg, file) = config_with_french_started();
    let broken = "{\n  \"File: Save\": \"Enregistrer\",\n";
    std::fs::write(&file, broken).unwrap();
    let out = Command::cargo_bin("croft")
        .unwrap()
        .env("XDG_CONFIG_HOME", cfg.path())
        .args(["locale-template", "fr", "--write"])
        .assert()
        .failure()
        .code(1);
    let stderr = String::from_utf8(out.get_output().stderr.clone()).unwrap();
    assert_eq!(std::fs::read_to_string(&file).unwrap(), broken);
    assert!(stderr.contains("fr.json"), "{stderr}");
}

/// English needs no translation file: `--write` writes nothing.
#[test]
fn locale_template_write_for_english_writes_nothing() {
    let cfg = tempfile::tempdir().unwrap();
    Command::cargo_bin("croft")
        .unwrap()
        .env("XDG_CONFIG_HOME", cfg.path())
        .args(["locale-template", "en", "--write"])
        .assert()
        .failure()
        .code(1);
    assert!(!cfg.path().join("croft/locales").exists());
}

/// #1179: `croft import-vscode | head` closes croft's stdout before the
/// report is out. The import is written anyway, and croft ends the way any
/// Unix tool does on a closed pipe instead of panicking with exit code 101.
#[cfg(unix)]
#[test]
fn import_vscode_writes_its_files_when_stdout_is_closed() {
    use std::os::unix::process::ExitStatusExt;
    let cfg = tempfile::tempdir().unwrap();
    let from = tempfile::tempdir().unwrap();
    std::fs::write(
        from.path().join("settings.json"),
        r#"{ "editor.formatOnSave": true }"#,
    )
    .unwrap();
    std::fs::write(
        from.path().join("keybindings.json"),
        r#"[{ "key": "ctrl+s", "command": "workbench.action.files.save" }]"#,
    )
    .unwrap();
    let mut child = std::process::Command::new(assert_cmd::cargo::cargo_bin("croft"))
        .args(["import-vscode", "--from"])
        .arg(from.path())
        .env("XDG_CONFIG_HOME", cfg.path())
        .current_dir(cfg.path())
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::piped())
        .spawn()
        .unwrap();
    // The reader is gone before croft has written a byte.
    drop(child.stdout.take());
    let out = child.wait_with_output().unwrap();
    let stderr = String::from_utf8_lossy(&out.stderr);
    assert!(
        out.status.success() || out.status.signal() == Some(13),
        "a closed pipe ends croft quietly, not with {:?}: {stderr}",
        out.status
    );
    assert!(!stderr.contains("panicked"), "{stderr}");
    let config = std::fs::read_to_string(cfg.path().join("croft/config.json")).unwrap();
    assert!(config.contains("format_on_save"), "{config}");
    assert!(cfg.path().join("croft/keybindings.json").is_file());
}

/// #1179 review: an import that cannot be written says so on stderr even
/// when stdout is already closed, instead of dying silently at the report's
/// first line.
#[cfg(unix)]
#[test]
fn import_vscode_reports_a_failed_write_when_stdout_is_closed() {
    let tmp = tempfile::tempdir().unwrap();
    let from = tempfile::tempdir().unwrap();
    std::fs::write(
        from.path().join("settings.json"),
        r#"{ "editor.formatOnSave": true }"#,
    )
    .unwrap();
    // A file where the config directory should be: nothing can be written.
    let blocked = tmp.path().join("not-a-dir");
    std::fs::write(&blocked, "").unwrap();
    let mut child = std::process::Command::new(assert_cmd::cargo::cargo_bin("croft"))
        .args(["import-vscode", "--from"])
        .arg(from.path())
        .env("XDG_CONFIG_HOME", &blocked)
        .current_dir(tmp.path())
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::piped())
        .spawn()
        .unwrap();
    drop(child.stdout.take());
    let out = child.wait_with_output().unwrap();
    let stderr = String::from_utf8_lossy(&out.stderr);
    assert!(!out.status.success(), "{:?}", out.status);
    assert!(
        stderr.contains("not-a-dir"),
        "the failure is said: {stderr}"
    );
}

/// The real binary on a pty with `HOME` at `dir`, for tests that type raw
/// terminal bytes at croft and read back what it did. Killed on drop.
#[cfg(unix)]
struct PtyCroft {
    child: std::process::Child,
    pty: std::mem::ManuallyDrop<std::fs::File>,
}

#[cfg(unix)]
impl PtyCroft {
    fn spawn(dir: &std::path::Path, args: &[&str]) -> Self {
        use std::os::fd::{FromRawFd, OwnedFd};
        use std::os::unix::process::CommandExt;

        let (mut master, mut slave) = (0, 0);
        let mut ws = libc::winsize {
            ws_row: 40,
            ws_col: 140,
            ws_xpixel: 0,
            ws_ypixel: 0,
        };
        assert_eq!(
            unsafe {
                libc::openpty(
                    &mut master,
                    &mut slave,
                    std::ptr::null_mut(),
                    std::ptr::null_mut(),
                    &raw mut ws,
                )
            },
            0
        );
        // Neither end may leak into croft or what it starts: a leaked master
        // keeps the terminal open after the test closes it, and macOS then
        // holds a killed croft in its exit until its output is read.
        for fd in [master, slave] {
            assert_ne!(
                unsafe { libc::fcntl(fd, libc::F_SETFD, libc::FD_CLOEXEC) },
                -1
            );
        }
        let slave = unsafe { OwnedFd::from_raw_fd(slave) };
        let mut cmd = std::process::Command::new(assert_cmd::cargo::cargo_bin("croft"));
        cmd.args(args)
            .current_dir(dir)
            .env("HOME", dir)
            .env("TERM", "xterm-256color")
            .env_remove("TERM_PROGRAM")
            .env_remove("TMUX")
            .stdin(slave.try_clone().unwrap())
            .stdout(slave.try_clone().unwrap())
            .stderr(slave.try_clone().unwrap());
        // A child that cannot take the pty as its controlling terminal
        // fails to spawn here, rather than running detached from it.
        unsafe {
            cmd.pre_exec(|| {
                if libc::setsid() == -1 || libc::ioctl(0, libc::TIOCSCTTY as _, 0) == -1 {
                    return Err(std::io::Error::last_os_error());
                }
                Ok(())
            });
        }
        let child = cmd.spawn().unwrap();
        drop(slave);
        unsafe {
            let flags = libc::fcntl(master, libc::F_GETFL);
            libc::fcntl(master, libc::F_SETFL, flags | libc::O_NONBLOCK);
        }
        let pty = unsafe { std::fs::File::from_raw_fd(master) };
        Self {
            child,
            pty: std::mem::ManuallyDrop::new(pty),
        }
    }

    /// What croft writes until `done` holds, checked on everything read so
    /// far after every read, or until `limit` (a bound for a slow machine,
    /// not a wait: a passing run returns as soon as `done` does). The end of
    /// the terminal (croft exited) stops the read; any other read error
    /// fails the test rather than passing for the end.
    fn read_until(
        &mut self,
        limit: std::time::Duration,
        mut done: impl FnMut(&str) -> bool,
    ) -> String {
        use std::io::Read;
        let mut out = String::new();
        let mut buf = [0u8; 1 << 16];
        let end = std::time::Instant::now() + limit;
        while !done(&out) && std::time::Instant::now() < end {
            match self.pty.read(&mut buf) {
                Ok(0) => break,
                Ok(k) => out.push_str(&String::from_utf8_lossy(&buf[..k])),
                Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => {
                    std::thread::sleep(std::time::Duration::from_millis(10))
                }
                // Linux reports the slave side closing as EIO.
                Err(e) if e.raw_os_error() == Some(libc::EIO) => break,
                Err(e) => panic!("reading croft's terminal: {e}"),
            }
        }
        out
    }

    /// Type `bytes` as the terminal would send them.
    fn send(&mut self, bytes: &[u8]) {
        use std::io::Write;
        self.pty.write_all(bytes).unwrap();
    }
}

/// How long a PTY test waits for croft to show what it is waiting for.
#[cfg(unix)]
const PTY_LIMIT: std::time::Duration = std::time::Duration::from_secs(20);

#[cfg(unix)]
impl Drop for PtyCroft {
    fn drop(&mut self) {
        let _ = self.child.kill();
        // macOS holds a killed process in its exit while output it wrote
        // sits unread on its terminal, so the terminal closes before the
        // wait. The kill comes first, so croft never sees the hangup.
        unsafe { std::mem::ManuallyDrop::drop(&mut self.pty) };
        let _ = self.child.wait();
    }
}

/// #857: what croft's key reader makes of the bytes tmux sends for
/// Ctrl+Shift+S. tmux 3.2-3.4 with `extended-keys always` (and any tmux
/// with `extended-keys-format csi-u` for a program that asked) send
/// `CSI 83;6u`, the uppercase letter with the Shift flag: that must read as
/// Ctrl+Shift, the Source Control jump. The negative cases must never gain a
/// Shift: the legacy control byte (what tmux 3.5+ and plain terminals send
/// for both Ctrl+S and Ctrl+Shift+S) and an uppercase S reported without the
/// flag stay Ctrl+S, a save; tmux 3.5's default `CSI 27;6;83~` form is not
/// read as a plain Ctrl+S either.
#[cfg(unix)]
#[test]
fn croft_keys_reads_tmux_ctrl_shift_s_with_the_shift_and_nothing_else_with_one() {
    let home = tempfile::tempdir().unwrap();
    let mut croft = PtyCroft::spawn(home.path(), &["keys"]);
    let banner = croft.read_until(PTY_LIMIT, |out| out.contains("croft keys"));
    assert!(banner.contains("croft keys"), "no banner: {banner:?}");
    // Each case is followed by a lone marker key, so the report lines of
    // one case never run into the next.
    let cases: [(&str, &[u8]); 4] = [
        ("tmux CSI u Ctrl+Shift+S", b"\x1b[83;6u"),
        ("legacy Ctrl+S byte", b"\x13"),
        ("uppercase S, Ctrl, no Shift flag", b"\x1b[83;5u"),
        ("tmux 3.5 CSI 27 Ctrl+Shift+S", b"\x1b[27;6;83~"),
    ];
    let mut reports = Vec::new();
    for (i, (name, bytes)) in cases.iter().enumerate() {
        let marker = format!("code=Char('{i}')");
        croft.send(bytes);
        croft.send(format!("{i}").as_bytes());
        let seen = croft.read_until(PTY_LIMIT, |out| out.contains(&marker));
        let lines: Vec<String> = seen
            .lines()
            .filter(|l| l.contains("code="))
            .take_while(|l| !l.contains(&marker))
            .map(|l| l.split("  code=").nth(1).unwrap_or(l).trim().to_string())
            .collect();
        assert!(seen.contains(&marker), "{name}: marker lost in {seen:?}");
        reports.push((*name, lines));
    }
    let [csi_u, legacy, no_flag, csi_27] = reports.try_into().unwrap();
    assert_eq!(
        csi_u.1,
        vec!["Char('S')  modifiers=[CONTROL + SHIFT]  kitty=true"],
        "{}",
        csi_u.0
    );
    assert_eq!(
        legacy.1,
        vec!["Char('s')  modifiers=[CONTROL]  kitty=true"],
        "{}",
        legacy.0
    );
    assert_eq!(
        no_flag.1,
        vec!["Char('S')  modifiers=[CONTROL]  kitty=true"],
        "{}",
        no_flag.0
    );
    assert!(
        csi_27
            .1
            .iter()
            .all(|l| !l.ends_with("modifiers=[CONTROL]  kitty=true")),
        "{}: {:?}",
        csi_27.0,
        csi_27.1
    );
}

/// #857 end to end on the real binary: with a file edited in the editor, the
/// bytes tmux sends for Ctrl+Shift+S (`CSI 83;6u`) jump to Source Control
/// and leave the file unsaved; before the fix they saved it. The legacy
/// Ctrl+S byte, which carries no Shift, still saves.
#[cfg(unix)]
#[test]
fn ctrl_shift_s_as_tmux_sends_it_does_not_save_while_ctrl_s_does() {
    let home = tempfile::tempdir().unwrap();
    let file = home.path().join("a.txt");
    std::fs::write(&file, "alpha\n").unwrap();
    let mut croft = PtyCroft::spawn(home.path(), &["a.txt"]);
    let saved = |want: &str| std::fs::read_to_string(&file).unwrap() == want;
    let started = croft.read_until(PTY_LIMIT, |out| out.contains("alpha"));
    assert!(
        started.contains("alpha"),
        "croft never drew a.txt: {started:?}"
    );
    // Dismiss anything a first launch shows over the editor. A terminal
    // reads ESC as Escape only when nothing follows it at once (ESC then
    // `x` is Alt+x), so this one pause is the protocol's, not a guess at
    // how long croft takes.
    croft.send(b"\x1b");
    croft.read_until(std::time::Duration::from_millis(300), |_| false);
    croft.send(b"x");
    croft.send(b"\x13");
    croft.read_until(PTY_LIMIT, |_| saved("xalpha\n"));
    assert_eq!(
        std::fs::read_to_string(&file).unwrap(),
        "xalpha\n",
        "setup: the typed x lands in a.txt and Ctrl+S saves it"
    );
    croft.send(b"y");
    croft.send(b"\x1b[83;6u");
    // Source Control on screen means the chord has been handled, so a save
    // it made would be on disk by now.
    let drawn = croft.read_until(PTY_LIMIT, |out| out.contains("SOURCE CONTROL"));
    assert_eq!(
        std::fs::read_to_string(&file).unwrap(),
        "xalpha\n",
        "Ctrl+Shift+S with the Shift reported must not save"
    );
    assert!(
        drawn.contains("SOURCE CONTROL"),
        "Ctrl+Shift+S opens Source Control"
    );
}

/// `croft edit --sequence-editor`, as git runs it from `GIT_SEQUENCE_EDITOR`,
/// with git's own editor set to `editor` and no git config in the way.
fn sequence_editor_command(dir: &std::path::Path, editor: &str) -> Command {
    let no_config = dir.join("empty.gitconfig");
    std::fs::write(&no_config, "").unwrap();
    let mut cmd = Command::cargo_bin("croft").unwrap();
    cmd.env("GIT_EDITOR", editor)
        .env("GIT_CONFIG_GLOBAL", &no_config)
        .env("GIT_CONFIG_NOSYSTEM", "1")
        .current_dir(dir);
    cmd
}

/// #1452: a shell that outlived its croft (a tmux server started from a
/// croft pane) keeps `GIT_SEQUENCE_EDITOR` and `CROFT_VIEW_SOCK`. Every
/// `git rebase -i` there failed with "the croft that opened this pane is
/// gone". As the sequence editor, croft now hands the plan to git's own
/// editor instead.
#[test]
fn the_sequence_editor_falls_back_to_gits_editor_when_its_croft_is_gone() {
    let tmp = tempfile::tempdir().unwrap();
    let plan = tmp.path().join("git-rebase-todo");
    std::fs::write(&plan, "pick 1234567 one\n").unwrap();
    let out = sequence_editor_command(tmp.path(), "echo edited >>")
        .env("CROFT_VIEW_SOCK", tmp.path().join("nobody.sock"))
        .args(["edit", "--wait", "--sequence-editor", "git-rebase-todo"])
        .assert();
    let out = out.success();
    let stderr = String::from_utf8(out.get_output().stderr.clone()).unwrap();
    assert!(
        stderr.contains("croft is not running; using echo edited >>"),
        "says which editor it used, was: {stderr}"
    );
    assert_eq!(
        std::fs::read_to_string(&plan).unwrap(),
        "pick 1234567 one\nedited\n",
        "git's editor ran on the plan"
    );
}

/// #1452: the same with no socket at all (the variable was dropped but
/// `GIT_SEQUENCE_EDITOR` kept).
#[test]
fn the_sequence_editor_falls_back_when_no_croft_socket_is_set() {
    let tmp = tempfile::tempdir().unwrap();
    let plan = tmp.path().join("git-rebase-todo");
    std::fs::write(&plan, "pick 1234567 one\n").unwrap();
    sequence_editor_command(tmp.path(), "echo edited >>")
        .env_remove("CROFT_VIEW_SOCK")
        .args(["edit", "--wait", "--sequence-editor", "git-rebase-todo"])
        .assert()
        .success();
    assert!(
        std::fs::read_to_string(&plan)
            .unwrap()
            .ends_with("edited\n")
    );
}

/// #1452: the fallback editor's own failure is git's to see: its exit code
/// comes back, so git aborts the rebase as it would without croft.
#[test]
fn a_failing_fallback_editor_fails_the_sequence_editor() {
    let tmp = tempfile::tempdir().unwrap();
    std::fs::write(tmp.path().join("git-rebase-todo"), "pick 1234567 one\n").unwrap();
    let out = sequence_editor_command(tmp.path(), "exit 3;")
        .env("CROFT_VIEW_SOCK", tmp.path().join("nobody.sock"))
        .args(["edit", "--wait", "--sequence-editor", "git-rebase-todo"])
        .assert();
    let out = out.failure().code(3);
    let stderr = String::from_utf8(out.get_output().stderr.clone()).unwrap();
    assert!(stderr.contains("using exit 3;"), "stderr was: {stderr}");
}

/// #1452 negative: a plain `croft edit` asked for croft by name, so a gone
/// croft is still an error there and no other editor runs.
#[test]
fn a_plain_edit_against_a_gone_croft_still_fails() {
    let tmp = tempfile::tempdir().unwrap();
    let plan = tmp.path().join("notes.txt");
    std::fs::write(&plan, "keep\n").unwrap();
    let out = sequence_editor_command(tmp.path(), "echo edited >>")
        .env("CROFT_VIEW_SOCK", tmp.path().join("nobody.sock"))
        .args(["edit", "--wait", "notes.txt"])
        .assert();
    let out = out.failure().code(1);
    let stderr = String::from_utf8(out.get_output().stderr.clone()).unwrap();
    assert!(
        stderr.contains("the croft that opened this pane is gone"),
        "stderr was: {stderr}"
    );
    assert_eq!(std::fs::read_to_string(&plan).unwrap(), "keep\n");
}

/// #1452 negative: a croft that is running but refuses the file is not a
/// gone croft. Its refusal stands, and git's editor does not run.
#[test]
fn a_croft_that_refuses_the_plan_is_not_replaced_by_gits_editor() {
    use std::io::{BufRead, BufReader, Write};
    let tmp = tempfile::tempdir().unwrap();
    let plan = tmp.path().join("git-rebase-todo");
    std::fs::write(&plan, "pick 1234567 one\n").unwrap();
    let sock = tmp.path().join("v.sock");
    let listener = std::os::unix::net::UnixListener::bind(&sock).unwrap();
    let server = std::thread::spawn(move || {
        let (mut stream, _) = listener.accept().unwrap();
        let mut line = String::new();
        BufReader::new(&stream).read_line(&mut line).unwrap();
        stream
            .write_all(b"{\"status\":\"err\",\"message\":\"cannot open that here\"}\n")
            .unwrap();
    });
    let out = sequence_editor_command(tmp.path(), "echo edited >>")
        .env("CROFT_VIEW_SOCK", &sock)
        .args(["edit", "--wait", "--sequence-editor", "git-rebase-todo"])
        .assert();
    server.join().unwrap();
    let out = out.failure().code(1);
    let stderr = String::from_utf8(out.get_output().stderr.clone()).unwrap();
    assert!(
        stderr.contains("cannot open that here"),
        "stderr was: {stderr}"
    );
    assert_eq!(
        std::fs::read_to_string(&plan).unwrap(),
        "pick 1234567 one\n"
    );
}

/// #1452 negative: a croft that took the plan and closed without a reply may
/// have opened it, so git's editor must not open the same plan a second time.
#[test]
fn a_croft_that_closes_without_replying_is_not_replaced_by_gits_editor() {
    use std::io::{BufRead, BufReader};
    let tmp = tempfile::tempdir().unwrap();
    let plan = tmp.path().join("git-rebase-todo");
    std::fs::write(&plan, "pick 1234567 one\n").unwrap();
    let sock = tmp.path().join("v.sock");
    let listener = std::os::unix::net::UnixListener::bind(&sock).unwrap();
    let server = std::thread::spawn(move || {
        let (stream, _) = listener.accept().unwrap();
        let mut line = String::new();
        BufReader::new(&stream).read_line(&mut line).unwrap();
    });
    let out = sequence_editor_command(tmp.path(), "echo edited >>")
        .env("CROFT_VIEW_SOCK", &sock)
        .args(["edit", "--wait", "--sequence-editor", "git-rebase-todo"])
        .assert();
    server.join().unwrap();
    let out = out.failure().code(1);
    let stderr = String::from_utf8(out.get_output().stderr.clone()).unwrap();
    assert!(!stderr.contains("not running"), "stderr was: {stderr}");
    assert_eq!(
        std::fs::read_to_string(&plan).unwrap(),
        "pick 1234567 one\n"
    );
}

/// #1452 negative: where git itself names no editor (a dumb terminal with
/// none set), the fallback fails as git would rather than inventing `vi`.
#[test]
fn the_sequence_editor_fails_when_git_names_no_editor() {
    let tmp = tempfile::tempdir().unwrap();
    let plan = tmp.path().join("git-rebase-todo");
    std::fs::write(&plan, "pick 1234567 one\n").unwrap();
    let out = sequence_editor_command(tmp.path(), "unused")
        .env_remove("GIT_EDITOR")
        .env_remove("VISUAL")
        .env_remove("EDITOR")
        .env("TERM", "dumb")
        .env("CROFT_VIEW_SOCK", tmp.path().join("nobody.sock"))
        .args(["edit", "--wait", "--sequence-editor", "git-rebase-todo"])
        .assert();
    let out = out.failure().code(1);
    let stderr = String::from_utf8(out.get_output().stderr.clone()).unwrap();
    assert!(!stderr.contains("using"), "stderr was: {stderr}");
    assert!(
        stderr.contains("the croft that opened this pane is gone"),
        "stderr was: {stderr}"
    );
    assert_eq!(
        std::fs::read_to_string(&plan).unwrap(),
        "pick 1234567 one\n"
    );
}

/// #1452 negative: with its croft alive the plan opens there, exactly as
/// before, and git's editor never runs.
#[test]
fn a_live_croft_still_gets_the_plan() {
    use std::io::{BufRead, BufReader, Write};
    let tmp = tempfile::tempdir().unwrap();
    let plan = tmp.path().join("git-rebase-todo");
    std::fs::write(&plan, "pick 1234567 one\n").unwrap();
    let sock = tmp.path().join("v.sock");
    let listener = std::os::unix::net::UnixListener::bind(&sock).unwrap();
    let server = std::thread::spawn(move || {
        let replies: [&[u8]; 2] = [
            b"{\"status\":\"ok\"}\n",
            b"{\"status\":\"err\",\"message\":\"closed\"}\n",
        ];
        for reply in replies {
            let (mut stream, _) = listener.accept().unwrap();
            let mut line = String::new();
            BufReader::new(&stream).read_line(&mut line).unwrap();
            stream.write_all(reply).unwrap();
        }
    });
    let out = sequence_editor_command(tmp.path(), "echo edited >>")
        .env("CROFT_VIEW_SOCK", &sock)
        .args(["edit", "--wait", "--sequence-editor", "git-rebase-todo"])
        .assert();
    server.join().unwrap();
    let out = out.success();
    let stderr = String::from_utf8(out.get_output().stderr.clone()).unwrap();
    assert!(!stderr.contains("not running"), "stderr was: {stderr}");
    assert_eq!(
        std::fs::read_to_string(&plan).unwrap(),
        "pick 1234567 one\n"
    );
}

/// #1439: off macOS, `setup-ghostty` refuses instead of writing a macOS
/// path Ghostty never reads there and reporting success.
#[cfg(not(target_os = "macos"))]
#[test]
fn setup_ghostty_refuses_off_macos_and_writes_nothing() {
    let home = tempfile::tempdir().unwrap();
    let config = home.path().join(".config/ghostty/config.ghostty");
    std::fs::create_dir_all(config.parent().unwrap()).unwrap();
    std::fs::write(&config, "font-size = 14\n").unwrap();
    let out = Command::cargo_bin("croft")
        .unwrap()
        .args(["setup-ghostty", "-y"])
        .env("HOME", home.path())
        .env_remove("XDG_CONFIG_HOME")
        .assert()
        .failure();
    let stderr = String::from_utf8(out.get_output().stderr.clone()).unwrap();
    assert!(stderr.contains("macOS-only"), "stderr was: {stderr}");
    assert!(!home.path().join("Library").exists());
    assert_eq!(
        std::fs::read_to_string(&config).unwrap(),
        "font-size = 14\n"
    );
}