zenith-tool 0.0.5

The Zenith command-line interface (the `zenith` binary) for the design-document toolchain.
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
//! Integration tests: read fixture files from the workspace root `examples/`
//! directory and render them to PNG, asserting valid, non-empty output and
//! byte-identical determinism across two back-to-back renders.

use zenith_cli::commands::render::to_png_with_dir;
use zenith_cli::config::CliPolicyFlags;

/// Render `examples/<name>.zen` twice and assert the output is a valid,
/// byte-identical PNG.  All per-fixture tests delegate here.
///
/// The asset provider is built from the example's own directory (`examples/`)
/// so that `image` nodes can source their raster bytes.
fn assert_example_renders(name: &str) {
    let manifest_dir = env!("CARGO_MANIFEST_DIR");
    let examples_dir = std::path::Path::new(manifest_dir)
        .join("..")
        .join("examples");
    let fixture = examples_dir.join(format!("{name}.zen"));

    let src = std::fs::read_to_string(&fixture)
        .unwrap_or_else(|e| panic!("could not read {}: {}", fixture.display(), e));

    let png = to_png_with_dir(
        &src,
        Some(&examples_dir),
        1,
        false,
        &CliPolicyFlags::default(),
        None,
    )
    .unwrap_or_else(|e| panic!("render failed (exit {}): {}", e.exit_code, e.message))
    .png;

    // Must be non-empty.
    assert!(!png.is_empty(), "PNG output must not be empty");

    // Must start with the PNG magic bytes.
    assert!(
        png.len() >= 8,
        "PNG must have at least 8 bytes; got {}",
        png.len()
    );
    assert_eq!(
        &png[0..8],
        &[0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A],
        "first 8 bytes must be the PNG signature"
    );

    // Determinism: two renders must be byte-identical.
    let png2 = to_png_with_dir(
        &src,
        Some(&examples_dir),
        1,
        false,
        &CliPolicyFlags::default(),
        None,
    )
    .unwrap_or_else(|e| panic!("second render failed (exit {}): {}", e.exit_code, e.message))
    .png;
    assert_eq!(
        png, png2,
        "two renders of {name}.zen must produce identical bytes"
    );
}

#[test]
fn rect_zen_renders_to_valid_png() {
    assert_example_renders("rect");
}

#[test]
fn line_zen_renders_to_valid_png() {
    assert_example_renders("line");
}

#[test]
fn group_zen_renders_to_valid_png() {
    assert_example_renders("group");
}

#[test]
fn ellipse_zen_renders_to_valid_png() {
    assert_example_renders("ellipse");
}

#[test]
fn frame_zen_renders_to_valid_png() {
    assert_example_renders("frame");
}

#[test]
fn image_zen_renders_to_valid_png() {
    assert_example_renders("image");
}

#[test]
fn polygon_zen_renders_to_valid_png() {
    assert_example_renders("polygon");
}

#[test]
fn polyline_zen_renders_to_valid_png() {
    assert_example_renders("polyline");
}

#[test]
fn star_zen_renders_to_valid_png() {
    assert_example_renders("star");
}

#[test]
fn pattern_zen_renders_to_valid_png() {
    assert_example_renders("pattern");
}

#[test]
fn anchors_zen_renders_to_valid_png() {
    assert_example_renders("anchors");
}

#[test]
fn blur_zen_renders_to_valid_png() {
    assert_example_renders("blur");
}

#[test]
fn bold_zen_renders_to_valid_png() {
    assert_example_renders("bold");
}

#[test]
fn decorations_zen_renders_to_valid_png() {
    assert_example_renders("decorations");
}

#[test]
fn filter_zen_renders_to_valid_png() {
    assert_example_renders("filter");
}

#[test]
fn flowchart_zen_renders_to_valid_png() {
    assert_example_renders("flowchart");
}

#[test]
fn gradient_zen_renders_to_valid_png() {
    assert_example_renders("gradient");
}

#[test]
fn hello_zen_renders_to_valid_png() {
    assert_example_renders("hello");
}

