konoma 0.28.0

Terminal file browser built for AI pair-programming — full-screen previews (Markdown, images, PDF, CSV), a git suite (jj/Jujutsu in preview), and an agent-watch mode that follows your AI's edits (macOS and Linux)
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
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
//! What stage 5b's eleven languages are checked with.
//!
//! # One module for eleven kinds, and why that is not a shortcut
//!
//! The eleven do not share a *layout* — three go through [`lay_out_spec`], two are placed on a
//! grid, four are banded, one is a lane diagram and one is a sequence diagram in disguise. What
//! they share is everything a test can state: the same [`Diagram`], the same twelve geometric
//! invariants, the same main instrument, the same golden format. Splitting that into eleven files
//! would mean eleven copies of the same six functions, and the failure mode konoma keeps hitting
//! is a copy that stopped matching its original.
//!
//! [`lay_out_spec`]: super::lay_out_spec
//!
//! # Which of the twelve shared invariants apply
//!
//! | # | invariant | applies to |
//! |---|---|---|
//! | 1 | nodes do not overlap | **the graph and grid kinds** — mindmap, requirement, C4, architecture, block, git graph. Not the banded ones: a journey's face sits above its own task box on purpose, and a gantt bar and its row label share a row. |
//! | 2 | edges stay out of shapes | **the graph kinds**. Not the grid or banded ones: an architecture line crosses the cell between two boxes by design, and a gantt grid line runs the whole height of the chart, which is what a grid line is. |
//! | 3 | labels ride their edge | **every kind that puts a label on an edge** — requirement, C4, block, architecture. |
//! | 4 | endpoints land on an outline | **the graph, grid and block kinds**. Not the git graph, whose lines join dot centres so a merge's two lines meet at a point. |
//! | 5 | the viewBox contains everything | **all eleven**, and it is the one that catches a clipped box — which is the crate's own requirement-diagram defect. |
//! | 6–11 | the six cluster invariants | **every kind that draws a frame** — C4, architecture, journey, timeline, gantt, kanban, block. |
//! | 12 | panels stay inside their box | **requirement, C4, kanban**. |
//!
//! Each is *called*, never copied — the rule stages 3, 4 and 5a settled.

use std::collections::HashSet;

use crate::preview::markdown::mermaid_to_svg_reason;
use crate::preview::mermaid::layout::Point;
use crate::preview::mermaid::text_metrics;

use super::shapes::{Glyph, Mark};
use super::tests::{
    assert_snapshot, check_endpoints_land_on_the_outline, check_labels_ride_their_edge,
    check_nested_clusters_sit_inside_their_parent, check_nodes_do_not_overlap,
    check_panels_stay_inside_their_box, check_unrelated_clusters_do_not_overlap,
    check_view_box_contains_everything, mask_numbers, measured_texts, tree_of,
};
use super::{svg, Diagram, Label, RenderError};

// ---------------------------------------------------------------------------------------------
// Corpus
// ---------------------------------------------------------------------------------------------

