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
use std::path::{Path, PathBuf};
use std::process::Command;

/// What activating a link region on a PDF page does.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum LinkTarget {
    /// External target (`https://…`, `mailto:…`) opened with the OS opener.
    Url(String),
    /// Internal link to another page of the same document (1-based).
    Page(u32),
}

/// One link region on a PDF page. `rect` is (left, top, right, bottom) in the
/// coordinate space declared by the owning [`PageLinks`] (top-left origin).
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PdfLink {
    pub rect: (u32, u32, u32, u32),
    pub target: LinkTarget,
}

/// The clickable link regions of one PDF page, in pdftohtml's page space.
/// Kept in integer page units (not fractions) so the type stays `Eq` for the
/// editor's tab-state comparisons; [`Self::link_at`] does the normalising.
#[derive(Clone, Debug, PartialEq, Eq, Default)]
pub struct PageLinks {
    pub page_w: u32,
    pub page_h: u32,
    pub links: Vec<PdfLink>,
}

impl PageLinks {
    /// The link whose region intersects the page-fraction rect
    /// `(x0, y0, x1, y1)` (0..1 on both axes, top-left origin). The caller
    /// passes the whole area a terminal cell covers, so a click needs no
    /// pixel-perfect aim on the (thin) text-line rects poppler reports.
    /// Adjacent TOC lines produce rects that overlap each other and one
    /// coarse cell can straddle two of them, so among several hits the link
    /// covering the largest share of the cell wins - not the first one in
    /// document order.
    pub fn link_at(&self, frac: (f64, f64, f64, f64)) -> Option<&PdfLink> {
        if self.page_w == 0 || self.page_h == 0 {
            return None;
        }
        let (w, h) = (self.page_w as f64, self.page_h as f64);
        self.links
            .iter()
            .filter_map(|l| {
                let (lx0, ly0, lx1, ly1) = l.rect;
                let ox = (frac.2.min(lx1 as f64 / w) - frac.0.max(lx0 as f64 / w)).max(0.0);
                let oy = (frac.3.min(ly1 as f64 / h) - frac.1.max(ly0 as f64 / h)).max(0.0);
                (ox > 0.0 && oy > 0.0).then_some((l, ox * oy))
            })
            .max_by(|a, b| a.1.total_cmp(&b.1))
            .map(|(l, _)| l)
    }
}

/// Extract the link regions of `page` (1-based) via `pdftohtml -xml`, which
/// reports each text run a link annotation covers as `<text top= left= …>
/// <a href=…>`. Text-anchored only: a link drawn over a bare image has no
/// text run and is not reported — the common case (hyperref/beamer URLs)
/// is always text.
pub fn page_links(pdf: &Path, page: u32) -> std::io::Result<PageLinks> {
    let program = which("pdftohtml")
        .ok_or_else(|| std::io::Error::other("install poppler (pdftohtml) to open PDF links"))?;
    let p = page.to_string();
    // Bounded: this runs on the UI thread from a click, and a wedged
    // pdftohtml (a file pdflatex is still writing) would otherwise freeze
    // the editor for as long as it hangs. The runner also keeps the child
    // off croft's TTY.
    let flags = ["-xml", "-stdout", "-i", "-f", &p, "-l", &p];
    let args: Vec<&std::ffi::OsStr> = flags
        .iter()
        .map(std::ffi::OsStr::new)
        .chain(std::iter::once(pdf.as_os_str()))
        .collect();
    let (status, out) = run_bounded_stdout(&program, &args, PDF_LINKS_BUDGET)
        .ok_or_else(|| std::io::Error::other("pdftohtml did not finish in time"))?;
    if !status.success() {
        return Err(std::io::Error::other(format!(
            "pdftohtml exited with {status}"
        )));
    }
    Ok(parse_pdf2xml_links(&out, page).unwrap_or_default())
}

/// Parse pdftohtml's `-xml` output, returning the links of `page` (matched
/// against the `<page number=…>` attribute — poppler keeps original numbers
/// when a `-f`/`-l` range is used). None when that page is absent.
pub fn parse_pdf2xml_links(xml: &str, page: u32) -> Option<PageLinks> {
    let mut in_page = false;
    let mut out = PageLinks::default();
    let mut found = false;
    // pdftohtml emits one element per line, but poppler also chats onto
    // stdout (a GoTo annotation prints " link to page N" glued to the
    // start of the <page> line), so elements are located with find(), never
    // by line prefix.
    for line in xml.lines() {
        if let Some(pos) = line.find("<page ") {
            let tag = &line[pos + "<page ".len()..];
            in_page = attr_u32(tag, "number") == Some(page);
            if in_page {
                found = true;
                out.page_w = attr_u32(tag, "width").unwrap_or(0);
                out.page_h = attr_u32(tag, "height").unwrap_or(0);
            }
            continue;
        }
        if !in_page {
            continue;
        }
        if line.contains("</page>") {
            break;
        }
        let Some(pos) = line.find("<text ") else {
            continue;
        };
        let tag = &line[pos + "<text ".len()..];
        let (Some(left), Some(top), Some(w), Some(h)) = (
            attr_u32(tag, "left"),
            attr_u32(tag, "top"),
            attr_u32(tag, "width"),
            attr_u32(tag, "height"),
        ) else {
            continue;
        };
        // poppler often wraps only part of the run in the <a> (") is another "
        // inside a longer sentence), and pdf2xml carries no per-<a> geometry.
        // Each link therefore gets the proportional horizontal slice of the
        // run rect its own characters cover, so the words next to a link do
        // not become clickable with it.
        let Some(gt) = tag.find('>') else { continue };
        let content = tag[gt + 1..].split("</text>").next().unwrap_or("");
        let total = visible_chars(content);
        let mut pre = 0usize;
        let mut rest = content;
        while let Some(pos) = rest.find("<a href=\"") {
            pre += visible_chars(&rest[..pos]);
            let after = &rest[pos + "<a href=\"".len()..];
            let Some(end) = after.find('"') else { break };
            let href = xml_unescape(&after[..end]);
            let Some(tag_close) = after[end..].find('>') else {
                break;
            };
            let (inner, after_anchor) = after[end + tag_close + 1..]
                .split_once("</a>")
                .unwrap_or((&after[end + tag_close + 1..], ""));
            let inner_len = visible_chars(inner);
            if let Some(target) = classify_href(&href) {
                // Extents clamp to at least one page unit: the hit test
                // demands positive overlap area, so a zero-width or
                // zero-height run would otherwise be unclickable forever.
                let (x0, x1) = if total == 0 {
                    (left, left + w.max(1))
                } else {
                    let at = |chars: usize| {
                        left + (w as f64 * chars.min(total) as f64 / total as f64).round() as u32
                    };
                    // The forced one-unit width of an empty anchor is carved
                    // out of the run (x0 shifts left at the right edge), so
                    // no slice ever extends past the run into its neighbour.
                    let run_right = left + w.max(1);
                    let x0 = at(pre).min(run_right - 1);
                    (x0, at(pre + inner_len).clamp(x0 + 1, run_right))
                };
                out.links.push(PdfLink {
                    rect: (x0, top, x1, top + h.max(1)),
                    target,
                });
            }
            pre += inner_len;
            rest = after_anchor;
        }
    }
    found.then_some(out)
}

/// Visible character count of a pdf2xml text-run fragment: markup tags
/// contribute nothing and an escaped entity counts as the one character it
/// stands for. Only a well-formed entity (`&` + a short alphanumeric/`#`
/// name + `;`) collapses; a bare `&` counts as itself - swallowing to some
/// distant `;` (or the end) would make the counts on the two sides of an
/// anchor disagree and push a link's slice past its own run rect.
fn visible_chars(s: &str) -> usize {
    let mut n = 0;
    let mut in_tag = false;
    let mut it = s.char_indices();
    while let Some((i, c)) = it.next() {
        match c {
            '<' => in_tag = true,
            '>' => in_tag = false,
            '&' if !in_tag => {
                let rest = &s[i + 1..];
                if let Some(semi) = rest.find(';')
                    && (1..=8).contains(&semi)
                    && rest[..semi]
                        .chars()
                        .all(|c| c == '#' || c.is_ascii_alphanumeric())
                {
                    // The name is ASCII (just checked), so chars == bytes.
                    for _ in 0..=semi {
                        it.next();
                    }
                }
                n += 1;
            }
            _ if !in_tag => n += 1,
            _ => {}
        }
    }
    n
}

/// `name="value"` attribute lookup on a single tag line. Values poppler emits
/// here are plain integers.
fn attr_u32(tag: &str, name: &str) -> Option<u32> {
    let pat = format!("{name}=\"");
    let start = tag.find(&pat)? + pat.len();
    let rest = &tag[start..];
    rest[..rest.find('"')?].parse().ok()
}

/// External hrefs open through the OS - but a document-supplied URI is
/// untrusted input, so only web and mail schemes qualify (`file://` or a
/// registered custom scheme would be a one-click app launch from a click on
/// what looks like plain text). Internal page links come out as
/// `docname.html#page`. Anything else (a relative file link) is dropped.
fn classify_href(href: &str) -> Option<LinkTarget> {
    let lower = href.to_ascii_lowercase();
    if lower.starts_with("http://") || lower.starts_with("https://") || lower.starts_with("mailto:")
    {
        return Some(LinkTarget::Url(href.to_string()));
    }
    if lower.contains("://") {
        return None;
    }
    let (_, frag) = href.rsplit_once('#')?;
    frag.parse().ok().map(LinkTarget::Page)
}

