merman-render 0.5.0

Headless layout + SVG renderer for Mermaid (parity-focused; upstream SVG goldens).
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
use merman_core::{Engine, ParseOptions};
use merman_render::text::{TextMeasurer, VendoredFontMetricsTextMeasurer, WrapMode};
use merman_render::{LayoutOptions, layout_parsed};
use std::path::PathBuf;
use std::sync::Arc;

fn workspace_root() -> PathBuf {
    PathBuf::from(env!("CARGO_MANIFEST_DIR"))
        .join("..")
        .join("..")
}

fn approx_gt(a: f64, b: f64) -> bool {
    a > b + 1e-6
}

fn rect_from_cluster(c: &merman_render::model::LayoutCluster) -> (f64, f64, f64, f64) {
    let hw = c.width / 2.0;
    let hh = c.height / 2.0;
    (c.x - hw, c.y - hh, c.x + hw, c.y + hh)
}

fn rect_from_label(l: &merman_render::model::LayoutLabel) -> (f64, f64, f64, f64) {
    let hw = l.width / 2.0;
    let hh = l.height / 2.0;
    (l.x - hw, l.y - hh, l.x + hw, l.y + hh)
}

fn rect_contains(outer: (f64, f64, f64, f64), inner: (f64, f64, f64, f64), eps: f64) -> bool {
    let (omin_x, omin_y, omax_x, omax_y) = outer;
    let (imin_x, imin_y, imax_x, imax_y) = inner;
    imin_x + eps >= omin_x
        && imax_x <= omax_x + eps
        && imin_y + eps >= omin_y
        && imax_y <= omax_y + eps
}

fn rects_overlap(a: (f64, f64, f64, f64), b: (f64, f64, f64, f64), eps: f64) -> bool {
    let (amin_x, amin_y, amax_x, amax_y) = a;
    let (bmin_x, bmin_y, bmax_x, bmax_y) = b;
    let sep_x = amax_x <= bmin_x + eps || bmax_x <= amin_x + eps;
    let sep_y = amax_y <= bmin_y + eps || bmax_y <= amin_y + eps;
    !(sep_x || sep_y)
}

#[test]
fn flowchart_layout_produces_positions_and_routes() {
    let path = workspace_root()
        .join("fixtures")
        .join("flowchart")
        .join("basic.mmd");
    let text = std::fs::read_to_string(&path).expect("fixture");
    let engine = Engine::new();
    let parsed = futures::executor::block_on(engine.parse_diagram(&text, ParseOptions::default()))
        .expect("parse ok")
        .expect("diagram detected");

    let out = layout_parsed(&parsed, &LayoutOptions::default()).expect("layout ok");
    let merman_render::model::LayoutDiagram::FlowchartV2(layout) = out.layout else {
        panic!("expected FlowchartV2 layout");
    };

    assert_eq!(layout.nodes.len(), 4);
    assert_eq!(layout.edges.len(), 3);

    let mut by_id = std::collections::HashMap::new();
    for n in &layout.nodes {
        by_id.insert(n.id.as_str(), (n.x, n.y));
        assert!(n.width.is_finite() && n.width > 0.0);
        assert!(n.height.is_finite() && n.height > 0.0);
    }

    let (ax, ay) = by_id["A"];
    let (bx, by) = by_id["B"];
    let (_cx, cy) = by_id["C"];
    let (_dx, dy) = by_id["D"];

    assert!(approx_gt(by, ay), "B should be below A in TB direction");
    assert!(approx_gt(cy, by), "C should be below B in TB direction");
    assert!(approx_gt(dy, by), "D should be below B in TB direction");
    assert!(ax.is_finite() && bx.is_finite());

    for e in &layout.edges {
        assert!(
            e.points.len() >= 2,
            "edge {} should have at least two points",
            e.id
        );
        for p in &e.points {
            assert!(p.x.is_finite() && p.y.is_finite());
        }
    }

    // Mermaid's modern flowchart layout represents edge labels as label nodes. Ensure we emit
    // stable label placeholders for labeled edges.
    let labeled = layout.edges.iter().filter(|e| e.label.is_some()).count();
    assert!(labeled >= 2, "expected at least two labeled edges");
}

#[test]
fn flowchart_layout_respects_lr_direction() {
    let text = "flowchart LR\nA-->B\nB-->C\n";
    let engine = Engine::new();
    let parsed = futures::executor::block_on(engine.parse_diagram(text, ParseOptions::default()))
        .expect("parse ok")
        .expect("diagram detected");

    let out = layout_parsed(&parsed, &LayoutOptions::default()).expect("layout ok");
    let merman_render::model::LayoutDiagram::FlowchartV2(layout) = out.layout else {
        panic!("expected FlowchartV2 layout");
    };

    let mut by_id = std::collections::HashMap::new();
    for n in &layout.nodes {
        by_id.insert(n.id.as_str(), (n.x, n.y));
    }
    let (ax, _ay) = by_id["A"];
    let (bx, _by) = by_id["B"];
    let (cx, _cy) = by_id["C"];

    assert!(approx_gt(bx, ax), "B should be right of A in LR direction");
    assert!(approx_gt(cx, bx), "C should be right of B in LR direction");
}

