azul-core 0.0.8

Common datatypes used for the Azul document object model, shared across all azul-* crates
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
//! prop_cache Tests
//!
//! Tests for the CSS property cache, dependency chains, and inheritance system.

use azul_core::{
    dom::{Dom, NodeType},
    prop_cache::{CssPropertyOrigin, CssPropertyWithOrigin},
    styled_dom::StyledDom,
};
use azul_css::dynamic_selector::CssPropertyWithConditions;
use azul_css::{
    css::Css,
    css::CssPropertyValue,
    props::{
        basic::{font::StyleFontSize, length::SizeMetric, pixel::PixelValue},
        property::{CssProperty, CssPropertyType},
    },
};

/// Helper: look up a CssPropertyType in a sorted Vec of (CssPropertyType, CssPropertyWithOrigin)
fn find_prop<'a>(
    computed: &'a [(CssPropertyType, CssPropertyWithOrigin)],
    prop_type: &CssPropertyType,
) -> Option<&'a CssPropertyWithOrigin> {
    computed
        .binary_search_by_key(prop_type, |(k, _)| *k)
        .ok()
        .map(|idx| &computed[idx].1)
}

// Helper macro to create a StyledDom and get the CSS property cache.
// Calls compute_inherited_values to populate computed_values
// (build_compact_cache_with_inheritance handles inheritance in compact
// format and does not fill computed_values).
macro_rules! setup_styled_dom {
    ($dom:expr) => {{
        let mut dom = $dom;
        let styled_dom = StyledDom::create(&mut dom, Css::empty());
        let mut cache = styled_dom.css_property_cache.ptr.clone();
        let h = styled_dom.node_hierarchy.as_container();
        let d = styled_dom.node_data.as_container();
        cache.compute_inherited_values(h.internal, d.internal);
        (styled_dom, cache)
    }};
    ($dom:expr, $css:expr) => {{
        let mut dom = $dom;
        let (css, _) = azul_css::parser2::new_from_str($css);
        let css_wrapper = css;
        let styled_dom = StyledDom::create(&mut dom, css_wrapper);
        let mut cache = styled_dom.css_property_cache.ptr.clone();
        let h = styled_dom.node_hierarchy.as_container();
        let d = styled_dom.node_data.as_container();
        cache.compute_inherited_values(h.internal, d.internal);
        (styled_dom, cache)
    }};
}

#[test]
fn test_computed_values_exist_for_all_nodes() {
    let dom = Dom::create_div()
        .with_child(Dom::create_node(NodeType::P).with_child(Dom::create_text("Text")));

    let (_styled_dom, cache) = setup_styled_dom!(dom);

    // Check that computed_values exist for all nodes
    let node_0 = azul_core::dom::NodeId::new(0);
    let node_1 = azul_core::dom::NodeId::new(1);
    let node_2 = azul_core::dom::NodeId::new(2);

    assert!(
        cache.computed_values.get(node_0.index()).is_some(),
        "Node 0 should have computed values"
    );
    assert!(
        cache.computed_values.get(node_1.index()).is_some(),
        "Node 1 should have computed values"
    );
    assert!(
        cache.computed_values.get(node_2.index()).is_some(),
        "Node 2 should have computed values"
    );
}

#[test]
fn test_inline_css_takes_precedence() {
    let dom = Dom::create_div().with_css_props(
        vec![CssPropertyWithConditions::simple(CssProperty::font_size(
            StyleFontSize::px(24.0),
        ))]
        .into(),
    );

    let (_styled_dom, cache) = setup_styled_dom!(dom);

    let node_0 = azul_core::dom::NodeId::new(0);
    let computed = cache
        .computed_values
        .get(node_0.index())
        .expect("should have computed values");
    let font_size_prop = find_prop(computed, &CssPropertyType::FontSize)
        .expect("should have font-size");

    assert_eq!(font_size_prop.origin, CssPropertyOrigin::Own);

    if let CssProperty::FontSize(val) = &font_size_prop.property {
        if let Some(size) = val.get_property() {
            assert!((size.inner.number.get() - 24.0).abs() < 0.001);
        } else {
            panic!("FontSize should have value");
        }
    } else {
        panic!("Property should be FontSize");
    }
}