/// The diagrams every structural test here runs over.
///
/// Built from **each language's own documentation page** — `mindmap.md`, `timeline.md`,
/// `userJourney.md`, `kanban.md`, `architecture.md`, `requirementDiagram.md`, `gitgraph.md`,
/// `c4.md`, `block.md`, `gantt.md`, `zenuml.md` at tag `mermaid@11.17.2` — plus the case
/// distinctions each grammar makes that no example happens to show. Not from a list of bugs
/// already found (memory `corpus-from-spec-not-from-bugs`).
pub const CASES: &[(&str, &str)] = &[
    // --- mindmap -----------------------------------------------------------------------------
    (
        "mindmap",
        "mindmap\n  root((mindmap))\n    Origins\n      Long history\n      Popularisation\n        \
         British popular psychology author Tony Buzan\n    Research\n      On effectiveness<br/>and \
         features\n      On Automatic creation\n    Tools\n      Pen and paper\n      Mermaid",
    ),
    (
        "mindmap-shapes",
        "mindmap\n  Root\n    a[square]\n    b(rounded)\n    c((circle))\n    d))bang((\n    \
         e)cloud(\n    f{{hexagon}}\n    plain",
    ),
    (
        // `getType` reads **both** brackets: `(` closed by `)` is a rounded rectangle and `(`
        // closed by anything else is a cloud. No documentation example shows the second, so the
        // corpus has to.
        "mindmap-open-cloud",
        "mindmap\n  Root\n    a(cloud(\n    b(rounded)",
    ),
    (
        "mindmap-cjk",
        "mindmap\n  root((全画面プレビュー))\n    ツリー\n      種別を解決\n    窓読み",
    ),
    (
        // `SPACELIST` is counted in **characters** and `getParent` takes the last node with a strictly
        // smaller count, so indentation does not have to be consistent. This is the documentation's own
        // uneven example: C at 6 hangs off A at 4, not off B at 8.
        "mindmap-uneven-indent",
        "mindmap\nRoot\n    A\n        B\n      C\n        D\n",
    ),
    (
        "mindmap-deep",
        "mindmap\n  root((core))\n   l1\n    l2\n     l3\n      l4\n       l5\n        l6\n",
    ),
    (
        "mindmap-wide",
        "mindmap\n  root((hub))\n    one\n    two\n    three\n    four\n    five\n    six\n    seven\n    eight\n",
    ),
    (
        // `::icon(...)` and `:::class` are statements of their own that decorate the node before them —
        // read so the line does not become a phantom node, drawn as neither.
        "mindmap-decorations",
        "mindmap\n  Root\n    Reading\n    ::icon(fa fa-book)\n    Writing\n    :::urgent large\n    Plain\n",
    ),
    (
        // Inside `NODE` a `["]` opens `NSTR`, which runs to the next quotation mark — brackets and all.
        // Without that state a bracket in the words closes the node early.
        "mindmap-quoted-brackets",
        "mindmap\n  Root\n    a[\"a (b) and [c]\"]\n    b(\"one ) two\")\n",
    ),
    (
        // `["][`]` … `[`]["]` spans lines, because `NSTR2` has no rule for a newline.
        "mindmap-markdown-string",
        "mindmap\n  id1[\"`**Root** with\na second line`\"]\n    id2[\"`a *child*\nover two lines`\"]\n",
    ),
    (
        // `nodeWithoutId` — the description becomes the id too.
        "mindmap-no-id",
        "mindmap\n  (a rounded label)\n    [a square label]\n    ((a circle))\n",
    ),
    (
        "mindmap-one-node",
        "mindmap\n  Only\n",
    ),
    (
        "mindmap-comments",
        "mindmap\n%% a comment is a blank line here\n  Root\n\n    A\n  %% and so is an indented one\n    B\n",
    ),
    (
        // Three siblings with the same words are three nodes with the **same id** upstream too, because
        // `nodeWithId` uses what was written. A drawing that collapsed them would lose two branches.
        "mindmap-duplicate-labels",
        "mindmap\n  Root\n    Same\n    Same\n    Same\n",
    ),
    (
        "mindmap-long-label",
        "mindmap\n  root((centre))\n    A label long enough that a terminal cannot show it on one line without wrapping it somewhere sensible\n    short\n",
    ),
    (
        // The first node's own count is the baseline and is subtracted from every later one, so a whole
        // diagram indented by eight is the same diagram.
        "mindmap-indented-whole",
        "mindmap\n        Root\n            A\n                B\n            C\n",
    ),
    (
        "mindmap-cjk-shapes",
        "mindmap\n  ルート((全体))\n    四角[設定を読む]\n    丸(描画する)\n    雲)ここは雲(\n    六角{{判定}}\n",
    ),
    // --- kanban ------------------------------------------------------------------------------
    (
        "kanban",
        "kanban\n  Todo\n    [Create Documentation]\n    docs[Create Blog about the new diagram]\n  \
         id7[In progress]\n    id6[Create renderer so that it works in all cases]\n  id9[Ready for \
         deploy]\n    id8[Design grammar]@{ assigned: 'knsv' }\n",
    ),
    (
        "kanban-meta",
        "kanban\n  Backlog\n    id1[Ticket]@{ assigned: 'knsv', ticket: MC-2038, priority: 'Very \
         High' }\n    id2[Another]@{ priority: 'Low' }\n  Done\n",
    ),
    (
        // A column with no cards is still a column: `getSection` makes one for every node at the first
        // node's level, whatever follows it.
        "kanban-empty-column",
        "kanban\n  Todo\n    a[First]\n  Blocked\n  Done\n    b[Second]\n",
    ),
    (
        "kanban-shapes",
        "kanban\n  Todo\n    a(rounded)\n    b((circle))\n    c{{hexagon}}\n    d[square]\n    plain\n",
    ),
    (
        "kanban-decorations",
        "kanban\n  Todo\n    a[Card]\n    ::icon(fa fa-bug)\n    b[Another]\n    :::urgent\n",
    ),
    (
        "kanban-cjk",
        "kanban\n  やること\n    [設計を書く]\n    id2[レンダラを実装する]@{ assigned: '担当', priority: 'Very High' }\n  完了\n    [調査]\n",
    ),
    (
        "kanban-many-columns",
        "kanban\n  Backlog\n    a1[One]\n  Todo\n    a2[Two]\n  Doing\n    a3[Three]\n  Review\n    a4[Four]\n  Done\n    a5[Five]\n",
    ),
    (
        "kanban-tall-column",
        "kanban\n  Todo\n    c1[One]\n    c2[Two]\n    c3[Three]\n    c4[Four]\n    c5[Five]\n    c6[Six]\n  Done\n    d1[Shipped]\n",
    ),
    (
        "kanban-long-card",
        "kanban\n  Todo\n    a[A card whose text is far longer than any column header and has to be wrapped to be read]\n  Done\n    b[ok]\n",
    ),
    (
        // Two columns written with the same words. Upstream makes two of them, so konoma does too, and
        // the frames have to stay apart — a lookup that collapsed them would hide a card in the wrong one.
        "kanban-duplicate-columns",
        "kanban\n  Todo\n    a[One]\n  Doing\n    b[Two]\n  Todo\n    c[Three]\n",
    ),
    (
        "kanban-markdown-card",
        "kanban\n  Todo\n    a[\"`**bold** card\nsecond line`\"]\n  Done\n    b[plain]\n",
    ),
    (
        "kanban-comments",
        "kanban\n%% the board\n  Todo\n\n    a[One]\n  %% between columns\n  Done\n    b[Two]\n",
    ),
    (
        "kanban-meta-all",
        "kanban\n  Backlog\n    a[Card]@{ assigned: 'knsv', ticket: MC-1, priority: 'Very Low', icon: 'bug' }\n    b[Other]@{ priority: 'High' }\n    c[Third]@{ priority: 'Low' }\n",
    ),
    (
        // Everything below the section level is a card in the last column, however deep — `getSection`
        // compares against `nodes[0].level` and not against the level of the node before it.
        "kanban-deeper-card",
        "kanban\n  Todo\n    a[One]\n        b[Deeper still]\n  Done\n    c[Two]\n",
    ),
    (
        "kanban-one-card",
        "kanban\n  Todo\n    a[Only]\n",
    ),
    (
        "kanban-label-override",
        "kanban\n  Todo\n    a[written in the brackets]@{ label: 'written in the metadata' }\n  Done\n    b[plain]\n",
    ),
    (
        // Cards written with nothing in their brackets. `nodeWithoutId` names a card by its own
        // words, so a card with no words has no name — and `kanbanDb` falls back to `kbn` plus a
        // counter. Nothing in the corpus reached that branch, so a mutation that made every
        // generated id the same string survived: there was never a second card that needed one.
        "kanban-empty-cards",
        "kanban\n  Todo\n    []\n    []\n  Done\n    a[Real]\n",
    ),
    (
        // The same words on two cards in two columns. `nodeWithId` names a card by what was
        // written, so these two are asking to be told apart by something other than their text.
        "kanban-repeated-card-words",
        "kanban\n  Todo\n    [Review]\n  Doing\n    [Review]\n  Done\n    [Ship]\n",
    ),
    // --- journey -----------------------------------------------------------------------------
    (
        "journey",
        "journey\n    title My working day\n    section Go to work\n      Make tea: 5: Me\n      Go \
         upstairs: 3: Me\n      Do work: 1: Me, Cat\n    section Go home\n      Go downstairs: 5: \
         Me\n      Sit down: 5: Me",
    ),
    (
        "journey-no-section",
        "journey\n  Wake: 4\n  Sleep: 2: Me, You",
    ),
    (
        // The mouth **is** the number, so every one of the five has to be drawn somewhere in the corpus.
        "journey-all-scores",
        "journey\n  title Every face\n  section Scores\n    One: 1: Me\n    Two: 2: Me\n    Three: 3: Me\n    Four: 4: Me\n    Five: 5: Me\n",
    ),
    (
        "journey-cjk",
        "journey\n  title 一日の流れ\n  section 出社\n    お茶をいれる: 5: 私\n    階段をのぼる: 3: 私, 猫\n  section 帰宅\n    座る: 4: 私\n",
    ),
    (
        "journey-many-people",
        "journey\n  title Handover\n  section Release\n    Cut the tag: 3: Alice, Bob, Carol, Dave, Erin\n    Announce: 5: Alice\n",
    ),
    (
        // `Number('n/a')` is `NaN` upstream. A face drawn anyway would be a claim the source never made,
        // so the task keeps its box and gets none.
        "journey-unreadable-score",
        "journey\n  title Missing scores\n  Ready: n/a: Me\n  Set: 4: Me\n  Go: : Me\n",
    ),
    (
        // The grammar takes any number. A face has five mouths, so what happens at 0 and at 9 is a
        // question the corpus has to ask rather than assume.
        "journey-out-of-range-score",
        "journey\n  title Out of range\n  Low: 0: Me\n  High: 9: Me\n  Fine: 3: Me\n",
    ),
    (
        "journey-many-sections",
        "journey\n  title A long day\n  section Morning\n    Wake: 3: Me\n  section Noon\n    Eat: 5: Me\n  section Afternoon\n    Work: 2: Me\n  section Evening\n    Rest: 5: Me\n",
    ),
    (
        // A section name used twice, not next to each other, is **two bands**. They have to stay apart,
        // and each has to hold its own tasks.
        "journey-repeated-section",
        "journey\n  title Back and forth\n  section Office\n    Arrive: 4: Me\n  section Home\n    Lunch: 5: Me\n  section Office\n    Leave: 2: Me\n",
    ),
    (
        "journey-accessibility",
        "journey\n  title Checkout\n  accTitle: The checkout journey\n  accDescr: How a customer pays\n  section Pay\n    Enter card: 4: Customer\n",
    ),
    (
        // `journey` is one of the two languages where `#` starts a comment anywhere on a line.
        "journey-comments",
        "journey\n  %% a mermaid comment\n  title Comments\n  section Work # this is dropped\n    Do it: 3: Me\n",
    ),
    (
        "journey-long-name",
        "journey\n  title Long\n  section Work\n    A task whose name is long enough that the box under the face has to wrap it: 4: Me\n    Short: 2: Me\n",
    ),
    (
        "journey-one-task",
        "journey\n  Only: 3: Me\n",
    ),
    (
        // `getActors` is first-appearance order over the whole journey, not per section.
        "journey-actors-across-sections",
        "journey\n  title Who is here\n  section First\n    A: 3: Ann\n    B: 4: Bob, Ann\n  section Second\n    C: 5: Cara, Bob\n",
    ),
    (
        "journey-no-people-mixed",
        "journey\n  title Some have people\n  section Solo\n    Alone: 4\n    Together: 5: Me, You\n",
    ),
    // --- timeline ----------------------------------------------------------------------------
    (
        "timeline",
        "timeline\n    title History of Social Media Platform\n    2002 : LinkedIn\n    2004 : \
         Facebook : Google\n    2005 : YouTube\n    2006 : Twitter",
    ),
    (
        "timeline-sections",
        "timeline\n    title Timeline of Industrial Revolution\n    section 17th-20th century\n      \
         Industry 1.0 : Machinery, Water power, Steam power\n      Industry 2.0 : Electricity, \
         Internal combustion engine\n    section 21st century\n      Industry 4.0 : Internet, \
         Robotics",
    ),
    (
        "timeline-td",
        "timeline TD\n  title MermaidChart 2023 Timeline\n  section 2023 Q1\n    Bullet 1 : \
         sub-point 1a : sub-point 1b\n    Bullet 2 : sub-point 2a\n  section 2023 Q2\n    Bullet 3 \
         : sub-point 3a",
    ),
    (
        "timeline-lr",
        "timeline LR\n  title Explicit direction\n  2020 : Started\n  2021 : Grew\n  2022 : Shipped\n",
    ),
    (
        // An event line begins with `: ` and belongs to the period above it, which is how the
        // documentation writes a period with several events.
        "timeline-events-on-their-own-lines",
        "timeline\n  title Continuations\n  2002 : LinkedIn\n  2004 : Facebook\n       : Google\n       : Orkut\n",
    ),
    (
        // `":"\s(?:[^:\n]|":"(?!\s))+` — a colon **not** followed by whitespace stays inside the event,
        // and one that is followed by whitespace starts another.
        "timeline-colon-inside-an-event",
        "timeline\n  title Times\n  Monday : Stand-up at 09:30\n  Tuesday : Review at 14:00 : Retro at 16:00\n",
    ),
    (
        "timeline-cjk",
        "timeline\n  title 日本語の年表\n  section 前半\n    2002年 : 創業 : 最初の製品\n    2004年 : 上場\n  section 後半\n    2010年 : 海外展開\n",
    ),
    (
        // A period with no events is still a period — `addTask` runs on the `period` rule alone.
        "timeline-no-events",
        "timeline\n  title Periods only\n  2019\n  2020\n  2021\n",
    ),
    (
        "timeline-many-events",
        "timeline\n  title Busy\n  2023 : One : Two : Three : Four : Five\n  2024 : Only one\n",
    ),
    (
        "timeline-one-period",
        "timeline\n  2020 : Alone\n",
    ),
    (
        "timeline-long-event",
        "timeline\n  title Long\n  2024 : An event whose text is long enough that it has to wrap inside the column its period owns\n  2025 : short\n",
    ),
    (
        "timeline-repeated-section",
        "timeline\n  title Back and forth\n  section Growth\n    2019 : Hired\n  section Pause\n    2020 : Waited\n  section Growth\n    2021 : Hired again\n",
    ),
    (
        // Two different rules, one line apart. `"title"\s[^\n]+` takes the **rest of the line**,
        // `#` included — unlike `journey`, whose title rule is `[^#\n;]+` — while a period line
        // is reached only after `\#[^\n]*` has been skipped. So the title here keeps its `#` and
        // the period loses everything after one.
        "timeline-comments",
        "timeline\n  %% a mermaid comment\n  title Comments # kept, because the title rule takes \
         the whole line\n  2020 : Fine # and this half is dropped\n",
    ),
    (
        // Direction crossed with *no* sections: the vertical renderer and the banding are separate
        // decisions, and the corpus only had them together.
        "timeline-td-no-sections",
        "timeline TD\n  title Down the page\n  Q1 : Plan\n  Q2 : Build\n  Q3 : Ship\n",
    ),
    (
        "timeline-accessibility",
        "timeline\n  title Accessible\n  accTitle: A short timeline\n  accDescr: Three years\n  2020 : One\n  2021 : Two\n",
    ),
    (
        "timeline-uneven-sections",
        "timeline\n  title Uneven\n  section Long\n    2001 : a\n    2002 : b\n    2003 : c\n  section Short\n    2004 : d\n",
    ),
    // --- gantt -------------------------------------------------------------------------------
    (
        "gantt",
        "gantt\n    title A Gantt Diagram\n    dateFormat YYYY-MM-DD\n    section Section\n        A \
         task          :a1, 2014-01-01, 30d\n        Another task    :after a1, 20d\n    section \
         Another\n        Task in Another :2014-01-12, 12d\n        another task    :24d",
    ),
    (
        "gantt-tags",
        "gantt\n  dateFormat YYYY-MM-DD\n  axisFormat %d/%m\n  section Design\n    done task :done, \
         d1, 2024-01-01, 5d\n    active task :active, d2, after d1, 5d\n    critical :crit, d3, \
         after d2, 3d\n    a milestone :milestone, m1, after d3, 0d",
    ),
    (
        "gantt-weekends",
        "gantt\n  dateFormat YYYY-MM-DD\n  excludes weekends\n  section S\n    long :2024-01-01, 10d",
    ),
    (
        // Every corpus case had a `section`, so the un-banded chart — which is what a short gantt in a
        // README looks like — was never drawn.
        "gantt-no-section",
        "gantt\n  title No sections at all\n  dateFormat YYYY-MM-DD\n  first :a1, 2024-01-01, 5d\n  second :after a1, 3d\n",
    ),
    (
        // `until x` is the other half of `after x`, and neither is in the documentation's own example.
        "gantt-until",
        "gantt\n  dateFormat YYYY-MM-DD\n  section Work\n    build :b1, 2024-01-01, 10d\n    watch :w1, 2024-01-02, until b1\n",
    ),
    (
        "gantt-tick-interval",
        "gantt\n  title Weekly ticks\n  dateFormat YYYY-MM-DD\n  axisFormat %d/%m\n  tickInterval 1week\n  weekday monday\n  section S\n    long :2024-01-01, 28d\n",
    ),
    (
        "gantt-unix-dates",
        "gantt\n  title Unix seconds\n  dateFormat X\n  axisFormat %H:%M\n  section S\n    a :0, 3600\n    b :3600, 7200\n",
    ),
    (
        "gantt-inclusive-and-top-axis",
        "gantt\n  title Inclusive end dates\n  dateFormat YYYY-MM-DD\n  inclusiveEndDates\n  topAxis\n  section S\n    a :2024-01-01, 2024-01-05\n    b :2024-01-06, 2024-01-08\n",
    ),
    (
        // Both are read and not drawn — a terminal has nothing to click, and an unread line becomes a
        // phantom task called `click a1 href` (§2-3).
        "gantt-today-marker-and-click",
        "gantt\n  dateFormat YYYY-MM-DD\n  todayMarker off\n  section S\n    a :a1, 2024-01-01, 5d\n    click a1 href \"https://example.invalid\"\n",
    ),
    (
        "gantt-cjk",
        "gantt\n  title 開発計画\n  dateFormat YYYY-MM-DD\n  section 設計\n    要件をまとめる :a1, 2024-01-01, 7d\n    設計書を書く :after a1, 5d\n  section 実装\n    実装する :2024-01-15, 20d\n",
    ),
    (
        "gantt-includes",
        "gantt\n  dateFormat YYYY-MM-DD\n  excludes weekends\n  includes 2024-01-06\n  section S\n    a :2024-01-01, 8d\n",
    ),
    (
        // A section written twice, apart, is **two bands**. Every band is a frame, and two frames with
        // the same name still have to be told apart — which is what a repeated title tests.
        "gantt-repeated-section",
        "gantt\n  title Back to the first section\n  dateFormat YYYY-MM-DD\n  section Design\n    a :2024-01-01, 3d\n  section Build\n    b :2024-01-04, 3d\n  section Design\n    c :2024-01-07, 3d\n",
    ),
    (
        // Three fields means **id**, start, end; two means start, end; one means end alone. Getting the
        // count rule wrong turns `i1` into a date.
        "gantt-explicit-ids",
        "gantt\n  dateFormat YYYY-MM-DD\n  section Chain\n    one :i1, 2024-01-01, 2d\n    two :i2, after i1, 2d\n    three :i3, after i2, 2d\n    four :after i3, 2d\n",
    ),
    (
        "gantt-long-names",
        "gantt\n  dateFormat YYYY-MM-DD\n  section S\n    A task whose name is much longer than the bar it labels :2024-01-01, 2d\n    b :2024-01-03, 2d\n",
    ),
    (
        "gantt-one-task",
        "gantt\n  dateFormat YYYY-MM-DD\n  section S\n    only :2024-01-01, 4d\n",
    ),
    (
        // A milestone is a moment, so it contributes a centre and not a bar — and the chart's own extent
        // starts and ends on one here, which is where a bound that only allowed for the plot fails.
        "gantt-milestone-only",
        "gantt\n  title Milestones and a bar\n  dateFormat YYYY-MM-DD\n  section S\n    kickoff :milestone, m1, 2024-01-01, 0d\n    work :2024-01-01, 5d\n    launch :milestone, m2, 2024-01-06, 0d\n",
    ),
    (
        "gantt-vert",
        "gantt\n  dateFormat YYYY-MM-DD\n  section S\n    a :2024-01-01, 5d\n    marker :vert, v1, 2024-01-03, 0d\n",
    ),
    (
        // A milestone written **with a duration**. `ganttDb` sets a milestone's end to its start
        // whatever the source asked for, because a milestone is a moment — and every milestone in
        // the corpus was written `0d`, which makes that rule and *no rule at all* the same
        // drawing. A mutation that deleted it therefore survived.
        "gantt-milestone-with-a-duration",
        "gantt\n  dateFormat YYYY-MM-DD\n  section S\n    review :milestone, m1, 2024-01-01, \
         5d\n    work :2024-01-02, 6d\n",
    ),
    // --- requirement -------------------------------------------------------------------------
    (
        "requirement",
        "requirementDiagram\n\n  requirement test_req {\n  id: 1\n  text: the test text.\n  risk: \
         high\n  verifymethod: test\n  }\n\n  functionalRequirement test_req2 {\n  id: 1.1\n  text: \
         the second test text.\n  risk: low\n  verifymethod: inspection\n  }\n\n  element \
         test_entity {\n  type: simulation\n  }\n\n  test_entity - satisfies -> test_req2\n  \
         test_req - contains -> test_req2",
    ),
    (
        "requirement-directions",
        "requirementDiagram\n  direction LR\n  requirement a {\n  id: 1\n  }\n  requirement b {\n  \
         id: 2\n  }\n  a <- derives - b",
    ),
    (
        "requirement-every-type",
        "requirementDiagram\n  requirement r1 {\n  id: 1\n  text: plain\n  }\n  functionalRequirement r2 {\n  id: 2\n  }\n  interfaceRequirement r3 {\n  id: 3\n  }\n  performanceRequirement r4 {\n  id: 4\n  }\n  physicalRequirement r5 {\n  id: 5\n  }\n  designConstraint r6 {\n  id: 6\n  }\n  r1 - contains -> r2\n  r1 - contains -> r3\n",
    ),
    (
        // All seven `relationship` terminals. The corpus had two.
        "requirement-every-relation",
        "requirementDiagram\n  requirement a {\n  id: 1\n  }\n  requirement b {\n  id: 2\n  }\n  element e {\n  type: test\n  }\n  a - contains -> b\n  a - copies -> b\n  a - derives -> b\n  e - satisfies -> a\n  e - verifies -> b\n  a - refines -> b\n  a - traces -> b\n",
    ),
    (
        "requirement-risks-and-methods",
        "requirementDiagram\n  requirement low {\n  id: 1\n  risk: low\n  verifymethod: analysis\n  }\n  requirement medium {\n  id: 2\n  risk: medium\n  verifymethod: inspection\n  }\n  requirement high {\n  id: 3\n  risk: high\n  verifymethod: demonstration\n  }\n  requirement tested {\n  id: 4\n  verifymethod: test\n  }\n  low - traces -> high\n",
    ),
    (
        "requirement-element-docref",
        "requirementDiagram\n  requirement r {\n  id: 1\n  text: needs a source\n  }\n  element doc {\n  type: document\n  docRef: docs/PRD.md\n  }\n  doc - traces -> r\n",
    ),
    (
        "requirement-quoted",
        "requirementDiagram\n  requirement \"a quoted name\" {\n  id: \"1.2.3\"\n  text: \"a quoted, comma-bearing text\"\n  risk: high\n  }\n  element \"a quoted element\" {\n  type: \"a quoted type\"\n  }\n  \"a quoted element\" - satisfies -> \"a quoted name\"\n",
    ),
    (
        // Colour rules are read and dropped. A line the grammar has a rule for and the parser has none
        // for becomes a phantom box (§2-3), which is the defect this shape guards.
        "requirement-styles",
        "requirementDiagram\n  requirement a {\n  id: 1\n  }\n  requirement b {\n  id: 2\n  }\n  classDef hot fill:#f96\n  class a hot\n  style b fill:#333\n  a - derives -> b\n",
    ),
    (
        "requirement-cjk",
        "requirementDiagram\n  requirement 要件A {\n  id: 1\n  text: 全画面で読めること\n  risk: high\n  verifymethod: test\n  }\n  element 検証環境 {\n  type: simulation\n  }\n  検証環境 - verifies -> 要件A\n",
    ),
    (
        "requirement-direction-tb",
        "requirementDiagram\n  direction TB\n  requirement a {\n  id: 1\n  }\n  requirement b {\n  id: 2\n  }\n  a - contains -> b\n",
    ),
    (
        "requirement-direction-rl",
        "requirementDiagram\n  direction RL\n  requirement a {\n  id: 1\n  }\n  requirement b {\n  id: 2\n  }\n  b <- derives - a\n",
    ),
    (
        // Every field is optional, so a body with nothing in it is legal and draws a box with only a
        // name in it.
        "requirement-empty-body",
        "requirementDiagram\n  requirement bare {\n  }\n  element thing {\n  }\n  thing - satisfies -> bare\n",
    ),
    (
        "requirement-chain",
        "requirementDiagram\n  requirement top {\n  id: 1\n  }\n  functionalRequirement mid {\n  id: 1.1\n  }\n  functionalRequirement leaf {\n  id: 1.1.1\n  }\n  element impl {\n  type: code\n  }\n  top - contains -> mid\n  mid - contains -> leaf\n  impl - satisfies -> leaf\n",
    ),
    (
        // `A - r -> B` and `A <- r - B` put the source at opposite ends. Both, on the same pair, is what
        // makes a swapped reading visible.
        "requirement-both-arrows",
        "requirementDiagram\n  requirement a {\n  id: 1\n  }\n  requirement b {\n  id: 2\n  }\n  a - derives -> b\n  a <- traces - b\n",
    ),
    (
        "requirement-long-text",
        "requirementDiagram\n  requirement r {\n  id: 1\n  text: a requirement text long enough that the box has to wrap it over more than one line to stay readable\n  }\n  element e {\n  type: manual\n  }\n  e - verifies -> r\n",
    ),
    (
        "requirement-one-box",
        "requirementDiagram\n  requirement alone {\n  id: 1\n  text: nothing points at it\n  }\n",
    ),
    (
        // **Two** elements. Every case had one, so "an element box holds its own element's rows"
        // and "every element box holds the first element's rows" were the same drawing.
        "requirement-two-elements",
        "requirementDiagram\n  requirement r {\n  id: 1\n  }\n  element rig {\n  type: \
         simulation\n  docRef: docs/rig.md\n  }\n  element bench {\n  type: hardware\n  \
         docRef: docs/bench.md\n  }\n  rig - verifies -> r\n  bench - satisfies -> r\n",
    ),
    // --- gitgraph ----------------------------------------------------------------------------
    (
        "gitgraph",
        "gitGraph\n   commit\n   commit\n   branch develop\n   checkout develop\n   commit\n   \
         commit\n   checkout main\n   merge develop\n   commit\n   commit",
    ),
    (
        "gitgraph-types",
        "gitGraph\n   commit id: \"Normal\" tag: \"v1.0.0\"\n   commit\n   commit id: \"Reverse\" \
         type: REVERSE tag: \"RC_1\"\n   commit id: \"Highlight\" type: HIGHLIGHT",
    ),
    (
        "gitgraph-cherry-pick",
        "gitGraph\n    commit id: \"ZERO\"\n    branch develop\n    branch release\n    commit \
         id:\"A\"\n    checkout main\n    commit id:\"ONE\"\n    checkout develop\n    commit \
         id:\"B\"\n    checkout main\n    merge develop id:\"MERGE\"\n    commit id:\"TWO\"\n    \
         checkout release\n    cherry-pick id:\"MERGE\" parent:\"B\"",
    ),
    (
        "gitgraph-tb",
        "gitGraph TB:\n  commit\n  branch develop\n  commit\n  checkout main\n  merge develop",
    ),
    (
        "gitgraph-lr-explicit",
        "gitGraph LR:\n  commit\n  branch feature\n  commit\n  checkout main\n  merge feature\n",
    ),
    (
        "gitgraph-bt",
        "gitGraph BT:\n  commit\n  branch develop\n  commit\n  checkout main\n  commit\n  merge develop\n",
    ),
    (
        // `switch` is `checkout` — the langium grammar lists both spellings.
        "gitgraph-switch",
        "gitGraph\n  commit\n  branch topic\n  switch topic\n  commit\n  switch main\n  merge topic\n",
    ),
    (
        // `order:` overrides which lane a branch is drawn on, which is a statement about the picture and
        // not about the history.
        "gitgraph-branch-order",
        "gitGraph\n  commit\n  branch last order: 9\n  branch first order: 1\n  checkout first\n  commit\n  checkout last\n  commit\n",
    ),
    (
        // Every field may come in any order and `tag:` **accumulates** while the others overwrite.
        "gitgraph-fields-in-any-order",
        "gitGraph\n  commit tag: \"v1\" id: \"one\" type: REVERSE\n  commit type: HIGHLIGHT id: \"two\"\n  commit id: \"three\" tag: \"v2\" tag: \"v3\"\n",
    ),
    (
        // A bare string is the message; `msg:` is optional.
        "gitgraph-messages",
        "gitGraph\n  commit msg: \"with the keyword\"\n  commit \"without it\"\n  commit id: \"third\" msg: \"both\"\n",
    ),
    (
        "gitgraph-many-branches",
        "gitGraph\n  commit\n  branch a\n  commit\n  branch b\n  commit\n  branch c\n  commit\n  checkout main\n  merge a\n  merge b\n  merge c\n",
    ),
    (
        "gitgraph-merge-fields",
        "gitGraph\n  commit\n  branch topic\n  commit\n  checkout main\n  merge topic id: \"the merge\" tag: \"v2.0\" type: HIGHLIGHT\n",
    ),
    (
        "gitgraph-cjk",
        "gitGraph\n  commit id: \"最初\" tag: \"リリース\"\n  branch 開発\n  commit id: \"作業\"\n  checkout main\n  merge 開発\n",
    ),
    (
        "gitgraph-one-commit",
        "gitGraph\n  commit\n",
    ),
    (
        "gitgraph-long-ids",
        "gitGraph\n  commit id: \"a commit message long enough to be wider than the lane it sits on\"\n  commit id: \"short\"\n",
    ),
    (
        // Direction crossed with a cherry-pick: the two were only ever exercised apart.
        "gitgraph-cherry-pick-tb",
        "gitGraph TB:\n  commit id: \"ZERO\"\n  branch develop\n  commit id: \"A\"\n  checkout main\n  cherry-pick id: \"A\"\n",
    ),
    // --- c4 ----------------------------------------------------------------------------------
    (
        "c4-context",
        "C4Context\n  title System Context diagram for Internet Banking System\n  \
         Enterprise_Boundary(b0, \"BankBoundary0\") {\n    Person(customerA, \"Banking Customer \
         A\", \"A customer of the bank.\")\n    System(SystemAA, \"Internet Banking System\", \
         \"Allows customers to view information.\")\n  }\n  System_Ext(SystemC, \"E-mail system\", \
         \"The internal Microsoft Exchange system.\")\n  Rel(customerA, SystemAA, \"Uses\")\n  \
         Rel(SystemAA, SystemC, \"Sends e-mails\", \"SMTP\")",
    ),
    (
        "c4-container",
        "C4Container\n  title Containers\n  Container_Boundary(c1, \"Internet Banking\") {\n    \
         Container(web_app, \"Web Application\", \"Java, Spring MVC\", \"Delivers the content.\")\n \
         ContainerDb(database, \"Database\", \"SQL Database\", \"Stores information.\")\n    \
         ContainerQueue(queue, \"Events\", \"Kafka\")\n  }\n  Rel(web_app, database, \"Reads from \
         and writes to\", \"JDBC\")",
    ),
    (
        "c4-deployment",
        "C4Deployment\n  Deployment_Node(dn, \"Big Bank plc\", \"Ubuntu 16.04 LTS\") {\n    \
         Deployment_Node(inner, \"bigbank-api\", \"Docker\") {\n      Container(api, \"API \
         Application\", \"Java\")\n    }\n  }\n  Person(user, \"Customer\")\n  Rel(user, api, \
         \"Uses\", \"HTTPS\")",
    ),
    (
        "c4-component",
        "C4Component\n  title Components of the API\n  Container_Boundary(api, \"API Application\") {\n    Component(sign, \"Sign In Controller\", \"MVC Rest Controller\", \"Allows users to sign in.\")\n    ComponentDb(store, \"Session Store\", \"Redis\", \"Holds sessions.\")\n    ComponentQueue(events, \"Events\", \"Kafka\")\n  }\n  Rel(sign, store, \"Reads and writes\", \"Jedis\")\n",
    ),
    (
        "c4-dynamic",
        "C4Dynamic\n  title A dynamic diagram\n  Person(customer, \"Customer\")\n  Container(spa, \"Single Page App\", \"JavaScript\")\n  Container(api, \"API\", \"Java\")\n  RelIndex(1, customer, spa, \"Submits\")\n  RelIndex(2, spa, api, \"Calls\")\n",
    ),
    (
        "c4-external-and-shapes",
        "C4Context\n  Person(a, \"Inside\", \"A person here.\")\n  Person_Ext(b, \"Outside\", \"A person elsewhere.\")\n  System(c, \"Ours\")\n  System_Ext(d, \"Theirs\")\n  SystemDb(e, \"Our database\")\n  SystemQueue(f, \"Our queue\")\n  Rel(a, c, \"Uses\")\n  Rel(b, d, \"Uses\")\n",
    ),
    (
        "c4-relationship-directions",
        "C4Context\n  System(a, \"A\")\n  System(b, \"B\")\n  System(c, \"C\")\n  Rel_U(a, b, \"up\")\n  Rel_D(a, c, \"down\")\n  Rel_L(b, c, \"left\")\n  Rel_R(c, b, \"right\")\n  BiRel(a, c, \"both ways\")\n  Rel_Back(b, a, \"back\")\n",
    ),
    (
        // `$key="value"` does not consume a position, which is the part a positional reader gets wrong.
        "c4-named-arguments",
        "C4Context\n  Person(a, \"A person\", $tags=\"v1\", \"a description that is still the third positional\")\n  System(b, \"A system\", \"its description\", $tags=\"v2\")\n  Rel(a, b, \"Uses\", $tags=\"v1\")\n",
    ),
    (
        // Appearance, read and dropped — §0-1 settles that konoma decides its own.
        "c4-update-styles",
        "C4Context\n  Person(a, \"A\")\n  System(b, \"B\")\n  Rel(a, b, \"Uses\")\n  UpdateElementStyle(a, $fontColor=\"red\", $bgColor=\"grey\")\n  UpdateRelStyle(a, b, $textColor=\"blue\", $offsetY=\"-10\")\n  UpdateLayoutConfig($c4ShapeInRow=\"3\", $c4BoundaryInRow=\"1\")\n",
    ),
    (
        "c4-cjk",
        "C4Context\n  title システム構成\n  Enterprise_Boundary(b0, \"社内\") {\n    Person(user, \"利用者\", \"社内の担当者。\")\n    System(app, \"業務システム\", \"申請を受け付ける。\")\n  }\n  System_Ext(mail, \"メール基盤\", \"社外の配信基盤。\")\n  Rel(user, app, \"利用する\")\n  Rel(app, mail, \"送信する\", \"SMTP\")\n",
    ),
    (
        // Three frames inside one another, which is where a frame that forgot its parent stops being
        // checked at all (§6-A item 14).
        "c4-boundaries-three-deep",
        "C4Deployment\n  Deployment_Node(dc, \"Data centre\", \"Ubuntu\") {\n    Deployment_Node(host, \"Host\", \"Docker\") {\n      Deployment_Node(pod, \"Pod\", \"Kubernetes\") {\n        Container(api, \"API\", \"Java\")\n      }\n    }\n  }\n  Person(u, \"User\")\n  Rel(u, api, \"Uses\", \"HTTPS\")\n",
    ),
    (
        "c4-boundary-brace-on-the-next-line",
        "C4Context\n  System_Boundary(b1, \"Boundary\")\n  {\n    System(a, \"A\")\n    System(b, \"B\")\n  }\n  Rel(a, b, \"Uses\")\n",
    ),
    (
        "c4-flat",
        "C4Context\n  title No boundaries at all\n  Person(a, \"A\")\n  System(b, \"B\")\n  System(c, \"C\")\n  Rel(a, b, \"Uses\")\n  Rel(b, c, \"Calls\", \"gRPC\")\n",
    ),
    (
        "c4-long-descriptions",
        "C4Context\n  Person(a, \"A person with a long name indeed\", \"A description long enough that the box has to wrap it over several lines before it can be read.\")\n  System(b, \"B\")\n  Rel(a, b, \"Uses in a way that needs explaining at length\", \"HTTPS/1.1\")\n",
    ),
    (
        // Two frames side by side with a line between them: the case that separates "inside its own
        // frame" from "inside some frame".
        "c4-two-boundaries",
        "C4Context\n  Enterprise_Boundary(left, \"Left\") {\n    System(a, \"A\")\n  }\n  Enterprise_Boundary(right, \"Right\") {\n    System(b, \"B\")\n  }\n  Rel(a, b, \"Crosses\")\n",
    ),
    (
        "c4-direction",
        "C4Context\n  direction LR\n  Person(a, \"A\")\n  System(b, \"B\")\n  Rel(a, b, \"Uses\")\n",
    ),
    // --- block -------------------------------------------------------------------------------
    (
        "block",
        "block-beta\ncolumns 1\n  db((\"DB\"))\n  blockArrowId6<[\"  \"]>(down)\n  block:ID\n    A\n \
         B[\"A wide one in the middle\"]\n    C\n  end\n  space\n  D\n  ID --> D\n  C --> D",
    ),
    (
        "block-columns",
        "block-beta\n  columns 3\n  a[\"A label\"] b:2 c:2 d\n  e f g",
    ),
    (
        // A `columns` count that has to wrap, with nothing else in the way: the row a block lands
        // on is what `columns` *means*.
        "block-wrap",
        "block-beta\n  columns 2\n  a b c d e",
    ),
    (
        "block-arrows",
        "block-beta\n  blockArrowId<[\"right\"]>(right)\n  blockArrowId2<[\"left\"]>(left)\n  \
         blockArrowId5<[\"both\"]>(x)",
    ),
    (
        "block-link",
        "block-beta\n  columns 5\n  a b c d e\n  a --> e\n  a-- \"labelled\" -->c",
    ),
    (
        // A `block:` inside a `block:` is documented syntax and was not in the corpus, which is how a
        // nested frame that never recorded its parent survived (§6-A item 14).
        "block-nested",
        "block-beta\n  columns 1\n  block:outer\n    columns 2\n    x y\n    block:inner\n      columns 1\n      z w\n    end\n    q\n  end\n  tail\n",
    ),
    (
        // An edge between two blocks **inside** different frames, and one between the frames themselves.
        "block-nested-edge",
        "block-beta\n  columns 2\n  block:left\n    columns 1\n    a\n    b\n  end\n  block:right\n    columns 1\n    c\n  end\n  a --> c\n  left --> right\n",
    ),
    (
        "block-shapes",
        "block-beta\n  columns 4\n  a[\"square\"] b(\"round\") c([\"stadium\"]) d[[\"subroutine\"]]\n  e[(\"cylinder\")] f((\"circle\")) g{\"diamond\"} h{{\"hexagon\"}}\n  i[/\"lean right\"/] j[\\\"lean left\"\\] k[/\"trapezoid\"\\] l[\\\"inv trapezoid\"/]\n",
    ),
    (
        // `space:n` leaves n cells empty, which is the only way to say "nothing here" on this grid.
        "block-space-sized",
        "block-beta\n  columns 4\n  a space:2 b\n  c d e f\n",
    ),
    (
        "block-arrows-vertical",
        "block-beta\n  columns 3\n  up<[\"up\"]>(up) down<[\"down\"]>(down) both<[\"y\"]>(y)\n",
    ),
    (
        // A composite may span columns too — `block:id:n`.
        "block-composite-width",
        "block-beta\n  columns 3\n  block:wide:2\n    columns 1\n    a\n    b\n  end\n  c\n  d e f\n",
    ),
    (
        "block-cjk",
        "block-beta\n  columns 3\n  設定[\"設定を読む\"] 解決[\"種別を解決\"] 描画[\"全画面で描く\"]\n  設定 --> 解決\n  解決 --> 描画\n",
    ),
    (
        // `columns auto` is `-1` and means one row as wide as it needs to be. It is also the default,
        // so writing it is the only way to tell the two apart.
        "block-columns-auto",
        "block-beta\n  columns auto\n  a b c d\n",
    ),
    (
        "block-styles",
        "block-beta\n  columns 2\n  a b\n  classDef hot fill:#f96\n  class a hot\n  style b fill:#333\n  a --> b\n",
    ),
    (
        "block-one",
        "block-beta\n  only\n",
    ),
    (
        "block-long-label",
        "block-beta\n  columns 2\n  a[\"A label long enough that the cell it sits in has to grow or wrap it\"] b[\"ok\"]\n",
    ),
    (
        "block-twelve",
        "block-beta\n  columns 4\n  a b c d\n  e f g h\n  i j k l\n  a --> l\n",
    ),
    (
        "block-edges-every-stroke",
        "block-beta\n  columns 5\n  a b c d e\n  a --> b\n  b --- c\n  c -.-> d\n  d ==> e\n  a -- \"labelled\" --> e\n",
    ),
    // --- architecture ------------------------------------------------------------------------
    (
        "architecture",
        "architecture-beta\n    group api(cloud)[API]\n\n    service db(database)[Database] in api\n \
         service disk1(disk)[Storage] in api\n    service disk2(disk)[Storage] in api\n    service \
         server(server)[Server] in api\n\n    db:L -- R:server\n    disk1:T -- B:server\n    \
         disk2:T -- B:db",
    ),
    (
        "architecture-groups",
        "architecture-beta\n    group sources(cloud)[Sources]\n        service src_a(server)[Source A] \
         in sources\n        service src_b(server)[Source B] in sources\n\n    group \
         storage(database)[Storage]\n        service db_one(database)[DB One] in storage\n        \
         service db_two(database)[DB Two] in storage\n\n    src_a:B --> T:db_one\n    src_b:B --> \
         T:db_two",
    ),
    (
        "architecture-junction",
        "architecture-beta\n    service left_disk(disk)[Disk]\n    service top_disk(disk)[Disk]\n    \
         service bottom_disk(disk)[Disk]\n    junction junctionCenter\n\n    left_disk:R -- \
         L:junctionCenter\n    top_disk:B -- T:junctionCenter\n    bottom_disk:T -- B:junctionCenter",
    ),
    (
        "architecture-align",
        "architecture-beta\n    service src1(server)[Source 1]\n    service src2(server)[Source 2]\n \
         service proc(server)[Processor]\n    src1:B --> T:proc\n    src2:B --> T:proc\n    align \
         row src1 src2",
    ),
    (
        // `group … in …` is documented syntax and was not in the corpus, which is how a group frame that
        // escaped its parent by twelve pixels survived a whole stage.
        "architecture-nested",
        "architecture-beta\n  group outer(cloud)[Outer]\n  group inner(cloud)[Inner] in outer\n  service s1(server)[S1] in inner\n  service s2(server)[S2] in outer\n  s1:R --> L:s2\n",
    ),
    (
        "architecture-nested-thrice",
        "architecture-beta\n  group a(cloud)[A]\n  group b(cloud)[B] in a\n  group c(cloud)[C] in b\n  service s(server)[S] in c\n  service t(server)[T] in a\n",
    ),
    (
        "architecture-titled-edges",
        "architecture-beta\n  service web(server)[Web]\n  service db(database)[Database]\n  service cache(disk)[Cache]\n  web:R -[reads]- L:db\n  web:B -[writes]- T:cache\n",
    ),
    (
        // All four end decorations: none, one at the far end, one at the near end, and both.
        //
        // Written as one chain on purpose. Two edges reaching the same service is a *different*
        // case — the grid walk honours the first and contradicts the second — and it is stated
        // on its own below rather than being smuggled in here, where it would only look like an
        // arrowhead test that fails.
        "architecture-arrow-forms",
        "architecture-beta\n  service a(server)[A]\n  service b(server)[B]\n  service c(server)[C]\n  service d(server)[D]\n  service e(server)[E]\n  a:R -- L:b\n  b:R --> L:c\n  c:R <-- L:d\n  d:R <--> L:e\n",
    ),
    (
        // `{group}` attaches the line to the group's border upstream. konoma reads it and draws to the
        // service, which is the same two things joined — but it has to be *read*, or it becomes part of
        // the id.
        "architecture-group-edge-end",
        "architecture-beta\n  group g(cloud)[G]\n  service a(server)[A] in g\n  service b(server)[B]\n  a{group}:R --> L:b\n",
    ),
    (
        "architecture-cjk",
        "architecture-beta\n  group 社内(cloud)[社内ネットワーク]\n  service 蓄積(database)[データベース] in 社内\n  service 配信(server)[配信サーバ] in 社内\n  蓄積:R -- L:配信\n",
    ),
    (
        "architecture-quoted-icon",
        "architecture-beta\n  service a(\"🚀\")[Launcher]\n  service b(\"📦\")[Store]\n  a:R --> L:b\n",
    ),
    (
        "architecture-align-column",
        "architecture-beta\n  service a(server)[A]\n  service b(server)[B]\n  service c(server)[C]\n  a:R --> L:c\n  b:R --> L:c\n  align column a b\n",
    ),
    (
        "architecture-one-service",
        "architecture-beta\n  service alone(server)[Alone]\n",
    ),
    (
        "architecture-long-labels",
        "architecture-beta\n  service a(server)[A service whose name is long enough to widen its own cell]\n  service b(disk)[Short]\n  a:R -- L:b\n",
    ),
    (
        "architecture-many-services",
        "architecture-beta\n  service s1(server)[S1]\n  service s2(server)[S2]\n  service s3(server)[S3]\n  service s4(server)[S4]\n  service s5(server)[S5]\n  service s6(server)[S6]\n  s1:R -- L:s2\n  s2:R -- L:s3\n  s4:R -- L:s5\n  s5:R -- L:s6\n  s1:B -- T:s4\n",
    ),
    (
        "architecture-junction-cross",
        "architecture-beta\n  service n(server)[North]\n  service s(server)[South]\n  service e(server)[East]\n  service w(server)[West]\n  junction j\n  n:B -- T:j\n  s:T -- B:j\n  e:L -- R:j\n  w:R -- L:j\n",
    ),
    (
        // A service in no group at all, beside two that are: "inside the groups it is written in, and
        // inside no other" has to hold for a service written in none.
        "architecture-groups-and-a-loose-service",
        "architecture-beta\n  group g1(cloud)[One]\n  service a(server)[A] in g1\n  group g2(cloud)[Two]\n  service b(server)[B] in g2\n  service loose(server)[Loose]\n  a:R --> L:b\n  b:B --> T:loose\n",
    ),
    // --- zenuml ------------------------------------------------------------------------------
    (
        "zenuml",
        "zenuml\n    title Demo\n    Alice->John: Hello John, how are you?\n    John->Alice: \
         Great!\n    Alice->John: See you later!",
    ),
    (
        "zenuml-sync",
        "zenuml\n    Client->A.method() {\n      B.method() {\n        return x1\n      }\n      \
         return x2\n    }",
    ),
    (
        "zenuml-blocks",
        "zenuml\n  @Actor Alice\n  Alice->Bob: Hello Bob, how are you?\n  if(is_sick) {\n    \
         Bob->Alice: Not so good\n  } else {\n    Bob->Alice: Fresh as a daisy\n  }\n  while(true) \
         {\n    Alice->Bob: again\n  }",
    ),
    (
        "zenuml-par-and-opt",
        "zenuml\n  A->B: start\n  par {\n    A->B: one\n    A->C: two\n  }\n  opt {\n    B->A: maybe\n  }\n",
    ),
    (
        "zenuml-try-catch-finally",
        "zenuml\n  Client->Service: call\n  try {\n    Service->Db: read\n  } catch {\n    Service->Client: error\n  } finally {\n    Service->Client: done\n  }\n",
    ),
    (
        "zenuml-new",
        "zenuml\n  @Starter(Client)\n  new Order(id)\n  Order.total() {\n    return 42\n  }\n",
    ),
    (
        // `x = A.m()` is the call **and** a dotted reply labelled `x`; a typed declaration in front of it
        // is the same statement.
        "zenuml-assignment",
        "zenuml\n  total = Order.total()\n  int count = Cart.size()\n  Cart.clear()\n",
    ),
    (
        "zenuml-else-if",
        "zenuml\n  A->B: ask\n  if(first) {\n    B->A: one\n  } else if(second) {\n    B->A: two\n  } else {\n    B->A: three\n  }\n",
    ),
    (
        "zenuml-annotators",
        "zenuml\n  @Actor Alice\n  @Database Store\n  @Boundary Gate\n  Alice->Gate: request\n  Gate->Store: query\n  Store->Alice: rows\n",
    ),
    (
        "zenuml-cjk",
        "zenuml\n  title 注文の流れ\n  @Actor 利用者\n  利用者->受付: 注文する\n  受付->倉庫: 在庫を確認\n  倉庫->受付: 在庫あり\n  受付->利用者: 受け付けました\n",
    ),
    (
        "zenuml-comments",
        "zenuml\n  // a comment, not a participant\n  A->B: one\n  // another\n  B->A: two\n",
    ),
    (
        "zenuml-one-message",
        "zenuml\n  A->B: only\n",
    ),
    (
        "zenuml-deep-nesting",
        "zenuml\n  Client->A.one() {\n    B.two() {\n      C.three() {\n        D.four() {\n          return d\n        }\n        return c\n      }\n      return b\n    }\n    return a\n  }\n",
    ),
    (
        "zenuml-long-message",
        "zenuml\n  A->B: a message long enough that the arrow it labels has to be widened or the words wrapped\n  B->A: ok\n",
    ),
    (
        // A frame inside a frame, in the one language here that is a sequence diagram: the nesting case
        // the corpus had in no other form.
        "zenuml-loop-inside-alt",
        "zenuml\n  A->B: begin\n  if(needed) {\n    while(more) {\n      A->B: again\n    }\n  } else {\n    A->B: skip\n  }\n",
    ),
    (
        "zenuml-reply-forms",
        "zenuml\n  Client->Service: call\n  @return\n  Service->Client: reply\n  Service.work() {\n    return done\n  }\n",
    ),
];

