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
//! Integration tests: styles 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::*;

// ── Style validation tests ─────────────────────────────────────────────

fn doc_with_styles(tokens: Vec<Token>, styles: Vec<Style>, pages: Vec<Page>) -> Document {
    Document {
        version: 1,
        colorspace: None,
        doc_id: None,
        mirror_margins: None,
        facing_pages: None,
        spread_gutter: None,
        page_progression: None,
        page_parity_start: None,
        margin_inner: None,
        margin_outer: None,
        margin_top: None,
        margin_bottom: None,
        project: None,
        assets: AssetBlock::default(),
        libraries: Vec::new(),
        actions: Vec::new(),
        tokens: TokenBlock {
            format: "zenith-token-v1".to_owned(),
            tokens,
        },
        styles: StyleBlock {
            styles,
            source_span: None,
        },
        components: Vec::new(),
        masters: Vec::new(),
        sections: Vec::new(),
        provenance: Vec::new(),
        variants: Vec::new(),
        recipes: Vec::new(),
        diagnostic_policy: zenith_core::DiagnosticPolicy::default(),
        brand_contract: BrandContract::default(),
        body: DocumentBody {
            id: "doc.main".to_owned(),
            title: None,
            block_styles: Vec::new(),
            pages,
        },
    }
}

fn style_with_props(id: &str, props: Vec<(&str, PropertyValue)>) -> Style {
    Style {
        id: id.to_owned(),
        properties: props.into_iter().map(|(k, v)| (k.to_owned(), v)).collect(),
        unknown_props: BTreeMap::new(),
        source_span: None,
    }
}

/// A node that references a non-declared style id → `style.unknown_reference` error.
#[test]
fn node_unknown_style_reference() {
    let rect = match minimal_rect("rect.one", None) {
        Node::Rect(mut r) => {
            r.style = Some("style.missing".to_owned());
            Node::Rect(r)
        }
        other => other,
    };
    let doc = doc_with_styles(
        vec![],
        vec![], // no styles declared
        vec![minimal_page("page.one", vec![rect])],
    );
    let report = validate(&doc);
    assert!(
        has_code(&report, "style.unknown_reference"),
        "expected style.unknown_reference; codes: {:?}",
        codes(&report)
    );
    assert!(report.has_errors());
}

/// A clean `code` node referencing a declared color token passes validation.
#[test]
fn clean_code_node_no_errors() {
    let doc = doc_with(
        vec![color_token("color.fg")],
        vec![minimal_page(
            "page.one",
            vec![minimal_code("code.one", Some(token_ref("color.fg")))],
        )],
    );
    let report = validate(&doc);
    assert!(
        report.diagnostics.is_empty(),
        "expected no diagnostics, got: {:?}",
        codes(&report)
    );
    assert!(!report.has_errors());
}

/// A `code` node referencing a non-declared style id → `style.unknown_reference`.
#[test]
fn code_node_unknown_style_reference() {
    let code = match minimal_code("code.one", None) {
        Node::Code(mut c) => {
            c.style = Some("style.missing".to_owned());
            Node::Code(c)
        }
        other => other,
    };
    let doc = doc_with_styles(
        vec![],
        vec![], // no styles declared
        vec![minimal_page("page.one", vec![code])],
    );
    let report = validate(&doc);
    assert!(
        has_code(&report, "style.unknown_reference"),
        "expected style.unknown_reference; codes: {:?}",
        codes(&report)
    );
    assert!(report.has_errors());
}

/// An unknown property on a `code` node → `node.unknown_property` warning.
#[test]
fn code_node_unknown_property_warns() {
    let code = match minimal_code("code.one", None) {
        Node::Code(mut c) => {
            c.unknown_props.insert(
                "future-prop".to_owned(),
                zenith_core::UnknownProperty {
                    value: zenith_core::UnknownValue::String("x".to_owned()),
                    ty: None,
                },
            );
            Node::Code(c)
        }
        other => other,
    };
    let doc = doc_with(vec![], vec![minimal_page("page.one", vec![code])]);
    let report = validate(&doc);
    assert!(
        has_code(&report, "node.unknown_property"),
        "expected node.unknown_property; codes: {:?}",
        codes(&report)
    );
    assert!(!report.has_errors());
}

