quarto-error-reporting 0.3.2

Structured, source-aware diagnostics with pluggable rendering (ariadne or annotate-snippets) and a pluggable error-code catalog.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
//! Core diagnostic message types.
//!
//! This module defines the fundamental structures for representing diagnostic messages
//! (errors, warnings, info) following tidyverse-style guidelines.

use serde::{Deserialize, Serialize};

/// The kind of diagnostic message.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum DiagnosticKind {
    /// An error that prevents completion
    Error,
    /// A warning that doesn't prevent completion but indicates a problem
    Warning,
    /// Informational message
    Info,
    /// A note providing additional context
    Note,
}

/// How detail items should be presented (tidyverse x/i bullet style).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum DetailKind {
    /// Error detail (✖ bullet in tidyverse style)
    Error,
    /// Info detail (i bullet in tidyverse style)
    Info,
    /// Note detail (plain bullet)
    Note,
    /// Faded detail — rendered in Ariadne with the same dim grey colour
    /// Ariadne uses for source characters outside any label. Use it to
    /// attach a high-priority label to a column range you want to
    /// *exclude* from a wider label's highlighting (e.g. a block-quote
    /// prefix inside a multi-line span). Treated the same as `Note` in
    /// tidyverse-style text output.
    Faded,
}

/// Options for rendering diagnostic messages to text.
///
/// This struct controls various aspects of text rendering, such as whether
/// to include terminal hyperlinks for clickable file paths.
#[derive(Debug, Clone)]
pub struct TextRenderOptions {
    /// Enable OSC 8 hyperlinks for clickable file paths in terminals.
    ///
    /// When enabled, file paths in error messages will include terminal
    /// escape codes for clickable links (supported by iTerm2, VS Code, etc.).
    /// Disable for snapshot testing to avoid absolute path differences.
    pub enable_hyperlinks: bool,
}

impl Default for TextRenderOptions {
    fn default() -> Self {
        Self {
            enable_hyperlinks: true,
        }
    }
}

/// Selects which source-context snippet renderer draws the visual code
/// excerpt in [`DiagnosticMessage::to_text_with_renderer`].
///
/// The available variants depend on which renderer features are enabled
/// at compile time, so this enum is `#[non_exhaustive]`: with neither
/// `ariadne` nor `annotate-snippets` enabled it has no variants at all,
/// and downstream `match`es must include a wildcard arm to stay
/// compiling across feature combinations.
///
/// Pass `None` to [`DiagnosticMessage::to_text_with_renderer`] (or use
/// [`DiagnosticMessage::to_text`] / [`DiagnosticMessage::to_text_with_options`])
/// to let the crate pick a default via [`SourceRenderer::default_for_features`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum SourceRenderer {
    /// [ariadne](https://crates.io/crates/ariadne)-style rendering: a
    /// boxed source excerpt. Available with the `ariadne` feature (on by
    /// default).
    #[cfg(feature = "ariadne")]
    Ariadne,
    /// [annotate-snippets](https://crates.io/crates/annotate-snippets)-style
    /// rendering: the rust-lang toolchain's `-->` / gutter-bar look.
    /// Available with the `annotate-snippets` feature.
    #[cfg(feature = "annotate-snippets")]
    AnnotateSnippets,
}

impl SourceRenderer {
    /// The renderer used when the caller does not specify one.
    ///
    /// Prefers [`SourceRenderer::Ariadne`] when the `ariadne` feature is
    /// enabled (preserving historical behavior), then falls back to
    /// [`SourceRenderer::AnnotateSnippets`]. Returns `None` when no
    /// renderer feature is enabled, in which case `to_text` drops the
    /// source-context snippet and prints the structured text block.
    pub fn default_for_features() -> Option<Self> {
        // Exactly one of these `#[cfg]` blocks survives in any feature
        // configuration, so the surviving block is the function's tail
        // expression — no `return` and no unreachable code.
        #[cfg(feature = "ariadne")]
        {
            Some(Self::Ariadne)
        }
        #[cfg(all(not(feature = "ariadne"), feature = "annotate-snippets"))]
        {
            Some(Self::AnnotateSnippets)
        }
        #[cfg(all(not(feature = "ariadne"), not(feature = "annotate-snippets")))]
        {
            None
        }
    }
}

/// The content of a message or detail item.
///
/// This will eventually support Pandoc AST for rich formatting, but starts
/// with simpler string-based content.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum MessageContent {
    /// Plain text content
    Plain(String),
    /// Markdown content (will be parsed to Pandoc AST in later phases)
    Markdown(String),
    // Future: PandocAst(Box<Inlines>)
}

impl MessageContent {
    /// Get the raw string content for display
    pub fn as_str(&self) -> &str {
        match self {
            MessageContent::Plain(s) => s,
            MessageContent::Markdown(s) => s,
        }
    }

    /// Convert to JSON value with type information
    pub fn to_json(&self) -> serde_json::Value {
        use serde_json::json;
        match self {
            MessageContent::Plain(s) => json!({
                "type": "plain",
                "content": s
            }),
            MessageContent::Markdown(s) => json!({
                "type": "markdown",
                "content": s
            }),
        }
    }
}

impl From<String> for MessageContent {
    fn from(s: String) -> Self {
        MessageContent::Markdown(s)
    }
}

impl From<&str> for MessageContent {
    fn from(s: &str) -> Self {
        MessageContent::Markdown(s.to_string())
    }
}

/// A detail item in a diagnostic message.
///
/// Following tidyverse guidelines, details provide specific information about
/// the error (what went wrong, where, with what values).
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct DetailItem {
    /// The kind of detail (error, info, note)
    pub kind: DetailKind,
    /// The content of the detail
    pub content: MessageContent,
    /// Optional source location for this detail
    ///
    /// When present, this identifies where in the source code this detail applies.
    /// This allows error messages to highlight multiple related locations.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub location: Option<quarto_source_map::SourceInfo>,
}

/// A diagnostic message following tidyverse-style structure.
///
/// Structure:
/// 1. **Code**: Optional error code (e.g., "Q-1-1") for searchability
/// 2. **Title**: Brief error message
/// 3. **Kind**: Error, Warning, Info
/// 4. **Problem**: What went wrong (the "must" or "can't" statement)
/// 5. **Details**: Specific information (bulleted, max 5 per tidyverse)
/// 6. **Hints**: Optional guidance for fixing (ends with ?)
///
/// # Example
///
/// ```ignore
/// let msg = DiagnosticMessage {
///     code: Some("Q-1-2".to_string()), // quarto-error-code-audit-ignore
///     title: "Incompatible types".to_string(),
///     kind: DiagnosticKind::Error,
///     problem: Some("Cannot combine date and datetime types".into()),
///     details: vec![
///         DetailItem {
///             kind: DetailKind::Error,
///             content: "`x`{.arg} has type `date`{.type}".into(),
///         },
///         DetailItem {
///             kind: DetailKind::Error,
///             content: "`y`{.arg} has type `datetime`{.type}".into(),
///         },
///     ],
///     hints: vec!["Convert both to the same type?".into()],
///     source_spans: vec![],
/// };
/// ```
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct DiagnosticMessage {
    /// Optional error code (e.g., "Q-1-1")
    ///
    /// Error codes are optional but encouraged. They provide:
    /// - Searchability (users can Google "Q-1-1")
    /// - Stability (codes don't change even if message wording improves)
    /// - Documentation (each code maps to a detailed explanation)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub code: Option<String>,

    /// Brief title for the error
    pub title: String,

    /// The kind of diagnostic (Error, Warning, Info)
    pub kind: DiagnosticKind,

    /// The problem statement (the "what" - using "must" or "can't")
    pub problem: Option<MessageContent>,

    /// Specific error details (the "where/why" - max 5 per tidyverse)
    pub details: Vec<DetailItem>,

    /// Optional hints for fixing (ends with ?)
    pub hints: Vec<MessageContent>,

    /// Source location for this diagnostic
    ///
    /// When present, this identifies where in the source code the issue occurred.
    /// The location may track transformation history, allowing the error to be
    /// mapped back through multiple processing steps to the original source file.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub location: Option<quarto_source_map::SourceInfo>,
}

/// A minimal multi-source [`ariadne::Cache`] over in-memory files, keyed
/// by display path. The single-file `(id, source)` tuple used previously
/// cannot serve labels rooted in another `Concat` piece, which need one
/// source section per file.
#[cfg(feature = "ariadne")]
struct ContextSourceCache {
    files: Vec<(String, ariadne::Source<String>)>,
}

#[cfg(feature = "ariadne")]
impl ariadne::Cache<String> for ContextSourceCache {
    type Storage = String;

    fn fetch(&mut self, id: &String) -> Result<&ariadne::Source<String>, impl std::fmt::Debug> {
        self.files
            .iter()
            .find(|(path, _)| path == id)
            .map(|(_, source)| source)
            .ok_or(MissingSource)
    }

    fn display<'a>(&self, id: &'a String) -> Option<impl std::fmt::Display + 'a> {
        Some(id)
    }
}

/// Fetch-error type for [`ContextSourceCache`]; ariadne only formats it
/// into an eprintln for sources no label references.
#[cfg(feature = "ariadne")]
struct MissingSource;

#[cfg(feature = "ariadne")]
impl std::fmt::Debug for MissingSource {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str("source not registered in the diagnostic's source context")
    }
}

impl DiagnosticMessage {
    /// Access the diagnostic message builder API.
    ///
    /// This is the recommended way to create diagnostic messages, as the builder API
    /// encodes tidyverse-style guidelines and makes it easy to construct well-structured
    /// error messages.
    ///
    /// # Example
    ///
    /// ```
    /// use quarto_error_reporting::{DiagnosticMessage, DiagnosticMessageBuilder};
    ///
    /// let error = DiagnosticMessageBuilder::error("Incompatible types")
    ///     .with_code("Q-1-2") // quarto-error-code-audit-ignore
    ///     .problem("Cannot combine date and datetime types")
    ///     .add_detail("`x` has type `date`")
    ///     .add_detail("`y` has type `datetime`")
    ///     .add_hint("Convert both to the same type?")
    ///     .build();
    /// ```
    pub fn builder() -> crate::builder::DiagnosticMessageBuilder {
        // This is just a convenience for accessing the builder type
        // Users should call DiagnosticMessageBuilder::error() etc directly
        crate::builder::DiagnosticMessageBuilder::error("")
    }

    /// Create a new diagnostic message with just a title and kind.
    ///
    /// Note: Consider using `DiagnosticMessage::builder()` instead for better structure.
    pub fn new(kind: DiagnosticKind, title: impl Into<String>) -> Self {
        Self {
            code: None,
            title: title.into(),
            kind,
            problem: None,
            details: Vec::new(),
            hints: Vec::new(),
            location: None,
        }
    }

    /// Create an error diagnostic.
    ///
    /// Note: Consider using `DiagnosticMessage::builder().error()` instead for better structure.
    pub fn error(title: impl Into<String>) -> Self {
        Self::new(DiagnosticKind::Error, title)
    }

    /// Create a warning diagnostic.
    ///
    /// Note: Consider using `DiagnosticMessage::builder().warning()` instead for better structure.
    pub fn warning(title: impl Into<String>) -> Self {
        Self::new(DiagnosticKind::Warning, title)
    }

    /// Create an info diagnostic.
    ///
    /// Note: Consider using `DiagnosticMessage::builder().info()` instead for better structure.
    pub fn info(title: impl Into<String>) -> Self {
        Self::new(DiagnosticKind::Info, title)
    }

    /// Set the error code.
    ///
    /// Error codes follow the format `Q-<subsystem>-<number>` (e.g., "Q-1-1").
    ///
    /// # Example
    ///
    /// ```
    /// use quarto_error_reporting::DiagnosticMessage;
    ///
    /// let msg = DiagnosticMessage::error("YAML Syntax Error")
    ///     .with_code("Q-1-1");
    /// ```
    pub fn with_code(mut self, code: impl Into<String>) -> Self {
        self.code = Some(code.into());
        self
    }

    /// Get the documentation URL for this error, if it has an error code.
    ///
    /// # Example
    ///
    /// Resolves the code against the installed [`CatalogProvider`]
    /// (`crate::catalog`); returns `None` when no catalog is installed, the
    /// code is unknown, or the entry has no docs URL.
    ///
    /// ```
    /// use quarto_error_reporting::DiagnosticMessage;
    ///
    /// let msg = DiagnosticMessage::error("Internal Error")
    ///     .with_code("Q-0-1");
    ///
    /// // `Some(url)` iff a catalog mapping "Q-0-1" (with a docs URL) is installed.
    /// let _ = msg.docs_url();
    /// ```
    pub fn docs_url(&self) -> Option<&str> {
        self.code
            .as_ref()
            .and_then(|code| crate::catalog::get_docs_url(code))
    }