/// The five entities pdftohtml escapes in attribute values.
fn xml_unescape(s: &str) -> String {
    s.replace("&lt;", "<")
        .replace("&gt;", ">")
        .replace("&quot;", "\"")
        .replace("&#39;", "'")
        .replace("&apos;", "'")
        .replace("&amp;", "&")
}

/// Rasteriser preference order:
/// 1. `pdftoppm` (poppler) — supports page selection, cross-platform when
///    poppler-utils is installed (`brew install poppler`, `apt install
///    poppler-utils`).
/// 2. `sips` — macOS built-in. Page 1 only; used as a fallback so the
///    feature works out of the box on every Mac without external deps.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum PdfBackend {
    PdftoppmCli,
    SipsCli,
}

pub fn detect_backend() -> Option<PdfBackend> {
    if which("pdftoppm").is_some() {
        return Some(PdfBackend::PdftoppmCli);
    }
    if cfg!(target_os = "macos") && which("sips").is_some() {
        return Some(PdfBackend::SipsCli);
    }
    None
}

fn which(cmd: &str) -> Option<PathBuf> {
    let path_var = std::env::var_os("PATH")?;
    for dir in std::env::split_paths(&path_var) {
        let candidate = dir.join(cmd);
        if candidate.is_file() {
            return Some(candidate);
        }
    }
    None
}

/// Total pages in `pdf`. Returns None when no detector is available so the
/// caller renders page 1 with no navigation rather than refusing the file.
// Bounded (#493). These run on the frame-loop open path, just before the
// render that `run_pdftoppm` already bounds, so an unbounded one parks the
// editor exactly as a hung renderer would - one call earlier. `croft view`
// makes that reachable from any pane.
//
// The budget is separate from and much smaller than the render's: reading a
// page count is a header parse, not a rasterisation, so a `pdfinfo` still
// running after this long is wedged rather than slow. Losing the count
// degrades to the single-page path; it does not fail the open.
pub fn detect_page_count(pdf: &Path) -> Option<u32> {
    if let Some(n) = page_count_via_pdfinfo(pdf) {
        return Some(n);
    }
    if cfg!(target_os = "macos")
        && let Some(n) = page_count_via_mdls(pdf)
    {
        return Some(n);
    }
    None
}

fn page_count_via_pdfinfo(pdf: &Path) -> Option<u32> {
    let program = which("pdfinfo")?;
    let (status, out) = run_bounded_stdout(&program, &[pdf.as_os_str()], PDF_INFO_BUDGET)?;
    // The guard the unbounded version had: stdout from a failed run is not a
    // page count, and every other subprocess in this file checks the same.
    if !status.success() {
        return None;
    }
    parse_pdfinfo_pages(&out)
}

pub fn parse_pdfinfo_pages(out: &str) -> Option<u32> {
    for line in out.lines() {
        if let Some(rest) = line.strip_prefix("Pages:")
            && let Ok(n) = rest.trim().parse::<u32>()
        {
            return Some(n);
        }
    }
    None
}

/// The argv for the `mdls` page-count query, split out so it can be asserted
/// without spawning anything. A reordering or a misspelt attribute name is
/// otherwise invisible: `mdls` may be absent on the machine running the
/// tests, and a test that silently no-ops when its subject is missing is not
/// a test.
fn mdls_argv(pdf: &Path) -> [&std::ffi::OsStr; 4] {
    [
        std::ffi::OsStr::new("-raw"),
        std::ffi::OsStr::new("-name"),
        std::ffi::OsStr::new("kMDItemNumberOfPages"),
        pdf.as_os_str(),
    ]
}

fn page_count_via_mdls(pdf: &Path) -> Option<u32> {
    let (status, out) = run_bounded_stdout(
        std::path::Path::new("mdls"),
        &mdls_argv(pdf),
        PDF_INFO_BUDGET,
    )?;
    // mdls exits 0 and prints "(null)" for a file with no page count, and
    // exits non-zero on a missing one. Both fail the parse below today, but
    // the guard is what keeps that true.
    if !status.success() {
        return None;
    }
    out.trim().parse::<u32>().ok()
}

/// Run `program` with a deadline, returning its exit status and stdout, or
/// `None` on timeout or spawn failure (#493).
///
/// `dap/install.rs` has a `run_bounded` of the same shape. They are NOT
/// merged, and the reason is the exit status: that one discards it, which is
/// right for its caller (a version probe where any output at all is the
/// answer) and wrong here, because both page-count probes must reject stdout
/// from a failed run - `mdls` exits non-zero on a missing file and prints to
/// a stderr this deliberately drops. This one also caps the read and recovers
/// lossily from bad bytes. Folding them together would mean pushing this
/// caller's requirements onto a function that does not want them; if a third
/// caller wants THIS contract, that is the point to lift it into a shared
/// module.
///
/// The reader runs on its own thread and is never joined: a descendant the
/// child left behind keeps the pipe's write end open, and a join would then
/// wait on that descendant past the budget - on the success path too. This is
/// the same reasoning `run_pdftoppm` records for its stderr drain.
///
/// No process group, unlike `run_pdftoppm`, which kills `-pid` so a forked
/// descendant dies with it. Neither probe forks: `pdfinfo` is a single
/// poppler process, and `mdls` queries `mdworker` over Mach IPC rather than
/// spawning. A caller whose program does fork wants the group treatment
/// instead.
fn run_bounded_stdout(
    program: &Path,
    args: &[&std::ffi::OsStr],
    budget: std::time::Duration,
) -> Option<(std::process::ExitStatus, String)> {
    use std::io::Read;
    use std::process::Stdio;
    let mut child = Command::new(program)
        .args(args)
        .stdin(Stdio::null())
        .stdout(Stdio::piped())
        // Null, never inherited: the child must not touch croft's TTY.
        .stderr(Stdio::null())
        .spawn()
        .ok()?;
    let Some(mut stdout) = child.stdout.take() else {
        // Unreachable while stdout is piped, but returning through `?` here
        // would leave the child unreaped.
        let _ = child.kill();
        let _ = child.wait();
        return None;
    };
    let (tx, rx) = std::sync::mpsc::channel();
    std::thread::spawn(move || {
        // Bytes, not a String: `read_to_string` fails outright on one bad
        // byte, losing a `Pages:` line that is itself valid. A title in
        // another encoding echoed into pdfinfo's header block does that.
        // Capped, because a program printing without end would otherwise
        // fill memory until the budget fires.
        let mut buf = Vec::new();
        let _ = stdout.by_ref().take(MAX_PROBE_STDOUT).read_to_end(&mut buf);
        let _ = tx.send(buf);
    });
    let started = std::time::Instant::now();
    let out = rx.recv_timeout(budget).ok();
    // Stdout reaching EOF does not mean the child has exited: it may still
    // be tearing down, and killing it then reports SIGKILL for a run that
    // succeeded. So it gets the rest of the budget to exit on its own.
    if out.is_some() {
        while started.elapsed() < budget {
            match child.try_wait() {
                Ok(Some(status)) => {
                    return out.map(|b| (status, String::from_utf8_lossy(&b).into_owned()));
                }
                Ok(None) => std::thread::sleep(EXIT_POLL_TICK),
                Err(_) => break,
            }
        }
    }
    let _ = child.kill();
    // Reap either way: on the timeout path the kill needs collecting, and on
    // the success path the child has exited but is still a zombie.
    let status = child.wait().ok()?;
    out.map(|bytes| (status, String::from_utf8_lossy(&bytes).into_owned()))
}

/// How long one page render may take before the renderer is killed and the
/// open reports a failure instead of parking the frame loop (#493).
const PDF_RENDER_BUDGET: std::time::Duration = std::time::Duration::from_secs(10);

/// Deadline for the page-count probes (#493). Far below `PDF_RENDER_BUDGET`
/// on purpose: `pdfinfo` and `mdls` parse a header rather than rasterise, so
/// one still running after two seconds is wedged, not slow. Overrunning costs
/// only the page count, and the viewer degrades to the single-page path.
const PDF_INFO_BUDGET: std::time::Duration = std::time::Duration::from_secs(2);

/// Deadline for one page's link extraction. pdftohtml parses a single page
/// without rasterising, so this sits between the probe and render budgets.
const PDF_LINKS_BUDGET: std::time::Duration = std::time::Duration::from_secs(5);

/// Cap on a page-count probe's stdout. `pdfinfo` prints a header block of a
/// few hundred bytes and `mdls -raw` prints one number, so this is orders
/// above either; it exists so a program that prints without end cannot fill
/// memory while the budget runs down.
const MAX_PROBE_STDOUT: u64 = 1 << 20;

/// How long a failed render's stderr is given to finish arriving after the
/// exit was observed (#493). The settle poll reads it once it has been
/// quiet for ~60 ms of ticks (≈85 ms of wall clock on a quiet machine, more
/// under load, since each tick's sleep overshoots),
/// so this is sized to leave that early exit real headroom rather than
/// being the exit itself; the worst case, a renderer that keeps writing,
/// pays the whole grace once, on a path that has already failed.
const STDERR_SETTLE_GRACE: std::time::Duration = std::time::Duration::from_millis(200);

/// How the settle poll ended, recorded under `cfg(test)` only (#548).
///
/// This is what diagnosed #548, and it is kept for the next occurrence. Which
/// exit the poll took is the whole diagnosis, and the old failure message
/// could not say:
///
/// - ended on the QUIET RUN holding less than expected: the window closed
///   while the writer was still going. Measured under load at 69 ms against
///   the 60 ms run, holding only the first of two writes;
/// - ended on the DEADLINE: the grace ran out with the buffer still growing,
///   which is a different bug and a different fix.
///
/// Guessing between those two is what this exists to stop - and it earned its
/// keep, since two plausible diagnoses were wrong before the numbers arrived.
#[cfg(test)]
#[derive(Debug, Clone, Copy)]
struct SettleOutcome {
    waited: std::time::Duration,
    on_quiet: bool,
    bytes: usize,
}

