termlens-cli 0.11.3

The termlens command: run a program in a PTY and print its screen, diff two saved screens, render one as SVG, HTML, ANSI, the text format or JSON
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
//! The CLI driven the way its users' programs are: through a PTY, by
//! termlens (#255). `diff` and `render` read the saved screens under
//! `tests/data`; `inspect` runs a shell.

use termlens::{Color, Screen};

fn data(name: &str) -> String {
    format!("{}/tests/data/{name}", env!("CARGO_MANIFEST_DIR"))
}

#[test]
fn diff_exits_one_and_paints_the_changed_cells_on_a_terminal() -> termlens::Result<()> {
    let mut t = termlens::bin!(
        "termlens",
        args(["diff", &data("before.snap"), &data("after.snap.new")])
    )?;
    let status = t.wait_exit()?;
    assert_eq!(status.code(), Some(1), "{}", t.screen());
    let s = t.screen();
    assert!(s.contains("size: 30x4"), "{s}");
    // Row 2 changed in one cell: `1` red on the left, `2` green on the right.
    let (row, col) = s.find("Counter: 1").expect("the before side");
    let one = s.cell(row, col + 9).unwrap();
    assert_eq!(one.contents(), "1");
    assert_eq!(one.style().fg, Color::Indexed(1), "{}", s.with_styles());
    let (row, col) = s.find("Counter: 2").expect("the after side");
    assert_eq!(s.cell(row, col + 9).unwrap().style().fg, Color::Indexed(2));
    // The style runs before โ†’ after, as the plain rendering prints them.
    assert!(
        s.contains("styles: 0: 0-4 fg=6 bold; 7-13 reverse โ†’ 0-4 fg=6 bold"),
        "{s}"
    );
    assert!(s.contains("2 rows unchanged"), "{s}");
    Ok(())
}

#[test]
fn diff_stays_plain_when_asked_and_exits_zero_on_the_same_picture() -> termlens::Result<()> {
    let mut t = termlens::bin!(
        "termlens",
        args([
            "diff",
            "--color",
            "never",
            &data("before.snap"),
            &data("after.snap.new")
        ])
    )?;
    assert_eq!(t.wait_exit()?.code(), Some(1));
    let s = t.screen();
    assert!(
        s.contains("^"),
        "the marker line stands in for colour:\n{s}"
    );
    assert!(
        s.find_by(|c| c.style().fg != Color::Default).is_none(),
        "{}",
        s.with_styles()
    );

    let mut t = termlens::bin!(
        "termlens",
        args(["diff", &data("before.snap"), &data("before.snap")])
    )?;
    assert_eq!(t.wait_exit()?.code(), Some(0));
    assert!(t.screen().contains("no difference"), "{}", t.screen());
    Ok(())
}

/// A comparison of two differently sized screens says so in colour mode
/// too (#365): the plain rendering's second header line, the one naming
/// the overlap, used to be dropped by `colored()`'s prefix filter.
#[test]
fn diff_color_always_keeps_the_overlap_note_and_paints_the_rows() -> termlens::Result<()> {
    let mut t = termlens::bin!(
        "termlens",
        args([
            "diff",
            "--color",
            "always",
            &data("overlap-before.snap"),
            &data("overlap-after.snap")
        ])
    )?;
    assert_eq!(t.wait_exit()?.code(), Some(1), "{}", t.screen());
    let s = t.screen();
    assert!(
        s.contains("compared over the 4x2 overlap; the rest is clipped"),
        "the note saying the comparison was partial:\n{s}"
    );
    // The rows are still painted: red for what the before screen showed,
    // green for what the after one shows.
    let (row, col) = s.find("abXX").expect("the after side");
    let two = s.cell(row, col + 2).expect("a changed cell");
    assert_eq!(two.contents(), "X");
    assert_eq!(two.style().fg, Color::Indexed(2), "{}", s.with_styles());
    // The before side's changed cells are two blanks here, and ConPTY does
    // not carry a foreground-only run written around isolated spaces across
    // the line boundary: on Windows the blanks arrive uncoloured and the
    // next row's red run is two cells wider (seen in the styles of run
    // 35188093990). The exact attribution is pinned where the stream
    // survives, and the note above is asserted everywhere.
    #[cfg(not(windows))]
    {
        let (row, col) = s.find("โ”‚ab  โ”‚").expect("the before side");
        let blank = s.cell(row, col + 4).expect("a painted blank");
        assert_eq!(blank.style().fg, Color::Indexed(1), "{}", s.with_styles());
    }

    // An empty diff is still the one line, with no colour to go with it.
    let mut t = termlens::bin!(
        "termlens",
        args([
            "diff",
            "--color",
            "always",
            &data("overlap-before.snap"),
            &data("overlap-before.snap")
        ])
    )?;
    assert_eq!(t.wait_exit()?.code(), Some(0));
    let s = t.screen();
    assert!(s.contains("no difference"), "{s}");
    assert!(
        s.find_by(|c| c.style().fg != Color::Default).is_none(),
        "{}",
        s.with_styles()
    );
    Ok(())
}

