cviz 2.0.3

A CLI tool to visualize WebAssembly component composition structure.
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
use crate::model::{short_interface_name, CompositionGraph};
use crate::output::{build_all_interfaces_view, build_full_view, DetailLevel, SymbolMap};
use crate::{find_chain_interfaces, get_chain_for};

/// Generate an ASCII diagram from the composition graph
pub fn generate_ascii(graph: &CompositionGraph, detail: DetailLevel, show_types: bool) -> String {
    match detail {
        DetailLevel::HandlerChain => generate_handler_chain_ascii(graph, show_types),
        DetailLevel::AllInterfaces => generate_all_interfaces_ascii(graph, show_types),
        DetailLevel::Full => generate_full_ascii(graph, show_types),
    }
}

/// Generate ASCII diagram showing all middleware chains (request flow direction)
fn generate_handler_chain_ascii(graph: &CompositionGraph, show_types: bool) -> String {
    let chain_interfaces = find_chain_interfaces(graph);

    if chain_interfaces.is_empty() {
        return box_content("Service Chains", &["No service chains found"]);
    }

    let mut symbols = SymbolMap::new();
    let mut lines = Vec::new();

    for (i, iface) in chain_interfaces.iter().enumerate() {
        let chain = get_chain_for(graph, iface);
        if chain.is_empty() {
            continue;
        }

        // Separator between chains
        if i > 0 {
            lines.push(String::new());
        }

        let short = short_interface_name(iface);

        let export_sym: String = show_types
            .then(|| {
                graph
                    .component_exports
                    .get(iface.as_str())
                    .and_then(|info| symbols.symbol_for_export(info, &graph.arena))
                    .map(str::to_string)
            })
            .flatten()
            .unwrap_or_default();

        // Export entry point
        if let Some(&first_idx) = chain.first() {
            if let Some(first_node) = graph.get_node(first_idx) {
                lines.push(format!(
                    "[Export: {}{}] ──> {}",
                    short,
                    export_sym,
                    first_node.display_label()
                ));
            }
        }

        // Chain connections
        for window in chain.windows(2) {
            if let [from_idx, to_idx] = window {
                if let (Some(from_node), Some(to_node)) =
                    (graph.get_node(*from_idx), graph.get_node(*to_idx))
                {
                    let conn_sym: String = show_types
                        .then(|| {
                            from_node
                                .imports
                                .iter()
                                .find(|c| &c.interface_name == iface)
                                .and_then(|c| symbols.symbol_for_conn(c, &graph.arena))
                                .map(str::to_string)
                        })
                        .flatten()
                        .unwrap_or_default();
                    lines.push(format!(
                        "{} ── {}{} ──> {}",
                        from_node.display_label(),
                        short,
                        conn_sym,
                        to_node.display_label()
                    ));
                }
            }
        }
    }

    // Key — shared across all chains
    if !symbols.is_empty() {
        lines.push(String::new());
        lines.extend(symbols.key_lines());
    }

    box_content("Service Chains", &lines)
}

/// Generate ASCII diagram showing all interface connections
fn generate_all_interfaces_ascii(graph: &CompositionGraph, show_types: bool) -> String {
    let view = build_all_interfaces_view(graph, show_types);

    if view.nodes.is_empty() {
        return box_content("Component Instances", &["No component instances found"]);
    }

    let mut output = String::new();

    if !view.host_names.is_empty() {
        let host_lines: Vec<String> = view
            .host_names
            .iter()
            .map(|i| format!("  {{{}}}", short_interface_name(i)))
            .collect();
        output.push_str(&box_content("Host Imports", &host_lines));
        output.push('\n');
    }

    let instance_lines: Vec<String> = view
        .nodes
        .iter()
        .map(|n| format!("  [{}]", n.display))
        .collect();
    output.push_str(&box_content("Component Instances", &instance_lines));
    output.push('\n');

    let mut symbols = SymbolMap::new();
    let mut connection_lines = Vec::new();

    for edge in &view.edges {
        let sym = symbols.assign(
            show_types,
            edge.fingerprint.as_deref(),
            edge.type_lines.clone(),
        );
        if edge.is_dashed {
            connection_lines.push(format!(
                "  {{{}}} --- {}{} --> [{}]",
                edge.from_display, edge.label, sym, edge.to_display
            ));
        } else {
            connection_lines.push(format!(
                "  [{}] ── {}{} ──> [{}]",
                edge.from_display, edge.label, sym, edge.to_display
            ));
        }
    }

    for exp in &view.exports {
        let sym = symbols.assign(
            show_types,
            exp.fingerprint.as_deref(),
            exp.type_lines.clone(),
        );
        connection_lines.push(format!(
            "  [{}] ──> (Export: {}{})",
            exp.from_display, exp.short_name, sym
        ));
    }

    if !symbols.is_empty() {
        connection_lines.push(String::new());
        connection_lines.extend(symbols.key_lines().into_iter().map(|l| format!("  {}", l)));
    }

    if !connection_lines.is_empty() {
        output.push_str(&box_content("Connections", &connection_lines));
    }

    output
}

