zenith-core 0.0.2

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
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
//! Integration tests: layout 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::*;

// ── Page bleed validation ─────────────────────────────────────────────

/// A page with a valid positive px bleed produces no bleed warning.
#[test]
fn valid_bleed_no_warning() {
    let mut page = minimal_page("page.bleed", vec![]);
    page.bleed = Some(px(35.0));
    let report = validate(&doc_with(vec![], vec![page]));
    assert!(
        !has_code(&report, "page.invalid_bleed"),
        "valid bleed must not warn: {:?}",
        codes(&report)
    );
}

/// A bleed declared with a non-resolvable unit (pct) warns but is not an error.
#[test]
fn bleed_bad_unit_warns_not_errors() {
    let mut page = minimal_page("page.bleed", vec![]);
    page.bleed = Some(Dimension {
        value: 5.0,
        unit: Unit::Pct,
    });
    let report = validate(&doc_with(vec![], vec![page]));
    assert!(
        has_code(&report, "page.invalid_bleed"),
        "bad-unit bleed must warn: {:?}",
        codes(&report)
    );
    assert!(
        !report.has_errors(),
        "bad-unit bleed must NOT be a hard error: {:?}",
        codes(&report)
    );
}

/// A negative bleed warns but is not an error.
#[test]
fn bleed_negative_warns_not_errors() {
    let mut page = minimal_page("page.bleed", vec![]);
    page.bleed = Some(px(-10.0));
    let report = validate(&doc_with(vec![], vec![page]));
    assert!(has_code(&report, "page.invalid_bleed"));
    assert!(!report.has_errors());
}

// ══════════════════════════════════════════════════════════════════════
// margin.violation advisory tests (book live-area)
// ══════════════════════════════════════════════════════════════════════

/// Helper: a book page with the standard four margins set
/// (inner 225, outer 150, top 210, bottom 240 on a 1240×1754 spread).
fn book_page(id: &str, children: Vec<Node>) -> Page {
    let mut page = bounded_page(id, 1240.0, 1754.0, children);
    page.margin_inner = Some(px(225.0));
    page.margin_outer = Some(px(150.0));
    page.margin_top = Some(px(210.0));
    page.margin_bottom = Some(px(240.0));
    page
}

/// Returns `true` when a `margin.violation` advisory names `node_id`.
fn has_margin_violation_for(report: &ValidationReport, node_id: &str) -> bool {
    report
        .diagnostics
        .iter()
        .any(|d| d.code == "margin.violation" && d.subject_id.as_deref() == Some(node_id))
}

#[test]
fn margin_recto_node_inside_live_area_no_violation() {
    // recto live area: x∈[225, 1090], y∈[210, 1514]. A rect fully inside.
    let doc = doc_with(
        vec![],
        vec![book_page(
            "page.recto",
            vec![rect_at("ok", 300.0, 300.0, 400.0, 400.0)],
        )],
    );
    let report = validate(&doc);
    assert!(
        !has_code(&report, "margin.violation"),
        "node inside the live area must not trip margin.violation; got {:?}",
        codes(&report)
    );
}

#[test]
fn margin_recto_node_left_of_inner_violates() {
    // mirror on, page 1 = recto → inner (225) insets the LEFT. A rect at x=100
    // crosses the left margin edge.
    let mut doc = doc_with(
        vec![],
        vec![book_page(
            "page.recto",
            vec![rect_at("bleeds", 100.0, 300.0, 50.0, 50.0)],
        )],
    );
    doc.mirror_margins = Some(true);
    let report = validate(&doc);
    assert!(
        has_margin_violation_for(&report, "bleeds"),
        "a recto node left of margin-inner must trip margin.violation; got {:?}",
        codes(&report)
    );
}