#[test]
fn flowchart_layout_includes_clusters_with_title_placeholders() {
    let path = workspace_root()
        .join("fixtures")
        .join("flowchart")
        .join("upstream_subgraphs.mmd");
    let text = std::fs::read_to_string(&path).expect("fixture");

    let engine = Engine::new();
    let parsed = futures::executor::block_on(engine.parse_diagram(&text, ParseOptions::default()))
        .expect("parse ok")
        .expect("diagram detected");

    let out = layout_parsed(&parsed, &LayoutOptions::default()).expect("layout ok");
    let merman_render::model::LayoutDiagram::FlowchartV2(layout) = out.layout else {
        panic!("expected FlowchartV2 layout");
    };

    assert_eq!(layout.clusters.len(), 5);
    let ids = layout
        .clusters
        .iter()
        .map(|c| c.id.as_str())
        .collect::<Vec<_>>();
    assert_eq!(ids, vec!["A", "child", "id1", "parent", "subGraph2"]);

    for c in &layout.clusters {
        assert!(c.width.is_finite() && c.width > 0.0);
        assert!(c.height.is_finite() && c.height > 0.0);
        assert!(c.title_label.width.is_finite() && c.title_label.width >= 0.0);
        assert!(c.title_label.height.is_finite() && c.title_label.height >= 0.0);

        // Title placeholder should be horizontally centered relative to the cluster.
        assert!((c.title_label.x - c.x).abs() < 1e-6);
        // Title placeholder should be at or above the cluster center (towards the top).
        assert!(c.title_label.y <= c.y + 1e-6);
        // Cluster width should be large enough to fit the title placeholder.
        assert!(c.width + 1e-6 >= c.title_label.width);
    }

    let clusters_by_id = layout
        .clusters
        .iter()
        .map(|c| (c.id.as_str(), c))
        .collect::<std::collections::HashMap<_, _>>();

    // Default `inheritDir` is false; when a subgraph does not specify `dir`, Mermaid toggles
    // the layout direction for isolated clusters (TB -> LR).
    assert_eq!(clusters_by_id["A"].effective_dir, "LR");
    assert_eq!(clusters_by_id["id1"].effective_dir, "LR");
    assert_eq!(clusters_by_id["subGraph2"].effective_dir, "RL");
    assert_eq!(clusters_by_id["child"].effective_dir, "BT");

    fn rect_from_layout_node(n: &merman_render::model::LayoutNode) -> (f64, f64, f64, f64) {
        let hw = n.width / 2.0;
        let hh = n.height / 2.0;
        (n.x - hw, n.y - hh, n.x + hw, n.y + hh)
    }

    fn rect_from_layout_cluster(c: &merman_render::model::LayoutCluster) -> (f64, f64, f64, f64) {
        let hw = c.width / 2.0;
        let hh = c.height / 2.0;
        (c.x - hw, c.y - hh, c.x + hw, c.y + hh)
    }

    let nodes_by_id = layout
        .nodes
        .iter()
        .map(|n| (n.id.as_str(), n))
        .collect::<std::collections::HashMap<_, _>>();

    // Verify that cluster `dir` (or toggled direction) affects internal node layout when the
    // cluster has no external connections.
    {
        let a = nodes_by_id.get("a").expect("node a");
        let b = nodes_by_id.get("b").expect("node b");
        assert!(b.x > a.x, "cluster A should lay out a->b left-to-right");

        let c = nodes_by_id.get("c").expect("node c");
        let d = nodes_by_id.get("d").expect("node d");
        assert!(d.x > c.x, "cluster id1 should lay out c->d left-to-right");

        let e = nodes_by_id.get("e").expect("node e");
        let f = nodes_by_id.get("f").expect("node f");
        assert!(
            f.x < e.x,
            "cluster subGraph2 dir=RL should lay out e->f right-to-left"
        );

        let g = nodes_by_id.get("g").expect("node g");
        let h = nodes_by_id.get("h").expect("node h");
        assert!(
            h.y < g.y,
            "cluster child dir=BT should lay out g->h bottom-to-top"
        );
    }

    let subgraphs = out
        .semantic
        .get("subgraphs")
        .and_then(|v| v.as_array())
        .expect("semantic subgraphs");
    for sg in subgraphs {
        let id = sg.get("id").and_then(|v| v.as_str()).expect("subgraph id");
        let members = sg
            .get("nodes")
            .and_then(|v| v.as_array())
            .expect("subgraph nodes");
        let cluster = clusters_by_id.get(id).expect("cluster output");
        let (cmin_x, cmin_y, cmax_x, cmax_y) = rect_from_layout_cluster(cluster);

        for m in members {
            let mid = m.as_str().expect("member id");

            let (min_x, min_y, max_x, max_y) = if let Some(child_cluster) = clusters_by_id.get(mid)
            {
                rect_from_layout_cluster(child_cluster)
            } else if let Some(node) = nodes_by_id.get(mid) {
                rect_from_layout_node(node)
            } else {
                continue;
            };

            assert!(
                min_x + 1e-6 >= cmin_x && max_x <= cmax_x + 1e-6,
                "member {mid} should fit horizontally in cluster {id}"
            );
            assert!(
                min_y + 1e-6 >= cmin_y && max_y <= cmax_y + 1e-6,
                "member {mid} should fit vertically in cluster {id}"
            );
        }
    }

    // Root-level isolated subgraphs are rendered recursively by Mermaid and should not overlap
    // after applying subgraph `dir`/toggle behavior and cluster padding/title extents.
    let semantic_edges = out
        .semantic
        .get("edges")
        .and_then(|v| v.as_array())
        .expect("semantic edges");

    let mut members_by_id: std::collections::HashMap<String, Vec<String>> =
        std::collections::HashMap::new();
    let mut subgraph_ids: std::collections::HashSet<String> = std::collections::HashSet::new();
    for sg in subgraphs {
        let id = sg.get("id").and_then(|v| v.as_str()).expect("subgraph id");
        let members = sg
            .get("nodes")
            .and_then(|v| v.as_array())
            .expect("subgraph nodes")
            .iter()
            .filter_map(|v| v.as_str().map(|s| s.to_string()))
            .collect::<Vec<_>>();
        members_by_id.insert(id.to_string(), members);
        subgraph_ids.insert(id.to_string());
    }

    let mut child_subgraphs: std::collections::HashSet<String> = std::collections::HashSet::new();
    for members in members_by_id.values() {
        for m in members {
            if subgraph_ids.contains(m) {
                child_subgraphs.insert(m.clone());
            }
        }
    }

    fn collect_leaf_nodes(
        id: &str,
        subgraph_ids: &std::collections::HashSet<String>,
        members_by_id: &std::collections::HashMap<String, Vec<String>>,
        out: &mut std::collections::HashSet<String>,
        visiting: &mut std::collections::HashSet<String>,
    ) {
        if !visiting.insert(id.to_string()) {
            return;
        }
        let Some(members) = members_by_id.get(id) else {
            visiting.remove(id);
            return;
        };
        for m in members {
            if subgraph_ids.contains(m) {
                collect_leaf_nodes(m, subgraph_ids, members_by_id, out, visiting);
            } else {
                out.insert(m.clone());
            }
        }
        visiting.remove(id);
    }

    let mut root_isolated_cluster_ids: Vec<String> = Vec::new();
    for id in subgraph_ids.iter() {
        if child_subgraphs.contains(id) {
            continue;
        }
        let mut leaves: std::collections::HashSet<String> = std::collections::HashSet::new();
        let mut visiting: std::collections::HashSet<String> = std::collections::HashSet::new();
        collect_leaf_nodes(
            id,
            &subgraph_ids,
            &members_by_id,
            &mut leaves,
            &mut visiting,
        );
        if leaves.is_empty() {
            continue;
        }

        let mut has_external = false;
        for e in semantic_edges {
            let from = e.get("from").and_then(|v| v.as_str()).expect("edge from");
            let to = e.get("to").and_then(|v| v.as_str()).expect("edge to");
            let in_from = leaves.contains(from);
            let in_to = leaves.contains(to);
            if in_from ^ in_to {
                has_external = true;
                break;
            }
        }

        if !has_external {
            root_isolated_cluster_ids.push(id.clone());
        }
    }

    root_isolated_cluster_ids.sort();
    for i in 0..root_isolated_cluster_ids.len() {
        for j in (i + 1)..root_isolated_cluster_ids.len() {
            let a = &root_isolated_cluster_ids[i];
            let b = &root_isolated_cluster_ids[j];
            let ca = clusters_by_id.get(a.as_str()).expect("cluster output");
            let cb = clusters_by_id.get(b.as_str()).expect("cluster output");
            assert!(
                !rects_overlap(rect_from_cluster(ca), rect_from_cluster(cb), 1e-6),
                "expected clusters {a} and {b} not to overlap"
            );
        }
    }
}