/// A style property that references a missing token → `token.unknown_reference` error.
#[test]
fn style_prop_unknown_token() {
    let style = style_with_props(
        "style.s",
        vec![("fill", PropertyValue::TokenRef("color.missing".to_owned()))],
    );
    let doc = doc_with_styles(
        vec![], // no tokens declared
        vec![style],
        vec![minimal_page("page.one", vec![])],
    );
    let report = validate(&doc);
    assert!(
        has_code(&report, "token.unknown_reference"),
        "expected token.unknown_reference; codes: {:?}",
        codes(&report)
    );
    assert!(report.has_errors());
}

/// A style property with a raw literal → `token.raw_visual_literal` error.
#[test]
fn style_raw_literal_fill() {
    let style = style_with_props(
        "style.s",
        vec![("fill", PropertyValue::Literal("#ff0000".to_owned()))],
    );
    let doc = doc_with_styles(vec![], vec![style], vec![minimal_page("page.one", vec![])]);
    let report = validate(&doc);
    assert!(
        has_code(&report, "token.raw_visual_literal"),
        "expected token.raw_visual_literal; codes: {:?}",
        codes(&report)
    );
    assert!(report.has_errors());
}

/// A style `padding` with a raw literal dimension → `token.raw_visual_literal`.
///
/// `padding` is a token-only visual dimension prop, identical to `font-size` /
/// `stroke-width`: a raw `(px)N` literal (a `PropertyValue::Dimension`, not a
/// token) MUST be flagged, never silently accepted.
#[test]
fn style_padding_raw_literal_rejected() {
    let style = style_with_props(
        "style.flow",
        vec![("padding", PropertyValue::Dimension(px(16.0)))],
    );
    let doc = doc_with_styles(vec![], vec![style], vec![minimal_page("page.one", vec![])]);
    let report = validate(&doc);
    assert!(
        has_code(&report, "token.raw_visual_literal"),
        "a raw-literal padding must flag token.raw_visual_literal; codes: {:?}",
        codes(&report)
    );
    assert!(report.has_errors());
}

/// A style `gap` with a raw literal dimension → `token.raw_visual_literal`.
#[test]
fn style_gap_raw_literal_rejected() {
    let style = style_with_props(
        "style.flow",
        vec![("gap", PropertyValue::Dimension(px(8.0)))],
    );
    let doc = doc_with_styles(vec![], vec![style], vec![minimal_page("page.one", vec![])]);
    let report = validate(&doc);
    assert!(
        has_code(&report, "token.raw_visual_literal"),
        "a raw-literal gap must flag token.raw_visual_literal; codes: {:?}",
        codes(&report)
    );
    assert!(report.has_errors());
}

/// Unknown style property children → `style.unknown_property` warning.
#[test]
fn style_unknown_property_warns() {
    let style = Style {
        id: "style.s".to_owned(),
        properties: BTreeMap::new(),
        unknown_props: {
            let mut m = BTreeMap::new();
            m.insert(
                "bogus-prop".to_owned(),
                UnknownStyleProp {
                    raw: "whatever".to_owned(),
                },
            );
            m
        },
        source_span: None,
    };
    let doc = doc_with_styles(vec![], vec![style], vec![minimal_page("page.one", vec![])]);
    let report = validate(&doc);
    assert!(
        has_code(&report, "style.unknown_property"),
        "expected style.unknown_property warning; codes: {:?}",
        codes(&report)
    );
    let diag = report
        .diagnostics
        .iter()
        .find(|d| d.code == "style.unknown_property")
        .expect("must exist");
    assert_eq!(diag.severity, Severity::Warning);
    assert!(
        !report.has_errors(),
        "unknown prop must only warn, not error"
    );
}