/// Generate a full ASCII diagram with all details
fn generate_full_ascii(graph: &CompositionGraph, show_types: bool) -> String {
    let view = build_full_view(graph, show_types);

    let mut instance_lines: Vec<String> = view
        .nodes
        .iter()
        .map(|n| {
            if n.is_synthetic {
                format!("  [{}] (synthetic)", n.display)
            } else {
                format!("  [{}] [comp:{}]", n.display, n.component_index)
            }
        })
        .collect();

    if instance_lines.is_empty() {
        instance_lines.push("  No instances found".to_string());
    }

    let mut output = String::new();
    output.push_str(&box_content("All Instances", &instance_lines));
    output.push('\n');

    let mut symbols = SymbolMap::new();
    let mut connection_lines = Vec::new();

    for edge in &view.edges {
        let sym = symbols.assign(
            show_types,
            edge.fingerprint.as_deref(),
            edge.type_lines.clone(),
        );
        connection_lines.push(format!(
            "  [{}] ── {}{} ──> [{}]",
            edge.from_display, edge.label, sym, edge.to_display
        ));
    }

    for exp in &view.exports {
        let sym = symbols.assign(
            show_types,
            exp.fingerprint.as_deref(),
            exp.type_lines.clone(),
        );
        connection_lines.push(format!(
            "  [{}] ──> (Export: {}{})",
            exp.from_display, exp.full_name, sym
        ));
    }

    if !symbols.is_empty() {
        connection_lines.push(String::new());
        connection_lines.extend(symbols.key_lines().into_iter().map(|l| format!("  {}", l)));
    }

    if !connection_lines.is_empty() {
        output.push_str(&box_content("Connections", &connection_lines));
    }

    output
}

/// Calculate the display width of a string (number of terminal columns).
/// Uses char count instead of byte length to handle multi-byte Unicode
/// characters like box-drawing characters (─) which are 3 bytes but 1 column.
fn display_width(s: &str) -> usize {
    s.chars().count()
}