#[test]
fn flowchart_cluster_exposes_mermaid_diff_and_offset_y() {
    // Base cluster width should come from the layout result (cluster bounds), not from any
    // title-driven widening. Measure it from an otherwise-identical graph with a short title.
    let short_text = "flowchart TB\nsubgraph A[\"`x`\"]\n  a\nend\n";
    let long_text = "flowchart TB\nsubgraph A[\"`This is a very very very very very very very long title that should wrap`\"]\n  a\nend\n";

    let engine = Engine::new();
    let parsed_short =
        futures::executor::block_on(engine.parse_diagram(short_text, ParseOptions::default()))
            .expect("parse ok")
            .expect("diagram detected");
    let out_short = layout_parsed(&parsed_short, &LayoutOptions::default()).expect("layout ok");
    let merman_render::model::LayoutDiagram::FlowchartV2(layout_short) = out_short.layout else {
        panic!("expected FlowchartV2 layout");
    };
    let base_cluster = layout_short
        .clusters
        .iter()
        .find(|c| c.id == "A")
        .expect("cluster A");
    let base_width = base_cluster.width;

    let parsed_long =
        futures::executor::block_on(engine.parse_diagram(long_text, ParseOptions::default()))
            .expect("parse ok")
            .expect("diagram detected");

    let out = layout_parsed(&parsed_long, &LayoutOptions::default()).expect("layout ok");
    let merman_render::model::LayoutDiagram::FlowchartV2(layout) = out.layout else {
        panic!("expected FlowchartV2 layout");
    };

    let cluster = layout
        .clusters
        .iter()
        .find(|c| c.id == "A")
        .expect("cluster A");

    // Mermaid FlowDB encodes subgraph nodes with `padding: 8`.
    let cluster_padding = cluster.padding;
    assert!((cluster_padding - 8.0).abs() < 1e-6);

    // `node.diff` is computed from the (layout) cluster node width and the measured title bbox.
    let title_w = cluster.title_label.width.max(1.0);

    let expected_diff = if base_width <= title_w {
        (title_w - base_width) / 2.0 - cluster_padding / 2.0
    } else {
        -cluster_padding / 2.0
    };
    let expected_offset_y = cluster.title_label.height - cluster_padding / 2.0;

    assert!((cluster.diff - expected_diff).abs() < 1e-6);
    assert!((cluster.offset_y - expected_offset_y).abs() < 1e-6);
}

#[test]
fn flowchart_recursive_cluster_title_bbox_feeds_parent_layout() {
    let text = std::fs::read_to_string(
        workspace_root()
            .join("fixtures")
            .join("flowchart")
            .join("stress_flowchart_subgraph_deep_nesting_title_padding_044.mmd"),
    )
    .expect("read fixture");

    let engine = Engine::new();
    let parsed = futures::executor::block_on(engine.parse_diagram(&text, ParseOptions::default()))
        .expect("parse ok")
        .expect("diagram detected");
    let out = layout_parsed(
        &parsed,
        &LayoutOptions {
            text_measurer: Arc::new(VendoredFontMetricsTextMeasurer::default()),
            ..Default::default()
        },
    )
    .expect("layout ok");
    let merman_render::model::LayoutDiagram::FlowchartV2(layout) = out.layout else {
        panic!("expected FlowchartV2 layout");
    };

    let cluster = |id: &str| {
        layout
            .clusters
            .iter()
            .find(|c| c.id == id)
            .unwrap_or_else(|| panic!("cluster {id}"))
    };
    let node = |id: &str| {
        layout
            .nodes
            .iter()
            .find(|n| n.id == id)
            .unwrap_or_else(|| panic!("node {id}"))
    };

    let c1 = cluster("c1");
    let c2 = cluster("c2");
    let c1a = node("c1a");

    // Mermaid measures the rendered child `<g class="root">` with the title-widened cluster rect
    // before laying out the parent graph. If we only feed the pre-title compound width back to the
    // parent, c1 collapses by about 59px and c1a lands too close to c2.
    assert!(
        c2.width >= c2.title_label.width + c2.padding - 1e-6,
        "c2 should expose the rendered title-widened cluster width"
    );
    assert!(
        c1.width >= c2.width + c1a.width + 100.0,
        "parent cluster width should reflect the rendered child clusterNode bbox"
    );
}