#[test]
fn margin_verso_parity_flips_inner_side() {
    // A rect at x=160 sits BETWEEN outer (150) and inner (225).
    // mirror on:
    //   - page 1 (recto): left inset = inner = 225 → 160 < 225 → VIOLATION.
    //   - page 2 (verso): left inset = outer = 150 → 160 ≥ 150 → NO violation.
    let recto_rect = rect_at("r.node", 160.0, 300.0, 400.0, 400.0);
    let verso_rect = rect_at("v.node", 160.0, 300.0, 400.0, 400.0);
    let mut doc = doc_with(
        vec![],
        vec![
            book_page("page.recto", vec![recto_rect]),
            book_page("page.verso", vec![verso_rect]),
        ],
    );
    doc.mirror_margins = Some(true);
    let report = validate(&doc);
    assert!(
        has_margin_violation_for(&report, "r.node"),
        "recto node at x=160 (< inner 225) must violate; got {:?}",
        codes(&report)
    );
    assert!(
        !has_margin_violation_for(&report, "v.node"),
        "verso node at x=160 (≥ outer 150) must NOT violate (inner side flipped); got {:?}",
        codes(&report)
    );
}

#[test]
fn margin_rtl_parity_is_mirror_of_ltr() {
    // page-progression="rtl" mirrors the spread: recto binding is on the RIGHT
    // (left inset = outer = 150), verso binding on the LEFT (left inset = inner
    // = 225) — the exact opposite of the LTR parity above. A rect at x=160:
    //   - page 1 (recto, RTL): left inset = outer = 150 → 160 ≥ 150 → NO violation.
    //   - page 2 (verso, RTL): left inset = inner = 225 → 160 < 225 → VIOLATION.
    let recto_rect = rect_at("r.node", 160.0, 300.0, 400.0, 400.0);
    let verso_rect = rect_at("v.node", 160.0, 300.0, 400.0, 400.0);
    let mut doc = doc_with(
        vec![],
        vec![
            book_page("page.recto", vec![recto_rect]),
            book_page("page.verso", vec![verso_rect]),
        ],
    );
    doc.mirror_margins = Some(true);
    doc.page_progression = Some("rtl".to_owned());
    let report = validate(&doc);
    assert!(
        !has_margin_violation_for(&report, "r.node"),
        "RTL recto node at x=160 (≥ outer 150) must NOT violate (inner on right); got {:?}",
        codes(&report)
    );
    assert!(
        has_margin_violation_for(&report, "v.node"),
        "RTL verso node at x=160 (< inner 225) must violate (inner on left); got {:?}",
        codes(&report)
    );
}

#[test]
fn margin_guide_role_is_exempt() {
    // A node with role="guide" intentionally lives in the margins → exempt.
    let mut guide = rect_at("guide.line", 0.0, 300.0, 50.0, 50.0);
    if let Node::Rect(r) = &mut guide {
        r.role = Some("guide".to_owned());
    }
    let doc = doc_with(vec![], vec![book_page("page.recto", vec![guide])]);
    let report = validate(&doc);
    assert!(
        !has_code(&report, "margin.violation"),
        "a role=guide node must be exempt from margin.violation; got {:?}",
        codes(&report)
    );
}

#[test]
fn margin_absent_skips_check() {
    // A plain page with no margins → the check is skipped entirely.
    let doc = doc_with(
        vec![],
        vec![bounded_page(
            "page.plain",
            1240.0,
            1754.0,
            vec![rect_at("any", 0.0, 0.0, 50.0, 50.0)],
        )],
    );
    let report = validate(&doc);
    assert!(
        !has_code(&report, "margin.violation"),
        "absent margins must skip the margin check; got {:?}",
        codes(&report)
    );
}

#[test]
fn margin_document_default_cascades_to_bare_page() {
    // The page declares NO margins, but the DOCUMENT sets all four defaults
    // (inner 225, outer 150, top 210, bottom 240). The bare page inherits them,
    // so its live area is computed and a node outside it trips margin.violation.
    // recto live area: x∈[225, 1090]. A rect at x=100 crosses the left edge.
    let mut doc = doc_with(
        vec![],
        vec![bounded_page(
            "page.bare",
            1240.0,
            1754.0,
            vec![rect_at("bleeds", 100.0, 300.0, 50.0, 50.0)],
        )],
    );
    doc.mirror_margins = Some(true);
    doc.margin_inner = Some(px(225.0));
    doc.margin_outer = Some(px(150.0));
    doc.margin_top = Some(px(210.0));
    doc.margin_bottom = Some(px(240.0));
    let report = validate(&doc);
    assert!(
        has_margin_violation_for(&report, "bleeds"),
        "a bare page must inherit the document default margins; got {:?}",
        codes(&report)
    );
}

