kuva 0.4.0

Scientific plotting library in Rust with various backends.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
mod common;
use kuva::backend::svg::SvgBackend;
use kuva::plot::{
    BarPlot, DensityPlot, EcdfPlot, Histogram, LinePlot, PiePlot, ScatterPlot, SeriesPlot,
    StripPlot, ViolinPlot, WaterfallPlot,
};
use kuva::render::layout::Layout;
use kuva::render::plots::Plot;
use kuva::render::render::render_multiple;

fn bw_svg(plots: Vec<Plot>, layout: Layout) -> String {
    let layout = layout.with_bw_mode();
    let scene = render_multiple(plots, layout);
    SvgBackend.render_scene(&scene)
}

/// Color-mode counterpart to `bw_svg`, used by the Group 6 colormap tests to
/// confirm the same plot construction actually produces colorful fills
/// outside BW mode (i.e. the test data exercises the colormap meaningfully).
fn plain_svg(plots: Vec<Plot>, layout: Layout) -> String {
    let scene = render_multiple(plots, layout);
    SvgBackend.render_scene(&scene)
}

/// True if the SVG contains a `<rect>` element filled with the bw-mode marker
/// color — i.e. a `Square` marker drawn by `draw_marker`. Chart-area/clip
/// rects never carry this fill, so this can't false-positive on chrome.
fn svg_has_bw_square_marker(svg: &str) -> bool {
    let mut search_from = 0;
    while let Some(rel) = svg[search_from..].find("<rect ") {
        let start = search_from + rel;
        let end = svg[start..].find("/>").map_or(svg.len(), |e| start + e);
        if svg[start..end].contains("fill=\"#1a1a1a\"") {
            return true;
        }
        search_from = end.max(start + 1);
    }
    false
}

/// Count of distinct `url(#kuva-fp-...)` pattern fills used strictly *inside*
/// the plot-body clip group — i.e. on the data marks themselves, not on
/// legend swatches (which live outside the clip group and, via the
/// already-bw-aware generic legend renderer, can register their own pattern
/// defs independent of whether the plot's data-drawing code is bw-aware).
/// `svg.matches("<pattern").count()` alone is NOT a safe signal here: pattern
/// *definitions* are deduplicated by id, so a plot with an unrelated but
/// already-bw-aware legend can satisfy a bare pattern-def-count assertion
/// even when its own data marks are not bw-aware at all.
fn distinct_patterns_in_plot_body(svg: &str) -> usize {
    let body = match svg.find("<g clip-path=") {
        Some(start) => {
            let open_end = svg[start..].find('>').map_or(svg.len(), |e| start + e + 1);
            let close = svg[open_end..]
                .rfind("</g>")
                .map_or(svg.len(), |e| open_end + e);
            &svg[open_end..close]
        }
        None => svg,
    };
    let mut ids = std::collections::HashSet::new();
    let mut search_from = 0;
    while let Some(rel) = body[search_from..].find("url(#kuva-fp-") {
        let start = search_from + rel;
        let end = body[start..]
            .find(')')
            .map_or(body.len(), |e| start + e + 1);
        ids.insert(&body[start..end]);
        search_from = end;
    }
    ids.len()
}

/// Count of distinct dash "groups" (dasharray value, or "solid" when the
/// attribute is absent) among elements stroked with `stroke`. Scans forward
/// from each `stroke="<stroke>"` occurrence to the element's closing `/>`
/// (both `<line>` and `<path>` end that way in this backend), so it works
/// for both primitive kinds without needing to know which one is used.
fn distinct_dash_groups_for_stroke(svg: &str, stroke: &str) -> usize {
    let marker = format!("stroke=\"{stroke}\"");
    let mut groups = std::collections::HashSet::new();
    let mut search_from = 0;
    while let Some(rel) = svg[search_from..].find(&marker) {
        let start = search_from + rel;
        let end = svg[start..].find("/>").map_or(svg.len(), |e| start + e);
        let seg = &svg[start..end];
        let dash = if let Some(dpos) = seg.find("stroke-dasharray=\"") {
            let s = dpos + "stroke-dasharray=\"".len();
            let e = seg[s..].find('"').map_or(seg.len(), |e| s + e);
            seg[s..e].to_string()
        } else {
            "solid".to_string()
        };
        groups.insert(dash);
        search_from = end.max(start + 1);
    }
    groups.len()
}

/// True if any `fill="#rrggbb"` in the SVG has unequal R/G/B channels — i.e.
/// a genuinely colored (not grey/black/white) fill. Used for the Group 6
/// colormap family: BW mode forces `ColorMap::Grayscale`, so a data fill
/// derived from the colormap should always have R == G == B, while chrome
/// (background, axis lines, gridlines) is already neutral in kuva's default
/// theme, so this is a safe whole-SVG scan rather than needing to scope to
/// a specific element.
fn svg_has_non_grey_fill(svg: &str) -> bool {
    let mut search_from = 0;
    while let Some(rel) = svg[search_from..].find("fill=\"#") {
        let start = search_from + rel + "fill=\"".len();
        let end = svg[start..].find('"').map_or(svg.len(), |e| start + e);
        let hex = &svg[start..end];
        if hex.len() == 7 {
            let r = u8::from_str_radix(&hex[1..3], 16);
            let g = u8::from_str_radix(&hex[3..5], 16);
            let b = u8::from_str_radix(&hex[5..7], 16);
            if let (Ok(r), Ok(g), Ok(b)) = (r, g, b) {
                if r != g || g != b {
                    return true;
                }
            }
        }
        search_from = end.max(start + 1);
    }
    false
}

// ── Tier 1: fills ────────────────────────────────────────────────────────────

#[test]
fn bw_bar_single_series() {
    let bar = BarPlot::new()
        .with_bar("A", 3.2)
        .with_bar("B", 4.7)
        .with_bar("C", 2.8);
    let plots = vec![Plot::Bar(bar)];
    let layout = Layout::auto_from_plots(&plots);
    let svg = bw_svg(plots, layout);
    common::write_test_output("test_outputs/bw_bar.svg", svg.clone()).unwrap();
    assert!(svg.contains("<svg"));
    assert!(
        svg.contains("<pattern"),
        "BW bar chart should emit SVG pattern defs"
    );
    assert!(
        svg.contains("kuva-fp-"),
        "Pattern defs should use kuva-fp- prefix"
    );
}

#[test]
fn bw_bar_multi_category() {
    let bar = BarPlot::new()
        .with_bar("A", 3.0)
        .with_bar("B", 4.5)
        .with_bar("C", 2.8)
        .with_bar("D", 5.1);
    let plots = vec![Plot::Bar(bar)];
    let layout = Layout::auto_from_plots(&plots);
    let svg = bw_svg(plots, layout);
    common::write_test_output("test_outputs/bw_bar_multi.svg", svg.clone()).unwrap();
    assert!(svg.contains("<pattern"));
}

#[test]
fn bw_histogram() {
    let hist = Histogram::new()
        .with_data(vec![1.0, 2.0, 2.5, 3.0, 3.5, 4.0, 5.0, 5.5, 6.0])
        .with_range((0.0, 7.0))
        .with_bins(7)
        .with_color("steelblue");
    let plots = vec![Plot::Histogram(hist)];
    let layout = Layout::auto_from_plots(&plots);
    let svg = bw_svg(plots, layout);
    common::write_test_output("test_outputs/bw_histogram.svg", svg.clone()).unwrap();
    assert!(svg.contains("<pattern"));
}

#[test]
fn bw_pie() {
    let pie = PiePlot::new()
        .with_slice("A", 40.0, "#4499cc")
        .with_slice("B", 35.0, "#cc4444")
        .with_slice("C", 25.0, "#44cc44");
    let plots = vec![Plot::Pie(pie)];
    let layout = Layout::auto_from_plots(&plots);
    let svg = bw_svg(plots, layout);
    common::write_test_output("test_outputs/bw_pie.svg", svg.clone()).unwrap();
    assert!(svg.contains("<pattern"));
}

#[test]
fn bw_waterfall() {
    let wf = WaterfallPlot::new()
        .with_delta("Revenue", 50.0)
        .with_delta("Costs", -30.0)
        .with_total("Net");
    let plots = vec![Plot::Waterfall(wf)];
    let layout = Layout::auto_from_plots(&plots);
    let svg = bw_svg(plots, layout);
    common::write_test_output("test_outputs/bw_waterfall.svg", svg.clone()).unwrap();
    assert!(svg.contains("<pattern"));
}

#[test]
fn bw_rose() {
    use kuva::plot::rose::RosePlot;
    let rose = RosePlot::new()
        .with_slice("N", 12.0)
        .with_slice("NE", 8.0)
        .with_slice("E", 5.0)
        .with_slice("SE", 9.0)
        .with_slice("S", 14.0)
        .with_slice("SW", 11.0)
        .with_slice("W", 6.0)
        .with_slice("NW", 10.0);
    let plots = vec![Plot::Rose(rose)];
    let layout = Layout::auto_from_plots(&plots);
    let svg = bw_svg(plots, layout);
    common::write_test_output("test_outputs/bw_rose.svg", svg.clone()).unwrap();
    assert!(svg.contains("<pattern"));
}

#[test]
fn bw_upset() {
    use kuva::plot::UpSetPlot;
    let upset = UpSetPlot::new().with_data(
        vec!["Set A", "Set B", "Set C"],
        vec![52usize, 47, 36],
        vec![
            (0b001u64, 10usize),
            (0b010, 8),
            (0b100, 12),
            (0b011, 5),
            (0b111, 20),
        ],
    );
    let plots = vec![Plot::UpSet(upset)];
    let layout = Layout::auto_from_plots(&plots);
    let svg = bw_svg(plots, layout);
    common::write_test_output("test_outputs/bw_upset.svg", svg.clone()).unwrap();
    assert!(svg.contains("<pattern"));
}

// ── Tier 2: fills ────────────────────────────────────────────────────────────

#[test]
fn bw_band() {
    use kuva::plot::BandPlot;
    let x: Vec<f64> = (0..20).map(|i| i as f64 * 0.5).collect();
    let y_lower: Vec<f64> = x.iter().map(|&v| v.sin() - 0.4).collect();
    let y_upper: Vec<f64> = x.iter().map(|&v| v.sin() + 0.4).collect();
    let band = BandPlot::new(x, y_lower, y_upper)
        .with_color("steelblue")
        .with_opacity(0.4);
    let plots = vec![Plot::Band(band)];
    let layout = Layout::auto_from_plots(&plots);
    let svg = bw_svg(plots, layout);
    common::write_test_output("test_outputs/bw_band.svg", svg.clone()).unwrap();
    assert!(svg.contains("<pattern"));
}

