mmdflux 2.5.0

Render Mermaid diagrams as Unicode text, ASCII, SVG, and MMDS JSON.
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
//! Flowchart-specific validation warnings.
//!
//! Produces `ParseDiagnostic` warnings for unsupported keywords,
//! missing subgraph `end` keywords, and strict-mode parse failures.

use crate::diagrams::flowchart::compiler::{IdCollision, collect_id_collisions};
use crate::errors::ParseDiagnostic;
use crate::graph::style::{
    parse_classdef_statement, parse_linkstyle_statement, parse_node_style_statement,
};
use crate::mermaid::{
    ParseOptions, parse_flowchart, parse_flowchart_with_options, strip_theme_only_compat_syntax,
};

const STRICT_PARSE_WARNING_PREFIX: &str = "Strict parsing would reject this input:";

const UNSUPPORTED_KEYWORDS: &[(&str, &str)] = &[(
    "click ",
    "click statements are not applicable in text/ASCII output",
)];

/// Collect all flowchart-specific validation warnings.
pub(crate) fn collect_all_warnings(input: &str) -> Vec<ParseDiagnostic> {
    let mut warnings = collect_unsupported_warnings(input);
    warnings.extend(collect_subgraph_warnings(input));
    warnings.extend(collect_id_collision_warnings(input));

    if let Some(strict_warning) = collect_strict_parse_warning(input) {
        warnings.push(strict_warning);
    }

    warnings
}

fn collect_unsupported_warnings(input: &str) -> Vec<ParseDiagnostic> {
    let mut warnings = Vec::new();

    for (line_num, line) in input.lines().enumerate() {
        let trimmed = line.trim();

        if ci_starts_with(trimmed, "style ") {
            warnings.extend(collect_style_warnings(trimmed, line_num + 1));
            continue;
        }

        // classDef: warn on unsupported CSS properties
        if ci_starts_with(trimmed, "classDef ") {
            warnings.extend(collect_classdef_warnings(trimmed, line_num + 1));
            continue;
        }

        // class: now supported, skip
        if ci_starts_with(trimmed, "class ") && !ci_starts_with(trimmed, "classDef") {
            continue;
        }

        // linkStyle: warn on unsupported properties or invalid indices
        if ci_starts_with(trimmed, "linkStyle ") {
            warnings.extend(collect_linkstyle_warnings(trimmed, line_num + 1));
            continue;
        }

        for &(prefix, message) in UNSUPPORTED_KEYWORDS {
            if ci_starts_with(trimmed, prefix) {
                warnings.push(ParseDiagnostic::warning(
                    Some(line_num + 1),
                    Some(1),
                    message.to_string(),
                ));
                break;
            }
        }
    }

    warnings
}

fn collect_classdef_warnings(line: &str, line_num: usize) -> Vec<ParseDiagnostic> {
    match parse_classdef_statement(line) {
        Some(parsed) => parsed
            .issues
            .into_iter()
            .map(|issue| ParseDiagnostic::warning(Some(line_num), Some(1), issue.message()))
            .collect(),
        None => vec![ParseDiagnostic::warning(
            Some(line_num),
            Some(1),
            "classDef statements must use the form `classDef className key:value,...`".to_string(),
        )],
    }
}

fn collect_style_warnings(line: &str, line_num: usize) -> Vec<ParseDiagnostic> {
    match parse_node_style_statement(line) {
        Some(parsed) => parsed
            .issues
            .into_iter()
            .map(|issue| ParseDiagnostic::warning(Some(line_num), Some(1), issue.message()))
            .collect(),
        None => vec![ParseDiagnostic::warning(
            Some(line_num),
            Some(1),
            "style statements must use the form `style NODE key:value,...`".to_string(),
        )],
    }
}

fn collect_linkstyle_warnings(line: &str, line_num: usize) -> Vec<ParseDiagnostic> {
    match parse_linkstyle_statement(line) {
        Some(parsed) => parsed
            .issues
            .into_iter()
            .map(|issue| ParseDiagnostic::warning(Some(line_num), Some(1), issue.message()))
            .collect(),
        None => vec![ParseDiagnostic::warning(
            Some(line_num),
            Some(1),
            "linkStyle statements must use the form `linkStyle <target> key:value,...`".to_string(),
        )],
    }
}