#[test]
fn italic_zen_renders_to_valid_png() {
    assert_example_renders("italic");
}

#[test]
fn mask_zen_renders_to_valid_png() {
    assert_example_renders("mask");
}

#[test]
fn multipage_zen_renders_to_valid_png() {
    assert_example_renders("multipage");
}

#[test]
fn richtext_zen_renders_to_valid_png() {
    assert_example_renders("richtext");
}

#[test]
fn shadow_zen_renders_to_valid_png() {
    assert_example_renders("shadow");
}

#[test]
fn stroke_align_zen_renders_to_valid_png() {
    assert_example_renders("stroke-align");
}

#[test]
fn table_zen_renders_to_valid_png() {
    assert_example_renders("table");
}

#[test]
fn chart_zen_renders_to_valid_png() {
    assert_example_renders("chart");
}

#[test]
fn noise_zen_renders_to_valid_png() {
    assert_example_renders("noise");
}

#[test]
fn stack_zen_renders_to_valid_png() {
    assert_example_renders("stack");
}

#[test]
fn connector_routing_zen_renders_to_valid_png() {
    assert_example_renders("connector-routing");
}

#[test]
fn styled_zen_renders_to_valid_png() {
    assert_example_renders("styled");
}

#[test]
fn rounded_zen_renders_to_valid_png() {
    assert_example_renders("rounded");
}

#[test]
fn code_zen_renders_to_valid_png() {
    assert_example_renders("code");
}

#[test]
fn markdown_zen_renders_to_valid_png() {
    assert_example_renders("markdown");
}

#[test]
fn article_zen_renders_to_valid_png() {
    assert_example_renders("article");
}

// ── --locked sha256 verification ────────────────────────────────────────────────

/// Path to the workspace-root `examples/` directory (where `assets/swatch.png`
/// lives) so `--locked` asset verification can read real bytes.
fn examples_dir() -> std::path::PathBuf {
    std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
        .join("..")
        .join("examples")
}

/// The known SHA-256 of `examples/assets/swatch.png`.
const SWATCH_SHA256: &str = "9c3fdf4f9c609c6ec749d4ccbd75fda384b32962d5d3893424e22e1fad44c042";

/// Build a `.zen` document referencing `assets/swatch.png` with the given
/// `sha256` property text. When `sha256` is `None` the property is omitted.
fn swatch_doc(sha256: Option<&str>) -> String {
    let sha_prop = match sha256 {
        Some(h) => format!(" sha256=\"{h}\""),
        None => String::new(),
    };
    format!(
        r##"zenith version=1 {{
  project id="proj.image" name="Image Example"
  assets {{
    asset id="asset.swatch" kind="image" src="assets/swatch.png"{sha_prop}
  }}
  tokens format="zenith-token-v1" {{
    token id="color.bg" type="color" value="#f8fafc"
  }}
  styles {{
  }}
  document id="doc.image" title="Image Example" {{
    page id="page.image" w=(px)320 h=(px)200 background=(token)"color.bg" {{
      image id="img.swatch" asset="asset.swatch" x=(px)40 y=(px)40 w=(px)160 h=(px)120 fit="stretch"
    }}
  }}
}}
"##
    )
}

#[test]
fn locked_correct_sha256_renders_ok() {
    let src = swatch_doc(Some(SWATCH_SHA256));
    let result = to_png_with_dir(
        &src,
        Some(&examples_dir()),
        1,
        true,
        &CliPolicyFlags::default(),
        None,
    );
    assert!(
        result.is_ok(),
        "correct sha256 in --locked mode must render: {:?}",
        result.err().map(|e| e.message)
    );
}

#[test]
fn locked_wrong_sha256_errors_exit_2() {
    // Flip the last hex digit.
    let wrong = "9c3fdf4f9c609c6ec749d4ccbd75fda384b32962d5d3893424e22e1fad44c043";
    let src = swatch_doc(Some(wrong));
    let err = to_png_with_dir(
        &src,
        Some(&examples_dir()),
        1,
        true,
        &CliPolicyFlags::default(),
        None,
    )
    .expect_err("wrong sha256 in --locked mode must error");
    assert_eq!(err.exit_code, 2, "sha256 mismatch must exit with code 2");
}