#[test]
fn bw_boxplot() {
    use kuva::plot::BoxPlot;
    let bp = BoxPlot::new()
        .with_group("A", vec![1.0, 2.0, 2.5, 3.0, 4.0, 5.0, 2.8])
        .with_group("B", vec![2.0, 2.1, 3.5, 3.8, 4.0, 4.2, 3.0])
        .with_group("C", vec![0.5, 1.5, 2.0, 2.5, 3.5, 4.5, 2.0]);
    let plots = vec![Plot::Box(bp)];
    let layout = Layout::auto_from_plots(&plots);
    let svg = bw_svg(plots, layout);
    common::write_test_output("test_outputs/bw_boxplot.svg", svg.clone()).unwrap();
    assert!(svg.contains("<pattern"));
}

#[test]
fn bw_violin() {
    let violin = ViolinPlot::new()
        .with_group("Normal", vec![1.0, 1.5, 2.0, 2.5, 2.4, 2.4, 3.1, 1.9, 2.2])
        .with_group("Bimodal", vec![0.5, 0.6, 3.8, 4.0, 0.4, 3.5, 4.2, 0.7, 3.9])
        .with_color("mediumpurple");
    let plots = vec![Plot::Violin(violin)];
    let layout = Layout::auto_from_plots(&plots);
    let svg = bw_svg(plots, layout);
    common::write_test_output("test_outputs/bw_violin.svg", svg.clone()).unwrap();
    assert!(svg.contains("<pattern"));
}

#[test]
fn bw_density() {
    let density = DensityPlot::new()
        .with_data(vec![1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 2.0, 2.5, 3.0])
        .with_color("steelblue")
        .with_filled(true)
        .with_opacity(0.4);
    let plots = vec![Plot::Density(density)];
    let layout = Layout::auto_from_plots(&plots);
    let svg = bw_svg(plots, layout);
    common::write_test_output("test_outputs/bw_density.svg", svg.clone()).unwrap();
    assert!(svg.contains("<pattern"));
}

#[test]
fn bw_ridgeline() {
    use kuva::plot::ridgeline::RidgelinePlot;
    let rp = RidgelinePlot::new()
        .with_group(
            "Spring",
            vec![12.0, 15.0, 18.0, 14.0, 16.0, 13.0, 17.0, 15.5],
        )
        .with_group(
            "Summer",
            vec![22.0, 25.0, 28.0, 24.0, 26.0, 23.0, 27.0, 25.5],
        )
        .with_group(
            "Autumn",
            vec![10.0, 13.0, 16.0, 12.0, 14.0, 11.0, 15.0, 13.5],
        )
        .with_filled(true)
        .with_opacity(0.7);
    let plots = vec![Plot::Ridgeline(rp)];
    let layout = Layout::auto_from_plots(&plots);
    let svg = bw_svg(plots, layout);
    common::write_test_output("test_outputs/bw_ridgeline.svg", svg.clone()).unwrap();
    assert!(svg.contains("<pattern"));
}

#[test]
fn bw_stacked_area() {
    use kuva::plot::StackedAreaPlot;
    let sa = StackedAreaPlot::new()
        .with_x(vec![0.0, 1.0, 2.0, 3.0, 4.0])
        .with_series(vec![10.0, 20.0, 15.0, 25.0, 18.0])
        .with_color("steelblue")
        .with_series(vec![5.0, 10.0, 8.0, 12.0, 9.0])
        .with_color("tomato")
        .with_series(vec![3.0, 5.0, 6.0, 4.0, 7.0])
        .with_color("goldenrod");
    let plots = vec![Plot::StackedArea(sa)];
    let layout = Layout::auto_from_plots(&plots);
    let svg = bw_svg(plots, layout);
    common::write_test_output("test_outputs/bw_stacked_area.svg", svg.clone()).unwrap();
    assert!(svg.contains("<pattern"));
}

#[test]
fn bw_streamgraph() {
    use kuva::plot::streamgraph::StreamgraphPlot;
    let sg = StreamgraphPlot::new()
        .with_x(vec![1.0, 2.0, 3.0, 4.0, 5.0])
        .with_series(vec![10.0, 14.0, 18.0, 22.0, 20.0])
        .with_label("Alpha")
        .with_series(vec![5.0, 8.0, 12.0, 15.0, 14.0])
        .with_label("Beta")
        .with_series(vec![3.0, 4.0, 6.0, 8.0, 9.0])
        .with_label("Gamma");
    let plots = vec![Plot::Streamgraph(sg)];
    let layout = Layout::auto_from_plots(&plots);
    let svg = bw_svg(plots, layout);
    common::write_test_output("test_outputs/bw_streamgraph.svg", svg.clone()).unwrap();
    assert!(svg.contains("<pattern"));
}

#[test]
fn bw_survival_ci_band() {
    use kuva::plot::SurvivalPlot;
    let sp = SurvivalPlot::new()
        .with_group(
            "Control",
            vec![2.0, 4.0, 6.0, 8.0, 10.0, 12.0, 14.0],
            vec![true, true, true, false, true, false, true],
        )
        .with_group(
            "Treatment",
            vec![3.0, 7.0, 9.0, 12.0, 15.0, 18.0, 20.0],
            vec![true, false, true, false, true, false, false],
        )
        .with_ci(true);
    let plots = vec![Plot::Survival(sp)];
    let layout = Layout::auto_from_plots(&plots);
    let svg = bw_svg(plots, layout);
    common::write_test_output("test_outputs/bw_survival.svg", svg.clone()).unwrap();
    assert!(svg.contains("<svg"));
    assert!(
        svg.contains("#1a1a1a"),
        "BW survival curves should use dark stroke"
    );
}

#[test]
fn bw_roc() {
    use kuva::plot::roc::{RocGroup, RocPlot};
    let data: Vec<(f64, bool)> = vec![
        (0.95, true),
        (0.88, true),
        (0.80, false),
        (0.72, true),
        (0.65, false),
        (0.55, true),
        (0.40, false),
        (0.30, false),
        (0.22, true),
        (0.10, false),
    ];
    let group = RocGroup::new("Classifier").with_raw(data).with_ci(true);
    let roc = RocPlot::new().with_group(group);
    let plots = vec![Plot::Roc(roc)];
    let layout = Layout::auto_from_plots(&plots);
    let svg = bw_svg(plots, layout);
    common::write_test_output("test_outputs/bw_roc.svg", svg.clone()).unwrap();
    assert!(
        svg.contains("#1a1a1a"),
        "BW ROC curve should use dark stroke"
    );
}

// ── Lines ────────────────────────────────────────────────────────────────────

#[test]
fn bw_line() {
    let line = LinePlot::new()
        .with_data(vec![
            (0.0, 1.0),
            (1.0, 3.0),
            (2.0, 2.0),
            (3.0, 4.0),
            (4.0, 3.5),
        ])
        .with_color("steelblue");
    let plots = vec![Plot::Line(line)];
    let layout = Layout::auto_from_plots(&plots);
    let svg = bw_svg(plots, layout);
    common::write_test_output("test_outputs/bw_line.svg", svg.clone()).unwrap();
    assert!(
        svg.contains("#1a1a1a"),
        "BW line chart should use dark stroke color"
    );
}

#[test]
fn bw_series() {
    let data: Vec<f64> = (0..40)
        .map(|x| (x as f64 * 0.3).sin() * 3.0 + 5.0)
        .collect();
    let series = SeriesPlot::new()
        .with_data(data)
        .with_color("tomato")
        .with_line_point_style();
    let plots = vec![Plot::Series(series)];
    let layout = Layout::auto_from_plots(&plots);
    let svg = bw_svg(plots, layout);
    common::write_test_output("test_outputs/bw_series.svg", svg.clone()).unwrap();
    assert!(svg.contains("#1a1a1a"));
}

#[test]
fn bw_pr() {
    use kuva::plot::pr::{PrGroup, PrPlot};
    let data: Vec<(f64, bool)> = vec![
        (0.92, true),
        (0.85, true),
        (0.78, false),
        (0.70, true),
        (0.60, false),
        (0.50, true),
        (0.38, false),
        (0.25, false),
        (0.18, true),
        (0.08, false),
    ];
    let group = PrGroup::new("Model A").with_raw(data);
    let pr = PrPlot::new().with_group(group);
    let plots = vec![Plot::Pr(pr)];
    let layout = Layout::auto_from_plots(&plots);
    let svg = bw_svg(plots, layout);
    common::write_test_output("test_outputs/bw_pr.svg", svg.clone()).unwrap();
    assert!(svg.contains("#1a1a1a"));
}

#[test]
fn bw_ecdf() {
    let ecdf = EcdfPlot::new()
        .with_data(
            "Sample A",
            vec![1.2, 3.4, 2.1, 5.6, 4.0, 0.8, 3.3, 2.7, 4.5, 1.9],
        )
        .with_data(
            "Sample B",
            vec![2.2, 3.8, 2.9, 4.6, 3.0, 1.8, 4.3, 3.7, 5.0, 2.4],
        )
        .with_confidence_band();
    let plots = vec![Plot::Ecdf(ecdf)];
    let layout = Layout::auto_from_plots(&plots);
    let svg = bw_svg(plots, layout);
    common::write_test_output("test_outputs/bw_ecdf.svg", svg.clone()).unwrap();
    assert!(svg.contains("#1a1a1a"));
}

#[test]
fn bw_slope() {
    use kuva::plot::slope::SlopePlot;
    let sp = SlopePlot::new()
        .with_before_label("2015")
        .with_after_label("2023")
        .with_point("Germany", 68.2, 71.5)
        .with_point("France", 70.1, 68.9)
        .with_point("Italy", 65.3, 69.1)
        .with_point("Spain", 72.0, 74.2);
    let plots = vec![Plot::Slope(sp)];
    let layout = Layout::auto_from_plots(&plots);
    let svg = bw_svg(plots, layout);
    common::write_test_output("test_outputs/bw_slope.svg", svg.clone()).unwrap();
    assert!(svg.contains("#1a1a1a"));
}

#[test]
fn bw_bump() {
    use kuva::plot::bump::BumpPlot;
    let bp = BumpPlot::new()
        .with_series("Alpha", vec![1, 3, 2, 1])
        .with_series("Beta", vec![2, 1, 1, 3])
        .with_series("Gamma", vec![3, 2, 3, 2])
        .with_x_labels(["2021", "2022", "2023", "2024"]);
    let plots = vec![Plot::Bump(bp)];
    let layout = Layout::auto_from_plots(&plots);
    let svg = bw_svg(plots, layout);
    common::write_test_output("test_outputs/bw_bump.svg", svg.clone()).unwrap();
    assert!(svg.contains("#1a1a1a"));
}

// ── Scatter / points ─────────────────────────────────────────────────────────