fn collect_subgraph_warnings(input: &str) -> Vec<ParseDiagnostic> {
    let mut subgraph_lines: Vec<usize> = Vec::new();
    let mut end_count: usize = 0;

    for (line_num, line) in input.lines().enumerate() {
        let trimmed = line.trim();

        if ci_starts_with(trimmed, "subgraph ") || trimmed.eq_ignore_ascii_case("subgraph") {
            subgraph_lines.push(line_num + 1);
        }

        if trimmed.eq_ignore_ascii_case("end")
            || ci_starts_with(trimmed, "end ")
            || ci_starts_with(trimmed, "end;")
        {
            end_count += 1;
        }
    }

    let unmatched = subgraph_lines.len().saturating_sub(end_count);
    if unmatched == 0 {
        return Vec::new();
    }

    subgraph_lines
        .into_iter()
        .rev()
        .take(unmatched)
        .map(|line_num| {
            ParseDiagnostic::warning(
                Some(line_num),
                Some(1),
                "Subgraph may be missing an 'end' keyword. \
                 Without 'end', the subgraph keyword is treated as a node identifier."
                    .to_string(),
            )
        })
        .collect()
}

fn collect_strict_parse_warning(input: &str) -> Option<ParseDiagnostic> {
    let strict = ParseOptions { strict: true };
    let original_error = match parse_flowchart_with_options(input, &strict) {
        Ok(_) => return None,
        Err(error) => error,
    };

    if let Some(stripped) = strip_theme_only_compat_syntax(input)
        && parse_flowchart_with_options(&stripped, &strict).is_ok()
    {
        return None;
    }

    let mut diagnostic = ParseDiagnostic::from(&original_error);
    diagnostic.severity = "warning".to_string();
    diagnostic.message = format!("{STRICT_PARSE_WARNING_PREFIX} {}", diagnostic.message);
    Some(diagnostic)
}

fn ci_starts_with(line: &str, prefix: &str) -> bool {
    line.len() >= prefix.len()
        && line.as_bytes()[..prefix.len()]
            .iter()
            .zip(prefix.as_bytes())
            .all(|(a, b)| a.eq_ignore_ascii_case(b))
}

/// Detect subgraph/node id collisions via the parser.
///
/// We parse the input, walk the AST to find vertices with an explicit shape
/// whose id matches a declared subgraph, then look up a source line for each
/// occurrence. Sourcing from the parser (rather than a private text scan)
/// guarantees the warning's coverage matches the compiler's collapse rule —
/// numeric ids, dotted ids, and the `@{shape: …}` form all flow through.
/// Bare references like `A --> B` are NOT collisions (no `vertex.shape`).
/// Text inside edge labels never becomes a `Vertex`, so the false-positive
/// case `B -->|A[NotNode]| C` is naturally excluded.
fn collect_id_collision_warnings(input: &str) -> Vec<ParseDiagnostic> {
    let Ok(flowchart) = parse_flowchart(input) else {
        return Vec::new();
    };

    let collisions = collect_id_collisions(&flowchart);
    if collisions.is_empty() {
        return Vec::new();
    }

    let positions = locate_collisions(input, &collisions);
    collisions
        .iter()
        .zip(positions)
        .map(|(collision, position)| {
            let id = &collision.id;
            let (line_num, column) = match position {
                Some((row, col)) => (Some(row + 1), Some(col + 1)),
                None => (None, None),
            };
            let message = format!(
                "Identifier '{id}' is declared as a subgraph; the explicit \
                 node reference is collapsed into the subgraph.",
            );
            ParseDiagnostic::warning(line_num, column, message)
        })
        .collect()
}

/// Locate each collision in source text. Returns a parallel vector with
/// `None` for collisions whose source line could not be recovered.
fn locate_collisions(input: &str, collisions: &[IdCollision]) -> Vec<Option<(usize, usize)>> {
    let lines: Vec<String> = input.lines().map(censor_edge_label_text).collect();
    let mut used: Vec<(usize, usize)> = Vec::new();
    let mut positions = Vec::with_capacity(collisions.len());
    for collision in collisions {
        let position = find_collision_position(&lines, &collision.id, &used);
        if let Some(pos) = position {
            used.push(pos);
        }
        positions.push(position);
    }
    positions
}

/// Replace bytes inside `|...|` segments with spaces so a collision-position
/// scan doesn't latch onto an edge-label substring like `B -->|A[NotNode]| C`.
fn censor_edge_label_text(line: &str) -> String {
    let mut censored = line.as_bytes().to_vec();
    let mut i = 0;
    while i < censored.len() {
        if censored[i] == b'|' {
            let start = i + 1;
            let mut j = start;
            while j < censored.len() && censored[j] != b'|' {
                j += 1;
            }
            if j < censored.len() {
                for byte in &mut censored[start..j] {
                    *byte = b' ';
                }
                i = j + 1;
                continue;
            }
        }
        i += 1;
    }
    String::from_utf8(censored).unwrap_or_else(|_| line.to_string())
}