/// A token referenced ONLY by a style (not by any node) must NOT be flagged `token.unused`.
#[test]
fn token_used_only_by_style_not_unused() {
    let style = style_with_props(
        "style.s",
        vec![("fill", PropertyValue::TokenRef("color.used".to_owned()))],
    );
    let doc = doc_with_styles(
        vec![color_token("color.used")],
        vec![style],
        // No nodes reference color.used — only the style does.
        vec![minimal_page("page.one", vec![])],
    );
    let report = validate(&doc);
    // Should NOT contain token.unused for color.used.
    let unused: Vec<_> = report
        .diagnostics
        .iter()
        .filter(|d| d.code == "token.unused")
        .collect();
    assert!(
        unused.is_empty(),
        "token referenced by style must not be flagged token.unused; codes: {:?}",
        codes(&report)
    );
}

fn minimal_code(id: &str, fill: Option<PropertyValue>) -> Node {
    Node::Code(CodeNode {
        id: id.to_owned(),
        name: None,
        role: None,
        x: Some(pxv(0.0)),
        y: Some(pxv(0.0)),
        w: Some(pxv(200.0)),
        h: Some(pxv(80.0)),
        overflow: None,
        language: None,
        line_numbers: None,
        tab_width: None,
        style: None,
        fill,
        font_family: None,
        font_size: None,
        font_weight: None,
        syntax_theme: None,
        opacity: None,
        visible: None,
        locked: None,
        selectable: None,
        rotate: None,
        anchor: None,
        anchor_zone: None,
        anchor_sibling: None,
        anchor_edge: None,
        anchor_gap: None,
        anchor_parent: None,
        content: String::new(),
        source_span: None,
        unknown_props: BTreeMap::new(),
    })
}

// ══════════════════════════════════════════════════════════════════════
// Library block validation tests
// ══════════════════════════════════════════════════════════════════════

/// Helper: build a document with the given libraries appended to a
/// single-page doc.
fn doc_with_libraries(libraries: Vec<LibraryDef>, pages: Vec<Page>) -> Document {
    let mut doc = doc_with(vec![], pages);
    doc.libraries = libraries;
    doc
}

fn minimal_library(id: &str) -> LibraryDef {
    LibraryDef {
        id: id.to_owned(),
        version: None,
        hash: None,
        source_span: None,
        unknown_props: BTreeMap::new(),
    }
}

#[test]
fn clean_libraries_block_no_diagnostics() {
    let lib = LibraryDef {
        id: "@acme/brand-kit".to_owned(),
        version: Some("1.4.0".to_owned()),
        hash: Some("sha256-abc".to_owned()),
        source_span: None,
        unknown_props: BTreeMap::new(),
    };
    let doc = doc_with_libraries(vec![lib], vec![minimal_page("p1", vec![])]);
    let report = validate(&doc);
    assert!(
        report.diagnostics.is_empty(),
        "a well-formed libraries block must produce no diagnostics; got: {:?}",
        codes(&report)
    );
    assert!(!report.has_errors());
}

#[test]
fn library_duplicate_id_is_error() {
    let a = minimal_library("@acme/brand-kit");
    let b = minimal_library("@acme/brand-kit"); // duplicate id
    let doc = doc_with_libraries(vec![a, b], vec![minimal_page("p1", vec![])]);
    let report = validate(&doc);
    assert!(
        has_code(&report, "id.duplicate"),
        "two libraries sharing an id must trigger id.duplicate; got {:?}",
        codes(&report)
    );
    assert!(report.has_errors());
}