#[test]
fn flowchart_cluster_title_margins_increase_cluster_height() {
    let text_no_margin = "flowchart TD\nsubgraph A\na-->b\nend\n";
    let text_with_margin = "%%{init: {\"flowchart\": {\"subGraphTitleMargin\": {\"top\": 10, \"bottom\": 5}}}}%%\nflowchart TD\nsubgraph A\na-->b\nend\n";

    let engine = Engine::new();

    let parsed_no_margin =
        futures::executor::block_on(engine.parse_diagram(text_no_margin, ParseOptions::default()))
            .expect("parse ok")
            .expect("diagram detected");
    let out_no_margin =
        layout_parsed(&parsed_no_margin, &LayoutOptions::default()).expect("layout");
    let merman_render::model::LayoutDiagram::FlowchartV2(layout_no_margin) = out_no_margin.layout
    else {
        panic!("expected FlowchartV2 layout");
    };
    let h0 = layout_no_margin
        .clusters
        .iter()
        .find(|c| c.id == "A")
        .expect("cluster A")
        .height;

    let parsed_with_margin = futures::executor::block_on(
        engine.parse_diagram(text_with_margin, ParseOptions::default()),
    )
    .expect("parse ok")
    .expect("diagram detected");
    let out_with_margin =
        layout_parsed(&parsed_with_margin, &LayoutOptions::default()).expect("layout");
    let merman_render::model::LayoutDiagram::FlowchartV2(layout_with_margin) =
        out_with_margin.layout
    else {
        panic!("expected FlowchartV2 layout");
    };
    let c = layout_with_margin
        .clusters
        .iter()
        .find(|c| c.id == "A")
        .expect("cluster A");

    assert!((c.height - h0 - 15.0).abs() < 1e-6);
    assert!((c.title_margin_top - 10.0).abs() < 1e-6);
    assert!((c.title_margin_bottom - 5.0).abs() < 1e-6);
}

#[test]
fn flowchart_edge_label_is_included_in_subgraph_bounds() {
    // Ensure edge labels participate in cluster bounding box calculation. Without including the
    // label node (used internally for layout), a very wide label in TB direction can extend
    // beyond the union of the member node rectangles.
    let text = "flowchart TB\nsubgraph A\n  direction TB\n  a -->|this is a very very very very very long label| b\nend\n";

    let engine = Engine::new();
    let parsed = futures::executor::block_on(engine.parse_diagram(text, ParseOptions::default()))
        .expect("parse ok")
        .expect("diagram detected");

    let out = layout_parsed(&parsed, &LayoutOptions::default()).expect("layout ok");
    let merman_render::model::LayoutDiagram::FlowchartV2(layout) = out.layout else {
        panic!("expected FlowchartV2 layout");
    };

    let cluster = layout
        .clusters
        .iter()
        .find(|c| c.id == "A")
        .expect("cluster A");

    let edge = layout
        .edges
        .iter()
        .find(|e| e.from == "a" && e.to == "b")
        .expect("edge a->b");
    let label = edge.label.as_ref().expect("edge label");

    let c_hw = cluster.width / 2.0;
    let c_hh = cluster.height / 2.0;
    let cmin_x = cluster.x - c_hw;
    let cmax_x = cluster.x + c_hw;
    let cmin_y = cluster.y - c_hh;
    let cmax_y = cluster.y + c_hh;

    let l_hw = label.width / 2.0;
    let l_hh = label.height / 2.0;
    let lmin_x = label.x - l_hw;
    let lmax_x = label.x + l_hw;
    let lmin_y = label.y - l_hh;
    let lmax_y = label.y + l_hh;

    assert!(
        lmin_x + 1e-6 >= cmin_x && lmax_x <= cmax_x + 1e-6,
        "edge label should fit horizontally in cluster A (cluster=[{cmin_x:.3},{cmax_x:.3}] label=[{lmin_x:.3},{lmax_x:.3}])"
    );
    assert!(
        lmin_y + 1e-6 >= cmin_y && lmax_y <= cmax_y + 1e-6,
        "edge label should fit vertically in cluster A (cluster=[{cmin_y:.3},{cmax_y:.3}] label=[{lmin_y:.3},{lmax_y:.3}])"
    );
}

#[test]
fn flowchart_subgraph_dir_is_not_applied_when_cluster_has_external_edges() {
    // Mermaid only applies subgraph `dir` as a recursive layout when the cluster is isolated
    // (no external connections). When there is an external edge, internal layout follows the
    // diagram direction.
    let text = "flowchart TB\nsubgraph A\n  direction LR\n  a --> b\nend\na --> c\n";

    let engine = Engine::new();
    let parsed = futures::executor::block_on(engine.parse_diagram(text, ParseOptions::default()))
        .expect("parse ok")
        .expect("diagram detected");

    let out = layout_parsed(&parsed, &LayoutOptions::default()).expect("layout ok");
    let merman_render::model::LayoutDiagram::FlowchartV2(layout) = out.layout else {
        panic!("expected FlowchartV2 layout");
    };

    let nodes_by_id = layout
        .nodes
        .iter()
        .map(|n| (n.id.as_str(), (n.x, n.y)))
        .collect::<std::collections::HashMap<_, _>>();

    let (_ax, ay) = nodes_by_id["a"];
    let (_bx, by) = nodes_by_id["b"];
    assert!(
        by > ay + 5.0,
        "node b should be below a (TB) when cluster A has external edges"
    );
}

#[test]
fn flowchart_edge_to_ancestor_cluster_keeps_ancestor_non_recursive() {
    let text = std::fs::read_to_string(
        workspace_root()
            .join("fixtures")
            .join("flowchart")
            .join("stress_flowchart_subgraph_title_margins_extreme_nested_030.mmd"),
    )
    .expect("read fixture");

    let engine = Engine::new();
    let parsed = futures::executor::block_on(engine.parse_diagram(&text, ParseOptions::default()))
        .expect("parse ok")
        .expect("diagram detected");
    let out = layout_parsed(
        &parsed,
        &LayoutOptions {
            text_measurer: Arc::new(VendoredFontMetricsTextMeasurer::default()),
            ..Default::default()
        },
    )
    .expect("layout ok");
    let merman_render::model::LayoutDiagram::FlowchartV2(layout) = out.layout else {
        panic!("expected FlowchartV2 layout");
    };

    let nodes_by_id = layout
        .nodes
        .iter()
        .map(|n| (n.id.as_str(), (n.x, n.y)))
        .collect::<std::collections::HashMap<_, _>>();
    let clusters_by_id = layout
        .clusters
        .iter()
        .map(|c| (c.id.as_str(), c))
        .collect::<std::collections::HashMap<_, _>>();

    let (ax, ay) = nodes_by_id["a"];
    let (bx, by) = nodes_by_id["b"];
    let (cx, cy) = nodes_by_id["c"];
    assert!(
        bx > ax + 60.0 && cx > bx + 60.0,
        "ancestor cluster edge should not make Outer use its TB direction as a recursive layout"
    );
    assert!(
        (cy - ay).abs() < 80.0 && (by - ay).abs() < 80.0,
        "nodes should stay in the root LR layout band"
    );

    let inner = clusters_by_id["Inner"];
    let outer = clusters_by_id["Outer"];
    assert!(
        inner.width > inner.height && outer.width > outer.height,
        "non-recursive nested clusters should keep the upstream wide LR footprint"
    );
}

