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
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
//! Integration tests for the canonical writer: document.
//!
//! Document-structure blocks — components/instances, masters/fields, page
//! margins/parity, sections, libraries, provenance, and actions — round-trip.
//!
//! Moved verbatim from the former in-`src` `format/writer/tests.rs`; the body of
//! every test is unchanged — only import paths were rewritten to the public
//! `zenith_core` surface. Span-stripping helpers live in `common`.

mod common;

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

/// A `.zen` document exercising the `components` block, an `instance` node, and
/// an `override` with a `span` replacement, a `fill`, and a `visible` flag.
const COMPONENT_DOC: &str = r##"zenith version=1 {
  project id="proj.comp" name="Component Project"
  tokens format="zenith-token-v1" {
    token id="color.bg" type="color" value="#101010"
    token id="color.fg" type="color" value="#fafafa"
    token id="color.alt" type="color" value="#ff0000"
    token id="size.body" type="dimension" value=(pt)18
  }
  styles {
  }
  components {
    component id="panel.master" {
      rect id="bg" x=(px)0 y=(px)0 w=(px)200 h=(px)120 fill=(token)"color.bg"
      text id="label" x=(px)10 y=(px)10 w=(px)180 h=(px)40 fill=(token)"color.fg" {
        span "Default"
      }
    }
  }
  document id="doc.comp" title="Comp Doc" {
    page id="page.one" w=(px)640 h=(px)360 background=(token)"color.bg" {
      instance id="inst.1" component="panel.master" x=(px)0 y=(px)0 {
        override ref="label" fill=(token)"color.alt" visible=#true {
          span "Back"
        }
      }
      instance id="inst.2" component="panel.master" x=(px)220 y=(px)0 {
        override ref="label" {
          span "Center"
        }
      }
    }
  }
}
"##;

/// **components / instance / override round-trip**: parse → format → parse must
/// yield the same AST (spans excluded), and the formatter must be idempotent.
#[test]
fn test_component_instance_override_round_trip() {
    let adapter = KdlAdapter;
    let doc_orig = adapter
        .parse(COMPONENT_DOC.as_bytes())
        .expect("original parse");

    // Structural sanity: one component, two instances, first with rich override.
    assert_eq!(doc_orig.components.len(), 1);
    assert_eq!(doc_orig.components[0].id, "panel.master");
    assert_eq!(doc_orig.components[0].children.len(), 2);
    match &doc_orig.body.pages[0].children[0] {
        Node::Instance(i) => {
            assert_eq!(i.id, "inst.1");
            assert_eq!(i.component, "panel.master");
            assert_eq!(i.overrides.len(), 1);
            let ov = &i.overrides[0];
            assert_eq!(ov.ref_id, "label");
            assert_eq!(ov.visible, Some(true));
            assert!(ov.fill.is_some());
            let spans = ov.spans.as_ref().expect("override spans");
            assert_eq!(spans.len(), 1);
            assert_eq!(spans[0].text, "Back");
        }
        other => panic!("expected Instance node, got {other:?}"),
    }

    let formatted = format_document(&doc_orig).expect("format");
    let doc_reparsed = adapter.parse(&formatted).expect("re-parse");

    assert_eq!(
        strip_spans(doc_orig),
        strip_spans(doc_reparsed),
        "components/instance/override must survive a format round-trip (spans excluded)"
    );

    // Idempotency.
    let s2 = format_document(&adapter.parse(&formatted).expect("re-parse for idempotency"))
        .expect("format 2");
    assert_eq!(
        String::from_utf8(formatted).unwrap(),
        String::from_utf8(s2).unwrap(),
        "component formatting must be idempotent"
    );
}

