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
//! Integration tests for the brand-contract validation feature.
//!
//! Covers:
//! - Parse: full block, empty `brand {}`, absent block, type errors.
//! - Format round-trip: parse→format→parse preserves the contract; absent brand
//!   block emits nothing.
//! - Validate: on-palette / off-palette, case-insensitive color, CMYK,
//!   font/weight, unconstrained categories, empty contract, governable policy.
//! - Diag-catalog: all 3 brand codes catalogued + governable.

mod common;

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

// ---------------------------------------------------------------------------
// Helper: minimal valid KDL document source with a brand block
// ---------------------------------------------------------------------------

/// Wrap KDL inner content in a minimal valid zenith document structure.
///
/// `tokens_block` is the inner content of the `tokens { … }` block (e.g. one
/// or more `token` lines). `brand_block` is the full `brand { … }` KDL text to
/// inject (or empty string to omit). The document always has exactly one page
/// with no children.
fn make_doc(tokens_inner: &str, brand_block: &str) -> String {
    format!(
        r##"zenith version=1 {{
  {brand_block}
  assets {{
  }}
  tokens format="zenith-token-v1" {{
    {tokens_inner}
  }}
  styles {{
  }}
  document id="doc.brand-test" {{
    page id="page.one" w=(px)1280 h=(px)720 {{
    }}
  }}
}}
"##
    )
}

fn parse_doc(src: &str) -> zenith_core::Document {
    let adapter = KdlAdapter;
    adapter.parse(src.as_bytes()).expect("parse must succeed")
}

// ---------------------------------------------------------------------------
// Parse tests
// ---------------------------------------------------------------------------

/// A full `brand { colors … fonts … weights … }` block round-trips through parse.
#[test]
fn parse_full_brand_block() {
    let src = make_doc(
        r##"token id="c.navy" type="color" value="#0b1f33""##,
        r##"brand {
    colors "#0b1f33" "#1b6cf0" "#ffffff"
    fonts "Noto Sans"
    weights 400 700
  }"##,
    );
    let doc = parse_doc(&src);
    let contract = &doc.brand_contract;
    assert_eq!(
        contract.allowed_colors,
        Some(vec![
            "#0b1f33".to_owned(),
            "#1b6cf0".to_owned(),
            "#ffffff".to_owned()
        ]),
        "colors should be parsed correctly"
    );
    assert_eq!(
        contract.allowed_fonts,
        Some(vec!["Noto Sans".to_owned()]),
        "fonts should be parsed correctly"
    );
    assert_eq!(
        contract.allowed_weights,
        Some(vec![400u32, 700u32]),
        "weights should be parsed correctly"
    );
}

/// An empty `brand {}` block (no children) → all categories remain `None`.
/// Absent children mean the `colors`/`fonts`/`weights` nodes never appear, so
/// all three fields remain `None` and `is_empty()` is true.
#[test]
fn parse_empty_brand_block_no_children() {
    let src = make_doc(
        "",
        r##"brand {
  }"##,
    );
    let doc = parse_doc(&src);
    assert!(
        doc.brand_contract.is_empty(),
        "empty brand block (no children) must produce an empty contract"
    );
}

/// Absent `brand` block → default empty contract.
#[test]
fn parse_absent_brand_block_gives_empty_contract() {
    // No brand block at all.
    let src = make_doc("", "");
    let doc = parse_doc(&src);
    assert!(
        doc.brand_contract.is_empty(),
        "absent brand block must produce an empty contract"
    );
    assert!(doc.brand_contract.allowed_colors.is_none());
    assert!(doc.brand_contract.allowed_fonts.is_none());
    assert!(doc.brand_contract.allowed_weights.is_none());
}

/// A `colors` child with a non-string argument (e.g. an integer) → ParseError.
#[test]
fn parse_invalid_color_type_is_parse_error() {
    let src = make_doc(
        "",
        r##"brand {
    colors 42
  }"##,
    );
    let adapter = KdlAdapter;
    let result = adapter.parse(src.as_bytes());
    assert!(
        result.is_err(),
        "non-string color argument must produce a ParseError"
    );
}

/// A `weights` child with a non-integer argument → ParseError.
#[test]
fn parse_invalid_weight_type_is_parse_error() {
    let src = make_doc(
        "",
        r##"brand {
    weights "bold"
  }"##,
    );
    let adapter = KdlAdapter;
    let result = adapter.parse(src.as_bytes());
    assert!(
        result.is_err(),
        "non-integer weight argument must produce a ParseError"
    );
}