/// Find the first un-used line/column where `id` appears as a token followed
/// by a vertex-shape opener (`[`, `(`, `{`, `>`, or `@{`). Skips
/// `subgraph <id>…` declaration lines so the diagnostic points at the
/// colliding reference.
fn find_collision_position(
    lines: &[String],
    id: &str,
    used: &[(usize, usize)],
) -> Option<(usize, usize)> {
    let id_bytes = id.as_bytes();
    for (row, line) in lines.iter().enumerate() {
        let trimmed = line.trim_start();
        if ci_starts_with(trimmed, "subgraph ") {
            continue;
        }
        let bytes = line.as_bytes();
        let mut start = 0;
        while let Some(rel) = find_subsequence(&bytes[start..], id_bytes) {
            let pos = start + rel;
            start = pos + 1;

            if used.contains(&(row, pos)) {
                continue;
            }

            let prev_is_ident_continue = pos > 0 && is_ident_continue(bytes[pos - 1]);
            if prev_is_ident_continue {
                continue;
            }

            let after = pos + id_bytes.len();
            let next = bytes.get(after).copied();
            // Shape openers: `[`, `(`, `{`, `>` (asymmetric `>…]`), and
            // `@{` (Mermaid v11 `@{shape: …}` form).
            let matches_shape = matches!(next, Some(b'[' | b'(' | b'{' | b'>'))
                || (next == Some(b'@') && bytes.get(after + 1) == Some(&b'{'));
            if !matches_shape {
                continue;
            }

            return Some((row, pos));
        }
    }
    None
}

fn is_ident_continue(byte: u8) -> bool {
    byte.is_ascii_alphanumeric() || byte == b'_' || byte == b'-' || byte == b'.'
}