    /// Render this diagnostic message as text following tidyverse style.
    ///
    /// This is a convenience method that uses default rendering options.
    /// For more control over rendering, use [`Self::to_text_with_options`].
    ///
    /// # Example
    ///
    /// ```
    /// use quarto_error_reporting::DiagnosticMessageBuilder;
    ///
    /// let msg = DiagnosticMessageBuilder::error("Invalid input")
    ///     .problem("Values must be numeric")
    ///     .add_detail("Found text in column 3")
    ///     .add_hint("Convert to numbers first?")
    ///     .build();
    /// let text = msg.to_text(None);
    /// assert!(text.contains("Error: Invalid input"));
    /// assert!(text.contains("Values must be numeric"));
    /// ```
    pub fn to_text(&self, ctx: Option<&quarto_source_map::SourceContext>) -> String {
        self.to_text_with_options(ctx, &TextRenderOptions::default())
    }

    /// Render this diagnostic message as text following tidyverse style with custom options.
    ///
    /// Format:
    /// ```text
    /// Error: title
    /// Problem statement here
    /// ✖ Error detail 1
    /// ✖ Error detail 2
    /// ℹ Info detail
    /// • Note detail
    /// ? Hint 1
    /// ? Hint 2
    /// ```
    ///
    /// # Example
    ///
    /// ```
    /// use quarto_error_reporting::{DiagnosticMessageBuilder, TextRenderOptions};
    ///
    /// let msg = DiagnosticMessageBuilder::error("Invalid input")
    ///     .problem("Values must be numeric")
    ///     .add_detail("Found text in column 3")
    ///     .add_hint("Convert to numbers first?")
    ///     .build();
    ///
    /// // Disable hyperlinks for snapshot testing
    /// let options = TextRenderOptions { enable_hyperlinks: false };
    /// let text = msg.to_text_with_options(None, &options);
    /// assert!(text.contains("Error: Invalid input"));
    /// ```
    pub fn to_text_with_options(
        &self,
        ctx: Option<&quarto_source_map::SourceContext>,
        options: &TextRenderOptions,
    ) -> String {
        self.to_text_with_renderer(ctx, options, None)
    }

    /// Like [`Self::to_text_with_options`], but explicitly selects which
    /// source-context snippet renderer draws the visual code excerpt.
    ///
    /// Pass `Some(SourceRenderer::Ariadne)` or
    /// `Some(SourceRenderer::AnnotateSnippets)` to force a specific
    /// renderer (the corresponding feature must be enabled), or `None`
    /// to use [`SourceRenderer::default_for_features`]. This is the seam
    /// for experimenting with diagnostic rendering styles without
    /// changing the rest of the API: only the source-excerpt block
    /// differs between renderers; the surrounding structured text
    /// (unlocated details, hints) is identical.
    ///
    /// When no renderer feature is enabled — or the diagnostic has no
    /// location / source context — this falls back to the structured
    /// tidyverse-style text block, exactly as [`Self::to_text_with_options`].
    ///
    /// # Example
    ///
    /// ```
    /// use quarto_error_reporting::{DiagnosticMessageBuilder, TextRenderOptions};
    ///
    /// let msg = DiagnosticMessageBuilder::error("Invalid input")
    ///     .problem("Values must be numeric")
    ///     .build();
    ///
    /// // `None` picks the default renderer for the enabled features.
    /// let text = msg.to_text_with_renderer(None, &TextRenderOptions::default(), None);
    /// assert!(text.contains("Invalid input"));
    /// ```
    pub fn to_text_with_renderer(
        &self,
        ctx: Option<&quarto_source_map::SourceContext>,
        options: &TextRenderOptions,
        renderer: Option<SourceRenderer>,
    ) -> String {
        use std::fmt::Write;

        let mut result = String::new();

        // Check if we have any location info that could be displayed in a
        // source excerpt. This includes the main diagnostic location OR
        // any detail with a location.
        let has_any_location =
            self.location.is_some() || self.details.iter().any(|d| d.location.is_some());

        // If we have location info and source context, render the source
        // excerpt with the selected (or default) renderer.
        let has_source_render = if let (true, Some(ctx_val)) = (has_any_location, ctx) {
            // Use main location if available, otherwise use first detail location
            let location = self
                .location
                .as_ref()
                .or_else(|| self.details.iter().find_map(|d| d.location.as_ref()));

            if let Some(loc) = location {
                if let Some(snippet_output) =
                    self.render_source_context(loc, ctx_val, options.enable_hyperlinks, renderer)
                {
                    result.push_str(&snippet_output);
                    true
                } else {
                    false
                }
            } else {
                false
            }
        } else {
            false
        };

        // If we don't have a source excerpt, show full tidyverse-style content.
        // If we do, only show details without locations and hints
        // (the renderer already shows: title, code, problem, and located details)
        if !has_source_render {
            // No source excerpt - show everything in tidyverse style

            // Title with kind prefix and error code (e.g., "Error [Q-1-1]: Invalid input")
            let kind_str = match self.kind {
                DiagnosticKind::Error => "Error",
                DiagnosticKind::Warning => "Warning",
                DiagnosticKind::Info => "Info",
                DiagnosticKind::Note => "Note",
            };
            if let Some(code) = &self.code {
                writeln!(result, "{} [{}]: {}", kind_str, code, self.title).unwrap();
            } else {
                writeln!(result, "{}: {}", kind_str, self.title).unwrap();
            }

            // Show location info if available (but no ariadne rendering)
            if let Some(loc) = &self.location {
                // Try to map with context if available
                if let Some(ctx) = ctx {
                    if let Some(mapped) = loc.map_offset(loc.start_offset(), ctx)
                        && let Some(file) = ctx.get_file(mapped.file_id)
                    {
                        writeln!(
                            result,
                            "  at {}:{}:{}",
                            file.path,
                            mapped.location.row + 1,
                            mapped.location.column + 1
                        )
                        .unwrap();
                    }
                } else {
                    // No context: show immediate location (1-indexed for display)
                    // Note: Without context, we can't get row/column from offsets
                    // We could map_offset with ctx to get Location, but ctx is None here
                    writeln!(result, "  at offset {}", loc.start_offset()).unwrap();
                }
            }

            // Problem statement (optional additional context)
            if let Some(problem) = &self.problem {
                writeln!(result, "{}", problem.as_str()).unwrap();
            }

            // All details with appropriate bullets
            for detail in &self.details {
                let bullet = match detail.kind {
                    DetailKind::Error => "✖",
                    DetailKind::Info => "ℹ",
                    DetailKind::Note | DetailKind::Faded => "•",
                };
                writeln!(result, "{} {}", bullet, detail.content.as_str()).unwrap();
            }

            // All hints
            for hint in &self.hints {
                writeln!(result, "ℹ {}", hint.as_str()).unwrap();
            }
        } else {
            // Have a source excerpt - only show details without locations and hints
            // (the renderer shows title, code, problem, and located details)

            // Details without locations (the source excerpt can't show these)
            for detail in &self.details {
                if detail.location.is_none() {
                    let bullet = match detail.kind {
                        DetailKind::Error => "✖",
                        DetailKind::Info => "ℹ",
                        DetailKind::Note | DetailKind::Faded => "•",
                    };
                    writeln!(result, "{} {}", bullet, detail.content.as_str()).unwrap();
                }
            }

            // All hints (ariadne doesn't show hints)
            for hint in &self.hints {
                writeln!(result, "ℹ {}", hint.as_str()).unwrap();
            }
        }

        result
    }

    /// Render this diagnostic message as a JSON value.
    ///
    /// Returns a structured JSON object with all fields:
    /// ```json
    /// {
    ///   "kind": "error",
    ///   "title": "Invalid input",
    ///   "code": "Q-1-2", // quarto-error-code-audit-ignore
    ///   "problem": "Values must be numeric",
    ///   "details": [{"kind": "error", "content": "Found text in column 3"}],
    ///   "hints": ["Convert to numbers first?"]
    /// }
    /// ```
    ///
    /// # Example
    ///
    /// ```
    /// use quarto_error_reporting::DiagnosticMessage;
    ///
    /// let msg = DiagnosticMessage::error("Something went wrong");
    /// let json = msg.to_json();
    /// assert_eq!(json["kind"], "error");
    /// assert_eq!(json["title"], "Something went wrong");
    /// ```
    pub fn to_json(&self) -> serde_json::Value {
        use serde_json::json;

        let kind_str = match self.kind {
            DiagnosticKind::Error => "error",
            DiagnosticKind::Warning => "warning",
            DiagnosticKind::Info => "info",
            DiagnosticKind::Note => "note",
        };

        let mut obj = json!({
            "kind": kind_str,
            "title": self.title,
        });

        // Add optional fields
        if let Some(code) = &self.code {
            obj["code"] = json!(code);
        }

        if let Some(problem) = &self.problem {
            obj["problem"] = problem.to_json();
        }

        if !self.details.is_empty() {
            let details: Vec<_> = self
                .details
                .iter()
                .map(|d| {
                    let detail_kind = match d.kind {
                        DetailKind::Error => "error",
                        DetailKind::Info => "info",
                        DetailKind::Note => "note",
                        DetailKind::Faded => "faded",
                    };
                    let mut detail_obj = json!({
                        "kind": detail_kind,
                        "content": d.content.to_json()
                    });
                    if let Some(location) = &d.location {
                        detail_obj["location"] = json!(location);
                    }
                    detail_obj
                })
                .collect();
            obj["details"] = json!(details);
        }

        if !self.hints.is_empty() {
            let hints: Vec<_> = self.hints.iter().map(|h| h.to_json()).collect();
            obj["hints"] = json!(hints);
        }

        if let Some(location) = &self.location {
            obj["location"] = json!(location); // quarto-source-map::SourceInfo is Serialize
        }

        obj
    }