#[test]
fn margin_page_inner_overrides_doc_default() {
    // Doc default inner = 225; the page overrides inner = 100 (keeps doc
    // outer/top/bottom). recto left inset becomes 100, so a rect at x=120 is now
    // INSIDE the live area and must NOT violate — proving the per-page override
    // wins over the doc default for inner only.
    let mut page = bounded_page(
        "page.over",
        1240.0,
        1754.0,
        vec![rect_at("ok", 120.0, 300.0, 50.0, 50.0)],
    );
    page.margin_inner = Some(px(100.0));
    let mut doc = doc_with(vec![], vec![page]);
    doc.mirror_margins = Some(true);
    doc.margin_inner = Some(px(225.0));
    doc.margin_outer = Some(px(150.0));
    doc.margin_top = Some(px(210.0));
    doc.margin_bottom = Some(px(240.0));
    let report = validate(&doc);
    assert!(
        !has_margin_violation_for(&report, "ok"),
        "the page's own inner margin (100) must override the doc default (225); got {:?}",
        codes(&report)
    );
}

#[test]
fn margin_doc_default_off_is_byte_identical_to_page_only() {
    // Regression guard for the default-off path: a doc with page margins but NO
    // document margins must produce EXACTLY the diagnostics it did before the
    // cascade existed. We assert against an explicit per-page book page with no
    // doc-level margins set.
    let mut doc = doc_with(
        vec![],
        vec![book_page(
            "page.recto",
            vec![rect_at("bleeds", 100.0, 300.0, 50.0, 50.0)],
        )],
    );
    doc.mirror_margins = Some(true);
    // No doc-level margins set — the cascade reads the page's own values verbatim.
    assert!(doc.margin_inner.is_none());
    let report = validate(&doc);
    assert!(
        has_margin_violation_for(&report, "bleeds"),
        "page-only margins must behave exactly as before; got {:?}",
        codes(&report)
    );
}

// ══════════════════════════════════════════════════════════════════════
// document.invalid_page_progression warning tests
// ══════════════════════════════════════════════════════════════════════

#[test]
fn page_progression_rtl_is_valid() {
    let mut doc = doc_with(vec![], vec![minimal_page("page.one", vec![])]);
    doc.page_progression = Some("rtl".to_owned());
    let report = validate(&doc);
    assert!(!has_code(&report, "document.invalid_page_progression"));
}

#[test]
fn page_progression_invalid_warns() {
    let mut doc = doc_with(vec![], vec![minimal_page("page.one", vec![])]);
    doc.page_progression = Some("sideways".to_owned());
    let report = validate(&doc);
    assert!(
        has_code(&report, "document.invalid_page_progression"),
        "an unrecognized page-progression must warn; got {:?}",
        codes(&report)
    );
    assert!(
        !report.has_errors(),
        "page-progression warning must not be a hard error"
    );
}

// ══════════════════════════════════════════════════════════════════════
// page-parity-start / page parity warning tests
// ══════════════════════════════════════════════════════════════════════

#[test]
fn page_parity_start_verso_is_valid() {
    let mut doc = doc_with(vec![], vec![minimal_page("page.one", vec![])]);
    doc.page_parity_start = Some("verso".to_owned());
    let report = validate(&doc);
    assert!(!has_code(&report, "document.invalid_page_parity_start"));
    assert!(!report.has_errors());
}

#[test]
fn page_parity_start_invalid_warns() {
    let mut doc = doc_with(vec![], vec![minimal_page("page.one", vec![])]);
    doc.page_parity_start = Some("sideways".to_owned());
    let report = validate(&doc);
    assert!(
        has_code(&report, "document.invalid_page_parity_start"),
        "an unrecognized page-parity-start must warn; got {:?}",
        codes(&report)
    );
    assert!(
        !report.has_errors(),
        "page-parity-start warning must not be a hard error"
    );
}