#[test]
fn test_css_stylesheet_applies() {
    let dom = Dom::create_div()
        .with_child(Dom::create_node(NodeType::P).with_child(Dom::create_text("Text")));

    let css = "p { font-size: 18px; }";
    let (_styled_dom, cache) = setup_styled_dom!(dom, css);

    let p_id = azul_core::dom::NodeId::new(1);
    let computed = cache
        .computed_values
        .get(p_id.index())
        .expect("p should have computed values");

    if let Some(font_size_prop) = find_prop(computed, &CssPropertyType::FontSize) {
        if let CssProperty::FontSize(val) = &font_size_prop.property {
            if let Some(size) = val.get_property() {
                assert!((size.inner.number.get() - 18.0).abs() < 0.001);
            }
        }
    }
}

#[test]
fn test_inherited_property_has_correct_origin() {
    let dom = Dom::create_div()
        .with_css_props(
            vec![CssPropertyWithConditions::simple(CssProperty::font_size(
                StyleFontSize::px(20.0),
            ))]
            .into(),
        )
        .with_child(Dom::create_node(NodeType::P).with_child(Dom::create_text("Text")));

    let (_styled_dom, cache) = setup_styled_dom!(dom);

    // P (node 1) should have font-size (either inherited or from UA CSS)
    // The important thing is the VALUE is correct (20px from parent)
    let p_id = azul_core::dom::NodeId::new(1);
    let computed = cache
        .computed_values
        .get(p_id.index())
        .expect("p should have computed values");
    let font_size_prop = find_prop(computed, &CssPropertyType::FontSize)
        .expect("should have font-size");

    // Check that P has the correct font-size value (inherited from div)
    if let CssProperty::FontSize(val) = &font_size_prop.property {
        if let Some(size) = val.get_property() {
            assert!(
                (size.inner.number.get() - 20.0).abs() < 0.001,
                "P should have inherited font-size 20px from parent, got {}",
                size.inner.number.get()
            );
        } else {
            panic!("FontSize should have value");
        }
    } else {
        panic!("Property should be FontSize");
    }
}

#[test]
fn test_own_property_overrides_inherited() {
    let dom = Dom::create_div()
        .with_css_props(
            vec![CssPropertyWithConditions::simple(CssProperty::font_size(
                StyleFontSize::px(20.0),
            ))]
            .into(),
        )
        .with_child(
            Dom::create_node(NodeType::P)
                .with_css_props(
                    vec![CssPropertyWithConditions::simple(CssProperty::font_size(
                        StyleFontSize::px(16.0),
                    ))]
                    .into(),
                )
                .with_child(Dom::create_text("Text")),
        );

    let (_styled_dom, cache) = setup_styled_dom!(dom);

    // P (node 1) has its own font-size, should override inherited
    let p_id = azul_core::dom::NodeId::new(1);
    let computed = cache
        .computed_values
        .get(p_id.index())
        .expect("p should have computed values");
    let font_size_prop = find_prop(computed, &CssPropertyType::FontSize)
        .expect("should have font-size");

    assert_eq!(font_size_prop.origin, CssPropertyOrigin::Own);

    if let CssProperty::FontSize(val) = &font_size_prop.property {
        if let Some(size) = val.get_property() {
            assert!((size.inner.number.get() - 16.0).abs() < 0.001);
        } else {
            panic!("FontSize should have value");
        }
    } else {
        panic!("Property should be FontSize");
    }
}