#[test]
fn flowchart_nested_subgraph_labeled_edge_label_is_inside_inner_cluster() {
    let text = "flowchart TB\nsubgraph Outer\n  subgraph Inner\n    a -->|this is a very very long label| b\n  end\nend\n";

    let engine = Engine::new();
    let parsed = futures::executor::block_on(engine.parse_diagram(text, ParseOptions::default()))
        .expect("parse ok")
        .expect("diagram detected");

    let out = layout_parsed(&parsed, &LayoutOptions::default()).expect("layout ok");
    let merman_render::model::LayoutDiagram::FlowchartV2(layout) = out.layout else {
        panic!("expected FlowchartV2 layout");
    };

    let clusters_by_id = layout
        .clusters
        .iter()
        .map(|c| (c.id.as_str(), c))
        .collect::<std::collections::HashMap<_, _>>();
    let inner = clusters_by_id.get("Inner").expect("cluster Inner");
    let outer = clusters_by_id.get("Outer").expect("cluster Outer");

    let edge = layout
        .edges
        .iter()
        .find(|e| e.from == "a" && e.to == "b")
        .expect("edge a->b");
    let label = edge.label.as_ref().expect("edge label");

    let label_rect = rect_from_label(label);
    assert!(
        rect_contains(rect_from_cluster(inner), label_rect, 1e-6),
        "edge label should fit in Inner cluster"
    );
    assert!(
        rect_contains(rect_from_cluster(outer), label_rect, 1e-6),
        "edge label should fit in Outer cluster"
    );
}

#[test]
fn flowchart_cross_subgraph_labeled_edge_label_belongs_to_outer_cluster() {
    // The edge spans two different subgraphs; the label node should be assigned to the lowest
    // common compound parent (the outer subgraph), so only the outer cluster must include it.
    let text = "flowchart TB\nsubgraph Outer\n  subgraph Left\n    a\n  end\n  subgraph Right\n    b\n  end\n  a -->|this is a very very very long cross-subgraph label| b\nend\n";

    let engine = Engine::new();
    let parsed = futures::executor::block_on(engine.parse_diagram(text, ParseOptions::default()))
        .expect("parse ok")
        .expect("diagram detected");

    let out = layout_parsed(&parsed, &LayoutOptions::default()).expect("layout ok");
    let merman_render::model::LayoutDiagram::FlowchartV2(layout) = out.layout else {
        panic!("expected FlowchartV2 layout");
    };

    let clusters_by_id = layout
        .clusters
        .iter()
        .map(|c| (c.id.as_str(), c))
        .collect::<std::collections::HashMap<_, _>>();
    let outer = clusters_by_id.get("Outer").expect("cluster Outer");
    let left = clusters_by_id.get("Left").expect("cluster Left");
    let right = clusters_by_id.get("Right").expect("cluster Right");

    let edge = layout
        .edges
        .iter()
        .find(|e| e.from == "a" && e.to == "b")
        .expect("edge a->b");
    let label = edge.label.as_ref().expect("edge label");

    let label_rect = rect_from_label(label);
    assert!(
        rect_contains(rect_from_cluster(outer), label_rect, 1e-6),
        "cross-subgraph edge label should fit in Outer cluster"
    );

    // If the label node were incorrectly assigned to `Left`/`Right`, those cluster bounds would
    // expand to include the (very wide) label. Instead, only the LCA (`Outer`) should include it.
    assert!(
        left.width < label.width * 0.8,
        "Left cluster should not expand to include cross-subgraph label"
    );
    assert!(
        right.width < label.width * 0.8,
        "Right cluster should not expand to include cross-subgraph label"
    );
}

#[test]
fn flowchart_html_multiline_edge_label_has_multiple_lines() {
    // The deterministic measurer normalizes `<br/>` into `\\n`, so multiline labels should get
    // larger height than a single-line label.
    let text = "flowchart TB\nA -->|line1<br/>line2| B\n";

    let engine = Engine::new();
    let parsed = futures::executor::block_on(engine.parse_diagram(text, ParseOptions::default()))
        .expect("parse ok")
        .expect("diagram detected");

    let out = layout_parsed(&parsed, &LayoutOptions::default()).expect("layout ok");
    let merman_render::model::LayoutDiagram::FlowchartV2(layout) = out.layout else {
        panic!("expected FlowchartV2 layout");
    };

    let edge = layout
        .edges
        .iter()
        .find(|e| e.from == "A" && e.to == "B")
        .expect("edge A->B");
    let label = edge.label.as_ref().expect("edge label");

    let measurer = merman_render::text::DeterministicTextMeasurer::default();
    let one = measurer.measure_wrapped(
        "line1",
        &merman_render::text::TextStyle::default(),
        Some(200.0),
        WrapMode::HtmlLike,
    );
    assert!(
        label.height > one.height + 1e-6,
        "expected multiline label to have larger height"
    );
}

#[test]
fn flowchart_multigraph_edges_keep_distinct_routes_and_labels() {
    // Mermaid flowcharts are multigraphs; ensure we can lay out multiple edges between the same
    // endpoints without collapsing their routes/labels.
    let text = "flowchart TB\nA -->|l1| B\nA -->|l2| B\nA --> B\n";

    let engine = Engine::new();
    let parsed = futures::executor::block_on(engine.parse_diagram(text, ParseOptions::default()))
        .expect("parse ok")
        .expect("diagram detected");

    let out = layout_parsed(&parsed, &LayoutOptions::default()).expect("layout ok");
    let merman_render::model::LayoutDiagram::FlowchartV2(layout) = out.layout else {
        panic!("expected FlowchartV2 layout");
    };

    let edges = layout
        .edges
        .iter()
        .filter(|e| e.from == "A" && e.to == "B")
        .collect::<Vec<_>>();
    assert_eq!(edges.len(), 3, "expected three A->B edges");

    let labeled = edges.iter().filter(|e| e.label.is_some()).count();
    assert_eq!(labeled, 2, "expected two labeled edges");

    for e in edges {
        assert!(e.points.len() >= 2);
    }
}

