zenith-core 0.0.5

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
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
//! Integration tests: geometry validation.
//!
//! Test bodies moved verbatim from the former in-`src` `validate/check/tests/`
//! concern files; only import paths changed (`crate::`/`super::common` ->
//! `zenith_core::`/`common`).

use std::collections::BTreeMap;

mod common;

use common::*;

// ── Bonus: stroke-width with dimension token (correct type) ──────────

#[test]
fn stroke_width_with_dimension_token_is_clean() {
    let doc = doc_with(
        vec![dim_token("size.stroke")],
        vec![minimal_page(
            "page.one",
            vec![Node::Rect(Box::new(RectNode {
                shadow: None,
                filter: None,
                mask: None,
                id: "rect.one".to_owned(),
                name: None,
                role: None,
                x: Some(pxv(0.0)),
                y: Some(pxv(0.0)),
                w: Some(pxv(50.0)),
                h: Some(pxv(50.0)),
                radius: None,
                radius_tl: None,
                radius_tr: None,
                radius_br: None,
                radius_bl: None,
                style: None,
                fill: None,
                stroke: None,
                stroke_width: Some(token_ref("size.stroke")),
                stroke_alignment: None,
                stroke_dash: None,
                stroke_gap: None,
                stroke_linecap: None,
                border_top: None,
                border_bottom: None,
                border_left: None,
                border_right: None,
                border_width: None,
                stroke_outer: None,
                stroke_outer_width: None,
                opacity: None,
                visible: None,
                locked: None,
                rotate: None,
                blend_mode: None,
                blur: None,
                anchor: None,
                anchor_zone: None,
                anchor_sibling: None,
                anchor_edge: None,
                anchor_gap: None,
                anchor_parent: None,
                source_span: None,
                unknown_props: BTreeMap::new(),
            }))],
        )],
    );
    let report = validate(&doc);
    assert!(
        report.diagnostics.is_empty(),
        "expected no diagnostics, got: {:?}",
        codes(&report)
    );
}

// ── Bonus: font-family on text node ────────────────────────────────────

#[test]
fn text_font_family_with_font_family_token_is_clean() {
    let doc = doc_with(
        vec![font_family_token("font.body")],
        vec![minimal_page(
            "page.one",
            vec![Node::Text(Box::new(TextNode {
                shadow: None,
                filter: None,
                mask: None,
                id: "text.one".to_owned(),
                name: None,
                role: None,
                x: Some(pxv(0.0)),
                y: Some(pxv(0.0)),
                w: Some(pxv(200.0)),
                h: Some(pxv(40.0)),
                align: None,
                v_align: None,
                direction: None,
                overflow: None,
                overflow_wrap: None,
                style: None,
                fill: None,
                stroke: None,
                stroke_width: None,
                contrast_bg: None,
                font_family: Some(token_ref("font.body")),
                font_size: None,
                font_size_min: None,
                font_weight: None,
                opacity: None,
                visible: None,
                locked: None,
                selectable: None,
                rotate: None,
                blend_mode: None,
                blur: None,
                chain: None,
                drop_cap_lines: None,
                hyphenate: None,
                widow_orphan: None,
                tab_leader: None,
                text_exclusion: None,
                padding_left: None,
                text_indent: None,
                content_format: None,
                src: None,
                bullet: None,
                bullet_gap: None,
                anchor: None,
                anchor_zone: None,
                anchor_sibling: None,
                anchor_edge: None,
                anchor_gap: None,
                anchor_parent: None,
                spans: vec![],
                block_styles: Vec::new(),
                source_span: None,
                unknown_props: BTreeMap::new(),
            }))],
        )],
    );
    let report = validate(&doc);
    assert!(
        report.diagnostics.is_empty(),
        "expected no diagnostics, got: {:?}",
        codes(&report)
    );
}

// ── Ellipse: clean doc produces no errors ─────────────────────────────