#[cfg(test)]
thread_local! {
    /// Per THREAD, so tests running in parallel cannot read each other's.
    static LAST_SETTLE: std::cell::Cell<Option<SettleOutcome>> =
        const { std::cell::Cell::new(None) };
}

/// One poll of the renderer's stderr buffer.
const SETTLE_TICK: std::time::Duration = std::time::Duration::from_millis(5);

/// How often the wait loop checks for the renderer's exit and drains its
/// stderr. Five milliseconds rather than the thirty it polled before: the
/// same loop now empties the pipe, so the gap between drains is what a very
/// chatty renderer would have to fill to block, and 64 KB in 5 ms is an
/// order of magnitude beyond anything a page render produces.
const EXIT_POLL_TICK: std::time::Duration = std::time::Duration::from_millis(5);

/// `O_NONBLOCK` onto one descriptor, reporting whether it is set now.
///
/// This is the whole of `set_nonblocking`'s unix arm, which does nothing but
/// hand it a descriptor. It exists as a seam because provoking the fcntl
/// failure through a live `ChildStderr` means closing the fd under the
/// handle, which trips Rust's IO-safety check and aborts the process instead
/// of failing an assertion - so the test drives the failure path here and the
/// success path through `set_nonblocking`, and between them every line of
/// both is executed.
#[cfg(unix)]
fn set_nonblocking_on(fd: std::os::fd::RawFd) -> bool {
    // `EINTR` is a signal arriving mid-call, not a verdict about the
    // descriptor: retrying is the documented response, and treating it as
    // permanent would disable the in-loop drain over a transient the next
    // call resolves - reintroducing the pipe-full stall this change removes.
    // Bounded rather than unbounded, so a pathological signal storm cannot
    // spin here instead of getting on with the render.
    fn retry_on_eintr(mut call: impl FnMut() -> libc::c_int) -> libc::c_int {
        for _ in 0..4 {
            let rc = call();
            if rc >= 0 || std::io::Error::last_os_error().raw_os_error() != Some(libc::EINTR) {
                return rc;
            }
        }
        -1
    }

    // SAFETY: both calls only read and set the status flags of `fd`, and
    // neither takes ownership of it. An invalid `fd` is reported as EBADF
    // rather than being undefined.
    unsafe {
        let flags = retry_on_eintr(|| libc::fcntl(fd, libc::F_GETFL));
        if flags < 0 {
            return false;
        }
        retry_on_eintr(|| libc::fcntl(fd, libc::F_SETFL, flags | libc::O_NONBLOCK)) >= 0
    }
}

/// Put the pipe in non-blocking mode so a read returns what is there rather
/// than waiting for more (#553).
///
/// Reports whether the pipe is actually non-blocking now, because the answer
/// decides whether the caller may read BEFORE the child is reaped. A blocking
/// read there waits on a renderer that is still running, and the budget and
/// the kill path both sit after it - so a failed `fcntl` would reinstate the
/// exact hang this change removes. Callers must not drain a pipe this
/// returned `false` for until the child has exited.
///
/// With no pipe at all the answer is `true`: there is nothing to block on,
/// and every drain returns at once on `None`.
///
/// Off unix the fd type does not exist, so no pipe is ever made non-blocking
/// and a real pipe always answers `false` - the caller then keeps to the
/// single after-exit read.
fn set_nonblocking(pipe: Option<&std::process::ChildStderr>) -> bool {
    #[cfg(unix)]
    {
        let Some(pipe) = pipe else {
            // No pipe is vacuously safe to drain: `drain_available` returns
            // at once on `None`, so it can never block.
            return true;
        };
        use std::os::fd::AsRawFd;
        set_nonblocking_on(pipe.as_raw_fd())
    }
    #[cfg(not(unix))]
    {
        // No fd type, so nothing is made non-blocking and a real pipe must
        // answer `false`. The caller then skips the in-loop drain, which
        // trades one hazard for a smaller one: a renderer writing more than a
        // pipeful before exiting can stall on a full buffer, where the unix
        // path would have drained it. The alternative is worse - a blocking
        // read BEFORE `try_wait` waits on a live renderer and takes the
        // budget and the kill path down with it, so the stall would be
        // unbounded rather than bounded by the budget.
        //
        // Reachable only on a non-unix target, and croft has none: there is
        // no Windows job in ci.yml and no windows dependency in Cargo.toml,
        // and the PDF path documents WSL2 - itself unix - as the Windows
        // story. Worth fixing with an overlapped/thread reader if that ever
        // changes; not worth a reader thread on every platform today.
        pipe.is_none()
    }
}

/// Append everything the pipe has RIGHT NOW, and stop.
///
/// The point of the whole change (#553): after the renderer exits, whatever
/// it wrote is sitting in the pipe, and this takes all of it without waiting
/// on a clock or a thread. `WouldBlock` means "nothing more available", not
/// "nothing more coming", which is exactly the distinction a blocking read
/// cannot make and a descendant holding the write end would otherwise hide.
fn drain_available(pipe: Option<&mut std::process::ChildStderr>, into: &mut Vec<u8>) {
    let Some(pipe) = pipe else {
        return;
    };
    use std::io::Read;
    let mut chunk = [0u8; 4096];
    loop {
        match pipe.read(&mut chunk) {
            Ok(0) => break,
            Ok(n) => into.extend_from_slice(&chunk[..n]),
            Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => break,
            Err(e) if e.kind() == std::io::ErrorKind::Interrupted => continue,
            Err(_) => break,
        }
    }
}

/// ONE read, for a pipe that could not be made non-blocking.
///
/// `drain_available` reads until `WouldBlock`, which a blocking pipe never
/// reports - so on that pipe it is a loop with no exit until every holder of
/// the write end is gone. A single read has no such problem once the child is
/// reaped: what the child wrote is buffered and comes back immediately. Only
/// a SECOND read would wait on a surviving descendant, so there isn't one.
///
/// The cost is a message truncated at one buffer rather than a hang, and a
/// renderer that wrote nothing before dying still blocks here until its
/// descendants exit - which is why the caller only reaches this after
/// `try_wait` has already returned.
fn read_once(pipe: Option<&mut std::process::ChildStderr>, into: &mut Vec<u8>) {
    let Some(pipe) = pipe else {
        return;
    };
    use std::io::Read;
    let mut chunk = [0u8; 4096];
    if let Ok(n) = pipe.read(&mut chunk) {
        into.extend_from_slice(&chunk[..n]);
    }
}

/// How many quiet ticks mean the renderer has stopped writing: twelve, 60 ms,
/// which bridges the gaps measured between a renderer's own writes.
///
/// Pinned by `the_settle_rule_bridges_a_gap_shorter_than_the_quiet_run` with
/// no clock involved, so cutting it fails a test rather than only showing up
/// as half an error message on a loaded machine.
const QUIET_RUN_TICKS: u32 = 12;

#[cfg(test)]
fn record_settle(outcome: SettleOutcome) {
    LAST_SETTLE.with(|c| c.set(Some(outcome)));
}

/// How the settle poll on this thread last ended, for a failure message.
/// `unix` too, because the only consumer is a `unix`-gated test.
#[cfg(all(test, unix))]
fn last_settle() -> String {
    match LAST_SETTLE.with(std::cell::Cell::get) {
        Some(o) => format!(
            "settle ended on {} after {:?} holding {} byte(s)",
            if o.on_quiet {
                "the QUIET RUN"
            } else {
                "the DEADLINE"
            },
            o.waited,
            o.bytes
        ),
        None => String::from("settle did not run"),
    }
}

/// Test-only: the next `rasterize_page` call for exactly this (path, page)
/// fails once, simulating a transient rasteriser failure (a spawn refused
/// under load, an OOM-killed child). Keyed on the full path so parallel
/// tests rendering the same page number of their own tempdir files never
/// see each other's injection.
#[cfg(test)]
pub static FAIL_RASTERIZE_ONCE_FOR_TEST: std::sync::Mutex<Option<(PathBuf, u32)>> =
    std::sync::Mutex::new(None);

/// Rasterise `page` (1-based) of `pdf` to PNG bytes at ~144 DPI. Returns
/// the encoded PNG. The caller is responsible for cleaning up no temp
/// files — every backend writes to a per-call temp path that we delete
/// before returning.
pub fn rasterize_page(pdf: &Path, page: u32, backend: PdfBackend) -> std::io::Result<Vec<u8>> {
    #[cfg(test)]
    {
        let mut fail = FAIL_RASTERIZE_ONCE_FOR_TEST.lock().unwrap();
        if fail.as_ref() == Some(&(pdf.to_path_buf(), page)) {
            *fail = None;
            return Err(std::io::Error::other("transient failure injected by test"));
        }
    }
    match backend {
        PdfBackend::PdftoppmCli => rasterize_with_pdftoppm(pdf, page),
        PdfBackend::SipsCli => {
            if page != 1 {
                return Err(std::io::Error::other("sips backend can only render page 1"));
            }
            rasterize_with_sips(pdf)
        }
    }
}

fn rasterize_with_pdftoppm(pdf: &Path, page: u32) -> std::io::Result<Vec<u8>> {
    run_pdftoppm(Path::new("pdftoppm"), pdf, page, PDF_RENDER_BUDGET)
}