// ---------------------------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------------------------

/// Every case of one kind, **found by that kind's own routing predicate**.
///
/// The `cases_of` pattern §6-A item 10 settled: a hard-coded list of case names stops covering the
/// corpus the moment somebody adds a case, and that is how a radar mutation survived twice.
pub fn cases_of(is_ours: fn(&str) -> bool) -> Vec<(&'static str, &'static str)> {
    let found: Vec<(&str, &str)> = CASES
        .iter()
        .filter(|(_, src)| is_ours(src))
        .copied()
        .collect();
    assert!(!found.is_empty(), "the corpus has no case of this kind");
    found
}

/// Which band each row of a banded diagram belongs to, counted in **runs**.
///
/// The four banded languages all draw one frame per run of consecutive rows sharing a section
/// name, so a name written twice with another between the two is two frames. Counting the runs
/// here — from the model, by the language's own rule — is what lets a test name the *second*
/// `Design` band rather than asking for `Design` and being handed the first.
fn section_runs<'a>(names: impl Iterator<Item = &'a str>) -> Vec<usize> {
    let mut out = Vec::new();
    let mut open: Option<&str> = None;
    let mut run = 0usize;
    for name in names {
        match open {
            Some(previous) if previous == name => {}
            Some(_) => run += 1,
            None => {}
        }
        open = Some(name);
        out.push(run);
    }
    out
}

