konoma 0.28.5

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
//! **The right words in the right places** — the third hole in one family, closed as a family.
//!
//! # What was missing
//!
//! Stage 4's and stage 5a's suites answer two questions about a drawing: *is a mark there*
//! ([`super::decoration_tests`]) and *is it the right size, in the right spot*
//! (the geometric invariants in [`super::tests`] and the per-chart ones in
//! [`super::chart::tests`]). Neither of them ever asks the third question:
//!
//! > does the text drawn at that spot say what **that** datum says?
//!
//! A mutation making every x-axis category label draw the *first* category's text was caught by
//! exactly one test — the masked corpus golden — and by no named test at all. For a chart that is
//! the worst failure available: the picture still reads as data, so nobody looks twice, and the
//! data is wrong.
//!
//! It is the same family as two earlier holes, which is why this module states it once for every
//! kind konoma draws rather than patching the one case:
//!
//! | stage | what survived | what was checked instead |
//! |---|---|---|
//! | 4 | an arrow tip drawn on the wrong end of a flowchart edge | that *an* arrow was emitted |
//! | 5a | a section rule drawn at the wrong y | that the wrapper `<g>` was emitted |
//! | 5b | every category label drawing the first category's words | that a label of the right *width* was drawn |
//!
//! # The shape of every test here
//!
//! Each one is a statement of the form **"the text belonging to datum *k* is drawn at the place
//! belonging to datum *k*"**, and it is stated so that *both halves have to be right*:
//!
//! * the **text** comes from the parsed model, never from another part of the renderer, so
//!   swapping two labels is caught;
//! * the **place** is stated as a relation the geometry has to satisfy — a reading order, a
//!   containment, an angular sector, a nearest-neighbour — never by recomputing the coordinate the
//!   renderer computed, which would be konoma checking konoma.
//!
//! Reading order is the workhorse and it is worth saying why it is strong: sorting the drawn
//! labels by position and comparing the resulting *sequence of words* to the source order fails if
//! any label carries the wrong words, if any two are swapped, if one is dropped, or if the axis
//! is drawn backwards. One assertion, four mutations.
//!
//! # Cases come from the corpus, never from a list of names
//!
//! §6-A item 10: a radar mutation survived twice, the second time because the test that should
//! have caught it listed its cases by name and a new case never joined. Every test here takes its
//! cases from its kind's corpus through its kind's own routing predicate, so a case added
//! tomorrow is checked by all of them without anybody remembering to add it.
//!
//! # This module never parses konoma's SVG
//!
//! The rule from stage 1 (§6): geometry is checked on the [`Diagram`], not on the markup. What a
//! `<text>` element *contains* is `svg::emit` writing out `Label::lines`, and
//! [`super::tests::corpus_golden`] pins that byte for byte; what this module checks is that the
//! right `Label` reached the right place, which is a statement about the diagram.

use std::collections::{HashMap, HashSet};

use super::labels::Label;
use super::shapes::{Glyph, Mark};
use super::{class, er, sequence, state, Diagram, PlacedCluster, PlacedNode, RenderError};
use crate::preview::mermaid::chart;
use crate::preview::mermaid::layout::Point;
use crate::preview::mermaid::text_metrics;

// ---------------------------------------------------------------------------------------------
// Corpora and dispatch
// ---------------------------------------------------------------------------------------------

use super::chart as draw_chart;
use super::class_tests::CASES as CLASS_CASES;
use super::er_tests::CASES as ER_CASES;
use super::sequence_tests::CASES as SEQUENCE_CASES;
use super::state_tests::CASES as STATE_CASES;
use super::tests::CORPUS as FLOW_CASES;
use crate::preview::mermaid::chart::tests::CASES as CHART_CASES;

/// Stage 5b's own corpus, and its dispatcher. The eleven languages this module's second
/// half is about are laid out by [`super::kinds_tests::laid_out`] — the same entry every
/// other stage-5b test goes through, so there is one place that knows which parser claims a
/// source and a case added to the corpus reaches these tests without being mentioned again.
use super::kinds_tests::{cases_of as kind_cases_of, laid_out as kind_diagram};

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

/// Lays a chart source out, whichever of the seven it is.
fn chart_diagram(src: &str) -> Diagram {
    fn go(src: &str) -> Result<Diagram, RenderError> {
        if chart::pie::is_pie(src) {
            return draw_chart::pie::lay_out(&chart::pie::parse(src)?);
        }
        if chart::xychart::is_xychart(src) {
            return draw_chart::xychart::lay_out(&chart::xychart::parse(src)?);
        }
        if chart::quadrant::is_quadrant_chart(src) {
            return draw_chart::quadrant::lay_out(&chart::quadrant::parse(src)?);
        }
        if chart::radar::is_radar(src) {
            return draw_chart::radar::lay_out(&chart::radar::parse(src)?);
        }
        if chart::treemap::is_treemap(src) {
            return draw_chart::treemap::lay_out(&chart::treemap::parse(src)?);
        }
        if chart::packet::is_packet(src) {
            return draw_chart::packet::lay_out(&chart::packet::parse(src)?);
        }
        if chart::sankey::is_sankey(src) {
            return draw_chart::sankey::lay_out(&chart::sankey::parse(src)?);
        }
        panic!("no chart parser claims this source")
    }
    go(src).unwrap_or_else(|e| panic!("corpus source must lay out: {e}\n{src}"))
}

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

/// The words a label will draw, as one string. Exactly what a golden would show.
fn words(label: &Label) -> String {
    label.lines.join(" ")
}

/// The words `text` will be drawn as once measured — the model side of every comparison here.
fn as_drawn(text: &str) -> String {
    words(&Label::measure(text))
}

/// The chart labels of a diagram whose id begins with `prefix`, as `(centre, words)`.
fn labels_with_prefix(d: &Diagram, prefix: &str) -> Vec<(Point, String)> {
    d.nodes
        .iter()
        .filter(|n| n.shape == Glyph::ChartLabel && n.id.starts_with(prefix))
        .map(|n| (n.center.clone(), words(&n.label)))
        .collect()
}

/// The same, read left to right.
fn read_across(d: &Diagram, prefix: &str) -> Vec<String> {
    let mut v = labels_with_prefix(d, prefix);
    v.sort_by(|a, b| {
        a.0.x
            .partial_cmp(&b.0.x)
            .unwrap_or(std::cmp::Ordering::Equal)
    });
    v.into_iter().map(|(_, t)| t).collect()
}

/// The same, read top to bottom.
fn read_down(d: &Diagram, prefix: &str) -> Vec<String> {
    let mut v = labels_with_prefix(d, prefix);
    v.sort_by(|a, b| {
        a.0.y
            .partial_cmp(&b.0.y)
            .unwrap_or(std::cmp::Ordering::Equal)
    });
    v.into_iter().map(|(_, t)| t).collect()
}

/// The legend's rows, read top to bottom. Every chart lays its legend out through
/// `chart::legend_nodes`, so one reader serves all of them.
fn legend_down(d: &Diagram) -> Vec<String> {
    read_down(d, "legend#")
}

/// Every piece of text a panel draws, row by row, each row's cells joined left to right.
fn panel_rows(node: &PlacedNode) -> Vec<String> {
    let Some(p) = &node.panel else {
        return Vec::new();
    };
    p.rows
        .iter()
        .map(|r| {
            let mut cells: Vec<(f64, String)> =
                r.cells.iter().map(|c| (c.x, words(&c.label))).collect();
            cells.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap_or(std::cmp::Ordering::Equal));
            cells
                .into_iter()
                .map(|(_, t)| t)
                .filter(|t| !t.trim().is_empty())
                .collect::<Vec<_>>()
                .join(" ")
        })
        .filter(|t| !t.trim().is_empty())
        .collect()
}

/// Which of `anchors` is nearest to `p`, by centre distance.
///
/// The workhorse for "this text belongs to *that* mark": a label that has drifted onto its
/// neighbour, or been swapped with it, stops being nearest to its own.
fn nearest(p: &Point, anchors: &[(usize, Point)]) -> usize {
    anchors
        .iter()
        .min_by(|a, b| {
            let da = (a.1.x - p.x).hypot(a.1.y - p.y);
            let db = (b.1.x - p.x).hypot(b.1.y - p.y);
            da.partial_cmp(&db).unwrap_or(std::cmp::Ordering::Equal)
        })
        .map(|(i, _)| *i)
        .expect("at least one anchor")
}

/// Whether `inner` sits inside `outer`, allowing half a pixel of rounding.
fn contains(outer: (f64, f64, f64, f64), inner: (f64, f64, f64, f64)) -> bool {
    inner.0 >= outer.0 - 0.5
        && inner.1 >= outer.1 - 0.5
        && inner.2 <= outer.2 + 0.5
        && inner.3 <= outer.3 + 0.5
}

/// `f64` order, with this file's usual answer for a comparison that cannot be made.
fn cmp(a: f64, b: f64) -> std::cmp::Ordering {
    a.partial_cmp(&b).unwrap_or(std::cmp::Ordering::Equal)
}

/// The one box that holds `p`, or `None` when no box does or more than one does.
///
/// The workhorse for the second half of this module. An edge's endpoint has been clipped to the
/// outline it meets, so it lies inside *that* node's box — and the kinds this is used on are the
/// ones whose boxes do not overlap (`kinds_tests`' first invariant), so it lies inside no other.
/// That is what makes "the box at this end of this line" a statement about the drawing rather
/// than about the ids the renderer wrote on the edge, which is the whole difference between a
/// placement test and a spelling one.
fn node_at<'a>(d: &'a Diagram, p: &Point) -> Option<&'a PlacedNode> {
    let mut hit = d.nodes.iter().filter(|n| {
        let (l, t, r, b) = n.bounds();
        p.x >= l - 0.75 && p.x <= r + 0.75 && p.y >= t - 0.75 && p.y <= b + 0.75
    });
    let first = hit.next()?;
    hit.next().is_none().then_some(first)
}

/// Every word a node draws, whether they live in its own label or in its panel's rows.
fn node_text(n: &PlacedNode) -> String {
    match n.panel {
        Some(_) => panel_rows(n).join(" "),
        None => words(&n.label),
    }
}

/// The lines a node draws, top to bottom — a C4 caption is three of them.
fn node_lines(n: &PlacedNode) -> Vec<String> {
    match n.panel {
        Some(_) => panel_rows(n),
        None => n
            .label
            .lines
            .iter()
            .filter(|l| !l.trim().is_empty())
            .cloned()
            .collect(),
    }
}

/// How many frames strictly hold this one — its nesting depth, read off the drawing.
///
/// Needed wherever two frames hold the same boxes: a `Deployment_Node` inside a `Deployment_Node`
/// contains exactly the same elements as its parent, so *what a frame contains* cannot tell the
/// two apart and *how deep it is* has to.
fn frame_depth(d: &Diagram, i: usize) -> usize {
    let me = d.clusters[i].bounds();
    d.clusters
        .iter()
        .enumerate()
        .filter(|(j, o)| *j != i && contains(o.bounds(), me))
        .count()
}

/// One grid's cells, read the way a grid is read: rows top to bottom, cells left to right.
///
/// A row is found by which boxes **overlap in y**, not by a row height, so a row holding a tall
/// block beside a short one is still one row — and a block that has landed on the wrong row stops
/// being read where its source says it is.
fn grid_order<'a>(cells: &[&'a PlacedNode]) -> Vec<&'a PlacedNode> {
    let mut sorted: Vec<&PlacedNode> = cells.to_vec();
    sorted.sort_by(|a, b| cmp(a.bounds().1, b.bounds().1));
    let mut out: Vec<&PlacedNode> = Vec::new();
    let mut row: Vec<&PlacedNode> = Vec::new();
    // The shallowest box in the row so far: a box whose top is below it shares no y with the row.
    let mut floor = f64::INFINITY;
    for n in sorted {
        let (_, t, _, b) = n.bounds();
        if !row.is_empty() && t > floor - 0.5 {
            row.sort_by(|a, b| cmp(a.center.x, b.center.x));
            out.append(&mut row);
            floor = f64::INFINITY;
        }
        floor = floor.min(b);
        row.push(n);
    }
    row.sort_by(|a, b| cmp(a.center.x, b.center.x));
    out.append(&mut row);
    out
}

/// The runs of consecutive items that share a band's name, as `(name, member indices)`.
///
/// A journey's sections, a gantt's and a timeline's are all this: `band` opens a frame when the
/// name changes and closes it when it changes again, so a name written twice with something else
/// between draws two frames — which is what the source says happened. A run with no name draws no
/// frame at all (`sections.retain`), so those are dropped here too.
fn bands(names: &[String]) -> Vec<(String, Vec<usize>)> {
    let mut runs: Vec<(String, Vec<usize>)> = Vec::new();
    for (i, name) in names.iter().enumerate() {
        match runs.last_mut() {
            Some((open, members)) if open == name => members.push(i),
            _ => runs.push((name.clone(), vec![i])),
        }
    }
    runs.retain(|(name, _)| !Label::measure(name).is_blank());
    runs
}

