svccat 1.6.0

Detect drift between your declared service catalog and what actually lives in the repo.
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
use crate::drift::DriftReport;
use crate::manifest::Manifest;
use crate::output::d3_force_graph::{self, D3GraphConfig, TooltipField};
use crate::output::json_script;
use std::collections::BTreeMap;

pub fn render_graph(manifest: &Manifest) {
    render_graph_filtered(manifest, None);
}

/// Render the graph optionally filtered to a single team.
///
/// When `team` is `Some`, only services owned by that team are included as
/// primary nodes.  Services referenced via `depends_on` that belong to *other*
/// teams (or have no `team:` set) are rendered as external placeholder nodes
/// outside any subgraph, clearly marked as `[ext]`.
pub fn render_graph_filtered(manifest: &Manifest, team: Option<&str>) {
    // Determine in-scope service names.
    let in_scope: std::collections::HashSet<&str> = manifest
        .services
        .iter()
        .filter(|s| match team {
            None => true,
            Some(t) => s
                .team
                .as_deref()
                .map(|v| v.eq_ignore_ascii_case(t))
                .unwrap_or(false),
        })
        .map(|s| s.name.as_str())
        .collect();

    // Group in-scope services by platform for subgraphs.
    let mut groups: BTreeMap<String, Vec<&crate::manifest::ServiceEntry>> = BTreeMap::new();
    for svc in &manifest.services {
        if in_scope.contains(svc.name.as_str()) {
            let platform = svc
                .platform
                .clone()
                .unwrap_or_else(|| "Undeployed".to_string());
            groups.entry(platform).or_default().push(svc);
        }
    }

    // Collect external dependency targets (cross-team or undeclared).
    let declared_names: std::collections::HashSet<&str> =
        manifest.services.iter().map(|s| s.name.as_str()).collect();
    let mut external: std::collections::BTreeSet<&str> = std::collections::BTreeSet::new();
    for svc in &manifest.services {
        if !in_scope.contains(svc.name.as_str()) {
            continue;
        }
        for dep in &svc.depends_on {
            if !in_scope.contains(dep.as_str()) && declared_names.contains(dep.as_str()) {
                external.insert(dep.as_str());
            }
        }
    }

    println!("```mermaid");
    println!("graph TD");

    for (platform, services) in &groups {
        let safe_plat = safe_id(platform);
        println!("  subgraph {}[\"{}\"]", safe_plat, platform);
        for svc in services {
            let node_id = safe_id(&svc.name);
            let label = build_label(svc);
            println!("    {}[\"{}\"]", node_id, label);
        }
        println!("  end");
    }

    // External nodes (cross-team dependencies): rendered as plain nodes with [ext] tag.
    if !external.is_empty() {
        println!("  subgraph External[\"External (other teams)\"]");
        for name in &external {
            println!("    {}[\"{}\\n[ext]\"]", safe_id(name), name);
        }
        println!("  end");
    }

    // Render dependency edges for in-scope services.
    for svc in &manifest.services {
        if !in_scope.contains(svc.name.as_str()) {
            continue;
        }
        for dep in &svc.depends_on {
            if in_scope.contains(dep.as_str()) || external.contains(dep.as_str()) {
                println!("  {} --> {}", safe_id(&svc.name), safe_id(dep));
            }
        }
    }

    println!("```");
}

pub fn render_markdown_table(manifest: &Manifest) {
    println!("# Service Catalog\n");
    println!("| Service | Language | Platform | Role | Team | URL |");
    println!("|---------|----------|----------|------|------|-----|");
    for svc in &manifest.services {
        println!(
            "| {} | {} | {} | {} | {} | {} |",
            svc.name,
            svc.language.as_deref().unwrap_or(""),
            svc.platform.as_deref().unwrap_or(""),
            svc.role.as_deref().unwrap_or(""),
            svc.team.as_deref().unwrap_or(""),
            svc.url
                .as_deref()
                .map(|u| format!("[link]({})", u))
                .unwrap_or_else(|| "".to_string()),
        );
    }
}