/// Render `page` of `pdf` with the pdftoppm at `program` (a parameter so a
/// test can hand in a script without touching PATH), within `budget`.
///
/// Bounded (#493): this runs on the frame loop, so a renderer that hangs on
/// a malformed document or a stalled filesystem must not park the editor.
/// The child is spawned in its own process group and polled, and at the
/// deadline the whole group is killed and the child reaped, as the video
/// poster's ffmpeg run is bounded; `.output()` would block for as long as
/// the child cared to run. Stderr is drained on its own thread into a
/// shared buffer that is never joined on: a descendant the child left
/// behind (a helper it forked) keeps the pipe's write end open, and a join
/// would wait on that descendant past the budget, on the success path too.
/// The timeout error carries no stderr on purpose: a hung renderer's partial
/// spray is rarely the reason it hung, and settling it would add to an
/// overrun the user is already waiting through.
fn run_pdftoppm(
    program: &Path,
    pdf: &Path,
    page: u32,
    budget: std::time::Duration,
) -> std::io::Result<Vec<u8>> {
    use std::process::Stdio;
    let dir = unique_temp_dir("croft-pdf")?;
    let guard = TempDirGuard(dir.clone());
    let prefix = dir.join("page");
    // Null and piped, never inherited: the child must not touch croft's TTY.
    // A half-written PDF (pdflatex rewriting the open file) made pdftoppm
    // spray "Syntax Error: Couldn't find trailer dictionary" over the UI.
    let mut cmd = Command::new(program);
    cmd.arg("-f")
        .arg(page.to_string())
        .arg("-l")
        .arg(page.to_string())
        .arg("-r")
        .arg("144")
        .args(["-png", "-singlefile"])
        .arg(pdf)
        .arg(&prefix)
        .stdin(Stdio::null())
        .stdout(Stdio::null())
        .stderr(Stdio::piped());
    #[cfg(unix)]
    {
        use std::os::unix::process::CommandExt;
        // Its own group, so the kill at the deadline reaches a descendant
        // the renderer forked and not only the renderer itself.
        cmd.process_group(0);
    }
    let mut child = cmd.spawn()?;
    let mut stderr_pipe = child.stderr.take();
    // Non-blocking, and read from ONE place: this thread (#553).
    //
    // A reader thread published chunk by chunk, which was right about the
    // problem - the pipe only reaches end of file once every holder of its
    // write end has gone, and a descendant the renderer forked can hold it
    // long after the renderer itself exited - but it made the answer depend
    // on that thread being SCHEDULED. Measured on a loaded machine, the
    // renderer's second write was still in the pipe when the poll gave up,
    // and the user got half the error.
    //
    // Non-blocking removes the question. Everything the renderer itself
    // wrote is in the pipe by the time it exits, so a drain-until-EAGAIN
    // after the exit takes all of it, with no window to lose and nothing to
    // schedule.
    // Whether the in-loop drain below is safe at all. If the pipe could not
    // be made non-blocking, a read here waits on a live renderer and the
    // budget check and kill path never run - worse than the buffer pressure
    // the in-loop drain exists to relieve, so it is skipped rather than
    // risked. The after-exit drain is unaffected: the child is gone by then.
    let nonblocking = set_nonblocking(stderr_pipe.as_ref());
    let mut stderr_bytes: Vec<u8> = Vec::new();
    let deadline = std::time::Instant::now() + budget;
    let status = loop {
        // Drained every tick as well, so a renderer spraying more than a
        // pipeful cannot block on a full buffer waiting for us.
        if nonblocking {
            drain_available(stderr_pipe.as_mut(), &mut stderr_bytes);
        }
        match child.try_wait() {
            Ok(Some(status)) => break status,
            Ok(None) if std::time::Instant::now() < deadline => {
                std::thread::sleep(EXIT_POLL_TICK);
            }
            Ok(None) => {
                kill_group(child, guard);
                return Err(std::io::Error::new(
                    std::io::ErrorKind::TimedOut,
                    format!(
                        "{} took longer than {:.1} s rendering page {page}; killed",
                        program.display(),
                        budget.as_secs_f64()
                    ),
                ));
            }
            Err(e) => {
                kill_group(child, guard);
                return Err(e);
            }
        }
    };
    if !status.success() {
        // Everything the renderer itself wrote is readable NOW - it exited,
        // so it has written all it will. Take that first, before any timing
        // question is asked (#553).
        //
        // ...but only on a pipe that reads without blocking. Reaping the
        // child does not close the write end if a DESCENDANT still holds it,
        // and on a blocking pipe a read then waits on that descendant rather
        // than returning EOF.
        //
        // Measured on a blocking pipe whose grandchild outlives the child:
        // the FIRST read after the reap returns what the child wrote, and the
        // SECOND blocks until the grandchild exits, because end of file waits
        // for every holder of the write end. So one read is safe here and a
        // loop is not - which is exactly the asymmetry `drain_available`
        // cannot exploit, since it reads until EAGAIN and EAGAIN never comes.
        //
        // Hence one bounded read on the blocking path: the renderer's own
        // message is what the user needs, and skipping the read entirely
        // would report the exit status with no text at all.
        let text = if nonblocking {
            drain_available(stderr_pipe.as_mut(), &mut stderr_bytes);
            settle_late_writes(stderr_pipe.as_mut(), &mut stderr_bytes, STDERR_SETTLE_GRACE)
        } else {
            read_once(stderr_pipe.as_mut(), &mut stderr_bytes);
            String::from_utf8_lossy(&stderr_bytes).trim().to_string()
        };
        return Err(std::io::Error::other(format!(
            "{} exited with {status}: {text}",
            program.display()
        )));
    }
    let png_path = prefix.with_extension("png");
    std::fs::read(&png_path)
}

/// The renderer's last words, once its own output has already been taken.
///
/// By the time this runs, `drain_available` has emptied the pipe of
/// everything the renderer wrote, so this is only about a DESCENDANT that
/// outlived it and may still write - the case the pipe cannot signal, because
/// end of file waits for every holder of the write end (#553).
///
/// So the grace here is genuinely optional generosity rather than the thing
/// standing between the user and half an error message. It drains on each
/// tick and settles once the bytes stop arriving for a run of them, or once
/// the grace is spent. An empty buffer counts as quiet, so a renderer that
/// said nothing settles immediately.
fn settle_late_writes(
    mut pipe: Option<&mut std::process::ChildStderr>,
    into: &mut Vec<u8>,
    grace: std::time::Duration,
) -> String {
    let started = std::time::Instant::now();
    let deadline = started + grace;
    let (text, on_quiet) = settle_over(QUIET_RUN_TICKS, |_| {
        drain_available(pipe.as_deref_mut(), into);
        let text = String::from_utf8_lossy(into).trim().to_string();
        if std::time::Instant::now() >= deadline {
            return Sample::Last(text);
        }
        std::thread::sleep(SETTLE_TICK);
        Sample::More(text)
    });
    #[cfg(test)]
    record_settle(SettleOutcome {
        waited: started.elapsed(),
        on_quiet,
        bytes: text.len(),
    });
    // Read only under `cfg(test)`, by the recording above.
    let _ = on_quiet;
    text
}

/// What the poll saw on one tick, and whether another tick is allowed.
enum Sample {
    /// This reading, and there is time for another.
    More(String),
    /// This reading, and the budget is spent - stop whatever it says.
    Last(String),
}

/// The settle decision, driven by TICKS rather than by a clock (#548).
///
/// Extracted so the rule can be tested without a wall clock: the caller
/// supplies the readings and says when the budget is spent, and this decides
/// when the buffer has stopped growing. Returns the text it settled on, and
/// whether it settled because the buffer went quiet (rather than because the
/// budget ran out).
///
/// The clock lived inside this loop before, which meant the ONLY way to test
/// the rule was to race a real one - and the test that did so was the flake
/// this issue is about. Worse, the wall-clock test could not pin
/// `QUIET_RUN_TICKS` at all: the run was silently clamped to a function of the
/// grace, so the constant could be cut in half and every test still passed.
///
/// TERMINATION IS THE CALLER'S: this loops until the buffer goes quiet or the
/// sampler says `Last`, so a sampler that never says `Last` while the text
/// keeps changing never returns. Production guarantees it with the deadline
/// check; the test bounds its script.
fn settle_over(quiet_run: u32, mut sample: impl FnMut(u32) -> Sample) -> (String, bool) {
    let quiet_run = quiet_run.max(1);
    let mut seen = 0usize;
    let mut quiet = 0u32;
    let mut tick = 0u32;
    loop {
        let (text, last) = match sample(tick) {
            Sample::More(t) => (t, false),
            Sample::Last(t) => (t, true),
        };
        quiet = if text.len() == seen { quiet + 1 } else { 0 };
        if quiet >= quiet_run || last {
            // `&& !last` matters on the tick where both fire: the recorded
            // outcome exists to tell a quiet exit from a deadline one, so
            // labelling the boundary as quiet mislabels precisely the case
            // the record is for.
            return (text, quiet >= quiet_run && !last);
        }
        seen = text.len();
        tick += 1;
    }
}

/// Kill the renderer's whole process group on the budget's overrun (and on
/// a wait error); a renderer that exited on its own is not chased, whatever
/// it forked. The reap still happens, on its own thread: a renderer stalled
/// in uninterruptible I/O (a stuck filesystem is one of the hangs this
/// bound exists for) keeps SIGKILL pending until the kernel lets it go, and
/// a synchronous `wait` here would hold the frame loop for exactly as long
/// as the budget was meant to stop it being held. The scratch dir's guard
/// goes with the child, so the dir is removed only once the renderer is
/// reaped; deleting it under a child still stalled in I/O would orphan its
/// output. One such thread exists per overrun of a user-initiated render,
/// each blocked in `wait` for as long as its renderer stays stuck, which is
/// bounded by the user's page turns rather than by the frame rate. Off unix
/// only the child itself can be killed.
fn kill_group(mut child: std::process::Child, scratch: TempDirGuard) {
    #[cfg(unix)]
    {
        // SAFETY: a negative pid addresses the process group the child was
        // started in (`process_group(0)` above); the call only sends a signal.
        if let Ok(pid) = libc::pid_t::try_from(child.id()) {
            unsafe {
                libc::kill(-pid, libc::SIGKILL);
            }
        }
    }
    std::thread::spawn(move || {
        // The non-unix path, and a no-op on unix after the group signal.
        let _ = child.kill();
        let _ = child.wait();
        drop(scratch);
    });
}