/// States, for one banded diagram, that each frame is titled with its own band's name and holds
/// exactly its own band's marks — with the frames taken **in the order they are drawn along the
/// axis**, which is the order the bands were written in.
fn check_bands(
    name: &str,
    d: &Diagram,
    marks: &[&PlacedNode],
    runs: &[(String, Vec<usize>)],
    along: impl Fn(&Point) -> f64,
) {
    let mut frames: Vec<&PlacedCluster> = d.clusters.iter().collect();
    frames.sort_by(|a, b| cmp(along(&a.center), along(&b.center)));
    assert_eq!(
        frames.len(),
        runs.len(),
        "{name}: {} frames were drawn for {} bands",
        frames.len(),
        runs.len()
    );
    for (frame, (band, members)) in frames.iter().zip(runs.iter()) {
        assert_eq!(
            words(&frame.title),
            as_drawn(band),
            "{name}: a frame is titled with another band's words"
        );
        let inside: Vec<usize> = marks
            .iter()
            .enumerate()
            .filter(|(_, m)| contains(frame.bounds(), m.bounds()))
            .map(|(k, _)| k)
            .collect();
        assert_eq!(
            &inside, members,
            "{name}: the frame reading {band:?} holds the marks at {inside:?}, and that band's \
             own are at {members:?}"
        );
    }
}

// ---------------------------------------------------------------------------------------------
// The seven charts
// ---------------------------------------------------------------------------------------------

/// **pie** — a slice's share is drawn *on that slice*, and the legend names the slices in order.
///
/// The share is checked by the angle it sits at: the vector from the centre of the pie to the
/// percentage's own centre has to fall inside the wedge's own sector. That is the one statement a
/// percentage moved onto the neighbouring slice cannot satisfy, and it needs nothing from the
/// renderer but the geometry it produced.
#[test]
fn a_pies_share_is_drawn_on_its_own_slice_and_its_legend_names_the_slices_in_order() {
    if !text_metrics::fonts_available() {
        return;
    }
    for (name, src) in cases_of(chart::pie::is_pie) {
        let model = chart::pie::parse(src).expect("parses");
        let d = chart_diagram(src);

        // (a) the legend, top to bottom, is the slices in source order.
        let expected: Vec<String> = model
            .slices
            .iter()
            .map(|s| {
                if model.show_data {
                    // The legend adds the raw value; the *name* is what this test is about, so it
                    // is compared as a prefix and the value is left to the golden.
                    as_drawn(&s.label)
                } else {
                    as_drawn(&s.label)
                }
            })
            .collect();
        let drawn = legend_down(&d);
        assert_eq!(
            drawn.len(),
            expected.len(),
            "{name}: the legend has {} rows for {} slices",
            drawn.len(),
            expected.len()
        );
        for (i, (got, want)) in drawn.iter().zip(expected.iter()).enumerate() {
            assert!(
                got.starts_with(want.as_str()),
                "{name}: legend row {i} reads {got:?} where slice {i} is {want:?} — the rows are \
                 {drawn:?}"
            );
        }

        // (b) every percentage that was drawn sits inside the sector of its own slice.
        let mut checked = 0usize;
        for (i, node) in d.nodes.iter().enumerate() {
            let _ = i;
            if node.shape != Glyph::ChartLabel || !node.id.ends_with("#pct") {
                continue;
            }
            let owner = node
                .id
                .trim_start_matches("slice#")
                .trim_end_matches("#pct")
                .to_string();
            let wedge = d
                .nodes
                .iter()
                .find(|n| n.shape == Glyph::Wedge && n.id == format!("slice#{owner}"))
                .unwrap_or_else(|| panic!("{name}: a share labelled for a slice with no wedge"));
            let Some(Mark::Wedge { start, sweep }) = wedge.mark else {
                panic!("{name}: a wedge with no angles");
            };
            // Screen coordinates: y grows downward, and a wedge starts at twelve o'clock.
            let dx = node.center.x - wedge.center.x;
            let dy = node.center.y - wedge.center.y;
            let mut deg = dy.atan2(dx).to_degrees() + 90.0;
            while deg < start {
                deg += 360.0;
            }
            assert!(
                deg <= start + sweep + 1e-6,
                "{name}: the share {:?} is drawn at {deg:.2}°, outside its own slice's \
                 {start:.2}°..{:.2}°",
                words(&node.label),
                start + sweep
            );
            checked += 1;
        }
        assert!(
            checked > 0 || model.slices.len() > 8,
            "{name}: no share label was checked — a case where every share is dropped proves \
             nothing about placement"
        );
    }
}

/// **xychart** — this is the mutation's own test.
///
/// Three statements, each about a different axis of the same failure:
///
/// 1. the category labels, read left to right, spell the categories in source order;
/// 2. the y tick labels, read bottom to top, are increasing numbers — so the value axis is not
///    drawn upside down and no two ticks carry each other's number;
/// 3. every bar is nearer to its own category's label than to any other, which is what ties the
///    *data* to the words under it.
#[test]
fn an_xy_charts_categories_ticks_and_bars_each_carry_their_own_datums_text() {
    if !text_metrics::fonts_available() {
        return;
    }
    for (name, src) in cases_of(chart::xychart::is_xychart) {
        let model = chart::xychart::parse(src).expect("parses");
        let d = chart_diagram(src);

        // The categories, as the model has them: the `x-axis [..]` list, or the keys of the first
        // plot when the source gave none. Derived from the *model*, not from the renderer.
        let categories: Vec<String> = match &model.x {
            chart::xychart::XAxis::Band(v) if !v.is_empty() => {
                v.iter().map(|c| as_drawn(c)).collect()
            }
            _ => model
                .plots
                .first()
                .map(|p| p.data.iter().map(|(k, _)| as_drawn(k)).collect())
                .unwrap_or_default(),
        };

        // (1) left to right, they spell the source.
        let drawn = read_across(&d, "xtick#");
        let expected: Vec<String> = categories
            .iter()
            .filter(|c| !c.trim().is_empty())
            .cloned()
            .collect();
        assert_eq!(
            drawn, expected,
            "{name}: the x axis reads {drawn:?} left to right, but the source's categories are \
             {expected:?} — every category label must draw its own words"
        );

        // (2) bottom to top, the value axis increases.
        let mut ticks: Vec<(Point, String)> = labels_with_prefix(&d, "ytick#");
        ticks.sort_by(|a, b| {
            b.0.y
                .partial_cmp(&a.0.y)
                .unwrap_or(std::cmp::Ordering::Equal)
        });
        let values: Vec<f64> = ticks
            .iter()
            .map(|(_, t)| {
                t.parse::<f64>()
                    .unwrap_or_else(|_| panic!("{name}: a y tick reading {t:?} is not a number"))
            })
            .collect();
        for w in values.windows(2) {
            assert!(
                w[1] > w[0],
                "{name}: the y ticks read {values:?} from the bottom up — a tick carrying its \
                 neighbour's number reads as a different chart"
            );
        }

        // (3) each bar stands over its own category.
        let anchors: Vec<(usize, Point)> = {
            let mut v = labels_with_prefix(&d, "xtick#");
            v.sort_by(|a, b| {
                a.0.x
                    .partial_cmp(&b.0.x)
                    .unwrap_or(std::cmp::Ordering::Equal)
            });
            v.into_iter()
                .enumerate()
                .map(|(i, (p, _))| (i, Point::new(p.x, 0.0)))
                .collect()
        };
        if anchors.is_empty() {
            continue;
        }
        for node in d.nodes.iter().filter(|n| n.shape == Glyph::ChartBar) {
            // The legend's swatches are bars too, and they stand beside the plot rather than in
            // it, so only the ones the plot drew are asked about.
            if node.id.starts_with("legend#") {
                continue;
            }
            let slot: usize = node
                .id
                .rsplit('#')
                .next()
                .and_then(|s| s.parse().ok())
                .unwrap_or_else(|| panic!("{name}: a bar with no slot in its id: {}", node.id));
            let got = nearest(&Point::new(node.center.x, 0.0), &anchors);
            assert_eq!(
                got, slot,
                "{name}: the bar for category {slot} stands nearest the label for category {got}"
            );
        }
    }
}

/// **quadrantChart** — the name of a quadrant is drawn *in* that quadrant, a point's name is
/// nearest its own dot, and the four axis words are on the four right sides.
#[test]
fn a_quadrant_charts_names_are_drawn_in_the_regions_and_beside_the_points_they_name() {
    if !text_metrics::fonts_available() {
        return;
    }
    for (name, src) in cases_of(chart::quadrant::is_quadrant_chart) {
        let model = chart::quadrant::parse(src).expect("parses");
        let d = chart_diagram(src);

        // The plot square, taken from the drawing rather than recomputed: it is the frame node.
        let frame = d
            .nodes
            .iter()
            .find(|n| n.shape == Glyph::PlotFrame)
            .unwrap_or_else(|| panic!("{name}: no quadrant frame"));
        let (fl, ft, fr, fb) = frame.bounds();
        let (mx, my) = ((fl + fr) / 2.0, (ft + fb) / 2.0);

        // (a) quadrant-1 is the top right, and mermaid numbers anticlockwise from there.
        //
        // **By the label's id**, not by finding whichever node happens to draw these words. The
        // id is the corner's own place in reading order — `quadrant#0` is the top left,
        // `quadrant#1` the top right, then the two below (`render::chart::quadrant`) — so it says
        // which corner is being asked about without saying what is written there, and the words
        // can then be asserted as well. The older form could only ask where a string had landed,
        // which is one half of the statement and the wrong half to keep on its own: it says
        // nothing at all about a corner, and two quadrants written with the same words would have
        // made it fail for a reason that is not a defect.
        for (text, id, want_right, want_top) in [
            (&model.quadrant1, "quadrant#1", true, true),
            (&model.quadrant2, "quadrant#0", false, true),
            (&model.quadrant3, "quadrant#2", false, false),
            (&model.quadrant4, "quadrant#3", true, false),
        ] {
            let drawn = as_drawn(text);
            if drawn.trim().is_empty() {
                continue;
            }
            let node = d
                .nodes
                .iter()
                .find(|n| n.shape == Glyph::ChartLabel && n.id == id)
                .unwrap_or_else(|| panic!("{name}: {id} was not drawn at all"));
            assert_eq!(
                words(&node.label),
                drawn,
                "{name}: {id} is where {drawn:?} belongs and it names another quadrant"
            );
            assert_eq!(
                node.center.x > mx,
                want_right,
                "{name}: {drawn:?} is on the wrong side of the vertical divide"
            );
            assert_eq!(
                node.center.y < my,
                want_top,
                "{name}: {drawn:?} is in the wrong half of the square"
            );
        }

        // (b) a point's name is nearer to its own dot than to any other.
        let dots: Vec<(usize, Point)> = d
            .nodes
            .iter()
            .filter(|n| n.shape == Glyph::ChartPoint)
            .map(|n| {
                let i: usize = n.id.trim_start_matches("point#").parse().expect("point id");
                (i, n.center.clone())
            })
            .collect();
        for node in d.nodes.iter() {
            if node.shape != Glyph::ChartLabel || !node.id.ends_with("#label") {
                continue;
            }
            let i: usize = node
                .id
                .trim_start_matches("point#")
                .trim_end_matches("#label")
                .parse()
                .expect("point label id");
            assert_eq!(
                words(&node.label),
                as_drawn(&model.points[i].label),
                "{name}: the label beside point {i} does not read that point's own name"
            );
            assert_eq!(
                nearest(&node.center, &dots),
                i,
                "{name}: the label {:?} is nearer to another point's dot than to its own",
                words(&node.label)
            );
        }

        // (c) the axis words: left before right, bottom below top.
        let at = |id: &str| {
            d.nodes
                .iter()
                .find(|n| n.shape == Glyph::ChartLabel && n.id == id)
                .map(|n| (n.center.clone(), words(&n.label)))
        };
        if let (Some((l, lt)), Some((r, rt))) = (at("xaxis#left"), at("xaxis#right")) {
            assert_eq!(lt, as_drawn(&model.x_left), "{name}: the left x word");
            assert_eq!(rt, as_drawn(&model.x_right), "{name}: the right x word");
            assert!(
                l.x < r.x,
                "{name}: the x axis words are the wrong way round"
            );
        }
        if let (Some((b, bt)), Some((t, tt))) = (at("yaxis#bottom"), at("yaxis#top")) {
            assert_eq!(bt, as_drawn(&model.y_bottom), "{name}: the bottom y word");
            assert_eq!(tt, as_drawn(&model.y_top), "{name}: the top y word");
            assert!(b.y > t.y, "{name}: the y axis words are upside down");
        }
    }
}