pub fn render_export_markdown(manifest: &Manifest, report: &DriftReport) {
    render_markdown_table(manifest);

    if !report.drifts.is_empty() {
        println!("\n## Drift Report\n");
        println!("| Severity | Kind | Service | Message |");
        println!("|----------|------|---------|---------|");
        for item in &report.drifts {
            let severity = format!("{:?}", item.severity).to_lowercase();
            let kind = format!("{:?}", item.kind);
            println!(
                "| {} | {} | {} | {} |",
                severity, kind, item.service, item.message
            );
        }
    }
}

// ── Helpers ───────────────────────────────────────────────────────────────────

fn safe_id(s: &str) -> String {
    s.chars()
        .map(|c| {
            if c.is_alphanumeric() || c == '_' {
                c
            } else {
                '_'
            }
        })
        .collect()
}

fn build_label(svc: &crate::manifest::ServiceEntry) -> String {
    match (&svc.language, &svc.role) {
        (Some(lang), Some(role)) => format!("{}\\n{}\\n{}", svc.name, lang, role),
        (Some(lang), None) => format!("{}\\n{}", svc.name, lang),
        (None, Some(role)) => format!("{}\\n{}", svc.name, role),
        (None, None) => svc.name.clone(),
    }
}

// ── Graphviz DOT renderer ─────────────────────────────────────────────────────

/// Emit a Graphviz DOT digraph of the service dependency graph.
///
/// Services are grouped into DOT subgraph clusters by platform.  When `team`
/// is `Some`, only services owned by that team are included as primary nodes.
pub fn render_dot(manifest: &Manifest, team: Option<&str>) {
    let in_scope: std::collections::HashSet<&str> = manifest
        .services
        .iter()
        .filter(|s| match team {
            None => true,
            Some(t) => s
                .team
                .as_deref()
                .map(|v| v.eq_ignore_ascii_case(t))
                .unwrap_or(false),
        })
        .map(|s| s.name.as_str())
        .collect();

    // Group in-scope services by platform.
    let mut groups: BTreeMap<String, Vec<&crate::manifest::ServiceEntry>> = BTreeMap::new();
    for svc in &manifest.services {
        if in_scope.contains(svc.name.as_str()) {
            let platform = svc
                .platform
                .clone()
                .unwrap_or_else(|| "undeployed".to_string());
            groups.entry(platform).or_default().push(svc);
        }
    }

    println!("digraph services {{");
    println!("  rankdir=LR;");
    println!("  node [shape=box fontname=Helvetica];");
    println!();

    for (idx, (platform, services)) in groups.iter().enumerate() {
        println!("  subgraph cluster_{idx} {{");
        println!("    label=\"{}\";", dot_escape(platform));
        println!("    style=filled;");
        println!("    color=lightgrey;");
        for svc in services {
            println!(
                "    \"{}\" [label=\"{}\"];",
                dot_escape(&svc.name),
                dot_escape(&svc.name)
            );
        }
        println!("  }}");
        println!();
    }

    // Dependency edges.
    for svc in &manifest.services {
        if !in_scope.contains(svc.name.as_str()) {
            continue;
        }
        for dep in &svc.depends_on {
            println!(
                "  \"{}\" -> \"{}\";",
                dot_escape(&svc.name),
                dot_escape(dep)
            );
        }
    }

    println!("}}");
}

fn dot_escape(s: &str) -> String {
    s.replace('\\', "\\\\").replace('"', "\\\"")
}

// ── PlantUML renderer ─────────────────────────────────────────────────────────