#[test]
fn flowchart_isolated_cluster_with_multiple_labeled_edges_contains_all_labels() {
    // When a cluster is isolated, we apply recursive layout (dir/toggle) and should still include
    // all internal edge labels in the cluster bounds.
    let text = "flowchart TB\nsubgraph A\n  direction TB\n  a -->|label one that is quite wide| b\n  b -->|another wide label for coverage| c\nend\n";

    let engine = Engine::new();
    let parsed = futures::executor::block_on(engine.parse_diagram(text, ParseOptions::default()))
        .expect("parse ok")
        .expect("diagram detected");

    let out = layout_parsed(&parsed, &LayoutOptions::default()).expect("layout ok");
    let merman_render::model::LayoutDiagram::FlowchartV2(layout) = out.layout else {
        panic!("expected FlowchartV2 layout");
    };

    let cluster = layout
        .clusters
        .iter()
        .find(|c| c.id == "A")
        .expect("cluster A");
    let cluster_rect = rect_from_cluster(cluster);

    let internal_labeled_edges = layout
        .edges
        .iter()
        .filter(|e| e.label.is_some())
        .collect::<Vec<_>>();
    assert_eq!(
        internal_labeled_edges.len(),
        2,
        "expected two labeled edges in cluster"
    );

    for e in internal_labeled_edges {
        let label = e.label.as_ref().expect("label");
        assert!(
            rect_contains(cluster_rect, rect_from_label(label), 1e-6),
            "edge {} label should fit in cluster A",
            e.id
        );
    }
}

#[test]
fn flowchart_various_edge_styles_do_not_break_layout() {
    // The renderer is headless; edge styling should not affect layout stability.
    // This test mainly ensures we don't crash and we always emit routed points.
    let text = "flowchart TB\nA --> B\nA --- C\nA -.-> D\nA ==> E\n";

    let engine = Engine::new();
    let parsed = futures::executor::block_on(engine.parse_diagram(text, ParseOptions::default()))
        .expect("parse ok")
        .expect("diagram detected");

    let out = layout_parsed(&parsed, &LayoutOptions::default()).expect("layout ok");
    let merman_render::model::LayoutDiagram::FlowchartV2(layout) = out.layout else {
        panic!("expected FlowchartV2 layout");
    };

    assert!(layout.nodes.len() >= 5);
    assert_eq!(layout.edges.len(), 4);
    for e in &layout.edges {
        assert!(!e.points.is_empty(), "edge {} should have points", e.id);
    }
}