    /// Snap a mapped byte range onto UTF-8 character boundaries within
    /// `content`, clamping it into the file and keeping `start <= end`.
    ///
    /// # Why the renderers need this
    ///
    /// Both source-context renderers slice the source by byte offset, and
    /// both **panic** — they do not merely mis-render — on an offset that
    /// falls inside a multi-byte character. Measured 2026-08-23 against the
    /// versions this crate's lockfile resolves (ariadne 0.6.0,
    /// annotate-snippets 0.12.16; the manifest declares only `0.6` and
    /// `0.12`), rendering `"text: <span>Ask AI \u{2728}</span>"` in which
    /// `\u{2728}` occupies bytes 19..22 and the offset each row names is
    /// placed at 20 or 21 — both interior to that character:
    ///
    /// | offset placed mid-character | ariadne | annotate-snippets |
    /// |---|---|---|
    /// | label start   | panics, `write.rs:84`  | panics, `renderer/source_map.rs:71` |
    /// | label end     | panics, `write.rs:102` | panics, `renderer/source_map.rs:98` |
    /// | report anchor | panics, `write.rs:267` | n/a — this crate passes no separate anchor |
    ///
    /// The clamping half guards two further aborts that boundary-snapping
    /// alone would not catch, measured the same way:
    ///
    /// | malformed range | ariadne | annotate-snippets |
    /// |---|---|---|
    /// | end past EOF          | tolerates (degraded excerpt) | panics, `renderer/source_map.rs:158` |
    /// | inverted, `end < start` | panics, `lib.rs:145` (a plain `assert!`, so also in release) | `renderer/render.rs:1394` subtracts unchecked: panics only where overflow checks are on (debug/test), wraps silently in a default release build |
    ///
    /// Printing a diagnostic must never be able to kill a render, so we
    /// normalize here rather than trusting the input.
    ///
    /// # What is actually load-bearing, and when
    ///
    /// **How much the snapping half is doing depends on what
    /// `quarto-source-map` resolves to.** Since 0.1.2,
    /// `FileInformation::offset_to_location` returns the *floored* offset
    /// (`src/file_info.rs:116-125`:
    /// `safe_offset` walks left onto a character boundary and is returned as
    /// `Location.offset`; 0.1.0 and 0.1.1 computed `safe_offset` but returned
    /// the raw `offset`). **This crate's declared floor is still
    /// `quarto-source-map = "0.1.0"` (`Cargo.toml:28`)**, so a consumer that
    /// resolves 0.1.0 or 0.1.1 — an existing lock, another graph member
    /// pinning `=0.1.1`, `-Z minimal-versions` — gets no upstream floor at
    /// all, and for that build the snap below is the only guard rather than
    /// a backstop. A published library's lockfile does not constrain its
    /// consumers; downstream, q2 had to raise its own floor, because this
    /// manifest does not force it.
    ///
    /// On a resolution that *does* have the floor, it covers the mapping
    /// path broadly: every value-producing path of `SourceInfo::map_offset`
    /// runs through `offset_to_location` (`src/mapping.rs:38`; `Generated`
    /// yields `None` instead), and all three call sites here are fed
    /// `map_offset` results — the span shared by the report anchor and the
    /// main label, and the detail-label spans, both in
    /// `render_ariadne_source_context`; and the `clamp` closure in
    /// `render_annotate_snippets_source_context`.
    ///
    /// So a mid-character offset can no longer reach a renderer through the
    /// mapping path **when the span resolves within a single file**. The
    /// cross-file `Concat` shape described below is an exception for the
    /// snapping half as well as the clamping half: `map_offset` floors an
    /// offset against the piece's *own* file content (`src/mapping.rs:25-38`),
    /// which says nothing about where character boundaries fall in
    /// `content`.
    ///
    /// The snap is therefore kept deliberately. It is the only guard on a
    /// pre-0.1.2 resolution, a live guard on the cross-file shape, and the
    /// backstop if the upstream floor regresses or a future caller hands us
    /// raw, unmapped offsets.
    ///
    /// **The clamping half is still live.** `end_mapped` is not always the
    /// mapped image of this span's end: when `map_offset(length())` fails,
    /// both renderer paths substitute `map_offset(length() - 1)` and then
    /// fall back to `start_mapped` (the `length() - 1` fallback at
    /// `:842-852` in `render_ariadne_source_context` and `:1038-1047` in
    /// `render_annotate_snippets_source_context`, line numbers as of 0.2.2).
    /// And `content` belongs to `root_file_id()`, which for a `Concat` is
    /// the file of the *first* piece that resolves to one
    /// (`quarto-source-map`'s `src/source_info.rs:549-560`), while
    /// `map_offset` resolves into whichever piece contains the offset.
    /// Structurally, then — a `Concat` spanning two files — the two ends
    /// can resolve into different files, whose offsets are neither ordered
    /// with respect to each other nor bounded by `content.len()`. (That
    /// shape is permitted by the types; unlike the panics tabled above it
    /// has not been exercised here.) `start.min(len)`, `end.min(len)` and
    /// `.max(s)` reduce any of that to an in-range, non-inverted span.
    ///
    /// # Behaviour
    ///
    /// The range is widened, not truncated: `start` floors to the start of
    /// the character containing it and `end` ceils to the end of the
    /// character containing it, so the highlight covers whole characters
    /// and can never invert. This differs from the upstream floor, which
    /// walks an end offset *left* rather than widening it; the two agree
    /// whenever only the start is misaligned.
    ///
    /// # Coverage
    ///
    /// The direct unit coverage is `snap_span_widens_to_whole_characters`.
    /// The two `..._renders_diagnostic_with_originally_mid_character_span`
    /// tests are end-to-end smoke checks only, and do not bind to this
    /// helper: commit `5e48166`, *"Re-anchor the mid-character-span crash
    /// tests after the 0.1.3 floor"*, re-anchored them and records why. (It
    /// is a commit of PR #5, which was squash-merged as `87f1d38`, so it is
    /// reachable through that PR rather than from `main`'s history.)
    ///
    /// Downstream, the quarto-dev/q2 repository is adding an end-to-end pin
    /// for the founding crash, under `crates/quarto/tests/integration/`: it
    /// drives the real `q2` binary over a website project whose
    /// `_quarto.yml` navbar entry embeds `\u{2728}` **mid-string**, as
    /// `text: '<span id="x">Ask AI \u{2728}</span>'`, and asserts a clean
    /// exit alongside the expected caret columns. The trailing `</span>` is
    /// load-bearing — one asserted caret falls past it — so it is the same
    /// shape as this crate's own tests, where `\u{2728}` sits at bytes
    /// 19..22 and `</span>` at 22..29. That pin will redden only if q2's
    /// mapping regresses *and* both this snap and the upstream floor are
    /// gone: it guards the combination, not this helper on its own.
    #[cfg(any(feature = "ariadne", feature = "annotate-snippets"))]
    fn snap_span_to_char_boundaries(
        content: &str,
        start: usize,
        end: usize,
    ) -> std::ops::Range<usize> {
        let len = content.len();
        let mut s = start.min(len);
        let mut e = end.min(len).max(s);
        while s > 0 && !content.is_char_boundary(s) {
            s -= 1;
        }
        while e < len && !content.is_char_boundary(e) {
            e += 1;
        }
        s..e
    }

