zenith-core 0.0.7

Zenith core: KDL parser adapter, semantic AST, canonical formatter, tokens, validation, and diagnostics.
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
//! Integration tests for the `chart` node: parse, format, and round-trip —
//! including two `series` children — plus the absent-chart byte-identical guarantee.

mod common;

use common::*;
use zenith_core::format::format_document;

/// **Chart parse + format + round-trip (with two series)**: a `chart` with the
/// chart-specific props (kind/title/caption/legend/axis-min/axis-max/axis-style),
/// geometry, fill, and two `series` children parses into the expected `ChartNode`
/// (series data intact), formats back out preserving everything, and survives a
/// format → re-parse round-trip (spans stripped).
#[test]
fn chart_parse_format_round_trip_with_series() {
    // NOTE: the '#' inside color strings requires r##...## quoting.
    let src = r##"zenith version=1 {
  project id="proj.chart" name="Chart"
  tokens format="zenith-token-v1" {
    token id="color.bar" type="color" value="#334155"
  }
  styles {
  }
  document id="doc.chart" title="Chart" {
    page id="page.chart" w=(px)800 h=(px)600 {
      chart id="c.sales" kind="bar" x=(px)50 y=(px)50 w=(px)600 h=(px)400 title="Sales" legend=#true {
        series 12.0 24.0 18.0 label="Q1"
        series 30.0 15.0 22.0 label="Q2" color=(token)"color.bar"
      }
    }
  }
}
"##;
    let adapter = KdlAdapter;
    let doc = adapter.parse(src.as_bytes()).expect("parse must succeed");

    let chart = match &doc.body.pages[0].children[0] {
        Node::Chart(c) => c,
        other => panic!("expected Chart node, got {other:?}"),
    };
    assert_eq!(chart.id, "c.sales");
    assert_eq!(chart.kind, "bar");
    assert_eq!(chart.title, Some("Sales".to_owned()));
    assert_eq!(chart.legend, Some(true));
    assert_eq!(
        chart.x,
        Some(PropertyValue::Dimension(Dimension {
            value: 50.0,
            unit: Unit::Px
        }))
    );
    assert_eq!(
        chart.w,
        Some(PropertyValue::Dimension(Dimension {
            value: 600.0,
            unit: Unit::Px
        }))
    );
    assert_eq!(chart.series.len(), 2);
    assert_eq!(chart.series[0].label, Some("Q1".to_owned()));
    assert_eq!(chart.series[0].values, vec![12.0, 24.0, 18.0]);
    assert_eq!(chart.series[0].color, None);
    assert_eq!(chart.series[1].label, Some("Q2".to_owned()));
    assert_eq!(chart.series[1].values, vec![30.0, 15.0, 22.0]);
    assert_eq!(chart.series[1].color, Some(token_ref("color.bar")));

    // The formatter emits the chart-specific props and the series block.
    let formatted = format_document(&doc).expect("format must succeed");
    let formatted_str = String::from_utf8(formatted.clone()).expect("formatted must be utf8");
    assert!(
        formatted_str.contains("chart id=\"c.sales\" kind=\"bar\""),
        "formatter must emit chart id + kind; got:\n{formatted_str}"
    );
    assert!(
        formatted_str.contains("title=\"Sales\""),
        "formatter must emit title; got:\n{formatted_str}"
    );
    assert!(
        formatted_str.contains("series"),
        "formatter must emit series children; got:\n{formatted_str}"
    );

    // Round-trip: re-parse equals the first parse (spans stripped).
    let reparsed = adapter.parse(&formatted).expect("re-parse after format");
    assert_eq!(
        strip_spans(doc),
        strip_spans(reparsed),
        "chart (with series) must round-trip identically"
    );
}