/// Lays a source out, whichever of the eleven it is.
pub fn laid_out(src: &str) -> Diagram {
    dispatch(src).unwrap_or_else(|e| panic!("corpus source must lay out: {e}\n{src}"))
}

fn dispatch(src: &str) -> Result<Diagram, RenderError> {
    use super as draw;
    use crate::preview::mermaid as m;
    if m::mindmap::is_mindmap(src) {
        return draw::mindmap::lay_out(&m::mindmap::parse(src)?);
    }
    if m::kanban::is_kanban(src) {
        return draw::kanban::lay_out(&m::kanban::parse(src)?);
    }
    if m::journey::is_journey(src) {
        return draw::journey::lay_out(&m::journey::parse(src)?);
    }
    if m::timeline::is_timeline(src) {
        return draw::timeline::lay_out(&m::timeline::parse(src)?);
    }
    if m::gantt::is_gantt(src) {
        return draw::gantt::lay_out(&m::gantt::parse(src)?);
    }
    if m::requirement::is_requirement_diagram(src) {
        return draw::requirement::lay_out(&m::requirement::parse(src)?);
    }
    if m::gitgraph::is_git_graph(src) {
        return draw::gitgraph::lay_out(&m::gitgraph::parse(src)?);
    }
    if m::c4::is_c4(src) {
        return draw::c4::lay_out(&m::c4::parse(src)?);
    }
    if m::block::is_block_diagram(src) {
        return draw::block::lay_out(&m::block::parse(src)?);
    }
    if m::architecture::is_architecture(src) {
        return draw::architecture::lay_out(&m::architecture::parse(src)?);
    }
    if m::zenuml::is_zenuml(src) {
        return draw::zenuml::lay_out(src);
    }
    panic!("no stage 5b parser claims this source")
}

/// The rendered SVG, through the production entry point.
fn rendered(src: &str) -> String {
    mermaid_to_svg_reason(src, "dark").unwrap_or_else(|e| panic!("{e}\n{src}"))
}

/// Which languages a case belongs to, for the invariant tables below.
fn is_graph_kind(src: &str) -> bool {
    use crate::preview::mermaid as m;
    m::mindmap::is_mindmap(src) || m::requirement::is_requirement_diagram(src) || m::c4::is_c4(src)
}

fn is_grid_kind(src: &str) -> bool {
    use crate::preview::mermaid as m;
    m::architecture::is_architecture(src) || m::block::is_block_diagram(src)
}

fn is_banded_kind(src: &str) -> bool {
    use crate::preview::mermaid as m;
    m::journey::is_journey(src)
        || m::timeline::is_timeline(src)
        || m::gantt::is_gantt(src)
        || m::kanban::is_kanban(src)
}

// ---------------------------------------------------------------------------------------------
// 1. The main instrument: what konoma measured vs what resvg drew
// ---------------------------------------------------------------------------------------------

/// Label strings the instrument runs over — every category §6 names, plus the double space.
const LABELS: &[(&str, &str)] = &[
    ("ascii", "Revenue"),
    ("kerning", "AVATAR To Wa"),
    ("thin", "illicit lilli"),
    ("cjk", "全画面プレビュー"),
    ("cjk-punct", "開始、処理。「確認」"),
    ("emoji", "build 🚀 ship"),
    ("digits", "0123456789"),
    ("mixed", "konoma の preview を全画面 fullscreen で"),
    ("double-space", "loop  Every minute"),
];

/// **The instrument §6 asks for, for every new kind of label stage 5b draws.**
///
/// A [`Glyph::ChartLabel`]'s box **is** its ink, so the relation is the sharp one — the number
/// konoma sized the box to and the number resvg laid the glyphs out at are the *same number*, with
/// nothing in between that could hide an error. The reference is usvg's own
/// `Text::bounding_box().width()`, read out of the document konoma emitted, so this is konoma
/// against **resvg's real shaper** and can never decay into konoma against konoma.
#[test]
fn every_new_label_kind_is_drawn_at_the_width_konoma_measured() {
    if !text_metrics::fonts_available() {
        eprintln!("no sans-serif face — skipping (the renderer refuses to draw here too)");
        return;
    }
    // (what kind of label this is, a source that puts `{}` into one).
    let kinds: &[(&str, &str)] = &[
        ("journey people", "journey\n  Task: 3: {}\n"),
        ("journey title", "journey\n  title {}\n  Task: 3: Me\n"),
        ("timeline title", "timeline\n  title {}\n  a : b\n"),
        // An `axisFormat` copies everything that is not a `%` directive through verbatim, so
        // this is how arbitrary text gets into a tick label.
        (
            "gantt tick",
            "gantt\n  dateFormat YYYY-MM-DD\n  axisFormat {}\n  a :2024-01-01, 3d\n",
        ),
        (
            "gantt row name",
            "gantt\n  dateFormat YYYY-MM-DD\n  {} :2024-01-01, 3d\n",
        ),
        (
            "gantt title",
            "gantt\n  title {}\n  dateFormat YYYY-MM-DD\n  a :2024-01-01, 3d\n",
        ),
        (
            "gitgraph branch",
            "gitGraph\n  commit\n  branch {}\n  commit\n",
        ),
        ("gitgraph caption", "gitGraph\n  commit tag: \"{}\"\n"),
    ];

    let mut checked = 0usize;
    for (kind, template) in kinds {
        for (case, text) in LABELS {
            // A gantt row name is `[^:\n]+`, so a colon in it would end the name; a git branch is
            // a `REFERENCE`, which has no spaces. Both are the grammar's rule, not a limitation
            // of the instrument, so those rows skip the texts they cannot carry.
            if kind.starts_with("gitgraph branch") && text.contains(' ') {
                continue;
            }
            if kind.starts_with("architecture edge") && text.contains(']') {
                continue;
            }
            let src = template.replace("{}", text);
            let d = match dispatch(&src) {
                Ok(d) => d,
                Err(e) => panic!("{kind}/{case}: {e}\n{src}"),
            };
            let drawn_text = text_metrics::collapse_spaces(text).into_owned();
            let node = d
                .nodes
                .iter()
                .filter(|n| n.shape == Glyph::ChartLabel)
                .find(|n| n.label.lines.join(" ") == drawn_text)
                .unwrap_or_else(|| {
                    panic!(
                        "{kind}/{case}: no label reading {drawn_text:?}; the labels are {:?}",
                        d.nodes
                            .iter()
                            .filter(|n| n.shape == Glyph::ChartLabel)
                            .map(|n| n.label.lines.join(" "))
                            .collect::<Vec<_>>()
                    )
                });

            let svg = rendered(&src);
            let mut texts = Vec::new();
            measured_texts(tree_of(&svg).root(), &mut texts);
            let drawn = texts
                .iter()
                .find(|(s, _)| *s == drawn_text)
                .map(|(_, w)| *w)
                .unwrap_or_else(|| {
                    panic!(
                        "{kind}/{case}: resvg drew no <text> reading {drawn_text:?} — a dropped \
                         label means the font resolution konoma measured with is not the one \
                         resvg drew with. It drew: {:?}",
                        texts.iter().map(|(s, _)| s).collect::<Vec<_>>()
                    )
                });
            assert!(
                (node.size.w - drawn).abs() <= 1.0,
                "{kind}/{case} {text:?}: konoma sized the box {} wide and resvg drew the text {} \
                 wide — a label's box *is* its ink, so these are one number",
                svg::num(node.size.w),
                svg::num(drawn)
            );
            checked += 1;
        }
    }
    assert!(checked >= 60, "only {checked} label measurements ran");
    eprintln!("{checked} stage-5b label measurements against resvg");
}

/// The same instrument for the labels that live **inside a box** rather than in a bare
/// [`Glyph::ChartLabel`] — a mindmap node, a timeline event, a block, an architecture service.
///
/// The relation is the padded one: the box is the ink plus what `shapes::size` adds, so what is
/// checked is that the ink still *fits*, with the slack it was given. A measurement that drifts
/// makes the words overflow their box, which is what this catches.
#[test]
fn a_boxed_label_still_fits_the_box_konoma_sized_for_it() {
    if !text_metrics::fonts_available() {
        return;
    }
    let kinds: &[(&str, &str)] = &[
        ("mindmap node", "mindmap\n  root\n    {}\n"),
        ("timeline period", "timeline\n  {} : an event\n"),
        ("timeline event", "timeline\n  a period : {}\n"),
        ("block", "block-beta\n  a[\"{}\"]\n"),
        (
            "architecture service",
            "architecture-beta\n  service a(x)[{}]\n",
        ),
        ("kanban card", "kanban\n  Todo\n    c[{}]\n"),
        ("journey task", "journey\n  {}: 3: Me\n"),
    ];
    let mut checked = 0usize;
    for (kind, template) in kinds {
        for (case, text) in LABELS {
            if kind.starts_with("journey task") && text.contains(':') {
                continue;
            }
            if (kind.starts_with("block") || kind.starts_with("kanban")) && text.contains(']') {
                continue;
            }
            let src = template.replace("{}", text);
            let d = match dispatch(&src) {
                Ok(d) => d,
                Err(e) => panic!("{kind}/{case}: {e}\n{src}"),
            };
            let drawn_text = text_metrics::collapse_spaces(text).into_owned();
            let svg = rendered(&src);
            let mut texts = Vec::new();
            measured_texts(tree_of(&svg).root(), &mut texts);
            let drawn = texts
                .iter()
                .find(|(s, _)| *s == drawn_text)
                .map(|(_, w)| *w)
                .unwrap_or_else(|| {
                    panic!(
                        "{kind}/{case}: resvg drew no <text> reading {drawn_text:?}. It drew: {:?}",
                        texts.iter().map(|(s, _)| s).collect::<Vec<_>>()
                    )
                });
            // Whatever holds those words — a node's own label, or a row of its panel.
            let holder = d
                .nodes
                .iter()
                .find(|n| {
                    n.label.lines.join(" ") == drawn_text
                        || n.panel.as_ref().is_some_and(|p| {
                            p.rows
                                .iter()
                                .flat_map(|r| &r.cells)
                                .any(|c| c.label.lines.join(" ") == drawn_text)
                        })
                })
                .unwrap_or_else(|| {
                    panic!("{kind}/{case}: nothing on the page holds {drawn_text:?}")
                });
            assert!(
                holder.size.w + 0.5 >= drawn,
                "{kind}/{case} {text:?}: resvg drew {} of ink in a box konoma made {} wide",
                svg::num(drawn),
                svg::num(holder.size.w)
            );
            checked += 1;
        }
    }
    assert!(checked >= 50, "only {checked} box measurements ran");
}

/// The same instrument for the labels that ride an **edge**.
///
/// An edge label's box is its ink plus [`super::LABEL_PAD_X`] on each side — the patch drawn
/// behind it — so the relation here is exact in the other direction: box minus padding *is* the
/// ink. Stated separately from the two above because it is a different number, and a test that
/// allowed either would catch neither.
#[test]
fn every_new_edge_label_is_drawn_at_the_width_konoma_measured() {
    if !text_metrics::fonts_available() {
        return;
    }
    let kinds: &[(&str, &str)] = &[
        (
            "architecture edge",
            "architecture-beta\n  service a(x)[A]\n  service b(x)[B]\n  a:R -[{}]- L:b\n",
        ),
        ("block link", "block-beta\n  a space b\n  a-- \"{}\" -->b\n"),
        (
            "c4 rel",
            "C4Context\n  System(a, \"A\")\n  System(b, \"B\")\n  Rel(a, b, \"{}\")\n",
        ),
    ];
    let mut checked = 0usize;
    for (kind, template) in kinds {
        for (case, text) in LABELS {
            if text.contains(']') || text.contains('"') {
                continue;
            }
            let src = template.replace("{}", text);
            let d = match dispatch(&src) {
                Ok(d) => d,
                Err(e) => panic!("{kind}/{case}: {e}\n{src}"),
            };
            let drawn_text = text_metrics::collapse_spaces(text).into_owned();
            let label = d
                .edges
                .iter()
                .filter_map(|e| e.label.as_ref())
                .find(|l| l.label.lines.join(" ") == drawn_text)
                .unwrap_or_else(|| {
                    panic!(
                        "{kind}/{case}: no edge label reading {drawn_text:?}; the labels are {:?}",
                        d.edges
                            .iter()
                            .filter_map(|e| e.label.as_ref())
                            .map(|l| l.label.lines.join(" "))
                            .collect::<Vec<_>>()
                    )
                });
            let svg = rendered(&src);
            let mut texts = Vec::new();
            measured_texts(tree_of(&svg).root(), &mut texts);
            let drawn = texts
                .iter()
                .find(|(s, _)| *s == drawn_text)
                .map(|(_, w)| *w)
                .unwrap_or_else(|| {
                    panic!(
                        "{kind}/{case}: resvg drew no <text> reading {drawn_text:?}. It drew: {:?}",
                        texts.iter().map(|(s, _)| s).collect::<Vec<_>>()
                    )
                });
            let slack = label.size.w - drawn;
            assert!(
                (slack - super::LABEL_PAD_X * 2.0).abs() <= 1.0,
                "{kind}/{case} {text:?}: the patch behind the words is {} wider than the ink, and \
                 the padding is {}",
                svg::num(slack),
                svg::num(super::LABEL_PAD_X * 2.0)
            );
            checked += 1;
        }
    }
    assert!(checked >= 18, "only {checked} edge-label measurements ran");
}

// ---------------------------------------------------------------------------------------------
// 2. The shared invariants, per kind
// ---------------------------------------------------------------------------------------------

/// (5) The viewBox holds everything — **all eleven kinds**.
///
/// This is the one that catches the crate's requirement-diagram defect (a box whose top is
/// clipped), so it runs over the whole corpus with no exceptions at all.
#[test]
fn nothing_is_drawn_outside_the_view_box() {
    if !text_metrics::fonts_available() {
        return;
    }
    for (name, src) in CASES {
        check_view_box_contains_everything(name, &laid_out(src));
    }
}

/// (12) A panel and the box drawn round it are the same box — requirement, C4, kanban.
#[test]
fn every_panel_stays_inside_its_own_box() {
    if !text_metrics::fonts_available() {
        return;
    }
    for (name, src) in CASES {
        check_panels_stay_inside_their_box(name, &laid_out(src));
    }
}

/// (1) Two boxes do not overlap — the graph and grid kinds, plus the git graph.
///
/// Not the banded ones, and the reason is in the module docs: a journey's face is *meant* to sit
/// over its task, and a gantt bar shares its row with the row's name.
#[test]
fn boxes_do_not_overlap_where_they_are_not_meant_to() {
    if !text_metrics::fonts_available() {
        return;
    }
    for (name, src) in CASES {
        if !(is_graph_kind(src) || is_grid_kind(src)) {
            continue;
        }
        check_nodes_do_not_overlap(name, &laid_out(src));
    }
}

/// (3) and (4): a label sits on its own line, and a line ends on an outline.
#[test]
fn edge_labels_ride_their_line_and_ends_land_on_an_outline() {
    if !text_metrics::fonts_available() {
        return;
    }
    for (name, src) in CASES {
        if !(is_graph_kind(src) || is_grid_kind(src)) {
            continue;
        }
        let d = laid_out(src);
        check_labels_ride_their_edge(name, &d);
        check_endpoints_land_on_the_outline(name, &d);
    }
}

/// (7) and (8): a nested frame sits inside its parent, and two unrelated frames do not overlap.
#[test]
fn frames_nest_and_do_not_collide() {
    if !text_metrics::fonts_available() {
        return;
    }
    for (name, src) in CASES {
        let d = laid_out(src);
        if d.clusters.is_empty() {
            continue;
        }
        check_nested_clusters_sit_inside_their_parent(name, &d);
        check_unrelated_clusters_do_not_overlap(name, &d);
    }
}

/// The two of the twelve that cannot apply anywhere here, asserted rather than assumed.
#[test]
fn no_stage_5b_kind_grows_a_lifeline_except_the_sequence_dialect() {
    if !text_metrics::fonts_available() {
        return;
    }
    for (name, src) in CASES {
        let d = laid_out(src);
        let zen = crate::preview::mermaid::zenuml::is_zenuml(src);
        assert_eq!(
            !d.lifelines.is_empty(),
            zen,
            "{name}: only a ZenUML diagram has lifelines, because only it is a sequence diagram"
        );
    }
}

// ---------------------------------------------------------------------------------------------
// 3. The invariants that are about these kinds
// ---------------------------------------------------------------------------------------------