/// **radar** — an axis label is drawn beyond *its own* spoke, and the legend names the curves in
/// source order.
///
/// The spoke's direction is not recomputed: it is read off the curve the renderer drew, whose
/// `k`th vertex is by construction on the `k`th axis. So the statement is "the words naming axis
/// `k` are the ones nearest, in angle, to the data plotted on axis `k`" — which a label moved to
/// its neighbour's spoke cannot satisfy.
#[test]
fn a_radar_charts_axis_labels_stand_beyond_their_own_spokes() {
    if !text_metrics::fonts_available() {
        return;
    }
    for (name, src) in cases_of(chart::radar::is_radar) {
        let model = chart::radar::parse(src).expect("parses");
        let d = chart_diagram(src);
        let centre = d
            .nodes
            .iter()
            .find(|n| n.shape == Glyph::Graticule)
            .map(|n| n.center.clone())
            .unwrap_or_else(|| panic!("{name}: no graticule"));

        // The angle of each axis, read off the drawn curve. Any curve will do — they all share
        // the spokes — so the first one with a vertex clear of the centre is used.
        let curve = d
            .edges
            .iter()
            .find(|e| e.series.is_some() && e.points.len() > model.axes.len())
            .unwrap_or_else(|| panic!("{name}: no curve was drawn"));
        let angle = |p: &Point| (p.y - centre.y).atan2(p.x - centre.x);
        let spokes: Vec<(usize, f64)> = curve
            .points
            .iter()
            .take(model.axes.len())
            .enumerate()
            .filter(|(_, p)| (p.x - centre.x).hypot(p.y - centre.y) > 1.0)
            .map(|(k, p)| (k, angle(p)))
            .collect();
        assert!(
            !spokes.is_empty(),
            "{name}: every curve vertex is at the centre, so no spoke direction can be read"
        );

        for (k, axis) in model.axes.iter().enumerate() {
            let Some((_, want)) = spokes.iter().find(|(i, _)| *i == k) else {
                continue;
            };
            let drawn = as_drawn(&axis.label);
            if drawn.trim().is_empty() {
                continue;
            }
            let node = d
                .nodes
                .iter()
                .find(|n| n.shape == Glyph::ChartLabel && n.id == format!("axis#{}", axis.name))
                .unwrap_or_else(|| panic!("{name}: axis {:?} has no label", axis.name));
            assert_eq!(
                words(&node.label),
                drawn,
                "{name}: the label on axis {:?} does not read that axis's own words",
                axis.name
            );
            // The nearest spoke, in angle, has to be its own.
            let got = spokes
                .iter()
                .min_by(|a, b| {
                    let da = angular_gap(angle(&node.center), a.1);
                    let db = angular_gap(angle(&node.center), b.1);
                    da.partial_cmp(&db).unwrap_or(std::cmp::Ordering::Equal)
                })
                .map(|(i, _)| *i)
                .expect("a spoke");
            assert_eq!(
                got,
                k,
                "{name}: the words {drawn:?} name axis {k} but stand beyond spoke {got} \
                 (at {:.1}°, the spoke is at {:.1}°)",
                angle(&node.center).to_degrees(),
                want.to_degrees()
            );
        }

        // The legend, top to bottom, is the curves in source order.
        if model.options.show_legend && !model.curves.is_empty() {
            let expected: Vec<String> = model.curves.iter().map(|c| as_drawn(&c.label)).collect();
            assert_eq!(
                legend_down(&d),
                expected,
                "{name}: the legend does not name the curves in the order they were written"
            );
        }
    }
}

/// How far right of the further participant a self-call's loop is allowed to reach.
///
/// A message from a participant to itself is not a straight line between two columns: it steps
/// out, down and back. The number is generous on purpose — this assertion is about a message drawn
/// between the *wrong* participants, which is a whole column away, not about the loop's width.
const SELF_CALL_REACH: f64 = 120.0;

/// The smaller of the two ways round between two angles, in radians.
fn angular_gap(a: f64, b: f64) -> f64 {
    let mut d = (a - b).abs() % (std::f64::consts::PI * 2.0);
    if d > std::f64::consts::PI {
        d = std::f64::consts::PI * 2.0 - d;
    }
    d
}

/// **treemap** — a tile's words name the datum whose area it is, and a child's tile is inside the
/// tile of the section that owns it.
///
/// # The tile is found by its id, never by the words it draws
///
/// The first version of this test built a map from *drawn text* to tile and looked each datum up
/// by its own name. That is §6-A item 15 exactly, and it was demonstrated rather than argued:
/// making every leaf draw its **next sibling's** name left this test green, because a rotation
/// does not change the multiset of strings — every name still found a tile, and for siblings the
/// tile it found was still inside the same section, so the nesting half passed too. The only test
/// that went red was [`super::chart::tests::a_treemap_tile_carries_its_own_name_and_its_sections_colour`],
/// which anchors on the id.
///
/// So this anchors on the id as well. `tile#k` is the node's own index in a depth-first walk of
/// the source (`render::chart::treemap::place`), which the walk below recomputes from
/// [`super::chart::treemap::subtree_len`] — the same arithmetic the renderer does, but driven off
/// the *model's* shape rather than off anything the renderer drew. What is then stated about that
/// tile is what it says and where it sits, and both halves fail under a rotation.
///
/// Containment is what keeps this a *placement* test rather than a spelling one: a leaf whose
/// tile has moved out of its section fails even when every name is right.
#[test]
fn a_treemaps_tiles_name_their_own_data_and_nest_the_way_the_source_nests() {
    if !text_metrics::fonts_available() {
        return;
    }
    for (name, src) in cases_of(chart::treemap::is_treemap) {
        let model = chart::treemap::parse(src).expect("parses");
        let d = chart_diagram(src);

        /// Walks the model's tree, stating for each node what its own tile says and where it sits.
        ///
        /// `base` is the depth-first index of `items[0]`; a node owns the span of indices its
        /// whole subtree covers, so the next sibling starts `subtree_len` further along.
        fn walk(
            name: &str,
            items: &[chart::treemap::Node],
            base: usize,
            outer: Option<(f64, f64, f64, f64)>,
            d: &Diagram,
            hit: &mut usize,
        ) {
            let mut index = base;
            for item in items {
                let me = index;
                index += draw_chart::treemap::subtree_len(item);
                // A tile with no area is not drawn at all, and neither is anything under it.
                let Some(tile) = d.nodes.iter().find(|n| n.id == format!("tile#{me}")) else {
                    walk(name, &item.children, me + 1, None, d, hit);
                    continue;
                };
                let mine = tile.bounds();
                if let Some(o) = outer {
                    assert!(
                        contains(o, mine),
                        "{name}: tile#{me} is the tile for {:?}, and it is drawn outside the \
                         section that owns it",
                        item.name
                    );
                }
                // A tile too small for its words draws none, which is a deliberate drop
                // (`tile_panel`), so an empty panel is not a failure — the wrong words are.
                let rows = panel_rows(tile);
                if let Some(first) = rows.first() {
                    assert_eq!(
                        *first,
                        as_drawn(&item.name),
                        "{name}: tile#{me} is where {:?} belongs and it reads {first:?}",
                        item.name
                    );
                    *hit += 1;
                }
                // A leaf also carries its own number, and a rotation moves that too.
                if let (Some(v), Some(second)) = (item.value, rows.get(1)) {
                    assert_eq!(
                        *second,
                        as_drawn(&draw_chart::tick_text(v)),
                        "{name}: tile#{me} is where {:?} belongs and the number under its name \
                         reads {second:?}",
                        item.name
                    );
                }
                walk(name, &item.children, me + 1, Some(mine), d, hit);
            }
        }
        let mut hit = 0usize;
        walk(name, &model.roots, 0, None, &d, &mut hit);
        assert!(
            hit > 0,
            "{name}: not one tile carried words, so nothing was read back"
        );
    }
}

/// **packet** — a field's bit range and its name are drawn in *that field's* block, and the blocks
/// run in bit order.
#[test]
fn a_packet_diagrams_fields_are_labelled_with_their_own_bits_and_names_in_bit_order() {
    if !text_metrics::fonts_available() {
        return;
    }
    for (name, src) in cases_of(chart::packet::is_packet) {
        let model = chart::packet::parse(src).expect("parses");
        let d = chart_diagram(src);

        // Reading order: rows top to bottom, fields left to right within a row.
        let mut blocks: Vec<(f64, f64, Vec<String>)> = d
            .nodes
            .iter()
            .filter(|n| n.shape == Glyph::ChartBar && n.id.starts_with("field#"))
            .map(|n| (n.center.y, n.center.x, panel_rows(n)))
            .collect();
        blocks.sort_by(|a, b| {
            a.0.partial_cmp(&b.0)
                .unwrap_or(std::cmp::Ordering::Equal)
                .then(a.1.partial_cmp(&b.1).unwrap_or(std::cmp::Ordering::Equal))
        });

        let fields: Vec<&chart::packet::Field> = model.rows.iter().flatten().collect();
        assert_eq!(
            blocks.len(),
            fields.len(),
            "{name}: {} blocks drawn for {} fields",
            blocks.len(),
            fields.len()
        );
        for (i, (field, (_, _, rows))) in fields.iter().zip(blocks.iter()).enumerate() {
            let range = if field.start == field.end {
                format!("{}", field.start)
            } else {
                format!("{}-{}", field.start, field.end)
            };
            let label = as_drawn(&field.label);
            // Both are dropped rather than overhanging when the block is too narrow, so what is
            // stated is that whatever *is* drawn belongs to this field.
            for row in rows {
                assert!(
                    *row == range || *row == label,
                    "{name}: the block in position {i} — which is bits {range} — is labelled \
                     {row:?}, which belongs to another field"
                );
            }
        }
    }
}

/// **sankey** — a node's name is drawn beside *that node's* bar.
#[test]
fn a_sankey_diagrams_names_stand_beside_their_own_bars() {
    if !text_metrics::fonts_available() {
        return;
    }
    for (name, src) in cases_of(chart::sankey::is_sankey) {
        let model = chart::sankey::parse(src).expect("parses");
        let d = chart_diagram(src);
        let bars: Vec<(usize, Point)> = d
            .nodes
            .iter()
            .filter(|n| n.shape == Glyph::ChartBar && n.id.starts_with("node#"))
            .map(|n| {
                let i: usize = n.id.trim_start_matches("node#").parse().expect("node id");
                (i, n.center.clone())
            })
            .collect();
        assert_eq!(bars.len(), model.nodes.len(), "{name}: one bar per node");

        let mut checked = 0usize;
        for node in d.nodes.iter() {
            if node.shape != Glyph::ChartLabel || !node.id.ends_with("#label") {
                continue;
            }
            let i: usize = node
                .id
                .trim_start_matches("node#")
                .trim_end_matches("#label")
                .parse()
                .expect("node label id");
            assert_eq!(
                words(&node.label),
                as_drawn(&model.nodes[i]),
                "{name}: the name drawn for node {i} is not that node's own"
            );
            let bar = bars
                .iter()
                .find(|(j, _)| *j == i)
                .map(|(_, p)| p.clone())
                .expect("its bar");
            assert!(
                (node.center.y - bar.y).abs() <= 0.5,
                "{name}: {:?} is not on the centre line of its own bar",
                words(&node.label)
            );
            assert_eq!(
                nearest(&node.center, &bars),
                i,
                "{name}: {:?} stands nearer to another node's bar than to its own",
                words(&node.label)
            );
            checked += 1;
        }
        assert_eq!(checked, model.nodes.len(), "{name}: every node is named");
    }
}

// ---------------------------------------------------------------------------------------------
// The four graph languages, and the sequence diagram
// ---------------------------------------------------------------------------------------------

