mecha10-cli 0.6.3

Mecha10 CLI tool
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
//! Generates `docs/schemas/` (LAB-1829) from this repo's real schemas: the
//! hand-written project-config schema (`mecha10_core::schema_validation::get_project_schema`,
//! with its `behaviors` property overridden by the real `schemars`-derived
//! `BehaviorsConfig` schema -- see [`project_schema_with_introspected_behaviors`])
//! and the `schemars`-derived per-node-type/model-catalog schemas
//! (`mecha10_schema_registry`). See `docs/validate-configs.md` for how these schemas
//! are used to validate configs; this module is purely about rendering them as
//! reference docs — it adds no new schema content and hand-writes no field
//! descriptions (those come from the schemas themselves, which pull node-config
//! field docs from `packages/nodes/*/src/config.rs`'s doc comments via `schemars`).
//!
//! Every file this writes is generated output: the raw schema JSON (one file per
//! schema) plus a single Markdown field reference. `scripts/ci/schema-docs-check.sh`
//! re-runs this and diffs against what's committed, so `docs/schemas/` can never
//! silently drift from the schemas it documents.

use anyhow::{Context, Result};
use serde_json::Value;
use std::collections::BTreeSet;
use std::fmt::Write as _;
use std::fs;
use std::path::{Path, PathBuf};

/// One schema this service exports: a relative path (under the export root) for
/// its raw JSON, a title used in the generated Markdown, and the schema itself.
struct ExportedSchema {
    /// Path relative to the export root, e.g. `"mecha10.schema.json"` or
    /// `"nodes/camera.schema.json"`.
    relative_json_path: PathBuf,
    /// Heading used for this schema's section in the generated Markdown.
    title: String,
    schema: Value,
}

/// Generates `docs/schemas/`-style reference docs (raw JSON Schema files plus a
/// Markdown field reference) from every schema this CLI knows about. See the
/// module-level docs for what "knows about" means and why nothing here
/// hand-writes schema content.
pub struct SchemaExportService;

impl SchemaExportService {
    /// Generate every schema this CLI knows about and write it under `out_dir`:
    /// raw JSON Schema files plus a single `README.md` field reference. Returns
    /// the list of files written (relative to `out_dir`), for callers that want
    /// to report what changed.
    ///
    /// # Errors
    ///
    /// Returns an error if `out_dir` can't be created, or a file can't be written.
    pub fn export(out_dir: &Path) -> Result<Vec<PathBuf>> {
        export_schemas(out_dir)
    }
}

/// Generate every schema this CLI knows about and write it under `out_dir`:
/// raw JSON Schema files plus a single `README.md` field reference. Returns the
/// list of files written (relative to `out_dir`), for callers that want to report
/// what changed.
///
/// # Errors
///
/// Returns an error if `out_dir` can't be created, or a file can't be written.
fn export_schemas(out_dir: &Path) -> Result<Vec<PathBuf>> {
    let schemas = collect_schemas();
    let mut written = Vec::new();

    for exported in &schemas {
        let path = out_dir.join(&exported.relative_json_path);
        if let Some(parent) = path.parent() {
            fs::create_dir_all(parent).with_context(|| format!("Failed to create directory {}", parent.display()))?;
        }
        let pretty = serde_json::to_string_pretty(&exported.schema)
            .with_context(|| format!("Failed to serialize schema for {}", exported.title))?;
        fs::write(&path, format!("{pretty}\n")).with_context(|| format!("Failed to write {}", path.display()))?;
        written.push(exported.relative_json_path.clone());
    }

    let readme_path = out_dir.join("README.md");
    fs::create_dir_all(out_dir).with_context(|| format!("Failed to create directory {}", out_dir.display()))?;
    fs::write(&readme_path, render_readme(&schemas)).context("Failed to write docs/schemas/README.md")?;
    written.push(PathBuf::from("README.md"));

    Ok(written)
}