/// **A gantt bar's width is its duration, through one scale.**
///
/// The statement that makes the crate's "axis to 2058" impossible, and it is written **scale
/// free**: the whole drawing spans the chart's whole extent, so a bar's share of the drawing has
/// to equal its task's share of the extent. Nothing here recomputes the plot's width, so the test
/// cannot agree with the renderer by copying it.
#[test]
fn a_gantt_bars_width_is_its_own_duration() {
    if !text_metrics::fonts_available() {
        return;
    }
    use crate::preview::mermaid::gantt;
    for (name, src) in cases_of(gantt::is_gantt) {
        let model = gantt::parse(src).expect("parses");
        let d = laid_out(src);
        let (from, to) = model.extent().expect("tasks");
        let span = (to.millis() - from.millis()) as f64;
        assert!(span > 0.0, "{name}: no extent");

        // How wide the whole time axis came out, read off the drawing: the earliest task starts
        // at one end of it and the latest ends at the other, by definition of the extent.
        //
        // A **milestone** contributes its centre, not its box: it is a moment drawn as a diamond
        // straddling its own date, so its outline reaches half a diamond past the instant it
        // marks — which is a fact about the mark and not about the time axis.
        let (mut left, mut right) = (f64::INFINITY, f64::NEG_INFINITY);
        for (i, task) in model.tasks.iter().enumerate() {
            let Some(node) = d.node(&format!("task#{i}")) else {
                continue;
            };
            let (l, r) = if task.tags.milestone {
                (node.center.x, node.center.x)
            } else {
                let (l, _, r, _) = node.bounds();
                (l, r)
            };
            left = left.min(l);
            right = right.max(r);
        }
        let plot = right - left;
        assert!(plot > 0.0, "{name}: the chart has no width");

        for (i, task) in model.tasks.iter().enumerate() {
            let node = d
                .node(&format!("task#{i}"))
                .unwrap_or_else(|| panic!("{name}: task {i} was not drawn"));
            if task.tags.milestone {
                assert_eq!(
                    node.shape,
                    Glyph::Flow(crate::preview::mermaid::flowchart::Shape::Diamond),
                    "{name}: a milestone is a moment, so it is not a bar"
                );
                continue;
            }
            let share = (task.end.millis() - task.start.millis()) as f64 / span;
            let drawn = node.size.w / plot;
            assert!(
                (drawn - share).abs() < 0.01,
                "{name}: task {i} ({:?}) is {:.1}% of the chart's time and {:.1}% of its width",
                task.name,
                share * 100.0,
                drawn * 100.0
            );
            // …and it *starts* where its date does.
            let offset = (task.start.millis() - from.millis()) as f64 / span;
            let drawn_offset = (node.bounds().0 - left) / plot;
            assert!(
                (drawn_offset - offset).abs() < 0.01,
                "{name}: task {i} ({:?}) begins {:.1}% into the chart's time and {:.1}% into its \
                 width",
                task.name,
                offset * 100.0,
                drawn_offset * 100.0
            );
        }
    }
}

/// **A gantt tick is inside the chart's own extent, and the ticks increase.**
///
/// An axis that runs past its data is exactly the crate's defect, so it is stated directly.
#[test]
fn every_gantt_tick_is_a_date_the_chart_actually_covers() {
    if !text_metrics::fonts_available() {
        return;
    }
    use crate::preview::mermaid::gantt;
    for (name, src) in cases_of(gantt::is_gantt) {
        let model = gantt::parse(src).expect("parses");
        let (from, to) = model.extent().expect("tasks");
        let ticks = super::gantt::tick_dates(from, to, &model.tick_interval);
        assert!(!ticks.is_empty(), "{name}: no ticks");
        for t in &ticks {
            assert!(
                *t >= from && *t <= to,
                "{name}: a tick at {} is outside the chart's own {}..{}",
                crate::preview::mermaid::gantt::time::format(*t, "%Y-%m-%d"),
                crate::preview::mermaid::gantt::time::format(from, "%Y-%m-%d"),
                crate::preview::mermaid::gantt::time::format(to, "%Y-%m-%d"),
            );
        }
        for w in ticks.windows(2) {
            assert!(w[1] > w[0], "{name}: the ticks do not increase");
        }
    }
}

/// **A journey's face is the score it was written with.**
#[test]
fn a_journeys_face_carries_its_own_task_score() {
    if !text_metrics::fonts_available() {
        return;
    }
    use crate::preview::mermaid::journey;
    for (name, src) in cases_of(journey::is_journey) {
        let model = journey::parse(src).expect("parses");
        let d = laid_out(src);
        for (i, task) in model.tasks.iter().enumerate() {
            let face = d.node(&format!("task#{i}#face"));
            match task.score {
                Some(score) => {
                    let face = face.unwrap_or_else(|| panic!("{name}: task {i} has no face"));
                    assert_eq!(
                        face.mark,
                        Some(Mark::Face { score }),
                        "{name}: the face on task {i} is not its own score"
                    );
                }
                // A score that could not be read draws no face, because a neutral one is a claim.
                None => assert!(
                    face.is_none(),
                    "{name}: task {i} has no readable score and drew a face anyway"
                ),
            }
        }
    }
}

/// **A git graph's commits run in `seq` order along the time axis, and a commit sits on its own
/// branch's lane.**
#[test]
fn a_git_graphs_commits_run_in_order_along_their_own_lanes() {
    if !text_metrics::fonts_available() {
        return;
    }
    use crate::preview::mermaid::gitgraph::{self, Direction};
    for (name, src) in cases_of(gitgraph::is_git_graph) {
        let model = gitgraph::parse(src).expect("parses");
        let d = laid_out(src);
        let lanes: Vec<&str> = model.lanes().iter().map(|b| b.name.as_str()).collect();

        // Time.
        let along = |p: &Point| match model.direction {
            Direction::LeftToRight => p.x,
            Direction::TopToBottom => p.y,
            Direction::BottomToTop => -p.y,
        };
        let across = |p: &Point| match model.direction {
            Direction::LeftToRight => p.y,
            _ => p.x,
        };
        let mut previous: Option<f64> = None;
        for commit in &model.commits {
            let node = d
                .node(&commit.id)
                .unwrap_or_else(|| panic!("{name}: commit {:?} was not drawn", commit.id));
            let t = along(&node.center);
            if let Some(p) = previous {
                assert!(
                    t > p,
                    "{name}: commit {:?} is drawn at or before the one made before it",
                    commit.id
                );
            }
            previous = Some(t);

            // Lane.
            let want = lanes
                .iter()
                .position(|b| *b == commit.branch)
                .unwrap_or_else(|| panic!("{name}: {:?} is on no lane", commit.branch));
            let others: Vec<(usize, f64)> = lanes
                .iter()
                .enumerate()
                .map(|(i, _)| {
                    // The lane a commit *would* be on, read off another commit of that branch.
                    let sample = model
                        .commits
                        .iter()
                        .find(|c| c.branch == *lanes[i])
                        .and_then(|c| d.node(&c.id))
                        .map(|n| across(&n.center));
                    (i, sample.unwrap_or(f64::INFINITY))
                })
                .collect();
            let nearest = others
                .iter()
                .filter(|(_, v)| v.is_finite())
                .min_by(|a, b| {
                    (a.1 - across(&node.center))
                        .abs()
                        .partial_cmp(&(b.1 - across(&node.center)).abs())
                        .unwrap_or(std::cmp::Ordering::Equal)
                })
                .map(|(i, _)| *i)
                .unwrap_or(want);
            assert_eq!(
                nearest, want,
                "{name}: commit {:?} is on branch {:?} but is drawn on another branch's lane",
                commit.id, commit.branch
            );
        }

        // **A merge's two lines come down two different lanes.** That is the whole of what a
        // merge dot says — this history joined two branches — and it was guarded by the corpus
        // golden alone: a mutation that routed both lines down the child's own lane left every
        // commit on its own lane, every commit in order, and a picture that no longer showed
        // where the merge came from.
        for commit in model.commits.iter().filter(|c| c.parents.len() > 1) {
            let tails: HashSet<String> = commit
                .parents
                .iter()
                .filter_map(|p| model.commit(p))
                .map(|p| p.branch.clone())
                .collect();
            if tails.len() < 2 {
                continue;
            }
            let mut arrived: Vec<f64> = d
                .edges
                .iter()
                .filter(|e| e.to == commit.id)
                .filter_map(|e| e.points.first())
                .map(across)
                .collect();
            arrived.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
            arrived.dedup_by(|a, b| (*a - *b).abs() < 0.5);
            assert!(
                arrived.len() >= 2,
                "{name}: the merge {:?} joins {:?} and its lines all come down one lane",
                commit.id,
                tails
            );
        }
    }
}

/// **An architecture edge leaves and arrives by the sides the source named.**
///
/// The whole reason this language is placed on a grid rather than ranked, so it is the property
/// that has to be stated: a line out of `:R` starts on its box's right edge.
#[test]
fn an_architecture_edge_leaves_by_the_side_it_names() {
    if !text_metrics::fonts_available() {
        return;
    }
    use crate::preview::mermaid::architecture::{self, Side};
    for (name, src) in cases_of(architecture::is_architecture) {
        let model = architecture::parse(src).expect("parses");
        let d = laid_out(src);
        for e in &model.edges {
            let Some(edge) = d.edges.iter().find(|x| x.from == e.from && x.to == e.to) else {
                continue;
            };
            for (id, side, at) in [
                (&e.from, e.from_side, edge.points.first()),
                (&e.to, e.to_side, edge.points.last()),
            ] {
                let Some(node) = d.node(id) else { continue };
                let (l, t, r, b) = node.bounds();
                let p = at.expect("a routed edge has ends");
                let ok = match side {
                    Side::Left => (p.x - l).abs() < 0.5,
                    Side::Right => (p.x - r).abs() < 0.5,
                    Side::Top => (p.y - t).abs() < 0.5,
                    Side::Bottom => (p.y - b).abs() < 0.5,
                };
                assert!(
                    ok,
                    "{name}: the line at {id:?} was written to meet its {side:?} side and does not"
                );
            }
        }
    }
}

/// **A block spans the columns it asked for.**
///
/// `a:2` is twice as wide as `a`, and that is the whole of what `columns` and `:n` mean.
#[test]
fn a_block_that_asked_for_two_columns_is_twice_as_wide() {
    if !text_metrics::fonts_available() {
        return;
    }
    use crate::preview::mermaid::block::{self, Item};
    for (name, src) in cases_of(block::is_block_diagram) {
        let model = block::parse(src).expect("parses");
        let d = laid_out(src);
        // The one-cell blocks are all the same width, and an n-cell one is n of those plus the
        // gaps between them.
        let unit = model
            .items
            .iter()
            .filter(|i| i.width() == 1)
            .filter_map(|i| i.id())
            .filter_map(|id| d.node(id))
            .map(|n| n.size.w)
            .fold(f64::NAN, f64::min);
        if !unit.is_finite() {
            continue;
        }
        for item in &model.items {
            let Item::Node(node) = item else { continue };
            if node.width < 2 {
                continue;
            }
            let Some(placed) = d.node(&node.id) else {
                continue;
            };
            let want = unit * node.width as f64 + super::block::CELL_GAP * (node.width - 1) as f64;
            assert!(
                (placed.size.w - want).abs() < 0.5,
                "{name}: {:?} asked for {} columns, which is {} wide, and was drawn {} wide",
                node.id,
                node.width,
                svg::num(want),
                svg::num(placed.size.w)
            );
        }
    }
}

/// **Every mindmap node is on the page, and a child is drawn beyond its parent.**
#[test]
fn a_mindmap_child_is_drawn_further_out_than_its_parent() {
    if !text_metrics::fonts_available() {
        return;
    }
    use crate::preview::mermaid::mindmap;
    for (name, src) in cases_of(mindmap::is_mindmap) {
        let model = mindmap::parse(src).expect("parses");
        let d = laid_out(src);
        assert_eq!(d.nodes.len(), model.nodes.len(), "{name}: node count");
        for (i, node) in model.nodes.iter().enumerate() {
            let Some(parent) = node.parent else { continue };
            let (child, up) = (
                d.node(&format!("n{i}")).expect("drawn"),
                d.node(&format!("n{parent}")).expect("drawn"),
            );
            assert!(
                child.center.x > up.center.x,
                "{name}: {:?} is a child of {:?} and is not drawn beyond it",
                node.label,
                model.nodes[parent].label
            );
        }
    }
}

/// **A gantt scale maps the chart's own ends onto the plot's own ends.**
///
/// Stated on [`super::gantt::Scale`] directly, with numbers chosen here, because everything else
/// on a gantt chart shares that one scale: shift it and every bar, every tick and every frame
/// shifts with it, so the picture stays self-consistent while being about the wrong dates. This is
/// the definition of a scale, not konoma's opinion of one, and it is where a lost origin shows.
#[test]
fn a_gantt_scale_puts_the_charts_ends_on_the_plots_ends() {
    use crate::preview::mermaid::gantt::time::Instant;
    let from = Instant::from_civil(2014, 1, 1, 0, 0, 0, 0);
    let to = Instant::from_civil(2014, 3, 1, 0, 0, 0, 0);
    let scale = super::gantt::Scale {
        left: 120.0,
        right: 640.0,
        from,
        to,
    };
    assert!(
        (scale.x(from) - 120.0).abs() < 1e-9,
        "the chart's first instant is not at the plot's left edge: {}",
        svg::num(scale.x(from))
    );
    assert!(
        (scale.x(to) - 640.0).abs() < 1e-9,
        "the chart's last instant is not at the plot's right edge: {}",
        svg::num(scale.x(to))
    );
    // …and the half-way date lands half-way along.
    let mid = Instant::from_millis((from.millis() + to.millis()) / 2);
    assert!(
        (scale.x(mid) - 380.0).abs() < 1.0,
        "{}",
        svg::num(scale.x(mid))
    );
    // A zero-width range has nowhere to put anything, and answers with the left edge rather than
    // with a division by zero.
    let flat = super::gantt::Scale {
        left: 10.0,
        right: 20.0,
        from,
        to: from,
    };
    assert_eq!(flat.x(from), 10.0);
}

/// **A gantt chart is as wide as its names plus its plot**, and not wider.
///
/// The other half of the statement above, at the level of the finished drawing: a scale that has
/// lost its origin sends every mark thousands of pixels to the right, the frames follow them, and
/// every relative check still passes — while the diagram rasterises to a smear. A bound on the
/// *absolute* width is the thing that cannot be satisfied by shifting everything equally.
#[test]
fn a_gantt_chart_is_no_wider_than_its_names_and_its_plot() {
    if !text_metrics::fonts_available() {
        return;
    }
    use crate::preview::mermaid::gantt;
    for (name, src) in cases_of(gantt::is_gantt) {
        let model = gantt::parse(src).expect("parses");
        let d = laid_out(src);
        let names = model
            .tasks
            .iter()
            .map(|t| Label::measure(&t.name).width)
            .fold(0.0_f64, f64::max);
        // The plot is at least `PLOT_WIDTH` and grows to fit the tick labels, which are dates.
        let (from, to) = model.extent().expect("tasks");
        let ticks = super::gantt::tick_dates(from, to, &model.tick_interval);
        let widest_tick = ticks
            .iter()
            .map(|t| Label::measure(&gantt::time::format(*t, &model.axis_format)).width)
            .fold(0.0_f64, f64::max);
        let plot = super::gantt::PLOT_WIDTH
            .max((widest_tick + super::chart::TICK_GAP * 2.0) * ticks.len().max(1) as f64);
        // Everything that legitimately reaches past the plot, named: the names column and the
        // gap to the plot, the tick label centred on **each** end of the axis (half of it hangs
        // over), half a milestone's diamond, the section frames' padding on each side, and the
        // two margins `normalise` adds.
        //
        // The left-hand overhang is not decoration. A tick label is centred on the plot's left
        // edge, so half of it reaches back into the names column — and where the row names are
        // short it reaches past them, which pushes the whole drawing right when `normalise`
        // moves the origin. A bound that only allowed for the right-hand half therefore called a
        // correctly drawn chart too wide the first time one came along with narrow names.
        let names_column = names + super::chart::TICK_GAP * 2.0;
        let overhang_left = (widest_tick / 2.0 - names_column).max(0.0);
        let overhang_right = (widest_tick / 2.0).max(super::gantt::MILESTONE / 2.0);
        let bound = overhang_left
            + names_column
            + plot
            + overhang_right
            + super::gantt::SECTION_PAD * 2.0
            + super::MARGIN * 2.0;
        assert!(
            d.width <= bound,
            "{name}: the chart is {} wide, and its names ({}) plus its plot ({}) come to {}",
            svg::num(d.width),
            svg::num(names),
            svg::num(plot),
            svg::num(bound)
        );
    }
}

/// **A gantt bar is on its own row, beside its own name.**
///
/// The chart is scale-free otherwise — every bar and every tick share one scale, so a uniform
/// shift of all of them is invisible. The *row names* do not share it, and this is what ties the
/// two halves of the drawing together: a bar sits on the centre line of the row whose name it is,
/// and to the right of the names.
#[test]
fn a_gantt_bar_shares_a_row_with_its_own_name() {
    if !text_metrics::fonts_available() {
        return;
    }
    use crate::preview::mermaid::gantt;
    for (name, src) in cases_of(gantt::is_gantt) {
        let model = gantt::parse(src).expect("parses");
        let d = laid_out(src);
        let names_right = d
            .nodes
            .iter()
            .filter(|n| n.id.ends_with("#name"))
            .map(|n| n.bounds().2)
            .fold(f64::NEG_INFINITY, f64::max);
        let axis_bottom = d
            .nodes
            .iter()
            .filter(|n| n.id.starts_with("tick#"))
            .map(|n| n.bounds().3)
            .fold(f64::NEG_INFINITY, f64::max);
        for i in 0..model.tasks.len() {
            let (Some(bar), Some(label)) = (
                d.node(&format!("task#{i}")),
                d.node(&format!("task#{i}#name")),
            ) else {
                continue;
            };
            assert!(
                (bar.center.y - label.center.y).abs() < 0.5,
                "{name}: the bar for task {i} is not on the row its name is on"
            );
            assert!(
                bar.bounds().0 >= names_right - 0.5,
                "{name}: the bar for task {i} runs back into the names column"
            );
            // …and below the axis. The bars and the ticks share one scale, so a uniform shift of
            // that scale is invisible between them; the axis is drawn at a *fixed* height, so this
            // is where a scale that lost its origin shows.
            assert!(
                bar.bounds().1 >= axis_bottom - 0.5,
                "{name}: the bar for task {i} is drawn up into the axis"
            );
        }

        // …and inside its own section's frame, which is drawn across the plot at a fixed width.
        // A time scale that has lost its origin sends every bar off the end of its own band, and
        // nothing else in this file would notice: everything else on the time axis moves with it.
        //
        // A band is **a run of consecutive tasks sharing a section**, which is the language's own
        // rule and not the renderer's — a section name written twice with another between the two
        // is two bands. So the ordinal is counted here rather than the name being looked up, and
        // a chart that put the third task in the first `Design` band would be caught instead of
        // being read as correct.
        let ordinals = section_runs(model.tasks.iter().map(|t| t.section.as_str()));
        for (i, task) in model.tasks.iter().enumerate() {
            if task.section.trim().is_empty() {
                continue;
            }
            let (Some(bar), Some(frame)) = (
                d.node(&format!("task#{i}")),
                d.cluster(&format!("section#{}#{}", ordinals[i], task.section)),
            ) else {
                panic!("{name}: task {i} has no bar or its own band was not drawn");
            };
            let (fl, ft, fr, fb) = frame.bounds();
            let (l, t, r, b) = bar.bounds();
            assert!(
                l >= fl - 0.5 && t >= ft - 0.5 && r <= fr + 0.5 && b <= fb + 0.5,
                "{name}: the bar for task {i} is drawn outside its own section {:?}",
                task.section
            );
        }
    }
}