/// **flowchart** — every box holds its own node's words, every line carries its own edge's, and
/// every frame its own block's title.
#[test]
fn a_flowcharts_boxes_lines_and_frames_each_carry_their_own_source_text() {
    if !text_metrics::fonts_available() {
        return;
    }
    for (name, src) in FLOW_CASES {
        let model = crate::preview::mermaid::flowchart::parse(src).expect("parses");
        let d = super::lay_out(&model).expect("lays out");

        for node in &model.nodes {
            let placed = d
                .node(&node.id)
                .unwrap_or_else(|| panic!("{name}: node {:?} was not drawn", node.id));
            assert_eq!(
                words(&placed.label),
                as_drawn(&node.label),
                "{name}: the box for {:?} holds another node's words",
                node.id
            );
        }

        // The drawable edges, in source order — which is the order `lay_out_spec` places them, so
        // the `k`th drawn edge is the `k`th drawable one and its words must be that edge's.
        let drawable: Vec<&crate::preview::mermaid::flowchart::Edge> = model
            .edges
            .iter()
            .filter(|e| {
                let known = |id: &str| {
                    model.node(id).is_some()
                        || model.subgraphs.iter().any(|s| s.id == id)
                        || d.cluster(id).is_some()
                };
                known(&e.from) && known(&e.to)
            })
            .collect();
        assert_eq!(drawable.len(), d.edges.len(), "{name}: edge count");
        for (k, (want, got)) in drawable.iter().zip(d.edges.iter()).enumerate() {
            assert_eq!(
                (got.from.as_str(), got.to.as_str()),
                (want.from.as_str(), want.to.as_str()),
                "{name}: drawn edge {k} joins the wrong pair"
            );
            let expected = want
                .label
                .as_deref()
                .map(as_drawn)
                .filter(|t| !t.trim().is_empty());
            let drawn = got.label.as_ref().map(|l| words(&l.label));
            assert_eq!(
                drawn, expected,
                "{name}: the words on {}->{} are not that link's own",
                want.from, want.to
            );
        }

        for block in &model.subgraphs {
            let Some(frame) = d.cluster(&block.id) else {
                continue;
            };
            assert_eq!(
                words(&frame.title),
                as_drawn(&block.title),
                "{name}: the frame {:?} is titled with another block's words",
                block.id
            );
        }
    }
}

/// **stateDiagram** — a state's words are in its own box, a transition's on its own arrow, and a
/// note's text is in the note that was written beside *that* state, on the side asked for.
#[test]
fn a_state_diagrams_boxes_arrows_and_notes_each_carry_their_own_source_text() {
    if !text_metrics::fonts_available() {
        return;
    }
    for (name, src) in STATE_CASES {
        let model = crate::preview::mermaid::state::parse(src).expect("parses");
        let d = state::lay_out(&model, super::Routing::Splines).expect("lays out");

        for s in &model.states {
            if s.kind.is_block() {
                if let Some(frame) = d.cluster(&s.id) {
                    assert_eq!(
                        words(&frame.title),
                        as_drawn(&s.label),
                        "{name}: the frame {:?} is titled with another state's words",
                        s.id
                    );
                }
                continue;
            }
            let Some(placed) = d.node(&s.id) else {
                continue;
            };
            assert_eq!(
                words(&placed.label),
                as_drawn(&s.label),
                "{name}: the box for {:?} holds another state's words",
                s.id
            );
        }

        // Transitions, in source order, skipping the tie-lines that hold notes on.
        let drawn: Vec<&super::PlacedEdge> = d
            .edges
            .iter()
            .filter(|e| {
                let is_note = |id: &str| d.node(id).is_some_and(|n| n.shape == Glyph::Note);
                !is_note(&e.from) && !is_note(&e.to)
            })
            .collect();
        let wanted: Vec<&crate::preview::mermaid::state::Transition> = model
            .transitions
            .iter()
            .filter(|t| !t.is_note_link)
            .filter(|t| {
                let known = |id: &str| d.node(id).is_some() || d.cluster(id).is_some();
                known(&t.from) && known(&t.to)
            })
            .collect();
        assert_eq!(drawn.len(), wanted.len(), "{name}: transition count");
        for (t, e) in wanted.iter().zip(drawn.iter()) {
            let expected = t
                .label
                .as_deref()
                .map(as_drawn)
                .filter(|s| !s.trim().is_empty());
            assert_eq!(
                e.label.as_ref().map(|l| words(&l.label)),
                expected,
                "{name}: the words on {}->{} are not that transition's own",
                t.from,
                t.to
            );
        }

        // Notes: the right words, beside the right state, on the right side.
        for note in model
            .states
            .iter()
            .filter(|s| s.kind == crate::preview::mermaid::state::Kind::Note)
        {
            let Some(placed) = d.node(&note.id) else {
                continue;
            };
            assert_eq!(
                words(&placed.label),
                as_drawn(&note.label),
                "{name}: the note {:?} holds another note's words",
                note.id
            );
            let Some(link) = model
                .transitions
                .iter()
                .find(|t| t.is_note_link && (t.from == note.id || t.to == note.id))
            else {
                continue;
            };
            let anchor_id = if link.from == note.id {
                &link.to
            } else {
                &link.from
            };
            let anchor = d
                .node(anchor_id)
                .map(|n| n.center.clone())
                .or_else(|| d.cluster(anchor_id).map(|c| c.center.clone()));
            let (Some(anchor), Some(position)) = (anchor, note.note_position) else {
                continue;
            };
            let right = placed.center.x > anchor.x;
            assert_eq!(
                right,
                position == crate::preview::mermaid::state::NotePosition::Right,
                "{name}: the note {:?} was written {position:?} of {anchor_id} and is drawn on \
                 the other side",
                words(&placed.label)
            );
        }
    }
}

/// **classDiagram** — a class box's compartments hold *that class's* name, annotations, attributes
/// and methods, in the order they were written.
///
/// Stated over the whole panel at once rather than row by row: a mutation that copies one class's
/// members into every box, drops the methods, or reverses the compartments all fail the same
/// assertion.
#[test]
fn a_class_boxs_compartments_hold_that_classs_own_members_in_order() {
    if !text_metrics::fonts_available() {
        return;
    }
    for (name, src) in CLASS_CASES {
        let model = crate::preview::mermaid::class::parse(src).expect("parses");
        let d = class::lay_out(&model).expect("lays out");

        for c in &model.classes {
            let placed = d
                .node(&c.id)
                .unwrap_or_else(|| panic!("{name}: class {:?} was not drawn", c.id));
            let mut want: Vec<String> = c
                .annotations
                .iter()
                .map(|a| as_drawn(&format!("«{a}»")))
                .collect();
            want.push(as_drawn(&c.label));
            want.extend(c.attributes.iter().map(|m| as_drawn(&m.display())));
            want.extend(c.methods.iter().map(|m| as_drawn(&m.display())));
            assert_eq!(
                panel_rows(placed),
                want,
                "{name}: the box for {:?} does not hold that class's own rows in order",
                c.id
            );
        }

        for note in &model.notes {
            let Some(placed) = d.node(&note.id) else {
                continue;
            };
            assert_eq!(
                words(&placed.label),
                as_drawn(&note.text),
                "{name}: the note {:?} holds another note's words",
                note.id
            );
        }

        for ns in &model.namespaces {
            let Some(frame) = d.cluster(&ns.id) else {
                continue;
            };
            assert_eq!(
                words(&frame.title),
                as_drawn(&ns.label),
                "{name}: the namespace frame {:?} is titled with other words",
                ns.id
            );
        }
    }
}

/// **erDiagram** — an entity's grid holds *that entity's* name and attribute rows, in order.
#[test]
fn an_entitys_grid_holds_that_entitys_own_attribute_rows_in_order() {
    if !text_metrics::fonts_available() {
        return;
    }
    for (name, src) in ER_CASES {
        let model = crate::preview::mermaid::er::parse(src).expect("parses");
        let d = er::lay_out(&model).expect("lays out");

        for e in &model.entities {
            let placed = d
                .node(&e.id)
                .unwrap_or_else(|| panic!("{name}: entity {:?} was not drawn", e.id));
            let mut want = vec![as_drawn(&e.label)];
            for a in &e.attributes {
                let cells = [
                    as_drawn(&a.kind),
                    as_drawn(&a.name),
                    as_drawn(&a.keys.join(", ")),
                    as_drawn(&a.comment),
                ];
                want.push(
                    cells
                        .iter()
                        .filter(|c| !c.trim().is_empty())
                        .cloned()
                        .collect::<Vec<_>>()
                        .join(" "),
                );
            }
            assert_eq!(
                panel_rows(placed),
                want,
                "{name}: the grid for {:?} does not hold that entity's own rows in order",
                e.id
            );
        }

        for r in &model.relationships {
            let Some(edge) = d
                .edges
                .iter()
                .find(|edge| edge.from == r.from && edge.to == r.to)
            else {
                continue;
            };
            let expected = r
                .label
                .as_deref()
                .map(as_drawn)
                .filter(|s| !s.trim().is_empty());
            assert_eq!(
                edge.label.as_ref().map(|l| words(&l.label)),
                expected,
                "{name}: the verb on {} -- {} is not that relationship's own",
                r.from,
                r.to
            );
        }
    }
}

/// **sequenceDiagram** — a message's words are drawn above *that message's* arrow, a participant's
/// name is in its own box and on its own lifeline, and a note's text is over the participants the
/// source named.
#[test]
fn a_sequence_diagrams_messages_participants_and_notes_each_carry_their_own_text() {
    if !text_metrics::fonts_available() {
        return;
    }
    for (name, src) in SEQUENCE_CASES {
        let model = crate::preview::mermaid::sequence::parse(src).expect("parses");
        let d = sequence::lay_out(&model).expect("lays out");

        check_sequence_text_placement(name, &model, &d);
    }
}

/// The three statements above, as one function, because **two languages reach this geometry**.
///
/// A `zenuml` source is read into the sequence *model* and drawn by the sequence renderer, so
/// these are exactly the right statements to make about one — see
/// [`a_zenuml_diagrams_participants_messages_and_notes_carry_their_own_translated_text`]. Calling
/// this rather than copying it is the rule stages 3, 4 and 5a settled: a copy that stops matching
/// its original is the failure konoma keeps hitting.
fn check_sequence_text_placement(
    name: &str,
    model: &crate::preview::mermaid::sequence::SequenceDiagram,
    d: &Diagram,
) {
    // (a) participants: name in the box, box over the lifeline.
    for p in &model.participants {
        let Some(node) = d.nodes.iter().find(|n| n.id == p.id) else {
            continue;
        };
        let drawn = match &node.panel {
            Some(_) => panel_rows(node).join(" "),
            None => words(&node.label),
        };
        assert_eq!(
            drawn,
            as_drawn(&p.label),
            "{name}: the head box for {:?} holds another participant's name",
            p.id
        );
        let line = d
            .lifelines
            .iter()
            .find(|l| l.id == p.id)
            .unwrap_or_else(|| panic!("{name}: {:?} has no lifeline", p.id));
        assert!(
            (node.center.x - line.x).abs() <= 0.5,
            "{name}: the box naming {:?} does not stand on that participant's own lifeline",
            p.id
        );
    }

    // (b) messages, in the order the events list them.
    let messages: Vec<&crate::preview::mermaid::sequence::Message> = model
        .events
        .iter()
        .filter_map(|e| match e {
            crate::preview::mermaid::sequence::Event::Message(m) => Some(m),
            _ => None,
        })
        .collect();
    assert_eq!(messages.len(), d.edges.len(), "{name}: message count");
    let x_of: HashMap<&str, f64> = d.lifelines.iter().map(|l| (l.id.as_str(), l.x)).collect();
    for (k, (m, e)) in messages.iter().zip(d.edges.iter()).enumerate() {
        let expected = as_drawn(&m.text);
        let drawn = e.label.as_ref().map(|l| words(&l.label));
        if expected.trim().is_empty() {
            assert!(drawn.is_none(), "{name}: message {k} grew words of its own");
            continue;
        }
        assert_eq!(
            drawn.as_deref(),
            Some(expected.as_str()),
            "{name}: the words on message {k} ({} -> {}) are not that message's own",
            m.from,
            m.to
        );
        // …and they are drawn over that arrow: within its own horizontal span (a self-call
        // loops out to the right, so the span is the shaft's, not the two lifelines'), and
        // above its own shaft, which is where mermaid puts a message's text.
        let label = e.label.as_ref().expect("a label");
        let lo = e.points.iter().map(|p| p.x).fold(f64::INFINITY, f64::min);
        let hi = e
            .points
            .iter()
            .map(|p| p.x)
            .fold(f64::NEG_INFINITY, f64::max);
        assert!(
            label.center.x >= lo - label.size.w && label.center.x <= hi + label.size.w,
            "{name}: the words on message {k} are drawn away from the arrow they name"
        );
        let line_y = e.points.first().map(|p| p.y).unwrap_or(0.0);
        if m.from == m.to {
            // A self-call is a loop, so there is nothing above it but the message before; its
            // words go *beside* the loop, inside its own vertical extent.
            let foot = e
                .points
                .iter()
                .map(|p| p.y)
                .fold(f64::NEG_INFINITY, f64::max);
            assert!(
                label.center.y >= line_y - 0.5 && label.center.y <= foot + 0.5,
                "{name}: the words on the self-call {k} are drawn off its own loop"
            );
            assert!(
                label.center.x > hi,
                "{name}: the words on the self-call {k} are drawn over its own loop"
            );
        } else {
            assert!(
                label.center.y < line_y + 0.5,
                "{name}: the words on message {k} are drawn below their own arrow"
            );
        }
        // The two lifelines it joins are still what the message is *about*, so a message
        // whose text drifted onto a different pair of columns is caught too.
        if let (Some(&a), Some(&b)) = (x_of.get(m.from.as_str()), x_of.get(m.to.as_str())) {
            assert!(
                lo >= a.min(b) - 0.5 && hi <= a.max(b) + SELF_CALL_REACH,
                "{name}: message {k} ({} -> {}) is not drawn between its own participants",
                m.from,
                m.to
            );
        }
    }

    // (c) notes: the right text, over the right participants.
    let notes: Vec<&crate::preview::mermaid::sequence::Note> = model
        .events
        .iter()
        .filter_map(|e| match e {
            crate::preview::mermaid::sequence::Event::Note(n) => Some(n),
            _ => None,
        })
        .collect();
    let mut drawn_notes: Vec<&PlacedNode> =
        d.nodes.iter().filter(|n| n.shape == Glyph::Note).collect();
    drawn_notes.sort_by_key(|n| {
        n.id.trim_start_matches("note#")
            .parse::<usize>()
            .unwrap_or(usize::MAX)
    });
    assert_eq!(drawn_notes.len(), notes.len(), "{name}: note count");
    for (k, (want, got)) in notes.iter().zip(drawn_notes.iter()).enumerate() {
        assert_eq!(
            words(&got.label),
            as_drawn(&want.text),
            "{name}: note {k} holds another note's words"
        );
        let named: HashSet<&str> = want.actors.iter().map(|s| s.as_str()).collect();
        let mut mine: Vec<f64> = x_of
            .iter()
            .filter(|(id, _)| named.contains(*id))
            .map(|(_, x)| *x)
            .collect();
        if mine.is_empty() {
            continue;
        }
        mine.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
        let (lo, hi) = (mine[0], mine[mine.len() - 1]);
        let reach = got.size.w.max(hi - lo) + 40.0;
        assert!(
            got.center.x >= lo - reach && got.center.x <= hi + reach,
            "{name}: note {k} is drawn away from the participants it names ({:?})",
            want.actors
        );
    }
}