#[test]
fn library_unknown_property_produces_warning() {
    let mut unknown_props = BTreeMap::new();
    unknown_props.insert(
        "registry".to_owned(),
        zenith_core::UnknownProperty {
            value: zenith_core::UnknownValue::String("x".to_owned()),
            ty: Some("token".to_owned()),
        },
    );
    let lib = LibraryDef {
        id: "@acme/brand-kit".to_owned(),
        version: None,
        hash: None,
        source_span: None,
        unknown_props,
    };
    let doc = doc_with_libraries(vec![lib], vec![minimal_page("p1", vec![])]);
    let report = validate(&doc);
    assert!(
        has_code(&report, "library.unknown_property"),
        "an unknown prop on a library must warn; got {:?}",
        codes(&report)
    );
    let diag = report
        .diagnostics
        .iter()
        .find(|d| d.code == "library.unknown_property")
        .expect("should exist");
    assert_eq!(diag.severity, Severity::Warning);
    assert!(!report.has_errors());
}

// ── provenance: cross-reference validation ────────────────────────────

/// A document with the given provenance records, libraries, and page children.
/// The page (id "p1") carries `children`, so node refs can resolve.
fn doc_with_provenance(
    provenance: Vec<ProvenanceDef>,
    libraries: Vec<LibraryDef>,
    children: Vec<Node>,
) -> Document {
    let mut doc = doc_with(vec![], vec![minimal_page("p1", children)]);
    doc.libraries = libraries;
    doc.provenance = provenance;
    doc
}

fn minimal_provenance(id: &str, node: &str, library: &str) -> ProvenanceDef {
    ProvenanceDef {
        id: id.to_owned(),
        node: node.to_owned(),
        library: library.to_owned(),
        item: None,
        linked: None,
        source_span: None,
        unknown_props: BTreeMap::new(),
    }
}

#[test]
fn clean_provenance_record_no_diagnostics() {
    // node "btn" exists on the page; library "@acme/brand-kit" is declared.
    let prov = ProvenanceDef {
        id: "prov.btn".to_owned(),
        node: "btn".to_owned(),
        library: "@acme/brand-kit".to_owned(),
        item: Some("button".to_owned()),
        linked: Some(true),
        source_span: None,
        unknown_props: BTreeMap::new(),
    };
    let doc = doc_with_provenance(
        vec![prov],
        vec![minimal_library("@acme/brand-kit")],
        vec![minimal_rect("btn", None)],
    );
    let report = validate(&doc);
    assert!(
        report.diagnostics.is_empty(),
        "a fully-resolved provenance record must produce no diagnostics; got: {:?}",
        codes(&report)
    );
    assert!(!report.has_errors());
}

#[test]
fn provenance_unknown_node_is_error() {
    let prov = minimal_provenance("prov.ghost", "ghost", "@acme/brand-kit");
    let doc = doc_with_provenance(
        vec![prov],
        vec![minimal_library("@acme/brand-kit")],
        vec![minimal_rect("btn", None)],
    );
    let report = validate(&doc);
    assert!(
        has_code(&report, "provenance.unknown_node"),
        "a provenance record referencing a non-existent node must error; got {:?}",
        codes(&report)
    );
    assert!(report.has_errors());
}

#[test]
fn provenance_node_may_be_a_declared_token() {
    // A provenance record whose `node` is a declared TOKEN id (a token imported
    // from a library) validates clean — the target need not be a node.
    let prov = minimal_provenance("prov.noir", "noir", "@zenith/filters");
    let mut doc = doc_with_provenance(
        vec![prov],
        vec![minimal_library("@zenith/filters")],
        vec![minimal_rect("btn", None)],
    );
    doc.tokens.tokens.push(color_token_hex("noir", "#000000"));
    let report = validate(&doc);
    assert!(
        !has_code(&report, "provenance.unknown_node"),
        "a provenance record targeting a declared token must not error; got {:?}",
        codes(&report)
    );
}