#[test]
fn bw_scatter() {
    let scatter = ScatterPlot::new()
        .with_data(vec![
            (1.0, 2.0),
            (2.0, 3.0),
            (3.0, 1.5),
            (4.0, 4.0),
            (5.0, 2.5),
        ])
        .with_color("steelblue");
    let plots = vec![Plot::Scatter(scatter)];
    let layout = Layout::auto_from_plots(&plots);
    let svg = bw_svg(plots, layout);
    common::write_test_output("test_outputs/bw_scatter.svg", svg.clone()).unwrap();
    assert!(
        svg.contains("#1a1a1a"),
        "BW scatter chart should use dark fill color"
    );
}

#[test]
fn bw_strip() {
    let strip = StripPlot::new()
        .with_group("A", vec![1.0, 2.0, 2.5, 3.1, 4.0, 3.5, 2.2])
        .with_group("B", vec![2.0, 2.1, 3.5, 3.8, 4.0, 4.2, 3.0])
        .with_group("C", vec![0.5, 1.5, 2.0, 2.5, 3.5, 1.8, 2.8])
        .with_color("steelblue");
    let plots = vec![Plot::Strip(strip)];
    let layout = Layout::auto_from_plots(&plots);
    let svg = bw_svg(plots, layout);
    common::write_test_output("test_outputs/bw_strip.svg", svg.clone()).unwrap();
    assert!(svg.contains("#1a1a1a"));
}

#[test]
fn bw_qq() {
    use kuva::plot::QQPlot;
    let qq = QQPlot::new()
        .with_data(
            "Sample",
            vec![1.2, 3.4, 2.1, 5.6, 4.0, 0.8, 3.3, 2.7, 4.5, 1.9, 2.3, 3.8],
        )
        .with_reference_line()
        .with_color("steelblue");
    let plots = vec![Plot::QQ(qq)];
    let layout = Layout::auto_from_plots(&plots);
    let svg = bw_svg(plots, layout);
    common::write_test_output("test_outputs/bw_qq.svg", svg.clone()).unwrap();
    assert!(svg.contains("#1a1a1a"));
}

// ── Multi-series / multi-group differentiation ───────────────────────────────

#[test]
fn bw_line_multi_series_uses_distinct_dash_styles() {
    // Each series uses different y-values so the lines are visually separated
    let data0 = vec![(0.0, 1.0), (1.0, 2.0), (2.0, 1.5), (3.0, 3.0)];
    let data1 = vec![(0.0, 5.0), (1.0, 6.0), (2.0, 5.5), (3.0, 7.0)];
    let data2 = vec![(0.0, 9.0), (1.0, 10.0), (2.0, 9.5), (3.0, 11.0)];
    let line0 = LinePlot::new()
        .with_data(data0)
        .with_color("steelblue")
        .with_legend("A");
    let line1 = LinePlot::new()
        .with_data(data1)
        .with_color("tomato")
        .with_legend("B");
    let line2 = LinePlot::new()
        .with_data(data2)
        .with_color("seagreen")
        .with_legend("C");
    let plots = vec![Plot::Line(line0), Plot::Line(line1), Plot::Line(line2)];
    let mut layout = Layout::auto_from_plots(&plots);
    layout.show_legend = true;
    let svg = bw_svg(plots, layout);
    common::write_test_output("test_outputs/bw_line_multi.svg", svg.clone()).unwrap();
    // Series 0 → Solid (no dasharray), Series 1 → Dashed "8 4", Series 2 → Dotted "2 4"
    assert!(svg.contains("8 4"), "Second line should be dashed (8 4)");
    assert!(svg.contains("2 4"), "Third line should be dotted (2 4)");
}

#[test]
fn bw_scatter_multi_series_uses_distinct_shapes() {
    let pts_a = vec![(1.0, 2.0), (2.0, 3.0), (3.0, 2.5)];
    let pts_b = vec![(1.0, 3.5), (2.0, 1.5), (3.0, 4.0)];
    let scatter0 = ScatterPlot::new()
        .with_data(pts_a)
        .with_color("steelblue")
        .with_legend("A");
    let scatter1 = ScatterPlot::new()
        .with_data(pts_b)
        .with_color("tomato")
        .with_legend("B");
    let plots = vec![Plot::Scatter(scatter0), Plot::Scatter(scatter1)];
    let mut layout = Layout::auto_from_plots(&plots);
    layout.show_legend = true;
    let svg = bw_svg(plots, layout);
    common::write_test_output("test_outputs/bw_scatter_multi.svg", svg.clone()).unwrap();
    // Series 0 → Circle (fast path CircleBatch), Series 1 → Square (rect element via slow path)
    assert!(
        svg.contains("#1a1a1a"),
        "All BW scatter points should be dark"
    );
    // The second scatter uses bw_shape(1) = Square → should contain a rect or polygon path,
    // not just circles.  A simple proxy: the SVG is longer / more complex than a single series.
    assert!(
        svg.len() > 500,
        "Multi-series BW scatter should produce non-trivial SVG"
    );
}

#[test]
fn bw_density_multi_series_distinct_patterns() {
    let density0 = DensityPlot::new()
        .with_data(vec![1.0, 1.5, 2.0, 2.5, 3.0, 2.0, 1.8])
        .with_color("steelblue")
        .with_filled(true)
        .with_opacity(0.5);
    let density1 = DensityPlot::new()
        .with_data(vec![3.0, 3.5, 4.0, 4.5, 5.0, 4.0, 3.8])
        .with_color("tomato")
        .with_filled(true)
        .with_opacity(0.5);
    let plots = vec![Plot::Density(density0), Plot::Density(density1)];
    let layout = Layout::auto_from_plots(&plots);
    let svg = bw_svg(plots, layout);
    common::write_test_output("test_outputs/bw_density_multi.svg", svg.clone()).unwrap();
    // Two densities → two different patterns → at least 2 pattern defs
    let pattern_count = svg.matches("<pattern").count();
    assert!(
        pattern_count >= 2,
        "Two BW density fills should use at least 2 distinct pattern defs, got {pattern_count}"
    );
}

// Plot::Series overlays

#[test]
fn bw_series_multi_distinct_dashes() {
    // Different y-ranges so the two series are visually separated
    let v0: Vec<f64> = (0..20)
        .map(|i| (i as f64 * 0.4).sin() * 2.0 + 3.0)
        .collect();
    let v1: Vec<f64> = (0..20)
        .map(|i| (i as f64 * 0.4).cos() * 2.0 + 9.0)
        .collect();
    let s0 = SeriesPlot::new()
        .with_data(v0)
        .with_color("steelblue")
        .with_line_style();
    let s1 = SeriesPlot::new()
        .with_data(v1)
        .with_color("tomato")
        .with_line_style();
    let plots = vec![Plot::Series(s0), Plot::Series(s1)];
    let layout = Layout::auto_from_plots(&plots);
    let svg = bw_svg(plots, layout);
    common::write_test_output("test_outputs/bw_series_multi.svg", svg.clone()).unwrap();
    assert!(
        svg.contains("8 4"),
        "Second SeriesPlot should be dashed (8 4)"
    );
}

// Plot::Band overlays

#[test]
fn bw_band_multi_distinct_patterns() {
    let x: Vec<f64> = (0..15).map(|i| i as f64).collect();
    let lo0: Vec<f64> = x.iter().map(|&v| v.sin() - 0.5).collect();
    let hi0: Vec<f64> = x.iter().map(|&v| v.sin() + 0.5).collect();
    let lo1: Vec<f64> = x.iter().map(|&v| v.cos() - 0.5).collect();
    let hi1: Vec<f64> = x.iter().map(|&v| v.cos() + 0.5).collect();
    use kuva::plot::BandPlot;
    let b0 = BandPlot::new(x.clone(), lo0, hi0)
        .with_color("steelblue")
        .with_opacity(0.4);
    let b1 = BandPlot::new(x, lo1, hi1)
        .with_color("tomato")
        .with_opacity(0.4);
    let plots = vec![Plot::Band(b0), Plot::Band(b1)];
    let layout = Layout::auto_from_plots(&plots);
    let svg = bw_svg(plots, layout);
    common::write_test_output("test_outputs/bw_band_multi.svg", svg.clone()).unwrap();
    let pattern_count = svg.matches("<pattern").count();
    assert!(
        pattern_count >= 2,
        "Two BW band overlays should use at least 2 distinct pattern defs, got {pattern_count}"
    );
}

// BoxPlot multi-group

#[test]
fn bw_boxplot_multi_group_distinct_patterns() {
    use kuva::plot::BoxPlot;
    let bp = BoxPlot::new()
        .with_group("A", vec![1.0, 2.0, 2.5, 3.0, 4.0, 2.8])
        .with_group("B", vec![2.0, 3.0, 3.5, 4.0, 4.5, 3.2])
        .with_group("C", vec![0.5, 1.5, 2.0, 2.5, 3.5, 2.0]);
    let plots = vec![Plot::Box(bp)];
    let layout = Layout::auto_from_plots(&plots);
    let svg = bw_svg(plots, layout);
    common::write_test_output("test_outputs/bw_boxplot_multi.svg", svg.clone()).unwrap();
    let pattern_count = svg.matches("<pattern").count();
    assert!(
        pattern_count >= 3,
        "Three box groups should use at least 3 distinct patterns, got {pattern_count}"
    );
}

// ViolinPlot multi-group

#[test]
fn bw_violin_multi_group_distinct_patterns() {
    let violin = ViolinPlot::new()
        .with_group("A", vec![1.0, 1.5, 2.0, 2.5, 3.0, 2.2, 1.8])
        .with_group("B", vec![2.5, 3.0, 3.5, 4.0, 4.5, 3.8, 3.2])
        .with_group("C", vec![0.5, 1.0, 1.5, 2.0, 2.5, 1.2, 0.8]);
    let plots = vec![Plot::Violin(violin)];
    let layout = Layout::auto_from_plots(&plots);
    let svg = bw_svg(plots, layout);
    common::write_test_output("test_outputs/bw_violin_multi.svg", svg.clone()).unwrap();
    let pattern_count = svg.matches("<pattern").count();
    assert!(
        pattern_count >= 3,
        "Three violin groups should use at least 3 distinct patterns, got {pattern_count}"
    );
}

// RidgelinePlot multi-group