#[test]
fn page_parity_override_valid_does_not_warn() {
    let mut page = minimal_page("page.one", vec![]);
    page.parity = Some("verso".to_owned());
    let doc = doc_with(vec![], vec![page]);
    let report = validate(&doc);
    assert!(!has_code(&report, "page.invalid_parity"));
    assert!(!report.has_errors());
}

#[test]
fn page_parity_override_invalid_warns() {
    let mut page = minimal_page("page.one", vec![]);
    page.parity = Some("upside-down".to_owned());
    let doc = doc_with(vec![], vec![page]);
    let report = validate(&doc);
    assert!(
        has_code(&report, "page.invalid_parity"),
        "an unrecognized per-page parity must warn; got {:?}",
        codes(&report)
    );
    assert!(
        !report.has_errors(),
        "page parity warning must not be a hard error"
    );
}

// ── Page line-jumps validation ────────────────────────────────────────

#[test]
fn line_jumps_known_values_do_not_warn() {
    for value in ["none", "arc", "gap"] {
        let mut page = minimal_page("page.lj", vec![]);
        page.line_jumps = Some(value.to_owned());
        let report = validate(&doc_with(vec![], vec![page]));
        assert!(
            !has_code(&report, "page.invalid_line_jumps"),
            "line-jumps=\"{value}\" must not warn: {:?}",
            codes(&report)
        );
        assert!(!report.has_errors());
    }
}

#[test]
fn line_jumps_unknown_value_warns_not_errors() {
    let mut page = minimal_page("page.lj", vec![]);
    page.line_jumps = Some("sproing".to_owned());
    let report = validate(&doc_with(vec![], vec![page]));
    assert!(
        has_code(&report, "page.invalid_line_jumps"),
        "an unrecognized line-jumps value must warn; got {:?}",
        codes(&report)
    );
    assert!(
        !report.has_errors(),
        "line-jumps warning must not be a hard error"
    );
}

// ══════════════════════════════════════════════════════════════════════
// Configurable parity drives the mirrored-margin live area
// ══════════════════════════════════════════════════════════════════════

/// With `mirror-margins`, `page-parity-start="verso"` makes page 1 a VERSO, so
/// its binding (inner) margin moves to the right and the left inset becomes the
/// OUTER margin — flipping the `margin.violation` advisory's named parity and
/// live-area x relative to the default (page 1 = recto).
#[test]
fn page_parity_start_verso_flips_page_one_live_area() {
    // book_page: inner=225, outer=150 on a 1240-wide page.
    // Default (recto): live x = inner = 225. A node at x=160 crosses the LEFT.
    // start=verso (page 1 = verso): live x = outer = 150. The SAME node at x=160
    // is now INSIDE on the left, but a node at x=140 would cross.
    let probe = rect_at("probe", 160.0, 300.0, 400.0, 400.0);

    // Default: page 1 recto, inner=225 → node at 160 is left of the live area.
    let mut doc_default = doc_with(vec![], vec![book_page("p1", vec![probe.clone()])]);
    doc_default.mirror_margins = Some(true);
    let report_default = validate(&doc_default);
    assert!(
        has_margin_violation_for(&report_default, "probe"),
        "recto page-1 default: node at x=160 must violate the inner(225) live edge; got {:?}",
        codes(&report_default)
    );

    // start=verso: page 1 verso, outer=150 → node at 160 is now inside on the left.
    let mut doc_verso = doc_with(vec![], vec![book_page("p1", vec![probe.clone()])]);
    doc_verso.mirror_margins = Some(true);
    doc_verso.page_parity_start = Some("verso".to_owned());
    let report_verso = validate(&doc_verso);
    assert!(
        !has_margin_violation_for(&report_verso, "probe"),
        "verso page-1: node at x=160 must sit inside the outer(150) live edge; got {:?}",
        codes(&report_verso)
    );
}