/// Emit a PlantUML component diagram of the service dependency graph.
///
/// Services are grouped as packages by platform.  Pipe the output to
/// `plantuml -pipe` or paste into https://www.plantuml.com/plantuml/uml/
pub fn render_plantuml(manifest: &Manifest, team: Option<&str>) {
    let in_scope: std::collections::HashSet<&str> = manifest
        .services
        .iter()
        .filter(|s| match team {
            None => true,
            Some(t) => s
                .team
                .as_deref()
                .map(|v| v.eq_ignore_ascii_case(t))
                .unwrap_or(false),
        })
        .map(|s| s.name.as_str())
        .collect();

    // Group in-scope services by platform for packages.
    let mut groups: BTreeMap<String, Vec<&crate::manifest::ServiceEntry>> = BTreeMap::new();
    for svc in &manifest.services {
        if in_scope.contains(svc.name.as_str()) {
            let platform = svc
                .platform
                .clone()
                .unwrap_or_else(|| "undeployed".to_string());
            groups.entry(platform).or_default().push(svc);
        }
    }

    println!("@startuml");
    println!("skinparam componentStyle rectangle");
    println!();

    for (platform, services) in &groups {
        println!("package \"{}\" {{", plantuml_escape(platform));
        for svc in services {
            let label = match &svc.language {
                Some(lang) => format!("{} ({})", svc.name, lang),
                None => svc.name.clone(),
            };
            println!(
                "  component [{}] as {}",
                plantuml_escape(&label),
                plantuml_id(&svc.name)
            );
        }
        println!("}}");
        println!();
    }

    // Dependency edges.
    for svc in &manifest.services {
        if !in_scope.contains(svc.name.as_str()) {
            continue;
        }
        for dep in &svc.depends_on {
            println!(
                "{} ..> {} : depends",
                plantuml_id(&svc.name),
                plantuml_id(dep)
            );
        }
    }

    println!();
    println!("@enduml");
}

fn plantuml_escape(s: &str) -> String {
    s.replace('"', "'")
}

fn plantuml_id(s: &str) -> String {
    s.chars()
        .map(|c| {
            if c.is_alphanumeric() || c == '_' {
                c
            } else {
                '_'
            }
        })
        .collect()
}

// ── String-building helpers for --output support ───────────────────────────────

/// Macro to build graph content into a String instead of printing to stdout.
macro_rules! sbuf {
    ($buf:ident, $($arg:tt)*) => {
        writeln!($buf, $($arg)*).unwrap()
    };
}

pub fn render_graph_filtered_string(manifest: &Manifest, team: Option<&str>) -> String {
    use std::fmt::Write;
    let mut buf = String::new();

    let in_scope: std::collections::HashSet<&str> = manifest
        .services
        .iter()
        .filter(|s| match team {
            None => true,
            Some(t) => s
                .team
                .as_deref()
                .map(|v| v.eq_ignore_ascii_case(t))
                .unwrap_or(false),
        })
        .map(|s| s.name.as_str())
        .collect();

    let mut groups: BTreeMap<String, Vec<&crate::manifest::ServiceEntry>> = BTreeMap::new();
    for svc in &manifest.services {
        if in_scope.contains(svc.name.as_str()) {
            let platform = svc
                .platform
                .clone()
                .unwrap_or_else(|| "Undeployed".to_string());
            groups.entry(platform).or_default().push(svc);
        }
    }

    let declared_names: std::collections::HashSet<&str> =
        manifest.services.iter().map(|s| s.name.as_str()).collect();
    let mut external: std::collections::BTreeSet<&str> = std::collections::BTreeSet::new();
    for svc in &manifest.services {
        if !in_scope.contains(svc.name.as_str()) {
            continue;
        }
        for dep in &svc.depends_on {
            if !in_scope.contains(dep.as_str()) && declared_names.contains(dep.as_str()) {
                external.insert(dep.as_str());
            }
        }
    }

    sbuf!(buf, "```mermaid");
    sbuf!(buf, "graph TD");
    for (platform, services) in &groups {
        let safe_plat = safe_id(platform);
        sbuf!(buf, "  subgraph {}[\"{}\"]", safe_plat, platform);
        for svc in services {
            sbuf!(buf, "    {}[\"{}\"]", safe_id(&svc.name), build_label(svc));
        }
        sbuf!(buf, "  end");
    }
    if !external.is_empty() {
        sbuf!(buf, "  subgraph External[\"External (other teams)\"]");
        for name in &external {
            sbuf!(buf, "    {}[\"{}\\n[ext]\"]", safe_id(name), name);
        }
        sbuf!(buf, "  end");
    }
    for svc in &manifest.services {
        if !in_scope.contains(svc.name.as_str()) {
            continue;
        }
        for dep in &svc.depends_on {
            if in_scope.contains(dep.as_str()) || external.contains(dep.as_str()) {
                sbuf!(buf, "  {} --> {}", safe_id(&svc.name), safe_id(dep));
            }
        }
    }
    sbuf!(buf, "```");
    buf
}