#[test]
fn test_em_resolved_to_px_in_computed() {
    let dom = Dom::create_div()
        .with_css_props(
            vec![CssPropertyWithConditions::simple(CssProperty::font_size(
                StyleFontSize::px(20.0),
            ))]
            .into(),
        )
        .with_child(
            Dom::create_node(NodeType::P)
                .with_css_props(
                    vec![CssPropertyWithConditions::simple(CssProperty::font_size(
                        StyleFontSize::em(1.5),
                    ))]
                    .into(),
                )
                .with_child(Dom::create_text("Text")),
        );

    let (_styled_dom, cache) = setup_styled_dom!(dom);

    // P's 1.5em should be resolved to 30px (1.5 * 20)
    let p_id = azul_core::dom::NodeId::new(1);
    let computed = cache
        .computed_values
        .get(p_id.index())
        .expect("p should have computed values");
    let font_size_prop = find_prop(computed, &CssPropertyType::FontSize)
        .expect("should have font-size");

    if let CssProperty::FontSize(val) = &font_size_prop.property {
        if let Some(size) = val.get_property() {
            assert_eq!(
                size.inner.metric,
                SizeMetric::Px,
                "Should be resolved to Px"
            );
            assert!(
                (size.inner.number.get() - 30.0).abs() < 0.001,
                "1.5em * 20px = 30px, got {}",
                size.inner.number.get()
            );
        } else {
            panic!("FontSize should have value");
        }
    } else {
        panic!("Property should be FontSize");
    }
}

#[test]
fn test_deeply_nested_inheritance() {
    let dom = Dom::create_div()
        .with_css_props(
            vec![CssPropertyWithConditions::simple(CssProperty::font_size(
                StyleFontSize::px(20.0),
            ))]
            .into(),
        )
        .with_child(Dom::create_div().with_child(
            Dom::create_div().with_child(Dom::create_div().with_child(Dom::create_text("Deep"))),
        ));

    let (_styled_dom, cache) = setup_styled_dom!(dom);

    // The deepest div (node 3) should inherit 20px from root
    let deep_id = azul_core::dom::NodeId::new(3);
    let computed = cache
        .computed_values
        .get(deep_id.index())
        .expect("deep node should have computed values");
    let font_size_prop = find_prop(computed, &CssPropertyType::FontSize)
        .expect("should have font-size");

    if let CssProperty::FontSize(val) = &font_size_prop.property {
        if let Some(size) = val.get_property() {
            assert!((size.inner.number.get() - 20.0).abs() < 0.001);
        }
    }
}

#[test]
fn test_ua_css_for_headings() {
    let dom = Dom::create_node(NodeType::H1).with_child(Dom::create_text("Heading"));

    let (_styled_dom, cache) = setup_styled_dom!(dom);

    // H1 should have UA CSS font-size: 2em (32px with 16px default)
    let h1_id = azul_core::dom::NodeId::new(0);
    let computed = cache
        .computed_values
        .get(h1_id.index())
        .expect("h1 should have computed values");

    if let Some(font_size_prop) = find_prop(computed, &CssPropertyType::FontSize) {
        if let CssProperty::FontSize(val) = &font_size_prop.property {
            if let Some(size) = val.get_property() {
                // H1 UA CSS is 2em, resolved with 16px default = 32px
                assert!(
                    (size.inner.number.get() - 32.0).abs() < 0.1,
                    "H1 font-size should be ~32px (2em * 16px), got {}",
                    size.inner.number.get()
                );
            }
        }
    }
}

#[test]
fn test_multiple_properties_computed() {
    let css = r#"
        div {
            font-size: 18px;
            display: block;
            width: 100px;
        }
    "#;
    let dom = Dom::create_div();

    let (styled_dom, _cache) = setup_styled_dom!(dom, css);

    // After compact-prune, compact-encoded properties live in the compact cache,
    // not in computed_values. Verify via compact cache.
    let cc = styled_dom.css_property_cache.ptr.compact_cache.as_ref().unwrap();
    let fs = cc.tier2_dims[0].font_size;
    assert_ne!(fs, 0, "font-size should be set in compact cache");
    let display = ((cc.tier1_enums[0] >> azul_css::compact_cache::DISPLAY_SHIFT)
        & azul_css::compact_cache::DISPLAY_MASK) as u8;
    // Block encodes as 0 (default), which is correct
    assert_eq!(display, 0, "display:block encodes as 0 in compact cache");
}