/// An explicit per-page `parity="recto"` override flips a page back even when
/// `page-parity-start="verso"` would otherwise make it a verso.
#[test]
fn page_parity_override_flips_one_page_live_area() {
    let probe = rect_at("probe", 160.0, 300.0, 400.0, 400.0);

    let mut page = book_page("p1", vec![probe]);
    page.parity = Some("recto".to_owned());
    let mut doc = doc_with(vec![], vec![page]);
    doc.mirror_margins = Some(true);
    doc.page_parity_start = Some("verso".to_owned());
    let report = validate(&doc);
    // Override forces recto → inner=225 live edge → node at x=160 violates again.
    assert!(
        has_margin_violation_for(&report, "probe"),
        "explicit parity=recto must restore the inner(225) live edge; got {:?}",
        codes(&report)
    );
}

// ══════════════════════════════════════════════════════════════════════
// Fold content-crossing advisories
// ══════════════════════════════════════════════════════════════════════

/// Helper: build a page with explicit folds and children (px page rect).
fn page_with_folds(id: &str, w: f64, h: f64, folds: Vec<Fold>, children: Vec<Node>) -> Page {
    Page {
        id: id.to_owned(),
        name: None,
        width: px(w),
        height: px(h),
        background: None,
        bleed: None,
        margin_inner: None,
        margin_outer: None,
        margin_top: None,
        margin_bottom: None,
        baseline_grid: None,
        line_jumps: None,
        parity: None,
        master: None,
        safe_zones: Vec::new(),
        folds,
        block_styles: Vec::new(),
        children,
        source_span: None,
    }
}

/// Helper: build a fold of the given orientation at the given px position.
fn fold(id: &str, orientation: &str, position: f64) -> Fold {
    Fold {
        id: id.to_owned(),
        orientation: orientation.to_owned(),
        position: Some(px(position)),
        source_span: None,
    }
}

/// A vertical fold at x=1169 with a node spanning x=80..2430 → crossing.
#[test]
fn vertical_fold_crossed_by_node_advises() {
    let doc = doc_with(
        vec![],
        vec![page_with_folds(
            "page.one",
            2480.0,
            1000.0,
            vec![fold("fold.1", "vertical", 1169.0)],
            vec![rect_at("rect.wide", 80.0, 100.0, 2350.0, 200.0)],
        )],
    );
    let report = validate(&doc);
    assert!(
        has_code(&report, "fold.content_crossing"),
        "expected fold.content_crossing; codes: {:?}",
        codes(&report)
    );
}

/// A node entirely left of the vertical fold → no crossing.
#[test]
fn vertical_fold_not_crossed_is_clean() {
    let doc = doc_with(
        vec![],
        vec![page_with_folds(
            "page.one",
            2480.0,
            1000.0,
            vec![fold("fold.1", "vertical", 1169.0)],
            // Right edge at 80+200 = 280 < 1169 → fully left of the fold.
            vec![rect_at("rect.left", 80.0, 100.0, 200.0, 200.0)],
        )],
    );
    let report = validate(&doc);
    assert!(
        !has_code(&report, "fold.content_crossing"),
        "node left of fold must not cross; codes: {:?}",
        codes(&report)
    );
}

/// A horizontal fold at y=500 with a node spanning y=100..900 → crossing.
#[test]
fn horizontal_fold_crossed_by_node_advises() {
    let doc = doc_with(
        vec![],
        vec![page_with_folds(
            "page.one",
            2480.0,
            1000.0,
            vec![fold("fold.h", "horizontal", 500.0)],
            vec![rect_at("rect.tall", 100.0, 100.0, 200.0, 800.0)],
        )],
    );
    let report = validate(&doc);
    assert!(
        has_code(&report, "fold.content_crossing"),
        "expected fold.content_crossing for horizontal fold; codes: {:?}",
        codes(&report)
    );
}

/// A node entirely above the horizontal fold → no crossing.
#[test]
fn horizontal_fold_not_crossed_is_clean() {
    let doc = doc_with(
        vec![],
        vec![page_with_folds(
            "page.one",
            2480.0,
            1000.0,
            vec![fold("fold.h", "horizontal", 500.0)],
            // Bottom edge at 100+200 = 300 < 500 → fully above the fold.
            vec![rect_at("rect.top", 100.0, 100.0, 200.0, 200.0)],
        )],
    );
    let report = validate(&doc);
    assert!(
        !has_code(&report, "fold.content_crossing"),
        "node above fold must not cross; codes: {:?}",
        codes(&report)
    );
}