/// Every schema `mecha10 schema export` knows about, in the order they should
/// appear in the generated Markdown.
fn collect_schemas() -> Vec<ExportedSchema> {
    let mut schemas = vec![ExportedSchema {
        relative_json_path: PathBuf::from("mecha10.schema.json"),
        title: "Project config (mecha10.json)".to_string(),
        schema: project_schema_with_introspected_behaviors(),
    }];

    for node_type in mecha10_schema_registry::BUILTIN_NODE_TYPES.iter() {
        let schema = mecha10_schema_registry::node_type_schema(node_type).unwrap_or_else(|| {
            panic!("mecha10-schema-registry declares '{node_type}' as built-in but has no schema for it")
        });
        schemas.push(ExportedSchema {
            relative_json_path: PathBuf::from("nodes").join(format!("{node_type}.schema.json")),
            title: format!("Node config: {node_type}"),
            schema,
        });
    }

    schemas.push(ExportedSchema {
        relative_json_path: PathBuf::from("model-catalog.schema.json"),
        title: "Model catalog (model_catalog.toml)".to_string(),
        schema: mecha10_schema_registry::model_catalog_schema(),
    });

    schemas
}

/// `mecha10_core::schema_validation::get_project_schema()`'s `behaviors` property
/// is a hand-written JSON Schema fragment, not derived from
/// `mecha10_cli_core::types::project::BehaviorsConfig` -- `mecha10-core` can't
/// depend on `mecha10-cli-core` (the reverse dependency already exists, see
/// `mecha10-cli-core`'s own doc comments), so the real struct isn't reachable
/// from where that schema is built, unlike node configs (see
/// `mecha10-schema-registry`, which lives downstream of every node crate for the
/// same reason).
///
/// That hand-written fragment silently drifted from the real struct for its
/// entire existence (it said `default` where the struct has always had
/// `active`) and survived two prior `docs/schemas/` regenerations (LAB-1829)
/// before an unrelated PR happened to fix its text. This crate *can* depend on
/// `mecha10-cli-core` (it already does, for other CLI functionality), so this
/// splices the real `schemars`-derived schema for `BehaviorsConfig` into the
/// `behaviors` property here -- after this point, `docs/schemas/` reflects the
/// real struct and can't silently drift from it again, even though the
/// hand-written fragment in `mecha10-core` (used for runtime validation, where
/// the dependency direction can't be worked around the same way) remains as a
/// separately-maintained copy.
fn project_schema_with_introspected_behaviors() -> Value {
    let mut schema = mecha10_core::schema_validation::get_project_schema();
    let behaviors_schema = schema_for::<mecha10_cli_core::types::project::BehaviorsConfig>();
    if let Some(properties) = schema.get_mut("properties").and_then(Value::as_object_mut) {
        properties.insert("behaviors".to_string(), behaviors_schema);
    }
    schema
}

/// The `schemars`-derived JSON Schema for `T`, as a `serde_json::Value` (the
/// shape [`ExportedSchema`] and every render helper in this module works with).
fn schema_for<T: schemars::JsonSchema>() -> Value {
    let root_schema = schemars::SchemaGenerator::default().into_root_schema_for::<T>();
    serde_json::to_value(root_schema).expect("schemars Schema always serializes to JSON")
}