/// A `weights` child with an out-of-range value (e.g. 50) → ParseError.
#[test]
fn parse_out_of_range_weight_is_parse_error() {
    let src = make_doc(
        "",
        r##"brand {
    weights 50
  }"##,
    );
    let adapter = KdlAdapter;
    let result = adapter.parse(src.as_bytes());
    assert!(result.is_err(), "weight < 100 must produce a ParseError");
}

/// A `weights` child with an out-of-range value (e.g. 950) → ParseError.
#[test]
fn parse_weight_above_900_is_parse_error() {
    let src = make_doc(
        "",
        r##"brand {
    weights 950
  }"##,
    );
    let adapter = KdlAdapter;
    let result = adapter.parse(src.as_bytes());
    assert!(result.is_err(), "weight > 900 must produce a ParseError");
}

/// Unknown children inside `brand { … }` are silently ignored (forward-compat).
#[test]
fn parse_unknown_brand_child_ignored() {
    let src = make_doc(
        "",
        r##"brand {
    colors "#ffffff"
    unknown-future-field "some value"
  }"##,
    );
    let doc = parse_doc(&src);
    assert_eq!(
        doc.brand_contract.allowed_colors,
        Some(vec!["#ffffff".to_owned()]),
        "known children must still be parsed; unknown children are ignored"
    );
}

/// A `fonts` child with a non-string argument → ParseError.
#[test]
fn parse_invalid_font_type_is_parse_error() {
    let src = make_doc(
        "",
        r##"brand {
    fonts 42
  }"##,
    );
    let adapter = KdlAdapter;
    let result = adapter.parse(src.as_bytes());
    assert!(
        result.is_err(),
        "non-string font argument must produce a ParseError"
    );
}

// ---------------------------------------------------------------------------
// Format round-trip tests
// ---------------------------------------------------------------------------

/// parse → format → parse preserves the brand contract.
#[test]
fn format_roundtrip_brand_block_preserved() {
    let src = make_doc(
        r##"token id="c.navy" type="color" value="#0b1f33""##,
        r##"brand {
    colors "#0b1f33" "#ffffff"
    fonts "Noto Sans"
    weights 400 700
  }"##,
    );
    let doc = parse_doc(&src);
    let formatted = format_document(&doc).expect("format must succeed");
    let formatted_str = String::from_utf8(formatted).expect("utf8");
    let doc2 = parse_doc(&formatted_str);

    // Compare sans spans (spans shift after reformat).
    let contract1 = {
        let mut c = doc.brand_contract.clone();
        c.source_span = None;
        c
    };
    let contract2 = {
        let mut c = doc2.brand_contract.clone();
        c.source_span = None;
        c
    };
    assert_eq!(
        contract1, contract2,
        "brand contract must survive a format round-trip"
    );
}

/// Absent brand block → formatter emits NO brand block.
#[test]
fn format_absent_brand_no_output() {
    let src = make_doc("", "");
    let doc = parse_doc(&src);
    let formatted = format_document(&doc).expect("format must succeed");
    let formatted_str = String::from_utf8(formatted).expect("utf8");
    assert!(
        !formatted_str
            .lines()
            .any(|l| l.trim_start().starts_with("brand ")
                || l.trim() == "brand"
                || l.trim_start().starts_with("brand{")),
        "absent brand contract must not emit a brand block; got:\n{formatted_str}"
    );
}

/// Format round-trip with all three categories present is byte-stable on second pass.
#[test]
fn format_is_idempotent() {
    let src = make_doc(
        r##"token id="c.navy" type="color" value="#0b1f33""##,
        r##"brand {
    colors "#0b1f33"
    fonts "Noto Sans"
    weights 400
  }"##,
    );
    let doc1 = parse_doc(&src);
    let fmt1 = String::from_utf8(format_document(&doc1).unwrap()).unwrap();
    let doc2 = parse_doc(&fmt1);
    let fmt2 = String::from_utf8(format_document(&doc2).unwrap()).unwrap();
    assert_eq!(fmt1, fmt2, "format must be idempotent");
}