pub fn render_markdown_table_string(manifest: &Manifest) -> String {
    use std::fmt::Write;
    let mut buf = String::new();
    sbuf!(buf, "# Service Catalog\n");
    sbuf!(buf, "| Service | Language | Platform | Role | Team | URL |");
    sbuf!(buf, "|---------|----------|----------|------|------|-----|");
    for svc in &manifest.services {
        sbuf!(
            buf,
            "| {} | {} | {} | {} | {} | {} |",
            svc.name,
            svc.language.as_deref().unwrap_or(""),
            svc.platform.as_deref().unwrap_or(""),
            svc.role.as_deref().unwrap_or(""),
            svc.team.as_deref().unwrap_or(""),
            svc.url.as_deref().unwrap_or("")
        );
    }
    buf
}

pub fn render_dot_string(manifest: &Manifest, team: Option<&str>) -> String {
    use std::fmt::Write;
    let mut buf = String::new();

    let in_scope: std::collections::HashSet<&str> = manifest
        .services
        .iter()
        .filter(|s| match team {
            None => true,
            Some(t) => s
                .team
                .as_deref()
                .map(|v| v.eq_ignore_ascii_case(t))
                .unwrap_or(false),
        })
        .map(|s| s.name.as_str())
        .collect();

    let mut groups: BTreeMap<String, Vec<&crate::manifest::ServiceEntry>> = BTreeMap::new();
    for svc in &manifest.services {
        if in_scope.contains(svc.name.as_str()) {
            let platform = svc
                .platform
                .clone()
                .unwrap_or_else(|| "undeployed".to_string());
            groups.entry(platform).or_default().push(svc);
        }
    }

    sbuf!(buf, "digraph services {{");
    sbuf!(buf, "  rankdir=LR;");
    sbuf!(buf, "  node [shape=box fontname=Helvetica];");
    sbuf!(buf, "");
    for (idx, (platform, services)) in groups.iter().enumerate() {
        sbuf!(buf, "  subgraph cluster_{idx} {{");
        sbuf!(buf, "    label=\"{}\";", dot_escape(platform));
        sbuf!(buf, "    style=filled;");
        sbuf!(buf, "    color=lightgrey;");
        for svc in services {
            sbuf!(
                buf,
                "    \"{}\" [label=\"{}\"];",
                dot_escape(&svc.name),
                dot_escape(&svc.name)
            );
        }
        sbuf!(buf, "  }}");
        sbuf!(buf, "");
    }
    for svc in &manifest.services {
        if !in_scope.contains(svc.name.as_str()) {
            continue;
        }
        for dep in &svc.depends_on {
            sbuf!(
                buf,
                "  \"{}\" -> \"{}\";",
                dot_escape(&svc.name),
                dot_escape(dep)
            );
        }
    }
    sbuf!(buf, "}}");
    buf
}