#[test]
fn test_no_computed_values_for_nonexistent_node() {
    let dom = Dom::create_div();
    let (_styled_dom, cache) = setup_styled_dom!(dom);

    // Node 100 doesn't exist
    let nonexistent_id = azul_core::dom::NodeId::new(100);
    assert!(cache.computed_values.get(nonexistent_id.index()).is_none());
}

#[test]
fn test_font_weight_inheritance() {
    // Use inline CSS to ensure div has bold font-weight
    let dom = Dom::create_div()
        .with_css_props(
            vec![CssPropertyWithConditions::simple(CssProperty::FontWeight(
                azul_css::css::CssPropertyValue::Exact(
                    azul_css::props::basic::font::StyleFontWeight::Bold,
                ),
            ))]
            .into(),
        )
        .with_child(Dom::create_node(NodeType::Span).with_child(Dom::create_text("Bold text")));

    let (_styled_dom, cache) = setup_styled_dom!(dom);

    // Span should have font-weight (inherited from div)
    let span_id = azul_core::dom::NodeId::new(1);
    let computed = cache
        .computed_values
        .get(span_id.index())
        .expect("span should have computed values");

    if let Some(font_weight_prop) = find_prop(computed, &CssPropertyType::FontWeight) {
        // Check that span has bold font-weight (value matters, origin may vary due to UA CSS)
        if let CssProperty::FontWeight(val) = &font_weight_prop.property {
            if let Some(weight) = val.get_property() {
                assert_eq!(
                    *weight,
                    azul_css::props::basic::font::StyleFontWeight::Bold,
                    "Span should have inherited bold font-weight from parent"
                );
            }
        }
    }
}

#[test]
fn test_color_inheritance() {
    // Use inline CSS to ensure div has red text color
    let dom = Dom::create_div()
        .with_css_props(
            vec![CssPropertyWithConditions::simple(CssProperty::TextColor(
                azul_css::css::CssPropertyValue::Exact(azul_css::props::style::StyleTextColor {
                    inner: azul_css::props::basic::color::ColorU {
                        r: 255,
                        g: 0,
                        b: 0,
                        a: 255,
                    },
                }),
            ))]
            .into(),
        )
        .with_child(Dom::create_node(NodeType::P).with_child(Dom::create_text("Red text")));

    let (_styled_dom, cache) = setup_styled_dom!(dom);

    // P should have red text color (inherited from div)
    let p_id = azul_core::dom::NodeId::new(1);
    let computed = cache
        .computed_values
        .get(p_id.index())
        .expect("p should have computed values");

    if let Some(color_prop) = find_prop(computed, &CssPropertyType::TextColor) {
        // Check that P has red color (value matters, origin may vary)
        if let CssProperty::TextColor(val) = &color_prop.property {
            if let Some(color) = val.get_property() {
                assert_eq!(
                    color.inner.r, 255,
                    "P should have inherited red color from parent"
                );
                assert_eq!(color.inner.g, 0);
                assert_eq!(color.inner.b, 0);
            }
        }
    }
}

#[test]
fn test_non_inheritable_property_not_inherited() {
    // display is not inheritable
    let css = "div { display: flex; }";
    let dom = Dom::create_div()
        .with_child(Dom::create_node(NodeType::P).with_child(Dom::create_text("Text")));

    let (_styled_dom, cache) = setup_styled_dom!(dom, css);

    // P should NOT inherit display from div
    let p_id = azul_core::dom::NodeId::new(1);
    let computed = cache
        .computed_values
        .get(p_id.index())
        .expect("p should have computed values");

    if let Some(display_prop) = find_prop(computed, &CssPropertyType::Display) {
        // If it exists, it should be the default (block for P), not inherited flex
        if let CssProperty::Display(val) = &display_prop.property {
            if let Some(display) = val.get_property() {
                // P's display should be block (UA CSS), not flex
                assert_ne!(
                    format!("{:?}", display).to_lowercase(),
                    "flex".to_lowercase()
                );
            }
        }
    }
}