/// **Page bleed round-trip**: a `bleed` attribute parses, formats back into the
/// canonical text (right after `background`), and survives parse→format→parse.
#[test]
fn test_page_bleed_round_trips() {
    let src = r##"zenith version=1 {
  project id="proj.bleed" name="Bleed"
  tokens format="zenith-token-v1" {
  }
  styles {
  }
  document id="doc.bleed" title="Bleed" {
    page id="page.bleed" w=(px)400 h=(px)600 bleed=(px)35 {
    }
  }
}
"##;
    let adapter = KdlAdapter;
    let doc = adapter.parse(src.as_bytes()).expect("parse");

    // The parsed page carries the bleed dimension.
    let page = &doc.body.pages[0];
    let bleed = page.bleed.as_ref().expect("bleed must be present");
    assert_eq!(bleed.value, 35.0);
    assert!(matches!(bleed.unit, zenith_core::Unit::Px));

    // Canonical form preserves it verbatim.
    let formatted = format_document(&doc).expect("format");
    let text = String::from_utf8(formatted).expect("utf8");
    assert!(
        text.contains("bleed=(px)35"),
        "formatted output must carry bleed; got:\n{text}"
    );

    // Round-trip AST equality (spans stripped).
    let reparsed = adapter
        .parse(text.as_bytes())
        .expect("re-parse after format");
    assert_eq!(
        strip_spans(doc),
        strip_spans(reparsed),
        "bleed must survive parse → format → parse"
    );
}

/// **Page mirrored margins + document mirror-margins + page-progression
/// round-trip**: the four page margins, the document `mirror-margins` toggle,
/// and `page-progression` all parse, format into canonical text, and survive
/// parse → format → parse.
#[test]
fn test_book_margins_round_trip() {
    let src = r##"zenith version=1 mirror-margins=#true page-progression="rtl" {
  project id="proj.book" name="Book"
  tokens format="zenith-token-v1" {
  }
  styles {
  }
  document id="doc.book" title="Book" {
    page id="page.recto" w=(px)1240 h=(px)1754 margin-inner=(px)225 margin-outer=(px)150 margin-top=(px)210 margin-bottom=(px)240 {
    }
  }
}
"##;
    let adapter = KdlAdapter;
    let doc = adapter.parse(src.as_bytes()).expect("parse");

    // Document-level toggles.
    assert_eq!(doc.mirror_margins, Some(true));
    assert_eq!(doc.page_progression.as_deref(), Some("rtl"));

    // Page-level margins.
    let page = &doc.body.pages[0];
    assert_eq!(page.margin_inner.as_ref().expect("inner").value, 225.0);
    assert_eq!(page.margin_outer.as_ref().expect("outer").value, 150.0);
    assert_eq!(page.margin_top.as_ref().expect("top").value, 210.0);
    assert_eq!(page.margin_bottom.as_ref().expect("bottom").value, 240.0);

    // Canonical form preserves every attribute verbatim.
    let formatted = format_document(&doc).expect("format");
    let text = String::from_utf8(formatted).expect("utf8");
    for needle in [
        "mirror-margins=#true",
        "page-progression=\"rtl\"",
        "margin-inner=(px)225",
        "margin-outer=(px)150",
        "margin-top=(px)210",
        "margin-bottom=(px)240",
    ] {
        assert!(
            text.contains(needle),
            "formatted output must carry `{needle}`; got:\n{text}"
        );
    }

    // Round-trip AST equality (spans stripped).
    let reparsed = adapter
        .parse(text.as_bytes())
        .expect("re-parse after format");
    assert_eq!(
        strip_spans(doc),
        strip_spans(reparsed),
        "book margins + mirror-margins + page-progression must survive round-trip"
    );
}