#[test]
fn ellipse_clean_doc_no_errors() {
    let doc = doc_with(
        vec![color_token("color.fill")],
        vec![minimal_page(
            "page.one",
            vec![minimal_ellipse(
                "ellipse.one",
                Some(token_ref("color.fill")),
            )],
        )],
    );
    let report = validate(&doc);
    assert!(
        report.diagnostics.is_empty(),
        "expected no diagnostics for clean ellipse doc, got: {:?}",
        codes(&report)
    );
    assert!(!report.has_errors());
}

// ── Ellipse: missing geometry → node.missing_geometry ─────────────────

#[test]
fn ellipse_missing_w_produces_node_missing_geometry() {
    let doc = doc_with(
        vec![],
        vec![minimal_page(
            "page.one",
            vec![Node::Ellipse(EllipseNode {
                shadow: None,
                filter: None,
                mask: None,
                id: "ellipse.no-w".to_owned(),
                name: None,
                role: None,
                x: Some(pxv(0.0)),
                y: Some(pxv(0.0)),
                w: None, // missing
                h: Some(pxv(100.0)),
                rx: None,
                ry: None,
                style: None,
                fill: None,
                stroke: None,
                stroke_width: None,
                stroke_dash: None,
                stroke_gap: None,
                stroke_linecap: None,
                opacity: None,
                visible: None,
                locked: None,
                rotate: None,
                blend_mode: None,
                blur: None,
                anchor: None,
                anchor_zone: None,
                anchor_sibling: None,
                anchor_edge: None,
                anchor_gap: None,
                anchor_parent: None,
                source_span: None,
                unknown_props: BTreeMap::new(),
            })],
        )],
    );
    let report = validate(&doc);
    assert!(
        has_code(&report, "node.missing_geometry"),
        "codes: {:?}",
        codes(&report)
    );
    assert!(report.has_errors());
}

// ── Ellipse: raw literal fill → token.raw_visual_literal ──────────────

#[test]
fn ellipse_fill_raw_literal_produces_raw_visual_literal() {
    let doc = doc_with(
        vec![],
        vec![minimal_page(
            "page.one",
            vec![minimal_ellipse(
                "ellipse.one",
                Some(PropertyValue::Literal("#ff0000".to_owned())),
            )],
        )],
    );
    let report = validate(&doc);
    assert!(
        has_code(&report, "token.raw_visual_literal"),
        "codes: {:?}",
        codes(&report)
    );
    assert!(report.has_errors());
}

// ── Ellipse: raw literal stroke → token.raw_visual_literal ────────────

#[test]
fn ellipse_stroke_raw_literal_produces_raw_visual_literal() {
    let doc = doc_with(
        vec![],
        vec![minimal_page(
            "page.one",
            vec![Node::Ellipse(EllipseNode {
                shadow: None,
                filter: None,
                mask: None,
                id: "ellipse.stroke-lit".to_owned(),
                name: None,
                role: None,
                x: Some(pxv(0.0)),
                y: Some(pxv(0.0)),
                w: Some(pxv(100.0)),
                h: Some(pxv(100.0)),
                rx: None,
                ry: None,
                style: None,
                fill: None,
                stroke: Some(PropertyValue::Literal("#ff0000".to_owned())),
                stroke_width: None,
                stroke_dash: None,
                stroke_gap: None,
                stroke_linecap: None,
                opacity: None,
                visible: None,
                locked: None,
                rotate: None,
                blend_mode: None,
                blur: None,
                anchor: None,
                anchor_zone: None,
                anchor_sibling: None,
                anchor_edge: None,
                anchor_gap: None,
                anchor_parent: None,
                source_span: None,
                unknown_props: BTreeMap::new(),
            })],
        )],
    );
    let report = validate(&doc);
    assert!(
        has_code(&report, "token.raw_visual_literal"),
        "ellipse with raw literal stroke must produce token.raw_visual_literal; codes: {:?}",
        codes(&report)
    );
    assert!(report.has_errors());
}