/// Colors are emitted in lowercase in canonical form (the parser normalises them).
#[test]
fn format_colors_emitted_lowercase() {
    let src = make_doc(
        "",
        r##"brand {
    colors "#0B1F33"
  }"##,
    );
    let doc = parse_doc(&src);
    let formatted = String::from_utf8(format_document(&doc).unwrap()).unwrap();
    assert!(
        formatted.contains("\"#0b1f33\""),
        "formatted colors should be lowercase; got:\n{formatted}"
    );
    assert!(
        !formatted.contains("\"#0B1F33\""),
        "uppercase hex should not appear in formatted output; got:\n{formatted}"
    );
}

// ---------------------------------------------------------------------------
// Validate tests
// ---------------------------------------------------------------------------

/// On-palette color → no `brand.color_off_palette` diagnostic.
#[test]
fn validate_color_on_palette_no_diagnostic() {
    let src = make_doc(
        r##"token id="c.navy" type="color" value="#0b1f33""##,
        r##"brand {
    colors "#0b1f33"
  }"##,
    );
    let doc = parse_doc(&src);
    let report = validate(&doc);
    assert!(
        !has_code(&report, "brand.color_off_palette"),
        "on-palette color must not fire brand.color_off_palette; got: {:?}",
        codes(&report)
    );
}

/// Off-palette color → `brand.color_off_palette` Warning.
#[test]
fn validate_color_off_palette_fires_warning() {
    let src = make_doc(
        r##"token id="c.red" type="color" value="#ff0000""##,
        r##"brand {
    colors "#0b1f33"
  }"##,
    );
    let doc = parse_doc(&src);
    let report = validate(&doc);
    assert!(
        has_code(&report, "brand.color_off_palette"),
        "off-palette color must fire brand.color_off_palette; got: {:?}",
        codes(&report)
    );
}

/// Color comparison is case-insensitive across both palette and token value.
#[test]
fn validate_color_case_insensitive() {
    // Token value is uppercase; palette entry is lowercase.
    let src = make_doc(
        r##"token id="c.upper" type="color" value="#0B1F33""##,
        r##"brand {
    colors "#0b1f33"
  }"##,
    );
    let doc = parse_doc(&src);
    let report = validate(&doc);
    assert!(
        !has_code(&report, "brand.color_off_palette"),
        "color match must be case-insensitive; got: {:?}",
        codes(&report)
    );
}

/// Absent `colors` category → no color diagnostic regardless of token values.
#[test]
fn validate_unconstrained_color_no_diagnostic() {
    let src = make_doc(
        r##"token id="c.any" type="color" value="#deadbe""##,
        r##"brand {
    weights 400
  }"##,
    );
    let doc = parse_doc(&src);
    let report = validate(&doc);
    assert!(
        !has_code(&report, "brand.color_off_palette"),
        "unconstrained color category must not fire diagnostic; got: {:?}",
        codes(&report)
    );
}

/// Empty contract (no brand block) → zero brand diagnostics.
#[test]
fn validate_empty_contract_no_diagnostics() {
    let src = make_doc(r##"token id="c.navy" type="color" value="#0b1f33""##, "");
    let doc = parse_doc(&src);
    let report = validate(&doc);
    let brand_diags: Vec<_> = report
        .diagnostics
        .iter()
        .filter(|d| d.code.starts_with("brand."))
        .collect();
    assert!(
        brand_diags.is_empty(),
        "empty contract must not fire brand diagnostics; got: {brand_diags:?}"
    );
}

/// Font not in allowed list → `brand.font_not_allowed` Warning.
#[test]
fn validate_font_off_list_fires_warning() {
    let src = make_doc(
        r##"token id="font.body" type="fontFamily" value="Comic Sans MS""##,
        r##"brand {
    fonts "Noto Sans"
  }"##,
    );
    let doc = parse_doc(&src);
    let report = validate(&doc);
    assert!(
        has_code(&report, "brand.font_not_allowed"),
        "off-list font must fire brand.font_not_allowed; got: {:?}",
        codes(&report)
    );
}

/// Font in allowed list → no diagnostic.
#[test]
fn validate_font_on_list_no_diagnostic() {
    let src = make_doc(
        r##"token id="font.body" type="fontFamily" value="Noto Sans""##,
        r##"brand {
    fonts "Noto Sans"
  }"##,
    );
    let doc = parse_doc(&src);
    let report = validate(&doc);
    assert!(
        !has_code(&report, "brand.font_not_allowed"),
        "on-list font must not fire brand.font_not_allowed; got: {:?}",
        codes(&report)
    );
}

