taut-rpc-cli 0.1.0

cargo taut subcommand: TS codegen, IR drift detection, MCP manifest, and IR inspection for taut-rpc.
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
//! `cargo taut inspect` — render the IR for humans.
//!
//! See `ROADMAP.md` Phase 5.
//!
//! Three output flavours are supported:
//!
//! - `table` (default): two ASCII tables — one for procedures, one for types.
//!   No external table crate is pulled in; the columns are aligned by the
//!   maximum width of each column. This is the format you eyeball during
//!   development.
//! - `json`: `serde_json::to_string_pretty(&ir)`. Suitable for piping to `jq`.
//! - `mermaid`: a `flowchart LR` block whose nodes are procedures and whose
//!   edges go from a procedure to its input/output type references. Useful
//!   for embedding the API surface in a markdown doc.
//!
//! The IR can come from either an existing JSON file (`--ir <PATH>`, default
//! `target/taut/ir.json`) or by spawning a compiled user binary with
//! `TAUT_DUMP_IR` set (`--from-binary <PATH>`); the two flags are mutually
//! exclusive. The from-binary flow mirrors `cargo taut gen` so that an
//! `inspect` invocation Just Works without a separate dump step.

use std::path::{Path, PathBuf};
use std::process::Command;

use anyhow::{anyhow, bail, Context, Result};
use clap::{Args, ValueEnum};

use taut_rpc::ir::{
    EnumDef, Field, Ir, Primitive, ProcKind, Procedure, TypeDef, TypeRef, TypeShape, Variant,
    VariantPayload,
};

/// Output format for `cargo taut inspect`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
pub enum InspectFormat {
    /// Human-readable ASCII tables (default).
    Table,
    /// Pretty-printed JSON, suitable for piping into `jq`.
    Json,
    /// Mermaid `flowchart LR` block, suitable for embedding in markdown.
    Mermaid,
}

/// Arguments for `cargo taut inspect`.
#[derive(Debug, Args)]
#[command(
    about = "Render the IR as a human-readable table, JSON, or Mermaid diagram.",
    long_about = "Loads target/taut/ir.json (or the path passed to --ir, or the IR \
                  dumped from --from-binary) and prints procedures and types in \
                  a format suitable for eyeballing during development."
)]
pub struct InspectArgs {
    /// Path to the IR JSON file to render.
    #[arg(
        long,
        value_name = "PATH",
        default_value = "target/taut/ir.json",
        conflicts_with = "from_binary"
    )]
    pub ir: PathBuf,

    /// Path to a compiled user binary. The binary is spawned with
    /// `TAUT_DUMP_IR=<--ir path>` so it writes its IR via
    /// `taut_rpc::dump_if_requested` and exits before binding any port.
    #[arg(long, value_name = "PATH", conflicts_with = "ir")]
    pub from_binary: Option<PathBuf>,

    /// Output format: `table` (default), `json`, or `mermaid`.
    #[arg(long, value_name = "KIND", value_enum, default_value_t = InspectFormat::Table)]
    pub format: InspectFormat,
}

/// Entry point for `cargo taut inspect`.
#[allow(clippy::needless_pass_by_value)] // owned `args` matches the clap-generated dispatch convention
pub fn run(args: InspectArgs) -> Result<()> {
    let cwd = std::env::current_dir()?;
    let ir_abs = if args.ir.is_absolute() {
        args.ir.clone()
    } else {
        cwd.join(&args.ir)
    };

    if let Some(bin) = args.from_binary.as_ref() {
        let bin_abs = if bin.is_absolute() {
            bin.clone()
        } else {
            cwd.join(bin)
        };
        dump_ir_from_binary(&bin_abs, &ir_abs)?;
    }

    if !ir_abs.exists() {
        return Err(anyhow!(
            "IR file not found at {}.\n\
             Build and run your crate first so the proc-macros can dump the IR \
             (e.g. `cargo run` with `taut_rpc::dump_if_requested(&router)` in \
             your `main`), or pass `--from-binary <PATH>` to spawn it for you.",
            ir_abs.display()
        ));
    }

    let raw = std::fs::read_to_string(&ir_abs)
        .with_context(|| format!("reading IR from {}", ir_abs.display()))?;
    let ir: Ir = serde_json::from_str(&raw)
        .with_context(|| format!("parsing IR JSON at {}", ir_abs.display()))?;

    let rendered = match args.format {
        InspectFormat::Table => render_table(&ir),
        InspectFormat::Json => render_json(&ir)?,
        InspectFormat::Mermaid => render_mermaid(&ir),
    };

    print!("{rendered}");
    if !rendered.ends_with('\n') {
        println!();
    }
    Ok(())
}