pub fn render_plantuml_string(manifest: &Manifest, team: Option<&str>) -> String {
    use std::fmt::Write;
    let mut buf = String::new();

    let in_scope: std::collections::HashSet<&str> = manifest
        .services
        .iter()
        .filter(|s| match team {
            None => true,
            Some(t) => s
                .team
                .as_deref()
                .map(|v| v.eq_ignore_ascii_case(t))
                .unwrap_or(false),
        })
        .map(|s| s.name.as_str())
        .collect();

    let mut groups: BTreeMap<String, Vec<&crate::manifest::ServiceEntry>> = BTreeMap::new();
    for svc in &manifest.services {
        if in_scope.contains(svc.name.as_str()) {
            let platform = svc
                .platform
                .clone()
                .unwrap_or_else(|| "undeployed".to_string());
            groups.entry(platform).or_default().push(svc);
        }
    }

    sbuf!(buf, "@startuml");
    sbuf!(buf, "skinparam componentStyle rectangle");
    sbuf!(buf, "");
    for (platform, services) in &groups {
        sbuf!(buf, "package \"{}\" {{", plantuml_escape(platform));
        for svc in services {
            let label = match &svc.language {
                Some(lang) => format!("{} ({})", svc.name, lang),
                None => svc.name.clone(),
            };
            sbuf!(
                buf,
                "  component [{}] as {}",
                plantuml_escape(&label),
                plantuml_id(&svc.name)
            );
        }
        sbuf!(buf, "}}");
        sbuf!(buf, "");
    }
    for svc in &manifest.services {
        if !in_scope.contains(svc.name.as_str()) {
            continue;
        }
        for dep in &svc.depends_on {
            sbuf!(
                buf,
                "{} ..> {} : depends",
                plantuml_id(&svc.name),
                plantuml_id(dep)
            );
        }
    }
    sbuf!(buf, "");
    sbuf!(buf, "@enduml");
    buf
}

// ── HTML interactive graph ─────────────────────────────────────────────────────

/// A D3 node for the single-repo graph: one entry per in-scope service,
/// carrying the fields the tooltip and colour scale read.
#[derive(serde::Serialize)]
struct D3Node<'a> {
    id: &'a str,
    platform: &'a str,
    team: &'a str,
    language: &'a str,
}

#[derive(serde::Serialize)]
struct D3Link<'a> {
    source: &'a str,
    target: &'a str,
}

#[derive(serde::Serialize)]
struct D3Graph<'a> {
    nodes: Vec<D3Node<'a>>,
    links: Vec<D3Link<'a>>,
}

/// Build the D3 node/link arrays from the in-scope services.
fn build_d3_graph<'a>(
    manifest: &'a Manifest,
    in_scope: &std::collections::HashSet<&str>,
) -> D3Graph<'a> {
    let nodes = manifest
        .services
        .iter()
        .filter(|s| in_scope.contains(s.name.as_str()))
        .map(|svc| D3Node {
            id: svc.name.as_str(),
            platform: svc.platform.as_deref().unwrap_or("unknown"),
            team: svc.team.as_deref().unwrap_or(""),
            language: svc.language.as_deref().unwrap_or(""),
        })
        .collect();

    let mut links = Vec::new();
    for svc in manifest
        .services
        .iter()
        .filter(|s| in_scope.contains(s.name.as_str()))
    {
        for dep in &svc.depends_on {
            links.push(D3Link {
                source: svc.name.as_str(),
                target: dep.as_str(),
            });
        }
    }

    D3Graph { nodes, links }
}