#[test]
fn bw_ridgeline_multi_group_distinct_patterns() {
    use kuva::plot::ridgeline::RidgelinePlot;
    // Five groups to clearly differentiate from the single bw_ridgeline test (3 groups)
    let rp = RidgelinePlot::new()
        .with_group("Jan", vec![2.0, 3.0, 4.0, 3.5, 2.5, 4.5, 3.0])
        .with_group("Mar", vec![8.0, 10.0, 12.0, 11.0, 9.0, 13.0, 10.5])
        .with_group("Jun", vec![20.0, 22.0, 24.0, 23.0, 21.0, 25.0, 22.5])
        .with_group("Sep", vec![14.0, 16.0, 18.0, 17.0, 15.0, 19.0, 16.5])
        .with_group("Nov", vec![6.0, 8.0, 10.0, 9.0, 7.0, 11.0, 8.5])
        .with_filled(true);
    let plots = vec![Plot::Ridgeline(rp)];
    let layout = Layout::auto_from_plots(&plots);
    let svg = bw_svg(plots, layout);
    common::write_test_output("test_outputs/bw_ridgeline_multi.svg", svg.clone()).unwrap();
    let pattern_count = svg.matches("<pattern").count();
    assert!(
        pattern_count >= 5,
        "Five ridgeline groups should use at least 5 distinct fill patterns, got {pattern_count}"
    );
}

// SurvivalPlot multi-group

#[test]
fn bw_survival_multi_group_distinct_dashes() {
    use kuva::plot::SurvivalPlot;
    let sp = SurvivalPlot::new()
        .with_group(
            "Ctrl",
            vec![2.0, 4.0, 6.0, 8.0, 10.0, 12.0],
            vec![true, true, false, true, false, true],
        )
        .with_group(
            "Trt A",
            vec![3.0, 6.0, 9.0, 12.0, 15.0, 18.0],
            vec![true, false, true, false, true, false],
        )
        .with_group(
            "Trt B",
            vec![5.0, 8.0, 11.0, 14.0, 17.0, 20.0],
            vec![true, true, false, false, true, true],
        );
    let plots = vec![Plot::Survival(sp)];
    let layout = Layout::auto_from_plots(&plots);
    let svg = bw_svg(plots, layout);
    common::write_test_output("test_outputs/bw_survival_multi.svg", svg.clone()).unwrap();
    assert!(
        svg.contains("8 4"),
        "Second survival group should use dashed line (8 4)"
    );
    assert!(
        svg.contains("2 4"),
        "Third survival group should use dotted line (2 4)"
    );
}

// EcdfPlot multi-group

#[test]
fn bw_ecdf_multi_group_distinct_dashes() {
    let ecdf = EcdfPlot::new()
        .with_data("A", vec![1.2, 2.3, 3.4, 2.1, 4.5, 1.8, 3.0, 2.7])
        .with_data("B", vec![2.2, 3.3, 4.4, 3.1, 5.5, 2.8, 4.0, 3.7])
        .with_confidence_band();
    let plots = vec![Plot::Ecdf(ecdf)];
    let layout = Layout::auto_from_plots(&plots);
    let svg = bw_svg(plots, layout);
    common::write_test_output("test_outputs/bw_ecdf_multi.svg", svg.clone()).unwrap();
    assert!(
        svg.contains("8 4"),
        "Second ECDF group should use dashed line (8 4)"
    );
}

// RocPlot multi-group

#[test]
fn bw_roc_multi_group_distinct_dashes() {
    use kuva::plot::roc::{RocGroup, RocPlot};
    // Model A: strong classifier (positives cluster at high scores)
    let data_a: Vec<(f64, bool)> = vec![
        (0.95, true),
        (0.90, true),
        (0.85, true),
        (0.80, true),
        (0.40, false),
        (0.30, false),
        (0.20, false),
        (0.10, false),
    ];
    // Model B: weak classifier (scores mixed between classes)
    let data_b: Vec<(f64, bool)> = vec![
        (0.75, true),
        (0.55, false),
        (0.65, true),
        (0.45, false),
        (0.60, false),
        (0.50, true),
        (0.40, false),
        (0.35, true),
    ];
    let g0 = RocGroup::new("Model A").with_raw(data_a);
    let g1 = RocGroup::new("Model B").with_raw(data_b);
    let roc = RocPlot::new().with_group(g0).with_group(g1);
    let plots = vec![Plot::Roc(roc)];
    let layout = Layout::auto_from_plots(&plots);
    let svg = bw_svg(plots, layout);
    common::write_test_output("test_outputs/bw_roc_multi.svg", svg.clone()).unwrap();
    assert!(
        svg.contains("8 4"),
        "Second ROC group should use dashed line (8 4)"
    );
}

// PrPlot multi-group

#[test]
fn bw_pr_multi_group_distinct_dashes() {
    use kuva::plot::pr::{PrGroup, PrPlot};
    // Model A: strong classifier
    let data_a: Vec<(f64, bool)> = vec![
        (0.95, true),
        (0.90, true),
        (0.85, true),
        (0.80, true),
        (0.40, false),
        (0.30, false),
        (0.20, false),
        (0.10, false),
    ];
    // Model B: weaker classifier
    let data_b: Vec<(f64, bool)> = vec![
        (0.75, true),
        (0.55, false),
        (0.65, true),
        (0.45, false),
        (0.60, false),
        (0.50, true),
        (0.40, false),
        (0.35, true),
    ];
    let g0 = PrGroup::new("Model A").with_raw(data_a);
    let g1 = PrGroup::new("Model B").with_raw(data_b);
    let pr = PrPlot::new().with_group(g0).with_group(g1);
    let plots = vec![Plot::Pr(pr)];
    let layout = Layout::auto_from_plots(&plots);
    let svg = bw_svg(plots, layout);
    common::write_test_output("test_outputs/bw_pr_multi.svg", svg.clone()).unwrap();
    assert!(
        svg.contains("8 4"),
        "Second PR group should use dashed line (8 4)"
    );
}

// BumpPlot multi-series

#[test]
fn bw_bump_multi_series_distinct_dashes() {
    use kuva::plot::bump::BumpPlot;
    let bp = BumpPlot::new()
        .with_series("Alpha", vec![1, 3, 2, 1])
        .with_series("Beta", vec![2, 1, 3, 2])
        .with_series("Gamma", vec![3, 2, 1, 3])
        .with_x_labels(["2021", "2022", "2023", "2024"]);
    let plots = vec![Plot::Bump(bp)];
    let layout = Layout::auto_from_plots(&plots);
    let svg = bw_svg(plots, layout);
    common::write_test_output("test_outputs/bw_bump_multi.svg", svg.clone()).unwrap();
    assert!(
        svg.contains("8 4"),
        "Second bump series should use dashed line (8 4)"
    );
    assert!(
        svg.contains("2 4"),
        "Third bump series should use dotted line (2 4)"
    );
}

// StackedAreaPlot multi-series patterns

#[test]
fn bw_stacked_area_multi_series_distinct_patterns() {
    use kuva::plot::StackedAreaPlot;
    // Five series to clearly differentiate from the single bw_stacked_area test (3 series)
    let sa = StackedAreaPlot::new()
        .with_x(vec![0.0, 1.0, 2.0, 3.0, 4.0])
        .with_series(vec![10.0, 12.0, 11.0, 14.0, 13.0])
        .with_color("steelblue")
        .with_series(vec![5.0, 7.0, 6.0, 8.0, 7.0])
        .with_color("tomato")
        .with_series(vec![3.0, 4.0, 3.0, 5.0, 4.0])
        .with_color("goldenrod")
        .with_series(vec![2.0, 3.0, 4.0, 3.0, 2.0])
        .with_color("orchid")
        .with_series(vec![1.0, 2.0, 1.0, 2.0, 3.0])
        .with_color("teal");
    let plots = vec![Plot::StackedArea(sa)];
    let layout = Layout::auto_from_plots(&plots);
    let svg = bw_svg(plots, layout);
    common::write_test_output("test_outputs/bw_stacked_area_multi.svg", svg.clone()).unwrap();
    let pattern_count = svg.matches("<pattern").count();
    assert!(
        pattern_count >= 5,
        "Five stacked-area series should use at least 5 distinct patterns, got {pattern_count}"
    );
}

#[test]
fn bw_histogram_multi_column_distinct_patterns() {
    // Multi-column mode overlays multiple Histogram plot instances (as the CLI's
    // `--y A,B,C` does). Before the bw_idx fix, every instance rendered with
    // rect_bw's hardcoded index 0 and got an identical pattern.
    let h0 = Histogram::new()
        .with_data(vec![1.0, 1.5, 2.0, 2.0, 2.5])
        .with_range((0.0, 10.0))
        .with_bins(10)
        .with_color("steelblue");
    let h1 = Histogram::new()
        .with_data(vec![5.0, 5.5, 6.0, 6.0, 6.5])
        .with_range((0.0, 10.0))
        .with_bins(10)
        .with_color("tomato");
    let plots = vec![Plot::Histogram(h0), Plot::Histogram(h1)];
    let layout = Layout::auto_from_plots(&plots);
    let svg = bw_svg(plots, layout);
    common::write_test_output("test_outputs/bw_histogram_multi.svg", svg.clone()).unwrap();
    let pattern_count = svg.matches("<pattern").count();
    assert!(
        pattern_count >= 2,
        "Two overlaid histograms should use at least 2 distinct patterns, got {pattern_count}"
    );
}

#[test]
fn bw_upset_dot_matrix_uses_guaranteed_contrast_not_user_colors() {
    // The dot matrix is a binary membership indicator, not a multi-series
    // encoding — BW mode should force dark/light contrast regardless of the
    // user's configured dot_color/dot_empty_color.
    use kuva::plot::UpSetPlot;
    let mut upset = UpSetPlot::new()
        .with_data(
            vec!["Set A", "Set B", "Set C"],
            vec![52usize, 47, 36],
            vec![
                (0b001u64, 10usize),
                (0b010, 8),
                (0b100, 12),
                (0b011, 5),
                (0b111, 20),
            ],
        )
        .with_dot_color("#4499cc");
    upset.dot_empty_color = "#eeeeee".to_string();
    let plots = vec![Plot::UpSet(upset)];
    let layout = Layout::auto_from_plots(&plots);
    let svg = bw_svg(plots, layout);
    common::write_test_output("test_outputs/bw_upset_dots.svg", svg.clone()).unwrap();
    assert!(
        svg.contains("#1a1a1a"),
        "filled dots must use the guaranteed-dark BW color, not the user's dot_color"
    );
    assert!(
        !svg.contains("#4499cc") && !svg.contains("#eeeeee"),
        "BW mode must not leak the user's configured dot colors into the output"
    );
}

// ── Group 2: point/marker family ──────────────────────────────────────────────
//
// All of these draw raw Circle points or call draw_marker directly. Without a
// bw fix, every point renders as a plain dark circle regardless of category —
// so in bw_mode a non-circle SVG tag (<rect> for Square, <path> for Triangle/
// Diamond) appearing at all is the signal that shape differentiation kicked in.