/// **Categories + bar-mode round-trip**: a `chart` with a `categories` child
/// and `bar-mode="stacked"` and two series parses correctly, formats the
/// `categories` line BEFORE the series lines, and re-parses to the same AST.
#[test]
fn chart_categories_and_bar_mode_round_trip() {
    // NOTE: '#' inside strings requires r##...## quoting.
    let src = r##"zenith version=1 {
  project id="proj.cat" name="CatChart"
  tokens format="zenith-token-v1" {
    token id="color.a" type="color" value="#112233"
  }
  styles {
  }
  document id="doc.cat" title="CatChart" {
    page id="page.cat" w=(px)800 h=(px)600 {
      chart id="c.cat" kind="bar" bar-mode="stacked" x=(px)50 y=(px)50 w=(px)600 h=(px)400 {
        categories "Q1" "Q2" "Q3"
        series 10.0 20.0 30.0 label="A"
        series 5.0 15.0 25.0 label="B" color=(token)"color.a"
      }
    }
  }
}
"##;
    let adapter = KdlAdapter;
    let doc = adapter.parse(src.as_bytes()).expect("parse must succeed");

    let chart = match &doc.body.pages[0].children[0] {
        Node::Chart(c) => c,
        other => panic!("expected Chart node, got {other:?}"),
    };
    assert_eq!(chart.id, "c.cat");
    assert_eq!(chart.bar_mode, Some("stacked".to_owned()));
    assert_eq!(
        chart.categories,
        vec!["Q1".to_owned(), "Q2".to_owned(), "Q3".to_owned()]
    );
    assert_eq!(chart.series.len(), 2);
    assert_eq!(chart.series[0].values, vec![10.0, 20.0, 30.0]);
    assert_eq!(chart.series[1].color, Some(token_ref("color.a")));

    let formatted = format_document(&doc).expect("format must succeed");
    let formatted_str = String::from_utf8(formatted.clone()).expect("formatted must be utf8");

    // categories line must appear before the first series line.
    let cat_pos = formatted_str
        .find("categories")
        .expect("must emit categories");
    let series_pos = formatted_str.find("series").expect("must emit series");
    assert!(
        cat_pos < series_pos,
        "categories must be emitted before series; got:\n{formatted_str}"
    );
    assert!(
        formatted_str.contains("bar-mode=\"stacked\""),
        "formatter must emit bar-mode; got:\n{formatted_str}"
    );
    assert!(
        formatted_str.contains("\"Q1\""),
        "formatter must emit Q1 category label; got:\n{formatted_str}"
    );

    // Round-trip: re-parse equals the first parse (spans stripped).
    let reparsed = adapter.parse(&formatted).expect("re-parse after format");
    assert_eq!(
        strip_spans(doc),
        strip_spans(reparsed),
        "chart (with categories + bar-mode) must round-trip identically"
    );
}

/// **No categories / no bar-mode = byte-identical**: a chart without those
/// fields must not emit `categories` or `bar-mode` text in the formatted output,
/// and must still round-trip.
#[test]
fn chart_without_categories_bar_mode_byte_identical() {
    let src = r##"zenith version=1 {
  project id="proj.plain" name="PlainChart"
  styles {
  }
  document id="doc.plain" title="PlainChart" {
    page id="page.plain" w=(px)800 h=(px)600 {
      chart id="c.plain" kind="bar" x=(px)50 y=(px)50 w=(px)600 h=(px)400 {
        series 1.0 2.0 3.0 label="S"
      }
    }
  }
}
"##;
    let adapter = KdlAdapter;
    let doc = adapter.parse(src.as_bytes()).expect("parse must succeed");

    let chart = match &doc.body.pages[0].children[0] {
        Node::Chart(c) => c,
        other => panic!("expected Chart node, got {other:?}"),
    };
    assert!(chart.categories.is_empty(), "categories must be empty");
    assert_eq!(chart.bar_mode, None, "bar_mode must be None");

    let formatted = format_document(&doc).expect("format must succeed");
    let formatted_str = String::from_utf8(formatted.clone()).expect("formatted must be utf8");

    assert!(
        !formatted_str.contains("categories"),
        "no categories field must not emit 'categories'; got:\n{formatted_str}"
    );
    assert!(
        !formatted_str.contains("bar-mode"),
        "no bar-mode field must not emit 'bar-mode'; got:\n{formatted_str}"
    );

    let reparsed = adapter.parse(&formatted).expect("re-parse after format");
    assert_eq!(
        strip_spans(doc),
        strip_spans(reparsed),
        "plain chart (no categories, no bar-mode) must round-trip identically"
    );
}