    /// Dispatch to the selected source-context renderer.
    ///
    /// `renderer` of `None` resolves to [`SourceRenderer::default_for_features`].
    /// Returns `None` when no renderer is available (no renderer feature
    /// enabled) or the chosen renderer could not draw the excerpt (e.g.
    /// the file content is unavailable — common in WASM), in which case
    /// the caller falls back to the structured text block.
    #[cfg_attr(
        not(any(feature = "ariadne", feature = "annotate-snippets")),
        allow(unused_variables)
    )]
    fn render_source_context(
        &self,
        main_location: &quarto_source_map::SourceInfo,
        ctx: &quarto_source_map::SourceContext,
        enable_hyperlinks: bool,
        renderer: Option<SourceRenderer>,
    ) -> Option<String> {
        let renderer = renderer.or_else(SourceRenderer::default_for_features)?;
        match renderer {
            #[cfg(feature = "ariadne")]
            SourceRenderer::Ariadne => {
                self.render_ariadne_source_context(main_location, ctx, enable_hyperlinks)
            }
            #[cfg(feature = "annotate-snippets")]
            SourceRenderer::AnnotateSnippets => {
                self.render_annotate_snippets_source_context(main_location, ctx, enable_hyperlinks)
            }
        }
    }

    /// Wrap a file path with OSC 8 ANSI hyperlink codes for clickable terminal links.
    ///
    /// OSC 8 is a terminal escape sequence that creates clickable hyperlinks:
    /// `\x1b]8;;URI\x1b\\TEXT\x1b\\`
    ///
    /// `path` is always the *displayed* text (for a virtual file, the
    /// cell-qualified label). `link_target` is the disk path the hyperlink
    /// opens — normally `path` itself when it exists on disk, or the owning
    /// notebook for a virtual file that carries a `FileOrigin`. `None`
    /// disables the link.
    ///
    /// A link is emitted only if:
    /// - Hyperlinks are enabled via the `enable_hyperlinks` parameter
    /// - `link_target` is `Some` and can be canonicalized
    ///
    /// The `url` crate handles:
    /// - Platform differences (Windows drive letters vs Unix paths)
    /// - Percent-encoding of special characters
    /// - Proper file:// URL construction
    ///
    /// Line and column numbers are added to the URL as a fragment identifier
    /// (e.g., `file:///path#line:column`), which is supported by iTerm2 3.4+
    /// and other terminal emulators for opening files at specific positions.
    /// The fragment is emitted only when the position belongs to the linked
    /// file itself (`link_target == path`); an origin link opens a different
    /// file whose coordinates we cannot speak to.
    ///
    /// Returns the wrapped path if conditions are met, otherwise returns the original path.
    ///
    /// Only used by the ariadne renderer (annotate-snippets has no OSC 8 support).
    #[cfg(all(feature = "ariadne", not(target_family = "wasm")))]
    fn wrap_path_with_hyperlink(
        path: &str,
        link_target: Option<&str>,
        line: Option<usize>,
        column: Option<usize>,
        enable_hyperlinks: bool,
    ) -> String {
        // Don't add hyperlinks if disabled (e.g., for snapshot testing)
        if !enable_hyperlinks {
            return path.to_string();
        }

        let Some(target) = link_target else {
            return path.to_string();
        };

        // Canonicalize to absolute path
        let abs_path = match std::fs::canonicalize(target) {
            Ok(p) => p,
            Err(_) => return path.to_string(), // Can't canonicalize, skip hyperlink
        };

        // Convert to file:// URL (handles Windows/Unix + percent-encoding)
        let mut file_url = match url::Url::from_file_path(Self::plain_absolute_path(abs_path)) {
            Ok(url) => url.as_str().to_string(),
            Err(_) => return path.to_string(), // Conversion failed, skip hyperlink
        };

        // Add line and column as fragment identifier (e.g., #line:column)
        // This format is supported by iTerm2 3.4+ semantic history — but
        // only when the position belongs to the linked file itself. An
        // origin link opens a *different* file (the owning notebook), and
        // the diagnostic's coordinates are relative to the virtual file.
        if target == path
            && let Some(line_num) = line
        {
            match column {
                Some(col_num) => file_url.push_str(&format!("#{}:{}", line_num, col_num)),
                None => file_url.push_str(&format!("#{}", line_num)),
            }
        }

        // Wrap with OSC 8 codes: \x1b]8;;URI\x1b\\TEXT\x1b]8;;\x1b\\
        format!("\x1b]8;;{}\x1b\\{}\x1b]8;;\x1b\\", file_url, path)
    }

    /// The absolute path to hand to `url::Url::from_file_path`.
    ///
    /// Gated with `wrap_path_with_hyperlink` (its only production caller);
    /// unit tests call it so the expected URL is computed the same way the
    /// production renderer does. Windows: `fs::canonicalize` returns
    /// verbatim (`\\?\C:\…`) paths, which the url crate renders as
    /// `file://?/C:/…` — no terminal can open that. Strip the verbatim
    /// prefix (`\\?\UNC\` → `\\`) to get the plain absolute form.
    #[cfg(all(feature = "ariadne", not(target_family = "wasm")))]
    fn plain_absolute_path(p: std::path::PathBuf) -> std::path::PathBuf {
        #[cfg(windows)]
        {
            let s = p.as_os_str().to_string_lossy();
            if let Some(rest) = s.strip_prefix(r#"\\?\UNC\"#) {
                return std::path::PathBuf::from(format!(r"\\{rest}"));
            }
            if let Some(rest) = s.strip_prefix(r#"\\?\"#) {
                return std::path::PathBuf::from(rest);
            }
        }
        p
    }

    /// The disk path a file's label should hyperlink to: the owning
    /// notebook for a virtual file with a `FileOrigin`, the file itself
    /// when it exists on disk, `None` otherwise (ephemeral or missing).
    #[cfg(feature = "ariadne")]
    fn hyperlink_target(file: &quarto_source_map::SourceFile) -> Option<&str> {
        match file.metadata.origin.as_ref() {
            Some(quarto_source_map::FileOrigin::NotebookCell { notebook_path, .. }) => {
                Some(notebook_path)
            }
            None => std::path::Path::new(&file.path)
                .exists()
                .then_some(file.path.as_str()),
        }
    }

    /// WASM version: hyperlinks don't make sense in WASM environments (no file system).
    /// Just return the path unmodified.
    #[cfg(all(feature = "ariadne", target_family = "wasm"))]
    fn wrap_path_with_hyperlink(
        path: &str,
        _link_target: Option<&str>,
        _line: Option<usize>,
        _column: Option<usize>,
        _enable_hyperlinks: bool,
    ) -> String {
        path.to_string()
    }

    /// One-line textual location for a span whose two ends resolve into
    /// different `Concat` pieces, rendered in place of a snippet. A
    /// cross-piece span cannot be drawn as one excerpt, and clamping it
    /// into either piece's content would point at the wrong file.
    #[cfg(any(feature = "ariadne", feature = "annotate-snippets"))]
    fn format_cross_piece_location(
        start: &quarto_source_map::MappedLocation,
        end: &quarto_source_map::MappedLocation,
        ctx: &quarto_source_map::SourceContext,
    ) -> String {
        let name = |file_id| match ctx.get_file(file_id) {
            Some(file) => file.path.clone(),
            None => "<unknown file>".to_string(),
        };
        // Line and column numbers are 1-indexed for display (Location uses 0-indexed)
        format!(
            "  --> {}:{}:{} (spans through {}:{}:{})\n",
            name(start.file_id),
            start.location.row + 1,
            start.location.column + 1,
            name(end.file_id),
            end.location.row + 1,
            end.location.column + 1,
        )
    }

    /// Render source context using ariadne (private helper for to_text).
    ///
    /// This produces the visual source code snippet with highlighting.
    /// The tidyverse-style problem/details/hints are added separately by to_text().
    #[cfg(feature = "ariadne")]
    fn render_ariadne_source_context(
        &self,
        main_location: &quarto_source_map::SourceInfo,
        ctx: &quarto_source_map::SourceContext,
        enable_hyperlinks: bool,
    ) -> Option<String> {
        use ariadne::{Color, Config, IndexType, Label, Report, ReportKind, Source};

        // Mirror of ariadne's private `Config::unimportant_color()` from
        // ariadne 0.6.0 (`src/lib.rs:543`). We use this for `DetailKind::Faded`
        // labels so they blend visually with characters that fall outside any
        // label. Bump this constant if the ariadne dependency upgrades and
        // changes the colour.
        const ARIADNE_UNIMPORTANT_COLOR: Color = Color::Fixed(249);

        // The report's file is the one the span *starts* in. For a
        // multi-piece `Concat` (q2's per-cell ipynb virtual files),
        // `root_file_id()` is the first piece's file — wrong for a
        // diagnostic rooted in a later piece — while `map_offset`
        // resolves piece-aware. `start_mapped.file_id` is correct for
        // both shapes (and identical to `root_file_id()` on a
        // single-file source).
        let start_mapped = main_location.map_offset(0, ctx)?;
        let file_id = start_mapped.file_id;

        let file = ctx.get_file(file_id)?;

        // Get file content: use stored content for ephemeral files, or read from disk.
        // In WASM (and any host with no real filesystem) the disk read fails with
        // "operation not supported on this platform"; the only graceful response is
        // to drop the source-context snippet. The diagnostic's code, message, and
        // hints still surface — only the Ariadne visual is unavailable.
        let content = match &file.content {
            Some(c) => c.clone(),
            None => match std::fs::read_to_string(&file.path) {
                Ok(s) => s,
                Err(_) => return None,
            },
        };

        // For end offset, try the full length first. If that fails (e.g., when the span
        // extends past EOF), clamp to the last valid position. This handles edge cases
        // like errors pointing to EOF or diagnostics with off-by-one end offsets.
        let end_mapped = main_location
            .map_offset(main_location.length(), ctx)
            .or_else(|| {
                // Clamp: if length() fails, try length()-1, which should be the last valid byte
                if main_location.length() > 0 {
                    main_location.map_offset(main_location.length() - 1, ctx)
                } else {
                    None
                }
            })
            .unwrap_or_else(|| start_mapped.clone());

        // A span whose two ends resolve into different pieces cannot be
        // drawn as one snippet — rendering it against either piece's
        // content would clamp it silently into the wrong file. Label both
        // ends textually instead.
        if end_mapped.file_id != start_mapped.file_id {
            return Some(Self::format_cross_piece_location(
                &start_mapped,
                &end_mapped,
                ctx,
            ));
        }

        // Create display path with OSC 8 hyperlink for clickable file paths.
        // A virtual cell file (FileOrigin) links its owning notebook; a real
        // file links itself; an ephemeral file with no origin stays unlinked.
        let link_target = Self::hyperlink_target(file);
        // Line and column numbers are 1-indexed for display (start_mapped.location uses 0-indexed)
        let line = Some(start_mapped.location.row + 1);
        let column = Some(start_mapped.location.column + 1);
        let display_path = Self::wrap_path_with_hyperlink(
            &file.path,
            link_target,
            line,
            column,
            enable_hyperlinks,
        );

        // Determine report kind and color
        let (report_kind, main_color) = match self.kind {
            DiagnosticKind::Error => (ReportKind::Error, Color::Red),
            DiagnosticKind::Warning => (ReportKind::Warning, Color::Yellow),
            DiagnosticKind::Info => (ReportKind::Advice, Color::Cyan),
            DiagnosticKind::Note => (ReportKind::Advice, Color::Blue),
        };

        // Snap once, up front: every offset handed to ariadne below (the
        // report anchor and the main label) must be char-boundary safe.
        let main_span = Self::snap_span_to_char_boundaries(
            &content,
            start_mapped.location.offset,
            end_mapped.location.offset,
        );

        // Build the report using the mapped offset for proper line:column display
        // IMPORTANT: Use IndexType::Byte because our offsets are byte offsets, not character offsets
        let mut report = Report::build(
            report_kind,
            (display_path.clone(), main_span.start..main_span.start),
        )
        .with_config(Config::default().with_index_type(IndexType::Byte));

        // Add title with error code
        if let Some(code) = &self.code {
            report = report.with_message(format!("[{}] {}", code, self.title));
        } else {
            report = report.with_message(&self.title);
        }

        // Add main location label using the snapped span computed above.
        let main_message = if let Some(problem) = &self.problem {
            problem.as_str()
        } else {
            &self.title
        };

        // Set `with_order` on every label using its end offset. Ariadne
        // groups labels by source and starts a new group whenever a label's
        // end line is *before* the previous label's end line. Without an
        // explicit order, multi-line main labels and per-line "padding"
        // detail labels (used to defeat Ariadne's middle-line elision) end
        // up in separate groups, producing a duplicated snippet block.
        // Sorting by end offset puts the smaller-line labels first so the
        // grouping algorithm extends rather than splits.
        report = report.with_label(
            Label::new((display_path.clone(), main_span.clone()))
                .with_message(main_message)
                .with_color(main_color)
                .with_order(main_span.end as i32),
        );

        // Add detail locations as additional labels (only those with locations).
        // Details rooted (start and end) in the report's file stay inline
        // labels; details rooted wholly in another piece are collected and
        // appended after the loop as labels in their own file's source
        // section. A detail whose own span straddles pieces is not
        // representable as one inline label and is skipped (the main-span
        // cross-piece label above covers the common cross-cell shape).
        let mut cache_files = vec![(display_path.clone(), Source::from(content.clone()))];
        let mut foreign_labels: Vec<(String, std::ops::Range<usize>, Option<String>, Color)> =
            Vec::new();
        for detail in &self.details {
            if let Some(detail_loc) = &detail.location {
                // Map detail offsets to original file positions
                // map_offset expects relative offsets (0 = start of SourceInfo's range)
                let (Some(detail_start), Some(detail_end)) = (
                    detail_loc.map_offset(0, ctx),
                    detail_loc.map_offset(detail_loc.length(), ctx),
                ) else {
                    continue;
                };
                let detail_color = match detail.kind {
                    DetailKind::Error => Color::Red,
                    DetailKind::Info => Color::Cyan,
                    DetailKind::Note => Color::Blue,
                    // Match Ariadne's unimportant colour so faded
                    // labels visually disappear into the surrounding
                    // unlabelled text.
                    DetailKind::Faded => ARIADNE_UNIMPORTANT_COLOR,
                };

                if detail_start.file_id == detail_end.file_id && detail_start.file_id != file_id {
                    // Wholly inside another piece: render it there as its
                    // own source section instead of silently dropping it.
                    let Some(detail_file) = ctx.get_file(detail_start.file_id) else {
                        continue;
                    };
                    let Some(detail_content) = detail_file
                        .content
                        .clone()
                        .or_else(|| std::fs::read_to_string(&detail_file.path).ok())
                    else {
                        continue;
                    };
                    // File-level id (no per-line hyperlink fragment) so
                    // several details in one foreign file share a source
                    // section; ariadne derives the header line:column from
                    // the label span itself.
                    let foreign_display = Self::wrap_path_with_hyperlink(
                        &detail_file.path,
                        Self::hyperlink_target(detail_file),
                        None,
                        None,
                        enable_hyperlinks,
                    );
                    let detail_span = Self::snap_span_to_char_boundaries(
                        &detail_content,
                        detail_start.location.offset,
                        detail_end.location.offset,
                    );
                    if !cache_files.iter().any(|(path, _)| *path == foreign_display) {
                        cache_files.push((foreign_display.clone(), Source::from(detail_content)));
                    }
                    let message = (!detail.content.as_str().is_empty())
                        .then(|| detail.content.as_str().to_string());
                    foreign_labels.push((foreign_display, detail_span, message, detail_color));
                    continue;
                }

                if detail_start.file_id == file_id && detail_end.file_id == file_id {
                    let detail_span = Self::snap_span_to_char_boundaries(
                        &content,
                        detail_start.location.offset,
                        detail_end.location.offset,
                    );
                    // Empty-content details exist purely to force Ariadne
                    // to display a line that would otherwise be elided
                    // inside a multi-line span. Leaving the label's
                    // message at None makes Ariadne skip drawing the
                    // `╰── ...` arrow row underneath, so the source line
                    // appears clean.
                    let mut label = Label::new((display_path.clone(), detail_span.clone()))
                        .with_color(detail_color)
                        .with_order(detail_span.end as i32);
                    if !detail.content.as_str().is_empty() {
                        label = label.with_message(detail.content.as_str());
                    }
                    report = report.with_label(label);
                }
            }
        }

        // Foreign-piece labels sort after every same-file label so
        // ariadne's order-keyed grouping draws the report's file first,
        // intact; no realistic same-file span end reaches this base.
        for (foreign_order, (path, span, message, color)) in (1_000_000i32..).zip(foreign_labels) {
            let mut label = Label::new((path, span))
                .with_color(color)
                .with_order(foreign_order);
            if let Some(message) = message {
                label = label.with_message(message);
            }
            report = report.with_label(label);
        }

        // Render to string
        let report = report.finish();
        let mut output = Vec::new();
        report
            .write(ContextSourceCache { files: cache_files }, &mut output)
            .ok()?;

        let output_str = String::from_utf8(output).ok()?;

        // Post-process to extend hyperlinks to include line:column numbers
        // Ariadne adds :line:column after our hyperlinked path, so we need to
        // move the hyperlink end marker to include those numbers. Only for
        // self-links: an origin link opens a different file, and the
        // appended coordinates are relative to the virtual file — they
        // must not leak into the notebook URL.
        if enable_hyperlinks && link_target == Some(file.path.as_str()) {
            Some(Self::extend_hyperlink_to_include_line_column(
                &output_str,
                &file.path,
            ))
        } else {
            Some(output_str)
        }
    }

    /// Render source context using [`annotate-snippets`](https://crates.io/crates/annotate-snippets),
    /// the rust-lang toolchain's diagnostic style (private helper for to_text).
    ///
    /// Mirrors [`Self::render_ariadne_source_context`]'s offset-mapping
    /// logic but emits the `error[CODE]: …` / `-->` / gutter-bar look.
    /// Differences from the ariadne path, by design:
    ///
    /// - The error code is rendered natively via `Title::id` (e.g.
    ///   `error[Q-2-5]: …`) rather than prefixed into the message.
    /// - There are **no terminal hyperlinks** — annotate-snippets has no
    ///   OSC 8 support, so `_enable_hyperlinks` is ignored.
    /// - Detail labels are all rendered as `Context` annotations
    ///   (annotate-snippets has no per-label color), so the `DetailKind`
    ///   color distinction and the `Faded` blend are not reproduced.
    /// - Empty-content "padding" details (an ariadne workaround for
    ///   mid-span line elision) are skipped: annotate-snippets folds
    ///   unannotated lines natively, which is the look we want here.
    #[cfg(feature = "annotate-snippets")]
    fn render_annotate_snippets_source_context(
        &self,
        main_location: &quarto_source_map::SourceInfo,
        ctx: &quarto_source_map::SourceContext,
        _enable_hyperlinks: bool,
    ) -> Option<String> {
        use annotate_snippets::{AnnotationKind, Level, Renderer, Snippet};

        // The report's file is the one the span *starts* in — same
        // contract as the ariadne path: `root_file_id()` is the first
        // `Concat` piece's file, wrong for a diagnostic rooted in a
        // later piece, while `map_offset` resolves piece-aware.
        let start_mapped = main_location.map_offset(0, ctx)?;
        let file_id = start_mapped.file_id;

        // A span whose two ends resolve into different pieces cannot be
        // drawn as one snippet; label both ends textually rather than
        // silently clamping into one piece's content.
        let end_mapped = main_location
            .map_offset(main_location.length(), ctx)
            .or_else(|| {
                if main_location.length() > 0 {
                    main_location.map_offset(main_location.length() - 1, ctx)
                } else {
                    None
                }
            })
            .unwrap_or_else(|| start_mapped.clone());
        if end_mapped.file_id != start_mapped.file_id {
            return Some(Self::format_cross_piece_location(
                &start_mapped,
                &end_mapped,
                ctx,
            ));
        }

        let file = ctx.get_file(file_id)?;
        let content = match &file.content {
            Some(c) => c.clone(),
            None => std::fs::read_to_string(&file.path).ok()?,
        };
        // Clamp a mapped byte range into the source, keeping start <= end
        // and both ends on UTF-8 character boundaries (annotate-snippets
        // panics on a mid-character offset just as ariadne does).
        let clamp = |start: usize, end: usize| -> std::ops::Range<usize> {
            Self::snap_span_to_char_boundaries(&content, start, end)
        };

        let main_span = clamp(start_mapped.location.offset, end_mapped.location.offset);

        let level = match self.kind {
            DiagnosticKind::Error => Level::ERROR,
            DiagnosticKind::Warning => Level::WARNING,
            DiagnosticKind::Info => Level::INFO,
            DiagnosticKind::Note => Level::NOTE,
        };

        // Primary label message: the problem statement, else the title.
        let main_message = match &self.problem {
            Some(problem) => problem.as_str(),
            None => self.title.as_str(),
        };

        let mut snippet = Snippet::source(content.as_str())
            .path(file.path.as_str())
            .line_start(1)
            .annotation(AnnotationKind::Primary.span(main_span).label(main_message));

        // Details rooted (start and end) in the report's file become
        // Context annotations; details rooted wholly in another piece
        // render as their own snippet element below the main one instead
        // of being silently dropped. A detail whose own span straddles
        // pieces is not representable as one annotation and is skipped
        // (the main-span cross-piece label covers the common shape).
        let mut foreign: Vec<(String, String, std::ops::Range<usize>, &str)> = Vec::new();
        for detail in &self.details {
            // Skip empty-content padding details (see the doc comment).
            if detail.content.as_str().is_empty() {
                continue;
            }
            let Some(detail_loc) = &detail.location else {
                continue;
            };
            let (Some(detail_start), Some(detail_end)) = (
                detail_loc.map_offset(0, ctx),
                detail_loc.map_offset(detail_loc.length(), ctx),
            ) else {
                continue;
            };
            if detail_start.file_id == detail_end.file_id && detail_start.file_id != file_id {
                let Some(detail_file) = ctx.get_file(detail_start.file_id) else {
                    continue;
                };
                let Some(detail_content) = detail_file
                    .content
                    .clone()
                    .or_else(|| std::fs::read_to_string(&detail_file.path).ok())
                else {
                    continue;
                };
                let detail_span = Self::snap_span_to_char_boundaries(
                    &detail_content,
                    detail_start.location.offset,
                    detail_end.location.offset,
                );
                foreign.push((
                    detail_file.path.clone(),
                    detail_content,
                    detail_span,
                    detail.content.as_str(),
                ));
                continue;
            }
            if detail_start.file_id == file_id && detail_end.file_id == file_id {
                let detail_span = clamp(detail_start.location.offset, detail_end.location.offset);
                snippet = snippet.annotation(
                    AnnotationKind::Context
                        .span(detail_span)
                        .label(detail.content.as_str()),
                );
            }
        }

        // Build the titled group; render the error code natively via `id`,
        // then append each foreign-piece detail as its own snippet element
        // (annotate-snippets draws one excerpt per element, each with its
        // own `--> path` header).
        let mut title = level.primary_title(self.title.as_str());
        if let Some(code) = &self.code {
            title = title.id(code.as_str());
        }
        let mut group = title.element(snippet);
        // Iterate by reference: the built `Snippet`s borrow `path`/`content`,
        // so the owned strings must stay alive in `foreign` until `render`.
        for (path, content, span, message) in &foreign {
            let extra = Snippet::source(content.as_str())
                .path(path)
                .line_start(1)
                .annotation(AnnotationKind::Context.span(span.clone()).label(*message));
            group = group.element(extra);
        }

        // `Renderer::render` returns text with no trailing newline, but
        // `to_text` appends unlocated details and hints directly after the
        // excerpt with `writeln!`. Match the ariadne path (which ends in a
        // newline) so those lines don't glue onto the last source row.
        let mut rendered = Renderer::styled().render(&[group]);
        if !rendered.ends_with('\n') {
            rendered.push('\n');
        }
        Some(rendered)
    }

    /// Extend OSC 8 hyperlinks to include the :line:column suffix that ariadne adds.
    ///
    /// Ariadne formats file references as `path:line:column`, but since we wrap the path
    /// with OSC 8 codes, the structure becomes: `[hyperlink:path]:line:column`
    /// We want: `[hyperlink:path:line:column]`
    ///
    /// This function finds patterns like `path]8;;\:line:column` and moves the hyperlink
    /// end marker to after the line:column part.
    #[cfg(feature = "ariadne")]
    fn extend_hyperlink_to_include_line_column(output: &str, original_path: &str) -> String {
        // Pattern: original_path followed by ]8;;\ then :numbers:numbers
        // We want to move the ]8;;\ to after the :numbers:numbers part
        let end_marker = "\x1b]8;;\x1b\\";
        let search_pattern = format!("{}{}", original_path, end_marker);

        let mut result = output.to_string();
        while let Some(pos) = result.find(&search_pattern) {
            let after_marker = pos + search_pattern.len();
            // Check if what follows is :line:column pattern
            if let Some(rest) = result.get(after_marker..) {
                // Match :digits:digits pattern
                if let Some(colon_end) = Self::find_line_column_end(rest) {
                    // Move the end marker to after the :line:column
                    let before = &result[..pos + original_path.len()];
                    let line_col = &rest[..colon_end];
                    let after = &rest[colon_end..];
                    result = format!("{}{}{}{}", before, line_col, end_marker, after);
                    continue;
                }
            }
            break;
        }
        result
    }

    /// Find the end position of a :line:column pattern at the start of the string.
    /// Returns None if the pattern doesn't match.
    #[cfg(feature = "ariadne")]
    fn find_line_column_end(s: &str) -> Option<usize> {
        let bytes = s.as_bytes();
        if bytes.is_empty() || bytes[0] != b':' {
            return None;
        }

        let mut pos = 1;
        // Read digits for line number
        while pos < bytes.len() && bytes[pos].is_ascii_digit() {
            pos += 1;
        }
        if pos == 1 || pos >= bytes.len() || bytes[pos] != b':' {
            return None; // No digits or no second colon
        }

        pos += 1; // Skip second colon
        let col_start = pos;
        // Read digits for column number
        while pos < bytes.len() && bytes[pos].is_ascii_digit() {
            pos += 1;
        }
        if pos == col_start {
            return None; // No digits for column
        }

        Some(pos)
    }
}

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

    #[test]
    fn test_diagnostic_kind() {
        assert_eq!(DiagnosticKind::Error, DiagnosticKind::Error);
        assert_ne!(DiagnosticKind::Error, DiagnosticKind::Warning);
    }

    #[test]
    fn test_message_content_from_str() {
        let content: MessageContent = "test".into();
        assert_eq!(content.as_str(), "test");
    }

    #[test]
    fn test_diagnostic_message_new() {
        let msg = DiagnosticMessage::new(DiagnosticKind::Error, "Test error");
        assert_eq!(msg.title, "Test error");
        assert_eq!(msg.kind, DiagnosticKind::Error);
        assert!(msg.code.is_none());
        assert!(msg.problem.is_none());
        assert!(msg.details.is_empty());
        assert!(msg.hints.is_empty());
    }

    #[test]
    fn test_diagnostic_message_constructors() {
        let error = DiagnosticMessage::error("Error");
        assert_eq!(error.kind, DiagnosticKind::Error);
        assert!(error.code.is_none());

        let warning = DiagnosticMessage::warning("Warning");
        assert_eq!(warning.kind, DiagnosticKind::Warning);

        let info = DiagnosticMessage::info("Info");
        assert_eq!(info.kind, DiagnosticKind::Info);
    }

    #[test]
    fn test_with_code() {
        let msg = DiagnosticMessage::error("Test error").with_code("Q-1-1");
        assert_eq!(msg.code, Some("Q-1-1".to_string()));
    }

    // The positive case — `docs_url()` for a real code resolves to the
    // quarto.org URL — moved to `quarto-error-catalog`'s integration tests,
    // where the `Q-*` catalog is installed. Here we only cover the
    // catalog-free cases (no code / unknown code → `None`), which hold
    // regardless of whether a catalog is installed.

    #[test]
    fn test_docs_url_without_code() {
        let msg = DiagnosticMessage::error("Test error");
        assert!(msg.docs_url().is_none());
    }

    #[test]
    fn test_docs_url_invalid_code() {
        let msg = DiagnosticMessage::error("Test error").with_code("Q-999-999"); // quarto-error-code-audit-ignore
        assert!(msg.docs_url().is_none());
    }

    #[test]
    fn test_to_text_simple_error() {
        let msg = DiagnosticMessage::error("Something went wrong");
        assert_eq!(msg.to_text(None), "Error: Something went wrong\n");
    }

    #[test]
    fn test_to_text_with_code() {
        let msg = DiagnosticMessage::error("Something went wrong").with_code("Q-1-1");
        assert_eq!(msg.to_text(None), "Error [Q-1-1]: Something went wrong\n");
    }

    #[test]
    fn test_to_text_full_message() {
        use crate::builder::DiagnosticMessageBuilder;

        let msg = DiagnosticMessageBuilder::error("Invalid input")
            .problem("Values must be numeric")
            .add_detail("Found text in column 3")
            .add_info("Columns should contain only numbers")
            .add_hint("Convert to numbers first?")
            .build();

        let text = msg.to_text(None);
        assert!(text.contains("Error: Invalid input"));
        assert!(text.contains("Values must be numeric"));
        assert!(text.contains("✖ Found text in column 3"));
        assert!(text.contains("ℹ Columns should contain only numbers"));
        assert!(text.contains("ℹ Convert to numbers first?"));
    }

    #[test]
    fn test_to_json_simple() {
        let msg = DiagnosticMessage::error("Something went wrong");
        let json = msg.to_json();

        assert_eq!(json["kind"], "error");
        assert_eq!(json["title"], "Something went wrong");
        assert!(json.get("code").is_none());
        assert!(json.get("problem").is_none());
    }

    #[test]
    fn test_to_json_with_code() {
        let msg = DiagnosticMessage::error("Something went wrong").with_code("Q-1-1");
        let json = msg.to_json();

        assert_eq!(json["kind"], "error");
        assert_eq!(json["title"], "Something went wrong");
        assert_eq!(json["code"], "Q-1-1");
    }

    #[test]
    fn test_to_json_full_message() {
        use crate::builder::DiagnosticMessageBuilder;

        let msg = DiagnosticMessageBuilder::error("Invalid input")
            .with_code("Q-1-2") // quarto-error-code-audit-ignore
            .problem("Values must be numeric")
            .add_detail("Found text in column 3")
            .add_info("Expected numbers")
            .add_hint("Convert to numbers first?")
            .build();

        let json = msg.to_json();
        assert_eq!(json["kind"], "error");
        assert_eq!(json["title"], "Invalid input");
        assert_eq!(json["code"], "Q-1-2"); // quarto-error-code-audit-ignore
        assert_eq!(json["problem"]["type"], "markdown");
        assert_eq!(json["problem"]["content"], "Values must be numeric");
        assert_eq!(json["details"][0]["kind"], "error");
        assert_eq!(json["details"][0]["content"]["type"], "markdown");
        assert_eq!(
            json["details"][0]["content"]["content"],
            "Found text in column 3"
        );
        assert_eq!(json["details"][1]["kind"], "info");
        assert_eq!(json["details"][1]["content"]["type"], "markdown");
        assert_eq!(json["details"][1]["content"]["content"], "Expected numbers");
        assert_eq!(json["hints"][0]["type"], "markdown");
        assert_eq!(json["hints"][0]["content"], "Convert to numbers first?");
    }

    #[test]
    fn test_to_json_warning() {
        let msg = DiagnosticMessage::warning("Be careful");
        let json = msg.to_json();

        assert_eq!(json["kind"], "warning");
        assert_eq!(json["title"], "Be careful");
    }

    #[test]
    fn test_location_in_to_text_without_context() {
        use crate::builder::DiagnosticMessageBuilder;

        // Create a location at offsets 100-110
        let location =
            quarto_source_map::SourceInfo::original(quarto_source_map::FileId(0), 100, 110);

        let msg = DiagnosticMessageBuilder::error("Invalid syntax")
            .with_location(location)
            .build();

        let text = msg.to_text(None);

        // Without context, should show offset (we can't get row/column without context)
        assert!(text.contains("Invalid syntax"));
        assert!(text.contains("at offset 100"));
    }

    #[test]
    fn test_location_in_to_text_with_context() {
        use crate::builder::DiagnosticMessageBuilder;

        // Create a source context with a file
        let mut ctx = quarto_source_map::SourceContext::new();
        let file_id = ctx.add_file(
            "test.qmd".to_string(),
            Some("line 1\nline 2\nline 3\nline 4".to_string()),
        );

        // Create a location in that file (offset 7 is start of "line 2")
        let location = quarto_source_map::SourceInfo::original(
            file_id, 7,  // Start of "line 2"
            13, // End of "line 2"
        );

        let msg = DiagnosticMessageBuilder::error("Invalid syntax")
            .with_location(location)
            .build();

        let text = msg.to_text(Some(&ctx));

        // With context, should show file path and 1-indexed location
        assert!(text.contains("Invalid syntax"));
        assert!(text.contains("test.qmd"));
        assert!(text.contains("2:1")); // row 1 + 1, column 0 + 1
    }

    #[test]
    fn test_location_in_to_json() {
        use crate::builder::DiagnosticMessageBuilder;

        let location =
            quarto_source_map::SourceInfo::original(quarto_source_map::FileId(0), 100, 110);

        let msg = DiagnosticMessageBuilder::error("Invalid syntax")
            .with_location(location)
            .build();

        let json = msg.to_json();

        // Should have location field with Original variant
        assert!(json.get("location").is_some());
        let loc = &json["location"];

        // Verify the SourceInfo is serialized correctly (as Original enum variant)
        assert!(loc.get("Original").is_some());
        let original = &loc["Original"];
        assert_eq!(original["file_id"], 0);
        assert_eq!(original["start_offset"], 100);
        assert_eq!(original["end_offset"], 110);
    }

    #[test]
    fn test_location_optional_in_to_json() {
        let msg = DiagnosticMessage::error("No location");
        let json = msg.to_json();

        // Should not have location field when not provided
        assert!(json.get("location").is_none());
    }

    #[test]
    fn test_text_render_options_disable_hyperlinks() {
        use crate::builder::DiagnosticMessageBuilder;

        let mut ctx = quarto_source_map::SourceContext::new();
        let file_id = ctx.add_file("test.qmd".to_string(), Some("test content".to_string()));

        let location = quarto_source_map::SourceInfo::original(file_id, 0, 4);

        let msg = DiagnosticMessageBuilder::error("Test error")
            .with_location(location)
            .build();

        // With hyperlinks enabled (default)
        let with_hyperlinks = msg.to_text(Some(&ctx));

        // With hyperlinks disabled
        let options = TextRenderOptions {
            enable_hyperlinks: false,
        };
        let without_hyperlinks = msg.to_text_with_options(Some(&ctx), &options);

        // When hyperlinks are disabled, output should be different
        // (specifically, no OSC 8 escape sequences)
        if with_hyperlinks.contains("\x1b]8;") {
            assert!(
                !without_hyperlinks.contains("\x1b]8;"),
                "Disabled hyperlinks should not contain OSC 8 codes"
            );
        }
    }

    #[test]
    fn test_text_render_options_default() {
        let options = TextRenderOptions::default();
        assert!(
            options.enable_hyperlinks,
            "Default should enable hyperlinks"
        );
    }

    #[test]
    fn test_render_with_custom_options() {
        use crate::builder::DiagnosticMessageBuilder;

        let msg = DiagnosticMessageBuilder::error("Test")
            .problem("Something went wrong")
            .add_detail("Detail 1")
            .add_hint("Try this")
            .build();

        let options = TextRenderOptions {
            enable_hyperlinks: false,
        };

        let text = msg.to_text_with_options(None, &options);

        // Should still render properly without hyperlinks
        assert!(text.contains("Error: Test"));
        assert!(text.contains("Something went wrong"));
        assert!(text.contains("Detail 1"));
        assert!(text.contains("Try this"));
    }

    /// Strip CSI SGR color sequences (`ESC [ … m`). The annotate-snippets
    /// path emits no OSC 8 hyperlinks, so color is all we need to remove
    /// to make substring assertions robust to styling.
    #[cfg(feature = "annotate-snippets")]
    fn strip_ansi(s: &str) -> String {
        let mut out = String::new();
        let mut chars = s.chars().peekable();
        while let Some(c) = chars.next() {
            if c == '\u{1b}' {
                for n in chars.by_ref() {
                    if n == 'm' {
                        break;
                    }
                }
            } else {
                out.push(c);
            }
        }
        out
    }

    /// The annotate-snippets renderer emits the rust-lang toolchain look:
    /// an `error[CODE]: …` header, a `-->` origin line, and `^` underlines
    /// — not ariadne's enclosing box.
    #[cfg(feature = "annotate-snippets")]
    #[test]
    fn annotate_snippets_renderer_produces_rust_style_output() {
        use crate::builder::DiagnosticMessageBuilder;

        let mut ctx = quarto_source_map::SourceContext::new();
        let file_id = ctx.add_file(
            "test.qmd".to_string(),
            Some("line 1\nline 2\nline 3".to_string()),
        );
        // Offsets 7..13 cover "line 2" on row 2.
        let location = quarto_source_map::SourceInfo::original(file_id, 7, 13);
        let msg = DiagnosticMessageBuilder::error("Bad thing")
            .with_code("Q-9-9")
            .with_location(location)
            .problem("this is wrong")
            .build();

        let opts = TextRenderOptions {
            enable_hyperlinks: false,
        };
        let raw =
            msg.to_text_with_renderer(Some(&ctx), &opts, Some(SourceRenderer::AnnotateSnippets));
        let text = strip_ansi(&raw);

        assert!(
            text.contains("error[Q-9-9]"),
            "expected rust-style code header; got: {text:?}"
        );
        assert!(
            text.contains("-->"),
            "expected rust-style origin arrow; got: {text:?}"
        );
        assert!(
            text.contains("test.qmd:2:1"),
            "expected mapped location; got: {text:?}"
        );
        assert!(
            !text.contains('\u{256D}'),
            "annotate-snippets must not draw ariadne's box corner; got: {text:?}"
        );
        // No OSC 8 hyperlinks from annotate-snippets.
        assert!(
            !raw.contains("\u{1b}]8;"),
            "annotate-snippets emits no OSC 8 hyperlinks; got: {raw:?}"
        );
    }

    /// Direct coverage of the snapping helper's contract: clamp into the
    /// file, widen to whole characters, never invert.
    #[cfg(any(feature = "ariadne", feature = "annotate-snippets"))]
    #[test]
    fn snap_span_widens_to_whole_characters() {
        // `\u{2728}` occupies bytes 3..6.
        let content = "abc\u{2728}def";
        assert_eq!(content.len(), 9);

        let snap = |s, e| DiagnosticMessage::snap_span_to_char_boundaries(content, s, e);

        // Already aligned: unchanged.
        assert_eq!(snap(0, 3), 0..3);
        assert_eq!(snap(3, 6), 3..6);

        // Start inside the char floors to its first byte; end inside it ceils
        // to its last, so the highlight covers the whole character.
        assert_eq!(snap(4, 9), 3..9);
        assert_eq!(snap(5, 9), 3..9);
        assert_eq!(snap(0, 4), 0..6);
        assert_eq!(snap(0, 5), 0..6);
        assert_eq!(snap(4, 5), 3..6);

        // Past EOF clamps to the file length.
        assert_eq!(snap(3, 999), 3..9);
        assert_eq!(snap(999, 999), 9..9);

        // Inverted input collapses to an empty range rather than inverting.
        assert_eq!(snap(6, 3), 6..6);

        // An empty range inside a character still snaps to a boundary.
        let r = snap(4, 4);
        assert!(content.is_char_boundary(r.start) && content.is_char_boundary(r.end));
        assert!(r.start <= r.end);

        // Second content block: the exact offset pair the two
        // `..._does_not_panic` integration tests below use, now that the
        // `quarto-source-map` 0.1.2+ floor makes 21 unreachable at their
        // level (see those tests' doc comments). This is where that
        // coverage now lives.
        //
        // Layout (byte offsets):
        //   `text: <span>Ask AI ` = 0..19, `\u{2728}` = 19..22, `</span>` = 22..29
        let content2 = "text: <span>Ask AI \u{2728}</span>";
        assert!(!content2.is_char_boundary(21), "test fixture precondition");
        let snap2 = |s, e| DiagnosticMessage::snap_span_to_char_boundaries(content2, s, e);

        // 21 is mid-`\u{2728}` (bytes 19..22) and floors to 19; 28 is
        // already on a boundary (inside the trailing ASCII `</span>`) and
        // is left unchanged.
        assert_eq!(snap2(21, 28), 19..28);
    }

    /// A diagnostic whose `SourceInfo` span originally lands mid-character
    /// still renders end to end under the ariadne renderer.
    ///
    /// This test used to be the integration-level proof that
    /// `snap_span_to_char_boundaries` prevents ariadne's mid-character
    /// panic. It no longer is: `quarto-source-map` 0.1.2+ floors
    /// `offset_to_location`'s returned offset to a UTF-8 character
    /// boundary, so by the time `map_offset` hands this test's span
    /// (21..28, with 21 mid-`\u{2728}`) to the renderer it has already
    /// become 19..28 — the snap in this crate is never exercised against a
    /// mid-character offset at this level, because one can no longer be
    /// constructed here. The snap's actual coverage moved to
    /// `snap_span_widens_to_whole_characters`'s second content block, which
    /// calls it directly with these same offsets.
    ///
    /// **Accepted, not an oversight:** after the upstream floor, no revert
    /// of this crate's own code (the snap helper or its three call sites)
    /// can turn this test red — it is unbound with respect to this crate's
    /// diff. That is known and accepted; the test stays as an end-to-end
    /// smoke check that a span-carrying diagnostic renders successfully
    /// under ariadne, not as a snap regression test.
    ///
    /// Layout of the source below (byte offsets):
    ///   `text: <span>Ask AI ` = 0..19, `\u{2728}` = 19..22, `</span>` = 22..29
    /// so 21 is two bytes into the three-byte char — exactly the observed
    /// off-by-one-left onto a multi-byte boundary.
    #[cfg(feature = "ariadne")]
    #[test]
    fn ariadne_renders_diagnostic_with_originally_mid_character_span() {
        use crate::builder::DiagnosticMessageBuilder;

        let content = "text: <span>Ask AI \u{2728}</span>".to_string();
        assert!(!content.is_char_boundary(21), "test fixture precondition");

        let mut ctx = quarto_source_map::SourceContext::new();
        let file_id = ctx.add_file("_quarto.yml".to_string(), Some(content.clone()));
        // 21..28 — start is mid-`\u{2728}`, mirroring the config-path shift.
        let location = quarto_source_map::SourceInfo::original(file_id, 21, 28);
        let msg = DiagnosticMessageBuilder::warning("HTML element converted to raw HTML")
            .with_code("Q-2-9")
            .with_location(location)
            .build();

        let opts = TextRenderOptions {
            enable_hyperlinks: false,
        };
        let text = msg.to_text_with_renderer(Some(&ctx), &opts, Some(SourceRenderer::Ariadne));

        assert!(
            text.contains("HTML element converted to raw HTML"),
            "diagnostic must still render; got: {text:?}"
        );
        assert!(
            text.contains("_quarto.yml"),
            "source context must still render; got: {text:?}"
        );
    }

    /// The same end-to-end smoke check as
    /// `ariadne_renders_diagnostic_with_originally_mid_character_span`, for
    /// the annotate-snippets renderer.
    ///
    /// It no longer exercises the mid-character path either, for the same
    /// reason: `quarto-source-map` 0.1.2+'s floor in `offset_to_location`
    /// means `map_offset` has already snapped this test's 21..28 span to
    /// 19..28 before it reaches annotate-snippets' `clamp` closure, so the
    /// closure never sees a mid-character offset from this call path. The
    /// snap's real coverage lives in `snap_span_widens_to_whole_characters`'s
    /// second content block (same 21..28 offsets, exercised directly).
    ///
    /// **Accepted, not an oversight:** as with the ariadne test above, no
    /// revert of this crate's own snap logic can turn this test red after
    /// the upstream floor — that unbinding is known and accepted.
    #[cfg(feature = "annotate-snippets")]
    #[test]
    fn annotate_snippets_renders_diagnostic_with_originally_mid_character_span() {
        use crate::builder::DiagnosticMessageBuilder;

        let content = "text: <span>Ask AI \u{2728}</span>".to_string();
        assert!(!content.is_char_boundary(21), "test fixture precondition");

        let mut ctx = quarto_source_map::SourceContext::new();
        let file_id = ctx.add_file("_quarto.yml".to_string(), Some(content.clone()));
        let location = quarto_source_map::SourceInfo::original(file_id, 21, 28);
        let msg = DiagnosticMessageBuilder::warning("HTML element converted to raw HTML")
            .with_code("Q-2-9")
            .with_location(location)
            .build();

        let opts = TextRenderOptions {
            enable_hyperlinks: false,
        };
        let text =
            msg.to_text_with_renderer(Some(&ctx), &opts, Some(SourceRenderer::AnnotateSnippets));

        assert!(
            text.contains("HTML element converted to raw HTML"),
            "diagnostic must still render; got: {text:?}"
        );
    }

    /// Strip CSI SGR color sequences, for tests gated under a single
    /// renderer feature that can't rely on `strip_ansi` above (which is
    /// gated on `annotate-snippets` only — ariadne colorizes its source
    /// line and marker row too, so an ariadne-only test needs the same
    /// stripping without pulling in that feature). Same logic as
    /// `strip_ansi`, duplicated rather than re-gated so as not to touch
    /// the existing helper.
    #[cfg(any(feature = "ariadne", feature = "annotate-snippets"))]
    fn strip_ansi_colors(s: &str) -> String {
        let mut out = String::new();
        let mut chars = s.chars().peekable();
        while let Some(c) = chars.next() {
            if c == '\u{1b}' {
                for n in chars.by_ref() {
                    if n == 'm' {
                        break;
                    }
                }
            } else {
                out.push(c);
            }
        }
        out
    }

    /// Measures the *rendered* width of a label whose mapped span is
    /// genuinely zero-width, under the ariadne renderer.
    ///
    /// `\u{2728}` occupies bytes 6..9 of the content below; the input span
    /// `SourceInfo::original(fid, 7, 8)` has **both ends** strictly inside
    /// that character (unlike the `..._does_not_panic` tests above, whose
    /// span only *starts* mid-character). Before the `quarto-source-map`
    /// 0.1.2+ floor, `map_offset` passed the raw offsets 7 and 8 through
    /// unchanged and this crate's own snap widened them to the whole
    /// character (6..9). After the floor, `offset_to_location` already
    /// floors both 7 and 8 down to 6 before this crate ever sees them, so
    /// both mapped offsets are 6 — the snap runs on `6..6`, which is
    /// already boundary-aligned, and has nothing left to widen. The
    /// highlight that reaches the renderer is therefore zero-width, not
    /// the whole character.
    ///
    /// ariadne's `Report::build` anchor is already `start..start`, so "a
    /// zero-width label probably renders fine" was a reasonable guess
    /// before this test — turning that guess into a measurement is the
    /// point here. The assertion is keyed on the renderer's own
    /// zero-width-vs-one-character marker *shape* (a bare `│` vs. `┬─`),
    /// not merely on the message text appearing, so it fails if the
    /// highlight ever widens back to covering the whole character.
    #[cfg(feature = "ariadne")]
    #[test]
    fn ariadne_zero_width_label_renders_a_bare_marker() {
        use crate::builder::DiagnosticMessageBuilder;

        let content = "x = 'A\u{2728}B'".to_string();
        let mut ctx = quarto_source_map::SourceContext::new();
        let file_id = ctx.add_file("scratch.qmd".to_string(), Some(content.clone()));
        let location = quarto_source_map::SourceInfo::original(file_id, 7, 8);
        let msg = DiagnosticMessageBuilder::warning("scratch")
            .with_code("Q-2-9")
            .with_location(location)
            .build();
        let opts = TextRenderOptions {
            enable_hyperlinks: false,
        };
        let text = msg.to_text_with_renderer(Some(&ctx), &opts, Some(SourceRenderer::Ariadne));
        let stripped = strip_ansi_colors(&text);

        let lines: Vec<&str> = stripped.lines().collect();
        let source_idx = lines
            .iter()
            .position(|l| l.contains("x = 'A\u{2728}B'"))
            .unwrap_or_else(|| panic!("source line must render; got: {stripped:?}"));
        let marker_line = lines[source_idx + 1];
        // Skip past the gutter's own `│` (present on every row, e.g.
        // `   │       │  `) to isolate the marker glyphs themselves.
        let gutter_end = marker_line
            .find('│')
            .map(|i| i + '│'.len_utf8())
            .unwrap_or_else(|| panic!("marker row must have a gutter `│`; got: {marker_line:?}"));
        let marker = marker_line[gutter_end..].trim();

        assert_eq!(
            marker, "│",
            "expected the zero-width `│` marker (a whole-character label \
             would instead draw `┬─`); got {marker:?} in:\n{stripped}"
        );
    }

    /// The same measurement as `ariadne_zero_width_label_renders_a_bare_marker`,
    /// for the annotate-snippets renderer. See that test's doc comment for
    /// why both ends of `SourceInfo::original(fid, 7, 8)` land on the same
    /// mapped offset (6) after the `quarto-source-map` 0.1.2+ floor.
    ///
    /// annotate-snippets underlines a span with one `^` per byte of width,
    /// so the discriminating measurement here is even more direct than
    /// ariadne's marker shape: a zero-width label draws exactly one `^`,
    /// a whole-character label draws two (`^^`).
    #[cfg(feature = "annotate-snippets")]
    #[test]
    fn annotate_snippets_zero_width_label_renders_a_single_caret() {
        use crate::builder::DiagnosticMessageBuilder;

        let content = "x = 'A\u{2728}B'".to_string();
        let mut ctx = quarto_source_map::SourceContext::new();
        let file_id = ctx.add_file("scratch.qmd".to_string(), Some(content.clone()));
        let location = quarto_source_map::SourceInfo::original(file_id, 7, 8);
        let msg = DiagnosticMessageBuilder::warning("scratch")
            .with_code("Q-2-9")
            .with_location(location)
            .build();
        let opts = TextRenderOptions {
            enable_hyperlinks: false,
        };
        let text =
            msg.to_text_with_renderer(Some(&ctx), &opts, Some(SourceRenderer::AnnotateSnippets));
        let stripped = strip_ansi_colors(&text);

        let lines: Vec<&str> = stripped.lines().collect();
        let source_idx = lines
            .iter()
            .position(|l| l.contains("x = 'A\u{2728}B'"))
            .unwrap_or_else(|| panic!("source line must render; got: {stripped:?}"));
        let marker_line = lines[source_idx + 1];
        let caret_run: String = marker_line.chars().filter(|&c| c == '^').collect();

        assert_eq!(
            caret_run, "^",
            "expected a single `^` caret marking a zero-width label (a \
             whole-character label would instead draw `^^`); got \
             {caret_run:?} in line: {marker_line:?}"
        );
    }

    /// Forcing a specific renderer is honored: ariadne draws its boxed
    /// excerpt (the U+256D corner) while annotate-snippets does not.
    #[cfg(all(feature = "ariadne", feature = "annotate-snippets"))]
    #[test]
    fn renderer_selection_switches_styles() {
        use crate::builder::DiagnosticMessageBuilder;

        let mut ctx = quarto_source_map::SourceContext::new();
        let file_id = ctx.add_file("a.qmd".to_string(), Some("alpha\nbeta\ngamma".to_string()));
        let location = quarto_source_map::SourceInfo::original(file_id, 6, 10); // "beta"
        let msg = DiagnosticMessageBuilder::error("Pick a style")
            .with_location(location)
            .build();
        let opts = TextRenderOptions {
            enable_hyperlinks: false,
        };

        let ariadne = msg.to_text_with_renderer(Some(&ctx), &opts, Some(SourceRenderer::Ariadne));
        let snippets =
            msg.to_text_with_renderer(Some(&ctx), &opts, Some(SourceRenderer::AnnotateSnippets));

        assert!(ariadne.contains('\u{256D}'), "ariadne draws a box corner");
        assert!(
            !strip_ansi(&snippets).contains('\u{256D}'),
            "annotate-snippets does not"
        );
        assert!(strip_ansi(&snippets).contains("-->"));
    }

    // ==================== Concat / cross-piece rendering ====================
    //
    // A diagnostic whose location resolves through a multi-piece `Concat`
    // (the shape q2's ipynb processor produces: one virtual file per
    // notebook cell) must render against the piece the span actually
    // resolves into — not `root_file_id()`, which is the *first* rooted
    // piece. These tests pin the plan-7c contract: report file from
    // `start_mapped.file_id`; a span straddling pieces renders a
    // cross-file location label with no snippet; a detail rooted in
    // another piece renders in its own file's source block.

    /// Two per-cell virtual files joined by a `Concat`, exactly as the
    /// ipynb converter registers them. Returns the context, the concat,
    /// and piece 1's length (the concat offset where piece 2 begins).
    #[cfg(any(feature = "ariadne", feature = "annotate-snippets"))]
    fn concat_fixture() -> (
        quarto_source_map::SourceContext,
        quarto_source_map::SourceInfo,
        usize,
    ) {
        let piece1 = "first cell text\n";
        let piece2 = "second cell text\n";
        let mut ctx = quarto_source_map::SourceContext::new();
        let f1 = ctx.add_file(
            "notebook.ipynb[cell 1, markdown]".to_string(),
            Some(piece1.to_string()),
        );
        let f2 = ctx.add_file(
            "notebook.ipynb[cell 2, markdown]".to_string(),
            Some(piece2.to_string()),
        );
        let concat = quarto_source_map::SourceInfo::concat(vec![
            (
                quarto_source_map::SourceInfo::original(f1, 0, piece1.len()),
                piece1.len(),
            ),
            (
                quarto_source_map::SourceInfo::original(f2, 0, piece2.len()),
                piece2.len(),
            ),
        ]);
        (ctx, concat, piece1.len())
    }

    /// The percent/spin shape: a `Concat` whose pieces all root to ONE
    /// file. Must render exactly as a plain single-file diagnostic — the
    /// fix must not disturb it.
    #[cfg(feature = "ariadne")]
    #[test]
    fn ariadne_concat_single_file_pieces_passthrough() {
        use crate::builder::DiagnosticMessageBuilder;

        let content = "alpha\nbeta\ngamma\n";
        let mut ctx = quarto_source_map::SourceContext::new();
        let f1 = ctx.add_file("script.qmd".to_string(), Some(content.to_string()));
        let concat = quarto_source_map::SourceInfo::concat(vec![
            (quarto_source_map::SourceInfo::original(f1, 0, 6), 6),
            (
                quarto_source_map::SourceInfo::original(f1, 6, content.len()),
                content.len() - 6,
            ),
        ]);
        let location = quarto_source_map::SourceInfo::substring(concat, 6, 10); // "beta"
        let msg = DiagnosticMessageBuilder::error("Bad chunk")
            .with_location(location)
            .build();
        let opts = TextRenderOptions {
            enable_hyperlinks: false,
        };
        let text = strip_ansi_colors(&msg.to_text_with_renderer(
            Some(&ctx),
            &opts,
            Some(SourceRenderer::Ariadne),
        ));

        assert!(
            text.contains("script.qmd"),
            "single-file passthrough must keep the file label; got:\n{text}"
        );
        assert!(
            text.contains("beta"),
            "single-file passthrough must render the snippet; got:\n{text}"
        );
    }

    /// A main span rooted wholly in the *first* piece already renders
    /// correctly (`root_file_id()` happens to agree with
    /// `start_mapped.file_id`); must stay correct after the fix.
    #[cfg(feature = "ariadne")]
    #[test]
    fn ariadne_concat_main_span_in_first_piece_renders_own_file() {
        use crate::builder::DiagnosticMessageBuilder;

        let (ctx, concat, _) = concat_fixture();
        let location = quarto_source_map::SourceInfo::substring(concat, 0, 5); // "first"
        let msg = DiagnosticMessageBuilder::error("Bad cell")
            .with_location(location)
            .build();
        let opts = TextRenderOptions {
            enable_hyperlinks: false,
        };
        let text = strip_ansi_colors(&msg.to_text_with_renderer(
            Some(&ctx),
            &opts,
            Some(SourceRenderer::Ariadne),
        ));

        assert!(
            text.contains("notebook.ipynb[cell 1, markdown]"),
            "first-piece span must label piece 1; got:\n{text}"
        );
        assert!(
            text.contains("first cell text"),
            "first-piece span must show piece 1's snippet; got:\n{text}"
        );
        assert!(
            !text.contains("notebook.ipynb[cell 2, markdown]"),
            "piece 2 must not appear; got:\n{text}"
        );
    }

    /// THE cross-piece case: a diagnostic rooted wholly in a later piece
    /// must be labeled and snippeted from *that* piece — today it gets
    /// piece 1's label and piece 1's content at piece-2 offsets.
    #[cfg(feature = "ariadne")]
    #[test]
    fn ariadne_concat_main_span_in_later_piece_renders_own_file() {
        use crate::builder::DiagnosticMessageBuilder;

        let (ctx, concat, l1) = concat_fixture();
        let location = quarto_source_map::SourceInfo::substring(concat, l1, l1 + 6); // "second"
        let msg = DiagnosticMessageBuilder::error("Bad cell")
            .with_location(location)
            .build();
        let opts = TextRenderOptions {
            enable_hyperlinks: false,
        };
        let text = strip_ansi_colors(&msg.to_text_with_renderer(
            Some(&ctx),
            &opts,
            Some(SourceRenderer::Ariadne),
        ));

        assert!(
            text.contains("notebook.ipynb[cell 2, markdown]"),
            "later-piece span must label the owning piece; got:\n{text}"
        );
        assert!(
            text.contains("second cell text"),
            "later-piece span must snippet the owning piece; got:\n{text}"
        );
        assert!(
            !text.contains("first cell text"),
            "the wrong piece must not render; got:\n{text}"
        );
    }

    /// A span straddling two pieces cannot be one snippet; it must render
    /// an explicit cross-file label naming both pieces and no snippet —
    /// today it is silently clamped into piece 1.
    #[cfg(feature = "ariadne")]
    #[test]
    fn ariadne_concat_straddling_span_labels_both_pieces_without_snippet() {
        use crate::builder::DiagnosticMessageBuilder;

        let (ctx, concat, l1) = concat_fixture();
        // Starts mid-piece-1 ("cell text…"), ends mid-piece-2 ("second").
        let location = quarto_source_map::SourceInfo::substring(concat, 6, l1 + 6);
        let msg = DiagnosticMessageBuilder::error("Cross-cell markdown")
            .with_location(location)
            .build();
        let opts = TextRenderOptions {
            enable_hyperlinks: false,
        };
        let text = strip_ansi_colors(&msg.to_text_with_renderer(
            Some(&ctx),
            &opts,
            Some(SourceRenderer::Ariadne),
        ));

        assert!(
            text.contains("--> notebook.ipynb[cell 1, markdown]:1:7"),
            "straddling span must name its start piece and position; got:\n{text}"
        );
        assert!(
            text.contains("(spans through notebook.ipynb[cell 2, markdown]:1:7)"),
            "straddling span must name its end piece and position; got:\n{text}"
        );
        assert!(
            !text.contains("first cell text") && !text.contains("second cell text"),
            "a cross-piece span must render no snippet; got:\n{text}"
        );
    }

    /// A detail rooted wholly in another piece must render — in its own
    /// file's source block — not be silently dropped.
    #[cfg(feature = "ariadne")]
    #[test]
    fn ariadne_concat_detail_in_another_piece_renders_own_file() {
        use crate::builder::DiagnosticMessageBuilder;

        let (ctx, concat, l1) = concat_fixture();
        let main = quarto_source_map::SourceInfo::substring(concat.clone(), 0, 5);
        let detail = quarto_source_map::SourceInfo::substring(concat, l1, l1 + 6);
        let msg = DiagnosticMessageBuilder::error("Mismatch")
            .with_location(main)
            .add_detail_at("related token in cell 2", detail)
            .build();
        let opts = TextRenderOptions {
            enable_hyperlinks: false,
        };
        let text = strip_ansi_colors(&msg.to_text_with_renderer(
            Some(&ctx),
            &opts,
            Some(SourceRenderer::Ariadne),
        ));

        assert!(
            text.contains("notebook.ipynb[cell 1, markdown]") && text.contains("first cell text"),
            "main span must still render; got:\n{text}"
        );
        assert!(
            text.contains("notebook.ipynb[cell 2, markdown]"),
            "foreign-piece detail must render its own block; got:\n{text}"
        );
        assert!(
            text.contains("second cell text"),
            "foreign-piece detail must snippet its own file; got:\n{text}"
        );
        assert!(
            text.contains("related token in cell 2"),
            "foreign-piece detail must keep its message; got:\n{text}"
        );
    }

    /// Passthrough for the annotate-snippets renderer (see the ariadne
    /// twin for the contract).
    #[cfg(feature = "annotate-snippets")]
    #[test]
    fn annotate_snippets_concat_single_file_pieces_passthrough() {
        use crate::builder::DiagnosticMessageBuilder;

        let content = "alpha\nbeta\ngamma\n";
        let mut ctx = quarto_source_map::SourceContext::new();
        let f1 = ctx.add_file("script.qmd".to_string(), Some(content.to_string()));
        let concat = quarto_source_map::SourceInfo::concat(vec![
            (quarto_source_map::SourceInfo::original(f1, 0, 6), 6),
            (
                quarto_source_map::SourceInfo::original(f1, 6, content.len()),
                content.len() - 6,
            ),
        ]);
        let location = quarto_source_map::SourceInfo::substring(concat, 6, 10); // "beta"
        let msg = DiagnosticMessageBuilder::error("Bad chunk")
            .with_location(location)
            .build();
        let opts = TextRenderOptions {
            enable_hyperlinks: false,
        };
        let text = strip_ansi(&msg.to_text_with_renderer(
            Some(&ctx),
            &opts,
            Some(SourceRenderer::AnnotateSnippets),
        ));

        assert!(
            text.contains("script.qmd") && text.contains("beta"),
            "single-file passthrough must render unchanged; got:\n{text}"
        );
    }

    /// First-piece span under annotate-snippets (see the ariadne twin).
    #[cfg(feature = "annotate-snippets")]
    #[test]
    fn annotate_snippets_concat_main_span_in_first_piece_renders_own_file() {
        use crate::builder::DiagnosticMessageBuilder;

        let (ctx, concat, _) = concat_fixture();
        let location = quarto_source_map::SourceInfo::substring(concat, 0, 5); // "first"
        let msg = DiagnosticMessageBuilder::error("Bad cell")
            .with_location(location)
            .build();
        let opts = TextRenderOptions {
            enable_hyperlinks: false,
        };
        let text = strip_ansi(&msg.to_text_with_renderer(
            Some(&ctx),
            &opts,
            Some(SourceRenderer::AnnotateSnippets),
        ));

        assert!(
            text.contains("notebook.ipynb[cell 1, markdown]") && text.contains("first cell text"),
            "first-piece span must render piece 1; got:\n{text}"
        );
        assert!(
            !text.contains("notebook.ipynb[cell 2, markdown]"),
            "piece 2 must not appear; got:\n{text}"
        );
    }

    /// Later-piece span under annotate-snippets (see the ariadne twin).
    #[cfg(feature = "annotate-snippets")]
    #[test]
    fn annotate_snippets_concat_main_span_in_later_piece_renders_own_file() {
        use crate::builder::DiagnosticMessageBuilder;

        let (ctx, concat, l1) = concat_fixture();
        let location = quarto_source_map::SourceInfo::substring(concat, l1, l1 + 6); // "second"
        let msg = DiagnosticMessageBuilder::error("Bad cell")
            .with_location(location)
            .build();
        let opts = TextRenderOptions {
            enable_hyperlinks: false,
        };
        let text = strip_ansi(&msg.to_text_with_renderer(
            Some(&ctx),
            &opts,
            Some(SourceRenderer::AnnotateSnippets),
        ));

        assert!(
            text.contains("notebook.ipynb[cell 2, markdown]"),
            "later-piece span must label the owning piece; got:\n{text}"
        );
        assert!(
            text.contains("second cell text"),
            "later-piece span must snippet the owning piece; got:\n{text}"
        );
        assert!(
            !text.contains("first cell text"),
            "the wrong piece must not render; got:\n{text}"
        );
    }

    /// Straddling span under annotate-snippets (see the ariadne twin).
    #[cfg(feature = "annotate-snippets")]
    #[test]
    fn annotate_snippets_concat_straddling_span_labels_both_pieces_without_snippet() {
        use crate::builder::DiagnosticMessageBuilder;

        let (ctx, concat, l1) = concat_fixture();
        let location = quarto_source_map::SourceInfo::substring(concat, 6, l1 + 6);
        let msg = DiagnosticMessageBuilder::error("Cross-cell markdown")
            .with_location(location)
            .build();
        let opts = TextRenderOptions {
            enable_hyperlinks: false,
        };
        let text = strip_ansi(&msg.to_text_with_renderer(
            Some(&ctx),
            &opts,
            Some(SourceRenderer::AnnotateSnippets),
        ));

        assert!(
            text.contains("--> notebook.ipynb[cell 1, markdown]:1:7"),
            "straddling span must name its start piece and position; got:\n{text}"
        );
        assert!(
            text.contains("(spans through notebook.ipynb[cell 2, markdown]:1:7)"),
            "straddling span must name its end piece and position; got:\n{text}"
        );
        assert!(
            !text.contains("first cell text") && !text.contains("second cell text"),
            "a cross-piece span must render no snippet; got:\n{text}"
        );
    }

    /// Foreign-piece detail under annotate-snippets (see the ariadne
    /// twin).
    #[cfg(feature = "annotate-snippets")]
    #[test]
    fn annotate_snippets_concat_detail_in_another_piece_renders_own_file() {
        use crate::builder::DiagnosticMessageBuilder;

        let (ctx, concat, l1) = concat_fixture();
        let main = quarto_source_map::SourceInfo::substring(concat.clone(), 0, 5);
        let detail = quarto_source_map::SourceInfo::substring(concat, l1, l1 + 6);
        let msg = DiagnosticMessageBuilder::error("Mismatch")
            .with_location(main)
            .add_detail_at("related token in cell 2", detail)
            .build();
        let opts = TextRenderOptions {
            enable_hyperlinks: false,
        };
        let text = strip_ansi(&msg.to_text_with_renderer(
            Some(&ctx),
            &opts,
            Some(SourceRenderer::AnnotateSnippets),
        ));

        assert!(
            text.contains("notebook.ipynb[cell 1, markdown]") && text.contains("first cell text"),
            "main span must still render; got:\n{text}"
        );
        assert!(
            text.contains("notebook.ipynb[cell 2, markdown]")
                && text.contains("second cell text")
                && text.contains("related token in cell 2"),
            "foreign-piece detail must render its own block with its message; got:\n{text}"
        );
    }

    // ==================== Origin-aware hyperlinks (plan 7c) ====================
    //
    // A virtual file carrying `FileMetadata::origin` (a notebook cell) is
    // hyperlinked to the *owning notebook on disk*, and the `#line:column`
    // fragment must be suppressed: origin coordinates are cell-relative and
    // would be wrong in the notebook's URL. Without origin, a virtual file
    // gets no hyperlink at all (its pseudo-path does not exist on disk).

    /// Extract every OSC 8 link URL from raw terminal output.
    #[cfg(feature = "ariadne")]
    fn osc8_urls(text: &str) -> Vec<String> {
        let mut urls = Vec::new();
        let mut rest = text;
        while let Some(start) = rest.find("\x1b]8;;") {
            let after = &rest[start + 5..];
            match after.find("\x1b\\") {
                Some(end) => {
                    urls.push(after[..end].to_string());
                    rest = &after[end..];
                }
                None => break,
            }
        }
        urls
    }

    /// A real notebook on disk plus two per-cell virtual files joined by a
    /// `Concat`, with `origin` attached to both cells. `notebook_path` is the
    /// absolute disk path — what q2 registers after resolving the document.
    #[cfg(feature = "ariadne")]
    fn origin_fixture() -> (
        quarto_source_map::SourceContext,
        quarto_source_map::SourceInfo,
        tempfile::TempDir,
        std::path::PathBuf,
        usize,
    ) {
        let piece1 = "first cell text\n";
        let piece2 = "second cell text\n";
        let dir = tempfile::TempDir::new().unwrap();
        let notebook = dir.path().join("notebook.ipynb");
        std::fs::write(&notebook, "{}").unwrap();
        let origin_for = |index: usize| {
            Some(quarto_source_map::FileOrigin::NotebookCell {
                notebook_path: notebook.display().to_string(),
                cell_index: index,
                cell_id: None,
                cell_type: "markdown".to_string(),
            })
        };
        let mut ctx = quarto_source_map::SourceContext::new();
        let f1 = ctx.add_file(
            "notebook.ipynb[cell 1, markdown]".to_string(),
            Some(piece1.to_string()),
        );
        let f2 = ctx.add_file(
            "notebook.ipynb[cell 2, markdown]".to_string(),
            Some(piece2.to_string()),
        );
        ctx.get_file_mut(f1).unwrap().metadata.origin = origin_for(1);
        ctx.get_file_mut(f2).unwrap().metadata.origin = origin_for(2);
        let concat = quarto_source_map::SourceInfo::concat(vec![
            (
                quarto_source_map::SourceInfo::original(f1, 0, piece1.len()),
                piece1.len(),
            ),
            (
                quarto_source_map::SourceInfo::original(f2, 0, piece2.len()),
                piece2.len(),
            ),
        ]);
        (ctx, concat, dir, notebook, piece1.len())
    }

    #[cfg(feature = "ariadne")]
    #[test]
    fn ariadne_origin_cell_hyperlinks_the_owning_notebook() {
        use crate::builder::DiagnosticMessageBuilder;

        let (ctx, concat, _dir, notebook, l1) = origin_fixture();
        let location = quarto_source_map::SourceInfo::substring(concat, l1, l1 + 6); // "second"
        let msg = DiagnosticMessageBuilder::error("Bad cell")
            .with_location(location)
            .build();
        let opts = TextRenderOptions {
            enable_hyperlinks: true,
        };
        let text = msg.to_text_with_renderer(Some(&ctx), &opts, Some(SourceRenderer::Ariadne));

        // Compute the expected URL exactly the way the renderer does, so
        // platform-specific path forms (Windows verbatim `\\?\…`) can't
        // make the test diverge from production.
        let canonical = std::fs::canonicalize(&notebook).unwrap();
        let expected_prefix =
            url::Url::from_file_path(super::DiagnosticMessage::plain_absolute_path(canonical))
                .unwrap()
                .as_str()
                .to_string();
        let urls = osc8_urls(&text);
        let cell_link = urls
            .iter()
            .find(|u| u.starts_with(&expected_prefix))
            .expect("cell label must hyperlink to the owning notebook");

        // The fragment must be absent: origin coordinates are cell-relative
        // and would be wrong as a notebook line/column.
        assert!(
            !cell_link.contains('#'),
            "cell hyperlink must not carry a #line:col fragment; got {cell_link:?}"
        );
        // The human-readable label is still the cell-qualified pseudo-path.
        assert!(
            text.contains("notebook.ipynb[cell 2, markdown]"),
            "label must stay the pseudo-path; got:\n{text}"
        );
    }

    #[cfg(feature = "ariadne")]
    #[test]
    fn ariadne_virtual_file_without_origin_gets_no_hyperlink() {
        use crate::builder::DiagnosticMessageBuilder;

        let (ctx, concat, _l1) = concat_fixture();
        let location = quarto_source_map::SourceInfo::substring(concat, 0, 5);
        let msg = DiagnosticMessageBuilder::error("Bad cell")
            .with_location(location)
            .build();
        let opts = TextRenderOptions {
            enable_hyperlinks: true,
        };
        let text = msg.to_text_with_renderer(Some(&ctx), &opts, Some(SourceRenderer::Ariadne));

        assert!(
            osc8_urls(&text).is_empty(),
            "a virtual file whose path does not exist on disk must not be hyperlinked; got:\n{text}"
        );
    }
}