#[test]
fn bw_volcano_categories_use_distinct_shapes() {
    use kuva::plot::VolcanoPlot;
    // fc_cutoff=1.0, p_cutoff=0.05 (defaults): one point per category (NS, Down, Up).
    let volcano = VolcanoPlot::new().with_points(vec![
        ("GeneNS", 0.1, 0.5),     // NS: |log2fc| < cutoff
        ("GeneDown", -2.0, 0.01), // Down: log2fc <= -cutoff, significant
        ("GeneUp", 2.0, 0.01),    // Up: log2fc >= cutoff, significant
    ]);
    let plots = vec![Plot::Volcano(volcano)];
    let layout = Layout::auto_from_plots(&plots);
    let svg = bw_svg(plots, layout);
    common::write_test_output("test_outputs/bw_volcano.svg", svg.clone()).unwrap();
    // NS -> bw_shape(0) = Circle, Down -> bw_shape(1) = Square (<rect>).
    assert!(
        svg_has_bw_square_marker(&svg),
        "Down category should render as a Square (<rect> filled #1a1a1a)"
    );
}

#[test]
fn bw_manhattan_chromosomes_use_distinct_shapes() {
    use kuva::plot::ManhattanPlot;
    let mp = ManhattanPlot::new().with_data(vec![("1", 0.01), ("2", 0.02), ("3", 0.03)]);
    let plots = vec![Plot::Manhattan(mp)];
    let layout = Layout::auto_from_plots(&plots);
    let svg = bw_svg(plots, layout);
    common::write_test_output("test_outputs/bw_manhattan.svg", svg.clone()).unwrap();
    // Chr "1" (span 0) -> Circle, chr "2" (span 1) -> Square (<rect>).
    assert!(
        svg_has_bw_square_marker(&svg),
        "second chromosome band should render as a Square (<rect> filled #1a1a1a)"
    );
}

#[test]
fn bw_manhattan_significance_lines_are_not_colored() {
    use kuva::plot::ManhattanPlot;
    let mp = ManhattanPlot::new().with_data(vec![("1", 0.01), ("2", 0.02), ("3", 0.03)]);
    let plots = vec![Plot::Manhattan(mp)];
    let layout = Layout::auto_from_plots(&plots);
    let svg = bw_svg(plots, layout);
    common::write_test_output("test_outputs/bw_manhattan_thresholds.svg", svg.clone()).unwrap();
    assert!(
        !svg.contains("#cc3333"),
        "genome-wide significance line should not be red in BW mode"
    );
    assert!(
        svg.contains("stroke=\"#1a1a1a\""),
        "genome-wide significance line should use the BW near-black color"
    );
}

#[test]
fn bw_polar_scatter_series_use_distinct_shapes() {
    use kuva::plot::polar::PolarPlot;
    let polar = PolarPlot::new()
        .with_series(vec![1.0, 2.0, 3.0], vec![0.0, 90.0, 180.0])
        .with_series(vec![1.5, 2.5], vec![45.0, 135.0]);
    let plots = vec![Plot::Polar(polar)];
    let layout = Layout::auto_from_plots(&plots);
    let svg = bw_svg(plots, layout);
    common::write_test_output("test_outputs/bw_polar.svg", svg.clone()).unwrap();
    assert!(
        svg_has_bw_square_marker(&svg),
        "second polar series should render as a Square (<rect> filled #1a1a1a)"
    );
}

#[test]
fn bw_ternary_groups_use_distinct_shapes() {
    use kuva::plot::ternary::TernaryPlot;
    let ternary = TernaryPlot::new()
        .with_point_group(0.6, 0.2, 0.2, "A")
        .with_point_group(0.2, 0.6, 0.2, "B")
        .with_point_group(0.3, 0.5, 0.2, "B");
    let plots = vec![Plot::Ternary(ternary)];
    let layout = Layout::auto_from_plots(&plots);
    let svg = bw_svg(plots, layout);
    common::write_test_output("test_outputs/bw_ternary.svg", svg.clone()).unwrap();
    assert!(
        svg_has_bw_square_marker(&svg),
        "second ternary group should render as a Square (<rect> filled #1a1a1a)"
    );
}

#[test]
fn bw_diceplot_categorical_dots_use_distinct_shapes() {
    use kuva::plot::diceplot::DicePlot;
    let dice = DicePlot::new(1)
        .with_category_labels(vec!["Only".into()])
        .with_dot_legend(vec![("Down", "#2166ac"), ("Up", "#b2182b")])
        .with_records(vec![
            ("Row1", "Col1", "Only", "#2166ac"),
            ("Row2", "Col1", "Only", "#b2182b"),
        ]);
    let plots = vec![Plot::DicePlot(dice)];
    let layout = Layout::auto_from_plots(&plots);
    let svg = bw_svg(plots, layout);
    common::write_test_output("test_outputs/bw_diceplot.svg", svg.clone()).unwrap();
    // "Down" (legend index 0) -> Circle, "Up" (legend index 1) -> Square (<rect>).
    assert!(
        svg_has_bw_square_marker(&svg),
        "second dot-legend category should render as a Square (<rect> filled #1a1a1a)"
    );
}

#[test]
fn bw_diceplot_dot_legend_swatches_are_not_colored() {
    use kuva::plot::diceplot::DicePlot;
    let dice = DicePlot::new(1)
        .with_category_labels(vec!["Only".into()])
        .with_dot_legend(vec![("Down", "#2166ac"), ("Up", "#b2182b")])
        .with_records(vec![
            ("Row1", "Col1", "Only", "#2166ac"),
            ("Row2", "Col1", "Only", "#b2182b"),
        ]);
    let plots = vec![Plot::DicePlot(dice)];
    let layout = Layout::auto_from_plots(&plots);
    let svg = bw_svg(plots, layout);
    common::write_test_output("test_outputs/bw_diceplot_legend.svg", svg.clone()).unwrap();
    assert!(
        !svg.contains("#2166ac") && !svg.contains("#b2182b"),
        "dot-legend swatches (drawn via add_legend_at) should not leak the user's configured colors in BW mode"
    );
}

#[test]
fn bw_venn_sets_use_distinct_patterns() {
    use kuva::plot::venn::VennPlot;
    let venn = VennPlot::new()
        .with_set_size("Set A", 100)
        .with_set_size("Set B", 80)
        .with_overlap(["Set A", "Set B"], 30);
    let plots = vec![Plot::Venn(venn)];
    let layout = Layout::auto_from_plots(&plots);
    let svg = bw_svg(plots, layout);
    common::write_test_output("test_outputs/bw_venn.svg", svg.clone()).unwrap();
    let pattern_count = svg.matches("<pattern").count();
    assert!(
        pattern_count >= 2,
        "Two venn sets should use at least 2 distinct patterns, got {pattern_count}"
    );
}

#[test]
fn bw_scatter3d_instances_use_distinct_shapes() {
    use kuva::plot::scatter3d::Scatter3DPlot;
    let s0 = Scatter3DPlot::new()
        .with_data(vec![(1.0, 2.0, 3.0), (2.0, 3.0, 4.0)])
        .with_color("steelblue");
    let s1 = Scatter3DPlot::new()
        .with_data(vec![(4.0, 5.0, 1.0), (5.0, 6.0, 2.0)])
        .with_color("tomato");
    let plots = vec![Plot::Scatter3D(s0), Plot::Scatter3D(s1)];
    let layout = Layout::auto_from_plots(&plots);
    let svg = bw_svg(plots, layout);
    common::write_test_output("test_outputs/bw_scatter3d.svg", svg.clone()).unwrap();
    assert!(
        svg_has_bw_square_marker(&svg),
        "second Scatter3D instance should render as a Square (<rect> filled #1a1a1a)"
    );
}

// ── Group 3: hierarchical rect/path family ──────────────────────────────────

#[test]
fn bw_mosaic_rows_use_distinct_patterns() {
    use kuva::plot::mosaic::MosaicPlot;
    let mosaic = MosaicPlot::new()
        .with_cell("Control", "Positive", 30.0)
        .with_cell("Control", "Negative", 70.0)
        .with_cell("Treated", "Positive", 60.0)
        .with_cell("Treated", "Negative", 40.0);
    let plots = vec![Plot::Mosaic(mosaic)];
    let layout = Layout::auto_from_plots(&plots);
    let svg = bw_svg(plots, layout);
    common::write_test_output("test_outputs/bw_mosaic.svg", svg.clone()).unwrap();
    let pattern_count = distinct_patterns_in_plot_body(&svg);
    assert!(
        pattern_count >= 2,
        "two mosaic rows should use at least 2 distinct patterns, got {pattern_count}"
    );
}

#[test]
fn bw_treemap_roots_use_distinct_patterns() {
    use kuva::plot::treemap::{TreemapNode, TreemapPlot};
    let treemap = TreemapPlot::new()
        .with_node(TreemapNode::new(
            "Foods",
            vec![
                TreemapNode::leaf("Apples", 30.0),
                TreemapNode::leaf("Oranges", 20.0),
            ],
        ))
        .with_node(TreemapNode::new(
            "Drinks",
            vec![
                TreemapNode::leaf("Coffee", 15.0),
                TreemapNode::leaf("Tea", 10.0),
            ],
        ));
    let plots = vec![Plot::Treemap(treemap)];
    let layout = Layout::auto_from_plots(&plots);
    let svg = bw_svg(plots, layout);
    common::write_test_output("test_outputs/bw_treemap.svg", svg.clone()).unwrap();
    let pattern_count = distinct_patterns_in_plot_body(&svg);
    assert!(
        pattern_count >= 2,
        "two treemap roots should use at least 2 distinct patterns, got {pattern_count}"
    );
}

#[test]
fn bw_sunburst_roots_use_distinct_patterns() {
    use kuva::plot::sunburst::SunburstPlot;
    use kuva::plot::treemap::TreemapNode;
    let sunburst = SunburstPlot::new()
        .with_node(TreemapNode::new(
            "Org1",
            vec![
                TreemapNode::leaf("Alice", 40.0),
                TreemapNode::leaf("Bob", 30.0),
            ],
        ))
        .with_node(TreemapNode::new(
            "Org2",
            vec![
                TreemapNode::leaf("Carol", 25.0),
                TreemapNode::leaf("Dave", 20.0),
            ],
        ));
    let plots = vec![Plot::Sunburst(sunburst)];
    let layout = Layout::auto_from_plots(&plots);
    let svg = bw_svg(plots, layout);
    common::write_test_output("test_outputs/bw_sunburst.svg", svg.clone()).unwrap();
    let pattern_count = distinct_patterns_in_plot_body(&svg);
    assert!(
        pattern_count >= 2,
        "two sunburst roots should use at least 2 distinct patterns, got {pattern_count}"
    );
}