#[test]
fn flowchart_node_shape_dimensions_follow_mermaid_rules() {
    // Verify key flowchart shapes follow Mermaid `@11.12.2` sizing rules (headless approximation).
    // This mainly protects us from regressions when refactoring shape sizing.
    let text = r#"flowchart TB
A[Label]
B(Label)
C((Label))
D(((Label)))
E{Label}
F{{Label}}
G>Label]
H([Label])
I[(Label)]
J[[Label]]
K[/Label/]
L[\Label\]
M[/Label\]
N[\Label/]
O(-Label-)
"#;

    let engine = Engine::new();
    let parsed = futures::executor::block_on(engine.parse_diagram(text, ParseOptions::default()))
        .expect("parse ok")
        .expect("diagram detected");

    let out = layout_parsed(&parsed, &LayoutOptions::default()).expect("layout ok");
    let merman_render::model::LayoutDiagram::FlowchartV2(layout) = out.layout else {
        panic!("expected FlowchartV2 layout");
    };

    let nodes_by_id = layout
        .nodes
        .iter()
        .map(|n| (n.id.as_str(), n))
        .collect::<std::collections::HashMap<_, _>>();

    let measurer = merman_render::text::DeterministicTextMeasurer::default();
    let metrics = measurer.measure_wrapped(
        "Label",
        &merman_render::text::TextStyle::default(),
        Some(200.0),
        WrapMode::HtmlLike,
    );
    let p = 15.0;
    let tw = metrics.width;
    let th = metrics.height;

    fn assert_close(actual: f64, expected: f64, name: &str) {
        let eps = 1e-6;
        assert!(
            (actual - expected).abs() <= eps,
            "{name}: expected {expected}, got {actual}"
        );
    }

    // squareRect
    {
        let n = nodes_by_id["A"];
        assert_close(n.width, tw + 4.0 * p, "squareRect width");
        assert_close(n.height, th + 2.0 * p, "squareRect height");
    }

    // roundedRect
    {
        let n = nodes_by_id["B"];
        assert_close(n.width, tw + 2.0 * p, "roundedRect width");
        assert_close(n.height, th + 2.0 * p, "roundedRect height");
    }

    // circle / doublecircle
    {
        let n = nodes_by_id["C"];
        assert_close(n.width, tw + p, "circle width");
        assert_close(n.height, tw + p, "circle height");

        let n = nodes_by_id["D"];
        assert_close(n.width, tw + p + 10.0, "doublecircle width");
        assert_close(n.height, tw + p + 10.0, "doublecircle height");
    }

    // diamond/question
    {
        let n = nodes_by_id["E"];
        let s = (tw + p) + (th + p);
        assert_close(n.width, s, "diamond width");
        assert_close(n.height, s, "diamond height");
    }

    // hexagon
    {
        let n = nodes_by_id["F"];
        let w0 = tw + 2.5 * p;
        // Mermaid uses `updateNodeBounds(...).getBBox()` to set the final node width/height that
        // Dagre uses for layout. For hexagon nodes, Chromium rounds the roughjs path bbox to an
        // f32 grid, so we mirror that to match strict SVG parity `data-points`.
        let expected_w = (w0 * (7.0 / 6.0)) as f32 as f64;
        let expected_h = (th + p) as f32 as f64;
        assert_close(n.width, expected_w, "hexagon width");
        assert_close(n.height, expected_h, "hexagon height");
    }

    // odd (`rect_left_inv_arrow`)
    {
        let n = nodes_by_id["G"];
        let w = tw + p;
        let h = th + p;
        assert_close(n.width, w + h / 4.0, "odd width");
        assert_close(n.height, h, "odd height");
    }

    // stadium
    {
        let n = nodes_by_id["H"];
        let h = th + p;
        let w = tw + h / 4.0 + p;

        // Flowchart-v2 stadium nodes are rendered via a roughjs path built from sampled arc points.
        // Mermaid runs `updateNodeBounds(getBBox)` on that path and feeds the resulting bbox width
        // into Dagre layout. Because the arc sampling (50 points over 180deg) does not include the
        // exact extrema, the bbox is slightly narrower than `w`.
        let radius = h / 2.0;
        let mut min_x = f64::INFINITY;
        let mut max_x = f64::NEG_INFINITY;
        let mut include_x = |x: f64| {
            min_x = min_x.min(x);
            max_x = max_x.max(x);
        };
        include_x(-w / 2.0 + radius);
        include_x(w / 2.0 - radius);
        // `generateCirclePoints(...)` returns negated coordinates.
        let step = std::f64::consts::PI / (50_f64 - 1.0); // 180deg / (n-1)
        for i in 0..50 {
            let angle = (std::f64::consts::FRAC_PI_2) + (i as f64) * step; // 90deg..270deg
            let x = (-w / 2.0 + radius) + radius * angle.cos();
            include_x(-x);
        }
        for i in 0..50 {
            let angle = (std::f64::consts::FRAC_PI_2 * 3.0) + (i as f64) * step; // 270deg..450deg
            let x = (w / 2.0 - radius) + radius * angle.cos();
            include_x(-x);
        }
        let expected_w = (max_x - min_x).max(0.0);

        assert_close(n.width, expected_w, "stadium width");
        assert_close(n.height, h, "stadium height");
    }

    // cylinder
    {
        let n = nodes_by_id["I"];
        let w = tw + p;
        let rx = w / 2.0;
        let ry = rx / (2.5 + w / 50.0);
        let expected_h = {
            let h = th + p + 3.0 * ry;
            let h_f32 = h as f32;
            if h_f32.is_finite() && h_f32.is_sign_positive() {
                let bits = h_f32.to_bits();
                if bits < u32::MAX {
                    f32::from_bits(bits + 1) as f64
                } else {
                    h
                }
            } else {
                h
            }
        };
        assert_close(n.width, w, "cylinder width");
        assert_close(n.height, expected_h, "cylinder height");
    }

    // subroutine
    {
        let n = nodes_by_id["J"];
        assert_close(n.width, tw + p + 16.0, "subroutine width");
        assert_close(n.height, th + p, "subroutine height");
    }

    // lean right/left
    {
        let n = nodes_by_id["K"];
        let w = tw + p;
        let h = th + p;
        assert_close(n.width, w + h, "lean_right width");
        assert_close(n.height, h, "lean_right height");

        let n = nodes_by_id["L"];
        assert_close(n.width, w + h, "lean_left width");
        assert_close(n.height, h, "lean_left height");
    }

    // trapezoid / inv_trapezoid
    {
        let n = nodes_by_id["M"];
        let w = tw + p;
        let h = th + p;
        assert_close(n.width, w + h, "trapezoid width");
        assert_close(n.height, h, "trapezoid height");

        let n = nodes_by_id["N"];
        let w = tw + 2.0 * p;
        let h = th + 2.0 * p;
        assert_close(n.width, w + h, "inv_trapezoid width");
        assert_close(n.height, h, "inv_trapezoid height");
    }

    // ellipse (broken upstream, but keep stable headless sizing)
    {
        let n = nodes_by_id["O"];
        assert_close(n.width, tw + 2.0 * p, "ellipse width");
        assert_close(n.height, th + 2.0 * p, "ellipse height");
    }
}

#[test]
fn flowchart_anchor_shape_ignores_label_for_layout() {
    let text = "flowchart TB\nA@{ shape: anchor, label: 'Ignored by Mermaid' }\n";

    let engine = Engine::new();
    let parsed = futures::executor::block_on(engine.parse_diagram(text, ParseOptions::default()))
        .expect("parse ok")
        .expect("diagram detected");

    let out = layout_parsed(&parsed, &LayoutOptions::default()).expect("layout ok");
    let merman_render::model::LayoutDiagram::FlowchartV2(layout) = out.layout else {
        panic!("expected FlowchartV2 layout");
    };

    let node = layout
        .nodes
        .iter()
        .find(|n| n.id == "A")
        .expect("anchor node");
    assert!((node.width - 2.001_899_003_982_544).abs() <= 1e-9);
    assert!((node.height - 2.0).abs() <= 1e-9);
}

#[test]
fn flowchart_wrapping_width_increases_height_for_long_labels() {
    let text = "%%{init: {\"flowchart\": {\"wrappingWidth\": 60}}}%%\nflowchart TB\nA[This is a long label that should wrap]\n";

    let engine = Engine::new();
    let parsed = futures::executor::block_on(engine.parse_diagram(text, ParseOptions::default()))
        .expect("parse ok")
        .expect("diagram detected");

    let out = layout_parsed(&parsed, &LayoutOptions::default()).expect("layout ok");
    let merman_render::model::LayoutDiagram::FlowchartV2(layout) = out.layout else {
        panic!("expected FlowchartV2 layout");
    };

    let a = layout.nodes.iter().find(|n| n.id == "A").expect("node A");

    let measurer = merman_render::text::DeterministicTextMeasurer::default();
    let style = merman_render::text::TextStyle::default();
    let single = measurer.measure_wrapped(
        "This is a long label that should wrap",
        &style,
        None,
        WrapMode::HtmlLike,
    );

    // With wrapping, the node should become taller than the single-line size would indicate.
    assert!(
        a.height > single.height + 1e-6,
        "expected wrapped label to increase node height"
    );

    // Node width should be constrained by wrappingWidth plus the shape's padding rule (squareRect).
    let p = 15.0;
    assert!(
        a.width <= 60.0 + 4.0 * p + 1e-6,
        "expected wrapped label to constrain node width"
    );
}