/// A fold content-crossing is ADVISORY — it must not flag the report errored.
#[test]
fn fold_content_crossing_is_advisory_not_error() {
    let doc = doc_with(
        vec![],
        vec![page_with_folds(
            "page.one",
            2480.0,
            1000.0,
            vec![fold("fold.1", "vertical", 1169.0)],
            vec![rect_at("rect.wide", 80.0, 100.0, 2350.0, 200.0)],
        )],
    );
    let report = validate(&doc);
    assert!(
        report
            .diagnostics
            .iter()
            .any(|d| d.code == "fold.content_crossing" && d.severity == Severity::Advisory),
        "fold.content_crossing must be Advisory; codes: {:?}",
        codes(&report)
    );
    assert!(!report.has_errors());
}

/// A fold with no resolvable position → no crossing advisory (skipped).
#[test]
fn fold_without_position_is_skipped() {
    let doc = doc_with(
        vec![],
        vec![page_with_folds(
            "page.one",
            2480.0,
            1000.0,
            vec![Fold {
                id: "fold.none".to_owned(),
                orientation: "vertical".to_owned(),
                position: None,
                source_span: None,
            }],
            vec![rect_at("rect.wide", 80.0, 100.0, 2350.0, 200.0)],
        )],
    );
    let report = validate(&doc);
    assert!(
        !has_code(&report, "fold.content_crossing"),
        "fold without position must be skipped; codes: {:?}",
        codes(&report)
    );
}

// ══════════════════════════════════════════════════════════════════════
// safe-zone advisory tests
// ══════════════════════════════════════════════════════════════════════

/// Helper: build a page with explicit safe-zones and children (px page rect).
fn page_with_zones(
    id: &str,
    w: f64,
    h: f64,
    safe_zones: Vec<SafeZone>,
    children: Vec<Node>,
) -> Page {
    Page {
        id: id.to_owned(),
        name: None,
        width: px(w),
        height: px(h),
        background: None,
        bleed: None,
        margin_inner: None,
        margin_outer: None,
        margin_top: None,
        margin_bottom: None,
        baseline_grid: None,
        line_jumps: None,
        parity: None,
        master: None,
        safe_zones,
        folds: Vec::new(),
        block_styles: Vec::new(),
        children,
        source_span: None,
    }
}

/// Helper: build a safe-zone rect of the given type at (x, y, w, h) px.
fn zone(id: &str, zone_type: SafeZoneType, x: f64, y: f64, w: f64, h: f64) -> SafeZone {
    SafeZone {
        id: id.to_owned(),
        zone_type,
        x: px(x),
        y: px(y),
        w: px(w),
        h: px(h),
        label: None,
        source_span: None,
    }
}

/// Helper: a full-bleed background image covering the whole page rect.
fn image_at(id: &str, x: f64, y: f64, w: f64, h: f64) -> Node {
    Node::Image(ImageNode {
        id: id.to_owned(),
        name: None,
        role: None,
        asset: "asset.bg".to_owned(),
        x: Some(pxv(x)),
        y: Some(pxv(y)),
        w: Some(pxv(w)),
        h: Some(pxv(h)),
        src_x: None,
        src_y: None,
        src_w: None,
        src_h: None,
        fit: None,
        clip: None,
        clip_radius: None,
        object_position_x: None,
        object_position_y: None,
        opacity: None,
        shadow: None,
        filter: None,
        mask: None,
        visible: None,
        locked: None,
        rotate: None,
        blend_mode: None,
        blur: None,
        style: 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(),
    })
}

/// An exclusion zone overlapped by a content node → `safe_zone.violation`.
#[test]
fn exclusion_zone_overlapping_node_violates() {
    let doc = doc_with(
        vec![],
        vec![page_with_zones(
            "page.one",
            1500.0,
            500.0,
            vec![zone(
                "sz.avatar",
                SafeZoneType::Exclusion,
                0.0,
                358.0,
                175.0,
                142.0,
            )],
            vec![rect_at("rect.bad", 50.0, 380.0, 100.0, 80.0)],
        )],
    );
    let report = validate(&doc);
    assert!(
        has_code(&report, "safe_zone.violation"),
        "expected safe_zone.violation; codes: {:?}",
        codes(&report)
    );
}