/// **point-placement + value-labels + value-color round-trip**: a chart with all
/// three new fields parses correctly and round-trips through format → re-parse
/// to an identical AST.
#[test]
fn chart_point_placement_value_labels_value_color_round_trip() {
    // NOTE: '#' inside color hex requires r##...## quoting.
    let src = r##"zenith version=1 {
  project id="proj.pp" name="PointPlacement"
  tokens format="zenith-token-v1" {
    token id="color.label" type="color" value="#334455"
  }
  styles {
  }
  document id="doc.pp" title="PointPlacement" {
    page id="page.pp" w=(px)800 h=(px)600 {
      chart id="c.pp" kind="line" point-placement="edge" value-labels="center" value-color=(token)"color.label" x=(px)50 y=(px)50 w=(px)600 h=(px)400 {
        series 10.0 20.0 30.0 label="A"
      }
    }
  }
}
"##;
    let adapter = KdlAdapter;
    let doc = adapter.parse(src.as_bytes()).expect("parse must succeed");

    let chart = match &doc.body.pages[0].children[0] {
        Node::Chart(c) => c,
        other => panic!("expected Chart node, got {other:?}"),
    };
    assert_eq!(chart.id, "c.pp");
    assert_eq!(
        chart.point_placement,
        Some("edge".to_owned()),
        "point_placement must be parsed"
    );
    assert_eq!(
        chart.value_labels,
        Some("center".to_owned()),
        "value_labels must be parsed"
    );
    assert_eq!(
        chart.value_color,
        Some(token_ref("color.label")),
        "value_color must be parsed as TokenRef"
    );

    let formatted = format_document(&doc).expect("format must succeed");
    let formatted_str = String::from_utf8(formatted.clone()).expect("formatted must be utf8");

    assert!(
        formatted_str.contains("point-placement=\"edge\""),
        "formatter must emit point-placement; got:\n{formatted_str}"
    );
    assert!(
        formatted_str.contains("value-labels=\"center\""),
        "formatter must emit value-labels; got:\n{formatted_str}"
    );
    assert!(
        formatted_str.contains("value-color=(token)\"color.label\""),
        "formatter must emit value-color; got:\n{formatted_str}"
    );

    // Round-trip: re-parse equals the first parse (spans stripped).
    let reparsed = adapter.parse(&formatted).expect("re-parse after format");
    assert_eq!(
        strip_spans(doc),
        strip_spans(reparsed),
        "chart (with point-placement + value-labels + value-color) must round-trip identically"
    );
}

/// **New fields absent = byte-identical**: a chart without point-placement,
/// value-labels, or value-color must not emit those keywords in the formatter
/// output, and must still round-trip.
#[test]
fn chart_without_new_fields_byte_identical() {
    let src = r##"zenith version=1 {
  project id="proj.nf" name="NoNewFields"
  styles {
  }
  document id="doc.nf" title="NoNewFields" {
    page id="page.nf" w=(px)800 h=(px)600 {
      chart id="c.nf" kind="bar" x=(px)50 y=(px)50 w=(px)600 h=(px)400 {
        series 1.0 2.0 3.0 label="S"
      }
    }
  }
}
"##;
    let adapter = KdlAdapter;
    let doc = adapter.parse(src.as_bytes()).expect("parse must succeed");

    let chart = match &doc.body.pages[0].children[0] {
        Node::Chart(c) => c,
        other => panic!("expected Chart node, got {other:?}"),
    };
    assert_eq!(chart.point_placement, None, "point_placement must be None");
    assert_eq!(chart.value_labels, None, "value_labels must be None");
    assert_eq!(chart.value_color, None, "value_color must be None");

    let formatted = format_document(&doc).expect("format must succeed");
    let formatted_str = String::from_utf8(formatted.clone()).expect("formatted must be utf8");

    assert!(
        !formatted_str.contains("point-placement"),
        "absent point-placement must not be emitted; got:\n{formatted_str}"
    );
    assert!(
        !formatted_str.contains("value-labels"),
        "absent value-labels must not be emitted; got:\n{formatted_str}"
    );
    assert!(
        !formatted_str.contains("value-color"),
        "absent value-color must not be emitted; got:\n{formatted_str}"
    );

    let reparsed = adapter.parse(&formatted).expect("re-parse after format");
    assert_eq!(
        strip_spans(doc),
        strip_spans(reparsed),
        "chart without new fields must round-trip identically"
    );
}