// ---------------------------------------------------------------------------------------------
// The eleven of stage 5b
// ---------------------------------------------------------------------------------------------
//
// The twelve tests above were written for the kinds that existed when this module was; the eleven
// languages `cba308a` added had none, and the gap was not a guess. Labelling every gantt row with
// the **next** task's name — a rotation, which is what a real off-by-one produces — turned exactly
// one test red, the masked corpus golden, and no named test at all. A snapshot is the thing this
// project has twice lost behaviour to by regenerating it (§6-A), so a chart whose every row names
// the wrong task shipped green.
//
// A rotation is also the right acceptance test for what is written below, and for the same reason
// it defeats the older instruments: it leaves the **multiset of strings unchanged**. The size
// check in `kinds_tests` looks each drawn string up by *whichever* node owns it, so under a
// permutation every string still finds an owner and every box is still wide enough; it is
// structurally blind to mis-pairing. "Is this text somewhere on the page" is worth nothing here.
// Only "is this text at **this datum's** place" is.

/// **gantt** — the name beside a bar is that bar's own task, the ticks read in date order, and a
/// task sits in its own section's frame.
///
/// The place is the **row**, read off the drawing: a gantt's rows are its tasks in source order,
/// so the `k`th mark down the page is the `k`th task and the label sharing its row is that task's
/// name. Nothing here recomputes a y.
#[test]
fn a_gantt_charts_rows_ticks_and_sections_each_carry_their_own_tasks_words() {
    if !text_metrics::fonts_available() {
        return;
    }
    use crate::preview::mermaid::gantt::{self, time};
    for (name, src) in kind_cases_of(gantt::is_gantt) {
        let model = gantt::parse(src).expect("parses");
        let d = kind_diagram(src);

        // The marks — a bar, or the diamond a milestone is drawn as — read down the page.
        let mut marks: Vec<&PlacedNode> = d
            .nodes
            .iter()
            .filter(|n| n.id.starts_with("task#") && !n.id.ends_with("#name"))
            .collect();
        marks.sort_by(|a, b| cmp(a.center.y, b.center.y));
        assert_eq!(
            marks.len(),
            model.tasks.len(),
            "{name}: {} marks were drawn for {} tasks",
            marks.len(),
            model.tasks.len()
        );

        // (1) the row names, top to bottom, spell the source's tasks. A label shares a row with
        // exactly one mark and stands to its left, which is where a gantt puts a row's name.
        let mut rows: Vec<String> = Vec::new();
        for mark in &marks {
            let beside: Vec<&PlacedNode> = d
                .nodes
                .iter()
                .filter(|n| n.shape == Glyph::ChartLabel)
                .filter(|n| {
                    (n.center.y - mark.center.y).abs() < 0.5 && n.center.x < mark.bounds().0
                })
                .collect();
            assert!(
                beside.len() <= 1,
                "{name}: {} labels share one row, so no one of them is that row's name",
                beside.len()
            );
            rows.push(beside.first().map(|n| words(&n.label)).unwrap_or_default());
        }
        let wanted: Vec<String> = model.tasks.iter().map(|t| as_drawn(&t.name)).collect();
        assert_eq!(
            rows, wanted,
            "{name}: the rows read {rows:?} top to bottom, but the source's tasks are {wanted:?} \
             — the name beside a bar has to be that bar's own task"
        );

        // (2) the ticks. Each carries the date its own mark stands on, written the way the source
        // asked for it, and they run left to right in date order.
        let mut ticks: Vec<(f64, time::Instant, String)> = d
            .nodes
            .iter()
            .filter(|n| n.shape == Glyph::ChartLabel && n.id.starts_with("tick#"))
            .map(|n| {
                let iso = n.id.trim_start_matches("tick#");
                let at = time::parse(iso, "YYYY-MM-DDTHH:mm")
                    .unwrap_or_else(|| panic!("{name}: a tick standing on no date: {}", n.id));
                (n.center.x, at, words(&n.label))
            })
            .collect();
        ticks.sort_by(|a, b| cmp(a.0, b.0));
        assert!(!ticks.is_empty(), "{name}: the time axis carries no ticks");
        let drawn: Vec<String> = ticks.iter().map(|(_, _, t)| t.clone()).collect();
        let expected: Vec<String> = ticks
            .iter()
            .map(|(_, at, _)| as_drawn(&time::format(*at, &model.axis_format)))
            .collect();
        assert_eq!(
            drawn, expected,
            "{name}: the axis reads {drawn:?} left to right, and the dates its ticks stand on are \
             {expected:?}"
        );
        for w in ticks.windows(2) {
            assert!(
                w[1].1 > w[0].1,
                "{name}: the axis reads {drawn:?} left to right, which is not date order"
            );
        }

        // (3) a task is in its own section's frame, and the frame carries that section's name.
        let sections: Vec<String> = model.tasks.iter().map(|t| t.section.clone()).collect();
        check_bands(name, &d, &marks, &bands(&sections), |p| p.y);
    }
}

/// **gitGraph** — a caption belongs to its own commit's dot, a tag to the commit under it, and a
/// branch's name to its own lane.
///
/// The place is stated once for each axis of the picture. A caption is drawn half a dot plus a gap
/// **across the lanes** from its own commit, on the far side, so the nearest dot back toward the
/// lane at the same point in time is the one it captions; a tag is drawn across the other way, so
/// the nearest dot forward from it is the commit it marks. Stated in the picture's own two axes
/// rather than in x and y, because across is `+y` in `LR:` and `+x` in `TB:`/`BT:` — a caption
/// hung below its commit in `TB:` would be hung on the lane's own line, which is the direction
/// this asserts it is not in. A lane's position is read off the commits made on it, so nothing
/// here recomputes the lane spacing.
#[test]
fn a_git_graphs_captions_and_lane_names_belong_to_their_own_commits_and_branches() {
    if !text_metrics::fonts_available() {
        return;
    }
    use crate::preview::mermaid::gitgraph::{self, Commit, Direction};
    for (name, src) in kind_cases_of(gitgraph::is_git_graph) {
        let model = gitgraph::parse(src).expect("parses");
        let d = kind_diagram(src);
        let across = |p: &Point| match model.direction {
            Direction::LeftToRight => p.y,
            _ => p.x,
        };
        let along = |p: &Point| match model.direction {
            Direction::LeftToRight => p.x,
            Direction::TopToBottom => p.y,
            Direction::BottomToTop => -p.y,
        };

        // What a commit is captioned with: the id **the author wrote**, and nothing else. A
        // generated id names nothing, so a commit that was given none is captioned with nothing —
        // and a tag is not a caption at all, it is drawn across the lane from the dot in a ticket
        // of its own.
        let says = |c: &Commit| -> String {
            if c.explicit_id {
                as_drawn(&c.id)
            } else {
                String::new()
            }
        };

        // (a) the captions.
        let dots: Vec<&PlacedNode> = model
            .commits
            .iter()
            .map(|c| {
                d.node(&c.id)
                    .unwrap_or_else(|| panic!("{name}: commit {:?} was not drawn", c.id))
            })
            .collect();
        let captions: Vec<&PlacedNode> = d
            .nodes
            .iter()
            .filter(|n| n.shape == Glyph::ChartLabel && n.id.ends_with("#caption"))
            .collect();
        for caption in &captions {
            let owner = dots
                .iter()
                .enumerate()
                .filter(|(_, dot)| {
                    (along(&dot.center) - along(&caption.center)).abs() < 0.5
                        && across(&dot.center) < across(&caption.center)
                })
                .max_by(|a, b| cmp(across(&a.1.center), across(&b.1.center)))
                .map(|(i, _)| i)
                .unwrap_or_else(|| {
                    panic!(
                        "{name}: the caption {:?} stands beside no commit at all",
                        words(&caption.label)
                    )
                });
            assert_eq!(
                words(&caption.label),
                says(&model.commits[owner]),
                "{name}: the caption under commit {:?} says something another commit said",
                model.commits[owner].id
            );
        }
        let carried = model.commits.iter().filter(|c| !says(c).is_empty()).count();
        assert_eq!(
            captions.len(),
            carried,
            "{name}: {} captions were drawn, and {carried} commits carry one",
            captions.len()
        );

        // (a2) the tags, on the other side of the dot. Found the same way and from the opposite
        // direction: the nearest dot **back across the lanes** from a ticket is the commit it
        // marks. Nothing here reads the tag's id, so a tag hung on the wrong commit is caught by
        // where it is drawn rather than by what it was called.
        let tickets: Vec<&PlacedNode> = d.nodes.iter().filter(|n| n.shape == Glyph::Tag).collect();
        for ticket in &tickets {
            let owner = dots
                .iter()
                .enumerate()
                .filter(|(_, dot)| {
                    (along(&dot.center) - along(&ticket.center)).abs() < 0.5
                        && across(&dot.center) > across(&ticket.center)
                })
                .min_by(|a, b| cmp(across(&a.1.center), across(&b.1.center)))
                .map(|(i, _)| i)
                .unwrap_or_else(|| {
                    panic!(
                        "{name}: the tag {:?} hangs beside no commit at all",
                        words(&ticket.label)
                    )
                });
            assert!(
                model.commits[owner]
                    .tags
                    .iter()
                    .any(|t| as_drawn(t) == words(&ticket.label)),
                "{name}: the tag {:?} hangs beside commit {:?}, which is tagged {:?}",
                words(&ticket.label),
                model.commits[owner].id,
                model.commits[owner].tags
            );
        }
        assert_eq!(
            tickets.len(),
            model.commits.iter().map(|c| c.tags.len()).sum::<usize>(),
            "{name}: {} tickets were drawn, and the source writes {} tags",
            tickets.len(),
            model.commits.iter().map(|c| c.tags.len()).sum::<usize>()
        );

        // (b) the lane names, read across the page.
        let lanes: Vec<&str> = model.lanes().iter().map(|b| b.name.as_str()).collect();
        let mut named: Vec<(f64, String)> = d
            .nodes
            .iter()
            .filter(|n| n.shape == Glyph::ChartLabel && n.id.starts_with("branch#"))
            .map(|n| (across(&n.center), words(&n.label)))
            .collect();
        named.sort_by(|a, b| cmp(a.0, b.0));
        let drawn: Vec<String> = named.iter().map(|(_, t)| t.clone()).collect();
        let expected: Vec<String> = lanes.iter().map(|b| as_drawn(b)).collect();
        assert_eq!(
            drawn, expected,
            "{name}: the lanes are named {drawn:?} across the page, and the source's branches are \
             {expected:?}"
        );
        // …and each name stands on the lane its own branch's commits are on.
        for (k, lane) in lanes.iter().enumerate() {
            let Some(sample) = model
                .commits
                .iter()
                .find(|c| c.branch == **lane)
                .and_then(|c| d.node(&c.id))
            else {
                continue;
            };
            assert!(
                (named[k].0 - across(&sample.center)).abs() < 0.5,
                "{name}: {:?} names branch {lane:?} and does not stand on the lane its commits \
                 are drawn on",
                named[k].1
            );
        }
    }
}