/// Render the full `README.md`: one section per schema, each with a field table
/// plus a subsection per nested `definitions` entry it references.
fn render_readme(schemas: &[ExportedSchema]) -> String {
    let mut out = String::new();
    out.push_str("# Schema reference\n\n");
    out.push_str(
        "Generated from this repo's JSON Schemas by `mecha10 schema export` (LAB-1829) — \
         do not hand-edit. Regenerate with:\n\n```\ncargo run -p mecha10-cli --bin mecha10 -- schema export\n```\n\n\
         Descriptions come from the schemas themselves (Rust doc comments on node config \
         structs, via `schemars`, or `get_project_schema()`'s own `description` fields for \
         the project config). See [`docs/validate-configs.md`](../validate-configs.md) for how \
         these schemas are used to validate configs, and the raw `.schema.json` files \
         alongside this doc for the exact JSON Schema.\n\n",
    );

    out.push_str("## Contents\n\n");
    for exported in schemas {
        let anchor = slugify(&exported.title);
        let _ = writeln!(out, "- [{}](#{anchor})", exported.title);
    }
    out.push('\n');

    for exported in schemas {
        let _ = writeln!(out, "## {}\n", exported.title);
        if let Some(description) = exported.schema.get("description").and_then(Value::as_str) {
            let _ = writeln!(out, "{description}\n");
        }
        let _ = writeln!(
            out,
            "Raw schema: [`{}`]({}).\n",
            exported.relative_json_path.display(),
            exported.relative_json_path.display()
        );

        render_schema_body(&exported.schema, &exported.schema, &mut out, &mut BTreeSet::new());
    }

    out
}

/// One more schema to render as its own subsection: either a named
/// `definitions` entry (referenced via `$ref`), or an anonymous inline object
/// schema (no `$ref` of its own — e.g. an array's `items`, embedded directly
/// rather than hoisted to `definitions`, as `model_catalog_schema()` does for
/// `ModelCatalogEntry` — see its doc comment for why). Anonymous sections are
/// titled after the field that introduced them.
struct PendingSection {
    title: String,
    schema: Value,
}

/// Render one schema's body: its own field table (if it's an object schema, or a
/// "Variants" breakdown if it's a `oneOf`/`anyOf`), followed by a subsection per
/// `definitions` entry and anonymous nested object it (transitively) references.
/// `visited` dedupes sections reached from more than one place so each is only
/// rendered once.
fn render_schema_body(root: &Value, schema: &Value, out: &mut String, visited: &mut BTreeSet<String>) {
    let mut pending: Vec<PendingSection> = Vec::new();
    render_value_body(schema, out, &mut pending);

    // Named `definitions` are collected up front (rather than only as
    // encountered while walking properties) so every one gets documented even
    // if it's only reachable indirectly (e.g. referenced solely from another
    // definition), and in a stable (sorted) order.
    if let Some(definitions) = root.get("definitions").and_then(Value::as_object) {
        let mut names: Vec<&String> = definitions.keys().collect();
        names.sort();
        for name in names {
            pending.push(PendingSection {
                title: name.clone(),
                schema: definitions[name].clone(),
            });
        }
    }

    // Anonymous nested objects are only discovered by walking properties (they
    // have no root-level registry to enumerate up front), so this drains
    // breadth-first, including sections newly queued while rendering others.
    let mut index = 0;
    while index < pending.len() {
        if visited.insert(pending[index].title.clone()) {
            let _ = writeln!(out, "### {}\n", pending[index].title);
            if let Some(description) = pending[index].schema.get("description").and_then(Value::as_str) {
                let _ = writeln!(out, "{description}\n");
            }
            let schema_to_render = pending[index].schema.clone();
            render_value_body(&schema_to_render, out, &mut pending);
        }
        index += 1;
    }
}