#[test]
fn test_empty_css_produces_only_ua_styles() {
    let dom = Dom::create_node(NodeType::P).with_child(Dom::create_text("Paragraph"));

    let (styled_dom, _cache) = setup_styled_dom!(dom);

    // P should have UA CSS applied. Compact cache encodes Block as 0
    // (default when bits unset), so we check the POPULATED bit + margin.
    let cc = styled_dom.css_property_cache.ptr.compact_cache.as_ref()
        .expect("compact cache should exist");
    // P has margin-top from UA CSS — check it's non-zero in compact dims
    let margin_top = cc.tier2_dims[0].margin_top;
    assert_ne!(margin_top, 0, "P should have non-zero margin-top from UA CSS");
}

#[test]
fn test_text_node_inherits_from_parent() {
    let dom = Dom::create_node(NodeType::P)
        .with_css_props(
            vec![CssPropertyWithConditions::simple(CssProperty::font_size(
                StyleFontSize::px(18.0),
            ))]
            .into(),
        )
        .with_child(Dom::create_text("Text inherits"));

    let (_styled_dom, cache) = setup_styled_dom!(dom);

    // Text node (node 1) should have font-size from P
    let text_id = azul_core::dom::NodeId::new(1);
    let computed = cache
        .computed_values
        .get(text_id.index())
        .expect("text should have computed values");

    if let Some(font_size_prop) = find_prop(computed, &CssPropertyType::FontSize) {
        // Check the VALUE is correct (18px from parent)
        if let CssProperty::FontSize(val) = &font_size_prop.property {
            if let Some(size) = val.get_property() {
                assert!(
                    (size.inner.number.get() - 18.0).abs() < 0.001,
                    "Text node should have font-size 18px from parent, got {}",
                    size.inner.number.get()
                );
            } else {
                panic!("FontSize should have value");
            }
        } else {
            panic!("Property should be FontSize");
        }
    } else {
        panic!("Text node should have font-size property");
    }
}

use azul_core::prop_cache::*;
use azul_core::dom::{NodeData, NodeId};
use azul_core::styled_dom::StyledNodeState;
use azul_css::props::layout::LayoutDisplay;

#[test]
fn test_ua_css_p_tag_properties() {
    // Create an empty CssPropertyCache
    let cache = CssPropertyCache::empty(1);

    // Create a minimal <p> tag NodeData using public API
    let mut node_data = NodeData::create_node(NodeType::P);

    let node_id = NodeId::new(0);
    let node_state = StyledNodeState::default();

    // Test that <p> has display: block from UA CSS
    let display = cache.get_display(&node_data, &node_id, &node_state);
    assert!(
        display.is_some(),
        "Expected <p> to have display property from UA CSS"
    );
    if let Some(d) = display {
        if let Some(display_value) = d.get_property() {
            assert_eq!(
                *display_value,
                LayoutDisplay::Block,
                "Expected <p> to have display: block, got {:?}",
                display_value
            );
        }
    }

    // NOTE: <p> does NOT have width: 100% in standard UA CSS
    // Block elements have width: auto by default, which means "fill available width"
    // but it's not the same as width: 100%. The difference is critical for flexbox.
    let width = cache.get_width(&node_data, &node_id, &node_state);
    // Width should be None because <p> should use auto width (no explicit width property)
    assert!(
        width.is_none(),
        "Expected <p> to NOT have explicit width (should be auto), but got {:?}",
        width
    );

    // Test that <p> does NOT have a default height from UA CSS
    // (height should be auto, which means None)
    let height = cache.get_height(&node_data, &node_id, &node_state);
    println!("Height for <p> tag: {:?}", height);

    // Height should be None because <p> should use auto height
    assert!(
        height.is_none(),
        "Expected <p> to NOT have explicit height (should be auto), but got {:?}",
        height
    );
}