/// Create a box around content with a title
fn box_content(title: &str, lines: &[impl AsRef<str>]) -> String {
    // Calculate the width needed (in display columns, not bytes)
    let title_width = display_width(title) + 2; // Add padding around title
    let max_line_width = lines
        .iter()
        .map(|l| display_width(l.as_ref()))
        .max()
        .unwrap_or(0);
    let width = std::cmp::max(title_width, max_line_width) + 4; // Add padding

    let mut output = String::new();

    // Top border
    output.push_str(&format!("{}\n", "".repeat(width)));

    // Title line centered
    let title_display = display_width(title);
    let title_padding = (width - title_display) / 2;
    let title_padding_right = width - title_display - title_padding;
    output.push_str(&format!(
        "{}{}{}\n",
        " ".repeat(title_padding),
        title,
        " ".repeat(title_padding_right)
    ));

    // Separator
    output.push_str(&format!("{}\n", "".repeat(width)));

    // Content lines
    for line in lines {
        let line_str = line.as_ref();
        let padding = width.saturating_sub(display_width(line_str));
        output.push_str(&format!("{}{}\n", line_str, " ".repeat(padding)));
    }

    // Bottom border
    output.push_str(&format!("{}", "".repeat(width)));

    output
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::model::{
        ComponentNode, FuncSignature, InstanceInterface, InterfaceConnection, InterfaceType,
        ValueType,
    };
    use crate::test_utils::*;
    use std::collections::BTreeMap;

    /// Return the 0-based line index of the first line containing `needle`.
    /// Panics with a clear message if not found.
    fn line_pos(output: &str, needle: &str) -> usize {
        output
            .lines()
            .position(|l| l.contains(needle))
            .unwrap_or_else(|| panic!("'{}' not found in output:\n{}", needle, output))
    }

    /// Build a graph: host → $srv$middleware → export(handler)
    fn test_graph() -> CompositionGraph {
        let mut graph = CompositionGraph::new();

        let mut srv = ComponentNode::new("$srv".to_string(), 0, 0);
        srv.add_import(InterfaceConnection {
            interface_name: "wasi:http/handler@0.3.0".to_string(),
            source_instance: None,
            is_host_import: true,
            interface_type: None,
            fingerprint: None,
        });
        graph.add_node(1, srv);

        let mut mw = ComponentNode::new("$middleware".to_string(), 1, 1);
        mw.add_import(InterfaceConnection {
            interface_name: "wasi:http/handler@0.3.0".to_string(),
            source_instance: Some(1),
            is_host_import: false,
            interface_type: None,
            fingerprint: None,
        });
        mw.add_import(InterfaceConnection {
            interface_name: "wasi:logging/log@0.1.0".to_string(),
            source_instance: None,
            is_host_import: true,
            interface_type: None,
            fingerprint: None,
        });
        graph.add_node(2, mw);

        graph.add_export("wasi:http/handler@0.3.0".to_string(), 2, None);
        graph
    }

    /// Build a graph with real type information for type-display tests.
    ///
    /// Adds an instance interface with a single `handle(u32) -> bool` function
    /// to both the import and export connections.
    fn test_graph_with_types() -> CompositionGraph {
        let mut graph = CompositionGraph::new();

        // Intern the param/result types up front
        let u32_id = graph.arena.intern_val(ValueType::U32);
        let bool_id = graph.arena.intern_val(ValueType::Bool);

        let handle_sig = FuncSignature {
            is_async: false,
            param_names: vec![],
            params: vec![u32_id],
            results: vec![bool_id],
        };
        let mut functions = BTreeMap::new();
        functions.insert("handle".to_string(), handle_sig);
        let iface_type = InterfaceType::Instance(InstanceInterface {
            functions,
            type_exports: BTreeMap::new(),
        });

        let mut srv = ComponentNode::new("$srv".to_string(), 0, 0);
        srv.add_import(InterfaceConnection {
            interface_name: "wasi:http/handler@0.3.0".to_string(),
            source_instance: None,
            is_host_import: true,
            interface_type: Some(iface_type.clone()),
            fingerprint: Some(iface_type.fingerprint(&graph.arena)),
        });
        graph.add_node(1, srv);

        let mut mw = ComponentNode::new("$middleware".to_string(), 1, 1);
        mw.add_import(InterfaceConnection {
            interface_name: "wasi:http/handler@0.3.0".to_string(),
            source_instance: Some(1),
            is_host_import: false,
            interface_type: Some(iface_type.clone()),
            fingerprint: Some(iface_type.fingerprint(&graph.arena)),
        });
        graph.add_node(2, mw);

        graph.add_export("wasi:http/handler@0.3.0".to_string(), 2, Some(iface_type));
        graph
    }

    #[test]
    fn test_box_content() {
        let result = box_content("Test", &["line 1", "line 2"]);
        assert!(result.contains("Test"));
        assert!(result.contains("line 1"));
        assert!(result.contains("line 2"));
        assert!(result.contains(""));
        assert!(result.contains(""));
    }

    #[test]
    fn test_handler_chain_ascii() {
        let graph = test_graph();
        let output = generate_ascii(&graph, DetailLevel::HandlerChain, false);

        assert!(output.contains("Service Chains"), "should have title");
        assert!(output.contains("srv"), "should show srv node");
        assert!(output.contains("middleware"), "should show middleware node");
        assert!(output.contains("handler"), "should show handler label");
        assert!(output.contains("Export"), "should show export at start");
        // Request flow order: export → middleware → srv
        assert!(
            output.contains("[Export: handler] ──> middleware"),
            "should show export pointing to outermost handler"
        );
        assert!(
            output.contains("middleware ── handler ──> srv"),
            "should show request flow from middleware to srv"
        );
    }

    #[test]
    fn test_all_interfaces_ascii() {
        let graph = test_graph();
        let output = generate_ascii(&graph, DetailLevel::AllInterfaces, false);

        assert!(
            output.contains("Host Imports"),
            "should have host imports section"
        );
        assert!(output.contains("handler"), "should show handler interface");
        assert!(output.contains("log"), "should show log interface");
        assert!(
            output.contains("Component Instances"),
            "should list instances"
        );
        assert!(output.contains("srv"), "should show srv");
        assert!(output.contains("middleware"), "should show middleware");
        assert!(
            output.contains("Connections"),
            "should have connections section"
        );
        assert!(output.contains("Export"), "should show export");
    }

    #[test]
    fn test_full_ascii() {
        let graph = test_graph();
        let output = generate_ascii(&graph, DetailLevel::Full, false);

        assert!(
            output.contains("All Instances"),
            "should have instances section"
        );
        assert!(output.contains("srv"), "should show srv");
        assert!(output.contains("middleware"), "should show middleware");
        assert!(
            output.contains("wasi:http/handler@0.3.0"),
            "should show full interface name"
        );
        assert!(output.contains("Connections"), "should have connections");
    }

    #[test]
    fn test_empty_graph_ascii() {
        let graph = CompositionGraph::new();

        let chain = generate_ascii(&graph, DetailLevel::HandlerChain, false);
        assert!(chain.contains("No service chains found"), "{}", chain);

        let all = generate_ascii(&graph, DetailLevel::AllInterfaces, false);
        assert!(all.contains("No component instances found"));

        let full = generate_ascii(&graph, DetailLevel::Full, false);
        assert!(full.contains("No instances found"));
    }

    #[test]
    fn test_show_types_all_interfaces() {
        let graph = test_graph_with_types();
        let output = generate_ascii(&graph, DetailLevel::AllInterfaces, true);

        // Each connection should be followed by the function signature
        assert!(
            output.contains("`handle`: (u32) -> bool"),
            "should show function signature"
        );
    }

    #[test]
    fn test_show_types_full() {
        let graph = test_graph_with_types();
        let output = generate_ascii(&graph, DetailLevel::Full, true);

        assert!(
            output.contains("`handle`: (u32) -> bool"),
            "should show function signature"
        );
    }

    #[test]
    fn test_hide_types_all_interfaces() {
        let graph = test_graph_with_types();
        let output = generate_ascii(&graph, DetailLevel::AllInterfaces, false);

        // Type lines should not appear when show_types=false
        assert!(
            !output.contains("`handle`: (u32) -> bool"),
            "should not show signatures when types disabled"
        );
    }

    // -----------------------------------------------------------------------
    // Ordering / request-flow order
    // -----------------------------------------------------------------------

    #[test]
    fn test_chain_request_flow_order() {
        let graph = simple_chain_graph();
        let output = generate_ascii(&graph, DetailLevel::HandlerChain, false);
        // Export entry point must appear before the middleware→srv edge
        let export_pos = line_pos(&output, "[Export: handler] ──>");
        let edge_pos = line_pos(&output, "middleware ── handler ──> srv");
        assert!(
            export_pos < edge_pos,
            "export entry should precede chain edge"
        );
    }

    #[test]
    fn test_long_chain_order() {
        let graph = long_chain_graph(); // messaging/consumer: gateway → service → backend
        let output = generate_ascii(&graph, DetailLevel::HandlerChain, false);
        let export_pos = line_pos(&output, "[Export: consumer] ──>");
        let first_edge_pos = line_pos(&output, "gateway ── consumer ──> service");
        let second_edge_pos = line_pos(&output, "service ── consumer ──> backend");
        assert!(
            export_pos < first_edge_pos,
            "export should precede gateway→service"
        );
        assert!(
            first_edge_pos < second_edge_pos,
            "gateway→service should precede service→backend"
        );
    }

    // -----------------------------------------------------------------------
    // Multiple chains
    // -----------------------------------------------------------------------

    #[test]
    fn test_two_chains_ascii() {
        let graph = two_chain_graph();
        let output = generate_ascii(&graph, DetailLevel::HandlerChain, false);
        assert!(
            output.contains("[Export: handler]"),
            "should show http handler export"
        );
        assert!(
            output.contains("[Export: store]"),
            "should show keyvalue store export"
        );
    }

    #[test]
    fn test_two_chains_both_edges() {
        let graph = two_chain_graph();
        let output = generate_ascii(&graph, DetailLevel::HandlerChain, false);
        assert!(
            output.contains("mw-http ── handler ──> srv-http"),
            "should show http handler chain edge, got:\n{}",
            output
        );
        assert!(
            output.contains("cache ── store ──> db"),
            "should show keyvalue chain edge, got:\n{}",
            output
        );
    }

    // -----------------------------------------------------------------------
    // Utility node isolation
    // -----------------------------------------------------------------------

    #[test]
    fn test_utility_node_absent_in_handler_chain() {
        let graph = chain_plus_utility_graph();
        let chain_out = generate_ascii(&graph, DetailLevel::HandlerChain, false);
        assert!(
            !chain_out.contains("logger"),
            "utility node should not appear in HandlerChain output"
        );

        let all_out = generate_ascii(&graph, DetailLevel::AllInterfaces, false);
        assert!(
            all_out.contains("logger"),
            "utility node should appear in AllInterfaces output"
        );
    }

    // -----------------------------------------------------------------------
    // Long (3-node) chain
    // -----------------------------------------------------------------------

    #[test]
    fn test_long_chain_ascii() {
        let graph = long_chain_graph();
        let output = generate_ascii(&graph, DetailLevel::HandlerChain, false);
        assert!(output.contains("gateway"), "should show gateway node");
        assert!(output.contains("service"), "should show service node");
        assert!(output.contains("backend"), "should show backend node");
        assert!(
            output.contains("gateway ── consumer ──> service"),
            "should show first hop"
        );
        assert!(
            output.contains("service ── consumer ──> backend"),
            "should show second hop"
        );
    }

    // -----------------------------------------------------------------------
    // HandlerChain type symbols / key
    // -----------------------------------------------------------------------

    #[test]
    fn test_handler_chain_types_key_appears() {
        let graph = typed_chain_graph();
        let output = generate_ascii(&graph, DetailLevel::HandlerChain, true);
        // The key section is separated from the chain by a blank line and
        // contains a symbol followed by the function signature.
        assert!(
            output.contains("`handle`: (u32) -> bool"),
            "key should contain function signature"
        );
    }

    #[test]
    fn test_handler_chain_types_symbol_in_export() {
        let graph = typed_chain_graph();
        let output = generate_ascii(&graph, DetailLevel::HandlerChain, true);
        // The export label should have a symbol appended, making it longer than just "[Export: handler]"
        let export_line = output
            .lines()
            .find(|l| l.contains("[Export: handler"))
            .unwrap_or_else(|| panic!("no export line found in:\n{}", output));
        assert!(
            export_line.len() > "[Export: handler]".len() + 5,
            "export label should include a type symbol, got: {}",
            export_line
        );
    }

    #[test]
    fn test_handler_chain_types_key_content() {
        let graph = typed_chain_graph();
        let output = generate_ascii(&graph, DetailLevel::HandlerChain, true);
        // Key line format: "<symbol> `handle`: (u32) -> bool"
        let key_line = output
            .lines()
            .find(|l| l.contains("`handle`"))
            .unwrap_or_else(|| panic!("no key line found in:\n{}", output));
        assert!(
            key_line.contains("(u32) -> bool"),
            "key line should contain full signature"
        );
    }

    #[test]
    fn test_two_typed_chains_distinct_symbols() {
        let graph = two_typed_chain_graph();
        let output = generate_ascii(&graph, DetailLevel::HandlerChain, true);
        // Two distinct interface types → two key entries
        let key_lines: Vec<&str> = output.lines().filter(|l| l.contains("->")).collect();
        assert!(
            key_lines.len() >= 2,
            "should have two key entries for two distinct types, got:\n{}",
            output
        );
        // The two key lines should have different symbols (first char differs)
        let symbols: Vec<&str> = key_lines
            .iter()
            .map(|l| l.split_whitespace().next().unwrap_or(""))
            .collect();
        assert_ne!(
            symbols[0], symbols[1],
            "distinct types should get distinct symbols"
        );
    }

    #[test]
    fn test_same_type_same_symbol() {
        // typed_chain_graph has the same interface type on the export and the
        // inter-component connection. Both should get the same symbol → only
        // one key entry.
        let graph = typed_chain_graph();
        let output = generate_ascii(&graph, DetailLevel::HandlerChain, true);
        let sig = "`handle`: (u32) -> bool";
        let occurrences = output.matches(sig).count();
        assert_eq!(
            occurrences, 1,
            "same type fingerprint should produce exactly one key entry"
        );
    }

    // -----------------------------------------------------------------------
    // AllInterfaces exact edge / section format
    // -----------------------------------------------------------------------

    #[test]
    fn test_all_interfaces_host_edge_format() {
        let graph = simple_chain_graph();
        let output = generate_ascii(&graph, DetailLevel::AllInterfaces, false);
        // Host import edges use dashed style with braces for the interface name.
        // $srv imports handler directly from the host.
        assert!(
            output.contains("{handler} --- handler --> [srv]"),
            "host edge should use dashed format, got:\n{}",
            output
        );
    }

    #[test]
    fn test_all_interfaces_component_edge_format() {
        let graph = simple_chain_graph();
        let output = generate_ascii(&graph, DetailLevel::AllInterfaces, false);
        assert!(
            output.contains("[srv] ── handler ──> [middleware]"),
            "component edge should use solid arrow format, got:\n{}",
            output
        );
    }

    #[test]
    fn test_all_interfaces_export_format() {
        let graph = simple_chain_graph();
        let output = generate_ascii(&graph, DetailLevel::AllInterfaces, false);
        assert!(
            output.contains("[middleware] ──> (Export: handler)"),
            "export line should use expected format, got:\n{}",
            output
        );
    }

    #[test]
    fn test_all_interfaces_type_lines_on_export() {
        // Type lines should appear *below the export line*, not just somewhere in the output.
        let graph = typed_chain_graph();
        let output = generate_ascii(&graph, DetailLevel::AllInterfaces, true);
        let export_pos = line_pos(&output, "──> (Export: handler");
        let sig_after_export = output
            .lines()
            .skip(export_pos + 1)
            .any(|l| l.contains("`handle`: (u32) -> bool"));
        assert!(
            sig_after_export,
            "type signature should appear after the export line, got:\n{}",
            output
        );
    }

    #[test]
    fn test_all_interfaces_two_chains() {
        let graph = two_chain_graph();
        let output = generate_ascii(&graph, DetailLevel::AllInterfaces, false);
        // All 4 nodes in Component Instances
        assert!(output.contains("srv-http"), "should show srv-http");
        assert!(output.contains("mw-http"), "should show mw-http");
        assert!(output.contains("db"), "should show db");
        assert!(output.contains("cache"), "should show cache");
        // Both chain edges
        assert!(
            output.contains("[srv-http] ── handler ──> [mw-http]"),
            "should show handler edge"
        );
        assert!(
            output.contains("[db] ── store ──> [cache]"),
            "should show store edge"
        );
        // Both exports
        assert!(
            output.contains("(Export: handler)"),
            "should show handler export"
        );
        assert!(
            output.contains("(Export: store)"),
            "should show store export"
        );
    }

    #[test]
    fn test_full_synthetic_node_visible() {
        use crate::model::{ComponentNode, SYNTHETIC_COMPONENT};
        let mut graph = CompositionGraph::new();
        let real = ComponentNode::new("$real".to_string(), 0, 0);
        graph.add_node(1, real);
        let synthetic = ComponentNode::new(
            "$synth".to_string(),
            SYNTHETIC_COMPONENT,
            SYNTHETIC_COMPONENT,
        );
        graph.add_node(99, synthetic);

        let output = generate_ascii(&graph, DetailLevel::Full, false);
        assert!(
            output.contains("synth"),
            "synthetic node should appear in Full output"
        );
        assert!(
            output.contains("(synthetic)"),
            "synthetic node should be labelled as synthetic"
        );
    }

    #[test]
    fn test_handler_chain_no_key_when_types_disabled() {
        let graph = typed_chain_graph();
        let output = generate_ascii(&graph, DetailLevel::HandlerChain, false);
        // No symbol from the pool should appear, and no function signature
        assert!(!output.contains("`handle`"), "no key when show_types=false");
        assert!(
            !output.contains("(u32) -> bool"),
            "no sig when show_types=false"
        );
    }

    #[test]
    fn test_handler_chain_key_after_chain_content() {
        let graph = typed_chain_graph();
        let output = generate_ascii(&graph, DetailLevel::HandlerChain, true);
        // When show_types=true the edge label gets a symbol appended (e.g. "──handler✦──> srv"),
        // so search for the portion that is always present regardless of symbol.
        let chain_edge_pos = line_pos(&output, "──> srv");
        let key_pos = line_pos(&output, "`handle`: (u32) -> bool");
        assert!(
            key_pos > chain_edge_pos,
            "key should appear after chain edges"
        );
    }

    #[test]
    fn test_two_chains_blank_line_separator() {
        let graph = two_chain_graph();
        let output = generate_ascii(&graph, DetailLevel::HandlerChain, false);
        // Inside a box, a blank separator line is rendered as "│<spaces>│" —
        // every character is either '│' or ' '.
        let has_separator = output
            .lines()
            .any(|l| l.contains('') && l.chars().all(|c| c == '' || c == ' '));
        assert!(
            has_separator,
            "blank separator line should exist between two chains, got:\n{}",
            output
        );
    }
}