#[test]
fn bw_funnel_stages_use_distinct_patterns() {
    use kuva::plot::funnel::FunnelPlot;
    let funnel = FunnelPlot::new()
        .with_stage("Screened", 1200.0)
        .with_stage("Eligible", 800.0)
        .with_stage("Enrolled", 600.0);
    let plots = vec![Plot::Funnel(funnel)];
    let layout = Layout::auto_from_plots(&plots);
    let svg = bw_svg(plots, layout);
    common::write_test_output("test_outputs/bw_funnel.svg", svg.clone()).unwrap();
    let pattern_count = distinct_patterns_in_plot_body(&svg);
    assert!(
        pattern_count >= 2,
        "funnel stages should use at least 2 distinct patterns, got {pattern_count}"
    );
}

#[test]
fn bw_pyramid_sides_use_distinct_patterns() {
    use kuva::plot::pyramid::PopulationPyramid;
    let pyramid = PopulationPyramid::new()
        .with_left_color("#4C72B0")
        .with_right_color("#DD8452")
        .with_group("0-4", 6.5, 6.2)
        .with_group("5-9", 6.8, 6.5);
    let plots = vec![Plot::Pyramid(pyramid)];
    let layout = Layout::auto_from_plots(&plots);
    let svg = bw_svg(plots, layout);
    common::write_test_output("test_outputs/bw_pyramid.svg", svg.clone()).unwrap();
    let pattern_count = distinct_patterns_in_plot_body(&svg);
    assert!(
        pattern_count >= 2,
        "pyramid left/right halves should use at least 2 distinct patterns, got {pattern_count}"
    );
}

#[test]
fn bw_waffle_categories_use_distinct_patterns() {
    use kuva::plot::waffle::WafflePlot;
    let waffle = WafflePlot::new()
        .with_category("Treated", 45.0, "steelblue")
        .with_category("Partial", 30.0, "gold")
        .with_category("Untreated", 25.0, "#e74c3c")
        .with_grid(5, 20);
    let plots = vec![Plot::Waffle(waffle)];
    let layout = Layout::auto_from_plots(&plots);
    let svg = bw_svg(plots, layout);
    common::write_test_output("test_outputs/bw_waffle.svg", svg.clone()).unwrap();
    let pattern_count = distinct_patterns_in_plot_body(&svg);
    assert!(
        pattern_count >= 2,
        "waffle categories should use at least 2 distinct patterns, got {pattern_count}"
    );
}

#[test]
fn bw_gantt_groups_use_distinct_patterns() {
    use kuva::plot::gantt::GanttPlot;
    let gantt = GanttPlot::new()
        .with_task_group("Design", "Wireframes", 0.0, 3.0)
        .with_task_group("Dev", "Backend API", 3.0, 8.0);
    let plots = vec![Plot::Gantt(gantt)];
    let layout = Layout::auto_from_plots(&plots);
    let svg = bw_svg(plots, layout);
    common::write_test_output("test_outputs/bw_gantt.svg", svg.clone()).unwrap();
    let pattern_count = distinct_patterns_in_plot_body(&svg);
    assert!(
        pattern_count >= 2,
        "gantt task groups should use at least 2 distinct patterns, got {pattern_count}"
    );
}

#[test]
fn bw_brick_template_chars_use_distinct_patterns() {
    use kuva::plot::brick::BrickPlot;
    use std::collections::HashMap;
    let mut template = HashMap::new();
    template.insert('X', "steelblue".to_string());
    template.insert('Y', "#ff7f0e".to_string());
    let brick = BrickPlot::new()
        .with_sequences(vec!["XYXYXY", "XXYXYY"])
        .with_template(template);
    let plots = vec![Plot::Brick(brick)];
    let layout = Layout::auto_from_plots(&plots);
    let svg = bw_svg(plots, layout);
    common::write_test_output("test_outputs/bw_brick.svg", svg.clone()).unwrap();
    let pattern_count = distinct_patterns_in_plot_body(&svg);
    assert!(
        pattern_count >= 2,
        "brick template characters should use at least 2 distinct patterns, got {pattern_count}"
    );
}

// ── Group 4: line/stroke family ─────────────────────────────────────────────

#[test]
fn bw_candlestick_up_down_use_distinct_patterns() {
    use kuva::plot::candlestick::CandlestickPlot;
    let candlestick = CandlestickPlot::new()
        .with_candle("Day1", 10.0, 12.0, 9.0, 11.5) // up
        .with_candle("Day2", 11.5, 12.0, 8.0, 8.5); // down
    let plots = vec![Plot::Candlestick(candlestick)];
    let layout = Layout::auto_from_plots(&plots);
    let svg = bw_svg(plots, layout);
    common::write_test_output("test_outputs/bw_candlestick.svg", svg.clone()).unwrap();
    let pattern_count = distinct_patterns_in_plot_body(&svg);
    assert!(
        pattern_count >= 2,
        "up vs down candle bodies should use at least 2 distinct patterns, got {pattern_count}"
    );
}

#[test]
fn bw_forest_rows_use_distinct_patterns() {
    use kuva::plot::forest::ForestPlot;
    let forest = ForestPlot::new()
        .with_row("Study A", 0.50, 0.10, 0.90)
        .with_row("Study B", -0.30, -0.80, 0.20);
    let plots = vec![Plot::Forest(forest)];
    let layout = Layout::auto_from_plots(&plots);
    let svg = bw_svg(plots, layout);
    common::write_test_output("test_outputs/bw_forest.svg", svg.clone()).unwrap();
    let pattern_count = distinct_patterns_in_plot_body(&svg);
    assert!(
        pattern_count >= 2,
        "forest row markers should use at least 2 distinct patterns, got {pattern_count}"
    );
}

#[test]
fn bw_lollipop_points_use_distinct_patterns() {
    use kuva::plot::lollipop::LollipopPlot;
    let lollipop = LollipopPlot::new()
        .with_point(1.0, 5.0)
        .with_point(2.0, 8.0);
    let plots = vec![Plot::Lollipop(lollipop)];
    let layout = Layout::auto_from_plots(&plots);
    let svg = bw_svg(plots, layout);
    common::write_test_output("test_outputs/bw_lollipop.svg", svg.clone()).unwrap();
    let pattern_count = distinct_patterns_in_plot_body(&svg);
    assert!(
        pattern_count >= 2,
        "lollipop dots should use at least 2 distinct patterns, got {pattern_count}"
    );
}

#[test]
fn bw_phylo_clades_use_distinct_dashes() {
    use kuva::plot::PhyloTree;
    let edges: Vec<(&str, &str, f64)> = vec![
        ("root", "Bacteria", 1.5),
        ("root", "Eukarya", 2.0),
        ("Bacteria", "E. coli", 0.5),
        ("Bacteria", "B. subtilis", 0.7),
        ("Eukarya", "Yeast", 1.0),
        ("Eukarya", "Human", 0.8),
    ];
    // node id 1 = "Bacteria", id 2 = "Eukarya" (see tests/phylo_basic.rs)
    let tree = PhyloTree::from_edges(&edges)
        .with_clade_color(1, "#e41a1c")
        .with_clade_color(2, "#377eb8");
    let plots = vec![Plot::PhyloTree(tree)];
    let layout = Layout::auto_from_plots(&plots);
    let svg = bw_svg(plots, layout);
    common::write_test_output("test_outputs/bw_phylo.svg", svg.clone()).unwrap();
    assert!(
        !svg.contains("#e41a1c") && !svg.contains("#377eb8"),
        "clade colors should not leak into BW mode"
    );
    let dash_groups = distinct_dash_groups_for_stroke(&svg, "#1a1a1a");
    assert!(
        dash_groups >= 2,
        "the two clades should use at least 2 distinct dash styles, got {dash_groups}"
    );
}

#[test]
fn bw_parallel_groups_use_distinct_dashes() {
    use kuva::plot::parallel::ParallelPlot;
    let parallel = ParallelPlot::new()
        .with_axis_names(vec!["A", "B", "C"])
        .with_row_group("Group1", vec![1.0, 2.0, 3.0])
        .with_row_group("Group2", vec![3.0, 2.0, 1.0]);
    let plots = vec![Plot::Parallel(parallel)];
    let layout = Layout::auto_from_plots(&plots);
    let svg = bw_svg(plots, layout);
    common::write_test_output("test_outputs/bw_parallel.svg", svg.clone()).unwrap();
    let dash_groups = distinct_dash_groups_for_stroke(&svg, "#1a1a1a");
    assert!(
        dash_groups >= 2,
        "the two row groups should use at least 2 distinct dash styles, got {dash_groups}"
    );
}

#[test]
fn bw_radar_series_use_distinct_patterns() {
    use kuva::plot::radar::RadarPlot;
    let radar = RadarPlot::new(vec!["Speed", "Power", "Range"])
        .with_series(vec![3.0, 4.0, 5.0])
        .with_series(vec![5.0, 3.0, 2.0])
        .with_filled(true);
    let plots = vec![Plot::Radar(radar)];
    let layout = Layout::auto_from_plots(&plots);
    let svg = bw_svg(plots, layout);
    common::write_test_output("test_outputs/bw_radar.svg", svg.clone()).unwrap();
    let pattern_count = distinct_patterns_in_plot_body(&svg);
    assert!(
        pattern_count >= 2,
        "the two radar series should use at least 2 distinct patterns, got {pattern_count}"
    );
}

#[test]
fn bw_horizon_pos_neg_are_not_colored() {
    use kuva::plot::horizon::HorizonPlot;
    let x: Vec<f64> = (0..10).map(|i| i as f64).collect();
    let y: Vec<f64> = x.iter().map(|&t| (t - 5.0) * 2.0).collect(); // crosses zero
    let horizon = HorizonPlot::new().with_series("Temp", x, y);
    let plots = vec![Plot::Horizon(horizon)];
    let layout = Layout::auto_from_plots(&plots);
    let svg = bw_svg(plots, layout);
    common::write_test_output("test_outputs/bw_horizon.svg", svg.clone()).unwrap();
    // Default pos_color/neg_color hex (see horizon_basic.rs) should not leak through.
    assert!(
        !svg.contains("#1f77b4") && !svg.contains("#d62728"),
        "pos/neg colors should not leak into BW mode"
    );
    assert!(
        svg.contains("#1a1a1a"),
        "positive bands should use the fixed BW dark grey"
    );
    assert!(
        svg.contains("#888888"),
        "negative bands should use a distinct fixed BW grey"
    );
}

// ── Group 5: composite/pixel-space, most bespoke ────────────────────────────

#[test]
fn bw_chord_nodes_use_distinct_patterns() {
    use kuva::plot::chord::ChordPlot;
    let chord = ChordPlot::new()
        .with_matrix(vec![
            vec![0.0, 10.0, 5.0],
            vec![10.0, 0.0, 3.0],
            vec![5.0, 3.0, 0.0],
        ])
        .with_labels(vec!["A", "B", "C"]);
    let plots = vec![Plot::Chord(chord)];
    let layout = Layout::auto_from_plots(&plots);
    let svg = bw_svg(plots, layout);
    common::write_test_output("test_outputs/bw_chord.svg", svg.clone()).unwrap();
    let pattern_count = distinct_patterns_in_plot_body(&svg);
    assert!(
        pattern_count >= 2,
        "chord nodes should use at least 2 distinct patterns, got {pattern_count}"
    );
}