/// **`columns n` wraps.** A grid with more blocks than columns puts the rest on the next row, and
/// the next row is below this one.
#[test]
fn a_block_grid_wraps_at_the_column_count_it_declares() {
    if !text_metrics::fonts_available() {
        return;
    }
    use crate::preview::mermaid::block::{self, AUTO};
    let mut checked = 0usize;
    for (name, src) in cases_of(block::is_block_diagram) {
        let model = block::parse(src).expect("parses");
        if model.columns == AUTO {
            continue;
        }
        let d = laid_out(src);
        let per_row = model.columns.max(1) as usize;
        // Which row each item was declared onto, by the same fill rule the source describes.
        let (mut col, mut row) = (0usize, 0usize);
        let mut rows: Vec<(String, usize)> = Vec::new();
        for item in &model.items {
            let span = item.width().max(1).min(per_row);
            if col + span > per_row && col > 0 {
                col = 0;
                row += 1;
            }
            if let Some(id) = item.id() {
                rows.push((id.to_string(), row));
            }
            col += span;
        }
        // Not every `columns` case has more blocks than columns; the ones that do are what this
        // test is about, and `checked` below makes sure at least one of them does.
        if !rows.iter().any(|(_, r)| *r > 0) {
            continue;
        }
        checked += 1;
        // Every block on a later row is drawn below every block on an earlier one.
        for (a, ra) in &rows {
            for (b, rb) in &rows {
                if ra >= rb {
                    continue;
                }
                let (Some(na), Some(nb)) = (d.node(a), d.node(b)) else {
                    continue;
                };
                assert!(
                    na.bounds().3 <= nb.bounds().1 + 0.5,
                    "{name}: {a:?} is on row {ra} and {b:?} on row {rb}, and {a:?} is not above it"
                );
            }
        }
    }
    assert!(
        checked > 0,
        "no corpus case has more blocks than its `columns`, so nothing was checked"
    );
}

/// **A circle stays a circle.** A cell is stretched to the grid's width; a shape whose
/// proportions carry meaning is not, or `columns 1` would draw an ellipse the width of the page.
#[test]
fn a_block_shape_whose_proportions_mean_something_is_not_stretched() {
    if !text_metrics::fonts_available() {
        return;
    }
    use crate::preview::mermaid::block::{self, Item};
    use crate::preview::mermaid::flowchart::Shape;
    for (name, src) in cases_of(block::is_block_diagram) {
        let model = block::parse(src).expect("parses");
        let d = laid_out(src);
        for item in &model.items {
            let Item::Node(n) = item else { continue };
            let Some(placed) = d.node(&n.id) else {
                continue;
            };
            if matches!(n.shape, Shape::Circle | Shape::DoubleCircle) {
                assert!(
                    (placed.size.w - placed.size.h).abs() < 0.5,
                    "{name}: {:?} is a circle drawn {} by {}",
                    n.id,
                    svg::num(placed.size.w),
                    svg::num(placed.size.h)
                );
            }
        }
    }
}

/// **An architecture edge's ports place the boxes.**
///
/// `db:L -- R:server` says `server` is to the *left* of `db`. That is the whole reason this
/// language is placed on a grid rather than ranked, and it is a statement about the boxes rather
/// than about the line — which is why it is stated separately from
/// [`an_architecture_edge_leaves_by_the_side_it_names`].
#[test]
fn an_architecture_edges_ports_decide_where_the_boxes_go() {
    if !text_metrics::fonts_available() {
        return;
    }
    use crate::preview::mermaid::architecture::{self, Side};
    for (name, src) in cases_of(architecture::is_architecture) {
        let model = architecture::parse(src).expect("parses");
        let d = laid_out(src);
        for e in &model.edges {
            // Only where the two are in the same group: an edge across two groups is drawn and
            // not obeyed, because a group's members are kept together (`place_on_grid`).
            let group_of = |id: &str| {
                model
                    .services
                    .iter()
                    .find(|s| s.id == id)
                    .and_then(|s| s.group.clone())
            };
            if group_of(&e.from) != group_of(&e.to) {
                continue;
            }
            let (Some(from), Some(to)) = (d.node(&e.from), d.node(&e.to)) else {
                continue;
            };
            let ok = match e.from_side {
                Side::Left => to.center.x < from.center.x,
                Side::Right => to.center.x > from.center.x,
                Side::Top => to.center.y < from.center.y,
                Side::Bottom => to.center.y > from.center.y,
            };
            assert!(
                ok,
                "{name}: `{}:{:?}` says {:?} is on that side of {:?}, and it is not",
                e.from, e.from_side, e.to, e.from
            );
        }
    }
}

/// **The same statement, on a source where two ported edges reach one service.**
///
/// `a:R -- L:b`, `b:R --> L:c`, `c:R <-- L:d` and `a:B <--> T:d` are all satisfiable together —
/// `a(0,0) b(1,0) c(2,0) d(3,1)` honours every one of them — and konoma does not find it. The
/// placement is a single walk of the edges (`place_on_grid`): a service is put one cell along
/// from whichever edge reached it **first**, and any later edge that also names it is drawn and
/// not obeyed. Repairing it afterwards does not converge — moving `c` to satisfy `b:R` breaks
/// `c:R`, and moving it back breaks `b:R` again — so the fix is a constraint solve over the
/// grid, not a patch to the walk, and that is a piece of work rather than a correction.
///
/// Left here, ignored, because the alternative is that nothing states it at all: the live test
/// above runs on chains, where each service is reached once, and would go on passing forever
/// while this shape stayed broken.
#[test]
#[ignore = "known limitation, recorded in docs/STATUS.md: the architecture grid is placed by one \
            greedy walk, so a service reached by two ported edges honours the first and \
            contradicts the second; satisfying both needs a constraint solve"]
fn an_architecture_edges_ports_are_honoured_even_when_two_edges_reach_one_service() {
    if !text_metrics::fonts_available() {
        return;
    }
    use crate::preview::mermaid::architecture::{self, Side};
    let name = "architecture-two-edges-reach-one-service";
    let src = "architecture-beta\n  service a(server)[A]\n  service b(server)[B]\n  service \
               c(server)[C]\n  service d(server)[D]\n  a:R -- L:b\n  b:R --> L:c\n  c:R <-- \
               L:d\n  a:B <--> T:d\n";
    let model = architecture::parse(src).expect("parses");
    let d = laid_out(src);
    for e in &model.edges {
        let (Some(from), Some(to)) = (d.node(&e.from), d.node(&e.to)) else {
            continue;
        };
        let ok = match e.from_side {
            Side::Left => to.center.x < from.center.x,
            Side::Right => to.center.x > from.center.x,
            Side::Top => to.center.y < from.center.y,
            Side::Bottom => to.center.y > from.center.y,
        };
        assert!(
            ok,
            "{name}: `{}:{:?}` says {:?} is on that side of {:?}, and it is not",
            e.from, e.from_side, e.to, e.from
        );
    }
}

/// **A service is inside every group it is written in, and inside no other.**
///
/// *Every* group, not one: `group inner in outer` is documented syntax, and a service in `inner`
/// is in `outer` too — a frame drawn inside another frame holds what that one holds. Saying
/// "inside exactly one" would be a statement about the language that is not true, and it would
/// come due the first time a nested group reached the corpus.
#[test]
fn every_architecture_service_is_inside_the_groups_it_is_written_in() {
    if !text_metrics::fonts_available() {
        return;
    }
    use crate::preview::mermaid::architecture;
    for (name, src) in cases_of(architecture::is_architecture) {
        let model = architecture::parse(src).expect("parses");
        let d = laid_out(src);
        // Whether a service written into `group` is, directly or through the groups between,
        // written into `want`. Walked here rather than borrowed from the renderer, which has the
        // same walk and would be checking itself.
        let written_in = |group: Option<&str>, want: &str| {
            let mut at = group;
            for _ in 0..model.groups.len() + 1 {
                let Some(id) = at else { return false };
                if id == want {
                    return true;
                }
                at = model
                    .groups
                    .iter()
                    .find(|g| g.id == id)
                    .and_then(|g| g.parent.as_deref());
            }
            false
        };
        for s in &model.services {
            let Some(node) = d.node(&s.id) else { continue };
            let (l, t, r, b) = node.bounds();
            for group in &model.groups {
                let Some(frame) = d.cluster(&group.id) else {
                    continue;
                };
                let (fl, ft, fr, fb) = frame.bounds();
                let inside = l >= fl - 0.5 && t >= ft - 0.5 && r <= fr + 0.5 && b <= fb + 0.5;
                let mine = written_in(s.group.as_deref(), &group.id);
                assert_eq!(
                    inside,
                    mine,
                    "{name}: {:?} is {}in {:?}",
                    s.id,
                    if inside { "" } else { "not " },
                    group.id
                );
            }
        }
    }
}

/// Whether a block diagram puts a `block:` inside a `block:`.
fn a_composite_holds_a_composite(items: &[crate::preview::mermaid::block::Item]) -> bool {
    use crate::preview::mermaid::block::Item;
    items.iter().any(|item| match item {
        Item::Composite(c) => {
            c.children.iter().any(|k| matches!(k, Item::Composite(_)))
                || a_composite_holds_a_composite(&c.children)
        }
        _ => false,
    })
}

/// Whether **this source** nests one frame inside another, read from its own model.
///
/// Read from the model and not from the drawing, because the drawing is what is on trial: a
/// renderer that lost the nesting would answer "no frames nest here" and skip its own defect.
/// Read from the model and not from a list of case names, because a name list stops covering the
/// corpus the moment somebody adds a case — the rule §6-A item 10 settled.
fn the_source_nests_a_frame(src: &str) -> bool {
    use crate::preview::mermaid as m;
    if m::architecture::is_architecture(src) {
        return m::architecture::parse(src)
            .map(|a| a.groups.iter().any(|g| g.parent.is_some()))
            .unwrap_or(false);
    }
    if m::block::is_block_diagram(src) {
        return m::block::parse(src)
            .map(|b| a_composite_holds_a_composite(&b.items))
            .unwrap_or(false);
    }
    if m::c4::is_c4(src) {
        return m::c4::parse(src)
            .map(|c| c.boundaries.iter().any(|b| b.parent.is_some()))
            .unwrap_or(false);
    }
    false
}

/// **A frame drawn inside another stays inside it — and says whose it is.**
///
/// The second half is not decoration. A [`PlacedCluster`] with no `parent` is not a frame missing
/// an attribute, it is a **switched-off check**: `check_nested_clusters_sit_inside_their_parent`
/// skips a parentless frame, so a test that only called it would have gone green on exactly the
/// defect it was written for, and `check_unrelated_clusters_do_not_overlap` would read a frame
/// and the frame around it as strangers and call their nesting a collision. So the drawing is
/// required to say whose each nested frame is *before* the invariants are called — and which
/// sources are required to say it is decided by [`the_source_nests_a_frame`], from the source.
///
/// [`PlacedCluster`]: super::PlacedCluster
#[test]
fn a_frame_drawn_inside_another_stays_inside_it() {
    if !text_metrics::fonts_available() {
        return;
    }
    let mut checked = 0usize;
    for (name, src) in CASES {
        if !the_source_nests_a_frame(src) {
            continue;
        }
        checked += 1;
        let d = laid_out(src);
        let nested = d.clusters.iter().filter(|c| c.parent.is_some()).count();
        assert!(
            nested > 0,
            "{name}: the source puts one frame inside another and not one drawn frame says which \
             frame it is inside, so the two invariants below would assert nothing about it"
        );
        check_nested_clusters_sit_inside_their_parent(name, &d);
        check_unrelated_clusters_do_not_overlap(name, &d);
        check_view_box_contains_everything(name, &d);

        // …and a child's frame is *strictly* smaller than its parent's, which is what makes the
        // nesting readable rather than two borders drawn on top of each other.
        for c in d.clusters.iter().filter(|c| c.parent.is_some()) {
            let parent = d
                .cluster(c.parent.as_deref().expect("has one"))
                .unwrap_or_else(|| {
                    panic!("{name}: frame {} names a parent that is not drawn", c.id)
                });
            assert!(
                c.size.w < parent.size.w && c.size.h < parent.size.h,
                "{name}: frame {} is {}x{} inside a parent that is {}x{}",
                c.id,
                svg::num(c.size.w),
                svg::num(c.size.h),
                svg::num(parent.size.w),
                svg::num(parent.size.h)
            );
        }
    }
    assert!(
        checked >= 3,
        "only {checked} corpus sources put a frame inside a frame, and all three languages that \
         can (architecture, block, C4) are supposed to be represented"
    );
}

/// **Two things drawn in one diagram do not share an id.**
///
/// `Diagram::node` and `Diagram::cluster` answer with the *first* match, so a duplicate id is not
/// an untidy label — it is a lookup that silently reads the wrong thing, and every test that goes
/// through one of them stops being able to tell the two apart. That is how a gantt section
/// written twice, and a kanban column written twice, made a correctly drawn diagram look wrong
/// while hiding the case where it really would have been.
///
/// Stated over the whole corpus rather than for the kinds it has already bitten, because the ids
/// several of these renderers use come from the author's own text and an author may repeat it.
#[test]
fn no_two_things_in_one_diagram_share_an_id() {
    if !text_metrics::fonts_available() {
        return;
    }
    for (name, src) in CASES {
        let d = laid_out(src);
        for (what, ids) in [
            ("nodes", d.nodes.iter().map(|n| &n.id).collect::<Vec<_>>()),
            (
                "frames",
                d.clusters.iter().map(|c| &c.id).collect::<Vec<_>>(),
            ),
        ] {
            let mut seen: HashSet<&String> = HashSet::new();
            for id in ids {
                assert!(
                    seen.insert(id),
                    "{name}: two {what} are both called {id:?}, so a lookup for it answers with \
                     one of them and never the other"
                );
            }
        }
    }
}

/// **Every kanban card is inside its own column's frame.**
#[test]
fn every_kanban_card_is_inside_its_own_column() {
    if !text_metrics::fonts_available() {
        return;
    }
    use crate::preview::mermaid::kanban;
    for (name, src) in cases_of(kanban::is_kanban) {
        let model = kanban::parse(src).expect("parses");
        let d = laid_out(src);
        for (ci, column) in model.columns.iter().enumerate() {
            let frame = d
                .cluster(&format!("column#{ci}"))
                .unwrap_or_else(|| panic!("{name}: column {:?} has no frame", column.id));
            let (fl, ft, fr, fb) = frame.bounds();
            for (k, card) in column.cards.iter().enumerate() {
                let node = d
                    .node(&format!("card#{ci}#{k}"))
                    .unwrap_or_else(|| panic!("{name}: card {:?} was not drawn", card.id));
                let (l, t, r, b) = node.bounds();
                assert!(
                    l >= fl - 0.5 && t >= ft - 0.5 && r <= fr + 0.5 && b <= fb + 0.5,
                    "{name}: card {:?} is drawn outside {:?}",
                    card.id,
                    column.label
                );
            }
        }
    }
}

/// **A timeline's periods run in source order along its own axis.**
#[test]
fn a_timelines_periods_run_in_source_order() {
    if !text_metrics::fonts_available() {
        return;
    }
    use crate::preview::mermaid::timeline::{self, Direction};
    for (name, src) in cases_of(timeline::is_timeline) {
        let model = timeline::parse(src).expect("parses");
        let d = laid_out(src);
        let along = |p: &Point| match model.direction {
            Direction::LeftToRight => p.x,
            Direction::TopToBottom => p.y,
        };
        let mut previous: Option<f64> = None;
        for i in 0..model.periods.len() {
            let node = d
                .node(&format!("period#{i}"))
                .unwrap_or_else(|| panic!("{name}: period {i} was not drawn"));
            let at = along(&node.center);
            if let Some(p) = previous {
                assert!(
                    at > p,
                    "{name}: period {i} is drawn before the one before it"
                );
            }
            previous = Some(at);
            // …and the **tie** between the period and its events runs down that period's own
            // column. Found by a mutation that moved the line one column over and was caught by
            // nothing at all: the events were still checked to be in their own column, and the
            // line that claims them was checked by nobody — so the drawing said the events below
            // belonged to the period beside them and every assertion passed.
            if !model.periods[i].events.is_empty() {
                let mine = d.edges.iter().any(|e| {
                    e.points.iter().all(|p| match model.direction {
                        Direction::LeftToRight => (p.x - node.center.x).abs() < 0.5,
                        Direction::TopToBottom => (p.y - node.center.y).abs() < 0.5,
                    })
                });
                assert!(
                    mine,
                    "{name}: period {i} has events and no line joining them runs along its own \
                     column"
                );
            }
            // …and every event is on its own period's line.
            for k in 0..model.periods[i].events.len() {
                let event = d
                    .node(&format!("period#{i}#event#{k}"))
                    .unwrap_or_else(|| panic!("{name}: event {i}.{k} was not drawn"));
                let axis = match model.direction {
                    Direction::LeftToRight => (event.center.x - node.center.x).abs(),
                    Direction::TopToBottom => 0.0,
                };
                assert!(
                    axis < 0.5,
                    "{name}: event {i}.{k} is not in its own period's column"
                );
            }
        }
    }
}

/// **Two commits' captions do not overlap.**
///
/// Seen in the gallery, not by a test: a commit whose caption is wider than [`COMMIT_STEP`] is
/// drawn over its neighbour's, and the two sets of words become one unreadable smear. The step
/// between commits is a constant, so the caption is the only thing that can move — and making the
/// step follow the widest caption is a change to where every commit in every git graph is drawn,
/// which is a piece of work with its own golden churn rather than a correction.
///
/// [`COMMIT_STEP`]: super::gitgraph::COMMIT_STEP
#[test]
#[ignore = "known defect, recorded in docs/STATUS.md: a commit caption wider than COMMIT_STEP is \
            drawn over the caption of the commit beside it; the fix is a caption-aware step, \
            which moves every existing git graph"]