// ── Line helpers ──────────────────────────────────────────────────────

fn minimal_line(id: &str, stroke: Option<PropertyValue>) -> Node {
    Node::Line(LineNode {
        id: id.to_owned(),
        name: None,
        role: None,
        x1: Some(px(0.0)),
        y1: Some(px(0.0)),
        x2: Some(px(100.0)),
        y2: Some(px(0.0)),
        style: None,
        stroke,
        stroke_width: None,
        stroke_dash: None,
        stroke_gap: None,
        stroke_linecap: None,
        opacity: None,
        visible: None,
        locked: None,
        source_span: None,
        unknown_props: BTreeMap::new(),
    })
}

// ── Line: clean doc produces no errors ───────────────────────────────

#[test]
fn line_clean_doc_no_errors() {
    let doc = doc_with(
        vec![color_token("color.rule")],
        vec![minimal_page(
            "page.one",
            vec![minimal_line("line.one", Some(token_ref("color.rule")))],
        )],
    );
    let report = validate(&doc);
    assert!(
        report.diagnostics.is_empty(),
        "expected no diagnostics for clean line doc, got: {:?}",
        codes(&report)
    );
    assert!(!report.has_errors());
}

// ── Line: missing x1 → node.missing_geometry ─────────────────────────

#[test]
fn line_missing_x1_produces_node_missing_geometry() {
    let doc = doc_with(
        vec![],
        vec![minimal_page(
            "page.one",
            vec![Node::Line(LineNode {
                id: "line.no-x1".to_owned(),
                name: None,
                role: None,
                x1: None, // missing
                y1: Some(px(0.0)),
                x2: Some(px(100.0)),
                y2: Some(px(0.0)),
                style: None,
                stroke: None,
                stroke_width: None,
                stroke_dash: None,
                stroke_gap: None,
                stroke_linecap: None,
                opacity: None,
                visible: None,
                locked: None,
                source_span: None,
                unknown_props: BTreeMap::new(),
            })],
        )],
    );
    let report = validate(&doc);
    assert!(
        has_code(&report, "node.missing_geometry"),
        "codes: {:?}",
        codes(&report)
    );
    assert!(report.has_errors());
}

// ── Line: stroke raw literal → token.raw_visual_literal ──────────────

#[test]
fn line_stroke_raw_literal_produces_raw_visual_literal() {
    let doc = doc_with(
        vec![],
        vec![minimal_page(
            "page.one",
            vec![minimal_line(
                "line.one",
                Some(PropertyValue::Literal("#000000".to_owned())),
            )],
        )],
    );
    let report = validate(&doc);
    assert!(
        has_code(&report, "token.raw_visual_literal"),
        "codes: {:?}",
        codes(&report)
    );
    assert!(report.has_errors());
}

fn minimal_ellipse(id: &str, fill: Option<PropertyValue>) -> Node {
    Node::Ellipse(EllipseNode {
        shadow: None,
        filter: None,
        mask: None,
        id: id.to_owned(),
        name: None,
        role: None,
        x: Some(pxv(0.0)),
        y: Some(pxv(0.0)),
        w: Some(pxv(100.0)),
        h: Some(pxv(100.0)),
        rx: None,
        ry: None,
        style: None,
        fill,
        stroke: None,
        stroke_width: None,
        stroke_dash: None,
        stroke_gap: None,
        stroke_linecap: None,
        opacity: None,
        visible: None,
        locked: None,
        rotate: None,
        blend_mode: None,
        blur: None,
        anchor: None,
        anchor_zone: None,
        anchor_sibling: None,
        anchor_edge: None,
        anchor_gap: None,
        anchor_parent: None,
        source_span: None,
        unknown_props: BTreeMap::new(),
    })
}

// ══════════════════════════════════════════════════════════════════════
// off_canvas advisory tests
// ══════════════════════════════════════════════════════════════════════