#[test]
fn bw_sankey_nodes_use_distinct_patterns() {
    use kuva::plot::sankey::SankeyPlot;
    let sankey = SankeyPlot::new()
        .with_link("Source A", "Target", 10.0)
        .with_link("Source B", "Target", 5.0);
    let plots = vec![Plot::Sankey(sankey)];
    let layout = Layout::auto_from_plots(&plots);
    let svg = bw_svg(plots, layout);
    common::write_test_output("test_outputs/bw_sankey.svg", svg.clone()).unwrap();
    let pattern_count = distinct_patterns_in_plot_body(&svg);
    assert!(
        pattern_count >= 2,
        "sankey nodes/links should use at least 2 distinct patterns, got {pattern_count}"
    );
}

#[test]
fn bw_sankey_node_rects_have_visible_border() {
    // Links share their source node's pattern (see bw_sankey_nodes_use_distinct_patterns),
    // so without an outline the node rect visually disappears into its own outgoing
    // ribbon — reported directly by the user after visually checking Group 5.
    use kuva::plot::sankey::SankeyPlot;
    let sankey = SankeyPlot::new().with_link("Source A", "Target", 10.0);
    let plots = vec![Plot::Sankey(sankey)];
    let layout = Layout::auto_from_plots(&plots);
    let svg = bw_svg(plots, layout);
    assert!(
        svg.contains("stroke=\"#1a1a1a\" stroke-width=\"2\""),
        "sankey node rects should have a visible dark border in BW mode"
    );
}

#[test]
fn bw_synteny_sequences_use_distinct_patterns() {
    use kuva::plot::synteny::SyntenyPlot;
    let synteny = SyntenyPlot::new()
        .with_sequences(vec![("Seq1", 100.0), ("Seq2", 100.0)])
        .with_block(0, 10.0, 40.0, 1, 10.0, 40.0);
    let plots = vec![Plot::Synteny(synteny)];
    let layout = Layout::auto_from_plots(&plots);
    let svg = bw_svg(plots, layout);
    common::write_test_output("test_outputs/bw_synteny.svg", svg.clone()).unwrap();
    let pattern_count = distinct_patterns_in_plot_body(&svg);
    assert!(
        pattern_count >= 2,
        "synteny sequence bars should use at least 2 distinct patterns, got {pattern_count}"
    );
}

#[test]
fn bw_synteny_bars_have_visible_border() {
    // Block index and sequence index are independent, so a ribbon can share
    // its sequence bar's pattern (as in this exact 2-sequence/1-block case,
    // both index 0) — without an outline the bar visually disappears into
    // the ribbon it sits on top of.
    use kuva::plot::synteny::SyntenyPlot;
    let synteny = SyntenyPlot::new()
        .with_sequences(vec![("Seq1", 100.0), ("Seq2", 100.0)])
        .with_block(0, 10.0, 40.0, 1, 10.0, 40.0);
    let plots = vec![Plot::Synteny(synteny)];
    let layout = Layout::auto_from_plots(&plots);
    let svg = bw_svg(plots, layout);
    assert!(
        svg.contains("stroke=\"#1a1a1a\" stroke-width=\"2\""),
        "synteny sequence bars should have a visible dark border in BW mode"
    );
}

#[test]
fn bw_network_groups_use_distinct_patterns() {
    use kuva::plot::network::NetworkPlot;
    let network = NetworkPlot::new()
        .with_node_group("N1", "GroupA")
        .with_node_group("N2", "GroupB")
        .with_edge("N1", "N2", 1.0);
    let plots = vec![Plot::Network(network)];
    let layout = Layout::auto_from_plots(&plots);
    let svg = bw_svg(plots, layout);
    common::write_test_output("test_outputs/bw_network.svg", svg.clone()).unwrap();
    let pattern_count = distinct_patterns_in_plot_body(&svg);
    assert!(
        pattern_count >= 2,
        "network node groups should use at least 2 distinct patterns, got {pattern_count}"
    );
}

#[test]
fn bw_joint_groups_use_distinct_patterns() {
    use kuva::plot::jointplot::JointPlot;
    let joint = JointPlot::new()
        .with_group(
            "Group1",
            vec![1.0, 2.0, 3.0, 2.0],
            vec![1.0, 2.0, 1.5, 2.5],
            "steelblue",
        )
        .with_group(
            "Group2",
            vec![4.0, 5.0, 6.0, 5.0],
            vec![4.0, 5.0, 4.5, 5.5],
            "tomato",
        );
    let plots = vec![Plot::Joint(joint)];
    let layout = Layout::auto_from_plots(&plots);
    let svg = bw_svg(plots, layout);
    common::write_test_output("test_outputs/bw_joint.svg", svg.clone()).unwrap();
    // Joint's marginal panels are appended to the scene *after* the inner
    // scatter sub-scene's own <g clip-path> group closes, so
    // distinct_patterns_in_plot_body (which assumes one clip group wraps all
    // data) would truncate before reaching them. The scatter markers use
    // bw_shape (no patterns) and the custom group legend uses bw_shape too
    // (see the DicePlot/Group2 legend fix), so a plain pattern-def count is
    // safe here — nothing else in this SVG registers a pattern.
    let pattern_count = svg.matches("<pattern").count();
    assert!(
        pattern_count >= 2,
        "joint marginal histograms should use at least 2 distinct patterns, got {pattern_count}"
    );
}

#[test]
fn bw_joint_standalone_render_jointplot_respects_bw_mode() {
    // render_jointplot() builds its own stub ComputedLayout independent of
    // render_multiple's dispatch path — it must copy layout.bw_mode across,
    // or bw_mode silently has no effect when called this way.
    use kuva::plot::jointplot::JointPlot;
    use kuva::render::render::render_jointplot;
    let joint = JointPlot::new()
        .with_group(
            "Group1",
            vec![1.0, 2.0, 3.0, 2.0],
            vec![1.0, 2.0, 1.5, 2.5],
            "steelblue",
        )
        .with_group(
            "Group2",
            vec![4.0, 5.0, 6.0, 5.0],
            vec![4.0, 5.0, 4.5, 5.5],
            "tomato",
        );
    let layout = Layout::new((0.0, 7.0), (0.0, 7.0)).with_bw_mode();
    let scene = render_jointplot(joint, layout);
    let svg = SvgBackend.render_scene(&scene);
    common::write_test_output("test_outputs/bw_joint_standalone.svg", svg.clone()).unwrap();
    assert!(
        !svg.contains("steelblue") && !svg.contains("tomato"),
        "group colors should not leak through the standalone render_jointplot path in BW mode"
    );
    assert!(
        svg.contains("<pattern"),
        "standalone render_jointplot should still emit BW patterns"
    );
}

#[test]
fn bw_raincloud_groups_use_distinct_patterns() {
    use kuva::plot::raincloud::RaincloudPlot;
    let raincloud = RaincloudPlot::new()
        .with_group("Control", vec![1.0, 2.0, 2.5, 3.0, 2.2, 2.8, 1.8])
        .with_group("Treated", vec![4.0, 5.0, 4.5, 5.5, 4.2, 4.8, 5.2]);
    let plots = vec![Plot::Raincloud(raincloud)];
    let layout = Layout::auto_from_plots(&plots);
    let svg = bw_svg(plots, layout);
    common::write_test_output("test_outputs/bw_raincloud.svg", svg.clone()).unwrap();
    let pattern_count = distinct_patterns_in_plot_body(&svg);
    assert!(
        pattern_count >= 2,
        "raincloud groups should use at least 2 distinct patterns, got {pattern_count}"
    );
}

// ── Group 6: continuous-value / colormap family ─────────────────────────────
//
// These plots encode a continuous numeric value via a `ColorMap`, not a
// discrete per-series category, so hatch/dash/shape differentiation doesn't
// apply. Design decision: force `ColorMap::Grayscale` in BW mode instead.
// Each test renders the same construction twice — once plain (sanity-checking
// that the default colormap, usually Viridis, actually produces colorful
// fills for this data) and once with BW mode (asserting no colorful fill
// survives).

#[test]
fn bw_histogram2d_forces_grayscale() {
    use kuva::plot::Histogram2D;
    let data = vec![(1.0, 1.0), (1.0, 1.0), (1.0, 1.0), (5.0, 5.0)];
    let build = || Histogram2D::new().with_data(data.clone(), (0.0, 6.0), (0.0, 6.0), 3, 3);
    let plain = plain_svg(
        vec![Plot::Histogram2d(build())],
        Layout::auto_from_plots(&[Plot::Histogram2d(build())]),
    );
    assert!(
        svg_has_non_grey_fill(&plain),
        "sanity: default colormap should produce colorful fills"
    );
    let svg = bw_svg(
        vec![Plot::Histogram2d(build())],
        Layout::auto_from_plots(&[Plot::Histogram2d(build())]),
    );
    common::write_test_output("test_outputs/bw_histogram2d.svg", svg.clone()).unwrap();
    assert!(
        !svg_has_non_grey_fill(&svg),
        "histogram2d bins should use only grayscale fills in BW mode"
    );
}

#[test]
fn bw_heatmap_forces_grayscale() {
    use kuva::plot::Heatmap;
    let build = || {
        Heatmap::new().with_data(vec![
            vec![1.0, 5.0, 9.0],
            vec![2.0, 6.0, 3.0],
            vec![8.0, 4.0, 7.0],
        ])
    };
    let plots_plain = vec![Plot::Heatmap(build())];
    let plots_bw = vec![Plot::Heatmap(build())];
    let plain = plain_svg(
        plots_plain,
        Layout::auto_from_plots(&[Plot::Heatmap(build())]),
    );
    assert!(
        svg_has_non_grey_fill(&plain),
        "sanity: default colormap should produce colorful fills"
    );
    let svg = bw_svg(plots_bw, Layout::auto_from_plots(&[Plot::Heatmap(build())]));
    common::write_test_output("test_outputs/bw_heatmap.svg", svg.clone()).unwrap();
    assert!(
        !svg_has_non_grey_fill(&svg),
        "heatmap cells should use only grayscale fills in BW mode"
    );
}