fn no_two_git_graph_captions_are_drawn_on_top_of_each_other() {
    if !text_metrics::fonts_available() {
        return;
    }
    use crate::preview::mermaid::gitgraph;
    for (name, src) in cases_of(gitgraph::is_git_graph) {
        let d = laid_out(src);
        let captions: Vec<&super::PlacedNode> = d
            .nodes
            .iter()
            .filter(|n| n.id.ends_with("#caption"))
            .collect();
        for (i, a) in captions.iter().enumerate() {
            for b in &captions[i + 1..] {
                let (al, at, ar, ab) = a.bounds();
                let (bl, bt, br, bb) = b.bounds();
                let overlap = al < br && bl < ar && at < bb && bt < ab;
                assert!(
                    !overlap,
                    "{name}: the captions {:?} and {:?} are drawn on top of one another",
                    a.label.lines.join(" "),
                    b.label.lines.join(" ")
                );
            }
        }
    }
}

/// **A box is drawn as the shape its own source named.**
///
/// Seven of the mutations in this file's campaign were killed by [`kinds_corpus_golden`] and by
/// nothing else, and every one of them was of this shape: a datum drawn with the *other* datum's
/// mark. That is the failure §6-A keeps recording — a behaviour only a golden guards is lost
/// silently the next time the golden is regenerated — so the mapping is written out here, where
/// changing it means changing a test that says what it is for.
///
/// [`kinds_corpus_golden`]: kinds_corpus_golden
#[test]
fn every_box_is_drawn_as_the_shape_its_own_source_named() {
    if !text_metrics::fonts_available() {
        return;
    }
    use crate::preview::mermaid::flowchart::Shape;
    use crate::preview::mermaid::mindmap::NodeShape;
    use crate::preview::mermaid::{c4, gitgraph, mindmap};
    let mut checked = 0usize;
    for (name, src) in CASES {
        if mindmap::is_mindmap(src) {
            let model = mindmap::parse(src).expect("parses");
            let d = laid_out(src);
            for (i, node) in model.nodes.iter().enumerate() {
                let want = match node.shape {
                    NodeShape::NoBorder => Glyph::Underline,
                    NodeShape::RoundedRect => Glyph::Flow(Shape::RoundedRect),
                    NodeShape::Rect => Glyph::Flow(Shape::Rect),
                    NodeShape::Circle => Glyph::Flow(Shape::Circle),
                    NodeShape::Cloud => Glyph::Cloud,
                    NodeShape::Bang => Glyph::Bang,
                    NodeShape::Hexagon => Glyph::Flow(Shape::Hexagon),
                };
                let placed = d.node(&format!("n{i}")).expect("drawn");
                checked += 1;
                assert_eq!(
                    placed.shape, want,
                    "{name}: {:?} was written as a {:?} and is drawn as a {:?}",
                    node.label, node.shape, placed.shape
                );
            }
        }
        if c4::is_c4(src) {
            let model = c4::parse(src).expect("parses");
            let d = laid_out(src);
            for e in &model.elements {
                let want = match e.shape {
                    c4::ShapeKind::Person => Glyph::Actor,
                    c4::ShapeKind::Box => Glyph::Flow(Shape::Rect),
                    c4::ShapeKind::Database => Glyph::Flow(Shape::Cylinder),
                    c4::ShapeKind::Queue => Glyph::Flow(Shape::Stadium),
                };
                let Some(placed) = d.node(&e.alias) else {
                    continue;
                };
                checked += 1;
                assert_eq!(
                    placed.shape, want,
                    "{name}: {:?} was declared a {:?} and is drawn as a {:?}",
                    e.alias, e.shape, placed.shape
                );
            }
        }
        if gitgraph::is_git_graph(src) {
            use crate::preview::mermaid::gitgraph::CommitKind;
            let model = gitgraph::parse(src).expect("parses");
            let d = laid_out(src);
            for commit in &model.commits {
                let want = match commit.custom_type.unwrap_or(commit.kind) {
                    // A highlight has to be told from an ordinary dot at a terminal's scale, and
                    // a square is what does that; a revert is the crossed dot.
                    CommitKind::Reverse => Glyph::Reverted,
                    CommitKind::Highlight => Glyph::Flow(Shape::Rect),
                    CommitKind::Merge => Glyph::Flow(Shape::DoubleCircle),
                    CommitKind::CherryPick | CommitKind::Normal => Glyph::Flow(Shape::Circle),
                };
                let placed = d.node(&commit.id).expect("drawn");
                checked += 1;
                assert_eq!(
                    placed.shape, want,
                    "{name}: commit {:?} is a {:?} and is drawn as a {:?}",
                    commit.id, commit.kind, placed.shape
                );
            }
        }
    }
    assert!(checked > 0, "no box was checked");
}

/// **A gantt bar is painted by its own tags, and the four states take four colours.**
///
/// Which colour goes with which state is konoma's decision (§0-1) and it was recorded *only* in
/// the corpus golden: a mutation that rotated the four survived every named test. The palette is
/// categorical, so the numbers below are the decision, not an accident of it.
#[test]
fn a_gantt_bars_colour_is_its_own_state() {
    if !text_metrics::fonts_available() {
        return;
    }
    use crate::preview::mermaid::gantt;
    let mut seen: HashSet<usize> = HashSet::new();
    for (name, src) in cases_of(gantt::is_gantt) {
        let model = gantt::parse(src).expect("parses");
        let d = laid_out(src);
        for (i, task) in model.tasks.iter().enumerate() {
            let want = match (task.tags.crit, task.tags.done, task.tags.active) {
                (true, _, _) => 3,
                (_, true, _) => 1,
                (_, _, true) => 2,
                _ => 0,
            };
            let Some(bar) = d.node(&format!("task#{i}")) else {
                continue;
            };
            seen.insert(want);
            assert_eq!(
                bar.series,
                Some(want),
                "{name}: task {:?} is {}{}{}and is painted in another state's colour",
                task.name,
                if task.tags.crit { "critical " } else { "" },
                if task.tags.done { "done " } else { "" },
                if task.tags.active { "active " } else { "" },
            );
        }
    }
    assert!(
        seen.len() >= 4,
        "only {} of the four task states are in the corpus, so this states less than it looks",
        seen.len()
    );
}

/// **`contains` is the one relationship drawn solid.**
///
/// It is the only *structural* verb — it says the destination is part of the source — and every
/// other one is drawn dashed, upstream and here. Also golden-only until now.
#[test]
fn only_a_contains_relationship_is_drawn_with_an_unbroken_line() {
    if !text_metrics::fonts_available() {
        return;
    }
    use crate::preview::mermaid::flowchart::Stroke;
    use crate::preview::mermaid::requirement::{self, Relation};
    let mut solid = 0usize;
    let mut dashed = 0usize;
    for (name, src) in cases_of(requirement::is_requirement_diagram) {
        let model = requirement::parse(src).expect("parses");
        let d = laid_out(src);
        if model.links.len() != d.edges.len() {
            continue;
        }
        for (edge, link) in d.edges.iter().zip(model.links.iter()) {
            let want = if link.relation == Relation::Contains {
                Stroke::Normal
            } else {
                Stroke::Dotted
            };
            if want == Stroke::Normal {
                solid += 1;
            } else {
                dashed += 1;
            }
            assert_eq!(
                edge.stroke,
                want,
                "{name}: `{}` is drawn {}",
                link.relation.word(),
                if edge.stroke == Stroke::Normal {
                    "solid"
                } else {
                    "dashed"
                }
            );
        }
    }
    assert!(
        solid > 0 && dashed > 0,
        "the corpus has {solid} `contains` links and {dashed} others, so one side of this is \
         asserting nothing"
    );
}

/// **A panel row holds one drawn line.**
///
/// `svg::emit_line_of_text` writes a panel cell as a *single* `<text>`, and `Panel::cell_bounds`
/// measures it as one line — so a cell whose label carries two is drawn as its lines joined with
/// a space, which is wider than the box the panel sized for it and wider than the rectangle
/// `check_panels_stay_inside_their_box` checks. The words run out of the box and every assertion
/// passes. A kanban card written with a two-line markdown string did exactly that.
///
/// Stated as the contract rather than as the symptom: if a cell never holds two lines, the
/// overflow cannot happen at all.
#[test]
fn no_panel_cell_holds_more_than_one_line() {
    if !text_metrics::fonts_available() {
        return;
    }
    let mut checked = 0usize;
    for (name, src) in CASES {
        let d = laid_out(src);
        for node in &d.nodes {
            let Some(panel) = &node.panel else { continue };
            for row in &panel.rows {
                for cell in &row.cells {
                    checked += 1;
                    assert!(
                        cell.label.lines.len() <= 1,
                        "{name}: a cell of {:?} holds {:?}, and a panel row is drawn as one line",
                        node.id,
                        cell.label.lines
                    );
                }
            }
        }
    }
    assert!(checked > 0, "no panel cell was checked");
}

/// **The arrowhead is at the end the source pointed to.**
///
/// Found by a mutation that moved it to the other end and was caught by *nothing* — the corpus
/// golden included. A tip is drawn as a `<path d="…">`, and `mask_numbers` replaces a `d`
/// wholesale, so which end of a line an arrowhead sits on is invisible to the golden by
/// construction. That is §6-A item 15 again: the mark is there, on the wrong datum.
///
/// The links are paired with the drawn lines **by position**, not by looking one up by its two
/// endpoints: `Rel_D(a, c, …)` and `BiRel(a, c, …)` in one source are two lines between the same
/// pair, and a lookup by endpoints answers with the first of them for both — which is the
/// mis-pairing this whole file is written to avoid.
#[test]
fn an_arrowhead_is_drawn_at_the_end_the_source_points_to() {
    if !text_metrics::fonts_available() {
        return;
    }
    use super::edges::Tip;
    use crate::preview::mermaid::{c4, requirement};
    let mut checked = 0usize;
    for (name, src) in CASES {
        // (what the two ends should carry, how to say which line it was).
        let wanted: Vec<((Tip, Tip), String)> = if requirement::is_requirement_diagram(src) {
            let model = requirement::parse(src).expect("parses");
            model
                .links
                .iter()
                .map(|l| {
                    (
                        (Tip::None, Tip::Arrow),
                        format!("{:?} - {} -> {:?}", l.src, l.relation.word(), l.dst),
                    )
                })
                .collect()
        } else if c4::is_c4(src) {
            let model = c4::parse(src).expect("parses");
            model
                .rels
                .iter()
                .map(|r| {
                    // `BiRel` is the only one that carries two heads, and it carries them
                    // because the source said the two talk both ways.
                    let start = if r.bidirectional {
                        Tip::Arrow
                    } else {
                        Tip::None
                    };
                    ((start, Tip::Arrow), format!("{:?} -> {:?}", r.from, r.to))
                })
                .collect()
        } else {
            continue;
        };
        let d = laid_out(src);
        if wanted.len() != d.edges.len() {
            // A line whose end never became a box is not drawn; nothing here is about that.
            continue;
        }
        for (edge, (tips, what)) in d.edges.iter().zip(wanted.iter()) {
            checked += 1;
            assert_eq!(
                (edge.tip_start, edge.tip_end),
                *tips,
                "{name}: {what} is drawn with its heads at the wrong ends"
            );
        }
    }
    assert!(checked > 0, "no line was checked");
}

/// **`direction` decides which way the ranks run.**
///
/// Also found by a mutation that was caught by nothing: forcing every diagram to `TB` left the
/// masked golden byte-identical, because a rotation moves *every* coordinate and the golden pins
/// none of them. So a documented keyword was being read and thrown away, and the corpus's four
/// `direction` cases were asserting that it parsed and nothing more.
///
/// Stated only over the sources whose own edges form a **DAG**: `lay_out_spec` reverses an edge
/// to break a cycle, so "every line runs forward along the axis" is not true of a diagram that
/// contains one — and that is a fact about the source, worked out here from the source.
#[test]
fn the_direction_a_source_names_is_the_axis_its_lines_run_along() {
    if !text_metrics::fonts_available() {
        return;
    }
    use crate::preview::mermaid::flowchart::Direction;
    use crate::preview::mermaid::{c4, requirement};
    let mut seen: HashSet<Direction> = HashSet::new();
    for (name, src) in CASES {
        let (direction, pairs): (Direction, Vec<(String, String)>) =
            if requirement::is_requirement_diagram(src) {
                let m = requirement::parse(src).expect("parses");
                (
                    m.direction,
                    m.links
                        .iter()
                        .map(|l| (l.src.clone(), l.dst.clone()))
                        .collect(),
                )
            } else if c4::is_c4(src) {
                let m = c4::parse(src).expect("parses");
                (
                    m.direction,
                    m.rels
                        .iter()
                        .map(|r| (r.from.clone(), r.to.clone()))
                        .collect(),
                )
            } else {
                continue;
            };
        if pairs.is_empty() || !is_a_dag(&pairs) {
            continue;
        }
        let d = laid_out(src);
        let mut any = false;
        for (from, to) in &pairs {
            let (Some(a), Some(b)) = (d.node(from), d.node(to)) else {
                continue;
            };
            let ok = match direction {
                Direction::TopToBottom => b.center.y > a.center.y,
                Direction::BottomToTop => b.center.y < a.center.y,
                Direction::LeftToRight => b.center.x > a.center.x,
                Direction::RightToLeft => b.center.x < a.center.x,
            };
            assert!(
                ok,
                "{name}: the source says `direction {}` and {from:?} -> {to:?} does not run that \
                 way",
                direction.as_str()
            );
            any = true;
        }
        if any {
            seen.insert(direction);
        }
    }
    assert!(
        seen.len() >= 3,
        "only {} of the four directions were exercised, so this states less than it looks",
        seen.len()
    );
}

/// Whether a list of (from, to) pairs has no cycle in it.
fn is_a_dag(pairs: &[(String, String)]) -> bool {
    let mut left: Vec<(String, String)> = pairs.to_vec();
    // Kahn's algorithm, written on the pairs themselves: repeatedly drop every edge whose source
    // nothing points at. What cannot be dropped is a cycle.
    for _ in 0..=pairs.len() {
        let before = left.len();
        let targets: HashSet<String> = left.iter().map(|(_, t)| t.clone()).collect();
        left.retain(|(f, _)| targets.contains(f));
        if left.len() == before {
            break;
        }
    }
    left.is_empty()
}

/// **`columns auto` is one row.**
///
/// `-1` means "one row, as wide as it needs to be" and it is also the default, so most block
/// diagrams in the wild are this shape. A mutation that made it one *column* survived everything:
/// reading order — top to bottom, then left to right — is the same for a row and a column, so the
/// text tests could not see it, and the golden masks every coordinate.
#[test]
fn a_block_grid_written_columns_auto_is_one_row() {
    if !text_metrics::fonts_available() {
        return;
    }
    use crate::preview::mermaid::block::{self, Item, AUTO};
    let mut checked = 0usize;
    for (name, src) in cases_of(block::is_block_diagram) {
        let model = block::parse(src).expect("parses");
        if model.columns != AUTO {
            continue;
        }
        let d = laid_out(src);
        let drawn: Vec<&super::PlacedNode> = model
            .items
            .iter()
            .filter_map(|i| match i {
                Item::Node(n) => d.node(&n.id),
                _ => None,
            })
            .collect();
        if drawn.len() < 2 {
            continue;
        }
        checked += 1;
        let first = drawn[0].center.y;
        for n in &drawn {
            assert!(
                (n.center.y - first).abs() < 0.5,
                "{name}: {:?} is not on the same row as the block written first, and `columns \
                 auto` is one row",
                n.id
            );
        }
    }
    assert!(checked > 0, "no `columns auto` case had two blocks in it");
}

/// **A block arrow points the way it was written.**
///
/// `<["left"]>(left)` is an arrow that points left, and that is the whole content of the mark.
/// A mutation that swapped left for right survived: the outline is a `<path d="…">`, which the
/// golden masks, and `new_glyph_emit_golden` pins the outline for a mark *assembled by hand* —
/// so the step from the source's `(left)` to the mark was checked by nothing.
#[test]
fn a_block_arrow_points_the_way_the_source_wrote_it() {
    if !text_metrics::fonts_available() {
        return;
    }
    use crate::preview::mermaid::block::{self, Dir, Item};
    let mut checked = 0usize;
    for (name, src) in cases_of(block::is_block_diagram) {
        let model = block::parse(src).expect("parses");
        let d = laid_out(src);
        for item in &model.items {
            let Item::Node(n) = item else { continue };
            let Some(dirs) = n.arrow.as_ref() else {
                continue;
            };
            let Some(placed) = d.node(&n.id) else {
                continue;
            };
            checked += 1;
            assert_eq!(
                placed.mark,
                Some(Mark::BlockArrow {
                    left: dirs.iter().any(|x| matches!(x, Dir::Left | Dir::X)),
                    right: dirs.iter().any(|x| matches!(x, Dir::Right | Dir::X)),
                    up: dirs.iter().any(|x| matches!(x, Dir::Up | Dir::Y)),
                    down: dirs.iter().any(|x| matches!(x, Dir::Down | Dir::Y)),
                }),
                "{name}: the arrow {:?} was written {dirs:?} and points somewhere else",
                n.id
            );
        }
    }
    assert!(checked > 0, "no block arrow was checked");
}

/// **A composite's frame is drawn round its contents, not beside them.**
///
/// A cell is levelled to the grid's column width, so a frame may be *wider* than the blocks in
/// it — but it is drawn round them, which means the room left over is the same on both sides. A
/// mutation that shifted every cell one column right kept the reading order, kept the
/// containment, and left a blank column inside every nested frame; nothing noticed, because a
/// uniform shift is exactly what a relative check cannot see (§6-A item 12).
#[test]
fn a_composite_frame_is_centred_on_what_is_inside_it() {
    if !text_metrics::fonts_available() {
        return;
    }
    use crate::preview::mermaid::block;
    let mut checked = 0usize;
    for (name, src) in cases_of(block::is_block_diagram) {
        let d = laid_out(src);
        for frame in &d.clusters {
            let (fl, _, fr, _) = frame.bounds();
            let mut left = f64::INFINITY;
            let mut right = f64::NEG_INFINITY;
            for n in &d.nodes {
                let (l, t, r, b) = n.bounds();
                let (_, ft, _, fb) = frame.bounds();
                if l >= fl - 0.5 && r <= fr + 0.5 && t >= ft - 0.5 && b <= fb + 0.5 {
                    left = left.min(l);
                    right = right.max(r);
                }
            }
            if !left.is_finite() {
                continue;
            }
            checked += 1;
            assert!(
                ((left - fl) - (fr - right)).abs() < 1.0,
                "{name}: frame {} leaves {} on its left and {} on its right",
                frame.id,
                svg::num(left - fl),
                svg::num(fr - right)
            );
        }
    }
    assert!(checked > 0, "no block frame was checked");
}