// Unbounded (#493): the follow-up that moves the open off the frame loop
// covers this backend too; the pdftoppm runner above is the shape to reuse.
fn rasterize_with_sips(pdf: &Path) -> std::io::Result<Vec<u8>> {
    let dir = unique_temp_dir("croft-pdf")?;
    let _guard = TempDirGuard(dir.clone());
    let out = dir.join("page.png");
    let status = Command::new("sips")
        .args(["-s", "format", "png"])
        .arg(pdf)
        .arg("--out")
        .arg(&out)
        .stdout(std::process::Stdio::null())
        .stderr(std::process::Stdio::null())
        .status()?;
    if !status.success() {
        return Err(std::io::Error::other(format!("sips exited with {status}")));
    }
    std::fs::read(&out)
}

fn unique_temp_dir(stem: &str) -> std::io::Result<PathBuf> {
    // pid + a process-wide counter, never a clock reading: two renders that
    // landed in the same nanosecond bucket used to pick the same directory,
    // and the second one's `remove_dir_all` below deleted the first one's
    // output from under `pdftoppm` - a page turn that silently did nothing.
    static SEQ: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
    let pid = std::process::id();
    let seq = SEQ.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
    // Under croft's own cache dir, never the system temp dir: /tmp is
    // world-writable on Linux, so a same-named leftover owned by another
    // user would make the remove_dir_all below fail and the render with it.
    let base = crate::session_state::dirs_cache_croft().join("tmp");
    std::fs::create_dir_all(&base)?;
    let path = base.join(format!("{stem}-{pid}-{seq}"));
    if path.exists() {
        std::fs::remove_dir_all(&path)?;
    }
    std::fs::create_dir_all(&path)?;
    Ok(path)
}

struct TempDirGuard(PathBuf);