/// Render a single schema value's field table or variant breakdown — no heading,
/// callers add their own. Queues any anonymous inline nested object schema it
/// finds (a property, or an array property's `items`, with its own `properties`
/// but no `$ref`) onto `pending` for [`render_schema_body`] to render as its own
/// subsection.
fn render_value_body(schema: &Value, out: &mut String, pending: &mut Vec<PendingSection>) {
    if let Some(properties) = schema.get("properties").and_then(Value::as_object) {
        let required: BTreeSet<&str> = schema
            .get("required")
            .and_then(Value::as_array)
            .map(|values| values.iter().filter_map(Value::as_str).collect())
            .unwrap_or_default();

        let mut names: Vec<&String> = properties.keys().collect();
        names.sort();

        out.push_str("| Field | Type | Required | Description |\n");
        out.push_str("| --- | --- | --- | --- |\n");
        for name in names {
            let field_schema = &properties[name];
            let type_label = type_label(field_schema);
            let is_required = if required.contains(name.as_str()) { "yes" } else { "no" };
            let description = field_description(field_schema).unwrap_or_default();
            let _ = writeln!(out, "| `{name}` | {type_label} | {is_required} | {description} |");
            if let Some(nested) = anonymous_nested_object(name, field_schema) {
                pending.push(nested);
            }
        }
        out.push('\n');
        return;
    }

    if let Some(variants) = schema
        .get("oneOf")
        .or_else(|| schema.get("anyOf"))
        .and_then(Value::as_array)
    {
        out.push_str("One of the following variants:\n\n");
        for (index, variant) in variants.iter().enumerate() {
            let label = variant_label(variant, index);
            let _ = writeln!(out, "- **{label}**");
        }
        out.push('\n');
        for (index, variant) in variants.iter().enumerate() {
            // A variant that's just `{"enum": [...]}` (a plain string-enum
            // member, e.g. one member of a C-like enum split across `oneOf`
            // by schemars so each member keeps its own doc comment) or a bare
            // `{"$ref": ...}`/`{"type": "null"}` (an `Option<T>` represented
            // as `anyOf: [T, null]`) is already fully captured by its bullet
            // above -- the referenced type gets its own section anyway, and
            // there's no additional body to render.
            let is_bare_reference_or_null = (variant.get("$ref").is_some()
                || variant.get("type").and_then(Value::as_str) == Some("null"))
                && variant.get("properties").is_none();
            if is_bare_reference_or_null || (variant.get("enum").is_some() && variant.get("properties").is_none()) {
                continue;
            }
            let label = variant_label(variant, index);
            let _ = writeln!(out, "**{label}**\n");
            render_value_body(variant, out, pending);
        }
        return;
    }

    if let Some(items) = schema.get("enum").and_then(Value::as_array) {
        let values: Vec<String> = items.iter().map(display_scalar).collect();
        let _ = writeln!(out, "Allowed values: {}\n", values.join(", "));
    }
}

/// If `field_schema` (or, for an array field, its `items`) is an anonymous
/// inline object schema — has its own `properties` but isn't a `$ref`/`allOf`-
/// wrapped reference to a named `definitions` entry — return a [`PendingSection`]
/// for it, titled after `field_name` (or the schema's own `title`, if it has
/// one). This is what documents e.g. `model_catalog_schema()`'s `models[]` items
/// (`ModelCatalogEntry`, embedded inline rather than hoisted to `definitions` —
/// see that function's doc comment for why), which a `$ref`-only walk would
/// otherwise silently skip.
fn anonymous_nested_object(field_name: &str, field_schema: &Value) -> Option<PendingSection> {
    let candidate = if field_schema.get("type").and_then(Value::as_str) == Some("array") {
        field_schema.get("items")?
    } else {
        field_schema
    };

    if candidate.get("$ref").is_some() {
        return None;
    }
    if let Some(all_of) = candidate.get("allOf").and_then(Value::as_array) {
        if all_of.iter().any(|s| s.get("$ref").is_some()) {
            return None;
        }
    }
    if candidate.get("oneOf").is_some() || candidate.get("anyOf").is_some() {
        return None;
    }

    let has_properties = candidate
        .get("properties")
        .and_then(Value::as_object)
        .is_some_and(|props| !props.is_empty());
    if !has_properties {
        return None;
    }

    let title = candidate
        .get("title")
        .and_then(Value::as_str)
        .map(str::to_string)
        .unwrap_or_else(|| field_name.to_string());
    Some(PendingSection {
        title,
        schema: candidate.clone(),
    })
}