/// A `.zen` document exercising the masters block, a page `master` attribute,
/// and all three field types (running-head, page-number, page-ref) — both via a
/// master and inline in a page.
const MASTER_FIELD_DOC: &str = r##"zenith version=1 mirror-margins=#true {
  project id="proj.mf" name="MF"
  tokens format="zenith-token-v1" {
    token id="color.ink" type="color" value="#111111"
  }
  styles {
  }
  masters {
    master id="m.body" {
      field id="rh" type="running-head" recto="Recto Title" verso="Verso Title" y=(px)80 h=(px)40 fill=(token)"color.ink"
      field id="folio" type="page-number" y=(px)1820 h=(px)40 fill=(token)"color.ink"
    }
  }
  document id="doc.mf" title="MF" {
    page id="p1" w=(px)1200 h=(px)1900 margin-inner=(px)160 margin-outer=(px)100 margin-top=(px)80 margin-bottom=(px)80 master="m.body" {
      field id="xref" type="page-ref" target="anchor" x=(px)10 y=(px)10 w=(px)80 h=(px)30 fill=(token)"color.ink"
    }
    page id="p2" w=(px)1200 h=(px)1900 margin-inner=(px)160 margin-outer=(px)100 margin-top=(px)80 margin-bottom=(px)80 master="m.body" {
      text id="anchor" x=(px)160 y=(px)200 w=(px)900 h=(px)40 fill=(token)"color.ink" {
        span "Body"
      }
    }
  }
}
"##;

/// **Masters + field round-trip**: the masters block, the page `master`
/// attribute, and every field node must survive parse → format → parse with an
/// identical AST (spans excluded), and the formatter must be idempotent.
#[test]
fn test_master_field_round_trip() {
    let adapter = KdlAdapter;
    let doc_orig = adapter
        .parse(MASTER_FIELD_DOC.as_bytes())
        .expect("original parse");
    let formatted = format_document(&doc_orig).expect("format");
    let text = String::from_utf8(formatted.clone()).expect("utf8");

    // The masters block emits after components / before document, and a field
    // node carries the canonical attribute order.
    assert!(text.contains("masters {"), "masters block missing:\n{text}");
    assert!(
        text.contains("master id=\"m.body\""),
        "master decl missing:\n{text}"
    );
    assert!(
        text.contains(
            "field id=\"rh\" type=\"running-head\" recto=\"Recto Title\" verso=\"Verso Title\""
        ),
        "running-head field line missing/incorrect:\n{text}"
    );
    assert!(
        text.contains("master=\"m.body\""),
        "page master attribute missing:\n{text}"
    );
    assert!(
        text.contains("field id=\"xref\" type=\"page-ref\" target=\"anchor\""),
        "page-ref field line missing/incorrect:\n{text}"
    );

    let doc_reparsed = adapter.parse(&formatted).expect("re-parse after format");

    // Idempotency (format the re-parsed doc before it is consumed by strip).
    let s2 = format_document(&doc_reparsed).expect("format 2");
    assert_eq!(
        text,
        String::from_utf8(s2).expect("utf8 2"),
        "format must be idempotent for masters + fields"
    );

    assert_eq!(
        strip_spans(doc_orig),
        strip_spans(doc_reparsed),
        "masters + field must survive a format round-trip (spans excluded)"
    );
}

/// **Page parity attributes round-trip**: the document `page-parity-start` and a
/// per-page `parity` override parse, format into canonical text, and survive
/// parse → format → parse.
#[test]
fn test_page_parity_round_trip() {
    let src = r##"zenith version=1 page-parity-start="verso" {
  project id="proj.par" name="Parity"
  tokens format="zenith-token-v1" {
  }
  styles {
  }
  document id="doc.par" title="Parity" {
    page id="page.one" w=(px)1240 h=(px)1754 parity="recto" {
    }
    page id="page.two" w=(px)1240 h=(px)1754 {
    }
  }
}
"##;
    let adapter = KdlAdapter;
    let doc = adapter.parse(src.as_bytes()).expect("parse");

    assert_eq!(doc.page_parity_start.as_deref(), Some("verso"));
    assert_eq!(doc.body.pages[0].parity.as_deref(), Some("recto"));
    assert_eq!(doc.body.pages[1].parity, None);

    let formatted = format_document(&doc).expect("format");
    let text = String::from_utf8(formatted).expect("utf8");
    for needle in ["page-parity-start=\"verso\"", "parity=\"recto\""] {
        assert!(
            text.contains(needle),
            "formatted output must carry `{needle}`; got:\n{text}"
        );
    }

    let reparsed = adapter
        .parse(text.as_bytes())
        .expect("re-parse after format");
    assert_eq!(
        strip_spans(doc),
        strip_spans(reparsed),
        "page-parity-start + page parity must survive round-trip"
    );
}