#[test]
fn test_ua_css_body_tag_properties() {
    let cache = CssPropertyCache::empty(1);

    let node_data = NodeData::create_node(NodeType::Body);

    let node_id = NodeId::new(0);
    let node_state = StyledNodeState::default();

    // NOTE: <body> does NOT have width: 100% in standard UA CSS
    // It inherits from the Initial Containing Block (ICB) and has width: auto
    let width = cache.get_width(&node_data, &node_id, &node_state);
    // Width should be None because <body> should use auto width (no explicit width property)
    assert!(
        width.is_none(),
        "Expected <body> to NOT have explicit width (should be auto), but got {:?}",
        width
    );

    // Note: height: 100% was removed from UA CSS (ua_css.rs:506 commented out)
    // This is correct - <body> should have height: auto by default per CSS spec
    let height = cache.get_height(&node_data, &node_id, &node_state);
    assert!(
        height.is_none(),
        "<body> should not have explicit height from UA CSS (should be auto)"
    );

    // Test margins (body has 8px margins from UA CSS)
    let margin_top = cache.get_margin_top(&node_data, &node_id, &node_state);
    assert!(
        margin_top.is_some(),
        "Expected <body> to have margin-top from UA CSS"
    );
}

// =========================================================================
// Regression tests for bugs found during performance optimization
// =========================================================================

/// Bug #1/#2: FlatVecVec::sort_each_and_flatten must free build Vecs
/// and shrink data to fit. Previously build Vecs were retained after
/// flatten (leaking ~1 MiB on a 1000-node DOM) and data capacity
/// wasn't shrunk after dedup.
#[test]
fn flatvecvec_flatten_frees_build_and_shrinks() {
    use azul_core::prop_cache::FlatVecVec;

    let mut fvv: FlatVecVec<u32> = FlatVecVec::new(100);
    for i in 0..100 {
        fvv.push_to(i, i as u32);
        fvv.push_to(i, i as u32); // duplicate
        fvv.push_to(i, (i + 1) as u32);
    }

    let build_bytes_before = fvv.heap_bytes(4);
    assert!(build_bytes_before > 0, "build phase should have allocations");

    fvv.sort_each_and_flatten(|x| *x);

    let after_bytes = fvv.heap_bytes(4);
    // After flatten+dedup: build should be freed, data should be shrunk
    // 100 nodes × 2 unique items each = 200 items × 4 bytes = 800 bytes data
    // + 100 offsets × 8 bytes = 800 bytes offsets
    // Total: ~1600 bytes. Before: much more due to build Vecs.
    assert!(
        after_bytes < build_bytes_before,
        "flatten should reduce memory: before={} after={}",
        build_bytes_before, after_bytes
    );
    // Verify data is accessible
    assert_eq!(fvv.get_slice(0), &[0, 1]);
    assert_eq!(fvv.get_slice(50), &[50, 51]);
}

/// Bug #3: FlatVecVec::retain must shrink_to_fit after filtering.
/// Previously capacity stayed at the pre-retain size.
#[test]
fn flatvecvec_retain_shrinks_capacity() {
    use azul_core::prop_cache::FlatVecVec;

    let mut fvv: FlatVecVec<u32> = FlatVecVec::new(10);
    for i in 0..10 {
        for j in 0..100 {
            fvv.push_to(i, (i * 100 + j) as u32);
        }
    }
    fvv.sort_each_and_flatten(|x| *x);

    let before = fvv.heap_bytes(4);
    // Keep only even numbers (removes ~50%)
    fvv.retain(|x| x % 2 == 0);
    let after = fvv.heap_bytes(4);

    assert!(
        after < before * 3 / 4,
        "retain should significantly reduce memory: before={} after={}",
        before, after
    );
    // Verify filtered data
    for i in 0..10 {
        let slice = fvv.get_slice(i);
        assert!(slice.iter().all(|x| x % 2 == 0), "all values should be even");
    }
}