#[test]
fn bw_hexbin_forces_grayscale() {
    use kuva::plot::hexbin::HexbinPlot;
    // Varying density per bin (not a uniform grid) so norm spans [0, 1] —
    // a uniform grid makes every bin normalize to the same value, which
    // would pass this assertion trivially without proving anything.
    let mut xs: Vec<f64> = (0..40).map(|i| (i % 8) as f64).collect();
    let mut ys: Vec<f64> = (0..40).map(|i| (i / 8) as f64).collect();
    for _ in 0..20 {
        xs.push(0.0);
        ys.push(0.0);
    }
    let build = || HexbinPlot::new().with_data(xs.clone(), ys.clone());
    let plain = plain_svg(
        vec![Plot::Hexbin(build())],
        Layout::auto_from_plots(&[Plot::Hexbin(build())]),
    );
    assert!(
        svg_has_non_grey_fill(&plain),
        "sanity: default colormap should produce colorful fills"
    );
    let svg = bw_svg(
        vec![Plot::Hexbin(build())],
        Layout::auto_from_plots(&[Plot::Hexbin(build())]),
    );
    common::write_test_output("test_outputs/bw_hexbin.svg", svg.clone()).unwrap();
    assert!(
        !svg_has_non_grey_fill(&svg),
        "hexbin cells should use only grayscale fills in BW mode"
    );
}

#[test]
fn bw_calendar_forces_grayscale() {
    use kuva::plot::calendar::{CalendarAgg, CalendarPlot};
    // CalendarAgg defaults to Count (occurrences per day, ignoring the value
    // field) — with one entry per date that collapses every day to the same
    // count and defeats the point of varying the input values. Sum makes a
    // single entry's value pass through unchanged.
    let build = || {
        CalendarPlot::new()
            .with_data(vec![
                ("2026-01-01", 1.0),
                ("2026-01-02", 5.0),
                ("2026-01-03", 10.0),
                ("2026-01-04", 20.0),
            ])
            .with_aggregation(CalendarAgg::Sum)
    };
    let plain = plain_svg(
        vec![Plot::Calendar(build())],
        Layout::auto_from_plots(&[Plot::Calendar(build())]),
    );
    assert!(
        svg_has_non_grey_fill(&plain),
        "sanity: default colormap should produce colorful fills"
    );
    let svg = bw_svg(
        vec![Plot::Calendar(build())],
        Layout::auto_from_plots(&[Plot::Calendar(build())]),
    );
    common::write_test_output("test_outputs/bw_calendar.svg", svg.clone()).unwrap();
    assert!(
        !svg_has_non_grey_fill(&svg),
        "calendar day cells should use only grayscale fills in BW mode"
    );
}

#[test]
fn bw_calendar_missing_days_use_a_pattern_not_flat_grey() {
    // Flat grey for "no data" sits on the same white-to-black scale as a real
    // low value, so it's ambiguous which one a viewer is looking at — reported
    // by the user after visually reviewing the Group 6 calendar preview.
    use kuva::plot::calendar::CalendarPlot;
    let calendar = CalendarPlot::new().with_data(vec![("2026-06-15", 5.0)]);
    let plots = vec![Plot::Calendar(calendar)];
    let layout = Layout::auto_from_plots(&plots);
    let svg = bw_svg(plots, layout);
    let pattern_count = distinct_patterns_in_plot_body(&svg);
    assert!(
        pattern_count >= 1,
        "missing calendar days should use a hatch pattern, not a flat fill, got {pattern_count}"
    );
}

#[test]
fn bw_clustermap_forces_grayscale() {
    use kuva::plot::clustermap::Clustermap;
    let build = || {
        Clustermap::new()
            .with_data(vec![
                vec![1.0, 5.0, 9.0],
                vec![2.0, 6.0, 3.0],
                vec![8.0, 4.0, 7.0],
            ])
            .with_cluster_rows(false)
            .with_cluster_cols(false)
    };
    let plain = plain_svg(
        vec![Plot::Clustermap(build())],
        Layout::auto_from_plots(&[Plot::Clustermap(build())]),
    );
    assert!(
        svg_has_non_grey_fill(&plain),
        "sanity: default colormap should produce colorful fills"
    );
    let svg = bw_svg(
        vec![Plot::Clustermap(build())],
        Layout::auto_from_plots(&[Plot::Clustermap(build())]),
    );
    common::write_test_output("test_outputs/bw_clustermap.svg", svg.clone()).unwrap();
    assert!(
        !svg_has_non_grey_fill(&svg),
        "clustermap cells should use only grayscale fills in BW mode"
    );
}

#[test]
fn bw_surface3d_forces_grayscale() {
    use kuva::plot::surface3d::Surface3DPlot;
    use kuva::plot::ColorMap;
    let build = || {
        Surface3DPlot::new(vec![
            vec![1.0, 5.0, 9.0],
            vec![2.0, 6.0, 3.0],
            vec![8.0, 4.0, 7.0],
        ])
        .with_z_colormap(ColorMap::Viridis)
    };
    let plain = plain_svg(
        vec![Plot::Surface3D(build())],
        Layout::auto_from_plots(&[Plot::Surface3D(build())]),
    );
    assert!(
        svg_has_non_grey_fill(&plain),
        "sanity: default colormap should produce colorful fills"
    );
    let svg = bw_svg(
        vec![Plot::Surface3D(build())],
        Layout::auto_from_plots(&[Plot::Surface3D(build())]),
    );
    common::write_test_output("test_outputs/bw_surface3d.svg", svg.clone()).unwrap();
    assert!(
        !svg_has_non_grey_fill(&svg),
        "surface3d faces should use only grayscale fills in BW mode"
    );
}

#[test]
fn bw_contour_forces_grayscale() {
    use kuva::plot::contour::ContourPlot;
    let z = vec![
        vec![0.0, 1.0, 2.0, 3.0],
        vec![1.0, 2.0, 3.0, 4.0],
        vec![2.0, 3.0, 4.0, 5.0],
        vec![3.0, 4.0, 5.0, 6.0],
    ];
    let build = || {
        ContourPlot::new()
            .with_grid(
                z.clone(),
                vec![0.0, 1.0, 2.0, 3.0],
                vec![0.0, 1.0, 2.0, 3.0],
            )
            .with_filled()
    };
    let plain = plain_svg(
        vec![Plot::Contour(build())],
        Layout::auto_from_plots(&[Plot::Contour(build())]),
    );
    assert!(
        svg_has_non_grey_fill(&plain),
        "sanity: default colormap should produce colorful fills"
    );
    let svg = bw_svg(
        vec![Plot::Contour(build())],
        Layout::auto_from_plots(&[Plot::Contour(build())]),
    );
    common::write_test_output("test_outputs/bw_contour.svg", svg.clone()).unwrap();
    assert!(
        !svg_has_non_grey_fill(&svg),
        "filled contour bands should use only grayscale fills in BW mode"
    );
}

#[test]
fn bw_dotplot_forces_grayscale() {
    use kuva::plot::dotplot::DotPlot;
    let build = || {
        DotPlot::new().with_data(vec![
            ("A", "X", 5.0, 1.0),
            ("B", "X", 5.0, 5.0),
            ("A", "Y", 5.0, 9.0),
        ])
    };
    let plain = plain_svg(
        vec![Plot::DotPlot(build())],
        Layout::auto_from_plots(&[Plot::DotPlot(build())]),
    );
    assert!(
        svg_has_non_grey_fill(&plain),
        "sanity: default colormap should produce colorful fills"
    );
    let svg = bw_svg(
        vec![Plot::DotPlot(build())],
        Layout::auto_from_plots(&[Plot::DotPlot(build())]),
    );
    common::write_test_output("test_outputs/bw_dotplot.svg", svg.clone()).unwrap();
    assert!(
        !svg_has_non_grey_fill(&svg),
        "dot fills should use only grayscale colors in BW mode"
    );
}

#[test]
fn bw_quiver_forces_grayscale() {
    use kuva::plot::quiver::QuiverPlot;
    use kuva::plot::ColorMap;
    let build = || {
        QuiverPlot::new()
            .with_arrow(0.0, 0.0, 1.0, 1.0)
            .with_arrow(1.0, 1.0, 3.0, 3.0)
            .with_arrow(2.0, 2.0, 5.0, 0.0)
            .with_color_map(ColorMap::Viridis)
    };
    let plain = plain_svg(
        vec![Plot::Quiver(build())],
        Layout::auto_from_plots(&[Plot::Quiver(build())]),
    );
    assert!(
        svg_has_non_grey_fill(&plain),
        "sanity: default colormap should produce colorful fills"
    );
    let svg = bw_svg(
        vec![Plot::Quiver(build())],
        Layout::auto_from_plots(&[Plot::Quiver(build())]),
    );
    common::write_test_output("test_outputs/bw_quiver.svg", svg.clone()).unwrap();
    assert!(
        !svg_has_non_grey_fill(&svg),
        "quiver arrows should use only grayscale colors in BW mode"
    );
}

#[test]
fn bw_diceplot_continuous_tile_forces_grayscale() {
    // The categorical dot-legend mode was covered in Group 2
    // (bw_diceplot_categorical_dots_use_distinct_shapes); this covers the
    // continuous per-tile fill mode deferred to Group 6 at the time.
    use kuva::plot::diceplot::DicePlot;
    let build = || {
        DicePlot::new(4).with_points(vec![
            ("Row1", "Col1", vec![0, 1, 2, 3], Some(0.1), Some(3.0)),
            ("Row2", "Col1", vec![0, 1, 2, 3], Some(0.9), Some(3.0)),
        ])
    };
    let plain = plain_svg(
        vec![Plot::DicePlot(build())],
        Layout::auto_from_plots(&[Plot::DicePlot(build())]),
    );
    assert!(
        svg_has_non_grey_fill(&plain),
        "sanity: default colormap should produce colorful fills"
    );
    let svg = bw_svg(
        vec![Plot::DicePlot(build())],
        Layout::auto_from_plots(&[Plot::DicePlot(build())]),
    );
    common::write_test_output("test_outputs/bw_diceplot_continuous.svg", svg.clone()).unwrap();
    assert!(
        !svg_has_non_grey_fill(&svg),
        "continuous dice tiles should use only grayscale fills in BW mode"
    );
}

// ── Sanity checks ─────────────────────────────────────────────────────────────

#[test]
fn bw_color_mode_no_patterns() {
    let bar = BarPlot::new().with_bar("A", 3.2).with_bar("B", 4.7);
    let plots = vec![Plot::Bar(bar)];
    let layout = Layout::auto_from_plots(&plots);
    let scene = render_multiple(plots, layout);
    let svg = SvgBackend.render_scene(&scene);
    assert!(
        !svg.contains("kuva-fp-"),
        "Color mode should NOT emit pattern defs"
    );
}

#[test]
fn bw_layout_flag_propagates_to_computed() {
    use kuva::render::layout::ComputedLayout;
    let layout = Layout::new((0.0, 1.0), (0.0, 1.0)).with_bw_mode();
    let computed = ComputedLayout::from_layout(&layout);
    assert!(
        computed.bw_mode,
        "bw_mode should propagate from Layout to ComputedLayout"
    );
}