/// **`align row` puts its members on one row, and `align column` in one column.**
///
/// Two documented statements whose whole content is a coordinate. A mutation that swapped the two
/// survived: both keep every box on the grid, both keep every frame nesting, and the golden masks
/// the coordinates that are the difference between them.
#[test]
fn an_architecture_alignment_lines_its_members_up_the_way_it_says() {
    if !text_metrics::fonts_available() {
        return;
    }
    use crate::preview::mermaid::architecture;
    let mut checked = 0usize;
    for (name, src) in cases_of(architecture::is_architecture) {
        let model = architecture::parse(src).expect("parses");
        let d = laid_out(src);
        for a in &model.alignments {
            let placed: Vec<&super::PlacedNode> =
                a.members.iter().filter_map(|m| d.node(m)).collect();
            if placed.len() < 2 {
                continue;
            }
            checked += 1;
            let first = placed[0];
            for n in &placed[1..] {
                if a.row {
                    assert!(
                        (n.center.y - first.center.y).abs() < 0.5,
                        "{name}: `align row` names {:?} and {:?} and they are on two rows",
                        first.id,
                        n.id
                    );
                    assert!(
                        (n.center.x - first.center.x).abs() > 0.5,
                        "{name}: `align row` names {:?} and {:?} and drew them on one another",
                        first.id,
                        n.id
                    );
                } else {
                    assert!(
                        (n.center.x - first.center.x).abs() < 0.5,
                        "{name}: `align column` names {:?} and {:?} and they are in two columns",
                        first.id,
                        n.id
                    );
                    assert!(
                        (n.center.y - first.center.y).abs() > 0.5,
                        "{name}: `align column` names {:?} and {:?} and drew them on one another",
                        first.id,
                        n.id
                    );
                }
            }
        }
    }
    assert!(checked > 0, "no alignment was checked");
}

/// Every kind draws **something**: a diagram that lays out to no marks at all is a blank page.
#[test]
fn every_case_puts_something_on_the_page() {
    if !text_metrics::fonts_available() {
        return;
    }
    for (name, src) in CASES {
        let d = laid_out(src);
        assert!(
            !d.nodes.is_empty() || !d.clusters.is_empty(),
            "{name}: nothing was placed"
        );
        assert!(d.width > 0.0 && d.height > 0.0, "{name}: no extent");
        let svg = rendered(src);
        assert!(svg.contains("<svg"), "{name}: no SVG");
    }
}

// ---------------------------------------------------------------------------------------------
// 3b. Adversarial: a diagram or an error, never a panic and never a hang
// ---------------------------------------------------------------------------------------------

/// Sources chosen to break a parser or a layout rather than to be drawn.
///
/// Nothing here asserts *which* answer comes back — several are legitimately readable — only that
/// one comes back. §1 says the renderer runs on a worker thread inside `catch_unwind`, so a panic
/// would not take the UI down; it would take the diagram down **silently**, which is worse than an
/// error message. Both halves are swept: the parser, and the whole path through to SVG.
#[test]
fn adversarial_sources_produce_a_diagram_or_an_error_and_never_a_panic() {
    let mut sources: Vec<String> = vec![
        // Headers with nothing under them.
        "mindmap".into(),
        "kanban".into(),
        "journey".into(),
        "timeline".into(),
        "gantt".into(),
        "requirementDiagram".into(),
        "gitGraph".into(),
        "C4Context".into(),
        "block-beta".into(),
        "architecture-beta".into(),
        "zenuml".into(),
        // Indentation that is not a tree.
        "mindmap\n\ta\n  b\n\t\tc\n".into(),
        "mindmap\nRoot\nSecond root\n".into(),
        "mindmap\n  a[unclosed\n".into(),
        "mindmap\n  a[\"`never closed\n".into(),
        "kanban\n      deep\n  shallow\n".into(),
        "kanban\n  Todo\n    a@{ not yaml at all }\n".into(),
        // Dates that are not dates, and arithmetic that cannot close.
        "gantt\n  dateFormat YYYY-MM-DD\n  a :2024-02-30, 1d\n".into(),
        "gantt\n  dateFormat YYYY-MM-DD\n  a :after a, 1d\n".into(),
        "gantt\n  dateFormat YYYY-MM-DD\n  a :a, after b, 1d\n  b :b, after a, 1d\n".into(),
        "gantt\n  dateFormat YYYY-MM-DD\n  a :2024-01-01, 99999999d\n".into(),
        "gantt\n  dateFormat X\n  a :99999999999999999999, 1d\n".into(),
        "gantt\n  dateFormat YYYY-MM-DD\n  excludes weekends monday tuesday wednesday thursday \
         friday saturday sunday\n  a :2024-01-01, 3d\n"
            .into(),
        "gantt\n  dateFormat YYYY-MM-DD\n  tickInterval 0day\n  a :2024-01-01, 3d\n".into(),
        "gantt\n  dateFormat YYYY-MM-DD\n  a :milestone, 2024-01-01, 0d\n".into(),
        // Histories that cannot have happened.
        "gitGraph\n  merge main\n".into(),
        "gitGraph\n  commit\n  branch a\n  branch a\n".into(),
        "gitGraph\n  commit\n  cherry-pick id: \"nope\"\n".into(),
        "gitGraph\n  commit id: \"\"\n".into(),
        "gitGraph\n  commit tag: \"unclosed\n".into(),
        // Grids that do not fit.
        "block-beta\n  columns 0\n  a b c\n".into(),
        "block-beta\n  columns 999999\n  a\n".into(),
        "block-beta\n  a:999999\n".into(),
        "block-beta\n  space:999999\n  a\n".into(),
        "block-beta\n  block:a\n    block:a\n      x\n    end\n  end\n".into(),
        "block-beta\n  a[unclosed\n".into(),
        "block-beta\n  a --> \n".into(),
        "block-beta\n  --> b\n".into(),
        // Ports and groups that contradict each other.
        "architecture-beta\n  service a(x)[A]\n  a:R -- L:a\n".into(),
        "architecture-beta\n  group a(x)[A] in b\n  group b(x)[B] in a\n  service s(x)[S] in a\n"
            .into(),
        "architecture-beta\n  service a(x)[A]\n  a:R -- L:nope\n".into(),
        "architecture-beta\n  service a(x)[A]\n  service b(x)[B]\n  align row a a\n".into(),
        // C4 argument lists that do not close.
        "C4Context\n  Person(a, \"unclosed\n".into(),
        "C4Context\n  System(a\n".into(),
        "C4Context\n  Boundary(b, \"B\") {\n".into(),
        "C4Context\n  }\n".into(),
        // Requirement bodies that do not close.
        "requirementDiagram\n  requirement a {\n".into(),
        "requirementDiagram\n  requirement a {\n  id: 1\n  }\n  a - contains -> nope\n".into(),
        // ZenUML statements outside the layer konoma reads.
        "zenuml\n  }\n".into(),
        "zenuml\n  if(a) {\n".into(),
        "zenuml\n  A.b().c()\n".into(),
        "zenuml\n  return\n".into(),
        // Unicode, emoji, control characters, and the double space.
        "mindmap\n  root((🚀))\n    日本語のラベル\n    \u{200b}\n".into(),
        "journey\n  タスク: 3: 私, 猫\n".into(),
        "timeline\n  2002 : ロケット🚀\n".into(),
        "kanban\n  やること\n    タブ\tと改行\n".into(),
        "journey\n  loop  Every minute: 3: Me\n".into(),
        "block-beta\n  a[\"loop  Every minute\"]\n".into(),
        // Keywords used as data.
        "timeline\n  section : an event\n".into(),
        "journey\n  title: 3: Me\n".into(),
        "gantt\n  dateFormat YYYY-MM-DD\n  section :a, 2024-01-01, 1d\n".into(),
        "gitGraph\n  commit id: \"commit\"\n".into(),
    ];
    // Sizes at which an accidental O(n^2) stops returning.
    let deep: String = (0..300)
        .map(|i| format!("{}n{i}\n", " ".repeat(i)))
        .collect();
    sources.push(format!("mindmap\n{deep}"));
    let wide: String = (0..400).map(|i| format!("  n{i}\n")).collect();
    sources.push(format!("kanban\n  Todo\n{wide}"));
    let tasks: String = (0..400)
        .map(|i: usize| format!("  t{i} :t{i}, after t{}, 1d\n", i.saturating_sub(1)))
        .collect();
    sources.push(format!(
        "gantt\n  dateFormat YYYY-MM-DD\n  first :t0, 2024-01-01, 1d\n{tasks}"
    ));
    let commits: String = (0..400).map(|_| "  commit\n".to_string()).collect();
    sources.push(format!("gitGraph\n{commits}"));
    let blocks: String = (0..400).map(|i| format!("  b{i}\n")).collect();
    sources.push(format!("block-beta\n  columns 20\n{blocks}"));
    let services: String = (0..200)
        .map(|i| format!("  service s{i}(x)[S{i}]\n"))
        .collect();
    let links: String = (0..199)
        .map(|i| format!("  s{i}:R --> L:s{}\n", i + 1))
        .collect();
    sources.push(format!("architecture-beta\n{services}{links}"));
    let msgs: String = (0..400).map(|i| format!("  A->B: m{i}\n")).collect();
    sources.push(format!("zenuml\n{msgs}"));
    let nested: String = (0..80)
        .map(|i| format!("{}A.m{i}() {{\n", " ".repeat(i)))
        .collect();
    let closes: String = (0..80)
        .map(|i| format!("{}}}\n", " ".repeat(79 - i)))
        .collect();
    sources.push(format!("zenuml\n{nested}{closes}"));

    for src in &sources {
        // The parser.
        let caught = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
            let _ = dispatch(src);
        }));
        assert!(
            caught.is_ok(),
            "the parser panicked on {:?}",
            &src[..src.len().min(120)]
        );
        // …and the whole path, through the production entry point.
        let caught = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
            let _ = mermaid_to_svg_reason(src, "dark");
        }));
        assert!(
            caught.is_ok(),
            "the renderer panicked on {:?}",
            &src[..src.len().min(120)]
        );
    }
    eprintln!("{} adversarial sources swept", sources.len());
}

// ---------------------------------------------------------------------------------------------
// 4. The golden
// ---------------------------------------------------------------------------------------------

/// The whole corpus, through the production entry point, masked as §6 requires.
#[test]
fn kinds_corpus_golden() {
    if !text_metrics::fonts_available() {
        return;
    }
    let mut out = String::new();
    for (name, src) in CASES {
        out.push_str(&format!("=== {name} ===\n"));
        match mermaid_to_svg_reason(src, "dark") {
            Ok(svg) => out.push_str(&mask_numbers(&svg)),
            Err(e) => out.push_str(&format!("(refused: {e})\n")),
        }
        out.push('\n');
    }
    assert_snapshot("mermaid_kinds", &out);
}

/// The six new glyphs, assembled by hand so **no font is involved and every number is pinned
/// exactly** — the other half of the golden.
///
/// The corpus golden above pins *structure* over real sources with its numbers masked, because a
/// coordinate descends from a label width and a label width descends from the machine's fonts.
/// This one pins the arithmetic: the cloud's four curves, the bang's thirty-two vertices, the
/// underline's rule, the face's mouth at each of the five scores, the block arrow's outline for
/// each combination of heads, and the crossed dot. None of them is measured, so it is identical on
/// every machine — and it is where a mutation to any of those shapes has to fail.
#[test]
fn new_glyph_emit_golden() {
    let d = synthetic_glyph_diagram();
    let mut out = String::new();
    for t in super::theme::ALL {
        out.push_str(&format!("=== {} ===\n", t.name));
        out.push_str(&svg::emit(&d, t));
        out.push('\n');
    }
    assert_snapshot("mermaid_kinds_emit", &out);
}

/// One node of every glyph stage 5b added, at coordinates chosen by hand.
fn synthetic_glyph_diagram() -> Diagram {
    use super::{PlacedNode, Size};
    let label = |text: &str, w: f64| Label {
        lines: text.split('\n').map(str::to_string).collect(),
        width: w,
        height: text.split('\n').count() as f64 * super::labels::line_height(),
    };
    let mut nodes: Vec<PlacedNode> = Vec::new();
    let mut push = |id: &str, shape: Glyph, x: f64, y: f64, size: Size, mark: Option<Mark>| {
        nodes.push(PlacedNode {
            id: id.to_string(),
            shape,
            center: Point::new(x, y),
            size,
            label: label("label", 40.0),
            panel: None,
            series: None,
            mark,
        });
    };
    push(
        "cloud",
        Glyph::Cloud,
        80.0,
        60.0,
        Size::new(120.0, 60.0),
        None,
    );
    push(
        "bang",
        Glyph::Bang,
        240.0,
        60.0,
        Size::new(120.0, 60.0),
        None,
    );
    push(
        "underline",
        Glyph::Underline,
        400.0,
        60.0,
        Size::new(100.0, 24.0),
        None,
    );
    push(
        "reverted",
        Glyph::Reverted,
        540.0,
        60.0,
        Size::new(24.0, 24.0),
        None,
    );
    // Every score, because the mouth **is** the number.
    for (i, score) in [1.0_f64, 2.0, 3.0, 4.0, 5.0].into_iter().enumerate() {
        push(
            &format!("face{i}"),
            Glyph::Face,
            80.0 + i as f64 * 50.0,
            160.0,
            Size::new(24.0, 24.0),
            Some(Mark::Face { score }),
        );
    }
    // Every combination of heads, because each one is a different outline.
    for (i, (left, right, up, down)) in [
        (true, false, false, false),
        (false, true, false, false),
        (false, false, true, false),
        (false, false, false, true),
        (true, true, false, false),
        (false, false, true, true),
        (true, true, true, true),
    ]
    .into_iter()
    .enumerate()
    {
        push(
            &format!("arrow{i}"),
            Glyph::BlockArrow,
            80.0 + i as f64 * 110.0,
            240.0,
            Size::new(100.0, 60.0),
            Some(Mark::BlockArrow {
                left,
                right,
                up,
                down,
            }),
        );
    }
    Diagram {
        width: 900.0,
        height: 320.0,
        nodes,
        ..Diagram::default()
    }
}

/// Every palette draws every case, and the five are all different.
#[test]
fn every_palette_draws_every_case() {
    if !text_metrics::fonts_available() {
        return;
    }
    for (name, src) in CASES {
        let mut seen: HashSet<String> = HashSet::new();
        for theme in ["dark", "light", "classic", "forest", "neutral"] {
            let svg =
                mermaid_to_svg_reason(src, theme).unwrap_or_else(|e| panic!("{name}/{theme}: {e}"));
            assert!(
                !svg.contains("var(--"),
                "{name}/{theme}: a CSS variable, which usvg does not resolve (§1)"
            );
            assert!(
                svg.contains("fill=\"none\"") || !svg.contains("<rect x=\"0\" y=\"0\""),
                "{name}/{theme}: an opaque page background, which §1 forbids"
            );
            seen.insert(svg);
        }
        assert_eq!(
            seen.len(),
            5,
            "{name}: two palettes drew byte-identical output"
        );
    }
}

// ---------------------------------------------------------------------------------------------
// 5. Looking at it
// ---------------------------------------------------------------------------------------------

/// `KONOMA_GALLERY=/some/dir cargo test -- --ignored kinds_tests::gallery`
#[test]
#[ignore = "writes SVG files for a person to look at: KONOMA_GALLERY=<dir> cargo test -- --ignored gallery"]
fn gallery() {
    let dir = std::env::var("KONOMA_GALLERY").expect("set KONOMA_GALLERY to a directory");
    std::fs::create_dir_all(&dir).expect("create the gallery directory");
    let theme = std::env::var("KONOMA_GALLERY_THEME").unwrap_or_else(|_| "dark".to_string());
    for (name, src) in CASES {
        match mermaid_to_svg_reason(src, &theme) {
            Ok(svg) => {
                std::fs::write(format!("{dir}/{name}.svg"), svg).expect("write");
            }
            Err(e) => eprintln!("{name}: {e}"),
        }
    }
    eprintln!("wrote the stage-5b gallery to {dir}");
}

/// A label as it will be drawn, for the messages above.
fn _words(label: &Label) -> String {
    label.lines.join(" ")
}

/// `cargo test --release -- --ignored kinds_tests::render_time` — what a diagram costs.
///
/// §6-A records stage 1's measurement (p50 0.72 ms against the crate's 13.2 ms) and §8 records the
/// crate's p90 at 86.8 ms and its worst case at 662 ms. This is the same measurement over
/// **every** corpus konoma now has — all twenty-three kinds — through the production entry point.
/// Reported rather than asserted: a timing threshold in a test suite is a flake, and the number is
/// for `docs/STATUS.md`.
#[test]
#[ignore = "a measurement, not an assertion: cargo test --release -- --ignored render_time"]
fn render_time() {
    if !text_metrics::fonts_available() {
        return;
    }
    let mut corpus: Vec<(&str, &str)> = Vec::new();
    corpus.extend(super::tests::CORPUS.iter().copied());
    corpus.extend(super::state_tests::CASES.iter().copied());
    corpus.extend(super::class_tests::CASES.iter().copied());
    corpus.extend(super::er_tests::CASES.iter().copied());
    corpus.extend(super::sequence_tests::CASES.iter().copied());
    corpus.extend(crate::preview::mermaid::chart::tests::CASES.iter().copied());
    corpus.extend(CASES.iter().copied());

    // One warm pass so the font database and the syntax caches are not in the numbers.
    for (_, src) in &corpus {
        let _ = mermaid_to_svg_reason(src, "dark");
    }

    let mut times: Vec<(f64, &str)> = Vec::new();
    for (name, src) in &corpus {
        let mut best = f64::INFINITY;
        for _ in 0..5 {
            let at = std::time::Instant::now();
            let _ = mermaid_to_svg_reason(src, "dark");
            best = best.min(at.elapsed().as_secs_f64() * 1000.0);
        }
        times.push((best, name));
    }
    times.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap_or(std::cmp::Ordering::Equal));
    let at = |q: f64| times[((times.len() as f64 - 1.0) * q).round() as usize];
    eprintln!(
        "{} diagrams: p50 {:.2}ms  p90 {:.2}ms  max {:.2}ms ({})",
        times.len(),
        at(0.5).0,
        at(0.9).0,
        times[times.len() - 1].0,
        times[times.len() - 1].1
    );
    for (t, name) in times.iter().rev().take(5) {
        eprintln!("  slowest: {name} {t:.2}ms");
    }
}