/// Weight not in allowed list → `brand.weight_not_allowed` Warning.
#[test]
fn validate_weight_off_list_fires_warning() {
    let src = make_doc(
        r##"token id="weight.thin" type="fontWeight" value=100"##,
        r##"brand {
    weights 400 700
  }"##,
    );
    let doc = parse_doc(&src);
    let report = validate(&doc);
    assert!(
        has_code(&report, "brand.weight_not_allowed"),
        "off-list weight must fire brand.weight_not_allowed; got: {:?}",
        codes(&report)
    );
}

/// Weight in allowed list → no diagnostic.
#[test]
fn validate_weight_on_list_no_diagnostic() {
    let src = make_doc(
        r##"token id="weight.regular" type="fontWeight" value=400"##,
        r##"brand {
    weights 400 700
  }"##,
    );
    let doc = parse_doc(&src);
    let report = validate(&doc);
    assert!(
        !has_code(&report, "brand.weight_not_allowed"),
        "on-list weight must not fire brand.weight_not_allowed; got: {:?}",
        codes(&report)
    );
}

/// Brand diagnostics are Warnings (not Errors or Advisories).
#[test]
fn validate_brand_diagnostics_are_warnings() {
    let src = make_doc(
        r##"token id="c.bad" type="color" value="#ff0000""##,
        r##"brand {
    colors "#0b1f33"
  }"##,
    );
    let doc = parse_doc(&src);
    let report = validate(&doc);
    for d in &report.diagnostics {
        if d.code.starts_with("brand.") {
            assert_eq!(
                d.severity,
                Severity::Warning,
                "brand diagnostic must be a Warning, got {:?}",
                d.severity
            );
        }
    }
}

/// A `deny "brand.color_off_palette"` entry in `diagnostics { … }` elevates the
/// Warning to Error (governable).
#[test]
fn validate_brand_color_is_governable_by_deny() {
    let src = r##"zenith version=1 {
  diagnostics {
    deny "brand.color_off_palette"
  }
  brand {
    colors "#0b1f33"
  }
  assets {
  }
  tokens format="zenith-token-v1" {
    token id="c.bad" type="color" value="#ff0000"
  }
  styles {
  }
  document id="doc.gov" {
    page id="page.one" w=(px)1280 h=(px)720 {
    }
  }
}
"##;
    let doc = parse_doc(src);
    let report = validate(&doc);
    let brand_diag = report
        .diagnostics
        .iter()
        .find(|d| d.code == "brand.color_off_palette");
    let d = brand_diag.expect("brand.color_off_palette must be present");
    assert_eq!(
        d.severity,
        Severity::Error,
        "deny on brand.color_off_palette must elevate to Error"
    );
}

/// An `allow "brand.color_off_palette"` entry suppresses the diagnostic.
#[test]
fn validate_brand_color_is_suppressable_by_allow() {
    let src = r##"zenith version=1 {
  diagnostics {
    allow "brand.color_off_palette"
  }
  brand {
    colors "#0b1f33"
  }
  assets {
  }
  tokens format="zenith-token-v1" {
    token id="c.bad" type="color" value="#ff0000"
  }
  styles {
  }
  document id="doc.gov" {
    page id="page.one" w=(px)1280 h=(px)720 {
    }
  }
}
"##;
    let doc = parse_doc(src);
    let report = validate(&doc);
    assert!(
        !has_code(&report, "brand.color_off_palette"),
        "allow on brand.color_off_palette must suppress the diagnostic; got: {:?}",
        codes(&report)
    );
}

// ---------------------------------------------------------------------------
// Diagnostic catalog tests
// ---------------------------------------------------------------------------

/// All three brand codes are in the catalog and governable (Warning severity).
#[test]
fn diag_catalog_brand_codes_are_catalogued_and_governable() {
    use zenith_core::diag_catalog::lookup;

    let cases = [
        "brand.color_off_palette",
        "brand.font_not_allowed",
        "brand.weight_not_allowed",
    ];

    for code in cases {
        let entry =
            lookup(code).unwrap_or_else(|| panic!("{code} must be in the diagnostic catalog"));
        assert_eq!(
            entry.severity,
            zenith_core::Severity::Warning,
            "{code} must have Warning severity in the catalog"
        );
        assert!(
            entry.is_governable(),
            "{code} must be governable (Warning severity)"
        );
    }
}