/// A rect with x=-20 on a 100×100 page → off_canvas advisory.
#[test]
fn rect_negative_x_is_off_canvas() {
    let doc = doc_with(
        vec![],
        vec![bounded_page(
            "page.one",
            100.0,
            100.0,
            vec![rect_at("rect.out", -20.0, 0.0, 50.0, 50.0)],
        )],
    );
    let report = validate(&doc);
    assert!(
        has_code(&report, "layout.off_canvas"),
        "expected off_canvas advisory; codes: {:?}",
        codes(&report)
    );
    let diag = report
        .diagnostics
        .iter()
        .find(|d| d.code == "layout.off_canvas")
        .expect("must exist");
    assert_eq!(diag.severity, Severity::Advisory);
    assert_eq!(diag.subject_id.as_deref(), Some("rect.out"));
    // off_canvas is advisory only — no errors.
    assert!(!report.has_errors());
}

/// A rect fully inside the page → NO off_canvas advisory.
#[test]
fn rect_fully_inside_no_off_canvas() {
    let doc = doc_with(
        vec![],
        vec![bounded_page(
            "page.one",
            100.0,
            100.0,
            vec![rect_at("rect.in", 10.0, 10.0, 80.0, 80.0)],
        )],
    );
    let report = validate(&doc);
    assert!(
        !has_code(&report, "layout.off_canvas"),
        "rect fully inside should NOT get off_canvas; codes: {:?}",
        codes(&report)
    );
}

/// A rect at x=80, w=40 (right edge=120 > page_w=100) → off_canvas.
#[test]
fn rect_overflowing_right_edge_is_off_canvas() {
    let doc = doc_with(
        vec![],
        vec![bounded_page(
            "page.one",
            100.0,
            100.0,
            vec![rect_at("rect.wide", 80.0, 0.0, 40.0, 50.0)],
        )],
    );
    let report = validate(&doc);
    assert!(
        has_code(&report, "layout.off_canvas"),
        "rect extending past right edge should be off_canvas; codes: {:?}",
        codes(&report)
    );
    let diag = report
        .diagnostics
        .iter()
        .find(|d| d.code == "layout.off_canvas")
        .expect("must exist");
    assert_eq!(diag.severity, Severity::Advisory);
    assert!(!report.has_errors());
}

/// A rect exactly touching the page edges (x=0,y=0,w=100,h=100) → no off_canvas.
#[test]
fn rect_exactly_on_page_edge_no_off_canvas() {
    let doc = doc_with(
        vec![],
        vec![bounded_page(
            "page.one",
            100.0,
            100.0,
            vec![rect_at("rect.edge", 0.0, 0.0, 100.0, 100.0)],
        )],
    );
    let report = validate(&doc);
    assert!(
        !has_code(&report, "layout.off_canvas"),
        "rect exactly on page boundary should NOT be off_canvas; codes: {:?}",
        codes(&report)
    );
}

/// Helper: rect at (x, y, w, h) in px with an optional rotation in degrees.
fn rect_at_rotated(id: &str, x: f64, y: f64, w: f64, h: f64, rotate_deg: Option<f64>) -> Node {
    let rotate = rotate_deg.map(|deg| Dimension {
        value: deg,
        unit: Unit::Deg,
    });
    Node::Rect(Box::new(RectNode {
        shadow: None,
        filter: None,
        mask: None,
        id: id.to_owned(),
        name: None,
        role: None,
        x: Some(pxv(x)),
        y: Some(pxv(y)),
        w: Some(pxv(w)),
        h: Some(pxv(h)),
        radius: None,
        radius_tl: None,
        radius_tr: None,
        radius_br: None,
        radius_bl: None,
        style: None,
        fill: None,
        stroke: None,
        stroke_width: None,
        stroke_alignment: None,
        stroke_dash: None,
        stroke_gap: None,
        stroke_linecap: None,
        border_top: None,
        border_bottom: None,
        border_left: None,
        border_right: None,
        border_width: None,
        stroke_outer: None,
        stroke_outer_width: None,
        opacity: None,
        visible: None,
        locked: None,
        rotate,
        blend_mode: None,
        blur: None,
        anchor: None,
        anchor_zone: None,
        anchor_sibling: None,
        anchor_edge: None,
        anchor_gap: None,
        anchor_parent: None,
        source_span: None,
        unknown_props: BTreeMap::new(),
    }))
}