/// Render a self-contained HTML file with a D3.js v7 force-directed dependency graph.
///
/// Nodes are coloured by platform; edges represent `depends_on` links.
/// The HTML file has no external CDN dependencies: D3 is embedded via a CDN
/// `<script>` tag so the file requires an internet connection to render.
///
/// Two escaping mechanisms are load-bearing here, the same split
/// `workspace_html.rs` documents for its own report:
///
/// - The D3 mechanics (drag physics, arrow marker, tick handler, and the
///   tooltip's HTML-escaping of untrusted service names) are shared with
///   `workspace_html::render_graph_panel` via [`crate::output::d3_force_graph`]
///   — see that module's docs.
/// - The node/link data itself is embedded inside a `<script>` element, a
///   different trust boundary: HTML-escaping alone does not stop a value
///   containing a literal `</script>` sequence from closing the element
///   early. That data is routed through [`crate::output::json_script::embed`]
///   instead — see that module's docs for why plain `serde_json::to_string`
///   (or, worse, raw `{:?}` Debug-format interpolation) is not sufficient.
pub fn render_html_graph(manifest: &Manifest, team: Option<&str>) -> String {
    let in_scope: std::collections::HashSet<&str> = manifest
        .services
        .iter()
        .filter(|s| match team {
            None => true,
            Some(t) => s
                .team
                .as_deref()
                .map(|v| v.eq_ignore_ascii_case(t))
                .unwrap_or(false),
        })
        .map(|s| s.name.as_str())
        .collect();

    let graph = build_d3_graph(manifest, &in_scope);
    let graph_json =
        json_script::embed(&graph).expect("D3Node/D3Link are plain strings and always serialize");

    let title = format!(
        "svccat graph - {} service(s)",
        manifest
            .services
            .iter()
            .filter(|s| in_scope.contains(s.name.as_str()))
            .count()
    );

    let script = d3_force_graph::render_script(&D3GraphConfig {
        svg_selector: "#graph",
        tooltip_id: "tooltip",
        arrow_id: "arrow",
        width_expr: "window.innerWidth",
        height_expr: "window.innerHeight - 60",
        color_field: "platform",
        node_radius: 16,
        text_dy: 28,
        link_distance: 120,
        charge_strength: -300,
        collide_radius: 40,
        tooltip_header_expr: "d.id",
        tooltip_fields: &[
            TooltipField {
                label: "platform",
                value_expr: "d.platform",
            },
            TooltipField {
                label: "team",
                value_expr: "d.team || \"-\"",
            },
            TooltipField {
                label: "lang",
                value_expr: "d.language || \"-\"",
            },
        ],
    });

    format!(
        r##"<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{title}</title>
<script src="https://cdn.jsdelivr.net/npm/d3@7/dist/d3.min.js"></script>
<style>
  body {{ margin: 0; background: #1a1a2e; color: #eee; font-family: sans-serif; }}
  h1 {{ text-align: center; padding: 1rem 0 0; font-size: 1.1rem; opacity: 0.7; }}
  svg {{ width: 100vw; height: calc(100vh - 60px); }}
  .node circle {{ stroke: #fff; stroke-width: 1.5px; cursor: pointer; }}
  .node text {{ font-size: 11px; fill: #eee; pointer-events: none; }}
  .link {{ stroke: #aaa; stroke-opacity: 0.5; stroke-width: 1.5px; }}
  #tooltip {{
    position: absolute; background: rgba(0,0,0,0.8); color: #fff;
    padding: 8px 12px; border-radius: 4px; font-size: 12px;
    pointer-events: none; display: none;
  }}
</style>
</head>
<body>
<h1>{title}</h1>
<div id="tooltip"></div>
<svg id="graph"></svg>
<script type="application/json" id="graph-data">{graph_json}</script>
<script>
const graphData = JSON.parse(document.getElementById("graph-data").textContent);
const nodes = graphData.nodes;
const links = graphData.links;

{script}
</script>
</body>
</html>
"##,
        title = title,
        graph_json = graph_json,
        script = script,
    )
}

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

    #[test]
    fn safe_id_normalizes_non_alphanumeric_chars() {
        assert_eq!(safe_id("api-gateway.v1"), "api_gateway_v1");
        assert_eq!(safe_id("service/name"), "service_name");
    }

    #[test]
    fn dot_escape_escapes_quotes_and_backslashes() {
        assert_eq!(dot_escape("a\\b"), "a\\\\b");
        assert_eq!(dot_escape("a\"b"), "a\\\"b");
    }

    #[test]
    fn build_label_includes_optional_fields() {
        let svc = crate::manifest::ServiceEntry {
            name: "api".to_string(),
            language: Some("Rust".to_string()),
            role: Some("Gateway".to_string()),
            ..Default::default()
        };
        assert_eq!(build_label(&svc), "api\\nRust\\nGateway");
    }

    #[test]
    fn malicious_service_name_in_graph_data_cannot_close_the_script_tag() {
        // Same vulnerability class PR #6 (commit 07b0485) fixed in
        // workspace_html.rs's D3 data island, and which json_script.rs's own
        // `script_breakout_attempt_is_neutralized` test proves the `embed`
        // helper neutralizes: this renderer used to build
        // nodes_json/links_json via raw `{:?}` Debug-format string
        // interpolation, which does NOT escape `<`, `>`, or `&`. A service,
        // team, platform, or language name containing a literal `</script>`
        // would close the surrounding `<script>` element early and inject
        // live markup into `svccat graph --format html` output.
        let manifest = Manifest {
            services: vec![crate::manifest::ServiceEntry {
                name: "</script><script>alert(1)</script>".to_string(),
                platform: Some("</script><script>alert(2)</script>".to_string()),
                team: Some("</script><script>alert(3)</script>".to_string()),
                language: Some("</script><script>alert(4)</script>".to_string()),
                ..Default::default()
            }],
            ..Default::default()
        };

        let html = render_html_graph(&manifest, None);

        // The raw, unescaped payload (as `{:?}` Debug-format interpolation
        // would have produced pre-fix) must not appear anywhere: that is
        // exactly the literal script-close/open sequence that terminates
        // the JSON `<script>` element early and starts a new, live one.
        assert!(
            !html
                .to_lowercase()
                .contains("</script><script>alert"),
            "a raw, unescaped payload must not survive as a literal script-close/open sequence: {html}"
        );

        // The escaped `<`/`>` form is present in the JSON data island for
        // every field, proving each survived intact but inert (mirrors
        // json_script.rs's script_breakout_attempt_is_neutralized and
        // workspace_html.rs's
        // malicious_service_name_in_graph_data_cannot_close_the_script_tag).
        for n in 1..=4 {
            assert!(
                html.contains(&format!("\\u003cscript\\u003ealert({n})")),
                "escaped payload {n} must survive intact in the JSON island: {html}"
            );
        }

        // The dependency graph's own D3 script now reads nodes/links from a
        // parsed JSON island rather than a raw string interpolation.
        assert!(html.contains(r#"id="graph-data">"#));
        assert!(html.contains(r#"JSON.parse(document.getElementById("graph-data").textContent)"#));
    }

    #[test]
    fn graph_data_json_island_round_trips_through_json_parse() {
        // Proves the new embed-based data path actually carries real node
        // and link data end to end, not just that it is "reachable" — the
        // escaping fix must not silently drop or corrupt legitimate data.
        let manifest = Manifest {
            services: vec![
                crate::manifest::ServiceEntry {
                    name: "api".to_string(),
                    platform: Some("Cloud Run".to_string()),
                    depends_on: vec!["db".to_string()],
                    ..Default::default()
                },
                crate::manifest::ServiceEntry {
                    name: "db".to_string(),
                    ..Default::default()
                },
            ],
            ..Default::default()
        };

        let html = render_html_graph(&manifest, None);
        let marker = r#"id="graph-data">"#;
        let start = html.find(marker).unwrap() + marker.len();
        let end = start + html[start..].find("</script>").unwrap();
        let parsed: serde_json::Value = serde_json::from_str(&html[start..end]).unwrap();

        assert_eq!(parsed["nodes"][0]["id"], "api");
        assert_eq!(parsed["nodes"][0]["platform"], "Cloud Run");
        assert_eq!(parsed["links"][0]["source"], "api");
        assert_eq!(parsed["links"][0]["target"], "db");
    }
}