/// **Absent chart is byte-identical**: a document that uses NO `chart` node
/// formats exactly as it did before the feature existed (additive guarantee).
#[test]
fn absent_chart_byte_identical() {
    // NOTE: '#' in color hex requires r##...## quoting.
    let src = r##"zenith version=1 {
  project id="proj.nc" name="NoChart"
  tokens format="zenith-token-v1" {
    token id="color.fill" type="color" value="#112233"
  }
  styles {
  }
  document id="doc.nc" title="NoChart" {
    page id="page.nc" w=(px)400 h=(px)300 {
      rect id="r.one" x=(px)0 y=(px)0 w=(px)100 h=(px)100 fill=(token)"color.fill"
    }
  }
}
"##;
    let adapter = KdlAdapter;
    let doc = adapter.parse(src.as_bytes()).expect("parse must succeed");
    let formatted = format_document(&doc).expect("format must succeed");
    let formatted_str = String::from_utf8(formatted).expect("formatted must be utf8");
    assert!(
        !formatted_str.contains("chart"),
        "a document without a chart must not emit the keyword; got:\n{formatted_str}"
    );
}

/// **label-colors child + series label-color round-trip**: a pie chart with a
/// `label-colors` child (two token refs) and a series with `label-color` parses
/// correctly, formats those fields, and re-parses to an identical AST.
#[test]
fn chart_label_colors_and_series_label_color_round_trip() {
    // NOTE: '#' in color hex requires r##...## quoting.
    let src = r##"zenith version=1 {
  project id="proj.lc" name="LabelColors"
  tokens format="zenith-token-v1" {
    token id="c.a" type="color" value="#ff0000"
    token id="c.b" type="color" value="#00ff00"
    token id="c.s" type="color" value="#0000ff"
  }
  styles {
  }
  document id="doc.lc" title="LabelColors" {
    page id="page.lc" w=(px)800 h=(px)600 {
      chart id="c.pie" kind="pie" x=(px)50 y=(px)50 w=(px)400 h=(px)400 {
        label-colors (token)"c.a" (token)"c.b"
        series label="Slice" label-color=(token)"c.s" 60.0 40.0
      }
    }
  }
}
"##;
    let adapter = KdlAdapter;
    let doc = adapter.parse(src.as_bytes()).expect("parse must succeed");

    let chart = match &doc.body.pages[0].children[0] {
        Node::Chart(c) => c,
        other => panic!("expected Chart node, got {other:?}"),
    };
    assert_eq!(chart.id, "c.pie");
    assert_eq!(
        chart.label_colors,
        vec![token_ref("c.a"), token_ref("c.b")],
        "label_colors must be parsed as two TokenRefs"
    );
    assert_eq!(chart.series.len(), 1);
    assert_eq!(
        chart.series[0].label_color,
        Some(token_ref("c.s")),
        "series label_color must be parsed as TokenRef"
    );

    let formatted = format_document(&doc).expect("format must succeed");
    let formatted_str = String::from_utf8(formatted.clone()).expect("formatted must be utf8");

    assert!(
        formatted_str.contains("label-colors (token)\"c.a\" (token)\"c.b\""),
        "formatter must emit label-colors child; got:\n{formatted_str}"
    );
    assert!(
        formatted_str.contains("label-color=(token)\"c.s\""),
        "formatter must emit series label-color; got:\n{formatted_str}"
    );

    // label-colors must appear before the series line.
    let lc_pos = formatted_str
        .find("label-colors")
        .expect("must emit label-colors");
    let series_pos = formatted_str.find("series").expect("must emit series");
    assert!(
        lc_pos < series_pos,
        "label-colors must be emitted before series; got:\n{formatted_str}"
    );

    // Round-trip: re-parse equals the first parse (spans stripped).
    let reparsed = adapter.parse(&formatted).expect("re-parse after format");
    assert_eq!(
        strip_spans(doc),
        strip_spans(reparsed),
        "chart (with label-colors + series label-color) must round-trip identically"
    );
}