#[test]
fn locked_missing_sha256_errors_exit_2() {
    let src = swatch_doc(None);
    let err = to_png_with_dir(
        &src,
        Some(&examples_dir()),
        1,
        true,
        &CliPolicyFlags::default(),
        None,
    )
    .expect_err("missing sha256 in --locked mode must error");
    assert_eq!(err.exit_code, 2, "missing sha256 must exit with code 2");
}

#[test]
fn unlocked_wrong_sha256_renders_ok() {
    let wrong = "0000000000000000000000000000000000000000000000000000000000000000";
    let src = swatch_doc(Some(wrong));
    let result = to_png_with_dir(
        &src,
        Some(&examples_dir()),
        1,
        false,
        &CliPolicyFlags::default(),
        None,
    );
    assert!(
        result.is_ok(),
        "wrong sha256 must be ignored when not locked: {:?}",
        result.err().map(|e| e.message)
    );
}

// ── asset.missing diagnostics ─────────────────────────────────────────────────

/// A `.zen` document referencing an asset whose file does not exist under the
/// project directory. The `src` points at a deterministic, never-present path
/// inside `examples/`.
fn missing_asset_doc() -> String {
    r##"zenith version=1 {
  project id="proj.missing" name="Missing Asset"
  assets {
    asset id="asset.absent" kind="image" src="assets/__zenith_does_not_exist__.png"
  }
  tokens format="zenith-token-v1" {
    token id="color.bg" type="color" value="#f8fafc"
  }
  styles {
  }
  document id="doc.missing" title="Missing Asset" {
    page id="page.missing" w=(px)320 h=(px)200 background=(token)"color.bg" {
      image id="img.absent" asset="asset.absent" x=(px)40 y=(px)40 w=(px)160 h=(px)120 fit="stretch"
    }
  }
}
"##
    .to_owned()
}

#[test]
fn render_missing_asset_yields_asset_missing_error_diagnostic() {
    // Render still returns Ok (the artifact carries the Error); the lib.rs gate
    // is what blocks the PNG write.
    let src = missing_asset_doc();
    let artifact = to_png_with_dir(
        &src,
        Some(&examples_dir()),
        1,
        false,
        &CliPolicyFlags::default(),
        None,
    )
    .expect("render must not hard-fail; the missing asset is carried as a diagnostic");
    let has_missing = artifact
        .diagnostics
        .iter()
        .any(|d| d.code == "asset.missing" && d.severity == zenith_core::Severity::Error);
    assert!(
        has_missing,
        "artifact must carry an asset.missing Error diagnostic; got: {:?}",
        artifact
            .diagnostics
            .iter()
            .map(|d| d.code.as_str())
            .collect::<Vec<_>>()
    );
}

#[test]
fn validate_missing_asset_reports_error_exit_1() {
    let src = missing_asset_doc();
    let out = zenith_cli::commands::validate::run(
        &src,
        Some(&examples_dir()),
        false,
        &zenith_cli::config::CliPolicyFlags::default(),
    );
    assert_eq!(
        out.exit_code, 1,
        "a missing asset must make validate report a hard error; stdout: {}",
        out.stdout
    );
    assert!(
        out.stdout.contains("asset.missing"),
        "validate output must mention asset.missing; got: {}",
        out.stdout
    );
}

#[test]
fn validate_missing_asset_json_reports_error() {
    let src = missing_asset_doc();
    let out = zenith_cli::commands::validate::run(
        &src,
        Some(&examples_dir()),
        true,
        &zenith_cli::config::CliPolicyFlags::default(),
    );
    assert!(
        out.stdout.contains("asset.missing"),
        "validate --json output must include asset.missing; got: {}",
        out.stdout
    );
    assert!(
        out.stdout.contains(r#""valid": false"#),
        "validate --json must mark the doc invalid; got: {}",
        out.stdout
    );
}