/// A rect centered on the page, small enough that its authored bbox is fully
/// inside, but rotated 45° so its rotated AABB extends beyond the page edge →
/// off_canvas advisory fires.
///
/// Page: 100×100. Rect: x=40, y=40, w=20, h=20 (authored bbox fully inside,
/// center at (50,50)). At 45° the AABB half-extents are
/// hw_rot = (10+10)*cos(45°) ≈ 14.14, so AABB = (35.86..64.14, 35.86..64.14)
/// which is still inside. Use a more extreme rect: x=35, y=35, w=30, h=30,
/// center (50,50). AABB half-extent = (15+15)/sqrt(2)*sqrt(2) = 15*sqrt(2) ≈
/// 21.2. AABB: (28.8..71.2, 28.8..71.2) — still inside.
///
/// Simpler: rect x=0, y=40, w=80, h=20 centered at (40, 50). At 45° the
/// AABB half-extents: x-half = |40*cos45 - 10*sin45| + ... use the standard
/// formula: hw = (|w/2|*|cos| + |h/2|*|sin|) = 40*cos45 + 10*sin45 ≈ 35.36.
/// hh = 40*sin45 + 10*cos45 ≈ 35.36. AABB: (40-35.36, 50-35.36) = (4.64, 14.64)
/// to (75.36, 85.36) — still inside. Need larger rect.
///
/// Rect x=0, y=0, w=80, h=20, center (40, 10). At 45°: hw=40*cos45+10*sin45≈35.36,
/// hh=40*sin45+10*cos45≈35.36. AABB: (4.64-35.36=-30.72, ...) → off_canvas!
#[test]
fn rotated_aabb_off_canvas_fires() {
    // A wide, short rect in the top-left: authored bbox inside the page, but
    // its 45° rotated AABB extends outside.
    // Page: 200×200. Rect: x=0, y=0, w=160, h=20 → center (80, 10).
    // At 45°: hw = 80*cos45 + 10*sin45 ≈ 63.6, hh = 80*sin45 + 10*cos45 ≈ 63.6.
    // AABB: (80-63.6, 10-63.6) = (16.4, -53.6) → ay < 0 → off_canvas.
    let doc = doc_with(
        vec![],
        vec![bounded_page(
            "page.rot",
            200.0,
            200.0,
            vec![rect_at_rotated(
                "rect.rot",
                0.0,
                0.0,
                160.0,
                20.0,
                Some(45.0),
            )],
        )],
    );
    let report = validate(&doc);
    assert!(
        has_code(&report, "layout.off_canvas"),
        "rotated rect whose AABB exits page should fire off_canvas; codes: {:?}",
        codes(&report)
    );
    let diag = report
        .diagnostics
        .iter()
        .find(|d| d.code == "layout.off_canvas")
        .expect("must exist");
    assert_eq!(diag.severity, Severity::Advisory);
    assert_eq!(diag.subject_id.as_deref(), Some("rect.rot"));
}

/// Same authored box as above but unrotated (rotate=None) → authored bbox is
/// fully inside the page → no off_canvas advisory.
#[test]
fn unrotated_inside_page_no_off_canvas() {
    let doc = doc_with(
        vec![],
        vec![bounded_page(
            "page.norot",
            200.0,
            200.0,
            vec![rect_at_rotated("rect.norot", 0.0, 0.0, 160.0, 20.0, None)],
        )],
    );
    let report = validate(&doc);
    assert!(
        !has_code(&report, "layout.off_canvas"),
        "unrotated rect inside page should NOT fire off_canvas; codes: {:?}",
        codes(&report)
    );
}