#[test]
fn flowchart_htmllabels_long_word_is_clamped_but_not_wrapped() {
    // Mermaid HTML labels use `white-space: nowrap` initially and do not split long words; layout
    // width is constrained by `max-width` but height should not increase.
    let text = "%%{init: {\"flowchart\": {\"wrappingWidth\": 60, \"htmlLabels\": true}}}%%\nflowchart TB\nA[Supercalifragilisticexpialidocious]\n";

    let engine = Engine::new();
    let parsed = futures::executor::block_on(engine.parse_diagram(text, ParseOptions::default()))
        .expect("parse ok")
        .expect("diagram detected");

    let out = layout_parsed(&parsed, &LayoutOptions::default()).expect("layout ok");
    let merman_render::model::LayoutDiagram::FlowchartV2(layout) = out.layout else {
        panic!("expected FlowchartV2 layout");
    };

    let a = layout.nodes.iter().find(|n| n.id == "A").expect("node A");

    let measurer = merman_render::text::DeterministicTextMeasurer::default();
    let style = merman_render::text::TextStyle::default();
    let single = measurer.measure_wrapped(
        "Supercalifragilisticexpialidocious",
        &style,
        None,
        WrapMode::HtmlLike,
    );

    // Height should remain single-line in HTML mode (no long-word splitting).
    assert!(
        (a.height - (single.height + 2.0 * 15.0)).abs() < 1e-6,
        "expected long word to remain single-line in HTML mode"
    );

    // Width should be clamped to wrappingWidth plus squareRect padding rule.
    let p = 15.0;
    assert!(
        a.width <= 60.0 + 4.0 * p + 1e-6,
        "expected HTML mode to clamp width"
    );
}

#[test]
fn flowchart_svglike_long_word_is_wrapped_into_multiple_lines() {
    // In SVG-like mode (`htmlLabels=false`), Mermaid's text wrapping logic can split long words to
    // satisfy the width constraint, increasing height.
    let text = "%%{init: {\"flowchart\": {\"wrappingWidth\": 60, \"htmlLabels\": false}}}%%\nflowchart TB\nA[Supercalifragilisticexpialidocious]\n";

    let engine = Engine::new();
    let parsed = futures::executor::block_on(engine.parse_diagram(text, ParseOptions::default()))
        .expect("parse ok")
        .expect("diagram detected");

    let out = layout_parsed(&parsed, &LayoutOptions::default()).expect("layout ok");
    let merman_render::model::LayoutDiagram::FlowchartV2(layout) = out.layout else {
        panic!("expected FlowchartV2 layout");
    };

    let a = layout.nodes.iter().find(|n| n.id == "A").expect("node A");

    let measurer = merman_render::text::DeterministicTextMeasurer::default();
    let style = merman_render::text::TextStyle::default();
    let single = measurer.measure_wrapped(
        "Supercalifragilisticexpialidocious",
        &style,
        None,
        WrapMode::SvgLike,
    );

    // Height should increase vs. the single-line size.
    assert!(
        a.height > single.height + 2.0 * 15.0 + 1e-6,
        "expected long word to wrap and increase height in SVG-like mode"
    );

    // Width should still respect wrappingWidth via wrapping.
    let p = 15.0;
    assert!(
        a.width <= 60.0 + 4.0 * p + 1e-6,
        "expected SVG-like mode to constrain width via wrapping"
    );
}

#[test]
fn flowchart_subgraph_title_uses_wrapping_placeholder_metrics() {
    let title = "This is a very long subgraph title that should wrap across multiple lines for layout parity";
    // Subgraph titles only wrap when the label type is `markdown` (Mermaid uses `createText(...)`
    // with the default width=200).
    let text = format!("flowchart TB\nsubgraph A[\"`{title}`\"]\n  a\nend\n");

    let engine = Engine::new();
    let parsed = futures::executor::block_on(engine.parse_diagram(&text, ParseOptions::default()))
        .expect("parse ok")
        .expect("diagram detected");

    let out = layout_parsed(&parsed, &LayoutOptions::default()).expect("layout ok");
    let merman_render::model::LayoutDiagram::FlowchartV2(layout) = out.layout else {
        panic!("expected FlowchartV2 layout");
    };

    let cluster = layout
        .clusters
        .iter()
        .find(|c| c.id == "A")
        .expect("cluster A");

    let measurer = merman_render::text::DeterministicTextMeasurer::default();
    let style = merman_render::text::TextStyle {
        font_family: Some("\"trebuchet ms\", verdana, arial, sans-serif".to_string()),
        font_size: 16.0,
        font_weight: None,
    };
    let expected = merman_render::text::measure_markdown_with_flowchart_bold_deltas(
        &measurer,
        title,
        &style,
        Some(200.0),
        WrapMode::HtmlLike,
    );

    assert!((cluster.title_label.width - expected.width).abs() < 1e-6);
    assert!((cluster.title_label.height - expected.height).abs() < 1e-6);
    assert!(
        cluster.height >= cluster.title_label.height,
        "cluster should be at least as tall as its title placeholder"
    );
}

#[test]
fn flowchart_subgraph_title_wraps_long_word_in_svglike_mode() {
    let title = "Supercalifragilisticexpialidocious";
    let text = format!(
        "%%{{init: {{\"htmlLabels\": false, \"flowchart\": {{\"htmlLabels\": false}}}}}}%%\nflowchart TB\nsubgraph A[\"`{title}`\"]\n  a\nend\n"
    );

    let engine = Engine::new();
    let parsed = futures::executor::block_on(engine.parse_diagram(&text, ParseOptions::default()))
        .expect("parse ok")
        .expect("diagram detected");

    let out = layout_parsed(&parsed, &LayoutOptions::default()).expect("layout ok");
    let merman_render::model::LayoutDiagram::FlowchartV2(layout) = out.layout else {
        panic!("expected FlowchartV2 layout");
    };

    let cluster = layout
        .clusters
        .iter()
        .find(|c| c.id == "A")
        .expect("cluster A");

    let measurer = merman_render::text::DeterministicTextMeasurer::default();
    let style = merman_render::text::TextStyle {
        font_family: Some("\"trebuchet ms\", verdana, arial, sans-serif".to_string()),
        font_size: 16.0,
        font_weight: None,
    };
    let single = measurer.measure_wrapped(title, &style, None, WrapMode::SvgLike);
    let wrapped = measurer.measure_wrapped(title, &style, Some(200.0), WrapMode::SvgLike);

    assert!(
        wrapped.height > single.height + 1e-6,
        "expected SVG-like mode to wrap long-word title"
    );
    assert!((cluster.title_label.height - wrapped.height).abs() < 1e-6);
}