#[test]
fn provenance_unknown_library_is_error() {
    let prov = minimal_provenance("prov.btn", "btn", "@nope/x");
    let doc = doc_with_provenance(
        vec![prov],
        vec![minimal_library("@acme/brand-kit")],
        vec![minimal_rect("btn", None)],
    );
    let report = validate(&doc);
    assert!(
        has_code(&report, "provenance.unknown_library"),
        "a provenance record referencing an undeclared library must error; got {:?}",
        codes(&report)
    );
    assert!(report.has_errors());
}

#[test]
fn provenance_duplicate_id_is_error() {
    let a = minimal_provenance("prov.dup", "btn", "@acme/brand-kit");
    let b = minimal_provenance("prov.dup", "btn", "@acme/brand-kit"); // duplicate id
    let doc = doc_with_provenance(
        vec![a, b],
        vec![minimal_library("@acme/brand-kit")],
        vec![minimal_rect("btn", None)],
    );
    let report = validate(&doc);
    assert!(
        has_code(&report, "id.duplicate"),
        "two provenance records sharing an id must trigger id.duplicate; got {:?}",
        codes(&report)
    );
    assert!(report.has_errors());
}

#[test]
fn provenance_unknown_property_produces_warning() {
    let mut unknown_props = BTreeMap::new();
    unknown_props.insert(
        "registry".to_owned(),
        zenith_core::UnknownProperty {
            value: zenith_core::UnknownValue::String("x".to_owned()),
            ty: Some("token".to_owned()),
        },
    );
    let prov = ProvenanceDef {
        id: "prov.btn".to_owned(),
        node: "btn".to_owned(),
        library: "@acme/brand-kit".to_owned(),
        item: None,
        linked: None,
        source_span: None,
        unknown_props,
    };
    let doc = doc_with_provenance(
        vec![prov],
        vec![minimal_library("@acme/brand-kit")],
        vec![minimal_rect("btn", None)],
    );
    let report = validate(&doc);
    assert!(
        has_code(&report, "provenance.unknown_property"),
        "an unknown prop on an origin must warn; got {:?}",
        codes(&report)
    );
    let diag = report
        .diagnostics
        .iter()
        .find(|d| d.code == "provenance.unknown_property")
        .expect("should exist");
    assert_eq!(diag.severity, Severity::Warning);
    // The unknown-property warning is not itself an error; node + library resolve.
    assert!(!report.has_errors());
}

#[test]
fn provenance_node_may_be_a_declared_action() {
    // A provenance record whose `node` is a declared ACTION id validates clean —
    // an action imported from a library is a valid provenance target.
    let prov = minimal_provenance("prov.brand", "apply-brand-kit", "@acme/brand-kit");
    let mut doc = doc_with_provenance(vec![prov], vec![minimal_library("@acme/brand-kit")], vec![]);
    doc.actions.push(ActionDef {
        id: "apply-brand-kit".to_owned(),
        label: Some("Apply Brand Kit".to_owned()),
        version: None,
        tx_json: "{}".to_owned(),
        source_span: None,
        unknown_props: BTreeMap::new(),
    });
    let report = validate(&doc);
    assert!(
        !has_code(&report, "provenance.unknown_node"),
        "a provenance record targeting a declared action must not fire unknown_node; got {:?}",
        codes(&report)
    );
    assert!(!report.has_errors());
}

#[test]
fn provenance_node_nonexistent_id_still_errors() {
    // Negative case: a provenance `node` that matches neither any node id, nor
    // any declared token id, nor any declared action id must still fire
    // `provenance.unknown_node`.
    let prov = minimal_provenance("prov.ghost2", "does-not-exist", "@acme/brand-kit");
    let doc = doc_with_provenance(
        vec![prov],
        vec![minimal_library("@acme/brand-kit")],
        vec![minimal_rect("btn", None)],
    );
    let report = validate(&doc);
    assert!(
        has_code(&report, "provenance.unknown_node"),
        "a provenance record with a non-existent node/token/action id must error; got {:?}",
        codes(&report)
    );
    assert!(report.has_errors());
}