/// Spawn `bin` with `TAUT_DUMP_IR=<ir_target>` and wait for it to exit.
///
/// Mirrors the helper in `commands::gen` so that `cargo taut inspect
/// --from-binary <PATH>` works without a separate dump step.
fn dump_ir_from_binary(bin: &Path, ir_target: &Path) -> Result<()> {
    if !bin.exists() {
        bail!("--from-binary path does not exist: {}", bin.display());
    }

    if let Some(parent) = ir_target.parent() {
        if !parent.as_os_str().is_empty() {
            std::fs::create_dir_all(parent).with_context(|| {
                format!("failed to create IR output directory: {}", parent.display())
            })?;
        }
    }

    let output = Command::new(bin)
        .env("TAUT_DUMP_IR", ir_target)
        .output()
        .with_context(|| format!("failed to spawn {}", bin.display()))?;

    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        let code = output
            .status
            .code()
            .map_or_else(|| "<terminated by signal>".to_string(), |c| c.to_string());
        return Err(anyhow!(
            "binary {} exited with status {} while dumping IR\n--- stderr ---\n{}",
            bin.display(),
            code,
            stderr.trim_end()
        ));
    }

    if !ir_target.exists() {
        bail!(
            "binary {} exited successfully but did not write IR to {}; \
             does its main() call `taut_rpc::dump_if_requested(&router)` \
             before any port binding?",
            bin.display(),
            ir_target.display()
        );
    }

    Ok(())
}

// ---------------------------------------------------------------------------
// JSON
// ---------------------------------------------------------------------------

fn render_json(ir: &Ir) -> Result<String> {
    serde_json::to_string_pretty(ir).context("serializing IR to JSON")
}

// ---------------------------------------------------------------------------
// Mermaid
// ---------------------------------------------------------------------------

/// Render the IR as a `flowchart LR` block.
///
/// Each procedure becomes a node labelled `<name> / <kind>`. Each procedure
/// gets two outgoing edges, one for its input typeref and one for its output,
/// labelled accordingly. The right-hand side of the edge is a *terminal* node
/// — the rendered `TypeRef` — not a procedure-style node, so primitive sinks
/// (e.g. `string`, `User`) collapse together when reused across procedures.
fn render_mermaid(ir: &Ir) -> String {
    use std::fmt::Write as _;
    let mut out = String::new();
    out.push_str("flowchart LR\n");

    if ir.procedures.is_empty() {
        out.push_str("    %% no procedures registered\n");
        return out;
    }

    for p in &ir.procedures {
        let node = format!(
            "{name}[{name} / {kind}]",
            name = p.name,
            kind = kind_label(&p.kind)
        );
        let input = render_typeref_short(&p.input);
        let output = render_typeref_short(&p.output);
        let _ = writeln!(out, "    {node} -->|input| {input}");
        let _ = writeln!(out, "    {name} -->|output| {output}", name = p.name);
        for err in &p.errors {
            let _ = writeln!(
                out,
                "    {name} -->|error| {err}",
                name = p.name,
                err = render_typeref_short(err),
            );
        }
    }

    out
}

// ---------------------------------------------------------------------------
// Table
// ---------------------------------------------------------------------------

/// Render the IR as two ASCII tables: procedures, then types.
fn render_table(ir: &Ir) -> String {
    let mut out = String::new();

    out.push_str("Procedures\n");
    out.push_str("----------\n");
    if ir.procedures.is_empty() {
        out.push_str("no procedures registered\n");
    } else {
        out.push_str(&render_proc_table(&ir.procedures));
    }
    out.push('\n');

    out.push_str("Types\n");
    out.push_str("-----\n");
    if ir.types.is_empty() {
        out.push_str("no types defined\n");
    } else {
        out.push_str(&render_type_table(&ir.types));
    }

    out
}

fn render_proc_table(procs: &[Procedure]) -> String {
    let header = ["Name", "Kind", "Method", "Input", "Output", "Errors"];
    let mut rows: Vec<[String; 6]> = Vec::with_capacity(procs.len());
    for p in procs {
        let errors = if p.errors.is_empty() {
            "-".to_string()
        } else {
            p.errors
                .iter()
                .map(render_typeref_short)
                .collect::<Vec<_>>()
                .join(", ")
        };
        rows.push([
            p.name.clone(),
            kind_label(&p.kind).to_string(),
            method_label(&p.http_method).to_string(),
            render_typeref_short(&p.input),
            render_typeref_short(&p.output),
            errors,
        ]);
    }
    render_grid(&header, &rows)
}