/// **label-colors and series label-color absent = byte-identical**: a chart without
/// those fields must not emit `label-colors` or `label-color` text, and must
/// still round-trip.
#[test]
fn chart_without_label_colors_byte_identical() {
    let src = r##"zenith version=1 {
  project id="proj.nlc" name="NoLabelColors"
  styles {
  }
  document id="doc.nlc" title="NoLabelColors" {
    page id="page.nlc" w=(px)800 h=(px)600 {
      chart id="c.nlc" kind="pie" x=(px)50 y=(px)50 w=(px)400 h=(px)400 {
        series label="A" 60.0 40.0
      }
    }
  }
}
"##;
    let adapter = KdlAdapter;
    let doc = adapter.parse(src.as_bytes()).expect("parse must succeed");

    let chart = match &doc.body.pages[0].children[0] {
        Node::Chart(c) => c,
        other => panic!("expected Chart node, got {other:?}"),
    };
    assert!(
        chart.label_colors.is_empty(),
        "label_colors must be empty when absent"
    );
    assert_eq!(
        chart.series[0].label_color, None,
        "series label_color must be None when absent"
    );

    let formatted = format_document(&doc).expect("format must succeed");
    let formatted_str = String::from_utf8(formatted.clone()).expect("formatted must be utf8");

    assert!(
        !formatted_str.contains("label-colors"),
        "absent label-colors must not be emitted; got:\n{formatted_str}"
    );
    assert!(
        !formatted_str.contains("label-color"),
        "absent series label-color must not be emitted; got:\n{formatted_str}"
    );

    let reparsed = adapter.parse(&formatted).expect("re-parse after format");
    assert_eq!(
        strip_spans(doc),
        strip_spans(reparsed),
        "chart without label-colors must round-trip identically"
    );
}

/// **legend-position + legend-layout + legend-align round-trip**: a chart with
/// all three new legend configuration props parses correctly and round-trips
/// through format → re-parse to an identical AST.
#[test]
fn chart_legend_position_layout_align_round_trip() {
    let src = r##"zenith version=1 {
  project id="proj.leg" name="LegendConfig"
  styles {
  }
  document id="doc.leg" title="LegendConfig" {
    page id="page.leg" w=(px)800 h=(px)600 {
      chart id="c.leg" kind="bar" legend-position="bottom" legend-layout="wrapped" legend-align="left" x=(px)50 y=(px)50 w=(px)600 h=(px)400 {
        series 1.0 2.0 3.0 label="A"
      }
    }
  }
}
"##;
    let adapter = KdlAdapter;
    let doc = adapter.parse(src.as_bytes()).expect("parse must succeed");

    let chart = match &doc.body.pages[0].children[0] {
        Node::Chart(c) => c,
        other => panic!("expected Chart node, got {other:?}"),
    };
    assert_eq!(chart.id, "c.leg");
    assert_eq!(
        chart.legend_position,
        Some("bottom".to_owned()),
        "legend_position must be parsed"
    );
    assert_eq!(
        chart.legend_layout,
        Some("wrapped".to_owned()),
        "legend_layout must be parsed"
    );
    assert_eq!(
        chart.legend_align,
        Some("left".to_owned()),
        "legend_align must be parsed"
    );

    let formatted = format_document(&doc).expect("format must succeed");
    let formatted_str = String::from_utf8(formatted.clone()).expect("formatted must be utf8");

    assert!(
        formatted_str.contains("legend-position=\"bottom\""),
        "formatter must emit legend-position; got:\n{formatted_str}"
    );
    assert!(
        formatted_str.contains("legend-layout=\"wrapped\""),
        "formatter must emit legend-layout; got:\n{formatted_str}"
    );
    assert!(
        formatted_str.contains("legend-align=\"left\""),
        "formatter must emit legend-align; got:\n{formatted_str}"
    );

    let reparsed = adapter.parse(&formatted).expect("re-parse after format");
    assert_eq!(
        strip_spans(doc),
        strip_spans(reparsed),
        "chart (with legend-position + legend-layout + legend-align) must round-trip identically"
    );
}