/// **mindmap** — a box holds its own node's words, and a line joins a parent's box to its own
/// child's.
///
/// The place is the **line**: both of its ends have been clipped to the outline they meet, so the
/// pair of boxes a line joins is read off the drawing. Shuffle the labels and the branches the
/// picture shows stop being the branches the source wrote, whichever way the tree was laid out.
///
/// # …except when the shuffle is between two leaves of one parent
///
/// That last sentence is not true of every shuffle, and the exception was demonstrated rather
/// than argued: making every **childless** node draw its next childless sibling's words left both
/// halves of this test green. The bag of drawn words is unchanged by any permutation at all, and
/// the branches are unchanged by this one, because a leaf's only appearance in a parent–child
/// pair is as the child — so rotating a parent's leaves rotates the second element of a set of
/// pairs that share a first, and the set comes back the same. **In the whole suite only the
/// corpus golden went red**, which is §6-A item 15 with the golden as the sole guard.
///
/// So the words are anchored on the id as well. A box's id is `n{i}`, its node's own index in the
/// source (`render::mindmap::spec_of` assigns it, because a mindmap's written ids are not unique
/// and cannot identify anything); the label beside it comes from `map.nodes[i].label`, which is a
/// different read of the same node, so a rotation moves one and not the other.
#[test]
fn a_mindmaps_boxes_hold_their_own_words_and_hang_off_their_own_parents() {
    if !text_metrics::fonts_available() {
        return;
    }
    use crate::preview::mermaid::mindmap;
    for (name, src) in kind_cases_of(mindmap::is_mindmap) {
        let model = mindmap::parse(src).expect("parses");
        let d = kind_diagram(src);

        for (i, node) in model.nodes.iter().enumerate() {
            let placed = d
                .node(&format!("n{i}"))
                .unwrap_or_else(|| panic!("{name}: node {i} was not drawn"));
            assert_eq!(
                words(&placed.label),
                as_drawn(&node.label),
                "{name}: n{i} is the box for {:?} and it holds another node's words",
                node.label
            );
        }

        let mut drawn: Vec<String> = d.nodes.iter().map(|n| words(&n.label)).collect();
        let mut wanted: Vec<String> = model.nodes.iter().map(|n| as_drawn(&n.label)).collect();
        drawn.sort();
        wanted.sort();
        assert_eq!(
            drawn, wanted,
            "{name}: the boxes read {drawn:?}, and the source's nodes are {wanted:?}"
        );

        let mut joined: Vec<(String, String)> = Vec::new();
        for e in &d.edges {
            let ends = (e.points.first(), e.points.last());
            let (Some(from), Some(to)) = ends else {
                panic!("{name}: a line with no ends")
            };
            let a =
                node_at(&d, from).unwrap_or_else(|| panic!("{name}: a line starts on no one box"));
            let b = node_at(&d, to).unwrap_or_else(|| panic!("{name}: a line ends on no one box"));
            joined.push((words(&a.label), words(&b.label)));
        }
        let mut branches: Vec<(String, String)> = model
            .nodes
            .iter()
            .filter_map(|n| {
                let parent = n.parent?;
                Some((as_drawn(&model.nodes[parent].label), as_drawn(&n.label)))
            })
            .collect();
        joined.sort();
        branches.sort();
        assert_eq!(
            joined, branches,
            "{name}: the lines join {joined:?}, and the source's branches are {branches:?} — a \
             child has to hang off its own parent's box"
        );
    }
}

/// **kanban** — a card's words are in its own column, in the order the column lists them.
///
/// The place is **containment plus reading order**: the columns run left to right in source
/// order, so the `k`th frame across is the `k`th column, and the cards inside it read down.
#[test]
fn a_kanban_boards_cards_are_in_their_own_columns_in_source_order() {
    if !text_metrics::fonts_available() {
        return;
    }
    use crate::preview::mermaid::kanban;
    for (name, src) in kind_cases_of(kanban::is_kanban) {
        let model = kanban::parse(src).expect("parses");
        let d = kind_diagram(src);

        let mut frames: Vec<&PlacedCluster> = d.clusters.iter().collect();
        frames.sort_by(|a, b| cmp(a.center.x, b.center.x));
        assert_eq!(
            frames.len(),
            model.columns.len(),
            "{name}: {} frames for {} columns",
            frames.len(),
            model.columns.len()
        );

        for (frame, column) in frames.iter().zip(model.columns.iter()) {
            let title = match column.ticket.as_deref().filter(|t| !t.trim().is_empty()) {
                Some(t) => format!("{} ({t})", column.label),
                None => column.label.clone(),
            };
            assert_eq!(
                words(&frame.title),
                as_drawn(&title),
                "{name}: a column's frame is titled with another column's words"
            );

            let mut inside: Vec<&PlacedNode> = d
                .nodes
                .iter()
                .filter(|n| contains(frame.bounds(), n.bounds()))
                .collect();
            inside.sort_by(|a, b| cmp(a.center.y, b.center.y));
            let drawn: Vec<Vec<String>> = inside.iter().map(|n| panel_rows(n)).collect();
            let wanted: Vec<Vec<String>> = column
                .cards
                .iter()
                .map(|c| {
                    // A card says its own words, and then whatever the source pinned to it.
                    // A panel row is **one drawn line**, so a card written with a two-line
                    // label is two rows — the same rule `panel::class_panel` follows, stated
                    // here so the model side and the drawing are counting the same things.
                    let mut rows: Vec<String> = Label::measure(&c.label).lines.clone();
                    for (key, value) in [
                        ("assigned", &c.assigned),
                        ("ticket", &c.ticket),
                        ("priority", &c.priority),
                    ] {
                        if let Some(v) = value.as_deref().filter(|v| !v.trim().is_empty()) {
                            rows.push(as_drawn(&format!("{key}: {v}")));
                        }
                    }
                    // A card written `[]` has no words, and a row with no words draws nothing —
                    // `panel_rows` reads what was drawn, so the model side has to drop the blank
                    // row too or the two would disagree about a card that is simply empty.
                    rows.retain(|r| !r.trim().is_empty());
                    rows
                })
                .collect();
            assert_eq!(
                drawn, wanted,
                "{name}: the column titled {title:?} holds {drawn:?} top to bottom, and its own \
                 cards are {wanted:?}"
            );
        }
    }
}

/// **journey** — a step's name, its score and the people on it all belong to that step, and the
/// step is in its own section's frame.
///
/// The place is the **column**: a journey is a row of steps in source order, and a step's face
/// stands above its own box and the people under it, on that box's own centre line. A score is
/// not text, so it is stated through the mark that carries it — the mouth curve **is** the number
/// (`journey`'s module docs), which is what makes a face on the wrong step a wrong reading rather
/// than a wrong colour.
#[test]
fn a_journeys_steps_carry_their_own_names_scores_and_people_in_their_own_sections() {
    if !text_metrics::fonts_available() {
        return;
    }
    use crate::preview::mermaid::flowchart::Shape;
    use crate::preview::mermaid::journey;
    for (name, src) in kind_cases_of(journey::is_journey) {
        let model = journey::parse(src).expect("parses");
        let d = kind_diagram(src);

        let mut boxes: Vec<&PlacedNode> = d
            .nodes
            .iter()
            .filter(|n| n.shape == Glyph::Flow(Shape::RoundedRect))
            .collect();
        boxes.sort_by(|a, b| cmp(a.center.x, b.center.x));
        assert_eq!(
            boxes.len(),
            model.tasks.len(),
            "{name}: {} boxes were drawn for {} steps",
            boxes.len(),
            model.tasks.len()
        );
        let drawn: Vec<String> = boxes.iter().map(|n| words(&n.label)).collect();
        let wanted: Vec<String> = model.tasks.iter().map(|t| as_drawn(&t.name)).collect();
        assert_eq!(
            drawn, wanted,
            "{name}: the steps read {drawn:?} left to right, and the source's are {wanted:?}"
        );

        for (k, step) in boxes.iter().enumerate() {
            let task = &model.tasks[k];
            let column = |n: &PlacedNode| (n.center.x - step.center.x).abs() < 0.5;

            // The face above this step's own box.
            let face = d
                .nodes
                .iter()
                .find(|n| n.shape == Glyph::Face && column(n) && n.center.y < step.center.y);
            match task.score {
                Some(score) => {
                    let face = face.unwrap_or_else(|| {
                        panic!(
                            "{name}: the step {:?} has no face over it",
                            words(&step.label)
                        )
                    });
                    assert_eq!(
                        face.mark,
                        Some(Mark::Face { score }),
                        "{name}: the face over {:?} is drawn at another step's score",
                        words(&step.label)
                    );
                }
                // An unreadable score draws no face, because a neutral one is a claim.
                None => assert!(
                    face.is_none(),
                    "{name}: the step {:?} has no readable score and grew a face anyway",
                    words(&step.label)
                ),
            }

            // The people under it, on the same column.
            let below: Vec<&PlacedNode> = d
                .nodes
                .iter()
                .filter(|n| n.shape == Glyph::ChartLabel && column(n) && n.center.y > step.center.y)
                .collect();
            let people = as_drawn(&task.people.join(", "));
            let said = below.first().map(|n| words(&n.label)).unwrap_or_default();
            assert!(
                below.len() <= 1,
                "{name}: {} names stand under the step {:?}",
                below.len(),
                words(&step.label)
            );
            assert_eq!(
                said,
                people,
                "{name}: {said:?} is drawn under the step {:?}, whose own people are {people:?}",
                words(&step.label)
            );
        }

        let sections: Vec<String> = model.tasks.iter().map(|t| t.section.clone()).collect();
        check_bands(name, &d, &boxes, &bands(&sections), |p| p.x);
    }
}

/// **timeline** — an event sits under its own period, and a period is in its own section's frame.
///
/// The place is a different relation in each direction, and that is the point: a left-to-right
/// timeline hangs a period's events in **its own column**, and a top-to-bottom one lays them
/// along **its own row** — where every event of every period shares one column, so the column
/// says nothing. Both are read off the drawing; neither is the id the renderer wrote.
#[test]
fn a_timelines_events_sit_under_their_own_periods_in_their_own_sections() {
    if !text_metrics::fonts_available() {
        return;
    }
    use crate::preview::mermaid::flowchart::Shape;
    use crate::preview::mermaid::timeline::{self, Direction};
    for (name, src) in kind_cases_of(timeline::is_timeline) {
        let model = timeline::parse(src).expect("parses");
        let d = kind_diagram(src);
        let along = |p: &Point| match model.direction {
            Direction::LeftToRight => p.x,
            Direction::TopToBottom => p.y,
        };

        let mut periods: Vec<&PlacedNode> = d
            .nodes
            .iter()
            .filter(|n| n.shape == Glyph::Flow(Shape::RoundedRect))
            .collect();
        periods.sort_by(|a, b| cmp(along(&a.center), along(&b.center)));
        assert_eq!(
            periods.len(),
            model.periods.len(),
            "{name}: {} periods were drawn for {}",
            periods.len(),
            model.periods.len()
        );
        let drawn: Vec<String> = periods.iter().map(|n| words(&n.label)).collect();
        let wanted: Vec<String> = model.periods.iter().map(|p| as_drawn(&p.name)).collect();
        assert_eq!(
            drawn, wanted,
            "{name}: the spine reads {drawn:?} along the axis, and the source's periods are \
             {wanted:?}"
        );

        let events: Vec<&PlacedNode> = d
            .nodes
            .iter()
            .filter(|n| n.shape == Glyph::Flow(Shape::Rect))
            .collect();
        // Which period each event belongs to, by the relation this direction makes available.
        let owner = |e: &PlacedNode| -> usize {
            match model.direction {
                Direction::LeftToRight => periods
                    .iter()
                    .position(|p| (p.center.x - e.center.x).abs() < 0.5)
                    .unwrap_or_else(|| {
                        panic!(
                            "{name}: the event {:?} hangs in no period's column",
                            words(&e.label)
                        )
                    }),
                Direction::TopToBottom => periods
                    .iter()
                    .enumerate()
                    .min_by(|a, b| {
                        cmp(
                            (a.1.center.y - e.center.y).abs(),
                            (b.1.center.y - e.center.y).abs(),
                        )
                    })
                    .map(|(i, _)| i)
                    .expect("a timeline has periods"),
            }
        };
        for (k, period) in model.periods.iter().enumerate() {
            let mut mine: Vec<&PlacedNode> =
                events.iter().copied().filter(|e| owner(e) == k).collect();
            mine.sort_by(|a, b| cmp(a.center.y, b.center.y));
            let drawn: Vec<String> = mine.iter().map(|e| words(&e.label)).collect();
            let wanted: Vec<String> = period.events.iter().map(|e| as_drawn(e)).collect();
            assert_eq!(
                drawn, wanted,
                "{name}: the period {:?} carries {drawn:?}, and its own events are {wanted:?}",
                period.name
            );
        }

        let sections: Vec<String> = model.periods.iter().map(|p| p.section.clone()).collect();
        check_bands(name, &d, &periods, &bands(&sections), along);
    }
}