fn render_type_table(types: &[TypeDef]) -> String {
    let header = ["Name", "Shape", "Fields"];
    let mut rows: Vec<[String; 3]> = Vec::with_capacity(types.len());
    for t in types {
        let (shape, fields) = describe_shape(&t.shape);
        rows.push([t.name.clone(), shape, fields]);
    }
    render_grid(&header, &rows)
}

/// Render a single ASCII grid given column headers and rows.
///
/// Columns are left-aligned and padded to the max width seen in that column
/// (header included). A separator line of `-` follows the header. Cells with
/// embedded newlines are left as-is (Mermaid/JSON paths don't go through
/// here, and we deliberately keep cells single-line for procs/types tables).
fn render_grid<const N: usize>(header: &[&str; N], rows: &[[String; N]]) -> String {
    let mut widths: [usize; N] = [0; N];
    for (i, h) in header.iter().enumerate() {
        widths[i] = h.len();
    }
    for row in rows {
        for (i, cell) in row.iter().enumerate() {
            if cell.len() > widths[i] {
                widths[i] = cell.len();
            }
        }
    }

    let mut out = String::new();
    push_row(&mut out, header.iter().copied(), &widths);
    push_separator(&mut out, &widths);
    for row in rows {
        push_row(&mut out, row.iter().map(String::as_str), &widths);
    }
    out
}

fn push_row<'a, I, const N: usize>(out: &mut String, cells: I, widths: &[usize; N])
where
    I: IntoIterator<Item = &'a str>,
{
    let mut first = true;
    for (i, cell) in cells.into_iter().enumerate() {
        if !first {
            out.push_str(" | ");
        }
        first = false;
        out.push_str(cell);
        // Pad to the column width with spaces. We don't pad the last column
        // since trailing whitespace would just bloat the output.
        if i + 1 < N {
            for _ in cell.len()..widths[i] {
                out.push(' ');
            }
        }
    }
    out.push('\n');
}

fn push_separator<const N: usize>(out: &mut String, widths: &[usize; N]) {
    let mut first = true;
    for (i, w) in widths.iter().enumerate() {
        if !first {
            out.push_str("-+-");
        }
        first = false;
        for _ in 0..*w {
            out.push('-');
        }
        // Match `push_row`: don't extend trailing whitespace past the last col.
        let _ = i;
    }
    out.push('\n');
}

// ---------------------------------------------------------------------------
// Shared helpers
// ---------------------------------------------------------------------------

fn kind_label(k: &ProcKind) -> &'static str {
    match k {
        ProcKind::Query => "query",
        ProcKind::Mutation => "mutation",
        ProcKind::Subscription => "subscription",
    }
}

fn method_label(m: &taut_rpc::ir::HttpMethod) -> &'static str {
    match m {
        taut_rpc::ir::HttpMethod::Post => "POST",
        taut_rpc::ir::HttpMethod::Get => "GET",
    }
}

fn primitive_label(p: Primitive) -> &'static str {
    match p {
        Primitive::Bool => "bool",
        Primitive::U8 => "u8",
        Primitive::U16 => "u16",
        Primitive::U32 => "u32",
        Primitive::U64 => "u64",
        Primitive::I8 => "i8",
        Primitive::I16 => "i16",
        Primitive::I32 => "i32",
        Primitive::I64 => "i64",
        Primitive::U128 => "u128",
        Primitive::I128 => "i128",
        Primitive::F32 => "f32",
        Primitive::F64 => "f64",
        Primitive::String => "string",
        Primitive::Bytes => "bytes",
        Primitive::Unit => "()",
        Primitive::DateTime => "DateTime",
        Primitive::Uuid => "Uuid",
    }
}

/// Render a [`TypeRef`] as a compact human-readable string, roughly mirroring
/// Rust syntax. This is the form used in both the table cells and the right-
/// hand side of mermaid edges. It is *not* TypeScript — that's
/// `taut_rpc::type_map::render_type`'s job.
fn render_typeref_short(t: &TypeRef) -> String {
    match t {
        TypeRef::Primitive(p) => primitive_label(*p).to_string(),
        TypeRef::Named(name) => name.clone(),
        TypeRef::Option(inner) => format!("Option<{}>", render_typeref_short(inner)),
        TypeRef::Vec(inner) => format!("Vec<{}>", render_typeref_short(inner)),
        TypeRef::Map { key, value } => format!(
            "Map<{}, {}>",
            render_typeref_short(key),
            render_typeref_short(value)
        ),
        TypeRef::Tuple(elems) => {
            if elems.is_empty() {
                "()".to_string()
            } else {
                let inner: Vec<String> = elems.iter().map(render_typeref_short).collect();
                format!("({})", inner.join(", "))
            }
        }
        TypeRef::FixedArray { elem, len } => {
            format!("[{}; {}]", render_typeref_short(elem), len)
        }
    }
}