// ── Geometry token refs: parse + validate + format round-trip ──────────

/// A box-geometry axis (`x`/`y`/`w`/`h`) accepts a `(token)"id"` dimension token
/// ref IN ADDITION to a raw `(px)N` literal (mirroring `font-size`). This must:
/// parse to `PropertyValue::TokenRef`, NOT trip `node.missing_geometry`, count
/// the token as referenced (so `token.unused` stays silent), NOT trip
/// `token.raw_visual_literal` (geometry is not a visual prop), and survive a
/// format -> re-parse round-trip.
#[test]
fn geometry_token_ref_parses_validates_and_round_trips() {
    let src = r##"zenith version=1 {
  project id="proj.geomtok" name="GeomTok"
  tokens format="zenith-token-v1" {
    token id="dim.h" type="dimension" value=(px)120
    token id="color.bg" type="color" value="#102030"
  }
  styles {
  }
  document id="doc.geomtok" title="GeomTok" {
    page id="page.one" w=(px)640 h=(px)480 {
      rect id="r.one" x=(px)0 y=(px)0 w=(px)100 h=(token)"dim.h" fill=(token)"color.bg"
    }
  }
}
"##;

    let adapter = KdlAdapter;
    let doc = adapter.parse(src.as_bytes()).expect("parse must succeed");
    let rect = match &doc.body.pages[0].children[0] {
        Node::Rect(r) => r,
        other => panic!("expected Rect node, got {other:?}"),
    };
    assert_eq!(
        rect.h,
        Some(PropertyValue::TokenRef("dim.h".to_owned())),
        "h=(token)\"dim.h\" must parse to a geometry token ref"
    );
    assert_eq!(
        rect.w,
        Some(PropertyValue::Dimension(px(100.0))),
        "raw px geometry must still parse to a dimension literal"
    );

    let report = validate(&doc);
    assert!(
        !has_code(&report, "node.missing_geometry"),
        "token-ref geometry must count as PRESENT; codes: {:?}",
        codes(&report)
    );
    assert!(
        !has_code(&report, "token.unused"),
        "a token used only as geometry must be registered as referenced; codes: {:?}",
        codes(&report)
    );
    assert!(
        !has_code(&report, "token.raw_visual_literal"),
        "geometry is not a visual prop; raw px must not trip raw_visual_literal; codes: {:?}",
        codes(&report)
    );
    assert!(
        !report.has_errors(),
        "the geometry-token-ref document must validate cleanly; codes: {:?}",
        codes(&report)
    );

    // Format round-trip: the token ref re-emits and re-parses identically, and the
    // raw px sibling axis is byte-preserved.
    let formatted = zenith_core::format::format_document(&doc).expect("format must succeed");
    let formatted_str = String::from_utf8(formatted).expect("formatted must be utf8");
    assert!(
        formatted_str.contains("h=(token)\"dim.h\""),
        "formatter must emit h as a token ref; got:\n{formatted_str}"
    );
    assert!(
        formatted_str.contains("w=(px)100"),
        "formatter must emit raw px geometry byte-identically; got:\n{formatted_str}"
    );

    let doc2 = adapter
        .parse(formatted_str.as_bytes())
        .expect("re-parse after format");
    let rect2 = match &doc2.body.pages[0].children[0] {
        Node::Rect(r) => r,
        other => panic!("expected Rect on re-parse, got {other:?}"),
    };
    assert_eq!(
        rect2.h,
        Some(PropertyValue::TokenRef("dim.h".to_owned())),
        "geometry token ref must survive a format -> re-parse round-trip"
    );
    assert_eq!(rect2.w, Some(PropertyValue::Dimension(px(100.0))));
}