/// **Document-level default margins round-trip**: the four `margin-*` attributes
/// on the root `zenith` node parse, format into canonical text, and survive
/// parse → format → parse.
#[test]
fn test_document_default_margins_round_trip() {
    let src = r##"zenith version=1 mirror-margins=#true margin-inner=(px)225 margin-outer=(px)150 margin-top=(px)210 margin-bottom=(px)240 {
  project id="proj.dm" name="DocMargins"
  tokens format="zenith-token-v1" {
  }
  styles {
  }
  document id="doc.dm" title="DocMargins" {
    page id="page.one" w=(px)1240 h=(px)1754 {
    }
  }
}
"##;
    let adapter = KdlAdapter;
    let doc = adapter.parse(src.as_bytes()).expect("parse");

    assert_eq!(doc.margin_inner.as_ref().expect("inner").value, 225.0);
    assert_eq!(doc.margin_outer.as_ref().expect("outer").value, 150.0);
    assert_eq!(doc.margin_top.as_ref().expect("top").value, 210.0);
    assert_eq!(doc.margin_bottom.as_ref().expect("bottom").value, 240.0);
    // The page declares no margins — it inherits the document defaults.
    assert_eq!(doc.body.pages[0].margin_inner, None);

    let formatted = format_document(&doc).expect("format");
    let text = String::from_utf8(formatted).expect("utf8");
    for needle in [
        "margin-inner=(px)225",
        "margin-outer=(px)150",
        "margin-top=(px)210",
        "margin-bottom=(px)240",
    ] {
        assert!(
            text.contains(needle),
            "formatted output must carry `{needle}`; got:\n{text}"
        );
    }

    let reparsed = adapter
        .parse(text.as_bytes())
        .expect("re-parse after format");
    assert_eq!(
        strip_spans(doc),
        strip_spans(reparsed),
        "document default margins must survive round-trip"
    );
}

/// **Parse test**: a `sections { section … }` block round-trips into
/// `Document.sections` with correct field values (including folio-start,
/// folio-style, and underscore aliases for both).
#[test]
fn test_sections_parse_fields() {
    let src = r##"zenith version=1 {
  project id="proj.s" name="S"
  tokens format="zenith-token-v1" {
  }
  styles {
  }
  sections {
    section id="sec.front" name="Front Matter" folio-start=1 folio-style="lower-roman" start-page="page.cover"
    section id="sec.body" name="Body" folio_start=10 folio_style="decimal" start_page="page.body"
  }
  document id="doc.s" title="S" {
    page id="page.cover" w=(px)640 h=(px)360 {
      rect id="r1" x=(px)0 y=(px)0 w=(px)640 h=(px)360 fill=(token)"c"
    }
    page id="page.body" w=(px)640 h=(px)360 {
      rect id="r2" x=(px)0 y=(px)0 w=(px)640 h=(px)360 fill=(token)"c"
    }
  }
}
"##;
    let adapter = KdlAdapter;
    let doc = adapter.parse(src.as_bytes()).expect("parse sections doc");

    assert_eq!(doc.sections.len(), 2, "expected 2 sections");

    let front = &doc.sections[0];
    assert_eq!(front.id, "sec.front");
    assert_eq!(front.name, "Front Matter");
    assert_eq!(front.folio_start, Some(1));
    assert_eq!(front.folio_style.as_deref(), Some("lower-roman"));
    assert_eq!(front.start_page, "page.cover");

    let body = &doc.sections[1];
    assert_eq!(body.id, "sec.body");
    assert_eq!(body.name, "Body");
    assert_eq!(body.folio_start, Some(10));
    assert_eq!(body.folio_style.as_deref(), Some("decimal"));
    assert_eq!(body.start_page, "page.body");
}