/// Describe a [`TypeShape`] as a `(shape_label, fields_summary)` pair for the
/// types table.
fn describe_shape(s: &TypeShape) -> (String, String) {
    match s {
        TypeShape::Struct(fields) => ("struct".to_string(), describe_fields(fields)),
        TypeShape::Enum(EnumDef { tag, variants }) => {
            (format!("enum (tag={tag})"), describe_variants(variants))
        }
        TypeShape::Tuple(elems) => {
            let inner: Vec<String> = elems.iter().map(render_typeref_short).collect();
            ("tuple".to_string(), format!("({})", inner.join(", ")))
        }
        TypeShape::Newtype(inner) => ("newtype".to_string(), render_typeref_short(inner)),
        TypeShape::Alias(inner) => ("alias".to_string(), render_typeref_short(inner)),
    }
}

fn describe_fields(fields: &[Field]) -> String {
    if fields.is_empty() {
        return "(empty)".to_string();
    }
    fields
        .iter()
        .map(|f| {
            let opt = if f.optional { "?" } else { "" };
            format!("{}{}: {}", f.name, opt, render_typeref_short(&f.ty))
        })
        .collect::<Vec<_>>()
        .join(", ")
}

fn describe_variants(variants: &[Variant]) -> String {
    if variants.is_empty() {
        return "(empty)".to_string();
    }
    variants
        .iter()
        .map(|v| match &v.payload {
            VariantPayload::Unit => v.name.clone(),
            VariantPayload::Tuple(elems) => {
                let inner: Vec<String> = elems.iter().map(render_typeref_short).collect();
                format!("{}({})", v.name, inner.join(", "))
            }
            VariantPayload::Struct(fields) => {
                format!("{} {{ {} }}", v.name, describe_fields(fields))
            }
        })
        .collect::<Vec<_>>()
        .join(" | ")
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    fn sample_ir() -> Ir {
        Ir {
            ir_version: Ir::CURRENT_VERSION,
            procedures: vec![
                Procedure {
                    name: "create_user".to_string(),
                    kind: ProcKind::Mutation,
                    input: TypeRef::Named("CreateUser".to_string()),
                    output: TypeRef::Named("User".to_string()),
                    errors: vec![TypeRef::Named("UserError".to_string())],
                    http_method: HttpMethod::Post,
                    doc: None,
                },
                Procedure {
                    name: "ping".to_string(),
                    kind: ProcKind::Query,
                    input: TypeRef::Primitive(Primitive::Unit),
                    output: TypeRef::Primitive(Primitive::String),
                    errors: vec![],
                    http_method: HttpMethod::Get,
                    doc: None,
                },
            ],
            types: vec![
                TypeDef {
                    name: "User".to_string(),
                    doc: None,
                    shape: TypeShape::Struct(vec![
                        Field {
                            name: "id".to_string(),
                            ty: TypeRef::Primitive(Primitive::U64),
                            optional: false,
                            undefined: false,
                            doc: None,
                            constraints: vec![],
                        },
                        Field {
                            name: "name".to_string(),
                            ty: TypeRef::Primitive(Primitive::String),
                            optional: false,
                            undefined: false,
                            doc: None,
                            constraints: vec![],
                        },
                    ]),
                },
                TypeDef {
                    name: "CreateUser".to_string(),
                    doc: None,
                    shape: TypeShape::Struct(vec![Field {
                        name: "name".to_string(),
                        ty: TypeRef::Primitive(Primitive::String),
                        optional: false,
                        undefined: false,
                        doc: None,
                        constraints: vec![],
                    }]),
                },
                TypeDef {
                    name: "UserError".to_string(),
                    doc: None,
                    shape: TypeShape::Enum(EnumDef {
                        tag: "type".to_string(),
                        variants: vec![
                            Variant {
                                name: "NotFound".to_string(),
                                payload: VariantPayload::Unit,
                            },
                            Variant {
                                name: "Conflict".to_string(),
                                payload: VariantPayload::Tuple(vec![TypeRef::Primitive(
                                    Primitive::String,
                                )]),
                            },
                        ],
                    }),
                },
            ],
        }
    }

    #[test]
    fn table_format_renders_procedures_and_types() {
        let ir = sample_ir();
        let out = render_table(&ir);

        // Section headers
        assert!(
            out.contains("Procedures"),
            "missing Procedures header:\n{out}"
        );
        assert!(out.contains("Types"), "missing Types header:\n{out}");

        // Column headers for proc table
        for col in ["Name", "Kind", "Method", "Input", "Output", "Errors"] {
            assert!(out.contains(col), "missing proc column {col}:\n{out}");
        }
        // Column headers for type table
        for col in ["Shape", "Fields"] {
            assert!(out.contains(col), "missing type column {col}:\n{out}");
        }

        // Procedure rows
        assert!(out.contains("create_user"), "missing proc name:\n{out}");
        assert!(out.contains("mutation"), "missing kind:\n{out}");
        assert!(out.contains("POST"), "missing method:\n{out}");
        assert!(out.contains("CreateUser"), "missing input typeref:\n{out}");
        assert!(out.contains("UserError"), "missing error typeref:\n{out}");
        assert!(out.contains("ping"), "missing second proc:\n{out}");
        assert!(out.contains("query"), "missing query kind:\n{out}");
        assert!(out.contains("GET"), "missing GET method:\n{out}");

        // Type rows
        assert!(out.contains("struct"), "missing struct shape:\n{out}");
        assert!(out.contains("id: u64"), "missing field rendering:\n{out}");
        assert!(out.contains("enum"), "missing enum shape:\n{out}");
        assert!(out.contains("NotFound"), "missing variant:\n{out}");
        assert!(
            out.contains("Conflict(string)"),
            "missing tuple variant:\n{out}"
        );

        // Empty IR uses placeholder messages
        let empty = render_table(&Ir::empty());
        assert!(empty.contains("no procedures registered"), "got:\n{empty}");
        assert!(empty.contains("no types defined"), "got:\n{empty}");

        // Alignment sanity check: every non-empty line of the procedures
        // section under the header has the same number of '|' separators
        // (5 separators -> 6 columns).
        let proc_block = out.split("\nTypes").next().expect("Procedures section");
        let pipe_counts: Vec<usize> = proc_block
            .lines()
            .skip(2) // "Procedures" + "----------"
            .filter(|l| !l.is_empty() && !l.starts_with('-'))
            .map(|l| l.matches('|').count())
            .collect();
        assert!(
            pipe_counts.iter().all(|&c| c == 5),
            "pipe counts not uniform: {pipe_counts:?}\nblock:\n{proc_block}"
        );
    }

    #[test]
    fn json_format_pretty_prints() {
        let ir = sample_ir();
        let out = render_json(&ir).expect("render json");

        // Pretty-printed JSON has newlines and 2-space indent.
        assert!(out.contains('\n'), "expected pretty JSON, got: {out}");
        assert!(
            out.contains("  \"ir_version\""),
            "expected indented ir_version, got: {out}"
        );
        assert!(out.contains("\"create_user\""), "missing proc name: {out}");
        assert!(out.contains("\"User\""), "missing type name: {out}");

        // Round-trips: pretty JSON parses back to the same IR.
        let back: Ir = serde_json::from_str(&out).expect("parse back");
        assert_eq!(back, ir);
    }

    #[test]
    fn mermaid_format_emits_flowchart_block() {
        let ir = sample_ir();
        let out = render_mermaid(&ir);

        assert!(
            out.starts_with("flowchart LR\n"),
            "missing flowchart header:\n{out}"
        );

        // Procedure node + edges per the spec example. The first edge for a
        // procedure carries the node-label syntax (`create_user[create_user /
        // mutation]`), subsequent edges reuse the bare node id.
        assert!(
            out.contains("create_user[create_user / mutation] -->|input| CreateUser"),
            "missing input edge:\n{out}"
        );
        assert!(
            out.contains("create_user -->|output| User"),
            "missing output edge:\n{out}"
        );
        assert!(
            out.contains("create_user -->|error| UserError"),
            "missing error edge:\n{out}"
        );
        assert!(
            out.contains("ping[ping / query] -->|input|"),
            "missing second proc node:\n{out}"
        );
        assert!(
            out.contains("ping -->|output| string"),
            "missing second proc output edge:\n{out}"
        );

        // Empty IR still produces a valid (if degenerate) flowchart block.
        let empty = render_mermaid(&Ir::empty());
        assert!(empty.starts_with("flowchart LR\n"), "got:\n{empty}");
        assert!(
            empty.contains("no procedures registered"),
            "missing empty placeholder:\n{empty}"
        );
    }
}