/// **requirement** — a box holds its own body rows in order, and a verb belongs to its own line.
///
/// The place for a verb is the **line's two ends**, clipped to the outlines they meet, so a verb
/// on the wrong pair of boxes is caught wherever the layout happened to put them. That is also
/// what used to pin which box was which, and it only reaches a box some link names: making the
/// boxes **no link names** swap rows with each other left this test green, and in the whole suite
/// only the corpus golden went red (§6-A item 15). The corpus has such a box — `tested`, in the
/// case that writes four requirements and one link.
///
/// So the rows are anchored on the id, which for both a requirement and an element is its own
/// name (`render::requirement::box_node`). The bag comparison is kept underneath it because it is
/// the half that catches a row the drawing *added*.
#[test]
fn a_requirement_boxs_rows_and_a_verbs_line_belong_to_their_own_datum() {
    if !text_metrics::fonts_available() {
        return;
    }
    use crate::preview::mermaid::requirement;
    for (name, src) in kind_cases_of(requirement::is_requirement_diagram) {
        let model = requirement::parse(src).expect("parses");
        let d = kind_diagram(src);

        // What one box says: its stereotype, its name, then the fields the source declared. A
        // field left out is a row that is not there, which is what "declares nothing" looks like.
        let rows_of = |stereotype: &str, subject: &str, fields: &[(&str, &str)]| -> Vec<String> {
            let mut rows = vec![as_drawn(&format!("«{stereotype}»")), as_drawn(subject)];
            rows.extend(
                fields
                    .iter()
                    .filter(|(_, v)| !v.trim().is_empty())
                    .map(|(k, v)| as_drawn(&format!("{k}: {v}"))),
            );
            rows
        };
        // Every box the source declares, with the id it is drawn under.
        let mut declared: Vec<(&str, Vec<String>)> = model
            .requirements
            .iter()
            .map(|r| {
                (
                    r.name.as_str(),
                    rows_of(
                        r.kind.title(),
                        &r.name,
                        &[
                            ("Id", &r.id),
                            ("Text", &r.text),
                            ("Risk", &r.risk),
                            ("Verification", &r.verify_method),
                        ],
                    ),
                )
            })
            .collect();
        declared.extend(model.elements.iter().map(|e| {
            (
                e.name.as_str(),
                rows_of(
                    "Element",
                    &e.name,
                    &[("Type", &e.kind), ("Doc Ref", &e.doc_ref)],
                ),
            )
        }));

        // **By the id**, which is the box's own name — never by looking the rows up among the
        // rows that were drawn, and never only through a link, which an isolated box has none of.
        for (id, want) in &declared {
            let placed = d
                .node(id)
                .unwrap_or_else(|| panic!("{name}: the box {id:?} was not drawn"));
            assert_eq!(
                &panel_rows(placed),
                want,
                "{name}: the box {id:?} does not hold that datum's own rows in order"
            );
        }

        let mut wanted: Vec<Vec<String>> = declared.into_iter().map(|(_, rows)| rows).collect();
        let mut drawn: Vec<Vec<String>> = d.nodes.iter().map(panel_rows).collect();
        drawn.sort();
        wanted.sort();
        assert_eq!(
            drawn, wanted,
            "{name}: the boxes hold {drawn:?}, and the source declares {wanted:?}"
        );

        // A box's second row is its name, which is what a line's ends have to be read back as.
        let subject = |n: &PlacedNode| panel_rows(n).get(1).cloned().unwrap_or_default();
        let mut joined: Vec<(String, String, String)> = Vec::new();
        for e in &d.edges {
            let (Some(from), Some(to)) = (e.points.first(), e.points.last()) else {
                panic!("{name}: a line with no ends")
            };
            let a =
                node_at(&d, from).unwrap_or_else(|| panic!("{name}: a line starts on no one box"));
            let b = node_at(&d, to).unwrap_or_else(|| panic!("{name}: a line ends on no one box"));
            joined.push((
                subject(a),
                subject(b),
                e.label
                    .as_ref()
                    .map(|l| words(&l.label))
                    .unwrap_or_default(),
            ));
        }
        let known = |id: &str| {
            model.requirements.iter().any(|r| r.name == id)
                || model.elements.iter().any(|e| e.name == id)
        };
        let mut links: Vec<(String, String, String)> = model
            .links
            .iter()
            .filter(|l| known(&l.src) && known(&l.dst))
            .map(|l| {
                (
                    as_drawn(&l.src),
                    as_drawn(&l.dst),
                    as_drawn(l.relation.word()),
                )
            })
            .collect();
        joined.sort();
        links.sort();
        assert_eq!(
            joined, links,
            "{name}: the lines say {joined:?}, and the source's relationships are {links:?}"
        );
    }
}

/// **C4** — a box holds its own element's name, type and description, and a frame carries its own
/// boundary's title.
///
/// A boundary's place is **what it holds and how deeply it is nested**. Containment alone is not
/// enough and the corpus says why: a `Deployment_Node` inside a `Deployment_Node` holds exactly
/// the same element as its parent, so the two frames are told apart by depth — which is read off
/// the drawing, not off the `depth` the renderer wrote on them.
///
/// # What the bag and the lines together still cannot see
///
/// Both statements below are about *bags*, and what pins an entry of one to a datum is the line
/// that names it. So an element **no relationship names** is pinned by nothing: two of them, of
/// the same shape, could swap captions unseen. It is not the case today — the mutation that says
/// so is vacuous on this corpus, because every untied element is the only one of its shape — and
/// that is precisely the kind of guarantee §6-A item 10 says stops holding the moment a case is
/// added. The caption is therefore anchored on the id (`e.alias`) first, which needs nothing from
/// the corpus, and the bags are kept for what they alone catch: a caption or a frame the drawing
/// *added*.
#[test]
fn a_c4_boxs_caption_and_a_boundarys_title_belong_to_their_own_datum() {
    if !text_metrics::fonts_available() {
        return;
    }
    use crate::preview::mermaid::c4;
    for (name, src) in kind_cases_of(c4::is_c4) {
        let model = c4::parse(src).expect("parses");
        let d = kind_diagram(src);

        // The three lines inside a box: who it is, what it is, and what it does.
        let caption = |e: &c4::Element| -> Vec<String> {
            let mut lines = vec![e.label.clone()];
            if !e.kind_line.trim().is_empty() {
                let external = if e.external { ", external" } else { "" };
                lines.push(format!("[{}{external}]", e.kind_line));
            }
            if !e.descr.trim().is_empty() {
                lines.push(e.descr.clone());
            }
            Label::measure(&lines.join("\n")).lines
        };
        // **By the alias**, which is the id the element's own box is drawn under.
        for e in &model.elements {
            let placed = d
                .node(&e.alias)
                .unwrap_or_else(|| panic!("{name}: the element {:?} was not drawn", e.alias));
            assert_eq!(
                node_lines(placed),
                caption(e),
                "{name}: the box {:?} reads another element's caption",
                e.alias
            );
        }

        let mut wanted: Vec<Vec<String>> = model.elements.iter().map(caption).collect();
        let mut drawn: Vec<Vec<String>> = d.nodes.iter().map(node_lines).collect();
        drawn.sort();
        wanted.sort();
        assert_eq!(
            drawn, wanted,
            "{name}: the boxes read {drawn:?}, and the source's elements are {wanted:?}"
        );

        // …and each caption is on the box the source's *lines* reach, which is what turns the
        // multiset above into a placement. A rotation leaves that multiset untouched — it is the
        // whole reason a permutation defeats a size check — so the relationships are what say
        // which box is which: both ends of a line have been clipped to the outline they meet.
        let head = |n: &PlacedNode| node_lines(n).first().cloned().unwrap_or_default();
        let mut joined: Vec<(String, String, String)> = Vec::new();
        for e in &d.edges {
            let (Some(from), Some(to)) = (e.points.first(), e.points.last()) else {
                panic!("{name}: a line with no ends")
            };
            let a =
                node_at(&d, from).unwrap_or_else(|| panic!("{name}: a line starts on no one box"));
            let b = node_at(&d, to).unwrap_or_else(|| panic!("{name}: a line ends on no one box"));
            joined.push((
                head(a),
                head(b),
                e.label
                    .as_ref()
                    .map(|l| words(&l.label))
                    .unwrap_or_default(),
            ));
        }
        let named = |alias: &str| {
            model
                .elements
                .iter()
                .find(|e| e.alias == alias)
                .map(|e| as_drawn(&e.label))
                .unwrap_or_default()
        };
        let drawable = |id: &str| model.elements.iter().any(|e| e.alias == id);
        let mut rels: Vec<(String, String, String)> = model
            .rels
            .iter()
            .filter(|r| drawable(&r.from) && drawable(&r.to))
            .map(|r| {
                let text = if r.techn.trim().is_empty() {
                    r.label.clone()
                } else {
                    format!("{}\n[{}]", r.label, r.techn)
                };
                (named(&r.from), named(&r.to), as_drawn(&text))
            })
            .collect();
        joined.sort();
        rels.sort();
        assert_eq!(
            joined, rels,
            "{name}: the lines say {joined:?}, and the source's relationships are {rels:?}"
        );

        // What one boundary's frame says. Two boundaries holding the same elements at the same
        // depth are indistinguishable in the bag below, so the title is anchored on the alias too.
        let frame_title = |b: &c4::Boundary| -> Vec<String> {
            let title = if b.kind_line.trim().is_empty() {
                b.label.clone()
            } else {
                format!("{}\n[{}]", b.label, b.kind_line)
            };
            Label::measure(&title).lines
        };
        for b in &model.boundaries {
            // A boundary with nothing under it draws no frame at all.
            let Some(frame) = d.cluster(&b.alias) else {
                continue;
            };
            assert_eq!(
                frame.title.lines,
                frame_title(b),
                "{name}: the frame {:?} is titled with another boundary's words",
                b.alias
            );
        }

        // A frame, as the drawing gives it: what it holds, how deep it is, and what it says.
        let head = |n: &PlacedNode| node_lines(n).first().cloned().unwrap_or_default();
        let mut frames: Vec<(Vec<String>, usize, Vec<String>)> = d
            .clusters
            .iter()
            .enumerate()
            .map(|(i, c)| {
                let mut held: Vec<String> = d
                    .nodes
                    .iter()
                    .filter(|n| contains(c.bounds(), n.bounds()))
                    .map(head)
                    .collect();
                held.sort();
                (held, frame_depth(&d, i), c.title.lines.clone())
            })
            .collect();

        // …and as the source gives it. A boundary's members are elements *and* boundaries, so the
        // elements under one are found by walking down through the boundaries it names.
        fn under(model: &c4::C4, alias: &str, out: &mut Vec<String>, guard: usize) {
            if guard == 0 {
                return;
            }
            for m in model
                .boundaries
                .iter()
                .filter(|b| b.alias == alias)
                .flat_map(|b| b.members.iter())
            {
                if let Some(e) = model.elements.iter().find(|e| e.alias == *m) {
                    out.push(as_drawn(&e.label));
                } else {
                    under(model, m, out, guard - 1);
                }
            }
        }
        let depth_of = |b: &c4::Boundary| -> usize {
            let mut at = b.parent.clone();
            let mut n = 0usize;
            for _ in 0..model.boundaries.len() + 1 {
                let Some(id) = at else { break };
                n += 1;
                at = model
                    .boundaries
                    .iter()
                    .find(|o| o.alias == id)
                    .and_then(|o| o.parent.clone());
            }
            n
        };
        let mut wanted: Vec<(Vec<String>, usize, Vec<String>)> = model
            .boundaries
            .iter()
            .map(|b| {
                let mut held = Vec::new();
                under(&model, &b.alias, &mut held, model.boundaries.len() + 1);
                held.sort();
                (held, depth_of(b), frame_title(b))
            })
            .collect();
        frames.sort();
        wanted.sort();
        assert_eq!(
            frames, wanted,
            "{name}: the frames hold and say {frames:?}, and the source's boundaries are {wanted:?}"
        );
    }
}