/// A human label for one `oneOf`/`anyOf` variant: its serde-tag discriminant value
/// if present (`properties.type.enum[0]`, the common internally-tagged-enum shape),
/// its own `enum` value if it's a plain string-enum member, else its `title`, else
/// a positional fallback.
fn variant_label(variant: &Value, index: usize) -> String {
    if let Some(tag) = variant
        .get("properties")
        .and_then(|p| p.get("type"))
        .and_then(|t| t.get("enum"))
        .and_then(Value::as_array)
        .and_then(|values| values.first())
        .and_then(Value::as_str)
    {
        return format!("`{tag}`");
    }
    if let Some(tag) = variant
        .get("enum")
        .and_then(Value::as_array)
        .and_then(|values| values.first())
        .and_then(Value::as_str)
    {
        return format!("`{tag}`");
    }
    if let Some(title) = variant.get("title").and_then(Value::as_str) {
        return title.to_string();
    }
    if variant.get("type").and_then(Value::as_str) == Some("null") {
        return "`null`".to_string();
    }
    if let Some(reference) = variant.get("$ref").and_then(Value::as_str) {
        return ref_type_label(reference);
    }
    format!("variant {index}")
}

/// The description for a property schema, accounting for the `{"description":
/// ..., "allOf": [{"$ref": ...}]}` wrapping `schemars` uses when a field has its
/// own doc comment on top of a `$ref` (draft-07 doesn't allow sibling keywords
/// next to `$ref` directly).
fn field_description(schema: &Value) -> Option<String> {
    schema
        .get("description")
        .and_then(Value::as_str)
        .map(|s| s.replace('\n', " ").trim().to_string())
        .filter(|s| !s.is_empty())
}

/// A short type label for a field/variant schema: resolves `$ref`/`allOf`-wrapped
/// refs to the referenced definition's name, arrays to `array of <item type>`,
/// and falls back to the JSON Schema `type` keyword otherwise.
fn type_label(schema: &Value) -> String {
    if let Some(reference) = schema.get("$ref").and_then(Value::as_str) {
        return ref_type_label(reference);
    }
    if let Some(all_of) = schema.get("allOf").and_then(Value::as_array) {
        if let Some(reference) = all_of.iter().find_map(|s| s.get("$ref")).and_then(Value::as_str) {
            return ref_type_label(reference);
        }
    }
    if let Some(one_of) = schema
        .get("oneOf")
        .or_else(|| schema.get("anyOf"))
        .and_then(Value::as_array)
    {
        let labels: Vec<String> = one_of.iter().map(type_label).collect();
        return format!("one of: {}", labels.join(", "));
    }
    if schema.get("type").and_then(Value::as_str) == Some("array") {
        let item_label = schema.get("items").map_or_else(|| "any".to_string(), type_label);
        return format!("array of {item_label}");
    }
    // An anonymous inline object (no `$ref`) still has its own subsection
    // rendered by `anonymous_nested_object`/`render_schema_body`, titled after
    // its own `title` if it has one — match that here (ahead of the generic
    // `type` match below, which would otherwise just say "object") so the type
    // column points a reader at the right heading.
    if schema.get("properties").is_some() {
        return schema
            .get("title")
            .and_then(Value::as_str)
            .map_or_else(|| "object".to_string(), |title| format!("`{title}`"));
    }
    match schema.get("type") {
        Some(Value::String(t)) => t.clone(),
        Some(Value::Array(types)) => types.iter().filter_map(Value::as_str).collect::<Vec<_>>().join(" or "),
        _ if schema.get("enum").is_some() => "enum".to_string(),
        _ => "any".to_string(),
    }
}

/// The definition name a `#/definitions/<Name>` ref points at, formatted for
/// display (backtick-quoted so it reads as a type name, not prose).
fn ref_type_label(reference: &str) -> String {
    let name = reference.rsplit('/').next().unwrap_or(reference);
    format!("`{name}`")
}

fn display_scalar(value: &Value) -> String {
    match value {
        Value::String(s) => format!("`{s}`"),
        other => format!("`{other}`"),
    }
}