/// `--color` with no WHEN used to print the raw Option โ€” `None` when the
/// flag ended the line, `Some("before.snap")` when it quietly ate the
/// operand after it โ€” internals that name neither the flag's requirement
/// nor the mistake (#452).
#[test]
fn diff_names_a_missing_or_unknown_color_value() -> termlens::Result<()> {
    // The operand after the flag is taken as the value and refused as one
    // quoted word, exactly as `--color=pink` always was.
    let out = with_stdin(
        &[
            "diff",
            "--color",
            &data("before.snap"),
            &data("after.snap.new"),
        ],
        "",
    );
    assert_eq!(out.status.code(), Some(2), "{out:?}");
    let stderr = String::from_utf8_lossy(&out.stderr);
    // The operand is the value, quoted and alone โ€” `data()`'s absolute path
    // Debug-escapes its backslashes on Windows, so the shape is pinned at
    // the front and the eaten operand at the back, `Some(` nowhere.
    assert!(
        stderr.starts_with(r#"termlens: --color takes auto, always or never, got ""#)
            && stderr.contains("before.snap")
            && !stderr.contains("Some("),
        "the operand named as the value it was taken for: {stderr}"
    );

    // The flag ending the line has no value to quote, so it says it needs
    // one โ€” the shape every other flag's missing argument takes.
    let out = with_stdin(
        &[
            "diff",
            &data("before.snap"),
            &data("after.snap.new"),
            "--color",
        ],
        "",
    );
    assert_eq!(out.status.code(), Some(2), "{out:?}");
    let stderr = String::from_utf8_lossy(&out.stderr);
    assert!(
        stderr.contains("--color needs a WHEN argument"),
        "the missing value, not `None`: {stderr}"
    );

    // The `=` spelling keeps its diagnostic, from the list it now shares.
    let out = with_stdin(
        &[
            "diff",
            "--color=pink",
            &data("before.snap"),
            &data("after.snap.new"),
        ],
        "",
    );
    assert_eq!(out.status.code(), Some(2), "{out:?}");
    let stderr = String::from_utf8_lossy(&out.stderr);
    assert!(
        stderr.contains(r#"--color takes auto, always or never, got "pink""#),
        "the offending word alone: {stderr}"
    );
    Ok(())
}

/// The three good WHENs, in both spellings of the flag (#452): `always`
/// paints even a pipe, `never` keeps it plain with the `^` marker line,
/// and `auto` is plain in a pipe โ€” the same bytes as `never` there.
#[test]
fn diff_accepts_the_three_color_words_in_both_spellings() -> termlens::Result<()> {
    let before = data("before.snap");
    let after = data("after.snap.new");
    for word in ["auto", "always", "never"] {
        let inline = format!("--color={word}");
        // One flag, two spellings: `--color never` and `--color=never`
        // parse the same three words.
        for args in [
            vec!["diff", "--color", word, before.as_str(), after.as_str()],
            vec!["diff", inline.as_str(), before.as_str(), after.as_str()],
        ] {
            let out = with_stdin(&args, "");
            assert_eq!(out.status.code(), Some(1), "{word}: {out:?}");
            let stdout = String::from_utf8_lossy(&out.stdout);
            if word == "always" {
                assert!(stdout.contains('\x1b'), "painted: {stdout}");
            } else {
                assert!(stdout.contains('^'), "plain, marker line and all: {stdout}");
            }
        }
    }

    // In a pipe `auto` is `never`: same bytes, no escape to be found.
    let never = with_stdin(&["diff", "--color", "never", &before, &after], "");
    let auto = with_stdin(&["diff", "--color=auto", &before, &after], "");
    assert_eq!(never.stdout, auto.stdout, "a pipe is not a terminal");
    assert!(
        !auto.stdout.contains(&0x1b),
        "no ANSI in a pipe: {}",
        String::from_utf8_lossy(&auto.stdout)
    );
    Ok(())
}

#[test]
fn render_writes_svg_html_ansi_and_text() -> termlens::Result<()> {
    for (flag, needle) in [("--svg", "<svg"), ("--html", "<pre"), ("--text", "styles:")] {
        let mut t = termlens::bin!("termlens", args(["render", flag, &data("before.snap")]))?;
        assert_eq!(t.wait_exit()?.code(), Some(0), "{flag}: {}", t.screen());
        assert!(t.screen().contains(needle), "{flag}:\n{}", t.screen());
    }
    // ANSI is the screen itself, styles and all, painted into our PTY.
    let mut t = termlens::bin!("termlens", args(["render", "--ansi", &data("before.snap")]))?;
    assert_eq!(t.wait_exit()?.code(), Some(0));
    let s = t.screen();
    let (row, col) = s.find("myapp").expect("the title");
    let title = s.cell(row, col).unwrap().style();
    assert!(
        title.bold && title.fg == Color::Indexed(6),
        "{}",
        s.with_styles()
    );
    let (row, col) = s.find("secret").expect("the field");
    assert!(
        s.cell(row, col).unwrap().style().conceal,
        "{}",
        s.with_styles()
    );
    Ok(())
}

/// `render --json` writes what `render --text` reads (#373): the format-1
/// JSON document `docs/STABILITY.md` promises. The CLI could read the
/// format and not write it, so the two promised saved-screen formats
/// converted in one direction only.
#[test]
fn render_json_writes_the_document_render_text_reads_back() -> termlens::Result<()> {
    use std::process::Command;
    let bin = env!("CARGO_BIN_EXE_termlens");

    let json = Command::new(bin)
        .args(["render", "--json", &data("before.snap")])
        .output()?;
    assert_eq!(json.status.code(), Some(0), "{json:?}");
    let document = String::from_utf8(json.stdout).expect("utf-8 JSON");
    let value: serde_json::Value = serde_json::from_str(&document).expect("a JSON document");
    assert_eq!(value["format"], 1, "{document}");
    // The library reads its own document back, byte for byte.
    let screen: Screen = serde_json::from_str(&document).expect("a termlens Screen");
    assert_eq!(screen.size(), (30, 4));
    assert_eq!(
        document,
        format!(
            "{}\n",
            serde_json::to_string_pretty(&screen).expect("serialize")
        )
    );

    // text -> json -> text: the round trip through the two formats lands on
    // the bytes the text rendering prints.
    let from_text = Command::new(bin)
        .args(["render", "--text", &data("before.snap")])
        .output()?;
    assert_eq!(from_text.status.code(), Some(0), "{from_text:?}");
    let back = with_stdin(&["render", "--text", "-"], &document);
    assert_eq!(back.status.code(), Some(0), "{back:?}");
    assert_eq!(
        back.stdout, from_text.stdout,
        "json -> text is the same screen"
    );

    // --out carries exactly the bytes stdout did, and stdin is an input
    // like any path: `render --json -` is `render --json before.snap`.
    let dir = std::env::temp_dir().join(format!("termlens-render-json-{}", std::process::id()));
    std::fs::create_dir_all(&dir)?;
    let written = dir.join("screen.json");
    let out = Command::new(bin)
        .args([
            "render",
            "--json",
            "--out",
            written.to_str().expect("utf-8 path"),
            &data("before.snap"),
        ])
        .output()?;
    assert_eq!(out.status.code(), Some(0), "{out:?}");
    assert!(out.stdout.is_empty(), "--out means not stdout: {out:?}");
    assert_eq!(std::fs::read(&written)?, document.as_bytes());
    let piped = with_stdin(&["render", "--json", "-"], &document);
    assert_eq!(piped.status.code(), Some(0), "{piped:?}");
    assert_eq!(piped.stdout, document.as_bytes(), "stdin is a path's equal");

    // A JSON file renders through --json unchanged โ€” format 1 in, the same
    // document out.
    let again = Command::new(bin)
        .args(["render", "--json", written.to_str().expect("utf-8 path")])
        .output()?;
    assert_eq!(again.status.code(), Some(0), "{again:?}");
    assert_eq!(
        again.stdout,
        document.as_bytes(),
        "the JSON twin round-trips through the CLI"
    );

    let help = Command::new(bin).args(["render", "--help"]).output()?;
    let usage = String::from_utf8_lossy(&help.stdout);
    let first = usage.lines().next().expect("a usage line");
    assert!(
        first.contains("--json"),
        "the usage line lists --json: {first}"
    );

    let _ = std::fs::remove_dir_all(&dir);
    Ok(())
}

#[test]
fn a_file_that_is_not_a_screen_exits_two_and_names_it() -> termlens::Result<()> {
    // Wide, so the path in the diagnostic is not wrapped across two rows.
    let mut t = termlens::bin!(
        "termlens",
        size(200, 10),
        args(["render", "--svg", &data("missing.snap")])
    )?;
    assert_eq!(t.wait_exit()?.code(), Some(2));
    assert!(
        t.screen().contains("termlens: ") && t.screen().contains("missing.snap"),
        "{}",
        t.screen()
    );

    let mut t = termlens::bin!(
        "termlens",
        args(["diff", &data("cli.rs"), &data("before.snap")])
    )?;
    // tests/data/cli.rs does not exist either; the source file next door is
    // the one that parses to nothing.
    assert_eq!(t.wait_exit()?.code(), Some(2));

    let mut t = termlens::bin!("termlens", args(["frobnicate"]))?;
    assert_eq!(t.wait_exit()?.code(), Some(2));
    assert!(t.screen().contains("unknown command"), "{}", t.screen());
    Ok(())
}

/// A flag typed in place of a subcommand is an unknown option, not an
/// unknown command (#475). `-v` and `--verbose` are the two most likely
/// wrong guesses at this CLI, and both are flags. `nonesuch` stays a
/// command โ€” `check-cli-contract.sh` pins the exit, this pins the word โ€”
/// and so does a bare `-`, which every subcommand reads as standard input
/// rather than as an option.
#[test]
fn a_top_level_flag_is_an_unknown_option_not_a_command() {
    use std::process::Command;
    let bin = env!("CARGO_BIN_EXE_termlens");
    for flag in ["--verbose", "-v"] {
        let out = Command::new(bin).arg(flag).output().expect("spawn");
        assert_eq!(out.status.code(), Some(2), "{flag}: {out:?}");
        let stderr = String::from_utf8_lossy(&out.stderr);
        assert_eq!(
            stderr,
            format!("termlens: unknown option {flag:?} (try --help)\n"),
            "{flag}"
        );
    }

    for command in ["nonesuch", "-"] {
        let out = Command::new(bin).arg(command).output().expect("spawn");
        assert_eq!(out.status.code(), Some(2), "{command}: {out:?}");
        assert_eq!(
            String::from_utf8_lossy(&out.stderr),
            format!("termlens: unknown command {command:?} (try --help)\n"),
            "{command}"
        );
    }
}

#[test]
fn the_text_a_wait_error_prints_is_a_saved_screen_too() -> termlens::Result<()> {
    // No header, no styles block: the block from a CI log.
    let path = std::env::temp_dir().join(format!("termlens-cli-{}.txt", std::process::id()));
    std::fs::write(&path, "size: 10x2  cursor: hidden\nhello\n")?;
    let mut t = termlens::bin!(
        "termlens",
        args(["render", "--text", path.to_str().unwrap()])
    )?;
    assert_eq!(t.wait_exit()?.code(), Some(0), "{}", t.screen());
    let s = t.screen();
    assert!(
        s.contains("size: 10x2  cursor: hidden") && s.contains("(none)"),
        "{s}"
    );
    let _ = std::fs::remove_file(&path);
    Ok(())
}

#[test]
fn a_crlf_checkout_of_a_snap_still_parses() -> termlens::Result<()> {
    // Git on Windows may hand the file over with CRLF endings; the header
    // and the grid are line-based and must not care.
    // (Normalized first: on Windows the checkout may already be CRLF.)
    let crlf = std::fs::read_to_string(data("before.snap"))?
        .replace("\r\n", "\n")
        .replace('\n', "\r\n");
    let path = std::env::temp_dir().join(format!("termlens-cli-crlf-{}.snap", std::process::id()));
    std::fs::write(&path, crlf)?;
    let mut t = termlens::bin!(
        "termlens",
        args(["render", "--text", path.to_str().unwrap()])
    )?;
    assert_eq!(t.wait_exit()?.code(), Some(0), "{}", t.screen());
    assert!(t.screen().contains("myapp  > Alpha"), "{}", t.screen());
    let _ = std::fs::remove_file(&path);
    Ok(())
}

#[test]
#[cfg_attr(windows, ignore = "the program under inspection is a POSIX shell")]
fn inspect_prints_the_screen_and_the_exit_trailer() -> termlens::Result<()> {
    let path = std::env::var("PATH").unwrap_or_default();
    let mut t = termlens::bin!(
        "termlens",
        env("PATH", &path),
        args(["inspect", "--size", "20x3", "sh", "-c", "printf 'hi there'"])
    )?;
    assert_eq!(t.wait_exit()?.code(), Some(0), "{}", t.screen());
    let s = t.screen();
    assert!(s.contains("size: 20x3") && s.contains("hi there"), "{s}");
    assert!(s.contains("--- exited: exit code 0 ---"), "{s}");

    let mut t = termlens::bin!(
        "termlens",
        env("PATH", &path),
        args(["inspect", "--size", "12", "sh"])
    )?;
    assert_eq!(t.wait_exit()?.code(), Some(2));
    assert!(
        t.screen().contains("expected e.g. 120x40"),
        "{}",
        t.screen()
    );

    // The `=` spelling reaches the same parse, so it keeps the same
    // diagnostic, naming the flag the user typed and not the whole token.
    let mut t = termlens::bin!(
        "termlens",
        env("PATH", &path),
        args(["inspect", "--size=12", "sh"])
    )?;
    assert_eq!(t.wait_exit()?.code(), Some(2));
    assert!(
        t.screen()
            .contains("bad --size \"12\", expected e.g. 120x40"),
        "{}",
        t.screen()
    );
    Ok(())
}

/// The `--flag=value` spelling beside the two-argument one, as `diff
/// --color=` and `render --out=` already have it (#366).
#[test]
#[cfg_attr(windows, ignore = "the program under inspection is a POSIX shell")]
fn inspect_accepts_the_flag_equals_value_spelling() -> termlens::Result<()> {
    let path = std::env::var("PATH").unwrap_or_default();
    let mut t = termlens::bin!(
        "termlens",
        env("PATH", &path),
        args([
            "inspect",
            "--size=60x3",
            "--timeout=5",
            "--idle=100",
            "sh",
            "-c",
            "printf 'hi there'"
        ])
    )?;
    assert_eq!(t.wait_exit()?.code(), Some(0), "{}", t.screen());
    let s = t.screen();
    assert!(s.contains("size: 60x3") && s.contains("hi there"), "{s}");
    Ok(())
}

/// `--flag=value` is for the flags that take a value; a value attached to a
/// flag that takes none is refused with the whole token named, as `render`
/// refuses `--svg=1`. Splitting every long flag would apply `--inherit-env`
/// and drop the `nonsense`, which is the silent-wrong-answer shape the
/// spelling was added to avoid (#366).
#[test]
fn inspect_refuses_a_value_on_a_flag_that_takes_none() -> termlens::Result<()> {
    let mut t = termlens::bin!(
        "termlens",
        args(["inspect", "--inherit-env=nonsense", "/bin/echo", "hi"])
    )?;
    let status = t.wait_exit()?;
    assert_eq!(status.code(), Some(2), "{}", t.screen());
    let s = t.screen();
    assert!(
        s.contains(r#"unknown option "--inherit-env=nonsense""#),
        "the diagnostic names the whole token: {s}"
    );
    Ok(())
}

/// `--env=KEY=VALUE` splits on the *first* `=` only, and `--cwd=PATH` runs
/// the program where it says, exactly as their two-argument forms do (#366).
#[test]
#[cfg_attr(windows, ignore = "the program under inspection is a POSIX shell")]
fn inspect_env_equals_splits_on_the_first_equals() -> termlens::Result<()> {
    let path = std::env::var("PATH").unwrap_or_default();
    // A directory made here, so the assertion is not about /tmp's own name
    // on a platform that symlinks it (macOS: /tmp -> /private/tmp).
    let dir = std::env::temp_dir().join(format!("termlens-eq-{}", std::process::id()));
    std::fs::create_dir_all(&dir)?;
    let real = std::fs::canonicalize(&dir)?;
    // Wide enough that the path is one row, as in the two-argument test.
    let cwd = format!("--cwd={}", dir.display());
    let mut t = termlens::bin!(
        "termlens",
        env("PATH", &path),
        args([
            "inspect",
            "--size=200x3",
            &cwd,
            "--env=A=b=c",
            "sh",
            "-c",
            "echo \"[$A] $(pwd)\""
        ])
    )?;
    assert_eq!(t.wait_exit()?.code(), Some(0), "{}", t.screen());
    let s = t.screen();
    assert!(s.contains("[b=c]"), "the value keeps its second `=`: {s}");
    assert!(
        s.contains(real.to_str().expect("utf-8 path")),
        "the program ran in {}: {s}",
        real.display()
    );
    let _ = std::fs::remove_dir_all(&dir);
    Ok(())
}

#[test]
fn help_and_version() -> termlens::Result<()> {
    let mut t = termlens::bin!("termlens", args(["--help"]))?;
    assert_eq!(t.wait_exit()?.code(), Some(0));
    assert!(
        t.screen().contains("usage: termlens <command>"),
        "{}",
        t.screen()
    );
    let mut t = termlens::bin!("termlens", args(["inspect", "--help"]))?;
    assert_eq!(t.wait_exit()?.code(), Some(0));
    let help = t.screen().to_string();
    // The help has to say which rendering a redirect gets, or the split
    // #454 and #478 introduced is undiscoverable: both flags behave one
    // way at a prompt and another through a pipe. Needles chosen to sit
    // within one line of the 80-column help, so this pins the claim
    // rather than the line breaks.
    assert!(
        help.contains("--ansi") && help.contains("gets a saved screen"),
        "`inspect --help` must say a redirect gets a saved screen:\n{help}"
    );
    let mut t = termlens::bin!("termlens", args(["--version"]))?;
    assert_eq!(t.wait_exit()?.code(), Some(0));
    assert!(
        t.screen()
            .contains(concat!("termlens ", env!("CARGO_PKG_VERSION"))),
        "{}",
        t.screen()
    );
    let mut t = termlens::bin!("termlens")?;
    assert_eq!(t.wait_exit()?.code(), Some(2));
    // Unused otherwise, but the round trip through the library is the point.
    let _: Screen = Screen::parse("size: 1x1  cursor: 0,0\n")?;
    Ok(())
}

#[test]
fn subcommand_version_prints_same_string_as_top_level() -> termlens::Result<()> {
    // The top-level version string is the reference.
    let mut top = termlens::bin!("termlens", args(["--version"]))?;
    assert_eq!(top.wait_exit()?.code(), Some(0));
    let expected = concat!("termlens ", env!("CARGO_PKG_VERSION"));

    for subcommand in ["diff", "render", "inspect"] {
        let mut t = termlens::bin!("termlens", args([subcommand, "--version"]))?;
        assert_eq!(
            t.wait_exit()?.code(),
            Some(0),
            "`termlens {subcommand} --version` exited non-zero"
        );
        assert!(
            t.screen().contains(expected),
            "`termlens {subcommand} --version` output:\n{}",
            t.screen()
        );
    }
    Ok(())
}

/// `inspect โ€ฆ > file` is a saved screen (#340): the screen alone goes to
/// stdout and the trailer to stderr, so the file a redirect captures is
/// what `render` and `diff` read. Driven without a PTY here on purpose โ€”
/// through one, both streams land on the same screen and the split is
/// invisible.
#[test]
#[cfg_attr(windows, ignore = "the program under inspection is a POSIX shell")]
fn inspect_stdout_is_a_saved_screen_and_the_trailer_is_on_stderr() -> termlens::Result<()> {
    use std::process::Command;
    let bin = env!("CARGO_BIN_EXE_termlens");
    let out = Command::new(bin)
        .args(["inspect", "--size", "20x3", "sh", "-c", "printf 'hi there'"])
        .output()?;
    assert!(out.status.success(), "{out:?}");
    let stdout = String::from_utf8(out.stdout).expect("utf-8");
    let stderr = String::from_utf8(out.stderr).expect("utf-8");
    assert!(stdout.starts_with("size: 20x3  cursor: "), "{stdout:?}");
    assert!(stdout.contains("hi there"), "{stdout:?}");
    assert!(
        stdout.contains("styles:"),
        "a redirect keeps the styles block: {stdout:?}"
    );
    assert!(!stdout.contains("---"), "no trailer on stdout: {stdout:?}");
    assert_eq!(
        stderr.trim_end(),
        "--- exited: exit code 0 ---",
        "{stderr:?}"
    );
    // What the library reads from it is the screen inspect saw.
    let parsed = Screen::parse(&stdout)?;
    assert_eq!(parsed.size(), (20, 3));
    assert_eq!(parsed.find("hi there"), Some((0, 0)));

    // And the CLI reads its own output back: render, and diff against itself.
    let path =
        std::env::temp_dir().join(format!("termlens-cli-inspect-{}.txt", std::process::id()));
    std::fs::write(&path, &stdout)?;
    let file = path.to_str().unwrap();
    let render = Command::new(bin)
        .args(["render", "--text", file])
        .output()?;
    assert_eq!(render.status.code(), Some(0), "{render:?}");
    let diff = Command::new(bin).args(["diff", file, file]).output()?;
    assert_eq!(diff.status.code(), Some(0), "{diff:?}");

    // A file a 0.10 inspect saved โ€” trailer on stdout โ€” still reads.
    let old = path.with_extension("old.txt");
    std::fs::write(&old, format!("{stdout}--- exited: exit code 0 ---\n"))?;
    let render = Command::new(bin)
        .args(["render", "--text", old.to_str().unwrap()])
        .output()?;
    assert_eq!(render.status.code(), Some(0), "{render:?}");
    let diff = Command::new(bin)
        .args(["diff", file, old.to_str().unwrap()])
        .output()?;
    assert_eq!(
        diff.status.code(),
        Some(0),
        "the trailer is not a row: {diff:?}"
    );
    // โ€ฆwhile a grid row that merely starts with `---` is content.
    let dashes = path.with_extension("dashes.txt");
    std::fs::write(&dashes, "size: 20x2  cursor: 0,0\n--- not a trailer\n")?;
    let render = Command::new(bin)
        .args(["render", "--text", dashes.to_str().unwrap()])
        .output()?;
    assert_eq!(render.status.code(), Some(0), "{render:?}");
    assert!(
        String::from_utf8_lossy(&render.stdout).contains("--- not a trailer"),
        "{render:?}"
    );
    for p in [path, old, dashes] {
        let _ = std::fs::remove_file(p);
    }
    Ok(())
}

/// `inspect --` ends the options, and it changes the outcome in exactly one
/// case: a program named like a flag (#453). Options already stop at the
/// first argument that does not begin with `-`, so without `--` such a name
/// is read as an option and refused before anything is spawned.
///
/// The two cases are told apart by *which* failure comes back. `-weird` is
/// not on PATH either way; with `--` it is looked up as a program and fails
/// to spawn, without it it never gets that far. A deleted `"--" => break`
/// arm turns the first into the second.
#[test]
fn inspect_double_dash_ends_the_options() {
    use std::process::Command;
    let bin = env!("CARGO_BIN_EXE_termlens");

    let with = Command::new(bin)
        .args(["inspect", "--size", "20x2", "--", "-weird", "ok"])
        .output()
        .expect("termlens runs");
    assert_eq!(with.status.code(), Some(2), "{with:?}");
    let stderr = String::from_utf8_lossy(&with.stderr);
    assert!(
        stderr.contains("failed to spawn") && stderr.contains("-weird"),
        "after `--` a flag-shaped name is a program: {stderr}"
    );
    assert!(
        !stderr.contains("unknown option"),
        "`--` must stop option parsing: {stderr}"
    );

    let without = Command::new(bin)
        .args(["inspect", "--size", "20x2", "-weird", "ok"])
        .output()
        .expect("termlens runs");
    assert_eq!(without.status.code(), Some(2), "{without:?}");
    assert!(
        String::from_utf8_lossy(&without.stderr).contains("unknown option \"-weird\""),
        "without `--` the same name is an option: {without:?}"
    );

    // And the everyday case: a program after `--` runs.
    let echo = Command::new(bin)
        .args(["inspect", "--size", "20x2", "--", "sh", "-c", "printf hi"])
        .output()
        .expect("termlens runs");
    #[cfg(not(windows))]
    {
        assert_eq!(echo.status.code(), Some(0), "{echo:?}");
        let screen = Screen::parse(&String::from_utf8_lossy(&echo.stdout)).expect("a saved screen");
        assert_eq!(screen.find("hi"), Some((0, 0)), "{}", screen.text());
    }
    #[cfg(windows)]
    let _ = echo; // no POSIX shell to run; the two refusals above are the contract
}

/// `inspect --ansi > file` is still a saved screen (#478). `--ansi` paints
/// on a terminal; a pipe writes `with_styles` so `render` and `diff` read
/// the file back, including the colour the flag was asked to keep.
#[test]
#[cfg_attr(windows, ignore = "the program under inspection is a POSIX shell")]
fn inspect_ansi_redirect_is_a_saved_screen() -> termlens::Result<()> {
    use std::process::Command;
    let bin = env!("CARGO_BIN_EXE_termlens");
    let out = Command::new(bin)
        .args([
            "inspect",
            "--size",
            "30x3",
            "--ansi",
            "sh",
            "-c",
            r#"printf '\033[1;31mred\033[0m'"#,
        ])
        .output()?;
    assert!(out.status.success(), "{out:?}");
    let stdout = String::from_utf8(out.stdout).expect("utf-8");
    assert!(
        !stdout.contains('\u{1b}'),
        "a redirect must not write C0: {stdout:?}"
    );
    let parsed = Screen::parse(&stdout).expect("stdout is a saved screen");
    assert_eq!(parsed.size(), (30, 3));
    assert_eq!(parsed.find("red"), Some((0, 0)));
    let cell = parsed.cell(0, 0).expect("the painted cell");
    assert_eq!(
        cell.style().fg,
        Color::Indexed(1),
        "{}",
        parsed.with_styles()
    );
    assert!(cell.style().bold, "{}", parsed.with_styles());
    assert!(
        stdout.contains("styles:"),
        "colour survives as the styles block: {stdout:?}"
    );

    let path = std::env::temp_dir().join(format!(
        "termlens-cli-inspect-ansi-{}.txt",
        std::process::id()
    ));
    std::fs::write(&path, &stdout)?;
    let file = path.to_str().unwrap();
    let render = Command::new(bin)
        .args(["render", "--text", file])
        .output()?;
    assert_eq!(render.status.code(), Some(0), "{render:?}");
    let diff = Command::new(bin).args(["diff", file, file]).output()?;
    assert_eq!(diff.status.code(), Some(0), "{diff:?}");
    let _ = std::fs::remove_file(&path);
    Ok(())
}

/// `--ansi` on a terminal still paints, so a person who asked for colour
/// sees it rather than a `styles:` block. Through a PTY, stdout *is* a
/// terminal, and the outer emulator consumes the SGR โ€” the cell is red.
#[test]
#[cfg_attr(windows, ignore = "the program under inspection is a POSIX shell")]
fn inspect_ansi_paints_on_a_terminal() -> termlens::Result<()> {
    let path = std::env::var("PATH").unwrap_or_default();
    let mut t = termlens::bin!(
        "termlens",
        env("PATH", &path),
        args([
            "inspect",
            "--size",
            "30x3",
            "--ansi",
            "sh",
            "-c",
            r#"printf '\033[1;31mred\033[0m'"#
        ])
    )?;
    assert_eq!(t.wait_exit()?.code(), Some(0), "{}", t.screen());
    let s = t.screen();
    assert!(s.contains("red"), "{s}");
    assert!(
        !s.contains("styles:"),
        "a terminal is painted, not described: {s}"
    );
    // This branch builds its header by hand, from the first line of the
    // plain rendering, and only then hands the grid to `to_ansi()` (#477).
    // The header is what a person scanning a CI log looks for first, and
    // nothing else pins that it survives the hand-built path.
    assert!(
        s.row_text(0).starts_with("size: 30x3"),
        "the header must lead the painted screen: {}",
        s.with_styles()
    );
    let (row, col) = s.find("red").expect("the painted word");
    let cell = s.cell(row, col).expect("the painted cell");
    assert_eq!(cell.style().fg, Color::Indexed(1), "{}", s.with_styles());
    assert!(cell.style().bold, "{}", s.with_styles());
    Ok(())
}

/// The other half of #454's split: a terminal is a person looking, so it
/// gets the plain text it always got. Without this, "a redirect keeps its
/// styles" collapses into "everything carries a styles block", which is
/// the noise #454 weighed and did not want at a prompt โ€” and nothing else
/// here would notice the difference, because every other test in this file
/// reads `inspect` through a pipe.
#[test]
#[cfg_attr(windows, ignore = "the program under inspection is a POSIX shell")]
fn inspect_on_a_terminal_stays_plain() -> termlens::Result<()> {
    let path = std::env::var("PATH").unwrap_or_default();
    let mut t = termlens::bin!(
        "termlens",
        env("PATH", &path),
        args([
            "inspect",
            "--size",
            "30x3",
            "sh",
            "-c",
            r#"printf '\033[1;31mred\033[0m'"#
        ])
    )?;
    assert_eq!(t.wait_exit()?.code(), Some(0), "{}", t.screen());
    let s = t.screen();
    assert!(s.contains("red"), "{s}");
    assert!(
        !s.contains("styles:"),
        "a terminal gets what it always got, not a saved screen: {s}"
    );
    Ok(())
}

/// `inspect โ€ฆ > file` used to write the plain rendering, so two screens
/// that differed only in colour compared as the same picture (#454).
/// The redirect is a `with_styles` snapshot now: `diff` names the run,
/// and the file still round-trips through `render` โ€” including one a
/// 0.10 inspect saved, trailer on stdout.
#[test]
#[cfg_attr(windows, ignore = "the program under inspection is a POSIX shell")]
fn inspect_redirect_keeps_styles_so_diff_sees_a_colour_change() -> termlens::Result<()> {
    use std::process::Command;
    let bin = env!("CARGO_BIN_EXE_termlens");
    let inspect = |sgr: &str| {
        Command::new(bin)
            .args([
                "inspect",
                "--size",
                "20x2",
                "sh",
                "-c",
                &format!(r#"printf '\033[{sgr}mred\033[0m'"#),
            ])
            .output()
            .expect("inspect ran")
    };
    let red = inspect("1;31");
    let green = inspect("1;32");
    assert!(red.status.success(), "{red:?}");
    assert!(green.status.success(), "{green:?}");
    let red_out = String::from_utf8(red.stdout).expect("utf-8");
    let green_out = String::from_utf8(green.stdout).expect("utf-8");
    assert!(
        red_out.contains("styles:") && green_out.contains("styles:"),
        "a redirect keeps the styles block:\n{red_out}\n{green_out}"
    );

    let red_screen = Screen::parse(&red_out).expect("red is a saved screen");
    let green_screen = Screen::parse(&green_out).expect("green is a saved screen");
    let red_cell = red_screen.cell(0, 0).expect("the painted cell");
    let green_cell = green_screen.cell(0, 0).expect("the painted cell");
    assert_eq!(
        red_cell.style().fg,
        Color::Indexed(1),
        "{}",
        red_screen.with_styles()
    );
    assert_eq!(
        green_cell.style().fg,
        Color::Indexed(2),
        "{}",
        green_screen.with_styles()
    );
    assert!(red_cell.style().bold && green_cell.style().bold);

    let dir = std::env::temp_dir().join(format!(
        "termlens-cli-inspect-styles-{}",
        std::process::id()
    ));
    std::fs::create_dir_all(&dir)?;
    let red_path = dir.join("red.snap");
    let green_path = dir.join("green.snap");
    std::fs::write(&red_path, &red_out)?;
    std::fs::write(&green_path, &green_out)?;
    let red_file = red_path.to_str().unwrap();
    let green_file = green_path.to_str().unwrap();

    let diff = Command::new(bin)
        .args(["diff", "--color", "never", red_file, green_file])
        .output()?;
    assert_eq!(diff.status.code(), Some(1), "{diff:?}");
    let diff_out = String::from_utf8_lossy(&diff.stdout);
    assert!(
        diff_out.contains("styles:") && diff_out.contains("fg=1") && diff_out.contains("fg=2"),
        "diff names the colour change: {diff_out}"
    );

    let text = Command::new(bin)
        .args(["render", "--text", red_file])
        .output()?;
    assert_eq!(text.status.code(), Some(0), "{text:?}");
    assert!(
        String::from_utf8_lossy(&text.stdout).contains("styles:"),
        "{text:?}"
    );
    let json = Command::new(bin)
        .args(["render", "--json", red_file])
        .output()?;
    assert_eq!(json.status.code(), Some(0), "{json:?}");
    let document = String::from_utf8(json.stdout).expect("utf-8 JSON");
    let from_json: Screen = serde_json::from_str(&document).expect("a termlens Screen");
    assert_eq!(
        from_json.cell(0, 0).expect("the painted cell").style().fg,
        Color::Indexed(1),
        "{}",
        from_json.with_styles()
    );

    // A file a 0.10 inspect saved โ€” trailer on stdout โ€” still reads.
    let old = dir.join("old.snap");
    std::fs::write(&old, format!("{red_out}--- exited: exit code 0 ---\n"))?;
    let render = Command::new(bin)
        .args(["render", "--text", old.to_str().unwrap()])
        .output()?;
    assert_eq!(render.status.code(), Some(0), "{render:?}");
    let against_old = Command::new(bin)
        .args(["diff", red_file, old.to_str().unwrap()])
        .output()?;
    assert_eq!(
        against_old.status.code(),
        Some(0),
        "the trailer is not a row: {against_old:?}"
    );

    let _ = std::fs::remove_dir_all(&dir);
    Ok(())
}

/// The compatibility corpus (#327): every saved screen a published release
/// wrote โ€” text and JSON โ€” is a file this CLI reads. The library's
/// `tests/compat.rs` holds the round trips; this is the fourth check it
/// names, from the tool's side.
#[test]
fn every_corpus_file_renders() -> termlens::Result<()> {
    use std::process::Command;
    let bin = env!("CARGO_BIN_EXE_termlens");
    let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../termlens/tests/compat");
    let mut files: Vec<std::path::PathBuf> = std::fs::read_dir(&root)?
        .map(|entry| entry.map(|e| e.path()))
        .collect::<Result<Vec<_>, _>>()?
        .into_iter()
        .filter(|p| p.is_dir())
        .flat_map(|dir| std::fs::read_dir(dir).expect("a version directory"))
        .map(|entry| entry.expect("a corpus file").path())
        .filter(|p| p.extension().is_some_and(|e| e == "txt" || e == "json"))
        .collect();
    files.sort();
    assert!(
        files.len() >= 12,
        "the 0.10.1 corpus alone is twelve files, found {}",
        files.len()
    );
    for file in files {
        let out = Command::new(bin)
            .args(["render", "--text", file.to_str().unwrap()])
            .output()?;
        assert_eq!(
            out.status.code(),
            Some(0),
            "{}: {}",
            file.display(),
            String::from_utf8_lossy(&out.stderr)
        );
        let text = String::from_utf8_lossy(&out.stdout);
        assert!(text.starts_with("size: "), "{}: {text}", file.display());
    }
    Ok(())
}

/// Run the CLI with `input` on standard input, and without a PTY: a pipe is
/// the whole point of `-`, and through a PTY there is no EOF to read to.
fn with_stdin(args: &[&str], input: &str) -> std::process::Output {
    use std::io::Write;
    use std::process::{Command, Stdio};
    let mut child = Command::new(env!("CARGO_BIN_EXE_termlens"))
        .args(args)
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .expect("spawn termlens");
    // A broken pipe here is a *result*, not a failure. `diff - -` refuses its
    // arguments before reading anything, so the child can exit and close the
    // pipe before this write lands โ€” the faster the refusal, the likelier it
    // is. The stress workflow found it on both Linux shards while it never
    // reproduced locally, because on an idle machine the bytes reach the
    // pipe buffer first. The child's exit code and stderr are what the
    // callers assert; whether it read the input is the child's business.
    let mut pipe = child.stdin.take().expect("a stdin pipe");
    match pipe.write_all(input.as_bytes()) {
        Ok(()) => {}
        Err(e) if e.kind() == std::io::ErrorKind::BrokenPipe => {}
        Err(e) => panic!("write to the child's stdin: {e}"),
    }
    drop(pipe);
    child.wait_with_output().expect("the child's output")
}

/// A saved screen most often arrives on a pipe โ€” out of a CI log, or from
/// the tool that made it a moment earlier โ€” and `-` is the convention for
/// that (#317).
#[test]
fn render_and_diff_read_a_screen_from_stdin() -> termlens::Result<()> {
    let before = std::fs::read_to_string(data("before.snap"))?;
    let after = std::fs::read_to_string(data("after.snap.new"))?;

    // `render --text -` is `render --text before.snap`, byte for byte.
    let piped = with_stdin(&["render", "--text", "-"], &before);
    assert_eq!(piped.status.code(), Some(0), "{piped:?}");
    let from_file = std::process::Command::new(env!("CARGO_BIN_EXE_termlens"))
        .args(["render", "--text", &data("before.snap")])
        .output()?;
    assert_eq!(
        piped.stdout, from_file.stdout,
        "the pipe and the path are the same screen"
    );

    // `diff` takes it as either operand, and the direction is the argument
    // order, not which one came off the pipe.
    let right = with_stdin(
        &["diff", "--color", "never", &data("before.snap"), "-"],
        &after,
    );
    assert_eq!(right.status.code(), Some(1), "{right:?}");
    let left = with_stdin(
        &["diff", "--color", "never", "-", &data("after.snap.new")],
        &before,
    );
    assert_eq!(left.status.code(), Some(1), "{left:?}");
    assert_eq!(right.stdout, left.stdout, "same two screens, same diff");
    assert!(
        String::from_utf8_lossy(&right.stdout).contains("Counter: 1"),
        "{}",
        String::from_utf8_lossy(&right.stdout)
    );

    // And a pipe that is not a screen is named as stdin, not as a file `-`.
    let junk = with_stdin(&["render", "--text", "-"], "not a saved screen\n");
    assert_eq!(junk.status.code(), Some(2), "{junk:?}");
    let stderr = String::from_utf8_lossy(&junk.stderr);
    assert!(stderr.contains("<stdin>"), "{stderr}");
    Ok(())
}

/// Standard input is read once, so it can be one of `diff`'s operands and
/// not both โ€” said plainly rather than left to look like an empty screen.
#[test]
fn diff_refuses_two_stdin_operands() -> termlens::Result<()> {
    let out = with_stdin(&["diff", "-", "-"], "size: 1x1  cursor: 0,0\n\n");
    assert_eq!(out.status.code(), Some(2), "{out:?}");
    let stderr = String::from_utf8_lossy(&out.stderr);
    assert!(
        stderr.contains("only one of the two screens can be `-`"),
        "one line, naming the reason: {stderr}"
    );
    assert_eq!(stderr.lines().count(), 1, "{stderr}");
    Ok(())
}

/// An empty value is what a shell hands over when a variable is unset, so
/// `--out=$DEST` with `DEST` never assigned is the way this arrives. Every
/// other `=`-spelled flag names itself when handed one; `--out=` reported
/// the filesystem's complaint about `""` instead (#451).
#[test]
fn render_empty_out_value_names_the_flag() -> termlens::Result<()> {
    use std::process::Command;
    let bin = env!("CARGO_BIN_EXE_termlens");
    let out = Command::new(bin)
        .args(["render", "--text", "--out=", &data("before.snap")])
        .output()?;
    assert_eq!(out.status.code(), Some(2), "{out:?}");
    let stderr = String::from_utf8_lossy(&out.stderr);
    assert_eq!(stderr, "termlens: --out needs a PATH argument\n");
    Ok(())
}

/// `render --out` exists because every caller was writing `> file.svg`, and
/// a redirect truncates the file before termlens runs โ€” so a failing render
/// leaves an empty file where a bug report expected an image (#313).
#[test]
fn render_out_writes_the_file_and_creates_none_when_the_render_fails() -> termlens::Result<()> {
    use std::process::Command;
    let bin = env!("CARGO_BIN_EXE_termlens");
    let dir = std::env::temp_dir().join(format!("termlens-render-out-{}", std::process::id()));
    std::fs::create_dir_all(&dir)?;

    let written = dir.join("screen.svg");
    let out = Command::new(bin)
        .args([
            "render",
            "--svg",
            "--out",
            written.to_str().expect("utf-8 path"),
            &data("before.snap"),
        ])
        .output()?;
    assert_eq!(out.status.code(), Some(0), "{out:?}");
    assert!(out.stdout.is_empty(), "--out means not stdout: {out:?}");
    let to_stdout = Command::new(bin)
        .args(["render", "--svg", &data("before.snap")])
        .output()?;
    assert_eq!(
        std::fs::read(&written)?,
        to_stdout.stdout,
        "the same bytes stdout would have carried"
    );

    // An unreadable input: exit 2, and nothing where the file would go.
    let junk = dir.join("junk.txt");
    std::fs::write(&junk, "not a saved screen\n")?;
    let missing = dir.join("never-written.svg");
    let out = Command::new(bin)
        .args([
            "render",
            "--svg",
            "--out",
            missing.to_str().expect("utf-8 path"),
            junk.to_str().expect("utf-8 path"),
        ])
        .output()?;
    assert_eq!(out.status.code(), Some(2), "{out:?}");
    assert!(
        !missing.exists(),
        "a failing render left a file behind: {}",
        missing.display()
    );

    let _ = std::fs::remove_dir_all(&dir);
    Ok(())
}

/// `--out -` is stdout, the same stream every other `-` operand already
/// is (#469). `--out` used to take it as a filename, so `render --out -`
/// wrote `./-` and printed nothing โ€” exit 0, and a file most shells make
/// awkward to delete. `./-` is still a file named `-`.
#[test]
fn render_out_dash_writes_to_stdout_and_creates_no_file() -> termlens::Result<()> {
    use std::process::Command;
    let bin = env!("CARGO_BIN_EXE_termlens");
    let dir = std::env::temp_dir().join(format!("termlens-render-out-dash-{}", std::process::id()));
    std::fs::create_dir_all(&dir)?;
    let snap = data("before.snap");
    let dash = dir.join("-");

    let to_stdout = Command::new(bin)
        .args(["render", "--text", &snap])
        .output()?;
    assert_eq!(to_stdout.status.code(), Some(0), "{to_stdout:?}");

    // Both spellings arrive at the same string; the cwd is the temp dir so
    // a regression that writes `./-` is visible here, not in the suite root.
    let spaced = Command::new(bin)
        .current_dir(&dir)
        .args(["render", "--text", "--out", "-", &snap])
        .output()?;
    assert_eq!(spaced.status.code(), Some(0), "{spaced:?}");
    assert_eq!(
        spaced.stdout, to_stdout.stdout,
        "the same bytes stdout would have carried"
    );
    assert!(
        !dash.exists(),
        "--out - created a file named -: {}",
        dash.display()
    );

    let equals = Command::new(bin)
        .current_dir(&dir)
        .args(["render", "--text", "--out=-", &snap])
        .output()?;
    assert_eq!(equals.status.code(), Some(0), "{equals:?}");
    assert_eq!(
        equals.stdout, to_stdout.stdout,
        "--out=- is the same stream"
    );
    assert!(
        !dash.exists(),
        "--out=- created a file named -: {}",
        dash.display()
    );

    let out = Command::new(bin)
        .current_dir(&dir)
        .args(["render", "--text", "--out", "./-", &snap])
        .output()?;
    assert_eq!(out.status.code(), Some(0), "{out:?}");
    assert!(out.stdout.is_empty(), "--out ./- means not stdout: {out:?}");
    assert_eq!(
        std::fs::read(&dash)?,
        to_stdout.stdout,
        "./- is a file named -"
    );

    let _ = std::fs::remove_dir_all(&dir);
    Ok(())
}

/// The operand is the whole input: two of them are ambiguous, and the last
/// silently winning is how a stale path renders a screen nobody named โ€”
/// with `--out` there is nothing on screen to reveal it (#364).
#[test]
fn render_refuses_a_second_operand() -> termlens::Result<()> {
    use std::process::Command;
    let bin = env!("CARGO_BIN_EXE_termlens");
    let dir = std::env::temp_dir().join(format!("termlens-render-operands-{}", std::process::id()));
    std::fs::create_dir_all(&dir)?;
    let a = dir.join("a.snap");
    let b = dir.join("b.snap");
    std::fs::write(&a, "size: 10x1  cursor: 0,0\nhi\n")?;
    std::fs::write(&b, "size: 10x1  cursor: 0,0\nbye\n")?;

    let out = Command::new(bin)
        .args([
            "render",
            "--text",
            a.to_str().expect("utf-8 path"),
            b.to_str().expect("utf-8 path"),
        ])
        .output()?;
    assert_eq!(out.status.code(), Some(2), "{out:?}");
    let stderr = String::from_utf8_lossy(&out.stderr);
    assert!(stderr.contains("usage: termlens render"), "{stderr}");
    assert!(
        !String::from_utf8_lossy(&out.stdout).contains("bye"),
        "the last operand must not win: {out:?}"
    );

    // `--out` is where the bug was invisible, so this is the case that
    // matters: exit 2 and no file, not the wrong screen in the right one.
    let shot = dir.join("shot.svg");
    let out = Command::new(bin)
        .args([
            "render",
            "--svg",
            "--out",
            shot.to_str().expect("utf-8 path"),
            a.to_str().expect("utf-8 path"),
            b.to_str().expect("utf-8 path"),
        ])
        .output()?;
    assert_eq!(out.status.code(), Some(2), "{out:?}");
    assert!(
        !shot.exists(),
        "a refused render left a file behind: {}",
        shot.display()
    );

    // A `-` beside a path is refused before stdin is read: the diagnostic
    // is the usage, not a complaint about what the pipe carried.
    let out = with_stdin(
        &["render", "--text", "-", a.to_str().expect("utf-8 path")],
        "not a saved screen\n",
    );
    assert_eq!(out.status.code(), Some(2), "{out:?}");
    let stderr = String::from_utf8_lossy(&out.stderr);
    assert!(stderr.contains("usage: termlens render"), "{stderr}");
    assert!(!stderr.contains("<stdin>"), "stdin was read: {stderr}");

    // And one operand remains exactly the command it was.
    let out = Command::new(bin)
        .args(["render", "--text", a.to_str().expect("utf-8 path")])
        .output()?;
    assert_eq!(out.status.code(), Some(0), "{out:?}");
    assert!(
        String::from_utf8_lossy(&out.stdout).contains("hi"),
        "{out:?}"
    );

    let _ = std::fs::remove_dir_all(&dir);
    Ok(())
}

/// One format and one `--out` (#450): the flag-position twin of #364. Two
/// formats are a contradiction, not a preference โ€” `--svg --html` asked for
/// an SVG and got HTML at exit 0 โ€” and two `--out`s wrote the last and said
/// nothing about the first. Refused like a second operand, with the
/// conflict named, and before anything is opened.
#[test]
fn render_refuses_a_second_format_or_a_second_out() -> termlens::Result<()> {
    use std::process::Command;
    let bin = env!("CARGO_BIN_EXE_termlens");
    let dir = std::env::temp_dir().join(format!("termlens-render-twice-{}", std::process::id()));
    std::fs::create_dir_all(&dir)?;
    let a = dir.join("a.snap");
    std::fs::write(&a, "size: 10x1  cursor: 0,0\nhi\n")?;
    let a = a.to_str().expect("utf-8 path").to_string();

    // Two formats, different and identical.
    for (first, second, named) in [
        ("--svg", "--html", "--svg and --html are two formats"),
        ("--text", "--json", "--text and --json are two formats"),
        ("--svg", "--svg", "--svg is given twice"),
    ] {
        let out = Command::new(bin)
            .args(["render", first, second, &a])
            .output()?;
        assert_eq!(out.status.code(), Some(2), "{first} {second}: {out:?}");
        let stderr = String::from_utf8_lossy(&out.stderr);
        assert!(
            stderr.contains(named),
            "{first} {second} must name the conflict: {stderr}"
        );
        assert!(stderr.contains("usage: termlens render"), "{stderr}");
        assert!(out.stdout.is_empty(), "nothing is rendered: {out:?}");
    }

    // Two `--out`s, in every mix of the two spellings, write neither file.
    let first = dir.join("first.svg");
    let second = dir.join("second.svg");
    let (f, s) = (first.to_str().unwrap(), second.to_str().unwrap());
    let eq = |p: &str| format!("--out={p}");
    for args in [
        vec!["render", "--svg", "--out", f, "--out", s, &a],
        vec!["render", "--svg", &eq(f), "--out", s, &a],
        vec!["render", "--svg", "--out", f, &eq(s), &a],
        vec!["render", "--svg", &eq(f), &eq(s), &a],
    ] {
        let out = Command::new(bin).args(&args).output()?;
        assert_eq!(out.status.code(), Some(2), "{args:?}: {out:?}");
        let stderr = String::from_utf8_lossy(&out.stderr);
        assert!(
            stderr.contains("--out is given twice"),
            "{args:?}: {stderr}"
        );
        assert!(
            !first.exists() && !second.exists(),
            "a refused render wrote a file: {args:?}"
        );
    }

    // And one of each is exactly the command it was, in both spellings.
    for args in [
        vec!["render", "--svg", "--out", f, &a],
        vec!["render", "--svg", &eq(f), &a],
    ] {
        let _ = std::fs::remove_file(&first);
        let out = Command::new(bin).args(&args).output()?;
        assert_eq!(out.status.code(), Some(0), "{args:?}: {out:?}");
        let svg = std::fs::read_to_string(&first)?;
        assert!(svg.contains("<svg"), "{args:?} wrote: {svg}");
    }
    let _ = std::fs::remove_dir_all(&dir);
    Ok(())
}

/// The working directory is part of "how the program is normally run", and
/// `TerminalBuilder::current_dir` had no way through to the command line
/// (#312).
#[test]
#[cfg_attr(windows, ignore = "the program under inspection is a POSIX shell")]
fn inspect_runs_the_program_where_cwd_says() -> termlens::Result<()> {
    let path = std::env::var("PATH").unwrap_or_default();
    // A directory made here, so the assertion is not about /tmp's own name
    // on a platform that symlinks it (macOS: /tmp -> /private/tmp).
    let dir = std::env::temp_dir().join(format!("termlens-cwd-{}", std::process::id()));
    std::fs::create_dir_all(&dir)?;
    let real = std::fs::canonicalize(&dir)?;
    // Wide enough that the path is one row. At 60 columns this went red on
    // the macOS leg and nowhere else: the temp directory there is
    // `/private/var/folders/36/tjdph2t965j8snz9_vkdnw0r0000gn/T/โ€ฆ`, which
    // wraps, and a wrapped needle is a test about the width rather than
    // about --cwd.
    let mut t = termlens::bin!(
        "termlens",
        env("PATH", &path),
        args([
            "inspect",
            "--size",
            "200x3",
            "--cwd",
            dir.to_str().expect("utf-8 path"),
            "sh",
            "-c",
            "pwd"
        ])
    )?;
    assert_eq!(t.wait_exit()?.code(), Some(0), "{}", t.screen());
    let s = t.screen();
    assert!(
        s.contains(real.to_str().expect("utf-8 path")),
        "the program ran in {}: {s}",
        real.display()
    );
    let _ = std::fs::remove_dir_all(&dir);
    Ok(())
}

/// A directory that is not there is a one-line diagnostic naming the flag,
/// and exit 2 โ€” not a panic, and not the builder's message about a
/// `current_dir` the caller never wrote.
#[test]
fn inspect_refuses_a_cwd_that_is_not_a_directory() -> termlens::Result<()> {
    use std::process::Command;
    let missing = std::env::temp_dir().join("termlens-no-such-directory-here");
    let _ = std::fs::remove_dir_all(&missing);
    let out = Command::new(env!("CARGO_BIN_EXE_termlens"))
        .args([
            "inspect",
            "--cwd",
            missing.to_str().expect("utf-8 path"),
            "true",
        ])
        .output()?;
    assert_eq!(out.status.code(), Some(2), "{out:?}");
    let stderr = String::from_utf8_lossy(&out.stderr);
    assert_eq!(stderr.lines().count(), 1, "one line: {stderr}");
    assert!(stderr.contains("--cwd"), "it names the flag: {stderr}");
    assert!(
        stderr.contains("not an existing directory"),
        "and what was wrong with it: {stderr}"
    );
    Ok(())
}

/// `inspect` exists so a test sees the screen its own program will see, so
/// the child environment starts bare; `--inherit-env` is the explicit
/// opt-in to the caller's (#367). A regression in either direction is
/// silent: the screen still renders, it is simply the wrong one.
#[test]
#[cfg_attr(windows, ignore = "the program under inspection is a POSIX shell")]
fn inspect_clears_the_caller_env_unless_inherit_env_is_passed() -> termlens::Result<()> {
    let path = std::env::var("PATH").unwrap_or_default();
    let mut t = termlens::bin!(
        "termlens",
        env("PATH", &path),
        env("TERMLENS_INSPECT_CALLER", "zzz"),
        args([
            "inspect",
            "--size",
            "60x3",
            "sh",
            "-c",
            "echo \"[$TERMLENS_INSPECT_CALLER]\""
        ])
    )?;
    assert_eq!(t.wait_exit()?.code(), Some(0), "{}", t.screen());
    let s = t.screen();
    assert!(s.contains("[]"), "absent without --inherit-env: {s}");
    assert!(!s.contains("zzz"), "the caller's variable leaked in: {s}");

    let mut t = termlens::bin!(
        "termlens",
        env("PATH", &path),
        env("TERMLENS_INSPECT_CALLER", "zzz"),
        args([
            "inspect",
            "--size",
            "60x3",
            "--inherit-env",
            "sh",
            "-c",
            "echo \"[$TERMLENS_INSPECT_CALLER]\""
        ])
    )?;
    assert_eq!(t.wait_exit()?.code(), Some(0), "{}", t.screen());
    assert!(t.screen().contains("[zzz]"), "{}", t.screen());
    Ok(())
}

/// `--env KEY=VALUE` sets variables in the otherwise bare child
/// environment, and splits on the **first** `=` so a value may contain
/// more of them (#367).
#[test]
#[cfg_attr(windows, ignore = "the program under inspection is a POSIX shell")]
fn inspect_env_sets_a_variable_and_splits_on_the_first_equals() -> termlens::Result<()> {
    let path = std::env::var("PATH").unwrap_or_default();
    let mut t = termlens::bin!(
        "termlens",
        env("PATH", &path),
        args([
            "inspect",
            "--size",
            "60x3",
            "--env",
            "TERMLENS_INSPECT_SET=zzz",
            "sh",
            "-c",
            "echo \"[$TERMLENS_INSPECT_SET]\""
        ])
    )?;
    assert_eq!(t.wait_exit()?.code(), Some(0), "{}", t.screen());
    assert!(t.screen().contains("[zzz]"), "{}", t.screen());

    // The first `=`, not the last: `A=b=c` means `A` is `b=c`.
    let mut t = termlens::bin!(
        "termlens",
        env("PATH", &path),
        args([
            "inspect",
            "--size",
            "60x3",
            "--env",
            "TERMLENS_INSPECT_SPLIT=b=c",
            "sh",
            "-c",
            "echo \"[$TERMLENS_INSPECT_SPLIT]\""
        ])
    )?;
    assert_eq!(t.wait_exit()?.code(), Some(0), "{}", t.screen());
    assert!(t.screen().contains("[b=c]"), "{}", t.screen());

    // The case where the two splits disagree: an empty key is refused, so
    // this cannot pass under both `split_once` and `rsplit_once` the way
    // the `[b=c]` assertion above can โ€” the child sees `A=b=c` either way
    // and the shell splits it itself. This is the assertion that bites.
    let mut t = termlens::bin!(
        "termlens",
        env("PATH", &path),
        args(["inspect", "--size", "60x3", "--env", "=a=b", "sh", "-c", "echo hi"])
    )?;
    assert_eq!(t.wait_exit()?.code(), Some(2), "{}", t.screen());
    assert!(
        t.screen()
            .contains(r#"bad --env "=a=b", expected e.g. NO_COLOR=1"#),
        "an empty key is refused with the flag's own diagnostic: {}",
        t.screen()
    );
    Ok(())
}

/// `--idle` is the settle window that ends the wait, and `--timeout` is its
/// one bound: output that arrives after the window, or after the deadline,
/// is not on the snapshot (#367, #374). A regression here would be someone
/// else's flake, so this test pins both sides of the window.
#[test]
#[cfg_attr(windows, ignore = "the program under inspection is a POSIX shell")]
fn inspect_idle_window_decides_what_the_deadline_snapshot_holds() -> termlens::Result<()> {
    let path = std::env::var("PATH").unwrap_or_default();
    // `first` lands well before the 3s deadline, `second` four seconds in.
    // The two sides: a 300 ms window is satisfied a moment after `first`,
    // ending the wait there with `first` only; a five-second window can
    // never be satisfied inside the deadline, so the deadline ends the wait
    // โ€” still with `first` only, because `second` lands a second after it.
    // Neither side races the 4 s mark that way.
    // The trailing `sleep` keeps the child alive after its last write: a
    // write-then-exit races the platform's PTY teardown and the final bytes
    // can be lost (docs/DESIGN.md).
    let child = "printf first; sleep 4; printf ' second'; sleep 30";
    let run = |idle: &str| {
        termlens::bin!(
            "termlens",
            env("PATH", &path),
            // The harness deadline also covers the spawn (CONTRIBUTING ยง3),
            // so it is generous rather than tight around the CLI's own 3s.
            timeout(std::time::Duration::from_secs(30)),
            args([
                "inspect",
                "--size",
                "60x3",
                "--timeout",
                "3",
                "--idle",
                idle,
                "sh",
                "-c",
                child
            ])
        )
    };

    let mut t = run("300")?;
    assert_eq!(t.wait_exit()?.code(), Some(0), "{}", t.screen());
    let s = t.screen();
    assert!(s.contains("first"), "{s}");
    assert!(
        !s.contains("second"),
        "the gap sits outside the window: {s}"
    );

    let started = std::time::Instant::now();
    let mut t = run("5000")?;
    assert_eq!(t.wait_exit()?.code(), Some(0), "{}", t.screen());
    assert!(
        !t.screen().contains("second"),
        "the deadline ends the wait before the overdue window: {}",
        t.screen()
    );
    // The deadline is the one bound: resolving there, not three seconds then
    // a five-second window on top (which used to hold `second`, four seconds
    // in, and return around eight). The ceiling is generous; the spawn is
    // covered by the harness deadline (CONTRIBUTING ยง3).
    assert!(
        started.elapsed() < std::time::Duration::from_secs(6),
        "returned after {:?}: the deadline did not bound the idle window",
        started.elapsed()
    );
    Ok(())
}

/// `--timeout` is the other half of the same pair: a program that outlives
/// it is snapshotted where it stands and reported as still running, not
/// treated as an error (#367). `second` is what separates a 3s deadline
/// from the 5s default: the default would hold it, the flag must not. The
/// harness deadline is generous because it also covers the spawn
/// (CONTRIBUTING ยง3).
///
/// `--idle 5000` is what keeps this about `--timeout` since #374: the wait
/// now ends on the silence window too, and this child goes quiet the
/// instant it prints `first`, so the default 300ms window would end the
/// wait at 300ms whatever the deadline said โ€” and the test would pass
/// against a `--timeout` that did nothing at all. A window above the
/// deadline can never be satisfied inside it, so the deadline is once again
/// the thing being measured, and the trailer says so.
#[test]
#[cfg_attr(windows, ignore = "the program under inspection is a POSIX shell")]
fn inspect_timeout_snapshots_a_program_that_outlives_it() -> termlens::Result<()> {
    let path = std::env::var("PATH").unwrap_or_default();
    let mut t = termlens::bin!(
        "termlens",
        env("PATH", &path),
        timeout(std::time::Duration::from_secs(30)),
        args([
            "inspect",
            "--size",
            "60x3",
            "--timeout",
            "3",
            "--idle",
            "5000",
            "sh",
            "-c",
            "printf first; sleep 4; printf ' second'; sleep 30"
        ])
    )?;
    assert_eq!(t.wait_exit()?.code(), Some(0), "{}", t.screen());
    let s = t.screen();
    assert!(s.contains("first"), "the pre-deadline output is kept: {s}");
    assert!(
        !s.contains("second"),
        "output past the 3s deadline is not the snapshot: {s}"
    );
    assert!(
        s.contains("--- still running at the deadline (killed on exit) ---"),
        "{s}"
    );
    Ok(())
}

/// The idle window ends the wait, not the deadline (#374). A program that
/// paints once and sleeps is a TUI in miniature: `--idle 200` must end the
/// wait, where the sequential version charged the whole `--timeout` and
/// only then looked at `--idle` โ€” six seconds for a screen complete in a
/// millisecond. The ceiling is generous, and the harness deadline sixty
/// times it, so this goes red only if the wait itself moved; the harness
/// timeout also covers the spawn (CONTRIBUTING ยง3).
#[test]
#[cfg_attr(windows, ignore = "the program under inspection is a POSIX shell")]
fn inspect_resolves_on_the_idle_window_before_the_deadline() -> termlens::Result<()> {
    let path = std::env::var("PATH").unwrap_or_default();
    let started = std::time::Instant::now();
    let mut t = termlens::bin!(
        "termlens",
        timeout(std::time::Duration::from_secs(60)),
        env("PATH", &path),
        args([
            "inspect",
            "--size",
            "20x3",
            "--idle",
            "200",
            "--timeout",
            "30",
            "sh",
            "-c",
            "printf READY; sleep 300"
        ])
    )?;
    assert_eq!(t.wait_exit()?.code(), Some(0), "{}", t.screen());
    let elapsed = started.elapsed();
    let s = t.screen();
    assert!(
        s.contains("READY"),
        "the output before idleness is kept: {s}"
    );
    assert!(
        s.contains("--- still running (killed on exit) ---"),
        "the program never exited, so the trailer says so โ€” and the window \
         ended the wait, so it does not claim the deadline did: {s}"
    );
    assert!(
        elapsed < std::time::Duration::from_secs(10),
        "took {elapsed:?}: the 200ms idle window did not end the wait before the 30s deadline"
    );
    Ok(())
}

/// The other arm of the race (#374): an exit still wins, promptly and with
/// the exited trailer, even when `--idle` is far longer than the deadline โ€”
/// the fix must not turn a program that prints and exits into one held for
/// the silence window.
#[test]
#[cfg_attr(windows, ignore = "the program under inspection is a POSIX shell")]
fn inspect_reports_an_exit_without_waiting_for_the_idle_window() -> termlens::Result<()> {
    let path = std::env::var("PATH").unwrap_or_default();
    let started = std::time::Instant::now();
    let mut t = termlens::bin!(
        "termlens",
        timeout(std::time::Duration::from_secs(30)),
        env("PATH", &path),
        args([
            "inspect",
            "--size",
            "20x3",
            "--idle",
            "30000",
            "--timeout",
            "30",
            "sh",
            "-c",
            "printf 'done here'"
        ])
    )?;
    assert_eq!(t.wait_exit()?.code(), Some(0), "{}", t.screen());
    let elapsed = started.elapsed();
    let s = t.screen();
    assert!(
        s.contains("done here"),
        "the final output is the screen: {s}"
    );
    assert!(s.contains("--- exited: exit code 0 ---"), "{s}");
    assert!(
        elapsed < std::time::Duration::from_secs(10),
        "took {elapsed:?} for a program that exited at once"
    );
    Ok(())
}

/// The deadline is still the bound when the idle window never opens (#374):
/// a program that keeps talking is snapshotted where it stands and reported
/// as still running, and the wait is not cut short by the fix. The 1s
/// deadline is the wait that must expire, and the child keeps it from
/// opening โ€” the outer deadline stays generous all the same.
#[test]
#[cfg_attr(windows, ignore = "the program under inspection is a POSIX shell")]
fn inspect_chatter_waits_for_the_deadline_and_says_still_running() -> termlens::Result<()> {
    let path = std::env::var("PATH").unwrap_or_default();
    let started = std::time::Instant::now();
    let mut t = termlens::bin!(
        "termlens",
        timeout(std::time::Duration::from_secs(30)),
        env("PATH", &path),
        args([
            "inspect",
            "--size",
            "20x3",
            "--idle",
            "500",
            "--timeout",
            "1",
            "sh",
            "-c",
            "while :; do printf x; sleep 0.01; done"
        ])
    )?;
    assert_eq!(t.wait_exit()?.code(), Some(0), "{}", t.screen());
    let elapsed = started.elapsed();
    let s = t.screen();
    assert!(
        s.contains("--- still running at the deadline (killed on exit) ---"),
        "{s}"
    );
    assert!(
        elapsed >= std::time::Duration::from_millis(800),
        "returned after {elapsed:?}: the 1s deadline was not waited"
    );
    Ok(())
}

/// A child that closes its terminal but keeps running is not an exited
/// child: the reap that answers "exited" never comes, so the trailer must
/// say still running (#374). The reap grace a genuinely exited child is
/// given must not mislabel this one.
///
/// On Linux the EOF ends the wait: closing the last descriptor on the
/// terminal is one, at once. On macOS no EOF comes while the child lives โ€”
/// the terminal is still its controlling terminal โ€” and a child that never
/// printed starts no silence window, so the deadline ends it, and says so.
#[test]
#[cfg_attr(windows, ignore = "the program under inspection is a POSIX shell")]
fn inspect_reports_still_running_when_the_child_closes_its_terminal() -> termlens::Result<()> {
    use std::process::Command;
    // The short deadline goes where it must expire, and only there
    // (CONTRIBUTING ยง3).
    let (timeout, trailer) = if cfg!(target_os = "macos") {
        (
            "2",
            "--- still running at the deadline (killed on exit) ---",
        )
    } else {
        ("30", "--- still running (killed on exit) ---")
    };
    let bin = env!("CARGO_BIN_EXE_termlens");
    let started = std::time::Instant::now();
    let out = Command::new(bin)
        .args([
            "inspect",
            "--size",
            "20x3",
            "--timeout",
            timeout,
            "sh",
            "-c",
            "exec 0<&- 1>&- 2>&-; exec sleep 30",
        ])
        .output()?;
    assert!(out.status.success(), "{out:?}");
    let stderr = String::from_utf8(out.stderr).expect("utf-8");
    assert_eq!(stderr.trim_end(), trailer, "{stderr:?}");
    // On Linux it returned on the EOF, not the 30s deadline: the reap grace
    // is the only wait left, and the ceiling is generous for the spawn.
    if !cfg!(target_os = "macos") {
        assert!(
            started.elapsed() < std::time::Duration::from_secs(10),
            "returned after {:?}: the EOF did not end the wait",
            started.elapsed()
        );
    }
    Ok(())
}