/// **block** — a cell holds its own words, in grid order, spans and nested blocks included.
///
/// The place is the **reading order of the grid**, which is what `columns` and `:n` exist to
/// decide: rows top to bottom, cells left to right, with a row found by which boxes overlap in y.
/// A nested `block:` is its own grid inside its own frame, so it is asked the same question about
/// its own cells and the outer grid is asked about the ones outside every frame.
#[test]
fn a_block_grids_cells_hold_their_own_words_in_grid_order() {
    if !text_metrics::fonts_available() {
        return;
    }
    use crate::preview::mermaid::block::{self, Item};
    for (name, src) in kind_cases_of(block::is_block_diagram) {
        let model = block::parse(src).expect("parses");
        let d = kind_diagram(src);

        // Which frame each box and each frame sits *directly* inside — the deepest one holding it.
        let deepest = |bounds: (f64, f64, f64, f64), not: Option<usize>| -> Option<usize> {
            d.clusters
                .iter()
                .enumerate()
                .filter(|(i, c)| Some(*i) != not && contains(c.bounds(), bounds))
                .max_by_key(|(i, _)| frame_depth(&d, *i))
                .map(|(i, _)| i)
        };
        let owner: Vec<Option<usize>> = d.nodes.iter().map(|n| deepest(n.bounds(), None)).collect();
        let holder: Vec<Option<usize>> = (0..d.clusters.len())
            .map(|i| deepest(d.clusters[i].bounds(), Some(i)))
            .collect();

        // One grid, stated: the cells directly in it, read in grid order, spell its own items.
        fn check(
            name: &str,
            where_: &str,
            items: &[Item],
            region: Option<usize>,
            d: &Diagram,
            owner: &[Option<usize>],
            holder: &[Option<usize>],
        ) {
            let cells: Vec<&PlacedNode> = d
                .nodes
                .iter()
                .enumerate()
                .filter(|(i, _)| owner[*i] == region)
                .map(|(_, n)| n)
                .collect();
            let drawn: Vec<String> = grid_order(&cells).iter().map(|n| words(&n.label)).collect();
            let wanted: Vec<String> = items
                .iter()
                .filter_map(|i| match i {
                    Item::Node(n) => Some(as_drawn(&n.label)),
                    _ => None,
                })
                .collect();
            assert_eq!(
                drawn, wanted,
                "{name}: {where_} reads {drawn:?} in grid order, and its own blocks are {wanted:?}"
            );

            // …and every block nested in it is its own grid, in the same order.
            let frames: Vec<usize> = {
                let mut v: Vec<usize> = (0..d.clusters.len())
                    .filter(|i| holder[*i] == region)
                    .collect();
                v.sort_by(|a, b| {
                    cmp(d.clusters[*a].bounds().1, d.clusters[*b].bounds().1)
                        .then(cmp(d.clusters[*a].center.x, d.clusters[*b].center.x))
                });
                v
            };
            let nested: Vec<&block::Composite> = items
                .iter()
                .filter_map(|i| match i {
                    Item::Composite(c) => Some(c),
                    _ => None,
                })
                .collect();
            assert_eq!(
                frames.len(),
                nested.len(),
                "{name}: {where_} drew {} frames for {} nested blocks",
                frames.len(),
                nested.len()
            );
            for (frame, c) in frames.iter().zip(nested.iter()) {
                assert_eq!(
                    words(&d.clusters[*frame].title),
                    as_drawn(if c.named { c.id.as_str() } else { "" }),
                    "{name}: a nested block's frame is titled with other words"
                );
                check(
                    name,
                    &format!("the block {:?}", c.id),
                    &c.children,
                    Some(*frame),
                    d,
                    owner,
                    holder,
                );
            }
        }
        check(name, "the grid", &model.items, None, &d, &owner, &holder);
    }
}

/// **architecture** — a box holds its own service's name, and a frame carries its own group's
/// title.
///
/// The place for a service is the **line**: an edge names a side at each end, both ends are on
/// the outline they meet, so the pair of boxes a line joins is read off the drawing and a name
/// that moved to its neighbour makes the picture disagree with the source. A group's place is
/// what its frame holds and how deeply it is nested, as it is for a C4 boundary.
///
/// Both of those reach a service only through something else that names it: a line, or a frame it
/// changes the contents of. A service **no edge names**, in the same group as another such, is
/// reached by neither — the bag of drawn words is unchanged by any permutation, and a swap inside
/// one group changes no frame's contents. As with C4 the mutation proving it is vacuous on this
/// corpus (no two unwired services share a group) and that is exactly the guarantee §6-A item 10
/// says a corpus can withdraw without anyone noticing, so the words are anchored on the service's
/// own id first.
#[test]
fn an_architectures_boxes_and_group_titles_belong_to_their_own_services_and_groups() {
    if !text_metrics::fonts_available() {
        return;
    }
    use crate::preview::mermaid::architecture;
    for (name, src) in kind_cases_of(architecture::is_architecture) {
        let model = architecture::parse(src).expect("parses");
        let d = kind_diagram(src);

        // A junction is a dot and says nothing; a service with no title is named by its own id.
        let says = |s: &architecture::Service| -> String {
            if s.junction {
                String::new()
            } else if s.title.trim().is_empty() {
                as_drawn(&s.id)
            } else {
                as_drawn(&s.title)
            }
        };
        // **By the service's own id**, which is the id its box is drawn under.
        for s in &model.services {
            let placed = d
                .node(&s.id)
                .unwrap_or_else(|| panic!("{name}: the service {:?} was not drawn", s.id));
            assert_eq!(
                words(&placed.label),
                says(s),
                "{name}: the box for {:?} reads another service's words",
                s.id
            );
        }

        let mut drawn: Vec<String> = d.nodes.iter().map(|n| words(&n.label)).collect();
        let mut wanted: Vec<String> = model.services.iter().map(says).collect();
        drawn.sort();
        wanted.sort();
        assert_eq!(
            drawn, wanted,
            "{name}: the boxes read {drawn:?}, and the source's services are {wanted:?}"
        );

        let mut joined: Vec<(String, String)> = Vec::new();
        for e in &d.edges {
            let (Some(from), Some(to)) = (e.points.first(), e.points.last()) else {
                panic!("{name}: a line with no ends")
            };
            let a =
                node_at(&d, from).unwrap_or_else(|| panic!("{name}: a line starts on no one box"));
            let b = node_at(&d, to).unwrap_or_else(|| panic!("{name}: a line ends on no one box"));
            joined.push((words(&a.label), words(&b.label)));
        }
        let known = |id: &str| model.services.iter().any(|s| s.id == id);
        let service = |id: &str| {
            model
                .services
                .iter()
                .find(|s| s.id == id)
                .map(says)
                .unwrap_or_default()
        };
        let mut wired: Vec<(String, String)> = model
            .edges
            .iter()
            .filter(|e| known(&e.from) && known(&e.to))
            .map(|e| (service(&e.from), service(&e.to)))
            .collect();
        joined.sort();
        wired.sort();
        assert_eq!(
            joined, wired,
            "{name}: the lines join {joined:?}, and the source wires {wired:?}"
        );

        // The groups. Two groups holding the same services at the same depth cannot be told apart
        // in the bag below, so the title is anchored on the group's own id first.
        for g in &model.groups {
            // A group nothing is in draws no frame at all.
            let Some(frame) = d.cluster(&g.id) else {
                continue;
            };
            assert_eq!(
                words(&frame.title),
                as_drawn(&g.title),
                "{name}: the frame for {:?} is titled with another group's words",
                g.id
            );
        }

        // The groups: what each frame holds, how deep it is, and what it says.
        let mut frames: Vec<(Vec<String>, usize, String)> = d
            .clusters
            .iter()
            .enumerate()
            .map(|(i, c)| {
                let mut held: Vec<String> = d
                    .nodes
                    .iter()
                    .filter(|n| contains(c.bounds(), n.bounds()))
                    .map(|n| words(&n.label))
                    .collect();
                held.sort();
                (held, frame_depth(&d, i), words(&c.title))
            })
            .collect();
        let inside = |group: Option<&str>, want: &str| -> bool {
            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
        };
        let mut wanted: Vec<(Vec<String>, usize, String)> = model
            .groups
            .iter()
            .filter_map(|g| {
                let mut held: Vec<String> = model
                    .services
                    .iter()
                    .filter(|s| inside(s.group.as_deref(), &g.id))
                    .map(says)
                    .collect();
                // A group nothing is in draws no frame at all.
                if held.is_empty() {
                    return None;
                }
                held.sort();
                Some((held, inside_depth(&model, &g.id), as_drawn(&g.title)))
            })
            .collect();
        frames.sort();
        wanted.sort();
        assert_eq!(
            frames, wanted,
            "{name}: the frames hold and say {frames:?}, and the source's groups are {wanted:?}"
        );
    }
}

/// How many groups a group is written inside.
fn inside_depth(model: &crate::preview::mermaid::architecture::Architecture, id: &str) -> usize {
    let mut at = model
        .groups
        .iter()
        .find(|g| g.id == id)
        .and_then(|g| g.parent.as_deref());
    let mut n = 0usize;
    for _ in 0..model.groups.len() + 1 {
        let Some(parent) = at else { break };
        n += 1;
        at = model
            .groups
            .iter()
            .find(|g| g.id == parent)
            .and_then(|g| g.parent.as_deref());
    }
    n
}

/// **zenuml** — each name, message and note is drawn on its own datum, and the words on the
/// arrows came out of the source in the order it wrote them.
///
/// ZenUML is read into the **sequence model** and drawn by stage 4's renderer; `render::zenuml`
/// is those two calls and nothing else. So the first half of this is deliberately not a new set
/// of statements — it is [`check_sequence_text_placement`], the same function
/// [`a_sequence_diagrams_messages_participants_and_notes_each_carry_their_own_text`] calls, over
/// sources `SEQUENCE_CASES` does not contain: a nested sync call, an `@Actor`, an `if`/`else` and
/// a `while`. Writing those statements again would be the copy this project keeps being bitten
/// by.
///
/// **Be exact about what that half can see.** `zenuml::parse` produces *both* the model the
/// checker compares against and the diagram it compares, so a translation that shuffled the
/// messages between arrows would move both sides together and the checker would stay green —
/// the self-comparison §6-A records konoma being caught by twice. That is why the second half is
/// here, and why it reads the **source bytes**, which the translation cannot move: a ZenUML
/// message's words are written literally on its own line, so "each arrow's words occur in the
/// source after the arrow before it" holds for every diagram this language can express and fails
/// the moment two messages change places.
#[test]
fn a_zenuml_diagrams_participants_messages_and_notes_carry_their_own_translated_text() {
    if !text_metrics::fonts_available() {
        return;
    }
    use crate::preview::mermaid::zenuml;
    for (name, src) in kind_cases_of(zenuml::is_zenuml) {
        let model = zenuml::parse(src).expect("parses");
        let d = super::zenuml::lay_out(src).expect("lays out");
        check_sequence_text_placement(name, &model, &d);

        // **Lines, not bytes.** One ZenUML statement can put two arrows' words on one line and
        // put them in the other order while it is at it: `total = Order.total()` is the call
        // `total()` *and* the reply labelled `total`, and the reply's word is written to the
        // left of the call's. So what holds for every diagram this language can express is that
        // the arrows read down the source — never that they read along it.
        let lines: Vec<&str> = src.lines().collect();
        let mut cursor = 0usize;
        let mut read = 0usize;
        for e in &d.edges {
            let Some(label) = e.label.as_ref() else {
                continue;
            };
            let text = words(&label.label);
            // Words the reader had to change on the way in — a `<br>`, a doubled space — cannot
            // be looked back up in the source, so they are left to the checker above rather than
            // reported as being out of order.
            if text.trim().is_empty() || !src.contains(text.as_str()) {
                continue;
            }
            let at = lines
                .iter()
                .enumerate()
                .skip(cursor)
                .find(|(_, line)| line.contains(text.as_str()))
                .map(|(i, _)| i)
                .unwrap_or_else(|| {
                    panic!(
                        "{name}: the arrow reading {text:?} is drawn after one whose words the \
                         source writes on a later line — the messages have changed places"
                    )
                });
            cursor = at;
            read += 1;
        }
        assert!(
            read > 0,
            "{name}: not one arrow's words could be found in the source, so nothing was read \
             back and this half of the test asserted nothing"
        );
    }
}