/// GitHub-flavored-Markdown heading-anchor slug for the table of contents links
/// (lowercase, spaces to hyphens, parens dropped).
fn slugify(title: &str) -> String {
    title
        .to_lowercase()
        .chars()
        .filter_map(|c| {
            if c.is_alphanumeric() || c == ' ' || c == '-' {
                Some(if c == ' ' { '-' } else { c })
            } else {
                None
            }
        })
        .collect()
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;
    use tempfile::tempdir;

    #[test]
    fn type_label_resolves_plain_primitive() {
        let schema = json!({"type": "string"});
        assert_eq!(type_label(&schema), "string");
    }

    #[test]
    fn type_label_resolves_direct_ref() {
        let schema = json!({"$ref": "#/definitions/SourceConfig"});
        assert_eq!(type_label(&schema), "`SourceConfig`");
    }

    #[test]
    fn type_label_resolves_all_of_wrapped_ref() {
        let schema = json!({
            "description": "Frame source configuration",
            "allOf": [{"$ref": "#/definitions/SourceConfig"}]
        });
        assert_eq!(type_label(&schema), "`SourceConfig`");
    }

    #[test]
    fn type_label_resolves_array_of_ref() {
        let schema = json!({"type": "array", "items": {"$ref": "#/definitions/ModelCatalogEntry"}});
        assert_eq!(type_label(&schema), "array of `ModelCatalogEntry`");
    }

    #[test]
    fn field_description_reads_plain_description() {
        let schema = json!({"type": "string", "description": "Camera ID for identification"});
        assert_eq!(
            field_description(&schema).as_deref(),
            Some("Camera ID for identification")
        );
    }

    #[test]
    fn field_description_reads_all_of_wrapped_description() {
        let schema = json!({
            "description": "Frame source configuration",
            "allOf": [{"$ref": "#/definitions/SourceConfig"}]
        });
        assert_eq!(
            field_description(&schema).as_deref(),
            Some("Frame source configuration")
        );
    }

    #[test]
    fn render_value_body_produces_a_field_table_with_required_column() {
        let root = json!({
            "type": "object",
            "required": ["name"],
            "properties": {
                "name": {"type": "string", "description": "The name"},
                "note": {"type": "string", "description": "Optional note"}
            }
        });
        let mut out = String::new();
        render_value_body(&root, &mut out, &mut Vec::new());
        assert!(out.contains("| `name` | string | yes | The name |"));
        assert!(out.contains("| `note` | string | no | Optional note |"));
    }

    #[test]
    fn render_value_body_queues_anonymous_nested_array_item_object() {
        let root = json!({
            "type": "object",
            "properties": {
                "models": {
                    "type": "array",
                    "items": {
                        "type": "object",
                        "properties": {"name": {"type": "string", "description": "Model name"}}
                    }
                }
            }
        });
        let mut out = String::new();
        render_schema_body(&root, &root, &mut out, &mut BTreeSet::new());
        assert!(out.contains("### models"));
        assert!(out.contains("| `name` | string | no | Model name |"));
    }

    #[test]
    fn render_schema_body_documents_referenced_definitions_once() {
        let root = json!({
            "type": "object",
            "properties": {
                "source": {"allOf": [{"$ref": "#/definitions/SourceConfig"}]}
            },
            "definitions": {
                "SourceConfig": {
                    "type": "object",
                    "properties": {"path": {"type": "string"}}
                }
            }
        });
        let mut out = String::new();
        render_schema_body(&root, &root, &mut out, &mut BTreeSet::new());
        assert_eq!(out.matches("### SourceConfig").count(), 1);
        assert!(out.contains("| `path` | string | no |"));
    }

    #[test]
    fn project_schema_behaviors_property_reflects_the_real_behaviors_config_struct() {
        // Regression guard (see `project_schema_with_introspected_behaviors`'s doc
        // comment): `get_project_schema()`'s hand-written `behaviors` fragment said
        // `default` where the real `BehaviorsConfig` struct has always had `active`,
        // and that wrong field name survived two prior `docs/schemas/` regenerations
        // because nothing tied the hand-written fragment to the real struct. This
        // pins the exported schema's `behaviors` property to the real struct's shape
        // so it can't drift like that again.
        let schema = project_schema_with_introspected_behaviors();
        let behaviors = &schema["properties"]["behaviors"];

        assert_eq!(behaviors["title"], "BehaviorsConfig");
        assert!(
            behaviors["properties"].get("active").is_some(),
            "expected `behaviors.properties.active`, got: {behaviors}"
        );
        assert!(
            behaviors["properties"].get("default").is_none(),
            "`behaviors` schema still has a stale `default` field: {behaviors}"
        );
        let required: Vec<&str> = behaviors["required"]
            .as_array()
            .expect("BehaviorsConfig schema should have a `required` array")
            .iter()
            .filter_map(Value::as_str)
            .collect();
        assert_eq!(required, vec!["active"]);
    }

    #[test]
    fn variant_label_prefers_serde_tag_discriminant() {
        let variant = json!({
            "type": "object",
            "properties": {"type": {"enum": ["mock"]}}
        });
        assert_eq!(variant_label(&variant, 0), "`mock`");
    }

    #[test]
    fn export_schemas_writes_json_and_readme() {
        let dir = tempdir().unwrap();
        let written = export_schemas(dir.path()).unwrap();

        assert!(written.contains(&PathBuf::from("mecha10.schema.json")));
        assert!(written.contains(&PathBuf::from("model-catalog.schema.json")));
        assert!(written.contains(&PathBuf::from("nodes/camera.schema.json")));
        assert!(written.contains(&PathBuf::from("README.md")));

        assert!(dir.path().join("mecha10.schema.json").exists());
        assert!(dir.path().join("nodes/camera.schema.json").exists());
        assert!(dir.path().join("model-catalog.schema.json").exists());

        let readme = fs::read_to_string(dir.path().join("README.md")).unwrap();
        assert!(readme.contains("# Schema reference"));
        assert!(readme.contains("## Project config (mecha10.json)"));
        assert!(readme.contains("## Node config: camera"));
        assert!(readme.contains("## Model catalog (model_catalog.toml)"));
        // The model catalog embeds `ModelCatalogEntry` inline (not via a named
        // `definitions` entry — see `model_catalog_schema()`'s doc comment), so
        // this also guards that its fields still surface as their own
        // subsection (titled after the struct's own schemars-generated
        // `title`) rather than being silently skipped.
        assert!(readme.contains("### ModelCatalogEntry"));
        assert!(readme.contains("| `name` |"));
    }

    #[test]
    fn export_schemas_covers_every_builtin_node_type() {
        let dir = tempdir().unwrap();
        export_schemas(dir.path()).unwrap();
        for node_type in mecha10_schema_registry::BUILTIN_NODE_TYPES.iter() {
            let path = dir.path().join("nodes").join(format!("{node_type}.schema.json"));
            assert!(
                path.exists(),
                "expected schema file for '{node_type}' at {}",
                path.display()
            );
        }
    }

    #[test]
    fn export_schemas_output_is_deterministic() {
        let dir_a = tempdir().unwrap();
        let dir_b = tempdir().unwrap();
        export_schemas(dir_a.path()).unwrap();
        export_schemas(dir_b.path()).unwrap();

        let readme_a = fs::read_to_string(dir_a.path().join("README.md")).unwrap();
        let readme_b = fs::read_to_string(dir_b.path().join("README.md")).unwrap();
        assert_eq!(readme_a, readme_b);

        let schema_a = fs::read_to_string(dir_a.path().join("mecha10.schema.json")).unwrap();
        let schema_b = fs::read_to_string(dir_b.path().join("mecha10.schema.json")).unwrap();
        assert_eq!(schema_a, schema_b);
    }
}