/// Bug #4: prune_compact_normal_props must handle cascaded_props in
/// build phase (not yet flattened). Previously it would silently skip
/// the prune because retain() early-returns when offsets is empty.
#[test]
fn prune_handles_unflattened_cascaded_props() {
    use azul_core::prop_cache::{CssPropertyCache, StatefulCssProperty};
    use azul_css::dynamic_selector::PseudoStateType;

    let mut cache = CssPropertyCache::empty(5);
    // Add some Normal-state compact properties to cascaded_props (build phase)
    for i in 0..5 {
        cache.cascaded_props.push_to(i, StatefulCssProperty {
            state: PseudoStateType::Normal,
            prop_type: CssPropertyType::Display,
            property: CssProperty::Display(CssPropertyValue::Exact(
                azul_css::props::layout::display::LayoutDisplay::Block,
            )),
        });
    }

    // cascaded_props is still in build phase (not flattened)
    assert!(!cache.cascaded_props.is_flattened());

    // Build a dummy compact cache so prune can run
    cache.compact_cache = Some(azul_css::compact_cache::CompactLayoutCache::with_capacity(5));

    // This should NOT panic and should actually prune
    cache.prune_compact_normal_props();

    // After prune, cascaded_props should be flattened and pruned
    assert!(cache.cascaded_props.is_flattened());
    // Display is compact-encoded + Normal state → should be removed
    let total: usize = (0..5).map(|i| cache.cascaded_props.get_slice(i).len()).sum();
    assert_eq!(total, 0, "all Normal+compact entries should be pruned");
}

/// Bug #6: has_compact_encoding must return true for all properties
/// that apply_css_property_to_compact handles.
#[test]
fn has_compact_encoding_covers_all_compact_properties() {
    use azul_css::props::property::CssPropertyType::*;

    // All properties that are encoded in compact_cache_builder.rs
    let compact_props = [
        Display, Position, Float, OverflowX, OverflowY, BoxSizing,
        FlexDirection, FlexWrap, JustifyContent, AlignItems, AlignContent,
        WritingMode, Clear, FontWeight, FontStyle, TextAlign,
        Visibility, WhiteSpace, Direction, VerticalAlign, BorderCollapse,
        AlignSelf, JustifySelf, GridAutoFlow, JustifyItems,
        Width, Height, MinWidth, MaxWidth, MinHeight, MaxHeight,
        FlexBasis, FontSize,
        PaddingTop, PaddingRight, PaddingBottom, PaddingLeft,
        MarginTop, MarginRight, MarginBottom, MarginLeft,
        BorderTopWidth, BorderRightWidth, BorderBottomWidth, BorderLeftWidth,
        Top, Right, Bottom, Left,
        FlexGrow, FlexShrink,
        ZIndex,
        BorderTopStyle, BorderRightStyle, BorderBottomStyle, BorderLeftStyle,
        BorderTopColor, BorderRightColor, BorderBottomColor, BorderLeftColor,
        BorderSpacing, TabSize,
        TextColor, FontFamily, LineHeight, LetterSpacing, WordSpacing, TextIndent,
        ColumnGap, RowGap, Gap,
        GridColumn, GridRow,
    ];

    for pt in &compact_props {
        assert!(
            pt.has_compact_encoding(),
            "{:?} should have compact encoding but has_compact_encoding() returns false",
            pt
        );
    }

    // These should NOT have compact encoding
    let non_compact = [
        BackgroundContent, BackgroundPosition, BackgroundSize,
        Transform, TransformOrigin, BoxShadowLeft, Opacity,
        GridTemplateColumns, GridTemplateRows, GridTemplateAreas,
    ];
    for pt in &non_compact {
        assert!(
            !pt.has_compact_encoding(),
            "{:?} should NOT have compact encoding",
            pt
        );
    }
}