/// **Serialize round-trip**: parse a doc with sections → format → re-parse →
/// sections identical (spans stripped). Also assert the formatted output
/// contains the `section id=..` line.
#[test]
fn test_sections_round_trip() {
    let src = r##"zenith version=1 {
  project id="proj.rt" name="RT"
  tokens format="zenith-token-v1" {
  }
  styles {
  }
  sections {
    section id="sec.intro" name="Introduction" folio-start=1 folio-style="lower-roman" start-page="pg1"
    section id="sec.main" name="Main" start-page="pg2"
  }
  document id="doc.rt" title="RT" {
    page id="pg1" w=(px)640 h=(px)360 {
      rect id="r1" x=(px)0 y=(px)0 w=(px)10 h=(px)10 fill=(token)"c"
    }
    page id="pg2" w=(px)640 h=(px)360 {
      rect id="r2" x=(px)0 y=(px)0 w=(px)10 h=(px)10 fill=(token)"c"
    }
  }
}
"##;
    let adapter = KdlAdapter;
    let doc = adapter.parse(src.as_bytes()).expect("parse");

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

    assert!(
        formatted_str.contains(r#"section id="sec.intro""#),
        "formatted output must contain section id; got:\n{formatted_str}"
    );
    assert!(
        formatted_str.contains("folio-start=1"),
        "formatted output must contain folio-start; got:\n{formatted_str}"
    );
    assert!(
        formatted_str.contains(r#"folio-style="lower-roman""#),
        "formatted output must contain folio-style; got:\n{formatted_str}"
    );

    let reparsed = adapter.parse(&formatted).expect("re-parse");
    assert_eq!(
        strip_spans(doc).sections,
        strip_spans(reparsed).sections,
        "sections must survive a parse → format → parse round-trip"
    );
}

// ── libraries: parse, serialize, and round-trip ───────────────────────

/// **Serialize round-trip**: parse a doc with a `libraries` block (id/version/
/// hash plus an annotated unknown prop) → format → re-parse → libraries
/// identical (spans stripped). Also assert the formatted output contains both
/// `library` lines with their id/version/hash and that the annotated unknown
/// prop survives. Mirrors `test_sections_round_trip`.
#[test]
fn test_libraries_round_trip() {
    let src = r##"zenith version=1 {
  project id="proj.lib" name="LIB"
  libraries {
    library id="@acme/brand-kit" version="1.4.0" hash="sha256-abc" registry=(token)"x"
    library id="@acme/icons" version="2.0.1"
  }
  tokens format="zenith-token-v1" {
  }
  styles {
  }
  document id="doc.lib" title="LIB" {
    page id="pg1" w=(px)640 h=(px)360 {
      rect id="r1" x=(px)0 y=(px)0 w=(px)10 h=(px)10 fill=(token)"c"
    }
  }
}
"##;
    let adapter = KdlAdapter;
    let doc = adapter.parse(src.as_bytes()).expect("parse");

    assert_eq!(doc.libraries.len(), 2, "expected 2 libraries");
    let brand = &doc.libraries[0];
    assert_eq!(brand.id, "@acme/brand-kit");
    assert_eq!(brand.version.as_deref(), Some("1.4.0"));
    assert_eq!(brand.hash.as_deref(), Some("sha256-abc"));
    let registry = brand
        .unknown_props
        .get("registry")
        .expect("annotated unknown prop must be preserved");
    assert_eq!(
        registry.ty.as_deref(),
        Some("token"),
        "unknown prop annotation must survive"
    );

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

    assert!(
        formatted_str.contains(r#"library id="@acme/brand-kit" version="1.4.0" hash="sha256-abc""#),
        "formatted output must contain the brand-kit library line; got:\n{formatted_str}"
    );
    assert!(
        formatted_str.contains(r#"library id="@acme/icons" version="2.0.1""#),
        "formatted output must contain the icons library line; got:\n{formatted_str}"
    );
    assert!(
        formatted_str.contains(r#"registry=(token)"x""#),
        "annotated unknown prop must round-trip; got:\n{formatted_str}"
    );
    // Canonical order: assets, then libraries, then tokens.
    let libs_at = formatted_str.find("libraries {").expect("libraries block");
    let tokens_at = formatted_str.find("tokens ").expect("tokens block");
    assert!(
        libs_at < tokens_at,
        "libraries must be emitted before tokens; got:\n{formatted_str}"
    );

    let reparsed = adapter.parse(&formatted).expect("re-parse");
    assert_eq!(
        strip_spans(doc).libraries,
        strip_spans(reparsed).libraries,
        "libraries must survive a parse → format → parse round-trip (idempotent)"
    );
}

// ── provenance: parse, serialize, and round-trip ──────────────────────

/// **Serialize round-trip**: parse a doc with a `provenance` block (two origin
/// records, one carrying every field + an annotated unknown prop) plus a
/// matching `libraries` block and the referenced nodes → format → re-parse →
/// provenance identical (spans stripped). Also assert all fields are emitted,
/// the block comes AFTER `sections`, and the annotated unknown prop survives.
/// Mirrors `test_libraries_round_trip`.
#[test]
fn test_provenance_round_trip() {
    let src = r##"zenith version=1 {
  project id="proj.prov" name="PROV"
  libraries {
    library id="@acme/brand-kit" version="1.4.0"
  }
  tokens format="zenith-token-v1" {
  }
  styles {
  }
  sections {
    section id="sec.body" name="Body" start-page="pg1"
  }
  provenance {
    origin id="prov.btn" node="btn" library="@acme/brand-kit" item="button" linked=#true registry=(token)"x"
    origin id="prov.x" node="x" library="@acme/brand-kit"
  }
  document id="doc.prov" title="PROV" {
    page id="pg1" w=(px)640 h=(px)360 {
      rect id="btn" x=(px)0 y=(px)0 w=(px)10 h=(px)10 fill=(token)"c"
      rect id="x" x=(px)0 y=(px)0 w=(px)10 h=(px)10 fill=(token)"c"
    }
  }
}
"##;
    let adapter = KdlAdapter;
    let doc = adapter.parse(src.as_bytes()).expect("parse");

    assert_eq!(doc.provenance.len(), 2, "expected 2 provenance records");
    let btn = &doc.provenance[0];
    assert_eq!(btn.id, "prov.btn");
    assert_eq!(btn.node, "btn");
    assert_eq!(btn.library, "@acme/brand-kit");
    assert_eq!(btn.item.as_deref(), Some("button"));
    assert_eq!(btn.linked, Some(true));
    let registry = btn
        .unknown_props
        .get("registry")
        .expect("annotated unknown prop must be preserved");
    assert_eq!(
        registry.ty.as_deref(),
        Some("token"),
        "unknown prop annotation must survive"
    );
    let x = &doc.provenance[1];
    assert_eq!(x.item, None);
    assert_eq!(x.linked, None);

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

    assert!(
        formatted_str.contains(
            r#"origin id="prov.btn" node="btn" library="@acme/brand-kit" item="button" linked=#true"#
        ),
        "formatted output must contain the full btn origin line; got:\n{formatted_str}"
    );
    assert!(
        formatted_str.contains(r#"origin id="prov.x" node="x" library="@acme/brand-kit""#),
        "formatted output must contain the minimal x origin line; got:\n{formatted_str}"
    );
    assert!(
        formatted_str.contains(r#"registry=(token)"x""#),
        "annotated unknown prop must round-trip; got:\n{formatted_str}"
    );
    // Canonical order: sections, then provenance, then document.
    let sections_at = formatted_str.find("sections {").expect("sections block");
    let prov_at = formatted_str
        .find("provenance {")
        .expect("provenance block");
    let doc_at = formatted_str.find("document ").expect("document block");
    assert!(
        sections_at < prov_at && prov_at < doc_at,
        "provenance must be emitted after sections and before document; got:\n{formatted_str}"
    );

    let reparsed = adapter.parse(&formatted).expect("re-parse");
    assert_eq!(
        strip_spans(doc).provenance,
        strip_spans(reparsed).provenance,
        "provenance must survive a parse → format → parse round-trip (idempotent)"
    );
}

// ── actions: parse, serialize, and round-trip ─────────────────────────

/// **Serialize round-trip**: parse a doc with an `actions` block (two action
/// entries — one carrying every field plus an annotated unknown prop, one
/// minimal) → format → re-parse → actions identical (spans stripped). Also
/// assert all fields are emitted, the block comes AFTER `provenance`, and the
/// annotated unknown prop and the `tx` JSON payload survive. Mirrors
/// `test_provenance_round_trip`.
#[test]
fn test_actions_round_trip() {
    let src = r##"zenith version=1 {
  project id="proj.act" name="ACT"
  tokens format="zenith-token-v1" {
  }
  styles {
  }
  actions {
    action id="apply-brand-kit" label="Apply Brand Kit" version="1.0.0" meta=(token)"x" {
      tx "{\"ops\":[{\"op\":\"update_token_value\",\"id\":\"color.brand\",\"value\":\"#e11d48\"}]}"
    }
    action id="reset-spacing" {
      tx "{\"ops\":[]}"
    }
  }
  document id="doc.act" title="ACT" {
    page id="pg1" w=(px)640 h=(px)360 {
      rect id="r1" x=(px)0 y=(px)0 w=(px)10 h=(px)10
    }
  }
}
"##;
    let adapter = KdlAdapter;
    let doc = adapter.parse(src.as_bytes()).expect("parse");

    assert_eq!(doc.actions.len(), 2, "expected 2 actions");
    let brand = &doc.actions[0];
    assert_eq!(brand.id, "apply-brand-kit");
    assert_eq!(brand.label.as_deref(), Some("Apply Brand Kit"));
    assert_eq!(brand.version.as_deref(), Some("1.0.0"));
    assert!(
        brand.tx_json.contains("update_token_value"),
        "tx_json must contain the op name"
    );
    let meta = brand
        .unknown_props
        .get("meta")
        .expect("annotated unknown prop must be preserved");
    assert_eq!(
        meta.ty.as_deref(),
        Some("token"),
        "unknown prop annotation must survive"
    );

    let reset = &doc.actions[1];
    assert_eq!(reset.id, "reset-spacing");
    assert_eq!(reset.label, None);
    assert_eq!(reset.version, None);

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

    assert!(
        formatted_str
            .contains(r#"action id="apply-brand-kit" label="Apply Brand Kit" version="1.0.0""#),
        "formatted output must contain the full action line; got:\n{formatted_str}"
    );
    assert!(
        formatted_str.contains(r#"action id="reset-spacing""#),
        "formatted output must contain the minimal action line; got:\n{formatted_str}"
    );
    assert!(
        formatted_str.contains("update_token_value"),
        "tx payload must survive formatting; got:\n{formatted_str}"
    );
    assert!(
        formatted_str.contains(r#"meta=(token)"x""#),
        "annotated unknown prop must round-trip; got:\n{formatted_str}"
    );

    // actions must appear after provenance and before document.
    let actions_at = formatted_str.find("actions {").expect("actions block");
    let doc_at = formatted_str.find("document ").expect("document block");
    assert!(
        actions_at < doc_at,
        "actions must be emitted before document; got:\n{formatted_str}"
    );

    let reparsed = adapter.parse(&formatted).expect("re-parse");
    assert_eq!(
        strip_spans(doc).actions,
        strip_spans(reparsed).actions,
        "actions must survive a parse → format → parse round-trip (idempotent)"
    );
}