fn find_subsequence(haystack: &[u8], needle: &[u8]) -> Option<usize> {
    if needle.is_empty() || needle.len() > haystack.len() {
        return None;
    }
    haystack
        .windows(needle.len())
        .position(|window| window == needle)
}

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

    #[test]
    fn strict_warning_for_permissive_input() {
        let input = "%%{init: {}}%%\ngraph TD\nA --> B\n";
        let warning = collect_strict_parse_warning(input);
        assert!(warning.is_some());
        assert!(
            warning
                .unwrap()
                .message
                .contains("Strict parsing would reject")
        );
    }

    #[test]
    fn no_strict_warning_for_strict_valid_input() {
        let input = "graph TD\n  A --> B\n";
        let warning = collect_strict_parse_warning(input);
        assert!(warning.is_none());
    }

    #[test]
    fn strict_warning_ignores_theme_only_frontmatter() {
        let input = "---\nconfig:\n  theme: dark\n---\ngraph TD\nA --> B\n";
        let warning = collect_strict_parse_warning(input);
        assert!(warning.is_none());
    }

    #[test]
    fn strict_warning_ignores_theme_only_init_directive() {
        let input = "%%{init: {\"theme\": \"dark\"}}%%\ngraph TD\nA --> B\n";
        let warning = collect_strict_parse_warning(input);
        assert!(warning.is_none());
    }

    #[test]
    fn strict_warning_keeps_non_theme_init_keys() {
        let input = "%%{init: {\"theme\": \"dark\", \"flowchart\": {\"curve\": \"basis\"}}}%%\ngraph TD\nA --> B\n";
        let warning = collect_strict_parse_warning(input);
        assert!(warning.is_some());
        assert!(
            warning
                .unwrap()
                .message
                .contains("Strict parsing would reject")
        );
    }

    #[test]
    fn classdef_no_longer_warned_as_unsupported() {
        let input = "graph TD\n  classDef foo fill:#f00\n  A --> B\n";
        let warnings = collect_unsupported_warnings(input);
        assert!(
            !warnings.iter().any(|w| w.message.contains("classDef")),
            "classDef with valid properties should not produce warnings: {:?}",
            warnings
        );
    }

    #[test]
    fn classdef_unsupported_property_warned() {
        let input = "graph TD\n  classDef foo fill:#f00,shape-padding:14px\n  A:::foo\n";
        let warnings = collect_unsupported_warnings(input);
        assert!(
            warnings.iter().any(|w| w.message.contains("shape-padding")),
            "unsupported property in classDef should warn: {:?}",
            warnings
        );
    }

    #[test]
    fn unsupported_keyword_click() {
        let input = "graph TD\n  click A callback\n  A --> B\n";
        let warnings = collect_unsupported_warnings(input);
        assert!(!warnings.is_empty());
        assert!(warnings[0].message.contains("click"));
    }

    #[test]
    fn class_statement_no_longer_warned() {
        let input = "graph TD\n  class A foo\n  A --> B\n";
        let warnings = collect_unsupported_warnings(input);
        assert!(
            !warnings.iter().any(|w| w.message.contains("class")),
            "class statements should not produce warnings: {:?}",
            warnings
        );
    }

    #[test]
    fn missing_subgraph_end_warned() {
        let input = "graph TD\n  subgraph sg1\n  A --> B\n";
        let warnings = collect_subgraph_warnings(input);
        assert!(!warnings.is_empty());
        assert!(warnings[0].message.contains("missing an 'end'"));
    }

    #[test]
    fn matched_subgraph_no_warning() {
        let input = "graph TD\n  subgraph sg1\n  A --> B\n  end\n";
        let warnings = collect_subgraph_warnings(input);
        assert!(warnings.is_empty());
    }

    #[test]
    fn collect_all_includes_strict_and_unsupported() {
        let input = "%%{init: {}}%%\ngraph TD\n  click A callback\n  A --> B\n";
        let all = collect_all_warnings(input);
        assert!(
            all.len() >= 2,
            "expected strict + unsupported warnings, got {}: {:?}",
            all.len(),
            all.iter().map(|w| &w.message).collect::<Vec<_>>()
        );
    }

    #[test]
    fn clean_input_no_warnings() {
        let input = "graph TD\n  A --> B\n";
        let all = collect_all_warnings(input);
        assert!(all.is_empty());
    }

    #[test]
    fn linkstyle_valid_no_warning() {
        let input = "graph TD\n  A --> B\n  linkStyle 0 stroke:#f00\n";
        let warnings = collect_unsupported_warnings(input);
        assert!(
            warnings.is_empty(),
            "valid linkStyle should not produce warnings: {:?}",
            warnings
        );
    }

    #[test]
    fn linkstyle_invalid_index_warned() {
        let input = "graph TD\n  A --> B\n  B --> C\n  linkStyle nope,1 stroke:#f00\n";
        let warnings = collect_unsupported_warnings(input);
        assert!(
            warnings.iter().any(|w| w.message.contains("nope")),
            "invalid linkStyle index should warn: {:?}",
            warnings
        );
    }

    #[test]
    fn linkstyle_unsupported_property_warned() {
        let input = "graph TD\n  A --> B\n  linkStyle 0 opacity:0.5\n";
        let warnings = collect_unsupported_warnings(input);
        assert!(
            warnings.iter().any(|w| w.message.contains("opacity")),
            "unsupported linkStyle property should warn: {:?}",
            warnings
        );
    }

    #[test]
    fn linkstyle_malformed_warned() {
        let input = "graph TD\n  A --> B\n  linkStyle\n";
        let warnings = collect_unsupported_warnings(input);
        assert!(
            warnings.is_empty(),
            "bare linkStyle keyword (no target) should not match: {:?}",
            warnings
        );
    }

    #[test]
    fn id_collision_emits_warning_with_line() {
        let input = "\
flowchart LR

subgraph A
a1
end

subgraph C
A[NodeBox] --> B[NodeBox2]
end
";

        let warnings = collect_all_warnings(input);

        let collision = warnings
            .iter()
            .find(|w| w.message.contains("declared as a subgraph"))
            .expect("expected a collision warning");

        assert!(
            collision.message.contains("'A'"),
            "warning should single-quote the colliding id; got {:?}",
            collision.message,
        );
        assert_eq!(collision.severity, "warning");
        assert_eq!(
            collision.line,
            Some(8),
            "warning should point at the A[NodeBox] --> B[NodeBox2] line",
        );
    }

    #[test]
    fn no_collision_yields_no_warning() {
        let input = "flowchart LR\nsubgraph A\na1\nend\n";
        let warnings = collect_all_warnings(input);
        assert!(
            !warnings
                .iter()
                .any(|w| w.message.contains("declared as a subgraph")),
            "no collision in input but warning emitted: {warnings:?}",
        );
    }

    #[test]
    fn bare_reference_to_subgraph_id_does_not_emit_collision_warning() {
        let input = "\
flowchart LR

subgraph A
a1
end

subgraph C
A --> B
end
";
        let warnings = collect_all_warnings(input);
        assert!(
            !warnings
                .iter()
                .any(|w| w.message.contains("declared as a subgraph")),
            "bare reference must not trigger a collision warning: {warnings:?}",
        );
    }

    #[test]
    fn strict_parse_does_not_escalate_id_collision() {
        // Id collisions are resolved at the compiler layer
        // (`add_vertex_to_diagram` / `apply_collision_nesting`); the pest
        // grammar accepts the same input under strict and permissive
        // options. There is no strict-mode rejection path to advise
        // about, so the strict-warning channel intentionally stays
        // silent for collisions — only the permissive
        // "declared as a subgraph" warning fires (see
        // `collect_id_collision_warnings`). Issue #352.
        let input = "\
flowchart LR

subgraph A
a1
end

subgraph C
A[NodeBox] --> B[NodeBox2]
end
";

        let warning = collect_strict_parse_warning(input);

        match warning {
            None => {}
            Some(diagnostic) => {
                assert!(
                    !diagnostic.message.contains("declared as a subgraph"),
                    "strict-mode advisories should not duplicate the permissive collision warning; got {diagnostic:?}",
                );
            }
        }
    }

    #[test]
    fn numeric_subgraph_id_collision_emits_warning() {
        let input = "\
flowchart LR

subgraph 123[Numbers]
a1
end

subgraph C
123[NodeBox] --> B
end
";
        let warnings = collect_all_warnings(input);
        assert!(
            warnings.iter().any(
                |w| w.message.contains("declared as a subgraph") && w.message.contains("'123'")
            ),
            "numeric id collision must warn: {warnings:?}",
        );
    }

    #[test]
    fn dotted_subgraph_id_collision_emits_warning() {
        let input = "\
flowchart LR

subgraph my.node[Group]
a1
end

subgraph C
my.node[NodeBox] --> B
end
";
        let warnings = collect_all_warnings(input);
        assert!(
            warnings
                .iter()
                .any(|w| w.message.contains("declared as a subgraph")
                    && w.message.contains("'my.node'")),
            "dotted id collision must warn: {warnings:?}",
        );
    }

    #[test]
    fn asymmetric_shape_collision_emits_warning_with_line() {
        // Mermaid's asymmetric/flag shape `>...]`. The parser recognizes
        // `A>NodeBox]` as a Vertex with shape, so the warning must
        // line-locate it like the other shape forms.
        let input = "\
flowchart LR

subgraph A
a1
end

A>NodeBox]
";
        let warnings = collect_all_warnings(input);
        let collision = warnings
            .iter()
            .find(|w| w.message.contains("declared as a subgraph"))
            .expect("expected a collision warning");
        assert_eq!(
            collision.line,
            Some(7),
            "asymmetric shape collision must line-locate; got {:?}",
            collision,
        );
    }

    #[test]
    fn at_brace_shape_collision_emits_warning() {
        // Mermaid v11 `@{shape: ...}` shape syntax: the compiler collapses
        // `A@{shape: rect, label: "NodeBox"}` against `subgraph A`, so the
        // diagnostic path must too.
        let input = "\
flowchart LR

subgraph A
a1
end

A@{shape: rect, label: \"NodeBox\"}
";
        let warnings = collect_all_warnings(input);
        assert!(
            warnings
                .iter()
                .any(|w| w.message.contains("declared as a subgraph") && w.message.contains("'A'")),
            "@{{shape}} collision must warn: {warnings:?}",
        );
    }

    #[test]
    fn bare_id_in_edge_label_does_not_emit_collision_warning() {
        // `B -->|A[NotNode]| C` writes the literal `A[NotNode]` as an edge
        // label between pipes. The parser sees no Vertex for `A` here, so
        // the warning path must not surface a false collision.
        let input = "\
flowchart LR

subgraph A
a1
end

B -->|A[NotNode]| C
";
        let warnings = collect_all_warnings(input);
        assert!(
            !warnings
                .iter()
                .any(|w| w.message.contains("declared as a subgraph")),
            "edge-label text must not trigger a collision warning: {warnings:?}",
        );
    }

    #[test]
    fn top_level_explicit_shape_collision_still_emits_warning() {
        let input = "\
flowchart LR

subgraph A
a1
end

A[NodeBox]
";
        let warnings = collect_all_warnings(input);
        assert!(
            warnings
                .iter()
                .any(|w| w.message.contains("declared as a subgraph")),
            "top-level explicit-shape collision must still warn: {warnings:?}",
        );
    }
}