/// **Legend props absent = byte-identical**: a chart without legend-position,
/// legend-layout, or legend-align must not emit those keywords in the formatter
/// output, and must still round-trip.
#[test]
fn chart_without_legend_props_byte_identical() {
    let src = r##"zenith version=1 {
  project id="proj.nleg" name="NoLegendProps"
  styles {
  }
  document id="doc.nleg" title="NoLegendProps" {
    page id="page.nleg" w=(px)800 h=(px)600 {
      chart id="c.nleg" kind="bar" x=(px)50 y=(px)50 w=(px)600 h=(px)400 {
        series 1.0 2.0 3.0 label="S"
      }
    }
  }
}
"##;
    let adapter = KdlAdapter;
    let doc = adapter.parse(src.as_bytes()).expect("parse must succeed");

    let chart = match &doc.body.pages[0].children[0] {
        Node::Chart(c) => c,
        other => panic!("expected Chart node, got {other:?}"),
    };
    assert_eq!(chart.legend_position, None, "legend_position must be None");
    assert_eq!(chart.legend_layout, None, "legend_layout must be None");
    assert_eq!(chart.legend_align, None, "legend_align must be None");

    let formatted = format_document(&doc).expect("format must succeed");
    let formatted_str = String::from_utf8(formatted.clone()).expect("formatted must be utf8");

    assert!(
        !formatted_str.contains("legend-position"),
        "absent legend-position must not be emitted; got:\n{formatted_str}"
    );
    assert!(
        !formatted_str.contains("legend-layout"),
        "absent legend-layout must not be emitted; got:\n{formatted_str}"
    );
    assert!(
        !formatted_str.contains("legend-align"),
        "absent legend-align must not be emitted; got:\n{formatted_str}"
    );

    let reparsed = adapter.parse(&formatted).expect("re-parse after format");
    assert_eq!(
        strip_spans(doc),
        strip_spans(reparsed),
        "chart without legend props must round-trip identically"
    );
}

/// **Chart orientation round-trip**: a bar chart with `orientation="horizontal"`
/// formats with the prop present and re-parses byte-identically (spans stripped).
#[test]
fn chart_orientation_horizontal_round_trips() {
    let src = r#"zenith version=1 {
  project id="proj.ori" name="Orientation"
  styles {
  }
  document id="doc.ori" title="Orientation" {
    page id="page.ori" w=(px)800 h=(px)600 {
      chart id="c.h" kind="bar" x=(px)0 y=(px)0 w=(px)400 h=(px)300 orientation="horizontal" {
        series 10.0 20.0 label="A"
      }
    }
  }
}
"#;
    let adapter = KdlAdapter;
    let doc = adapter.parse(src.as_bytes()).expect("parse must succeed");

    let chart = match &doc.body.pages[0].children[0] {
        Node::Chart(c) => c,
        other => panic!("expected Chart node, got {other:?}"),
    };
    assert_eq!(
        chart.orientation,
        Some("horizontal".to_owned()),
        "orientation must parse to Some(\"horizontal\")"
    );

    let formatted = format_document(&doc).expect("format must succeed");
    let formatted_str = String::from_utf8(formatted.clone()).expect("formatted must be utf8");
    assert!(
        formatted_str.contains("orientation=\"horizontal\""),
        "formatter must emit orientation=\"horizontal\"; got:\n{formatted_str}"
    );

    let reparsed = adapter.parse(&formatted).expect("re-parse after format");
    assert_eq!(
        strip_spans(doc),
        strip_spans(reparsed),
        "chart with orientation must round-trip identically"
    );
}

/// **Chart without orientation is byte-identical**: a chart that does not set
/// `orientation` must not emit the prop at all, keeping existing docs byte-stable.
#[test]
fn chart_absent_orientation_emits_nothing() {
    let src = r#"zenith version=1 {
  project id="proj.nori" name="NoOrientation"
  styles {
  }
  document id="doc.nori" title="NoOrientation" {
    page id="page.nori" w=(px)800 h=(px)600 {
      chart id="c.plain" kind="bar" x=(px)0 y=(px)0 w=(px)400 h=(px)300 {
        series 5.0 10.0 label="S"
      }
    }
  }
}
"#;
    let adapter = KdlAdapter;
    let doc = adapter.parse(src.as_bytes()).expect("parse must succeed");

    let chart = match &doc.body.pages[0].children[0] {
        Node::Chart(c) => c,
        other => panic!("expected Chart node, got {other:?}"),
    };
    assert_eq!(
        chart.orientation, None,
        "orientation must be None when absent"
    );

    let formatted = format_document(&doc).expect("format must succeed");
    let formatted_str = String::from_utf8(formatted.clone()).expect("formatted must be utf8");
    assert!(
        !formatted_str.contains("orientation"),
        "formatter must NOT emit orientation when absent; got:\n{formatted_str}"
    );

    let reparsed = adapter.parse(&formatted).expect("re-parse after format");
    assert_eq!(
        strip_spans(doc),
        strip_spans(reparsed),
        "chart without orientation must round-trip identically"
    );
}