impl Drop for TempDirGuard {
    fn drop(&mut self) {
        let _ = std::fs::remove_dir_all(&self.0);
    }
}

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

    /// A wait handed to a spawned process, scaled for a loaded host as the
    /// repo's spawn waits are (CONTRIBUTING: never a fresh fixed constant).
    #[cfg(unix)]
    fn budget(base_ms: u64) -> std::time::Duration {
        crate::test_budget::spawn_budget(std::time::Duration::from_millis(base_ms))
    }

    /// A pdftoppm script that behaves as told: `sleep <secs>` to hang, or
    /// write a byte to the `<prefix>.png` it is asked for and exit 0.
    #[cfg(unix)]
    fn fake_pdftoppm(dir: &Path, body: &str) -> PathBuf {
        use std::os::unix::fs::PermissionsExt;
        let script = dir.join("pdftoppm");
        std::fs::write(&script, format!("#!/bin/sh\n{body}\n")).unwrap();
        std::fs::set_permissions(&script, std::fs::Permissions::from_mode(0o755)).unwrap();
        script
    }

    /// #493: a renderer that hangs is killed at the budget and the open
    /// reports it, instead of parking the frame loop for as long as the
    /// child cares to run.
    #[cfg(unix)]
    #[test]
    fn a_pdftoppm_that_overruns_the_budget_is_killed_and_reported() {
        let tmp = tempfile::tempdir().unwrap();
        // The renderer sleeps well past the widest timeout the budget can
        // reach, so the elapsed ceiling below is met only by the kill.
        let timeout = budget(300);
        let script = fake_pdftoppm(
            tmp.path(),
            &format!("sleep {}", timeout.as_secs_f64() * 20.0),
        );
        let pdf = tmp.path().join("doc.pdf");
        std::fs::write(&pdf, b"%PDF-1.4\n").unwrap();
        let started = std::time::Instant::now();
        let err = run_pdftoppm(&script, &pdf, 1, timeout)
            .expect_err("an overrunning renderer is a failure, not a wait");
        let elapsed = started.elapsed();
        assert!(
            elapsed < timeout * 4,
            "the open returned at the budget, not when the child felt like it: {elapsed:?}"
        );
        assert_eq!(err.kind(), std::io::ErrorKind::TimedOut, "{err}");
        assert!(
            err.to_string().contains("longer than"),
            "the error names the budget: {err}"
        );
    }

    /// A renderer that exits promptly but leaves a descendant behind (a
    /// helper it forked, a stuck plugin) must not hold the call for as long
    /// as that descendant keeps the stderr pipe open: the budget bounds the
    /// whole call, not only the direct child's exit.
    #[cfg(unix)]
    #[test]
    fn a_pdftoppm_whose_descendant_lingers_does_not_hold_the_call() {
        let tmp = tempfile::tempdir().unwrap();
        let timeout = budget(500);
        // Comfortably past the cap at every load scale, so a call that
        // waited for the descendant would blow the assertion.
        let linger = timeout.as_secs_f64() * 6.0;
        let script = fake_pdftoppm(
            tmp.path(),
            &format!(r#"sleep {linger} & for last; do :; done; printf 'x' > "$last.png""#),
        );
        let pdf = tmp.path().join("doc.pdf");
        std::fs::write(&pdf, b"%PDF-1.4\n").unwrap();
        let started = std::time::Instant::now();
        let bytes = run_pdftoppm(&script, &pdf, 1, timeout).unwrap_or_else(|e| {
            // This failed once on CI and the `expect` said only that it should
            // not have, which left nothing to tell a timeout apart from a
            // missing page (#548). The error names which.
            panic!("the page was written; a lingering descendant is not a failure: {e}")
        });
        let elapsed = started.elapsed();
        assert_eq!(bytes, b"x");
        assert!(
            elapsed < std::time::Duration::from_secs_f64(linger / 2.0),
            "the call returned when the page was ready, not when the descendant exited: {elapsed:?}"
        );
    }

    /// A renderer that fails still has its stderr reported, through the
    /// drained pipe: the trailer-dictionary spray this module exists to keep
    /// off the TUI lands in the error instead.
    #[cfg(unix)]
    #[test]
    fn a_pdftoppm_that_fails_reports_its_stderr() {
        let tmp = tempfile::tempdir().unwrap();
        let script = fake_pdftoppm(tmp.path(), r#"echo "Syntax Error: no trailer" >&2; exit 1"#);
        let pdf = tmp.path().join("doc.pdf");
        std::fs::write(&pdf, b"%PDF-1.4\n").unwrap();
        let err = run_pdftoppm(&script, &pdf, 1, budget(5_000))
            .expect_err("a non-zero exit is a failure");
        let text = err.to_string();
        assert!(
            text.contains("exited with") && text.contains("Syntax Error: no trailer"),
            "the error carries the exit and the stderr: {text}"
        );
    }

    /// A failing renderer's stderr is reported even when a descendant it
    /// forked keeps the pipe open past its exit: the drain publishes what
    /// has arrived as it arrives, not only at end of file.
    #[cfg(unix)]
    #[test]
    fn a_failing_pdftoppm_with_a_lingering_descendant_still_reports_its_stderr() {
        let tmp = tempfile::tempdir().unwrap();
        let script = fake_pdftoppm(
            tmp.path(),
            r#"sleep 5 & echo "Syntax Error: no trailer" >&2; exit 1"#,
        );
        let pdf = tmp.path().join("doc.pdf");
        std::fs::write(&pdf, b"%PDF-1.4\n").unwrap();
        let err = run_pdftoppm(&script, &pdf, 1, budget(5_000))
            .expect_err("a non-zero exit is a failure");
        let text = err.to_string();
        assert!(
            text.contains("Syntax Error: no trailer"),
            "the stderr is reported despite the lingering descendant: {text}"
        );
    }

    /// A renderer that fails has its stderr and its exit status in the
    /// error, rather than sprayed over the TUI.
    ///
    /// It no longer asserts the SECOND of two bursts: whether a write landing
    /// after the exit arrives inside the settle window is a race this test
    /// cannot win on a loaded machine (#548). That property is asked of the
    /// rule directly in
    /// [`the_settle_rule_bridges_a_gap_shorter_than_the_quiet_run`].
    #[cfg(unix)]
    #[test]
    fn a_failing_pdftoppm_reports_the_stderr_it_sprayed() {
        // End to end: a renderer that fails has its stderr in the error
        // rather than on the user's TUI. What this canNOT assert is the
        // second burst (#548): whether a write landing after the exit arrives
        // inside the settle window is a race against scheduling, measured at
        // 69 ms under load against a 60 ms window, and asserting it here made
        // the test fail for the machine's reasons rather than croft's.
        //
        // The property itself is not dropped - it moved to
        // `the_settle_rule_bridges_a_gap_shorter_than_the_quiet_run`, which
        // asks it of the rule directly, in ticks, with no clock to lose to.
        let tmp = tempfile::tempdir().unwrap();
        let script = fake_pdftoppm(
            tmp.path(),
            r#"( sleep 0.05; echo "Syntax Error: second" >&2 ) & echo "Syntax Error: first" >&2; exit 1"#,
        );
        let pdf = tmp.path().join("doc.pdf");
        std::fs::write(&pdf, b"%PDF-1.4\n").unwrap();
        // Once, not five times: the repeat existed to shake out the flaky
        // second-burst assertion, and what is left is deterministic.
        let err = run_pdftoppm(&script, &pdf, 1, budget(5_000))
            .expect_err("a non-zero exit is a failure");
        let text = err.to_string();
        assert!(
            text.contains("Syntax Error: first"),
            "the renderer's spray reaches the error: {text}\n[#548] {}",
            last_settle()
        );
        assert!(
            text.contains("exited with"),
            "and so does the exit status: {text}"
        );
    }

    /// The settle rule bridges a gap shorter than its quiet run and does not
    /// wait for one longer, which is what `QUIET_RUN_TICKS` means.
    ///
    /// Scripted by TICK INDEX, with no threads and no clock: the readings are
    /// stated rather than raced for, so the rule is what is under test and
    /// the host's scheduling cannot decide the result.
    #[test]
    fn the_settle_rule_bridges_a_gap_shorter_than_the_quiet_run() {
        // Ticks, not a clock (#548). The wall-clock version of this raced
        // scheduling and was the flake; worse, it could not pin
        // `QUIET_RUN_TICKS` at all, because the run was silently clamped to a
        // function of the grace - the constant could be halved and every test
        // still passed.
        //
        // A scripted buffer says exactly what arrived and when, so the
        // question this asks is the real one: is the production run long
        // enough to bridge the gap it exists to bridge?
        // The gaps are ABSOLUTE on purpose. Deriving them from
        // `QUIET_RUN_TICKS` reads better and pins nothing: the fixture then
        // moves with the constant and holds for any value it takes, which a
        // re-break caught - halving the constant left this test green.
        // Written as numbers, the pair straddles the production run, so the
        // constant cannot move far in either direction without this failing.
        // Measured tolerance: this accepts a run of 10 through 14, and 12
        // sits in the middle, so it is a bound with two ticks of slack rather
        // than an exact pin.
        let script = |gap: u32| {
            move |tick: u32| {
                let text = if tick < gap {
                    String::from("Syntax Error: first")
                } else {
                    String::from("Syntax Error: first\nSyntax Error: second")
                };
                // Far past the gap, so the QUIET RUN decides and the deadline
                // is never the reason.
                if tick > gap + 60 {
                    Sample::Last(text)
                } else {
                    Sample::More(text)
                }
            }
        };

        let (text, on_quiet) = settle_over(QUIET_RUN_TICKS, script(10));
        assert!(
            text.contains("second") && on_quiet,
            "a 10-tick gap is inside the production run and must be bridged, \
             settling on the run rather than the deadline: {text:?}"
        );

        let (text, _) = settle_over(QUIET_RUN_TICKS, script(15));
        assert!(
            !text.contains("second"),
            "a 15-tick gap is outside it and must NOT be waited for - without \
             this half the pair above is satisfied by a rule that waits \
             forever: {text:?}"
        );
    }

    /// Everything the RENDERER ITSELF wrote is reported, however loaded the
    /// machine is (#553).
    ///
    /// This is the case the user meets: poppler prints a generic first line
    /// and the specific cause after it, then exits. Both are in the pipe by
    /// the time it exits, so taking them cannot depend on a clock - and under
    /// the previous design it did, which is how an error arrived with its
    /// useful half missing and nothing to say so.
    ///
    /// The writes are separated by a sleep so they land as two reads rather
    /// than one, which is what made the old drain lose the second.
    #[cfg(unix)]
    #[test]
    fn every_line_the_renderer_wrote_before_exiting_is_reported() {
        let tmp = tempfile::tempdir().unwrap();
        let script = fake_pdftoppm(
            tmp.path(),
            r#"echo "Syntax Error: couldn't find trailer dictionary" >&2; sleep 0.05; echo "Syntax Error: Couldn't read xref table" >&2; exit 1"#,
        );
        let pdf = tmp.path().join("doc.pdf");
        std::fs::write(&pdf, b"%PDF-1.4\n").unwrap();
        for _ in 0..5 {
            let err = run_pdftoppm(&script, &pdf, 1, budget(5_000))
                .expect_err("a non-zero exit is a failure");
            let text = err.to_string();
            assert!(
                text.contains("trailer dictionary") && text.contains("xref table"),
                "both of the renderer's own lines reach the error: {text}\n[#548] {}",
                last_settle()
            );
        }
    }

    /// A stream that has stopped settles on the QUIET RUN, not at the
    /// deadline: a failing render must not pay the whole grace when nothing
    /// more is coming.
    ///
    /// In ticks rather than wall clock, so the assertion is about the rule
    /// and not about how promptly this machine schedules a sleep.
    #[test]
    fn a_stream_that_has_stopped_settles_on_the_quiet_run() {
        for seed in ["Syntax Error: complete", ""] {
            let (text, on_quiet) = settle_over(QUIET_RUN_TICKS, |tick| {
                let text = seed.to_string();
                // A budget far beyond the run, so settling early is the only
                // way to return before it.
                if tick > QUIET_RUN_TICKS * 10 {
                    Sample::Last(text)
                } else {
                    Sample::More(text)
                }
            });
            assert!(
                on_quiet,
                "a stream that stopped settles on the run, not the deadline: {text:?}"
            );
            assert_eq!(text, seed.trim(), "and returns what had arrived");
        }
    }

    /// The deadline is the bound when the stream never goes quiet: bytes
    /// arriving faster than the run can complete are cut off with what
    /// arrived, rather than followed indefinitely.
    #[test]
    fn a_stream_that_never_pauses_is_cut_off_at_the_deadline() {
        let budget = QUIET_RUN_TICKS * 3;
        let (text, on_quiet) = settle_over(QUIET_RUN_TICKS, |tick| {
            // Grows on every tick, so the quiet run can never complete.
            let text = "x".repeat(tick as usize + 1);
            if tick >= budget {
                Sample::Last(text)
            } else {
                Sample::More(text)
            }
        });
        assert!(
            !on_quiet,
            "the deadline is the exit when nothing settles: {text:?}"
        );
        assert_eq!(
            text.len() as u32,
            budget + 1,
            "and it returns everything that had arrived by then"
        );
    }

    /// A renderer that fails silently still names its exit status.
    #[cfg(unix)]
    #[test]
    fn a_silently_failing_pdftoppm_still_names_its_exit() {
        let tmp = tempfile::tempdir().unwrap();
        let script = fake_pdftoppm(tmp.path(), "exit 3");
        let pdf = tmp.path().join("doc.pdf");
        std::fs::write(&pdf, b"%PDF-1.4\n").unwrap();
        let err = run_pdftoppm(&script, &pdf, 1, budget(5_000))
            .expect_err("a non-zero exit is a failure");
        assert!(err.to_string().contains("exited with"), "{err}");
    }

    /// A hung renderer's descendants go down with it: the kill reaches the
    /// process group, so a helper the renderer forked cannot outlive the
    /// budget and keep running (or keep the pipe open) after the open failed.
    #[cfg(unix)]
    #[test]
    fn a_hung_pdftoppm_takes_its_descendants_down_with_it() {
        let tmp = tempfile::tempdir().unwrap();
        let marker = tmp.path().join("alive");
        // The forked helper outlives the renderer unless the GROUP is killed.
        // Its delay is derived from the scaled timeout, comfortably past the
        // widest value the budget can reach, so the helper is still pending
        // when the kill lands at every load scale rather than only at low
        // ones; the renderer's own sleep is longer still.
        let timeout = budget(300);
        let helper_delay = timeout.as_secs_f64() * 3.0;
        let script = fake_pdftoppm(
            tmp.path(),
            &format!(
                "(sleep {helper_delay}; touch '{}') & sleep {}",
                marker.display(),
                helper_delay * 10.0
            ),
        );
        let pdf = tmp.path().join("doc.pdf");
        std::fs::write(&pdf, b"%PDF-1.4\n").unwrap();
        run_pdftoppm(&script, &pdf, 1, timeout).expect_err("an overrun is a failure");
        std::thread::sleep(std::time::Duration::from_secs_f64(helper_delay * 2.0));
        assert!(
            !marker.exists(),
            "the forked helper survived the group kill"
        );
    }

    /// The control for the tests above: a renderer that finishes inside the
    /// budget hands back the bytes it wrote, budget or no budget.
    #[cfg(unix)]
    #[test]
    fn a_pdftoppm_that_finishes_in_time_hands_back_its_page() {
        let tmp = tempfile::tempdir().unwrap();
        // The prefix is the last argument; the renderer writes `<prefix>.png`.
        let script = fake_pdftoppm(
            tmp.path(),
            r#"for last; do :; done; printf 'x' > "$last.png""#,
        );
        let pdf = tmp.path().join("doc.pdf");
        std::fs::write(&pdf, b"%PDF-1.4\n").unwrap();
        let bytes = run_pdftoppm(&script, &pdf, 1, budget(5_000))
            .expect("a renderer that finishes is not a failure");
        assert_eq!(bytes, b"x");
    }

    /// Two renders in flight at once must never share a scratch directory:
    /// the loser's `remove_dir_all` used to delete the winner's PNG before it
    /// was read, and the page turn silently did nothing.
    #[test]
    fn concurrent_scratch_dirs_are_distinct() {
        let dirs: Vec<_> = std::thread::scope(|s| {
            let handles: Vec<_> = (0..16)
                .map(|_| s.spawn(|| unique_temp_dir("croft-pdf-test").unwrap()))
                .collect();
            handles.into_iter().map(|h| h.join().unwrap()).collect()
        });
        let unique: std::collections::HashSet<_> = dirs.iter().collect();
        assert_eq!(unique.len(), dirs.len(), "every render needs its own dir");
        for d in dirs {
            let _ = std::fs::remove_dir_all(d);
        }
    }

    /// pdftoppm's stderr must be captured into the returned error, never
    /// inherited: with a half-written PDF (pdflatex rewriting the open file)
    /// the child used to spray "Syntax Error: Couldn't find trailer
    /// dictionary" straight onto croft's TTY, corrupting the whole UI.
    #[test]
    fn pdftoppm_failure_captures_stderr_instead_of_writing_to_tty() {
        if which("pdftoppm").is_none() {
            eprintln!("skipping: pdftoppm not installed");
            return;
        }
        let dir = unique_temp_dir("croft-pdf-test-stderr").unwrap();
        let truncated = dir.join("half-written.pdf");
        std::fs::write(&truncated, b"%PDF-1.7\ntruncated mid-write, no xref").unwrap();
        let err = rasterize_with_pdftoppm(&truncated, 1).unwrap_err();
        let msg = err.to_string();
        assert!(
            msg.contains("Syntax Error") || msg.contains("trailer"),
            "error must carry the child's stderr, got: {msg}"
        );
        let _ = std::fs::remove_dir_all(dir);
    }

    /// Real pdftohtml -xml shape: link runs are `<a href>` inside a `<text>`
    /// element carrying the rect; external hrefs keep their scheme, internal
    /// ones are `docname.html#page`; `&amp;` needs unescaping; the requested
    /// page is selected by its `number` attribute.
    #[test]
    fn parses_links_from_pdf2xml_output() {
        let xml = r##"<?xml version="1.0" encoding="UTF-8"?>
<pdf2xml producer="poppler" version="25.04.0">
 link to page 2 <page number="1" position="absolute" top="0" left="0" height="1188" width="918">
<text top="192" left="223" width="55" height="13" font="0">Jump to</text>
<text top="192" left="283" width="65" height="13" font="1"><a href="ilink.html#2">the target</a></text>
</page>
<page number="2" position="absolute" top="0" left="0" height="1188" width="918">
<text top="192" left="511" width="58" height="13" font="3"><a href="https://a.b/c?d=1&amp;e=2">amp link</a></text>
<text top="300" left="100" width="90" height="12" font="2"><a href="mailto:x@y.z">mailto:x@y.z</a></text>
<text top="500" left="100" width="90" height="12" font="0">plain text</text>
</page>
</pdf2xml>
"##;
        let p1 = parse_pdf2xml_links(xml, 1).unwrap();
        assert_eq!((p1.page_w, p1.page_h), (918, 1188));
        assert_eq!(
            p1.links,
            vec![PdfLink {
                rect: (283, 192, 348, 205),
                target: LinkTarget::Page(2),
            }]
        );
        let p2 = parse_pdf2xml_links(xml, 2).unwrap();
        assert_eq!(
            p2.links,
            vec![
                PdfLink {
                    rect: (511, 192, 569, 205),
                    target: LinkTarget::Url(String::from("https://a.b/c?d=1&e=2")),
                },
                PdfLink {
                    rect: (100, 300, 190, 312),
                    target: LinkTarget::Url(String::from("mailto:x@y.z")),
                },
            ]
        );
        assert!(
            parse_pdf2xml_links(xml, 3).is_none(),
            "a page absent from the output has no links"
        );
    }

    /// poppler often wraps only part of a text run in the `<a>`: the link
    /// rect must cover just the linked words (proportionally by character),
    /// not the whole run, or clicking plain text next to a link opens it.
    /// Real shape from a published PDF where the link text is ") is another "
    /// (13 of the run's 28 visible characters).
    #[test]
    fn a_partial_run_link_covers_only_its_own_words() {
        let xml = r##"<?xml version="1.0" encoding="UTF-8"?>
<pdf2xml producer="poppler" version="25.04.0">
<page number="1" position="absolute" top="0" left="0" height="1188" width="918">
<text top="700" left="514" width="196" height="21" font="3"><a href="https://pycoders.com">) is another </a>popular weekly </text>
</page>
</pdf2xml>
"##;
        let p = parse_pdf2xml_links(xml, 1).unwrap();
        assert_eq!(p.links.len(), 1);
        let (x0, y0, x1, y1) = p.links[0].rect;
        assert_eq!((x0, y0, y1), (514, 700, 721));
        // 13/28 of the 196-wide run is 91: the rect ends near 605, well
        // short of the run's right edge at 710 where "popular weekly" sits.
        assert!(
            (600..=610).contains(&x1),
            "link rect must stop at the linked words, got x1={x1}"
        );
    }

    /// Adjacent TOC lines produce thin rects that genuinely overlap each
    /// other, and one coarse terminal cell can straddle two of them. The
    /// click must resolve to the link covering most of the cell, not the
    /// first overlapping one in document order.
    #[test]
    fn overlapping_toc_rects_resolve_to_the_most_covered_link() {
        let links = PageLinks {
            page_w: 1000,
            page_h: 1188,
            links: vec![
                PdfLink {
                    rect: (100, 189, 400, 212),
                    target: LinkTarget::Page(509),
                },
                PdfLink {
                    rect: (100, 208, 400, 231),
                    target: LinkTarget::Page(511),
                },
            ],
        };
        // A cell row covering page-y 203.7..237.6 (a 35-row canvas over a
        // 1188-tall page): 8 units of the first rect, 23 of the second.
        let hit = links
            .link_at((0.15, 203.7 / 1188.0, 0.30, 237.6 / 1188.0))
            .expect("the cell overlaps both rects");
        assert_eq!(hit.target, LinkTarget::Page(511));
    }

    /// A document-supplied href is untrusted input: only web and mail links
    /// may reach the OS opener. `file://` (one-click app launch on macOS)
    /// and arbitrary registered schemes must be dropped, not opened.
    #[test]
    fn only_web_and_mail_hrefs_open_externally() {
        assert_eq!(
            classify_href("file:///System/Applications/Calculator.app"),
            None
        );
        assert_eq!(classify_href("vscode://extension/whatever"), None);
        assert_eq!(classify_href("ssh://root@host"), None);
        assert_eq!(
            classify_href("https://a.b/c"),
            Some(LinkTarget::Url(String::from("https://a.b/c")))
        );
        assert_eq!(
            classify_href("HTTPS://A.B/c"),
            Some(LinkTarget::Url(String::from("HTTPS://A.B/c")))
        );
        assert_eq!(
            classify_href("mailto:x@y.z"),
            Some(LinkTarget::Url(String::from("mailto:x@y.z")))
        );
    }

    /// A degenerate run (`height="0"` or `width="0"`, which the parser must
    /// tolerate even if poppler is not known to emit it) still yields a rect
    /// the positive-area hit test can match: extents clamp to at least one
    /// page unit, or the link is silently unclickable forever.
    #[test]
    fn a_zero_extent_run_still_yields_a_clickable_rect() {
        let xml = r##"<?xml version="1.0" encoding="UTF-8"?>
<pdf2xml producer="poppler" version="25.04.0">
<page number="1" position="absolute" top="0" left="0" height="1000" width="1000">
<text top="100" left="50" width="120" height="0"><a href="https://a.b/">flat</a></text>
<text top="300" left="50" width="0" height="12"><a href="https://c.d/"></a></text>
</page>
</pdf2xml>
"##;
        let p = parse_pdf2xml_links(xml, 1).unwrap();
        assert_eq!(p.links.len(), 2);
        for l in &p.links {
            let (x0, y0, x1, y1) = l.rect;
            assert!(x1 > x0 && y1 > y0, "degenerate rect survived: {:?}", l.rect);
        }
        // And a cell over each degenerate run actually hits it.
        assert!(p.link_at((0.05, 0.099, 0.18, 0.102)).is_some());
        assert!(p.link_at((0.049, 0.30, 0.052, 0.312)).is_some());
    }

    /// An empty anchor sitting at the right edge of a run with text: the
    /// forced one-unit width must be carved out of the run, not appended
    /// past its edge where it would make the neighbouring run clickable.
    #[test]
    fn an_empty_anchor_at_the_run_edge_stays_inside_the_run() {
        let xml = r##"<?xml version="1.0" encoding="UTF-8"?>
<pdf2xml producer="poppler" version="25.04.0">
<page number="1" position="absolute" top="0" left="0" height="1000" width="1000">
<text top="100" left="200" width="120" height="12">text<a href="https://a.b/"></a></text>
</page>
</pdf2xml>
"##;
        let p = parse_pdf2xml_links(xml, 1).unwrap();
        assert_eq!(p.links.len(), 1);
        let (x0, _, x1, _) = p.links[0].rect;
        assert!(
            x0 >= 200 && x1 <= 320 && x0 < x1,
            "the forced width must stay inside the run rect (200..320), got {x0}..{x1}"
        );
    }

    /// A bare `&` (no terminating `;`) must count as a plain character, not
    /// swallow the rest of the fragment as a half-open entity: the counts on
    /// the two sides of the anchor would disagree and the link's slice could
    /// extend past its own run rect, making unrelated text clickable.
    #[test]
    fn an_unterminated_entity_cannot_push_a_link_past_its_run() {
        let xml = r##"<?xml version="1.0" encoding="UTF-8"?>
<pdf2xml producer="poppler" version="25.04.0">
<page number="1" position="absolute" top="0" left="0" height="1000" width="1000">
<text top="100" left="200" width="120" height="12">a & b<a href="https://a.b/">L</a></text>
</page>
</pdf2xml>
"##;
        let p = parse_pdf2xml_links(xml, 1).unwrap();
        assert_eq!(p.links.len(), 1);
        let (x0, _, x1, _) = p.links[0].rect;
        assert!(
            x0 >= 200 && x1 <= 320 && x0 < x1,
            "the link slice must stay inside its run rect (200..320), got {x0}..{x1}"
        );
        // Terminated entities still count as the single character they
        // stand for: "&amp; " is 2 visible chars, so the anchor's slice
        // starts past the midpoint of this 6-char run, not at its left edge.
        let ent = r##"<?xml version="1.0" encoding="UTF-8"?>
<pdf2xml producer="poppler" version="25.04.0">
<page number="1" position="absolute" top="0" left="0" height="1000" width="1000">
<text top="100" left="0" width="600" height="12">&amp; ab<a href="https://a.b/">cd</a></text>
</page>
</pdf2xml>
"##;
        let p = parse_pdf2xml_links(ent, 1).unwrap();
        assert_eq!(p.links[0].rect.0, 400, "4 of 6 chars precede the anchor");
    }

    /// The whole cell area is hit-tested, so a click one row under a thin
    /// text-line rect still lands: any overlap counts, containment is not
    /// required. Misses stay misses.
    #[test]
    fn link_at_matches_on_overlap_not_containment() {
        let links = PageLinks {
            page_w: 1000,
            page_h: 1000,
            links: vec![PdfLink {
                rect: (100, 100, 200, 113),
                target: LinkTarget::Page(2),
            }],
        };
        // A cell straddling the rect's bottom edge (y 0.110..0.130) hits.
        assert!(links.link_at((0.15, 0.110, 0.16, 0.130)).is_some());
        // A cell fully past the rect misses.
        assert!(links.link_at((0.15, 0.120, 0.16, 0.140)).is_none());
        // A cell left of the rect misses.
        assert!(links.link_at((0.05, 0.105, 0.09, 0.110)).is_none());
    }

    #[test]
    fn parses_page_count_from_pdfinfo_output() {
        let sample = "\
Title:          \nAuthor:         \nCreator:        \nProducer:       \n\
Pages:          12\nEncrypted:      no\nPage size:      612 x 792 pts\n";
        assert_eq!(parse_pdfinfo_pages(sample), Some(12));
    }

    /// The `mdls` argv, asserted without spawning `mdls` (#493). This is the
    /// one part of that probe a test can reach on any machine.
    #[test]
    fn the_mdls_argv_names_the_page_count_attribute_in_order() {
        let pdf = std::path::Path::new("/tmp/doc.pdf");
        let argv = mdls_argv(pdf);
        assert_eq!(argv[0], std::ffi::OsStr::new("-raw"));
        assert_eq!(argv[1], std::ffi::OsStr::new("-name"));
        assert_eq!(argv[2], std::ffi::OsStr::new("kMDItemNumberOfPages"));
        assert_eq!(argv[3], pdf.as_os_str(), "the file is the last argument");
    }

    /// The probe budget must stay below the render's. The doc comments argue
    /// the gap at length - a header parse still running that long is wedged,
    /// not slow - and nothing else would notice someone raising it.
    #[test]
    fn the_probe_budget_stays_under_the_render_budget() {
        assert!(
            PDF_INFO_BUDGET < PDF_RENDER_BUDGET,
            "a page-count probe must not be allowed to outlive a rasterisation"
        );
    }

    /// The budget must actually fire (#493). Without a test that HANGS a
    /// child, the bound is only shown on the path where the child exits on
    /// its own - which is the path that never needed bounding.
    ///
    /// `sleep 30` against a 150ms budget: the call must return `None` in
    /// well under the sleep, and the assertion on elapsed time is what
    /// distinguishes "the deadline fired" from "the child happened to be
    /// fast". Without the elapsed bound this test would also pass if
    /// `recv_timeout` were removed entirely.
    #[cfg(unix)]
    #[test]
    fn a_hanging_probe_is_killed_at_the_budget_not_waited_on() {
        // Scaled, not a literal: CONTRIBUTING's "Waiting on a spawned
        // process in a test" names exactly this case - a deadline handed to
        // a recv_timeout - and the test twelve lines up already uses this
        // helper. A fixed 150ms is a spawn plus a channel round trip on a
        // contended runner.
        let timeout = budget(150);
        let start = std::time::Instant::now();
        let out = run_bounded_stdout(
            std::path::Path::new("sleep"),
            &[std::ffi::OsStr::new("30")],
            timeout,
        );
        let elapsed = start.elapsed();
        assert!(out.is_none(), "a child that outruns the budget yields None");
        assert!(
            elapsed < timeout.mul_f64(20.0),
            "the deadline must fire rather than wait out the child: took {elapsed:?}"
        );
    }

    /// The paired presence case, so the refusal above cannot pass vacuously:
    /// a child that finishes inside the budget still returns its stdout.
    #[cfg(unix)]
    #[test]
    fn a_quick_probe_returns_its_output() {
        // `sh -c` with a sleep, not a bare `echo`: the interesting success
        // path is a child that writes and exits while the parent is INSIDE
        // recv_timeout, which an immediate exit never reaches.
        let (status, out) = run_bounded_stdout(
            std::path::Path::new("sh"),
            &[
                std::ffi::OsStr::new("-c"),
                std::ffi::OsStr::new("sleep 0.05; echo 'Pages:          7'"),
            ],
            budget(2000),
        )
        .expect("a child that exits in time yields its stdout");
        assert!(status.success(), "a clean exit is reported as success");
        assert_eq!(parse_pdfinfo_pages(&out), Some(7));
    }

    /// The status must reach the caller, because both probes gate on it and
    /// the unbounded version they replaced did too. Stdout that parses from a
    /// FAILED run is the case that would otherwise be taken as an answer.
    #[cfg(unix)]
    #[test]
    fn a_failing_probe_reports_its_status_even_with_parseable_output() {
        let (status, out) = run_bounded_stdout(
            std::path::Path::new("sh"),
            &[
                std::ffi::OsStr::new("-c"),
                std::ffi::OsStr::new("echo 'Pages:          9'; exit 1"),
            ],
            budget(2000),
        )
        .expect("output is still returned; the status is what says to ignore it");
        assert!(
            !status.success(),
            "a non-zero exit must be visible to the caller"
        );
        assert_eq!(
            parse_pdfinfo_pages(&out),
            Some(9),
            "the output really would have parsed, which is why the guard matters"
        );
    }

    #[test]
    fn pdfinfo_returns_none_when_pages_line_missing() {
        let sample = "Title:  Untitled\nAuthor:  Anon\n";
        assert!(parse_pdfinfo_pages(sample).is_none());
    }

    #[test]
    fn pdfinfo_handles_extra_whitespace() {
        let sample = "Pages:    7\n";
        assert_eq!(parse_pdfinfo_pages(sample), Some(7));
    }

    /// The blocking-pipe fallback must still report the renderer's message.
    ///
    /// This is the regression the first cut of the fix shipped: with both
    /// drains skipped, the error read `"... exited with exit status: 1: "`
    /// with no text, because every write to the buffer sat behind the
    /// non-blocking branch. `read_once` is what puts the message back, so
    /// the assertion is on CONTENT - an `is_empty` check either way would
    /// pass against both the bug and the fix.
    ///
    /// The pipe here is left BLOCKING on purpose: that is the state a failed
    /// fcntl leaves, and the whole point is that one read still works in it.
    #[cfg(unix)]
    #[test]
    fn a_blocking_pipe_still_yields_what_the_renderer_wrote() {
        let mut child = std::process::Command::new("sh")
            .args(["-c", "echo 'Syntax Error: no trailer' >&2; exit 1"])
            .stderr(std::process::Stdio::piped())
            .spawn()
            .expect("spawn");
        let mut pipe = child.stderr.take().expect("piped stderr");
        let status = child.wait().expect("wait");
        assert!(!status.success(), "the fixture must fail");

        // Deliberately NOT made non-blocking - this is the failed-fcntl state.
        let mut buf = Vec::new();
        read_once(Some(&mut pipe), &mut buf);
        let text = String::from_utf8_lossy(&buf).trim().to_string();
        assert_eq!(
            text, "Syntax Error: no trailer",
            "the renderer's own line must survive the blocking path"
        );
    }

    /// `set_nonblocking` must REPORT the outcome, because the caller reads
    /// the pipe before reaping the child and only a non-blocking pipe makes
    /// that read safe. A bare `true` would pass the live-pipe case, so the
    /// bad-descriptor case is what gives the assertion teeth: the two
    /// together pin a return value that actually tracks the fcntl.
    ///
    /// The failure is provoked on a RAW fd rather than a real pipe, because
    /// closing one out from under a `ChildStderr` aborts the process under
    /// Rust's IO-safety checks - which would "detect" a regression by
    /// crashing rather than failing, and abort a passing run just as readily.
    #[cfg(unix)]
    #[test]
    fn set_nonblocking_reports_whether_the_pipe_is_now_non_blocking() {
        let mut child = std::process::Command::new("sh")
            .args(["-c", "exit 0"])
            .stderr(std::process::Stdio::piped())
            .spawn()
            .expect("spawn");
        let pipe = child.stderr.take().expect("piped stderr");

        // A live pipe: the fcntl pair succeeds and the answer is yes.
        assert!(
            set_nonblocking(Some(&pipe)),
            "a live pipe must report non-blocking"
        );

        // -1 is never a valid descriptor, so F_GETFL fails with EBADF - the
        // same branch a real fcntl failure takes. `set_nonblocking`'s unix
        // arm is a call to this and nothing else, so this IS its failure
        // path, reached without closing an fd under a live handle.
        assert!(
            !set_nonblocking_on(-1),
            "a bad descriptor must report failure, not be ignored"
        );

        // And the no-pipe case the doc promises, so the contract is pinned on
        // every input shape rather than only the one with a pipe.
        assert!(set_nonblocking(None), "no pipe means nothing to block on");

        let _ = child.wait();
    }
}