/// An exclusion zone NOT overlapped by a content node → no violation.
#[test]
fn exclusion_zone_non_overlapping_node_is_clean() {
    let doc = doc_with(
        vec![],
        vec![page_with_zones(
            "page.one",
            1500.0,
            500.0,
            vec![zone(
                "sz.avatar",
                SafeZoneType::Exclusion,
                0.0,
                358.0,
                175.0,
                142.0,
            )],
            vec![rect_at("rect.ok", 600.0, 40.0, 100.0, 80.0)],
        )],
    );
    let report = validate(&doc);
    assert!(
        !has_code(&report, "safe_zone.violation"),
        "non-overlapping node must not violate; codes: {:?}",
        codes(&report)
    );
}

/// A full-bleed background image overlapping an exclusion zone → no violation
/// (full-bleed nodes are exempt).
#[test]
fn full_bleed_background_is_exempt_from_exclusion_zone() {
    let doc = doc_with(
        vec![],
        vec![page_with_zones(
            "page.one",
            1500.0,
            500.0,
            vec![zone(
                "sz.avatar",
                SafeZoneType::Exclusion,
                0.0,
                358.0,
                175.0,
                142.0,
            )],
            vec![image_at("img.bg", 0.0, 0.0, 1500.0, 500.0)],
        )],
    );
    let report = validate(&doc);
    assert!(
        !has_code(&report, "safe_zone.violation"),
        "full-bleed background must be exempt; codes: {:?}",
        codes(&report)
    );
}

/// A required zone with a node fully outside → `safe_zone.violation`.
#[test]
fn required_zone_node_fully_outside_violates() {
    let doc = doc_with(
        vec![],
        vec![page_with_zones(
            "page.one",
            1500.0,
            500.0,
            vec![zone(
                "sz.title",
                SafeZoneType::Required,
                600.0,
                40.0,
                300.0,
                100.0,
            )],
            vec![rect_at("rect.out", 0.0, 400.0, 50.0, 50.0)],
        )],
    );
    let report = validate(&doc);
    assert!(
        has_code(&report, "safe_zone.violation"),
        "node outside required zone must violate; codes: {:?}",
        codes(&report)
    );
}

/// A required zone with a node overlapping it → no violation (lenient).
#[test]
fn required_zone_overlapping_node_is_clean() {
    let doc = doc_with(
        vec![],
        vec![page_with_zones(
            "page.one",
            1500.0,
            500.0,
            vec![zone(
                "sz.title",
                SafeZoneType::Required,
                600.0,
                40.0,
                300.0,
                100.0,
            )],
            vec![rect_at("rect.in", 650.0, 50.0, 100.0, 40.0)],
        )],
    );
    let report = validate(&doc);
    assert!(
        !has_code(&report, "safe_zone.violation"),
        "node overlapping required zone must not violate; codes: {:?}",
        codes(&report)
    );
}

/// A safe-zone violation is ADVISORY — it must not flag the report as errored.
#[test]
fn safe_zone_violation_is_advisory_not_error() {
    let doc = doc_with(
        vec![],
        vec![page_with_zones(
            "page.one",
            1500.0,
            500.0,
            vec![zone(
                "sz.avatar",
                SafeZoneType::Exclusion,
                0.0,
                358.0,
                175.0,
                142.0,
            )],
            vec![rect_at("rect.bad", 50.0, 380.0, 100.0, 80.0)],
        )],
    );
    let report = validate(&doc);
    assert!(
        report
            .diagnostics
            .iter()
            .any(|d| d.code == "safe_zone.violation" && d.severity == Severity::Advisory),
        "safe_zone.violation must be Advisory severity; codes: {:?}",
        codes(&report)
    );
    assert!(
        !report.has_errors(),
        "safe_zone.violation must not make the report errored"
    );
}