1
#[allow(unused_imports)]
2
pub use super::*;
3
#[cfg(test)]
4
mod tests {
5
    use azul_css::AzString;
6

            
7
    use super::*;
8
    use crate::{
9
        dom::{DomId, DomNodeId},
10
        geom::LogicalPosition,
11
        id::NodeId,
12
        styled_dom::NodeHierarchyItemId,
13
        task::{Instant, SystemTick},
14
        window::{
15
            KeyboardState, MouseState, OptionVirtualKeyCode, VirtualKeyCode, VirtualKeyCodeVec,
16
        },
17
    };
18

            
19
    struct MockSelectionManager {
20
        click_count: u8,
21
        has_sel: bool,
22
    }
23
    impl SelectionManagerQuery for MockSelectionManager {
24
7
        fn get_click_count(&self) -> u8 {
25
7
            self.click_count
26
7
        }
27
7
        fn get_drag_start_position(&self) -> Option<LogicalPosition> {
28
7
            None
29
7
        }
30
7
        fn has_selection(&self) -> bool {
31
7
            self.has_sel
32
7
        }
33
    }
34

            
35
    struct MockFocusManager(Option<DomNodeId>);
36
    impl FocusManagerQuery for MockFocusManager {
37
7
        fn get_focused_node_id(&self) -> Option<DomNodeId> {
38
7
            self.0
39
7
        }
40
    }
41

            
42
7
    fn focused_node(node_idx: usize) -> DomNodeId {
43
7
        DomNodeId {
44
7
            dom: DomId { inner: 0 },
45
7
            node: NodeHierarchyItemId::from_crate_internal(Some(NodeId::new(node_idx))),
46
7
        }
47
7
    }
48

            
49
6
    fn make_keyboard_state(vk: VirtualKeyCode) -> KeyboardState {
50
6
        KeyboardState {
51
6
            current_virtual_keycode: OptionVirtualKeyCode::Some(vk),
52
6
            pressed_virtual_keycodes: VirtualKeyCodeVec::from_vec(vec![vk]),
53
6
            ..KeyboardState::default()
54
6
        }
55
6
    }
56

            
57
2
    fn make_keydown_event(target: DomNodeId) -> SyntheticEvent {
58
2
        SyntheticEvent::new(
59
2
            EventType::KeyDown,
60
2
            EventSource::User,
61
2
            target,
62
2
            Instant::Tick(SystemTick::new(0)),
63
2
            EventData::Keyboard(KeyboardEventData {
64
2
                key_code: VirtualKeyCode::Back as u32,
65
2
                char_code: None,
66
2
                modifiers: KeyModifiers::default(),
67
2
                repeat: false,
68
2
                ..Default::default()
69
2
            }),
70
        )
71
2
    }
72

            
73
    #[test]
74
1
    fn backspace_generates_delete_text_selection() {
75
1
        let target = focused_node(2);
76
1
        let events = vec![make_keydown_event(target)];
77
1
        let kb = make_keyboard_state(VirtualKeyCode::Back);
78
1
        let mouse = MouseState::default();
79
1
        let sel = MockSelectionManager {
80
1
            click_count: 0,
81
1
            has_sel: false,
82
1
        };
83
1
        let focus = MockFocusManager(Some(target));
84

            
85
1
        let result =
86
1
            pre_callback_filter_internal_events(&events, None, &kb, &mouse, &sel, &focus, true);
87

            
88
1
        let ops: Vec<_> = result
89
1
            .system_changes
90
1
            .iter()
91
1
            .filter(|c| matches!(c, SystemChange::ApplySelectionOp { .. }))
92
1
            .collect();
93
1
        assert_eq!(ops.len(), 1, "Backspace should generate ApplySelectionOp");
94
1
        match &ops[0] {
95
1
            SystemChange::ApplySelectionOp { op, .. } => {
96
1
                assert_eq!(op.direction, SelectionDirection::Backward);
97
1
                assert_eq!(op.step, SelectionStep::Character);
98
1
                assert_eq!(op.mode, SelectionMode::Delete);
99
            }
100
            _ => unreachable!(),
101
        }
102
1
    }
103

            
104
    #[test]
105
1
    fn delete_key_generates_forward_deletion() {
106
1
        let target = focused_node(2);
107
1
        let event = SyntheticEvent::new(
108
1
            EventType::KeyDown,
109
1
            EventSource::User,
110
1
            target,
111
1
            Instant::Tick(SystemTick::new(0)),
112
1
            EventData::Keyboard(KeyboardEventData {
113
1
                key_code: VirtualKeyCode::Delete as u32,
114
1
                char_code: None,
115
1
                modifiers: KeyModifiers::default(),
116
1
                repeat: false,
117
1
                ..Default::default()
118
1
            }),
119
        );
120
1
        let kb = make_keyboard_state(VirtualKeyCode::Delete);
121
1
        let mouse = MouseState::default();
122
1
        let sel = MockSelectionManager {
123
1
            click_count: 0,
124
1
            has_sel: false,
125
1
        };
126
1
        let focus = MockFocusManager(Some(target));
127
1
        let result =
128
1
            pre_callback_filter_internal_events(&[event], None, &kb, &mouse, &sel, &focus, true);
129
1
        let ops: Vec<_> = result
130
1
            .system_changes
131
1
            .iter()
132
1
            .filter(|c| matches!(c, SystemChange::ApplySelectionOp { .. }))
133
1
            .collect();
134
1
        assert_eq!(ops.len(), 1);
135
1
        match &ops[0] {
136
1
            SystemChange::ApplySelectionOp { op, .. } => {
137
1
                assert_eq!(op.direction, SelectionDirection::Forward);
138
1
                assert_eq!(op.step, SelectionStep::Character);
139
1
                assert_eq!(op.mode, SelectionMode::Delete);
140
            }
141
            _ => unreachable!(),
142
        }
143
1
    }
144

            
145
    #[test]
146
1
    fn arrow_left_generates_navigation() {
147
1
        let target = focused_node(2);
148
1
        let event = SyntheticEvent::new(
149
1
            EventType::KeyDown,
150
1
            EventSource::User,
151
1
            target,
152
1
            Instant::Tick(SystemTick::new(0)),
153
1
            EventData::Keyboard(KeyboardEventData {
154
1
                key_code: VirtualKeyCode::Left as u32,
155
1
                char_code: None,
156
1
                modifiers: KeyModifiers::default(),
157
1
                repeat: false,
158
1
                ..Default::default()
159
1
            }),
160
        );
161
1
        let kb = make_keyboard_state(VirtualKeyCode::Left);
162
1
        let mouse = MouseState::default();
163
1
        let sel = MockSelectionManager {
164
1
            click_count: 0,
165
1
            has_sel: false,
166
1
        };
167
1
        let focus = MockFocusManager(Some(target));
168
1
        let result =
169
1
            pre_callback_filter_internal_events(&[event], None, &kb, &mouse, &sel, &focus, true);
170
1
        let ops: Vec<_> = result
171
1
            .system_changes
172
1
            .iter()
173
1
            .filter(|c| matches!(c, SystemChange::ApplySelectionOp { .. }))
174
1
            .collect();
175
1
        assert_eq!(ops.len(), 1, "Left arrow should generate ApplySelectionOp");
176
1
        match &ops[0] {
177
1
            SystemChange::ApplySelectionOp { op, .. } => {
178
1
                assert_eq!(op.direction, SelectionDirection::Backward);
179
1
                assert_eq!(op.step, SelectionStep::Character);
180
1
                assert_eq!(op.mode, SelectionMode::Move);
181
            }
182
            _ => unreachable!(),
183
        }
184
1
    }
185

            
186
    #[test]
187
1
    fn no_focused_node_means_no_keyboard_system_changes() {
188
1
        let target = focused_node(2);
189
1
        let event = make_keydown_event(target);
190
1
        let kb = make_keyboard_state(VirtualKeyCode::Back);
191
1
        let mouse = MouseState::default();
192
1
        let sel = MockSelectionManager {
193
1
            click_count: 0,
194
1
            has_sel: false,
195
1
        };
196
1
        let focus = MockFocusManager(None); // No focus!
197

            
198
1
        let result =
199
1
            pre_callback_filter_internal_events(&[event], None, &kb, &mouse, &sel, &focus, true);
200

            
201
1
        assert!(
202
1
            result.system_changes.is_empty(),
203
            "No system changes should be generated without focused node"
204
        );
205
1
    }
206

            
207
    #[test]
208
1
    fn keydown_without_keyboard_data_generates_no_system_change() {
209
1
        let target = focused_node(2);
210
1
        let event = SyntheticEvent::new(
211
1
            EventType::KeyDown,
212
1
            EventSource::User,
213
1
            target,
214
1
            Instant::Tick(SystemTick::new(0)),
215
1
            EventData::None, // Bug: missing keyboard data
216
        );
217
1
        let kb = make_keyboard_state(VirtualKeyCode::Back);
218
1
        let mouse = MouseState::default();
219
1
        let sel = MockSelectionManager {
220
1
            click_count: 0,
221
1
            has_sel: false,
222
1
        };
223
1
        let focus = MockFocusManager(Some(target));
224

            
225
1
        let result =
226
1
            pre_callback_filter_internal_events(&[event], None, &kb, &mouse, &sel, &focus, true);
227

            
228
        // This test documents the bug we just fixed: EventData::None causes
229
        // the handle_key_down function to return None (early exit at line 2737)
230
1
        assert!(
231
1
            result.system_changes.is_empty(),
232
            "EventData::None should not generate system changes (documents the old bug)"
233
        );
234
1
    }
235

            
236
    #[test]
237
1
    fn ctrl_c_generates_copy() {
238
        // MWA-A2/MWA-D: shortcuts key off the PRIMARY modifier — Cmd on
239
        // macOS, Ctrl elsewhere — so this test presses the platform's
240
        // primary key. (The old version hardcoded LControl and correctly
241
        // started failing on macOS hosts once primary_down() landed:
242
        // Ctrl+C must NOT copy on macOS, Cmd+C does.)
243
1
        let primary_key = if cfg!(target_os = "macos") {
244
            VirtualKeyCode::LWin
245
        } else {
246
1
            VirtualKeyCode::LControl
247
        };
248
1
        let target = focused_node(2);
249
1
        let event = SyntheticEvent::new(
250
1
            EventType::KeyDown,
251
1
            EventSource::User,
252
1
            target,
253
1
            Instant::Tick(SystemTick::new(0)),
254
1
            EventData::Keyboard(KeyboardEventData {
255
1
                key_code: VirtualKeyCode::C as u32,
256
1
                char_code: Some('c'),
257
1
                modifiers: KeyModifiers {
258
1
                    ctrl: !cfg!(target_os = "macos"),
259
1
                    shift: false,
260
1
                    alt: false,
261
1
                    meta: cfg!(target_os = "macos"),
262
1
                },
263
1
                repeat: false,
264
1
                ..Default::default()
265
1
            }),
266
        );
267
1
        let mut kb = make_keyboard_state(VirtualKeyCode::C);
268
1
        kb.pressed_virtual_keycodes =
269
1
            VirtualKeyCodeVec::from_vec(vec![VirtualKeyCode::C, primary_key]);
270
1
        let mouse = MouseState::default();
271
1
        let sel = MockSelectionManager {
272
1
            click_count: 0,
273
1
            has_sel: false,
274
1
        };
275
1
        let focus = MockFocusManager(Some(target));
276

            
277
1
        let result =
278
1
            pre_callback_filter_internal_events(&[event], None, &kb, &mouse, &sel, &focus, true);
279

            
280
1
        let copy_changes = result
281
1
            .system_changes
282
1
            .iter()
283
1
            .filter(|c| matches!(c, SystemChange::CopyToClipboard))
284
1
            .count();
285

            
286
1
        assert_eq!(copy_changes, 1, "primary+C should generate CopyToClipboard");
287
1
    }
288

            
289
2
    fn make_hit_test_with_node(node_idx: usize) -> FullHitTest {
290
        use std::collections::BTreeMap;
291

            
292
        use crate::{
293
            dom::OptionDomNodeId,
294
            hit_test::{FullHitTest, HitTest, HitTestItem},
295
            spaces::ContentBoxLocal,
296
        };
297

            
298
2
        let node_id = NodeId::new(node_idx);
299
2
        let dom_id = DomId { inner: 0 };
300

            
301
2
        let mut regular = BTreeMap::new();
302
2
        regular.insert(
303
2
            node_id,
304
2
            HitTestItem {
305
2
                point_in_viewport: LogicalPosition::new(100.0, 200.0),
306
2
                point_relative_to_item: ContentBoxLocal::new(LogicalPosition::new(50.0, 30.0)),
307
2
                is_focusable: true,
308
2
                is_virtual_view_hit: None,
309
2
                hit_depth: 0,
310
2
            },
311
        );
312

            
313
2
        let mut hovered = BTreeMap::new();
314
2
        hovered.insert(
315
2
            dom_id,
316
2
            HitTest {
317
2
                regular_hit_test_nodes: regular,
318
2
                scroll_hit_test_nodes: BTreeMap::new(),
319
2
                scrollbar_hit_test_nodes: BTreeMap::new(),
320
2
                cursor_hit_test_nodes: BTreeMap::new(),
321
2
            },
322
        );
323

            
324
2
        FullHitTest {
325
2
            hovered_nodes: hovered,
326
2
            focused_node: OptionDomNodeId::None,
327
2
        }
328
2
    }
329

            
330
    #[test]
331
1
    fn mousedown_generates_text_selection_click() {
332
1
        let target = focused_node(2);
333
1
        let event = SyntheticEvent::new(
334
1
            EventType::MouseDown,
335
1
            EventSource::User,
336
1
            target,
337
1
            Instant::Tick(SystemTick::new(0)),
338
1
            EventData::Mouse(MouseEventData {
339
1
                position: LogicalPosition::new(100.0, 200.0),
340
1
                button: MouseButton::Left,
341
1
                buttons: 1,
342
1
                modifiers: KeyModifiers::default(),
343
1
                ..Default::default()
344
1
            }),
345
        );
346
1
        let hit_test = make_hit_test_with_node(2);
347
1
        let kb = KeyboardState::default();
348
1
        let mouse = MouseState::default();
349
1
        let sel = MockSelectionManager {
350
1
            click_count: 1,
351
1
            has_sel: false,
352
1
        };
353
1
        let focus = MockFocusManager(Some(target));
354

            
355
1
        let result = pre_callback_filter_internal_events(
356
1
            &[event],
357
1
            Some(&hit_test),
358
1
            &kb,
359
1
            &mouse,
360
1
            &sel,
361
1
            &focus,
362
            true,
363
        );
364

            
365
1
        let click_changes = result
366
1
            .system_changes
367
1
            .iter()
368
1
            .filter(|c| matches!(c, SystemChange::TextSelectionClick { .. }))
369
1
            .count();
370

            
371
1
        assert_eq!(
372
            click_changes, 1,
373
            "MouseDown with hit_test should generate TextSelectionClick"
374
        );
375
1
    }
376

            
377
    #[test]
378
1
    fn process_event_result_max_self_picks_higher_variant() {
379
1
        let lo = ProcessEventResult::ShouldReRenderCurrentWindow;
380
1
        let hi = ProcessEventResult::ShouldRegenerateDomCurrentWindow;
381
1
        assert_eq!(lo.max_self(hi), hi);
382
1
        assert_eq!(hi.max_self(lo), hi);
383
1
        assert_eq!(lo.max_self(lo), lo);
384
1
    }
385

            
386
    #[test]
387
1
    fn keyboard_shortcut_keys_off_primary_modifier() {
388
        use crate::window::VirtualKeyCode::{A, C, V, X, Z};
389
        // No primary modifier → never a shortcut (MWA-A2).
390
1
        assert_eq!(KeyboardShortcut::from_key(C, false, false), None);
391
1
        assert_eq!(KeyboardShortcut::from_key(Z, false, true), None);
392
        // Primary held → the standard editing set.
393
1
        assert_eq!(
394
1
            KeyboardShortcut::from_key(C, true, false),
395
            Some(KeyboardShortcut::Copy)
396
        );
397
1
        assert_eq!(
398
1
            KeyboardShortcut::from_key(X, true, false),
399
            Some(KeyboardShortcut::Cut)
400
        );
401
1
        assert_eq!(
402
1
            KeyboardShortcut::from_key(V, true, false),
403
            Some(KeyboardShortcut::Paste)
404
        );
405
1
        assert_eq!(
406
1
            KeyboardShortcut::from_key(A, true, false),
407
            Some(KeyboardShortcut::SelectAll)
408
        );
409
1
        assert_eq!(
410
1
            KeyboardShortcut::from_key(Z, true, false),
411
            Some(KeyboardShortcut::Undo)
412
        );
413
1
        assert_eq!(
414
1
            KeyboardShortcut::from_key(Z, true, true),
415
            Some(KeyboardShortcut::Redo)
416
        );
417
1
    }
418

            
419
    #[test]
420
1
    fn primary_modifier_is_platform_correct() {
421
        use crate::window::{KeyboardState, VirtualKeyCode};
422
1
        let cmd_held = KeyboardState {
423
1
            pressed_virtual_keycodes: vec![VirtualKeyCode::LWin].into(),
424
1
            ..Default::default()
425
1
        };
426
        // Cmd/super is primary ONLY on macOS.
427
1
        assert_eq!(cmd_held.primary_down(), cfg!(target_os = "macos"));
428

            
429
1
        let ctrl_held = KeyboardState {
430
1
            pressed_virtual_keycodes: vec![VirtualKeyCode::LControl].into(),
431
1
            ..Default::default()
432
1
        };
433
        // Ctrl is primary everywhere EXCEPT macOS.
434
1
        assert_eq!(ctrl_held.primary_down(), !cfg!(target_os = "macos"));
435
1
    }
436

            
437
    #[test]
438
1
    fn arrow_direction_from_key_maps_arrows_and_home_end() {
439
        use crate::window::VirtualKeyCode::*;
440
1
        assert_eq!(
441
1
            ArrowDirection::from_key(Left, false),
442
            Some(ArrowDirection::Left)
443
        );
444
1
        assert_eq!(
445
1
            ArrowDirection::from_key(Right, false),
446
            Some(ArrowDirection::Right)
447
        );
448
1
        assert_eq!(
449
1
            ArrowDirection::from_key(Up, false),
450
            Some(ArrowDirection::Up)
451
        );
452
1
        assert_eq!(
453
1
            ArrowDirection::from_key(Down, false),
454
            Some(ArrowDirection::Down)
455
        );
456
1
        assert_eq!(
457
1
            ArrowDirection::from_key(Home, false),
458
            Some(ArrowDirection::LineStart)
459
        );
460
1
        assert_eq!(
461
1
            ArrowDirection::from_key(End, false),
462
            Some(ArrowDirection::LineEnd)
463
        );
464
1
        assert_eq!(
465
1
            ArrowDirection::from_key(Home, true),
466
            Some(ArrowDirection::DocumentStart)
467
        );
468
1
        assert_eq!(
469
1
            ArrowDirection::from_key(End, true),
470
            Some(ArrowDirection::DocumentEnd)
471
        );
472
1
        assert_eq!(ArrowDirection::from_key(C, false), None);
473
1
    }
474

            
475
    #[test]
476
1
    fn arrow_direction_to_selection_respects_ctrl() {
477
1
        let (d, s) = ArrowDirection::Left.to_selection(false);
478
1
        assert_eq!(
479
1
            (d, s),
480
            (SelectionDirection::Backward, SelectionStep::Character)
481
        );
482
1
        let (d, s) = ArrowDirection::Left.to_selection(true);
483
1
        assert_eq!((d, s), (SelectionDirection::Backward, SelectionStep::Word));
484
1
        let (d, s) = ArrowDirection::Up.to_selection(false);
485
1
        assert_eq!(
486
1
            (d, s),
487
            (SelectionDirection::Backward, SelectionStep::VisualLine)
488
        );
489
1
        let (d, s) = ArrowDirection::DocumentEnd.to_selection(false);
490
1
        assert_eq!(
491
1
            (d, s),
492
            (SelectionDirection::Forward, SelectionStep::Document)
493
        );
494
1
    }
495

            
496
    #[test]
497
1
    fn keyboard_shortcut_from_key_recognizes_editing_combos() {
498
        use crate::window::VirtualKeyCode::*;
499
1
        assert_eq!(
500
1
            KeyboardShortcut::from_key(C, true, false),
501
            Some(KeyboardShortcut::Copy)
502
        );
503
1
        assert_eq!(
504
1
            KeyboardShortcut::from_key(X, true, false),
505
            Some(KeyboardShortcut::Cut)
506
        );
507
1
        assert_eq!(
508
1
            KeyboardShortcut::from_key(V, true, false),
509
            Some(KeyboardShortcut::Paste)
510
        );
511
1
        assert_eq!(
512
1
            KeyboardShortcut::from_key(A, true, false),
513
            Some(KeyboardShortcut::SelectAll)
514
        );
515
1
        assert_eq!(
516
1
            KeyboardShortcut::from_key(Z, true, false),
517
            Some(KeyboardShortcut::Undo)
518
        );
519
1
        assert_eq!(
520
1
            KeyboardShortcut::from_key(Z, true, true),
521
            Some(KeyboardShortcut::Redo)
522
        );
523
1
        assert_eq!(
524
1
            KeyboardShortcut::from_key(Y, true, false),
525
            Some(KeyboardShortcut::Redo)
526
        );
527
        // Non-ctrl combos must not match
528
1
        assert_eq!(KeyboardShortcut::from_key(C, false, false), None);
529
        // Unknown keys
530
1
        assert_eq!(KeyboardShortcut::from_key(D, true, false), None);
531
1
    }
532

            
533
    #[test]
534
1
    fn mouse_button_state_round_trips_from_mouse_state() {
535
1
        let ms = MouseState {
536
1
            left_down: true,
537
1
            middle_down: true,
538
1
            ..MouseState::default()
539
1
        };
540
1
        let bs: MouseButtonState = (&ms).into();
541
1
        assert!(bs.left_down);
542
1
        assert!(!bs.right_down);
543
1
        assert!(bs.middle_down);
544
1
        assert!(bs.any_down());
545

            
546
1
        let none = MouseButtonState {
547
1
            left_down: false,
548
1
            right_down: false,
549
1
            middle_down: false,
550
1
        };
551
1
        assert!(!none.any_down());
552
1
    }
553

            
554
    #[test]
555
1
    fn callback_to_call_collects_hits_for_dom() {
556
1
        let dom_id = DomId { inner: 0 };
557
1
        let hit_test = make_hit_test_with_node(2);
558
1
        let filter = EventFilter::Hover(HoverEventFilter::MouseDown);
559
1
        let calls = CallbackToCall::from_hit_test(&hit_test, dom_id, filter);
560
1
        assert_eq!(calls.len(), 1);
561
1
        assert_eq!(calls[0].node_id, NodeId::new(2));
562
1
        assert_eq!(calls[0].event_filter, filter);
563
1
        assert!(calls[0].hit_test_item.is_some());
564

            
565
        // Unknown DOM id => empty list
566
1
        let other = CallbackToCall::from_hit_test(
567
1
            &hit_test,
568
1
            DomId { inner: 999 },
569
1
            EventFilter::Hover(HoverEventFilter::MouseUp),
570
        );
571
1
        assert!(other.is_empty());
572

            
573
        // Direct constructor builds expected fields
574
1
        let direct = CallbackToCall::new(
575
1
            NodeId::new(7),
576
1
            None,
577
1
            EventFilter::Focus(FocusEventFilter::FocusReceived),
578
        );
579
1
        assert_eq!(direct.node_id, NodeId::new(7));
580
1
        assert!(direct.hit_test_item.is_none());
581
1
    }
582

            
583
    #[test]
584
1
    fn restyle_relayout_aliases_are_btreemap_compatible() {
585
        // RestyleNodes / RelayoutNodes are aliases for BTreeMap<NodeId, Vec<ChangedCssProperty>>.
586
        // Confirm we can construct empty ones via the alias and that they accept the same keys.
587
1
        let restyle: RestyleNodes = BTreeMap::new();
588
1
        let relayout: RelayoutNodes = BTreeMap::new();
589
1
        assert!(restyle.is_empty());
590
1
        assert!(relayout.is_empty());
591

            
592
        // RelayoutWords is BTreeMap<NodeId, AzString>.
593
1
        let mut words: RelayoutWords = BTreeMap::new();
594
1
        words.insert(NodeId::new(1), AzString::from_const_str("hello"));
595
1
        assert_eq!(
596
1
            words.get(&NodeId::new(1)).map(azul_css::AzString::as_str),
597
            Some("hello")
598
        );
599
1
    }
600

            
601
    #[test]
602
1
    fn detect_lifecycle_events_with_reconciliation_is_callable() {
603
        // Smoke test: empty old/new node data must produce no events and an
604
        // empty migration map. This proves the function is callable from
605
        // the public API and threads through `crate::diff::reconcile_dom`.
606
1
        let dom_id = DomId { inner: 0 };
607
1
        let old_data: Vec<crate::dom::NodeData> = Vec::new();
608
1
        let new_data: Vec<crate::dom::NodeData> = Vec::new();
609
1
        let old_hier: Vec<crate::styled_dom::NodeHierarchyItem> = Vec::new();
610
1
        let new_hier: Vec<crate::styled_dom::NodeHierarchyItem> = Vec::new();
611
1
        let old_layout = OrderedMap::default();
612
1
        let new_layout = OrderedMap::default();
613
1
        let result: LifecycleEventResult = detect_lifecycle_events_with_reconciliation(
614
1
            dom_id,
615
1
            &old_data,
616
1
            &new_data,
617
1
            &old_hier,
618
1
            &new_hier,
619
1
            &old_layout,
620
1
            &new_layout,
621
1
            Instant::Tick(SystemTick::new(0)),
622
        );
623
1
        assert!(result.events.is_empty());
624
1
        assert!(result.node_id_mapping.is_empty());
625
1
    }
626

            
627
    #[test]
628
1
    fn nodedata_focusable_and_activation_traits_are_wired() {
629
        use crate::{
630
            dom::{NodeData, NodeType},
631
            events::{ActivationBehavior as _, Focusable as _},
632
        };
633

            
634
        // <button> is naturally focusable and has activation behavior.
635
1
        let btn = NodeData::create_node(NodeType::Button);
636
1
        assert!(<NodeData as Focusable>::is_naturally_focusable(&btn));
637
1
        assert!(<NodeData as Focusable>::is_focusable(&btn));
638
1
        assert!(<NodeData as ActivationBehavior>::has_activation_behavior(
639
1
            &btn
640
        ));
641
1
        assert!(<NodeData as ActivationBehavior>::is_activatable(&btn));
642

            
643
        // A plain <div> is neither naturally focusable nor activatable.
644
1
        let div = NodeData::create_node(NodeType::Div);
645
1
        assert!(!<NodeData as Focusable>::is_naturally_focusable(&div));
646
1
        assert!(!<NodeData as ActivationBehavior>::has_activation_behavior(
647
1
            &div
648
1
        ));
649

            
650
        // <input> is naturally focusable.
651
1
        let input = NodeData::create_node(NodeType::Input);
652
1
        assert!(<NodeData as Focusable>::is_naturally_focusable(&input));
653
1
    }
654

            
655
    #[test]
656
1
    fn first_hovered_node_picks_frontmost_by_depth() {
657
        use std::collections::BTreeMap;
658

            
659
        use crate::{
660
            dom::OptionDomNodeId,
661
            hit_test::{FullHitTest, HitTest, HitTestItem},
662
            spaces::ContentBoxLocal,
663
        };
664

            
665
1
        let item = |depth: u32| HitTestItem {
666
2
            point_in_viewport: LogicalPosition::zero(),
667
2
            point_relative_to_item: ContentBoxLocal::zero(),
668
            is_focusable: true,
669
2
            is_virtual_view_hit: None,
670
2
            hit_depth: depth,
671
2
        };
672

            
673
        // Front-most node (depth 0) has the HIGHER NodeId; back node (depth 5)
674
        // has the lower id. The old `.next()` logic returned the lowest id
675
        // (node 2, the back one). We must now return the front-most (node 5).
676
1
        let mut regular = BTreeMap::new();
677
1
        regular.insert(NodeId::new(2), item(5));
678
1
        regular.insert(NodeId::new(5), item(0));
679

            
680
1
        let mut hovered = BTreeMap::new();
681
1
        hovered.insert(
682
1
            DomId { inner: 0 },
683
1
            HitTest {
684
1
                regular_hit_test_nodes: regular,
685
1
                scroll_hit_test_nodes: BTreeMap::new(),
686
1
                scrollbar_hit_test_nodes: BTreeMap::new(),
687
1
                cursor_hit_test_nodes: BTreeMap::new(),
688
1
            },
689
        );
690
1
        let ht = FullHitTest {
691
1
            hovered_nodes: hovered,
692
1
            focused_node: OptionDomNodeId::None,
693
1
        };
694

            
695
1
        let got = get_first_hovered_node(Some(&ht)).unwrap();
696
1
        assert_eq!(got.node.into_crate_internal(), Some(NodeId::new(5)));
697
1
    }
698

            
699
    #[test]
700
1
    fn size_changed_nan_guard_stops_resize_loop() {
701
        use crate::geom::LogicalSize;
702
        // A NaN dimension present on BOTH frames must read as "unchanged" so no
703
        // Resize is emitted every frame.
704
1
        let a = LogicalSize::new(f32::NAN, 100.0);
705
1
        let b = LogicalSize::new(f32::NAN, 100.0);
706
1
        assert!(!size_changed(a, b));
707
        // A real change is still detected.
708
1
        assert!(size_changed(
709
1
            LogicalSize::new(100.0, 100.0),
710
1
            LogicalSize::new(100.0, 120.0)
711
        ));
712
        // Sub-quantum jitter is ignored.
713
1
        assert!(!size_changed(
714
1
            LogicalSize::new(100.0, 100.0),
715
1
            LogicalSize::new(100.00005, 100.0)
716
1
        ));
717
1
    }
718

            
719
    #[test]
720
1
    fn dom_path_terminates_on_parent_cycle() {
721
        use crate::id::{Node, NodeHierarchy};
722
        // Two nodes whose parents point at each other -> a cycle.
723
1
        let nodes = vec![
724
1
            Node {
725
1
                parent: Some(NodeId::new(1)),
726
1
                ..Node::ROOT
727
1
            },
728
1
            Node {
729
1
                parent: Some(NodeId::new(0)),
730
1
                ..Node::ROOT
731
1
            },
732
        ];
733
1
        let hier = NodeHierarchy::new(nodes);
734
1
        let target = NodeHierarchyItemId::from_crate_internal(Some(NodeId::new(0)));
735
        // Must not hang / OOM; bounded by node count + visited-set.
736
1
        let path = get_dom_path(&hier, target);
737
1
        assert!(path.len() <= 2);
738
1
    }
739

            
740
    /// `Click` is ACTIVATION and has its own filter. It must NOT also reach
741
    /// `MouseUp` listeners: a real pointer release emits BOTH `MouseUp` and
742
    /// `Click` (W3C, and `event_determination` does exactly that), so a Click
743
    /// that also fired MouseUp handlers ran every activation TWICE - the
744
    /// ColorInput opened its picker and instantly closed it again
745
    /// (2026-09-01).
746
    #[test]
747
1
    fn click_maps_to_the_activation_filter_and_not_to_mouse_up() {
748
1
        let filters = event_type_to_filters(EventType::Click, &EventData::None);
749
1
        assert!(filters.contains(&EventFilter::Hover(HoverEventFilter::Click)));
750
1
        assert!(
751
1
            !filters.contains(&EventFilter::Hover(HoverEventFilter::MouseUp)),
752
            "a Click must not double-fire MouseUp listeners: {filters:?}",
753
        );
754
1
        assert!(!filters.contains(&EventFilter::Hover(HoverEventFilter::LeftMouseUp)));
755
1
        assert!(!filters.contains(&EventFilter::Hover(HoverEventFilter::LeftMouseDown)));
756
1
    }
757
}
758

            
759
#[cfg(test)]
760
#[allow(clippy::float_cmp, clippy::too_many_lines)]
761
mod autotest_generated {
762
    use super::*;
763
    use crate::{
764
        dom::{DomId, DomNodeId, OptionDomNodeId},
765
        geom::{LogicalPosition, LogicalRect, LogicalSize},
766
        hit_test::{FullHitTest, HitTest, HitTestItem},
767
        id::{Node, NodeHierarchy, NodeId},
768
        spaces::ContentBoxLocal,
769
        styled_dom::NodeHierarchyItemId,
770
        task::{Instant, SystemTick},
771
        window::{CursorPosition, KeyboardState, MouseState, VirtualKeyCode, VirtualKeyCodeVec},
772
    };
773

            
774
    // ---------------------------------------------------------------- helpers
775

            
776
    fn tick(n: u64) -> Instant {
777
        Instant::Tick(SystemTick::new(n))
778
    }
779

            
780
    fn dnid(dom: usize, node: usize) -> DomNodeId {
781
        DomNodeId {
782
            dom: DomId { inner: dom },
783
            node: NodeHierarchyItemId::from_crate_internal(Some(NodeId::new(node))),
784
        }
785
    }
786

            
787
    /// A `DomNodeId` whose node slot is the `None` sentinel (raw inner == 0).
788
    fn dnid_none(dom: usize) -> DomNodeId {
789
        DomNodeId {
790
            dom: DomId { inner: dom },
791
            node: NodeHierarchyItemId::NONE,
792
        }
793
    }
794

            
795
    fn hit_item(depth: u32) -> HitTestItem {
796
        HitTestItem {
797
            point_in_viewport: LogicalPosition::new(1.0, 2.0),
798
            point_relative_to_item: ContentBoxLocal::new(LogicalPosition::new(3.0, 4.0)),
799
            is_focusable: true,
800
            is_virtual_view_hit: None,
801
            hit_depth: depth,
802
        }
803
    }
804

            
805
    /// Hit test containing `(node_index, hit_depth)` pairs, all under one DOM.
806
    fn hit_test_with(dom: usize, nodes: &[(usize, u32)]) -> FullHitTest {
807
        let mut regular = BTreeMap::new();
808
        for (idx, depth) in nodes {
809
            regular.insert(NodeId::new(*idx), hit_item(*depth));
810
        }
811
        let mut hovered = BTreeMap::new();
812
        hovered.insert(
813
            DomId { inner: dom },
814
            HitTest {
815
                regular_hit_test_nodes: regular,
816
                scroll_hit_test_nodes: BTreeMap::new(),
817
                scrollbar_hit_test_nodes: BTreeMap::new(),
818
                cursor_hit_test_nodes: BTreeMap::new(),
819
            },
820
        );
821
        FullHitTest {
822
            hovered_nodes: hovered,
823
            focused_node: OptionDomNodeId::None,
824
        }
825
    }
826

            
827
    fn empty_hit_test() -> FullHitTest {
828
        FullHitTest {
829
            hovered_nodes: BTreeMap::new(),
830
            focused_node: OptionDomNodeId::None,
831
        }
832
    }
833

            
834
    fn mouse_event(ty: EventType, button: MouseButton, pos: LogicalPosition) -> SyntheticEvent {
835
        SyntheticEvent::new(
836
            ty,
837
            EventSource::User,
838
            dnid(0, 0),
839
            tick(0),
840
            EventData::Mouse(MouseEventData {
841
                position: pos,
842
                button,
843
                buttons: 1,
844
                modifiers: KeyModifiers::default(),
845
                ..Default::default()
846
            }),
847
        )
848
    }
849

            
850
    fn key_event(key_code: u32, modifiers: KeyModifiers) -> SyntheticEvent {
851
        SyntheticEvent::new(
852
            EventType::KeyDown,
853
            EventSource::User,
854
            dnid(0, 0),
855
            tick(0),
856
            EventData::Keyboard(KeyboardEventData {
857
                key_code,
858
                char_code: None,
859
                modifiers,
860
                repeat: false,
861
                ..Default::default()
862
            }),
863
        )
864
    }
865

            
866
    /// Straight parent chain: node 0 = root, node i's parent = node i-1.
867
    fn hierarchy_chain(len: usize) -> NodeHierarchy {
868
        let nodes = (0..len)
869
            .map(|i| Node {
870
                parent: if i == 0 {
871
                    None
872
                } else {
873
                    Some(NodeId::new(i - 1))
874
                },
875
                ..Node::ROOT
876
            })
877
            .collect::<Vec<_>>();
878
        NodeHierarchy::new(nodes)
879
    }
880

            
881
    /// Modifiers with the platform's PRIMARY modifier held (Cmd on macOS, Ctrl elsewhere).
882
    fn primary_modifiers() -> KeyModifiers {
883
        if cfg!(target_os = "macos") {
884
            KeyModifiers::new().with_meta()
885
        } else {
886
            KeyModifiers::new().with_ctrl()
887
        }
888
    }
889

            
890
    fn keyboard_with_primary_held() -> KeyboardState {
891
        let key = if cfg!(target_os = "macos") {
892
            VirtualKeyCode::LWin
893
        } else {
894
            VirtualKeyCode::LControl
895
        };
896
        KeyboardState {
897
            pressed_virtual_keycodes: VirtualKeyCodeVec::from_vec(vec![key]),
898
            ..KeyboardState::default()
899
        }
900
    }
901

            
902
    // ============================================================ numeric edge
903
    // size_changed / quantization
904

            
905
    #[test]
906
    fn size_changed_zero_and_identity() {
907
        assert!(!size_changed(LogicalSize::zero(), LogicalSize::zero()));
908
        assert!(!size_changed(
909
            LogicalSize::new(0.0, 0.0),
910
            LogicalSize::new(-0.0, -0.0)
911
        ));
912
        // 0 -> any real size is a change.
913
        assert!(size_changed(
914
            LogicalSize::zero(),
915
            LogicalSize::new(0.0, 1.0)
916
        ));
917
        assert!(size_changed(
918
            LogicalSize::zero(),
919
            LogicalSize::new(1.0, 0.0)
920
        ));
921
    }
922

            
923
    #[test]
924
    fn size_changed_single_sided_nan_is_a_change() {
925
        // NaN on ONE side only must register as changed (the both-sides NaN case
926
        // is the loop-guard covered by `size_changed_nan_guard_stops_resize_loop`).
927
        assert!(size_changed(
928
            LogicalSize::new(f32::NAN, 10.0),
929
            LogicalSize::new(10.0, 10.0)
930
        ));
931
        assert!(size_changed(
932
            LogicalSize::new(10.0, 10.0),
933
            LogicalSize::new(10.0, f32::NAN)
934
        ));
935
        // NaN on both sides in *different* dimensions is still a change in the
936
        // other dimension only if that dimension actually differs.
937
        assert!(!size_changed(
938
            LogicalSize::new(f32::NAN, f32::NAN),
939
            LogicalSize::new(f32::NAN, f32::NAN)
940
        ));
941
    }
942

            
943
    #[test]
944
    fn size_changed_negative_and_infinite_do_not_panic() {
945
        // Negative sizes (degenerate layouts) must be handled deterministically.
946
        assert!(size_changed(
947
            LogicalSize::new(-100.0, 0.0),
948
            LogicalSize::new(100.0, 0.0)
949
        ));
950
        assert!(!size_changed(
951
            LogicalSize::new(-100.0, -50.0),
952
            LogicalSize::new(-100.0, -50.0)
953
        ));
954
        // f32 * 1000.0 overflows to +/-inf, and `inf as i64` SATURATES (it does
955
        // not wrap or UB). So the comparison stays total and panic-free.
956
        assert!(!size_changed(
957
            LogicalSize::new(f32::INFINITY, f32::INFINITY),
958
            LogicalSize::new(f32::INFINITY, f32::INFINITY)
959
        ));
960
        assert!(size_changed(
961
            LogicalSize::new(f32::INFINITY, 0.0),
962
            LogicalSize::new(f32::NEG_INFINITY, 0.0)
963
        ));
964
        // Finite-but-huge values saturate into the same bucket as infinity: the
965
        // documented quantization trade-off, asserted here so a future change of
966
        // the quantizer (e.g. to i128 or a float compare) is a deliberate one.
967
        assert!(!size_changed(
968
            LogicalSize::new(f32::MAX, 0.0),
969
            LogicalSize::new(f32::INFINITY, 0.0)
970
        ));
971
    }
972

            
973
    #[test]
974
    fn size_changed_ignores_sub_quantum_jitter_but_sees_one_quantum() {
975
        // The quantizer is 1/1000, so a 0.0005 wobble must be ignored...
976
        assert!(!size_changed(
977
            LogicalSize::new(50.0, 50.0),
978
            LogicalSize::new(50.0004, 50.0)
979
        ));
980
        // ...but a full quantum must be seen.
981
        assert!(size_changed(
982
            LogicalSize::new(50.0, 50.0),
983
            LogicalSize::new(50.002, 50.0)
984
        ));
985
    }
986

            
987
    // ------------------------------------------------- create_*_event numerics
988

            
989
    #[test]
990
    fn create_mount_event_without_layout_entry_falls_back_to_zero_rect() {
991
        let layout: BTreeMap<NodeId, LogicalRect> = BTreeMap::new();
992
        let ev = create_mount_event(NodeId::new(3), DomId { inner: 0 }, &layout, &tick(7));
993
        assert_eq!(ev.event_type, EventType::Mount);
994
        assert_eq!(ev.source, EventSource::Lifecycle);
995
        assert_eq!(ev.phase, EventPhase::Target);
996
        assert_eq!(ev.target, ev.current_target);
997
        assert_eq!(ev.target.node.into_crate_internal(), Some(NodeId::new(3)));
998
        match ev.data {
999
            EventData::Lifecycle(d) => {
                assert_eq!(d.reason, LifecycleReason::InitialMount);
                assert!(d.previous_bounds.is_none());
                assert_eq!(d.current_bounds, LogicalRect::zero());
            }
            _ => panic!("mount event must carry lifecycle data"),
        }
    }
    #[test]
    fn create_unmount_event_reports_previous_bounds_and_zero_current() {
        let mut layout = BTreeMap::new();
        let rect = LogicalRect::new(LogicalPosition::new(1.0, 2.0), LogicalSize::new(3.0, 4.0));
        layout.insert(NodeId::new(1), rect);
        let ev = create_unmount_event(NodeId::new(1), DomId { inner: 2 }, &layout, &tick(9));
        assert_eq!(ev.event_type, EventType::Unmount);
        match ev.data {
            EventData::Lifecycle(d) => {
                assert_eq!(d.reason, LifecycleReason::Unmount);
                assert_eq!(d.previous_bounds, Some(rect));
                assert_eq!(d.current_bounds, LogicalRect::zero());
            }
            _ => panic!("unmount event must carry lifecycle data"),
        }
    }
    #[test]
    fn create_lifecycle_event_survives_extreme_node_ids() {
        // The largest NodeId that survives the 1-based (`n + 1`) FFI encoding.
        // (NodeId::new(usize::MAX) would overflow that encoding — out of scope here.)
        let huge = NodeId::new(usize::MAX - 1);
        let layout: BTreeMap<NodeId, LogicalRect> = BTreeMap::new();
        let ev = create_mount_event(huge, DomId { inner: usize::MAX }, &layout, &tick(0));
        assert_eq!(ev.target.node.into_crate_internal(), Some(huge));
        assert_eq!(ev.target.dom, DomId { inner: usize::MAX });
        // NodeId 0 (the root) must round-trip too — the 1-based encoding makes
        // 0 the value most likely to collide with the `None` sentinel.
        let root = create_mount_event(NodeId::ZERO, DomId { inner: 0 }, &layout, &tick(0));
        assert_eq!(
            root.target.node.into_crate_internal(),
            Some(NodeId::ZERO),
            "NodeId 0 must not decode as `None`"
        );
    }
    #[test]
    fn create_resize_event_returns_none_for_missing_or_unchanged_layout() {
        let dom = DomId { inner: 0 };
        let node = NodeId::new(1);
        let rect = LogicalRect::new(LogicalPosition::zero(), LogicalSize::new(10.0, 10.0));
        let empty: BTreeMap<NodeId, LogicalRect> = BTreeMap::new();
        let mut one = BTreeMap::new();
        one.insert(node, rect);
        // Missing in old, missing in new, missing in both -> None (no panic).
        assert!(create_resize_event(node, dom, &empty, &one, &tick(0)).is_none());
        assert!(create_resize_event(node, dom, &one, &empty, &tick(0)).is_none());
        assert!(create_resize_event(node, dom, &empty, &empty, &tick(0)).is_none());
        // Present on both sides but unchanged -> None.
        assert!(create_resize_event(node, dom, &one, &one, &tick(0)).is_none());
    }
    #[test]
    fn create_resize_event_ignores_pure_origin_moves() {
        // Only the SIZE is compared: moving a node without resizing it must not
        // emit a Resize event.
        let dom = DomId { inner: 0 };
        let node = NodeId::new(0);
        let size = LogicalSize::new(10.0, 10.0);
        let mut old = BTreeMap::new();
        old.insert(node, LogicalRect::new(LogicalPosition::new(0.0, 0.0), size));
        let mut new = BTreeMap::new();
        new.insert(
            node,
            LogicalRect::new(LogicalPosition::new(500.0, 500.0), size),
        );
        assert!(create_resize_event(node, dom, &old, &new, &tick(0)).is_none());
    }
    #[test]
    fn create_resize_event_nan_size_does_not_loop_forever() {
        // Regression guard: a NaN dimension on BOTH frames must NOT emit a Resize
        // every frame (a raw f32 `!=` would, since NaN != NaN).
        let dom = DomId { inner: 0 };
        let node = NodeId::new(0);
        let nan_rect = LogicalRect::new(LogicalPosition::zero(), LogicalSize::new(f32::NAN, 100.0));
        let mut old = BTreeMap::new();
        old.insert(node, nan_rect);
        let mut new = BTreeMap::new();
        new.insert(node, nan_rect);
        assert!(create_resize_event(node, dom, &old, &new, &tick(0)).is_none());
    }
    #[test]
    fn create_resize_event_reports_both_bounds_on_real_change() {
        let dom = DomId { inner: 0 };
        let node = NodeId::new(0);
        let old_rect = LogicalRect::new(LogicalPosition::zero(), LogicalSize::new(10.0, 10.0));
        let new_rect = LogicalRect::new(LogicalPosition::zero(), LogicalSize::new(10.0, 20.0));
        let mut old = BTreeMap::new();
        old.insert(node, old_rect);
        let mut new = BTreeMap::new();
        new.insert(node, new_rect);
        let ev = create_resize_event(node, dom, &old, &new, &tick(3))
            .expect("a real size change must emit a Resize");
        assert_eq!(ev.event_type, EventType::Resize);
        match ev.data {
            EventData::Lifecycle(d) => {
                assert_eq!(d.reason, LifecycleReason::Resize);
                assert_eq!(d.previous_bounds, Some(old_rect));
                assert_eq!(d.current_bounds, new_rect);
            }
            _ => panic!("resize event must carry lifecycle data"),
        }
    }
    // ------------------------------------------------- detect_lifecycle_events
    #[test]
    fn detect_lifecycle_events_all_none_is_empty() {
        let events = detect_lifecycle_events(
            DomId { inner: 0 },
            DomId { inner: 0 },
            None,
            None,
            None,
            None,
            tick(0),
        );
        assert!(events.is_empty());
    }
    #[test]
    fn detect_lifecycle_events_without_layout_emits_nothing() {
        // Hierarchies differ, but no layout maps -> the fn must not fabricate events.
        let old = hierarchy_chain(1);
        let new = hierarchy_chain(4);
        let events = detect_lifecycle_events(
            DomId { inner: 0 },
            DomId { inner: 0 },
            Some(&old),
            Some(&new),
            None,
            None,
            tick(0),
        );
        assert!(events.is_empty());
    }
    #[test]
    fn detect_lifecycle_events_emits_mounts_unmounts_and_resizes() {
        let dom = DomId { inner: 0 };
        let old_hier = hierarchy_chain(2); // nodes 0,1
        let new_hier = hierarchy_chain(3); // nodes 0,1,2
        let r = |h: f32| LogicalRect::new(LogicalPosition::zero(), LogicalSize::new(10.0, h));
        let mut old_layout = BTreeMap::new();
        old_layout.insert(NodeId::new(0), r(10.0));
        old_layout.insert(NodeId::new(1), r(10.0));
        let mut new_layout = BTreeMap::new();
        new_layout.insert(NodeId::new(0), r(10.0)); // unchanged
        new_layout.insert(NodeId::new(1), r(99.0)); // resized
        new_layout.insert(NodeId::new(2), r(10.0)); // mounted
        let events = detect_lifecycle_events(
            dom,
            dom,
            Some(&old_hier),
            Some(&new_hier),
            Some(&old_layout),
            Some(&new_layout),
            tick(5),
        );
        let mounts: Vec<_> = events
            .iter()
            .filter(|e| e.event_type == EventType::Mount)
            .collect();
        let resizes: Vec<_> = events
            .iter()
            .filter(|e| e.event_type == EventType::Resize)
            .collect();
        assert_eq!(mounts.len(), 1, "only node 2 is new");
        assert_eq!(
            mounts[0].target.node.into_crate_internal(),
            Some(NodeId::new(2))
        );
        assert_eq!(resizes.len(), 1, "only node 1 changed size");
        assert_eq!(
            resizes[0].target.node.into_crate_internal(),
            Some(NodeId::new(1))
        );
        assert!(
            !events.iter().any(|e| e.event_type == EventType::Unmount),
            "nothing was removed"
        );
        assert!(events.iter().all(|e| e.source == EventSource::Lifecycle));
        // Reverse direction: the removed node must unmount.
        let events = detect_lifecycle_events(
            dom,
            dom,
            Some(&new_hier),
            Some(&old_hier),
            Some(&new_layout),
            Some(&old_layout),
            tick(6),
        );
        let unmounts: Vec<_> = events
            .iter()
            .filter(|e| e.event_type == EventType::Unmount)
            .collect();
        assert_eq!(unmounts.len(), 1);
        assert_eq!(
            unmounts[0].target.node.into_crate_internal(),
            Some(NodeId::new(2))
        );
    }
    #[test]
    fn detect_lifecycle_events_mount_of_node_missing_from_layout_uses_zero_rect() {
        let dom = DomId { inner: 0 };
        let new_hier = hierarchy_chain(2);
        let new_layout: BTreeMap<NodeId, LogicalRect> = BTreeMap::new(); // empty!
        let events = detect_lifecycle_events(
            dom,
            dom,
            None,
            Some(&new_hier),
            None,
            Some(&new_layout),
            tick(0),
        );
        assert_eq!(events.len(), 2);
        for ev in &events {
            match ev.data {
                EventData::Lifecycle(d) => assert_eq!(d.current_bounds, LogicalRect::zero()),
                _ => panic!("expected lifecycle data"),
            }
        }
    }
    #[test]
    fn collect_node_ids_handles_none_and_empty_hierarchies() {
        assert!(collect_node_ids(None).is_empty());
        let empty = NodeHierarchy::new(Vec::new());
        assert!(collect_node_ids(Some(&empty)).is_empty());
        let three = hierarchy_chain(3);
        let ids = collect_node_ids(Some(&three));
        assert_eq!(ids.len(), 3);
        assert!(ids.contains(&NodeId::ZERO));
        assert!(ids.contains(&NodeId::new(2)));
    }
    // ======================================================== getters/predicates
    #[test]
    fn process_event_result_order_is_dense_and_monotonic() {
        let all = [
            ProcessEventResult::DoNothing,
            ProcessEventResult::ShouldReRenderCurrentWindow,
            ProcessEventResult::ShouldUpdateDisplayListCurrentWindow,
            ProcessEventResult::UpdateHitTesterAndProcessAgain,
            ProcessEventResult::ShouldIncrementalRelayout,
            ProcessEventResult::ShouldRegenerateDomCurrentWindow,
            ProcessEventResult::ShouldRegenerateDomAllWindows,
        ];
        for (i, r) in all.iter().enumerate() {
            assert_eq!(r.order(), i, "order() must match declaration index");
        }
        // Ord/PartialOrd must agree with order(), and max_self must be the join.
        for a in all {
            for b in all {
                assert_eq!(a < b, a.order() < b.order());
                let joined = a.max_self(b);
                assert_eq!(joined.order(), a.order().max(b.order()));
                assert_eq!(joined, b.max_self(a), "max_self must be commutative");
                assert_eq!(a.max_self(a), a, "max_self must be idempotent");
            }
        }
    }
    #[test]
    fn key_modifiers_builders_are_orthogonal_and_is_empty_tracks_them() {
        let empty = KeyModifiers::new();
        assert!(empty.is_empty());
        assert_eq!(empty, KeyModifiers::default());
        // Each builder sets exactly one flag.
        assert_eq!(
            KeyModifiers::new().with_shift(),
            KeyModifiers {
                shift: true,
                ctrl: false,
                alt: false,
                meta: false
            }
        );
        assert_eq!(
            KeyModifiers::new().with_ctrl(),
            KeyModifiers {
                shift: false,
                ctrl: true,
                alt: false,
                meta: false
            }
        );
        assert_eq!(
            KeyModifiers::new().with_alt(),
            KeyModifiers {
                shift: false,
                ctrl: false,
                alt: true,
                meta: false
            }
        );
        assert_eq!(
            KeyModifiers::new().with_meta(),
            KeyModifiers {
                shift: false,
                ctrl: false,
                alt: false,
                meta: true
            }
        );
        // Any single flag defeats is_empty; builders are idempotent and composable.
        assert!(!KeyModifiers::new().with_shift().is_empty());
        assert!(!KeyModifiers::new().with_ctrl().is_empty());
        assert!(!KeyModifiers::new().with_alt().is_empty());
        assert!(!KeyModifiers::new().with_meta().is_empty());
        assert_eq!(
            KeyModifiers::new().with_ctrl().with_ctrl(),
            KeyModifiers::new().with_ctrl()
        );
        let all = KeyModifiers::new()
            .with_shift()
            .with_ctrl()
            .with_alt()
            .with_meta();
        assert!(!all.is_empty());
        assert!(all.shift && all.ctrl && all.alt && all.meta);
    }
    #[test]
    fn scroll_into_view_options_presets_and_behavior_setters() {
        assert_eq!(
            ScrollIntoViewOptions::default(),
            ScrollIntoViewOptions::nearest(),
            "Default must be the `nearest`/`auto` preset"
        );
        for (opts, expected) in [
            (
                ScrollIntoViewOptions::nearest(),
                ScrollLogicalPosition::Nearest,
            ),
            (
                ScrollIntoViewOptions::center(),
                ScrollLogicalPosition::Center,
            ),
            (ScrollIntoViewOptions::start(), ScrollLogicalPosition::Start),
            (ScrollIntoViewOptions::end(), ScrollLogicalPosition::End),
        ] {
            assert_eq!(opts.block, expected);
            assert_eq!(
                opts.inline_axis, expected,
                "both axes must be aligned alike"
            );
            assert_eq!(opts.behavior, ScrollIntoViewBehavior::Auto);
            // The behavior setters must not disturb the axes, and last-writer-wins.
            let instant = opts.with_instant();
            assert_eq!(instant.behavior, ScrollIntoViewBehavior::Instant);
            assert_eq!(instant.block, opts.block);
            assert_eq!(instant.inline_axis, opts.inline_axis);
            let smooth = opts.with_smooth();
            assert_eq!(smooth.behavior, ScrollIntoViewBehavior::Smooth);
            assert_eq!(
                opts.with_instant().with_smooth().behavior,
                ScrollIntoViewBehavior::Smooth
            );
            assert_eq!(
                opts.with_smooth().with_instant().behavior,
                ScrollIntoViewBehavior::Instant
            );
        }
    }
    #[test]
    fn default_action_result_has_action_predicate() {
        assert!(!DefaultActionResult::default().has_action());
        assert!(!DefaultActionResult::prevented().has_action());
        assert!(DefaultActionResult::prevented().prevented);
        assert_eq!(DefaultActionResult::prevented().action, DefaultAction::None);
        // `None` action => nothing to do, even though it was not prevented.
        let none = DefaultActionResult::new(DefaultAction::None);
        assert!(!none.prevented);
        assert!(!none.has_action());
        // Any real action => has_action.
        for action in [
            DefaultAction::FocusNext,
            DefaultAction::FocusPrevious,
            DefaultAction::FocusFirst,
            DefaultAction::FocusLast,
            DefaultAction::ClearFocus,
            DefaultAction::SelectAllText,
            DefaultAction::ActivateFocusedElement { target: dnid(0, 1) },
            DefaultAction::SubmitForm {
                form_node: dnid(0, 1),
            },
            DefaultAction::CloseModal {
                modal_node: dnid(0, 1),
            },
            DefaultAction::ScrollFocusedContainer {
                direction: ScrollDirection::Down,
                amount: ScrollAmount::Page,
            },
        ] {
            let r = DefaultActionResult::new(action);
            assert_eq!(r.action, action);
            assert!(!r.prevented);
            assert!(r.has_action(), "{action:?} must be reported as actionable");
        }
    }
    #[test]
    fn synthetic_event_constructor_invariants_and_flag_transitions() {
        let target = dnid(3, 7);
        let mut ev = SyntheticEvent::new(
            EventType::Click,
            EventSource::Programmatic,
            target,
            tick(42),
            EventData::None,
        );
        // Post-construction invariants.
        assert_eq!(ev.event_type, EventType::Click);
        assert_eq!(ev.source, EventSource::Programmatic);
        assert_eq!(ev.phase, EventPhase::Target);
        assert_eq!(ev.target, target);
        assert_eq!(ev.current_target, target);
        assert_eq!(ev.timestamp, tick(42));
        assert!(!ev.is_propagation_stopped());
        assert!(!ev.is_immediate_propagation_stopped());
        assert!(!ev.is_default_prevented());
        // stop_propagation does NOT imply stop_immediate_propagation...
        ev.stop_propagation();
        assert!(ev.is_propagation_stopped());
        assert!(!ev.is_immediate_propagation_stopped());
        // ...but the reverse implication MUST hold, or propagate_phase's
        // `stopped_immediate` check could be bypassed by the `stopped` fast path.
        let mut ev2 = SyntheticEvent::new(
            EventType::Click,
            EventSource::User,
            target,
            tick(0),
            EventData::None,
        );
        ev2.stop_immediate_propagation();
        assert!(ev2.is_immediate_propagation_stopped());
        assert!(
            ev2.is_propagation_stopped(),
            "immediate stop must also stop normal propagation"
        );
        // All three flags are idempotent and independent.
        let mut ev3 = ev2.clone();
        ev3.stop_immediate_propagation();
        ev3.prevent_default();
        ev3.prevent_default();
        assert!(ev3.is_default_prevented());
        assert!(
            !ev.is_default_prevented(),
            "flags must not leak across events"
        );
    }
    #[test]
    fn hover_filter_is_system_internal_only_for_system_text_clicks() {
        for f in [
            HoverEventFilter::SystemTextSingleClick,
            HoverEventFilter::SystemTextDoubleClick,
            HoverEventFilter::SystemTextTripleClick,
        ] {
            assert!(f.is_system_internal(), "{f:?} is internal");
            assert!(
                f.to_focus_event_filter().is_none(),
                "internal filters must never be exposed as focus callbacks"
            );
        }
        for f in [
            HoverEventFilter::MouseOver,
            HoverEventFilter::MouseDown,
            HoverEventFilter::Drop,
            HoverEventFilter::KeyringResult,
            HoverEventFilter::MouseOut,
        ] {
            assert!(!f.is_system_internal(), "{f:?} is a user-visible filter");
        }
    }
    #[test]
    fn event_filter_kind_predicates_are_mutually_exclusive() {
        let hover = EventFilter::Hover(HoverEventFilter::MouseDown);
        let focus = EventFilter::Focus(FocusEventFilter::FocusReceived);
        let window = EventFilter::Window(WindowEventFilter::Resized);
        let component = EventFilter::Component(ComponentEventFilter::AfterMount);
        let app = EventFilter::Application(ApplicationEventFilter::DeviceConnected);
        assert!(focus.is_focus_callback());
        assert!(window.is_window_callback());
        for f in [hover, window, component, app] {
            assert!(!f.is_focus_callback(), "{f:?} is not a focus callback");
        }
        for f in [hover, focus, component, app] {
            assert!(!f.is_window_callback(), "{f:?} is not a window callback");
        }
        // The `as_*` accessors must agree with the predicates.
        assert_eq!(
            hover.as_hover_event_filter(),
            Some(HoverEventFilter::MouseDown)
        );
        assert_eq!(hover.as_focus_event_filter(), None);
        assert_eq!(hover.as_window_event_filter(), None);
        assert_eq!(
            focus.as_focus_event_filter(),
            Some(FocusEventFilter::FocusReceived)
        );
        assert_eq!(
            window.as_window_event_filter(),
            Some(WindowEventFilter::Resized)
        );
        assert_eq!(component.as_hover_event_filter(), None);
    }
    // ============================================================== round-trips
    #[test]
    fn window_to_hover_filter_mapping_never_yields_an_internal_filter() {
        // Every window filter that has a hover twin must map onto a filter the
        // user is actually allowed to register (never a SystemText* internal).
        for w in [
            WindowEventFilter::MouseOver,
            WindowEventFilter::MouseDown,
            WindowEventFilter::LeftMouseDown,
            WindowEventFilter::RightMouseDown,
            WindowEventFilter::MiddleMouseDown,
            WindowEventFilter::MouseUp,
            WindowEventFilter::LeftMouseUp,
            WindowEventFilter::RightMouseUp,
            WindowEventFilter::MiddleMouseUp,
            WindowEventFilter::Scroll,
            WindowEventFilter::TextInput,
            WindowEventFilter::VirtualKeyDown,
            WindowEventFilter::VirtualKeyUp,
            WindowEventFilter::HoveredFile,
            WindowEventFilter::DroppedFile,
            WindowEventFilter::HoveredFileCancelled,
            WindowEventFilter::TouchStart,
            WindowEventFilter::TouchEnd,
            WindowEventFilter::PenDown,
            WindowEventFilter::DragStart,
            WindowEventFilter::Drop,
            WindowEventFilter::DoubleClick,
            WindowEventFilter::PermissionChanged,
            WindowEventFilter::BiometricResult,
            WindowEventFilter::ScreenColorPicked,
            WindowEventFilter::KeyringResult,
        ] {
            let hover = w
                .to_hover_event_filter()
                .unwrap_or_else(|| panic!("{w:?} should have a hover twin"));
            assert!(
                !hover.is_system_internal(),
                "{w:?} must not map onto an internal filter"
            );
        }
        // Window-only events have deliberately NO hover twin.
        for w in [
            WindowEventFilter::MouseEnter,
            WindowEventFilter::MouseLeave,
            WindowEventFilter::Resized,
            WindowEventFilter::Moved,
            WindowEventFilter::FocusReceived,
            WindowEventFilter::FocusLost,
            WindowEventFilter::CloseRequested,
            WindowEventFilter::ThemeChanged,
            WindowEventFilter::WindowFocusReceived,
            WindowEventFilter::WindowFocusLost,
            WindowEventFilter::DpiChanged,
            WindowEventFilter::MonitorChanged,
        ] {
            assert_eq!(
                w.to_hover_event_filter(),
                None,
                "{w:?} is window-specific and must not map to a hover filter"
            );
        }
    }
    #[test]
    fn window_hover_focus_filter_names_round_trip() {
        // Window -> Hover -> Focus must preserve the *identity* of the event for
        // the shared (mouse / key / drag) subset — a mismatched row here means a
        // callback registered as Focus(X) would fire for hover event Y.
        let pairs = [
            (
                WindowEventFilter::MouseOver,
                HoverEventFilter::MouseOver,
                Some(FocusEventFilter::MouseOver),
            ),
            (
                WindowEventFilter::LeftMouseDown,
                HoverEventFilter::LeftMouseDown,
                Some(FocusEventFilter::LeftMouseDown),
            ),
            (
                WindowEventFilter::RightMouseUp,
                HoverEventFilter::RightMouseUp,
                Some(FocusEventFilter::RightMouseUp),
            ),
            (
                WindowEventFilter::TextInput,
                HoverEventFilter::TextInput,
                Some(FocusEventFilter::TextInput),
            ),
            (
                WindowEventFilter::VirtualKeyDown,
                HoverEventFilter::VirtualKeyDown,
                Some(FocusEventFilter::VirtualKeyDown),
            ),
            (
                WindowEventFilter::DragStart,
                HoverEventFilter::DragStart,
                Some(FocusEventFilter::DragStart),
            ),
            (
                WindowEventFilter::Drop,
                HoverEventFilter::Drop,
                Some(FocusEventFilter::Drop),
            ),
            // File events exist on window + hover, but have no focus twin.
            (
                WindowEventFilter::DroppedFile,
                HoverEventFilter::DroppedFile,
                None,
            ),
            (
                WindowEventFilter::TouchStart,
                HoverEventFilter::TouchStart,
                None,
            ),
        ];
        for (w, h, f) in pairs {
            assert_eq!(
                w.to_hover_event_filter(),
                Some(h),
                "window->hover for {w:?}"
            );
            assert_eq!(h.to_focus_event_filter(), f, "hover->focus for {h:?}");
        }
    }
    #[test]
    fn on_to_event_filter_conversion_is_stable() {
        use crate::dom::On;
        // On::TextInput / FocusReceived / FocusLost are FOCUS filters, and the
        // virtual-key events are WINDOW filters — everything else is Hover.
        assert_eq!(
            EventFilter::from(On::TextInput),
            EventFilter::Focus(FocusEventFilter::TextInput)
        );
        assert_eq!(
            EventFilter::from(On::VirtualKeyDown),
            EventFilter::Window(WindowEventFilter::VirtualKeyDown)
        );
        assert_eq!(
            EventFilter::from(On::MouseOver),
            EventFilter::Hover(HoverEventFilter::MouseOver)
        );
        // The a11y actions all collapse onto the ACTIVATION filter. They used
        // to map to `MouseUp`, which conflated "the user activated this" with
        // a raw pointer release - and a raw release is not something a screen
        // reader can produce.
        for on in [
            On::Default,
            On::Collapse,
            On::Expand,
            On::Increment,
            On::Decrement,
        ] {
            assert_eq!(
                EventFilter::from(on),
                EventFilter::Hover(HoverEventFilter::Click),
                "{on:?} must map to the click filter"
            );
        }
        assert!(EventFilter::from(On::TextInput).is_focus_callback());
        assert!(EventFilter::from(On::VirtualKeyUp).is_window_callback());
    }
    #[test]
    fn virtual_keycode_round_trips_for_every_key_events_rs_interprets() {
        // handle_key_down decodes `KeyboardEventData.key_code` with `from_u32`,
        // while producers write `vk as u32`. If that round-trip ever breaks, every
        // shortcut silently dies — so pin it for the keys this module interprets.
        for vk in [
            VirtualKeyCode::Left,
            VirtualKeyCode::Right,
            VirtualKeyCode::Up,
            VirtualKeyCode::Down,
            VirtualKeyCode::Home,
            VirtualKeyCode::End,
            VirtualKeyCode::Back,
            VirtualKeyCode::Delete,
            VirtualKeyCode::A,
            VirtualKeyCode::C,
            VirtualKeyCode::D,
            VirtualKeyCode::V,
            VirtualKeyCode::X,
            VirtualKeyCode::Y,
            VirtualKeyCode::Z,
        ] {
            assert_eq!(
                VirtualKeyCode::from_u32(vk as u32),
                Some(vk),
                "{vk:?} must survive the as-u32 / from_u32 round trip"
            );
        }
        // Out-of-range key codes must decode to None rather than index out of bounds.
        assert_eq!(VirtualKeyCode::from_u32(u32::MAX), None);
        assert_eq!(VirtualKeyCode::from_u32(100_000), None);
    }
    // ============================================ ArrowDirection / KeyboardShortcut
    #[test]
    fn arrow_direction_from_key_is_total_over_every_decodable_key() {
        // Fuzz every decodable key code (plus the undecodable tail) through both
        // key mappers: they must never panic and must only claim the nav keys.
        let nav = [
            VirtualKeyCode::Left,
            VirtualKeyCode::Right,
            VirtualKeyCode::Up,
            VirtualKeyCode::Down,
            VirtualKeyCode::Home,
            VirtualKeyCode::End,
        ];
        for raw in 0u32..1024 {
            let Some(vk) = VirtualKeyCode::from_u32(raw) else {
                continue;
            };
            for ctrl in [false, true] {
                let got = ArrowDirection::from_key(vk, ctrl);
                assert_eq!(
                    got.is_some(),
                    nav.contains(&vk),
                    "{vk:?} (ctrl={ctrl}) must map to an ArrowDirection iff it is a nav key"
                );
                if let Some(dir) = got {
                    // to_selection is total and never panics for any (dir, ctrl).
                    let (_d, _s) = dir.to_selection(ctrl);
                }
            }
        }
    }
    #[test]
    fn arrow_direction_ctrl_only_upgrades_horizontal_arrows_to_words() {
        // ctrl must upgrade Left/Right to Word steps, and must NOT change the
        // step for Up/Down/Home/End (those are already line/document scoped).
        for (dir, expect_no_ctrl, expect_ctrl) in [
            (
                ArrowDirection::Left,
                (SelectionDirection::Backward, SelectionStep::Character),
                (SelectionDirection::Backward, SelectionStep::Word),
            ),
            (
                ArrowDirection::Right,
                (SelectionDirection::Forward, SelectionStep::Character),
                (SelectionDirection::Forward, SelectionStep::Word),
            ),
            (
                ArrowDirection::Up,
                (SelectionDirection::Backward, SelectionStep::VisualLine),
                (SelectionDirection::Backward, SelectionStep::VisualLine),
            ),
            (
                ArrowDirection::Down,
                (SelectionDirection::Forward, SelectionStep::VisualLine),
                (SelectionDirection::Forward, SelectionStep::VisualLine),
            ),
            (
                ArrowDirection::LineStart,
                (SelectionDirection::Backward, SelectionStep::Line),
                (SelectionDirection::Backward, SelectionStep::Line),
            ),
            (
                ArrowDirection::DocumentEnd,
                (SelectionDirection::Forward, SelectionStep::Document),
                (SelectionDirection::Forward, SelectionStep::Document),
            ),
        ] {
            assert_eq!(dir.to_selection(false), expect_no_ctrl, "{dir:?} plain");
            assert_eq!(dir.to_selection(true), expect_ctrl, "{dir:?} + ctrl");
        }
        // Ctrl+Home/End are distinct DIRECTIONS (not a step upgrade).
        assert_eq!(
            ArrowDirection::from_key(VirtualKeyCode::Home, true),
            Some(ArrowDirection::DocumentStart)
        );
        assert_eq!(
            ArrowDirection::from_key(VirtualKeyCode::End, true),
            Some(ArrowDirection::DocumentEnd)
        );
    }
    #[test]
    fn keyboard_shortcut_from_key_requires_primary_for_every_key() {
        // Without the primary modifier NO key may produce a shortcut — otherwise
        // typing plain "c" into a text field would copy.
        for raw in 0u32..1024 {
            let Some(vk) = VirtualKeyCode::from_u32(raw) else {
                continue;
            };
            for shift in [false, true] {
                assert_eq!(
                    KeyboardShortcut::from_key(vk, false, shift),
                    None,
                    "{vk:?} (shift={shift}) must need the primary modifier"
                );
            }
        }
        // With primary held, exactly the editing set is recognised.
        assert_eq!(
            KeyboardShortcut::from_key(VirtualKeyCode::Z, true, true),
            Some(KeyboardShortcut::Redo),
            "primary+shift+Z is Redo, not Undo"
        );
        assert_eq!(
            KeyboardShortcut::from_key(VirtualKeyCode::Y, true, true),
            Some(KeyboardShortcut::Redo),
            "shift must not disturb primary+Y"
        );
        assert_eq!(
            KeyboardShortcut::from_key(VirtualKeyCode::C, true, true),
            Some(KeyboardShortcut::Copy),
            "shift must not disturb primary+C"
        );
        // D is handled separately (SelectNextOccurrence), not as a KeyboardShortcut.
        assert_eq!(
            KeyboardShortcut::from_key(VirtualKeyCode::D, true, false),
            None
        );
    }
    #[test]
    fn selection_op_new_defaults_to_a_single_repeat() {
        let op = SelectionOp::new(
            SelectionDirection::Forward,
            SelectionStep::Word,
            SelectionMode::Delete,
        );
        assert_eq!(op.direction, SelectionDirection::Forward);
        assert_eq!(op.step, SelectionStep::Word);
        assert_eq!(op.mode, SelectionMode::Delete);
        assert_eq!(op.repeat, 1, "a fresh op must apply exactly once");
    }
    // ================================================== filter/phase matching
    #[test]
    fn capture_phase_never_matches_any_filter() {
        // Regression guard: azul has no capture listeners. If this breaks, every
        // ancestor callback fires TWICE (once capturing, once bubbling).
        let ev = mouse_event(
            EventType::MouseDown,
            MouseButton::Left,
            LogicalPosition::zero(),
        );
        for filter in [
            EventFilter::Hover(HoverEventFilter::MouseDown),
            EventFilter::Hover(HoverEventFilter::LeftMouseDown),
            EventFilter::Focus(FocusEventFilter::MouseDown),
            EventFilter::Window(WindowEventFilter::MouseDown),
            EventFilter::Component(ComponentEventFilter::AfterMount),
            EventFilter::Application(ApplicationEventFilter::DeviceConnected),
        ] {
            assert!(
                !matches_filter_phase(filter, &ev, EventPhase::Capture),
                "{filter:?} must not match in the capture phase"
            );
        }
        // ...but the same filter DOES match at Target and Bubble.
        for phase in [EventPhase::Target, EventPhase::Bubble] {
            assert!(matches_filter_phase(
                EventFilter::Hover(HoverEventFilter::MouseDown),
                &ev,
                phase
            ));
        }
    }
    #[test]
    fn application_filters_never_match_yet() {
        // Documented stub: Application events are not routed through propagation.
        let ev = mouse_event(
            EventType::MouseDown,
            MouseButton::Left,
            LogicalPosition::zero(),
        );
        for phase in [EventPhase::Capture, EventPhase::Target, EventPhase::Bubble] {
            assert!(!matches_filter_phase(
                EventFilter::Application(ApplicationEventFilter::MonitorConnected),
                &ev,
                phase
            ));
        }
    }
    #[test]
    fn check_mouse_button_is_false_for_every_non_mouse_payload() {
        for data in [
            EventData::None,
            EventData::Keyboard(KeyboardEventData {
                key_code: 0,
                char_code: None,
                modifiers: KeyModifiers::default(),
                repeat: false,
                ..Default::default()
            }),
            EventData::Touch(TouchEventData {
                id: u64::MAX,
                position: LogicalPosition::zero(),
                force: f32::NAN,
                seat_id: crate::window::PRIMARY_POINTER_SEAT,
            }),
            EventData::Clipboard(ClipboardEventData { content: None }),
        ] {
            for button in [MouseButton::Left, MouseButton::Right, MouseButton::Middle] {
                assert!(
                    !check_mouse_button(&data, button),
                    "non-mouse payload must never claim a button"
                );
            }
        }
        // Exotic button ids compare by value, including the u8 boundary.
        let other_max = EventData::Mouse(MouseEventData {
            position: LogicalPosition::zero(),
            button: MouseButton::Other(u8::MAX),
            buttons: u8::MAX,
            modifiers: KeyModifiers::default(),
            ..Default::default()
        });
        assert!(check_mouse_button(&other_max, MouseButton::Other(u8::MAX)));
        assert!(!check_mouse_button(&other_max, MouseButton::Other(0)));
        assert!(!check_mouse_button(&other_max, MouseButton::Left));
    }
    #[test]
    fn button_specific_filters_require_the_matching_button() {
        let left = mouse_event(
            EventType::MouseDown,
            MouseButton::Left,
            LogicalPosition::zero(),
        );
        let right = mouse_event(
            EventType::MouseDown,
            MouseButton::Right,
            LogicalPosition::zero(),
        );
        let middle = mouse_event(
            EventType::MouseDown,
            MouseButton::Middle,
            LogicalPosition::zero(),
        );
        // The generic filter fires for every button...
        for ev in [&left, &right, &middle] {
            assert!(matches_hover_filter(
                HoverEventFilter::MouseDown,
                ev,
                EventPhase::Target
            ));
        }
        // ...the specific ones only for theirs.
        assert!(matches_hover_filter(
            HoverEventFilter::LeftMouseDown,
            &left,
            EventPhase::Target
        ));
        assert!(!matches_hover_filter(
            HoverEventFilter::LeftMouseDown,
            &right,
            EventPhase::Target
        ));
        assert!(matches_hover_filter(
            HoverEventFilter::RightMouseDown,
            &right,
            EventPhase::Target
        ));
        assert!(!matches_hover_filter(
            HoverEventFilter::MiddleMouseDown,
            &right,
            EventPhase::Target
        ));
        assert!(matches_hover_filter(
            HoverEventFilter::MiddleMouseDown,
            &middle,
            EventPhase::Target
        ));
        // A MouseDown filter must never fire on a MouseUp event and vice versa.
        let up = mouse_event(
            EventType::MouseUp,
            MouseButton::Left,
            LogicalPosition::zero(),
        );
        assert!(!matches_hover_filter(
            HoverEventFilter::MouseDown,
            &up,
            EventPhase::Target
        ));
        assert!(!matches_hover_filter(
            HoverEventFilter::MouseUp,
            &left,
            EventPhase::Target
        ));
        assert!(matches_focus_filter(
            FocusEventFilter::LeftMouseUp,
            &up,
            EventPhase::Target
        ));
        assert!(matches_window_filter(
            WindowEventFilter::LeftMouseUp,
            &up,
            EventPhase::Target
        ));
        // A MouseDown event carrying a NON-mouse payload cannot satisfy a
        // button-specific filter (there is no button to compare against).
        let payloadless = SyntheticEvent::new(
            EventType::MouseDown,
            EventSource::Synthetic,
            dnid(0, 0),
            tick(0),
            EventData::None,
        );
        assert!(matches_hover_filter(
            HoverEventFilter::MouseDown,
            &payloadless,
            EventPhase::Target
        ));
        assert!(!matches_hover_filter(
            HoverEventFilter::LeftMouseDown,
            &payloadless,
            EventPhase::Target
        ));
    }
    #[test]
    fn component_filter_matches_only_its_own_lifecycle_event() {
        let lifecycle = |ty: EventType| {
            SyntheticEvent::new(
                ty,
                EventSource::Lifecycle,
                dnid(0, 0),
                tick(0),
                EventData::None,
            )
        };
        let pairs = [
            (ComponentEventFilter::AfterMount, EventType::Mount),
            (ComponentEventFilter::BeforeUnmount, EventType::Unmount),
            (ComponentEventFilter::Updated, EventType::Update),
            (ComponentEventFilter::NodeResized, EventType::Resize),
            (ComponentEventFilter::Dismissed, EventType::Dismiss),
            (ComponentEventFilter::TornOff, EventType::TearOff),
            (ComponentEventFilter::Docked, EventType::Dock),
        ];
        for (filter, ty) in pairs {
            let ev = lifecycle(ty);
            assert!(
                matches_component_filter(filter, &ev, EventPhase::Target),
                "{filter:?} must match {ty:?}"
            );
            // ...and must NOT match any of the other lifecycle event types.
            for (_, other_ty) in pairs.iter().filter(|(_, t)| *t != ty) {
                assert!(
                    !matches_component_filter(filter, &lifecycle(*other_ty), EventPhase::Target),
                    "{filter:?} must not match {other_ty:?}"
                );
            }
        }
        // DefaultAction / Selected have no EventType twin: they must never match
        // a lifecycle event (they are driven by the a11y layer instead).
        for filter in [
            ComponentEventFilter::DefaultAction,
            ComponentEventFilter::Selected,
        ] {
            for (_, ty) in pairs {
                assert!(!matches_component_filter(
                    filter,
                    &lifecycle(ty),
                    EventPhase::Target
                ));
            }
        }
    }
    /// The next `EventType` in declaration order, or `None` at the end.
    ///
    /// THIS IS THE ENUMERATION, and it is sound in a way a hand-written array
    /// is not: the match is exhaustive, so adding a variant fails to compile
    /// until it is spliced into the chain - and splicing it in automatically
    /// puts it in the walk. A parallel `ALL_EVENT_TYPES` array would let a new
    /// variant be added to a match and forgotten in the list, which is how
    /// this ratchet came to cover 65% of the enum while reading as green, and
    /// how `TIER1_SLOTS` let `cursor` overwrite `align-self`.
    fn next_event_type(ty: EventType) -> Option<EventType> {
        Some(match ty {
            EventType::MouseOver => EventType::MouseEnter,
            EventType::MouseEnter => EventType::MouseLeave,
            EventType::MouseLeave => EventType::MouseOut,
            EventType::MouseOut => EventType::MouseDown,
            EventType::MouseDown => EventType::MouseUp,
            EventType::MouseUp => EventType::Click,
            EventType::Click => EventType::DoubleClick,
            EventType::DoubleClick => EventType::ContextMenu,
            EventType::ContextMenu => EventType::KeyDown,
            EventType::KeyDown => EventType::KeyUp,
            EventType::KeyUp => EventType::KeyPress,
            EventType::KeyPress => EventType::CompositionStart,
            EventType::CompositionStart => EventType::CompositionUpdate,
            EventType::CompositionUpdate => EventType::CompositionEnd,
            EventType::CompositionEnd => EventType::Focus,
            EventType::Focus => EventType::Blur,
            EventType::Blur => EventType::FocusIn,
            EventType::FocusIn => EventType::FocusOut,
            EventType::FocusOut => EventType::Input,
            EventType::Input => EventType::Change,
            EventType::Change => EventType::Submit,
            EventType::Submit => EventType::Reset,
            EventType::Reset => EventType::Invalid,
            EventType::Invalid => EventType::Scroll,
            EventType::Scroll => EventType::ScrollStart,
            EventType::ScrollStart => EventType::ScrollEnd,
            EventType::ScrollEnd => EventType::DragStart,
            EventType::DragStart => EventType::Drag,
            EventType::Drag => EventType::DragEnd,
            EventType::DragEnd => EventType::DragEnter,
            EventType::DragEnter => EventType::DragOver,
            EventType::DragOver => EventType::DragLeave,
            EventType::DragLeave => EventType::Drop,
            EventType::Drop => EventType::TouchStart,
            EventType::TouchStart => EventType::TouchMove,
            EventType::TouchMove => EventType::TouchEnd,
            EventType::TouchEnd => EventType::TouchCancel,
            EventType::TouchCancel => EventType::PenDown,
            EventType::PenDown => EventType::PenMove,
            EventType::PenMove => EventType::PenUp,
            EventType::PenUp => EventType::PenEnter,
            EventType::PenEnter => EventType::PenLeave,
            EventType::PenLeave => EventType::LongPress,
            EventType::LongPress => EventType::SwipeLeft,
            EventType::SwipeLeft => EventType::SwipeRight,
            EventType::SwipeRight => EventType::SwipeUp,
            EventType::SwipeUp => EventType::SwipeDown,
            EventType::SwipeDown => EventType::PinchIn,
            EventType::PinchIn => EventType::PinchOut,
            EventType::PinchOut => EventType::RotateClockwise,
            EventType::RotateClockwise => EventType::RotateCounterClockwise,
            EventType::RotateCounterClockwise => EventType::Copy,
            EventType::Copy => EventType::Cut,
            EventType::Cut => EventType::Paste,
            EventType::Paste => EventType::Play,
            EventType::Play => EventType::Pause,
            EventType::Pause => EventType::Ended,
            EventType::Ended => EventType::TimeUpdate,
            EventType::TimeUpdate => EventType::VolumeChange,
            EventType::VolumeChange => EventType::MediaError,
            EventType::MediaError => EventType::Mount,
            EventType::Mount => EventType::Unmount,
            EventType::Unmount => EventType::Update,
            EventType::Update => EventType::Resize,
            EventType::Resize => EventType::Dismiss,
            EventType::Dismiss => EventType::TearOff,
            EventType::TearOff => EventType::Dock,
            EventType::Dock => EventType::WindowResize,
            EventType::WindowResize => EventType::WindowMove,
            EventType::WindowMove => EventType::WindowClose,
            EventType::WindowClose => EventType::WindowFrameChanged,
            EventType::WindowFrameChanged => EventType::WindowFocusIn,
            EventType::WindowFocusIn => EventType::WindowFocusOut,
            EventType::WindowFocusOut => EventType::ThemeChange,
            EventType::ThemeChange => EventType::WindowDpiChanged,
            EventType::WindowDpiChanged => EventType::WindowMonitorChanged,
            EventType::WindowMonitorChanged => EventType::MonitorConnected,
            EventType::MonitorConnected => EventType::MonitorDisconnected,
            EventType::MonitorDisconnected => EventType::FileHover,
            EventType::FileHover => EventType::FileDrop,
            EventType::FileDrop => EventType::FileHoverCancel,
            EventType::FileHoverCancel => EventType::SensorChanged,
            EventType::SensorChanged => EventType::GamepadInput,
            EventType::GamepadInput => EventType::GeolocationFix,
            EventType::GeolocationFix => EventType::GeolocationError,
            EventType::GeolocationError => EventType::PermissionChanged,
            EventType::PermissionChanged => EventType::BiometricResult,
            EventType::BiometricResult => EventType::ScreenColorPicked,
            EventType::ScreenColorPicked => EventType::KeyringResult,
            EventType::KeyringResult => EventType::DocumentEdit,
            EventType::DocumentEdit => EventType::TextChanged,
            EventType::TextChanged => EventType::DeviceConnected,
            EventType::DeviceConnected => EventType::DeviceDisconnected,
            EventType::DeviceDisconnected => EventType::PenSqueeze,
            EventType::PenSqueeze => EventType::PenDoubleTap,
            EventType::PenDoubleTap => EventType::PenHover,
            EventType::PenHover => EventType::DefaultAction,
            EventType::DefaultAction => EventType::Selected,
            EventType::Selected => EventType::HidReport,
            EventType::HidReport => EventType::ModifiersChanged,
            EventType::ModifiersChanged => EventType::RawMouseMotion,
            EventType::RawMouseMotion => EventType::DialRotate,
            EventType::DialRotate => EventType::DialClick,
            EventType::DialClick => EventType::MouseMove,
            EventType::MouseMove => EventType::MediaControl,
            EventType::MediaControl => EventType::PointerLockChange,
            EventType::PointerLockChange => EventType::SystemAudioChange,
            EventType::SystemAudioChange => return None,
        })
    }
    /// Every `EventType`, in declaration order.
    fn all_event_types() -> Vec<EventType> {
        let mut out = vec![EventType::MouseOver];
        while let Some(next) = next_event_type(*out.last().expect("seeded above")) {
            out.push(next);
            assert!(
                out.len() < 10_000,
                "next_event_type has a cycle - an arm points backwards"
            );
        }
        out
    }
    #[test]
    fn event_type_to_filters_never_panics_and_stays_synced_with_the_hover_matcher() {
        // ROUND-TRIP INVARIANT: a Hover filter emitted by `event_type_to_filters`
        // is later re-checked by `matches_filter_phase` inside `propagate_event`
        // (see shell2/common/event.rs). If the two tables disagree, the callback
        // is collected and then silently dropped — a dead filter.
        //
        // KNOWN_DESYNC records the pairs that are ALREADY broken today (reported
        // separately). The assertion is a *subset* check, so fixing one of them
        // keeps this test green while any NEW desync fails it.
        // Entries here are pairs that are ALREADY broken (reported
        // separately). The assertion is a *subset* check, so fixing one keeps
        // this test green while any NEW desync fails it.
        //
        // Six entries were stale when this list was audited on 2026-09-01 —
        // `MouseOut`, `FocusIn`, `FocusOut` and the three `Composition*`
        // types name arms that DO exist. A stale entry is worse than no
        // entry: it silences the ratchet for a pair that currently works, so
        // a later regression lands green. Delete an entry the moment its pair
        // syncs, and never add one without a linked report.
        const KNOWN_DESYNC: &[EventType] = &[];
        let mouse_data = EventData::Mouse(MouseEventData {
            position: LogicalPosition::new(1.0, 1.0),
            button: MouseButton::Left,
            buttons: 1,
            modifiers: KeyModifiers::default(),
            ..Default::default()
        });
        let cases: Vec<(EventType, EventData)> = vec![
            (EventType::MouseOver, EventData::None),
            (EventType::MouseEnter, EventData::None),
            (EventType::MouseLeave, EventData::None),
            (EventType::MouseOut, EventData::None),
            (EventType::MouseDown, mouse_data.clone()),
            (EventType::MouseUp, mouse_data.clone()),
            (EventType::Click, mouse_data.clone()),
            (EventType::DoubleClick, mouse_data.clone()),
            (EventType::ContextMenu, mouse_data.clone()),
            (EventType::KeyDown, EventData::None),
            (EventType::KeyUp, EventData::None),
            (EventType::KeyPress, EventData::None),
            (EventType::CompositionStart, EventData::None),
            (EventType::CompositionUpdate, EventData::None),
            (EventType::CompositionEnd, EventData::None),
            (EventType::Focus, EventData::None),
            (EventType::Blur, EventData::None),
            (EventType::FocusIn, EventData::None),
            (EventType::FocusOut, EventData::None),
            (EventType::Input, EventData::None),
            (EventType::Change, EventData::None),
            (EventType::Scroll, EventData::None),
            (EventType::ScrollStart, EventData::None),
            (EventType::ScrollEnd, EventData::None),
            (EventType::DragStart, EventData::None),
            (EventType::Drag, EventData::None),
            (EventType::DragEnd, EventData::None),
            (EventType::DragEnter, EventData::None),
            (EventType::DragOver, EventData::None),
            (EventType::DragLeave, EventData::None),
            (EventType::Drop, EventData::None),
            (EventType::TouchStart, EventData::None),
            (EventType::TouchMove, EventData::None),
            (EventType::TouchEnd, EventData::None),
            (EventType::TouchCancel, EventData::None),
            (EventType::Mount, EventData::None),
            (EventType::Unmount, EventData::None),
            (EventType::Update, EventData::None),
            (EventType::Resize, EventData::None),
            (EventType::Dismiss, EventData::None),
            (EventType::TearOff, EventData::None),
            (EventType::Dock, EventData::None),
            (EventType::WindowResize, EventData::None),
            (EventType::WindowMove, EventData::None),
            (EventType::WindowClose, EventData::None),
            (EventType::ThemeChange, EventData::None),
            (EventType::FileHover, EventData::None),
            (EventType::FileDrop, EventData::None),
            (EventType::FileHoverCancel, EventData::None),
            (EventType::Copy, EventData::None),
            (EventType::Cut, EventData::None),
            (EventType::Paste, EventData::None),
            (EventType::SensorChanged, EventData::None),
            (EventType::GamepadInput, EventData::None),
            (EventType::GeolocationFix, EventData::None),
            (EventType::GeolocationError, EventData::None),
            (EventType::PermissionChanged, EventData::None),
            (EventType::BiometricResult, EventData::None),
            (EventType::ScreenColorPicked, EventData::None),
            (EventType::KeyringResult, EventData::None),
            (EventType::LongPress, EventData::None),
            (EventType::Play, EventData::None),
            (EventType::PenDown, EventData::None),
            (EventType::PenMove, EventData::None),
            (EventType::PenUp, EventData::None),
            (EventType::PenEnter, EventData::None),
            (EventType::PenLeave, EventData::None),
            (EventType::DocumentEdit, EventData::None),
            (EventType::TextChanged, EventData::None),
            // ── Added by 13f ──────────────────────────────────────────────
            // THIRTY-SIX of the 104 `EventType`s were absent from this list,
            // so the ratchet was green over 65% of the enum and silent about
            // the rest - including `MouseMove`, and including every type this
            // arc added. An allow-list that is empty proves nothing if the
            // table it guards is not complete; that is the same shape as the
            // `TIER1_SLOTS` table that let `cursor` overwrite `align-self`.
            // The exhaustiveness match below now makes an omission impossible.
            (EventType::MouseMove, mouse_data.clone()),
            (EventType::ModifiersChanged, EventData::None),
            (EventType::RawMouseMotion, EventData::None),
            (EventType::Submit, EventData::None),
            (EventType::Reset, EventData::None),
            (EventType::Invalid, EventData::None),
            (EventType::Selected, EventData::None),
            (EventType::DefaultAction, EventData::None),
            (EventType::SwipeLeft, EventData::None),
            (EventType::SwipeRight, EventData::None),
            (EventType::SwipeUp, EventData::None),
            (EventType::SwipeDown, EventData::None),
            (EventType::PinchIn, EventData::None),
            (EventType::PinchOut, EventData::None),
            (EventType::RotateClockwise, EventData::None),
            (EventType::RotateCounterClockwise, EventData::None),
            (EventType::Pause, EventData::None),
            (EventType::Ended, EventData::None),
            (EventType::TimeUpdate, EventData::None),
            (EventType::VolumeChange, EventData::None),
            (EventType::MediaError, EventData::None),
            (EventType::WindowFrameChanged, EventData::None),
            (EventType::WindowFocusIn, EventData::None),
            (EventType::WindowFocusOut, EventData::None),
            (EventType::WindowDpiChanged, EventData::None),
            (EventType::WindowMonitorChanged, EventData::None),
            (EventType::MonitorConnected, EventData::None),
            (EventType::MonitorDisconnected, EventData::None),
            (EventType::DeviceConnected, EventData::None),
            (EventType::DeviceDisconnected, EventData::None),
            (EventType::PenSqueeze, EventData::None),
            (EventType::PenDoubleTap, EventData::None),
            (EventType::PenHover, EventData::None),
            (EventType::HidReport, EventData::None),
            (EventType::DialRotate, EventData::None),
            (EventType::DialClick, EventData::None),
            (EventType::MediaControl, EventData::None),
            (EventType::PointerLockChange, EventData::None),
            (EventType::SystemAudioChange, EventData::None),
        ];
        // COVERAGE PROOF. Not "the list looks complete" - `all_event_types`
        // walks the enum itself, so a variant that exists and is missing here
        // fails the test by name rather than being silently skipped.
        {
            let covered: BTreeSet<EventType> = cases.iter().map(|(ty, _)| *ty).collect();
            assert_eq!(
                covered.len(),
                cases.len(),
                "`cases` lists the same EventType twice"
            );
            let missing: Vec<EventType> = all_event_types()
                .into_iter()
                .filter(|ty| !covered.contains(ty))
                .collect();
            assert!(
                missing.is_empty(),
                "the ratchet does not cover {} of {} EventTypes, so a desync in them lands green: \
                 {missing:?}",
                missing.len(),
                all_event_types().len(),
            );
        }
        for (ty, data) in cases {
            let filters = event_type_to_filters(ty, &data);
            let ev = SyntheticEvent::new(ty, EventSource::User, dnid(0, 0), tick(0), data);
            // No duplicate filters — a duplicate would invoke the callback twice.
            let mut seen = BTreeSet::new();
            for f in &filters {
                assert!(seen.insert(*f), "{ty:?} emitted {f:?} twice");
            }
            // Every family, not just Hover. `propagate_event` re-checks each
            // planned filter through `matches_filter_phase` regardless of
            // which family it belongs to, so a Focus or Window desync drops
            // the callback exactly as silently as a Hover one — and both
            // existed: `matches_focus_filter` had no Pen arm at all, and
            // planning named only the Hover half of Touch.
            for f in &filters {
                if matches_filter_phase(*f, &ev, EventPhase::Target) {
                    continue;
                }
                assert!(
                    KNOWN_DESYNC.contains(&ty),
                    "NEW DESYNC: event_type_to_filters({ty:?}) emits {f:?}, but \
                     matches_filter_phase rejects it at the Target phase, so the callback would \
                     be collected and then silently dropped"
                );
            }
        }
    }
    #[test]
    fn event_type_to_filters_omits_button_specific_filter_for_exotic_buttons() {
        // MouseButton::Other(n) has no dedicated filter: only the generic one.
        let data = EventData::Mouse(MouseEventData {
            position: LogicalPosition::zero(),
            button: MouseButton::Other(u8::MAX),
            buttons: 0,
            modifiers: KeyModifiers::default(),
            ..Default::default()
        });
        let down = event_type_to_filters(EventType::MouseDown, &data);
        assert_eq!(
            down,
            vec![
                // Planning is scope-complete now (it is derived from the
                // matcher), so the generic MouseDown appears in all three
                // scopes. The POINT of this test is unchanged: no
                // button-SPECIFIC filter for an exotic button.
                EventFilter::Hover(HoverEventFilter::MouseDown),
                EventFilter::Focus(FocusEventFilter::MouseDown),
                EventFilter::Window(WindowEventFilter::MouseDown),
            ]
        );
        let up = event_type_to_filters(EventType::MouseUp, &data);
        assert_eq!(
            up,
            vec![
                EventFilter::Hover(HoverEventFilter::MouseUp),
                EventFilter::Focus(FocusEventFilter::MouseUp),
                EventFilter::Window(WindowEventFilter::MouseUp),
            ]
        );
        // The unmapped-pin loop is EMPTY now (11c). It used to hold the media
        // six — Play / Pause / Ended / TimeUpdate / VolumeChange / MediaError
        // — with the note that they had no playback state machine to fire
        // from, so giving them filters would advertise events that cannot
        // happen. `azul_layout::managers::media_player` is that state machine,
        // so they moved to the POSITIVE loop below; Submit / Change / Reset /
        // Invalid left the same way earlier. A type left here would have to be
        // one that genuinely cannot be produced.
        let genuinely_unmapped: [EventType; 0] = [];
        for ty in genuinely_unmapped {
            assert!(
                event_type_to_filters(ty, &EventData::None).is_empty(),
                "{ty:?} is unmapped and must yield no filters"
            );
        }
        // Gestures ARE mapped (they were not, which is why a Pinch callback
        // never fired) — see every_gesture_event_matches_its_same_named_filter.
        //
        // The pen block and `DocumentEdit` are here for the same reason, found
        // 2026-09-01: they had a producer and a matcher arm but NO planning
        // arm, so they fell through `_ => vec![]` and planned an empty filter
        // list. An empty list is the silent failure this loop exists to catch
        // — nothing is looked up, nothing is rejected, nothing is logged.
        // Touch and the scroll phases were the same shape, one family deep:
        // planning named only the Hover half.
        for ty in [
            EventType::PinchIn,
            EventType::RotateClockwise,
            EventType::SwipeLeft,
            EventType::Submit,
            EventType::Change,
            EventType::Reset,
            EventType::Invalid,
            EventType::PenDown,
            EventType::PenMove,
            EventType::PenUp,
            EventType::PenEnter,
            EventType::PenLeave,
            EventType::DocumentEdit,
            EventType::ScrollStart,
            EventType::ScrollEnd,
            EventType::TouchStart,
            EventType::TouchMove,
            EventType::TouchEnd,
            EventType::TouchCancel,
            // The media six (11c). They were the last entries in the unmapped
            // pin above: six `EventType`s with NO filter in any family, so
            // planning fell through `_ => vec![]` and every one of them
            // dispatched to nothing. What unblocked them is
            // `azul_layout::managers::media_player::MediaPlayerManager` —
            // `{ playing, position, duration, volume, muted }` plus a
            // transport — which is the thing that can honestly raise them.
            EventType::Play,
            EventType::Pause,
            EventType::Ended,
            EventType::TimeUpdate,
            EventType::VolumeChange,
            EventType::MediaError,
        ] {
            assert!(
                !event_type_to_filters(ty, &EventData::None).is_empty(),
                "{ty:?} must map to its gesture filters"
            );
        }
    }
    /// The media six reach BOTH their homes (11c).
    ///
    /// `External` is the family they belong to — a player's state change is
    /// not hovered, not focused and not hit-tested, it is reported IN from
    /// outside, the same shape as a monitor being unplugged. `Window` is the
    /// mirror, so an app that already listens window-wide hears them without
    /// learning a second family.
    ///
    /// This pins the whole chain, not just the enum: `event_type_to_filters`
    /// is DERIVED by probing `ALL_EXTERNAL` / `ALL_WINDOW` against the
    /// matcher, so a variant that exists but is missing from its probe list
    /// (the failure this arc hit repeatedly) fails right here.
    #[test]
    fn the_media_events_plan_their_external_and_window_filters() {
        use crate::events::{ExternalEventFilter as X, WindowEventFilter as W};
        for (ty, external, window) in [
            (EventType::Play, X::Play, W::Play),
            (EventType::Pause, X::Pause, W::Pause),
            (EventType::Ended, X::Ended, W::Ended),
            (EventType::TimeUpdate, X::TimeUpdate, W::TimeUpdate),
            (EventType::VolumeChange, X::VolumeChange, W::VolumeChange),
            (EventType::MediaError, X::MediaError, W::MediaError),
        ] {
            let planned = event_type_to_filters(ty, &EventData::None);
            assert!(
                planned.contains(&EventFilter::External(external)),
                "{ty:?} must plan {external:?}, got {planned:?}",
            );
            assert!(
                planned.contains(&EventFilter::Window(window)),
                "{ty:?} must plan {window:?}, got {planned:?}",
            );
        }
    }
    /// A media filter must not answer for a DIFFERENT media event.
    ///
    /// Six near-identical arms in two tables is exactly where a copy-paste
    /// pairs `Pause` with `EventType::Play`; the cross-product catches it.
    #[test]
    fn each_media_filter_answers_for_exactly_one_event_type() {
        use crate::events::ExternalEventFilter as X;
        let pairs = [
            (EventType::Play, X::Play),
            (EventType::Pause, X::Pause),
            (EventType::Ended, X::Ended),
            (EventType::TimeUpdate, X::TimeUpdate),
            (EventType::VolumeChange, X::VolumeChange),
            (EventType::MediaError, X::MediaError),
        ];
        for (ty, _) in pairs {
            let planned = event_type_to_filters(ty, &EventData::None);
            for (other_ty, other_filter) in pairs {
                let expected = other_ty == ty;
                assert_eq!(
                    planned.contains(&EventFilter::External(other_filter)),
                    expected,
                    "{ty:?} planning {other_filter:?}: expected {expected}",
                );
            }
        }
    }
    // ================================================== DOM path / propagation
    #[test]
    fn get_dom_path_none_target_yields_empty_path() {
        let hier = hierarchy_chain(3);
        assert!(get_dom_path(&hier, NodeHierarchyItemId::NONE).is_empty());
        // An empty hierarchy with a real target must not index out of bounds.
        let empty = NodeHierarchy::new(Vec::new());
        let path = get_dom_path(
            &empty,
            NodeHierarchyItemId::from_crate_internal(Some(NodeId::ZERO)),
        );
        assert_eq!(
            path,
            vec![NodeId::ZERO],
            "unknown nodes still path to themselves"
        );
    }
    #[test]
    fn get_dom_path_out_of_range_target_does_not_panic() {
        let hier = hierarchy_chain(3);
        let huge = NodeHierarchyItemId::from_crate_internal(Some(NodeId::new(usize::MAX - 1)));
        let path = get_dom_path(&hier, huge);
        assert_eq!(path, vec![NodeId::new(usize::MAX - 1)]);
    }
    #[test]
    fn get_dom_path_returns_root_to_target_order() {
        let hier = hierarchy_chain(4); // 0 <- 1 <- 2 <- 3
        let path = get_dom_path(
            &hier,
            NodeHierarchyItemId::from_crate_internal(Some(NodeId::new(3))),
        );
        assert_eq!(
            path,
            vec![
                NodeId::new(0),
                NodeId::new(1),
                NodeId::new(2),
                NodeId::new(3)
            ],
            "path must run root -> target"
        );
        // The root itself paths to a single-element vec.
        let root_path = get_dom_path(
            &hier,
            NodeHierarchyItemId::from_crate_internal(Some(NodeId::ZERO)),
        );
        assert_eq!(root_path, vec![NodeId::ZERO]);
    }
    #[test]
    fn get_dom_path_terminates_on_a_self_parent_cycle() {
        // A node that is its own parent must not spin forever.
        let hier = NodeHierarchy::new(vec![Node {
            parent: Some(NodeId::ZERO),
            ..Node::ROOT
        }]);
        let path = get_dom_path(
            &hier,
            NodeHierarchyItemId::from_crate_internal(Some(NodeId::ZERO)),
        );
        assert_eq!(path, vec![NodeId::ZERO]);
    }
    #[test]
    fn get_dom_path_handles_a_deep_chain_without_recursing() {
        // 5000 levels deep: an iterative walk copes, a recursive one would blow
        // the stack.
        let hier = hierarchy_chain(5000);
        let path = get_dom_path(
            &hier,
            NodeHierarchyItemId::from_crate_internal(Some(NodeId::new(4999))),
        );
        assert_eq!(path.len(), 5000);
        assert_eq!(path[0], NodeId::ZERO);
        assert_eq!(path[4999], NodeId::new(4999));
    }
    #[test]
    fn propagate_event_visits_each_node_exactly_once() {
        // Regression guard for the double-fire bug: with capture + bubble both
        // walking the ancestors, a node's callback used to be collected TWICE.
        let hier = hierarchy_chain(3); // 0 <- 1 <- 2
        let mut callbacks: BTreeMap<NodeId, Vec<EventFilter>> = BTreeMap::new();
        for i in 0..3 {
            callbacks.insert(
                NodeId::new(i),
                vec![EventFilter::Hover(HoverEventFilter::MouseDown)],
            );
        }
        let mut ev = SyntheticEvent::new(
            EventType::MouseDown,
            EventSource::User,
            dnid(0, 2),
            tick(0),
            EventData::Mouse(MouseEventData {
                position: LogicalPosition::zero(),
                button: MouseButton::Left,
                buttons: 1,
                modifiers: KeyModifiers::default(),
                ..Default::default()
            }),
        );
        let result = propagate_event(&mut ev, &hier, &callbacks);
        let nodes: Vec<NodeId> = result.callbacks_to_invoke.iter().map(|(n, _)| *n).collect();
        assert_eq!(
            nodes,
            vec![NodeId::new(2), NodeId::new(1), NodeId::new(0)],
            "target first, then bubbling up to the root — each node once"
        );
        assert!(!result.default_prevented);
    }
    /// Bug class: a child's enter/leave reaching its parent. W3C `mouseleave`
    /// does not bubble, and every node that lost hover already gets its own
    /// event, so a parent that handles `MouseLeave` must hear ONLY about the
    /// pointer leaving the parent — not a child inside it. (The slider, the
    /// map and the split pane all ended their drags on a bubbled child leave.)
    #[test]
    fn enter_and_leave_events_stop_at_their_target() {
        let hier = hierarchy_chain(3); // 0 <- 1 <- 2
        for ty in [
            EventType::MouseEnter,
            EventType::MouseLeave,
            EventType::PenEnter,
            EventType::PenLeave,
        ] {
            let filter = || {
                EventFilter::Hover(match ty {
                    EventType::MouseEnter => HoverEventFilter::MouseEnter,
                    EventType::MouseLeave => HoverEventFilter::MouseLeave,
                    EventType::PenEnter => HoverEventFilter::PenEnter,
                    _ => HoverEventFilter::PenLeave,
                })
            };
            let mut callbacks: BTreeMap<NodeId, Vec<EventFilter>> = BTreeMap::new();
            for i in 0..3 {
                callbacks.insert(NodeId::new(i), vec![filter()]);
            }
            let mut ev =
                SyntheticEvent::new(ty, EventSource::User, dnid(0, 2), tick(0), EventData::None);
            let result = propagate_event(&mut ev, &hier, &callbacks);
            let nodes: Vec<NodeId> = result.callbacks_to_invoke.iter().map(|(n, _)| *n).collect();
            assert_eq!(
                nodes,
                vec![NodeId::new(2)],
                "{ty:?} reached ancestors — it must stop at its target"
            );
            assert!(!ty.bubbles());
        }
        // The rule is narrow: a move still bubbles to every ancestor.
        assert!(EventType::MouseOver.bubbles());
        assert!(EventType::DragLeave.bubbles(), "W3C dragleave bubbles");
    }
    /// REPORTED (AzMap pinch, 2026-08-21): the gesture detectors produced
    /// `PinchIn`/`PinchOut`, the widget registered `Hover(PinchOut)`, and the
    /// callback never ran — the filter truth tables had no arm for any gesture.
    /// Every gesture event type must match its same-named filter in all three
    /// tables; a new gesture added to the enums without its arm fails here.
    #[test]
    fn every_gesture_event_matches_its_same_named_filter() {
        let gestures: [(
            EventType,
            HoverEventFilter,
            FocusEventFilter,
            WindowEventFilter,
        ); 9] = [
            (
                EventType::LongPress,
                HoverEventFilter::LongPress,
                FocusEventFilter::LongPress,
                WindowEventFilter::LongPress,
            ),
            (
                EventType::SwipeLeft,
                HoverEventFilter::SwipeLeft,
                FocusEventFilter::SwipeLeft,
                WindowEventFilter::SwipeLeft,
            ),
            (
                EventType::SwipeRight,
                HoverEventFilter::SwipeRight,
                FocusEventFilter::SwipeRight,
                WindowEventFilter::SwipeRight,
            ),
            (
                EventType::SwipeUp,
                HoverEventFilter::SwipeUp,
                FocusEventFilter::SwipeUp,
                WindowEventFilter::SwipeUp,
            ),
            (
                EventType::SwipeDown,
                HoverEventFilter::SwipeDown,
                FocusEventFilter::SwipeDown,
                WindowEventFilter::SwipeDown,
            ),
            (
                EventType::PinchIn,
                HoverEventFilter::PinchIn,
                FocusEventFilter::PinchIn,
                WindowEventFilter::PinchIn,
            ),
            (
                EventType::PinchOut,
                HoverEventFilter::PinchOut,
                FocusEventFilter::PinchOut,
                WindowEventFilter::PinchOut,
            ),
            (
                EventType::RotateClockwise,
                HoverEventFilter::RotateClockwise,
                FocusEventFilter::RotateClockwise,
                WindowEventFilter::RotateClockwise,
            ),
            (
                EventType::RotateCounterClockwise,
                HoverEventFilter::RotateCounterClockwise,
                FocusEventFilter::RotateCounterClockwise,
                WindowEventFilter::RotateCounterClockwise,
            ),
        ];
        for (ty, hover, focus, window) in gestures {
            let ev =
                SyntheticEvent::new(ty, EventSource::User, dnid(0, 0), tick(0), EventData::None);
            assert!(
                matches_hover_filter(hover, &ev, EventPhase::Target),
                "Hover({hover:?}) must match {ty:?}"
            );
            assert!(
                matches_focus_filter(focus, &ev, EventPhase::Target),
                "Focus({focus:?}) must match {ty:?}"
            );
            assert!(
                matches_window_filter(window, &ev, EventPhase::Target),
                "Window({window:?}) must match {ty:?}"
            );
            // ...and the table is a truth table, not a wildcard.
            let other = SyntheticEvent::new(
                EventType::MouseDown,
                EventSource::User,
                dnid(0, 0),
                tick(0),
                EventData::None,
            );
            assert!(!matches_hover_filter(hover, &other, EventPhase::Target));
            // The dispatcher asks THIS table which filters to try for an event
            // type; it sent every gesture to `vec![]`.
            let filters = event_type_to_filters(ty, &EventData::None);
            assert!(
                filters.contains(&EventFilter::Hover(hover)),
                "{ty:?} must dispatch to Hover({hover:?})"
            );
            assert!(
                filters.contains(&EventFilter::Focus(focus)),
                "{ty:?} must dispatch to Focus({focus:?})"
            );
            assert!(
                filters.contains(&EventFilter::Window(window)),
                "{ty:?} must dispatch to Window({window:?})"
            );
        }
    }
    #[test]
    fn an_at_target_only_event_reaches_its_target_and_no_ancestor() {
        // The captured release for a pressed node: its ancestors already saw
        // the real release through the hovered node's path.
        let hier = hierarchy_chain(3); // 0 <- 1 <- 2
        let mut callbacks: BTreeMap<NodeId, Vec<EventFilter>> = BTreeMap::new();
        for i in 0..3 {
            callbacks.insert(
                NodeId::new(i),
                vec![EventFilter::Hover(HoverEventFilter::MouseUp)],
            );
        }
        let mut ev = SyntheticEvent::new(
            EventType::MouseUp,
            EventSource::User,
            dnid(0, 2),
            tick(0),
            EventData::None,
        )
        .at_target_only();
        let result = propagate_event(&mut ev, &hier, &callbacks);
        let nodes: Vec<NodeId> = result.callbacks_to_invoke.iter().map(|(n, _)| *n).collect();
        assert_eq!(
            nodes,
            vec![NodeId::new(2)],
            "at-target-only must skip capture and bubble"
        );
        // The plain event still walks the whole path.
        let mut ev = SyntheticEvent::new(
            EventType::MouseUp,
            EventSource::User,
            dnid(0, 2),
            tick(0),
            EventData::None,
        );
        let result = propagate_event(&mut ev, &hier, &callbacks);
        // Hover filters fire in the target and bubble phases only (never capture).
        assert_eq!(
            result.callbacks_to_invoke.len(),
            3,
            "target 2 + bubble 1, 0"
        );
    }
    #[test]
    fn propagate_event_on_a_dangling_target_is_a_no_op() {
        let hier = hierarchy_chain(2);
        let callbacks: BTreeMap<NodeId, Vec<EventFilter>> = BTreeMap::new();
        // Target = the `None` sentinel: the doc comment claims a panic, but the
        // implementation returns a default result. Pin the safe behavior.
        let mut ev = SyntheticEvent::new(
            EventType::MouseDown,
            EventSource::User,
            dnid_none(0),
            tick(0),
            EventData::None,
        );
        let result = propagate_event(&mut ev, &hier, &callbacks);
        assert!(result.callbacks_to_invoke.is_empty());
        assert!(!result.default_prevented);
        // Target = a node id far outside the hierarchy: also a no-op, no panic.
        let mut ev = SyntheticEvent::new(
            EventType::MouseDown,
            EventSource::User,
            dnid(0, 10_000),
            tick(0),
            EventData::None,
        );
        let result = propagate_event(&mut ev, &hier, &callbacks);
        assert!(result.callbacks_to_invoke.is_empty());
    }
    #[test]
    fn propagate_event_respects_a_pre_stopped_event() {
        let hier = hierarchy_chain(3);
        let mut callbacks: BTreeMap<NodeId, Vec<EventFilter>> = BTreeMap::new();
        for i in 0..3 {
            callbacks.insert(
                NodeId::new(i),
                vec![EventFilter::Hover(HoverEventFilter::MouseOver)],
            );
        }
        let base = SyntheticEvent::new(
            EventType::MouseOver,
            EventSource::User,
            dnid(0, 2),
            tick(0),
            EventData::None,
        );
        // stopped => neither target nor bubble collect anything.
        let mut stopped = base.clone();
        stopped.stop_propagation();
        let r = propagate_event(&mut stopped, &hier, &callbacks);
        assert!(
            r.callbacks_to_invoke.is_empty(),
            "a stopped event collects nothing"
        );
        // stopped_immediate => likewise (and it implies `stopped`).
        let mut immediate = base.clone();
        immediate.stop_immediate_propagation();
        let r = propagate_event(&mut immediate, &hier, &callbacks);
        assert!(r.callbacks_to_invoke.is_empty());
        // prevented_default is faithfully reported back out.
        let mut prevented = base;
        prevented.prevent_default();
        let r = propagate_event(&mut prevented, &hier, &callbacks);
        assert!(r.default_prevented);
        assert_eq!(
            r.callbacks_to_invoke.len(),
            3,
            "preventDefault must not stop dispatch"
        );
    }
    #[test]
    fn propagate_event_ignores_filters_that_do_not_match_the_event() {
        let hier = hierarchy_chain(2);
        let mut callbacks: BTreeMap<NodeId, Vec<EventFilter>> = BTreeMap::new();
        callbacks.insert(
            NodeId::new(1),
            vec![
                EventFilter::Hover(HoverEventFilter::MouseUp), // wrong event type
                EventFilter::Hover(HoverEventFilter::RightMouseDown), // wrong button
                EventFilter::Hover(HoverEventFilter::LeftMouseDown), // match
            ],
        );
        let mut ev = mouse_event(
            EventType::MouseDown,
            MouseButton::Left,
            LogicalPosition::zero(),
        );
        ev.target = dnid(0, 1);
        ev.current_target = ev.target;
        let r = propagate_event(&mut ev, &hier, &callbacks);
        assert_eq!(
            r.callbacks_to_invoke,
            vec![(
                NodeId::new(1),
                EventFilter::Hover(HoverEventFilter::LeftMouseDown)
            )]
        );
        // The event is left in the state of the LAST walked phase: bubble, ending
        // on the root ancestor. (`current_target` is only meaningful while a
        // callback is running, so this pins the post-walk residue rather than
        // asserting it is reset.)
        assert_eq!(ev.phase, EventPhase::Bubble);
        assert_eq!(ev.current_target, dnid(0, 0));
        assert_eq!(
            ev.target,
            dnid(0, 1),
            "the target itself must never be rewritten"
        );
    }
    #[test]
    fn collect_matching_callbacks_collects_nothing_once_immediate_stop_is_set() {
        let mut result = PropagationResult::default();
        let mut callbacks: BTreeMap<NodeId, Vec<EventFilter>> = BTreeMap::new();
        callbacks.insert(
            NodeId::ZERO,
            vec![EventFilter::Hover(HoverEventFilter::MouseOver)],
        );
        let mut ev = SyntheticEvent::new(
            EventType::MouseOver,
            EventSource::User,
            dnid(0, 0),
            tick(0),
            EventData::None,
        );
        ev.stop_immediate_propagation();
        collect_matching_callbacks(
            &ev,
            NodeId::ZERO,
            EventPhase::Target,
            &callbacks,
            &mut result,
        );
        assert!(result.callbacks_to_invoke.is_empty());
        // A node with no registered callbacks is simply skipped.
        let mut fresh = PropagationResult::default();
        let clean = SyntheticEvent::new(
            EventType::MouseOver,
            EventSource::User,
            dnid(0, 0),
            tick(0),
            EventData::None,
        );
        collect_matching_callbacks(
            &clean,
            NodeId::new(9),
            EventPhase::Target,
            &callbacks,
            &mut fresh,
        );
        assert!(fresh.callbacks_to_invoke.is_empty());
    }
    #[test]
    fn propagate_phase_over_an_empty_iterator_only_sets_the_phase() {
        let mut result = PropagationResult::default();
        let callbacks: BTreeMap<NodeId, Vec<EventFilter>> = BTreeMap::new();
        let mut ev = SyntheticEvent::new(
            EventType::MouseOver,
            EventSource::User,
            dnid(0, 0),
            tick(0),
            EventData::None,
        );
        propagate_phase(
            &mut ev,
            core::iter::empty(),
            EventPhase::Bubble,
            &callbacks,
            &mut result,
        );
        assert_eq!(ev.phase, EventPhase::Bubble);
        assert!(result.callbacks_to_invoke.is_empty());
        // propagate_target_phase resets phase + current_target to the target.
        propagate_target_phase(&mut ev, NodeId::ZERO, &callbacks, &mut result);
        assert_eq!(ev.phase, EventPhase::Target);
        assert_eq!(ev.current_target, ev.target);
    }
    // ================================================================== dedup
    #[test]
    fn deduplicate_synthetic_events_handles_empty_and_single() {
        assert!(deduplicate_synthetic_events(Vec::new()).is_empty());
        let one = vec![SyntheticEvent::new(
            EventType::Scroll,
            EventSource::User,
            dnid(0, 0),
            tick(1),
            EventData::None,
        )];
        assert_eq!(deduplicate_synthetic_events(one).len(), 1);
    }
    #[test]
    fn deduplicate_synthetic_events_keeps_the_latest_timestamp_per_target_and_type() {
        let mk = |node: usize, ty: EventType, t: u64| {
            SyntheticEvent::new(
                ty,
                EventSource::User,
                dnid(0, node),
                tick(t),
                EventData::None,
            )
        };
        // Same (target, type), out-of-order timestamps -> keep the newest.
        let events = vec![
            mk(1, EventType::Scroll, 5),
            mk(1, EventType::Scroll, 99),
            mk(1, EventType::Scroll, 1),
        ];
        let out = deduplicate_synthetic_events(events);
        assert_eq!(out.len(), 1);
        assert_eq!(out[0].timestamp, tick(99), "the newest event must survive");
        // Different node OR different type -> both survive.
        let events = vec![
            mk(1, EventType::Scroll, 1),
            mk(2, EventType::Scroll, 1),
            mk(1, EventType::MouseOver, 1),
        ];
        assert_eq!(deduplicate_synthetic_events(events).len(), 3);
        // Different DOM with the same node index -> distinct targets.
        let a = SyntheticEvent::new(
            EventType::Scroll,
            EventSource::User,
            dnid(0, 1),
            tick(0),
            EventData::None,
        );
        let b = SyntheticEvent::new(
            EventType::Scroll,
            EventSource::User,
            dnid(1, 1),
            tick(0),
            EventData::None,
        );
        assert_eq!(deduplicate_synthetic_events(vec![a, b]).len(), 2);
    }
    #[test]
    fn deduplicate_synthetic_events_collapses_a_large_duplicate_burst() {
        // 10k identical events (e.g. a scroll storm) must collapse to one, and
        // the result must be the newest — no quadratic blowup, no overflow.
        let events: Vec<SyntheticEvent> = (0..10_000u64)
            .map(|t| {
                SyntheticEvent::new(
                    EventType::Scroll,
                    EventSource::User,
                    dnid(0, 0),
                    tick(t),
                    EventData::None,
                )
            })
            .collect();
        let out = deduplicate_synthetic_events(events);
        assert_eq!(out.len(), 1);
        assert_eq!(out[0].timestamp, tick(9_999));
    }
    #[test]
    fn deduplicate_synthetic_events_preserves_unicode_payloads() {
        // Deduplication keys off (target, event_type) only — the payload must
        // survive untouched, including multi-byte / combining / RTL text.
        let text = "🦀 グラフ é\u{0301} مرحبا \u{1F1E6}\u{1F1F9}".repeat(200);
        let ev = SyntheticEvent::new(
            EventType::Input,
            EventSource::User,
            dnid(0, 0),
            tick(1),
            EventData::TextInput(TextInputEventData {
                inserted_text: text.clone(),
                old_text: String::new(),
            }),
        );
        let newer = SyntheticEvent::new(
            EventType::Input,
            EventSource::User,
            dnid(0, 0),
            tick(2),
            EventData::TextInput(TextInputEventData {
                inserted_text: text.clone(),
                old_text: text.clone(),
            }),
        );
        let out = deduplicate_synthetic_events(vec![ev, newer]);
        assert_eq!(out.len(), 1);
        match &out[0].data {
            EventData::TextInput(d) => {
                assert_eq!(d.inserted_text, text);
                assert_eq!(d.old_text, text, "the newer event won");
            }
            _ => panic!("payload must be preserved"),
        }
    }
    // ====================================================== hit-test extraction
    #[test]
    fn get_first_hovered_node_on_empty_input() {
        assert!(get_first_hovered_node(None).is_none());
        assert!(
            get_first_hovered_node(Some(&empty_hit_test())).is_none(),
            "a hit test with no hovered DOMs has no front-most node"
        );
        // A DOM entry that is present but has zero hit nodes is also `None`.
        let ht = hit_test_with(0, &[]);
        assert!(get_first_hovered_node(Some(&ht)).is_none());
    }
    #[test]
    fn get_first_hovered_node_picks_minimum_depth_and_breaks_ties_deterministically() {
        // Front-most (depth 0) has the HIGHER node id — a naive `.next()` on the
        // BTreeMap would wrongly return node 2.
        let ht = hit_test_with(0, &[(2, 5), (5, 0), (9, 3)]);
        let got = get_first_hovered_node(Some(&ht)).unwrap();
        assert_eq!(got.node.into_crate_internal(), Some(NodeId::new(5)));
        // Equal depths: the first in (DomId, NodeId) iteration order wins, and the
        // choice must be stable across calls.
        let ht = hit_test_with(0, &[(7, 2), (3, 2), (11, 2)]);
        let a = get_first_hovered_node(Some(&ht)).unwrap();
        let b = get_first_hovered_node(Some(&ht)).unwrap();
        assert_eq!(a, b, "tie-breaking must be deterministic");
        assert_eq!(a.node.into_crate_internal(), Some(NodeId::new(3)));
        // u32::MAX depth is still a valid (and only) candidate.
        let ht = hit_test_with(0, &[(1, u32::MAX)]);
        let got = get_first_hovered_node(Some(&ht)).unwrap();
        assert_eq!(got.node.into_crate_internal(), Some(NodeId::new(1)));
        assert_eq!(got.dom, DomId { inner: 0 });
    }
    #[test]
    fn get_mouse_position_with_fallback_prefers_the_event_payload() {
        let mouse = MouseState {
            cursor_position: CursorPosition::InWindow(LogicalPosition::new(9.0, 9.0)),
            ..MouseState::default()
        };
        let ev = mouse_event(
            EventType::MouseDown,
            MouseButton::Left,
            LogicalPosition::new(1.0, 2.0),
        );
        assert_eq!(
            get_mouse_position_with_fallback(&ev, &mouse),
            LogicalPosition::new(1.0, 2.0),
            "the event's own payload wins over the live cursor"
        );
        // Non-mouse payload -> fall back to the live cursor...
        let keyless = SyntheticEvent::new(
            EventType::MouseDown,
            EventSource::Synthetic,
            dnid(0, 0),
            tick(0),
            EventData::None,
        );
        assert_eq!(
            get_mouse_position_with_fallback(&keyless, &mouse),
            LogicalPosition::new(9.0, 9.0)
        );
        // ...and if the cursor is Uninitialized or OutOfWindow, fall back to zero
        // (`CursorPosition::get_position` only yields InWindow positions).
        for cursor in [
            CursorPosition::Uninitialized,
            CursorPosition::OutOfWindow(LogicalPosition::new(-5.0, -5.0)),
        ] {
            let ms = MouseState {
                cursor_position: cursor,
                ..MouseState::default()
            };
            assert_eq!(
                get_mouse_position_with_fallback(&keyless, &ms),
                LogicalPosition::zero()
            );
        }
    }
    #[test]
    fn get_mouse_position_with_fallback_passes_through_extreme_coordinates() {
        let mouse = MouseState::default();
        for pos in [
            LogicalPosition::new(f32::NAN, f32::NAN),
            LogicalPosition::new(f32::INFINITY, f32::NEG_INFINITY),
            LogicalPosition::new(f32::MAX, f32::MIN),
            LogicalPosition::new(-0.0, 0.0),
        ] {
            let ev = mouse_event(EventType::MouseDown, MouseButton::Left, pos);
            let got = get_mouse_position_with_fallback(&ev, &mouse);
            // Compare bitwise so NaN == NaN holds: the value must be forwarded
            // verbatim, never sanitized or panicked on.
            assert_eq!(got.x.to_bits(), pos.x.to_bits());
            assert_eq!(got.y.to_bits(), pos.y.to_bits());
        }
    }
    // ============================================= input-interpreter handlers
    #[test]
    fn handle_mouse_down_treats_zero_click_count_as_one() {
        let ht = hit_test_with(0, &[(0, 0)]);
        let mouse = MouseState::default();
        let kb = KeyboardState::default();
        let ev = mouse_event(
            EventType::MouseDown,
            MouseButton::Left,
            LogicalPosition::new(4.0, 5.0),
        );
        // click_count 0 is normalised to 1 -> a plain text-selection click.
        let action = handle_mouse_down(&ev, Some(&ht), 0, &mouse, &kb)
            .expect("click_count 0 must be treated as a single click");
        match action {
            InternalEventAction::AddAndPass(SystemChange::TextSelectionClick {
                position, ..
            }) => {
                assert_eq!(position, LogicalPosition::new(4.0, 5.0));
            }
            _ => panic!("expected a passed-through TextSelectionClick"),
        }
    }
    #[test]
    fn handle_mouse_down_saturates_above_a_triple_click() {
        let ht = hit_test_with(0, &[(0, 0)]);
        let mouse = MouseState::default();
        let kb = KeyboardState::default();
        let ev = mouse_event(
            EventType::MouseDown,
            MouseButton::Left,
            LogicalPosition::zero(),
        );
        // 1..=3 are real clicks.
        for count in 1u8..=3 {
            assert!(
                handle_mouse_down(&ev, Some(&ht), count, &mouse, &kb).is_some(),
                "click_count {count} must produce a selection click"
            );
        }
        // 4 and above (up to the u8 boundary) are dropped — no wraparound, no panic.
        for count in [4u8, 5, 100, u8::MAX] {
            assert!(
                handle_mouse_down(&ev, Some(&ht), count, &mouse, &kb).is_none(),
                "click_count {count} must be ignored"
            );
        }
    }
    #[test]
    fn handle_mouse_down_without_a_hit_test_is_a_no_op() {
        let mouse = MouseState::default();
        let kb = KeyboardState::default();
        let ev = mouse_event(
            EventType::MouseDown,
            MouseButton::Left,
            LogicalPosition::zero(),
        );
        assert!(handle_mouse_down(&ev, None, 1, &mouse, &kb).is_none());
        assert!(handle_mouse_down(&ev, Some(&empty_hit_test()), 1, &mouse, &kb).is_none());
    }
    #[test]
    fn handle_mouse_down_with_primary_held_adds_a_cursor_only_on_a_single_click() {
        let ht = hit_test_with(0, &[(0, 0)]);
        let mouse = MouseState::default();
        let kb = keyboard_with_primary_held();
        let ev = mouse_event(
            EventType::MouseDown,
            MouseButton::Left,
            LogicalPosition::new(7.0, 8.0),
        );
        // primary + single click -> multi-cursor add.
        match handle_mouse_down(&ev, Some(&ht), 1, &mouse, &kb) {
            Some(InternalEventAction::AddAndPass(SystemChange::AddCursorAtClick { position })) => {
                assert_eq!(position, LogicalPosition::new(7.0, 8.0));
            }
            _ => panic!("primary+click must add a cursor at the click position"),
        }
        // primary + double click -> NOT a cursor add (falls back to selection).
        match handle_mouse_down(&ev, Some(&ht), 2, &mouse, &kb) {
            Some(InternalEventAction::AddAndPass(SystemChange::TextSelectionClick { .. })) => {}
            _ => panic!("primary+double-click must not add a cursor"),
        }
    }
    /// Releasing the button must stop the autoscroll timer. Nothing emitted
    /// `StopAutoScrollTimer` at all, so a lost release left a 60Hz timer
    /// running for the life of the window.
    #[test]
    fn releasing_the_button_stops_the_autoscroll_timer() {
        match handle_mouse_up() {
            InternalEventAction::AddAndPass(SystemChange::StopAutoScrollTimer) => {}
            _ => panic!("expected AddAndPass(StopAutoScrollTimer)"),
        }
    }
    #[test]
    fn handle_mouse_over_requires_a_held_button_and_a_drag_origin() {
        let ht = hit_test_with(0, &[(0, 0)]);
        let start = LogicalPosition::new(1.0, 1.0);
        let ev = mouse_event(
            EventType::MouseOver,
            MouseButton::Left,
            LogicalPosition::new(50.0, 60.0),
        );
        // Button up -> never a drag, even with a drag origin.
        let up = MouseState::default();
        assert!(handle_mouse_move(&ev, Some(&ht), &up, Some(start)).is_none());
        // Button down but no drag origin -> not a drag either.
        let down = MouseState {
            left_down: true,
            ..MouseState::default()
        };
        assert!(handle_mouse_move(&ev, Some(&ht), &down, None).is_none());
        // Button down + origin but nothing under the cursor -> STILL a drag:
        // reaching past the text (into padding, past the last line) is how a
        // selection gets extended, and the endpoint resolves against the
        // anchor block, not against whatever is under the pointer.
        assert!(handle_mouse_move(&ev, None, &down, Some(start)).is_some());
        assert!(handle_mouse_move(&ev, Some(&empty_hit_test()), &down, Some(start)).is_some());
        // All three present -> a drag selection from origin to the current point.
        match handle_mouse_move(&ev, Some(&ht), &down, Some(start)) {
            Some(InternalEventAction::AddAndPass(SystemChange::TextSelectionDrag {
                start_position,
                current_position,
            })) => {
                assert_eq!(start_position, start);
                assert_eq!(current_position, LogicalPosition::new(50.0, 60.0));
            }
            _ => panic!("expected a TextSelectionDrag"),
        }
    }
    #[test]
    fn handle_key_down_needs_a_focused_node_and_a_keyboard_payload() {
        let kb = KeyboardState::default();
        let ev = key_event(VirtualKeyCode::Back as u32, KeyModifiers::default());
        assert!(
            handle_key_down(&ev, &kb, None, true).is_none(),
            "no focus => no keyboard system change"
        );
        // Focused, but the event carries no keyboard payload.
        let payloadless = SyntheticEvent::new(
            EventType::KeyDown,
            EventSource::User,
            dnid(0, 1),
            tick(0),
            EventData::None,
        );
        assert!(handle_key_down(&payloadless, &kb, Some(dnid(0, 1)), true).is_none());
    }
    #[test]
    fn handle_key_down_rejects_undecodable_key_codes() {
        let kb = KeyboardState::default();
        let target = Some(dnid(0, 1));
        // u32::MAX / out-of-table codes must fall out via `from_u32` -> None,
        // never index a table or panic.
        for code in [u32::MAX, u32::MAX - 1, 100_000, 9_999] {
            let ev = key_event(code, KeyModifiers::default());
            assert!(
                handle_key_down(&ev, &kb, target, true).is_none(),
                "key_code {code} must decode to None"
            );
        }
    }
    #[test]
    fn handle_key_down_reads_modifiers_from_the_event_not_the_live_keyboard() {
        // The live KeyboardState is deliberately EMPTY here: the handler must key
        // off the event payload's modifiers (the live state may have advanced
        // between queueing and dispatch).
        let kb = KeyboardState::default();
        let target = dnid(0, 1);
        let ev = key_event(VirtualKeyCode::C as u32, primary_modifiers());
        match handle_key_down(&ev, &kb, Some(target), true) {
            Some(InternalEventAction::AddAndSkip(SystemChange::CopyToClipboard)) => {}
            _ => panic!("primary+C in the payload must copy, regardless of the live state"),
        }
        // ...and conversely, a live primary key must NOT rewrite an unmodified event.
        let live = keyboard_with_primary_held();
        let plain = key_event(VirtualKeyCode::C as u32, KeyModifiers::default());
        assert!(
            handle_key_down(&plain, &live, Some(target), true).is_none(),
            "an unmodified C is plain text input, not a copy"
        );
    }
    #[test]
    fn handle_key_down_maps_backspace_and_delete_to_selection_ops() {
        let kb = KeyboardState::default();
        let target = dnid(0, 1);
        let expect_op = |ev: &SyntheticEvent| -> SelectionOp {
            match handle_key_down(ev, &kb, Some(target), true) {
                Some(InternalEventAction::AddAndSkip(SystemChange::ApplySelectionOp {
                    target: t,
                    op,
                    ..
                })) => {
                    assert_eq!(t, target);
                    op
                }
                _ => panic!("expected an ApplySelectionOp"),
            }
        };
        let back = expect_op(&key_event(
            VirtualKeyCode::Back as u32,
            KeyModifiers::default(),
        ));
        assert_eq!(back.direction, SelectionDirection::Backward);
        assert_eq!(back.step, SelectionStep::Character);
        assert_eq!(back.mode, SelectionMode::Delete);
        let del = expect_op(&key_event(
            VirtualKeyCode::Delete as u32,
            KeyModifiers::default(),
        ));
        assert_eq!(del.direction, SelectionDirection::Forward);
        assert_eq!(del.step, SelectionStep::Character);
        assert_eq!(del.mode, SelectionMode::Delete);
        // Shift+arrow extends instead of moving.
        let shift_right = expect_op(&key_event(
            VirtualKeyCode::Right as u32,
            KeyModifiers::new().with_shift(),
        ));
        assert_eq!(shift_right.mode, SelectionMode::Extend);
        assert_eq!(shift_right.step, SelectionStep::Character);
        // The word modifier upgrades Backspace to a word delete.
        let word_mod = if cfg!(target_os = "macos") {
            KeyModifiers::new().with_alt()
        } else {
            KeyModifiers::new().with_ctrl()
        };
        let word_back = expect_op(&key_event(VirtualKeyCode::Back as u32, word_mod));
        assert_eq!(word_back.step, SelectionStep::Word);
        assert_eq!(word_back.mode, SelectionMode::Delete);
    }
    #[test]
    fn handle_key_down_ignores_keys_it_does_not_interpret() {
        let kb = KeyboardState::default();
        let target = Some(dnid(0, 1));
        // Ordinary text keys must pass through to the user callbacks untouched.
        for vk in [
            VirtualKeyCode::B,
            VirtualKeyCode::Q,
            VirtualKeyCode::Space,
            VirtualKeyCode::F5,
        ] {
            let ev = key_event(vk as u32, KeyModifiers::default());
            assert!(
                handle_key_down(&ev, &kb, target, true).is_none(),
                "{vk:?} must not generate a system change"
            );
        }
    }
    // ================================================ default_input_interpreter
    #[test]
    fn default_input_interpreter_with_no_events_produces_nothing() {
        let kb = KeyboardState::default();
        let mouse = MouseState::default();
        let info = InputInterpreterInfo {
            seat_focus: &[],
            events: &[],
            hit_test: None,
            keyboard_state: &kb,
            mouse_state: &mouse,
            state: InputInterpreterState {
                focused_node: None,
                click_count: 0,
                drag_start_position: None,
                has_selection: false,
                focus_is_editable: true,
            },
        };
        let r = default_input_interpreter(&info);
        assert!(r.system_changes.is_empty());
        assert!(r.user_events.is_empty());
    }
    #[test]
    fn default_input_interpreter_skips_shortcut_events_but_passes_clicks_through() {
        let kb = KeyboardState::default();
        let mouse = MouseState::default();
        let ht = hit_test_with(0, &[(0, 0)]);
        let target = dnid(0, 1);
        // A primary+C shortcut is consumed (AddAndSkip) — the user callback must
        // NOT also see the raw key event...
        let copy = key_event(VirtualKeyCode::C as u32, primary_modifiers());
        // ...while a MouseDown is consumed AND forwarded (AddAndPass).
        let click = mouse_event(
            EventType::MouseDown,
            MouseButton::Left,
            LogicalPosition::zero(),
        );
        // ...and an unhandled event type is forwarded untouched.
        let scroll = SyntheticEvent::new(
            EventType::Scroll,
            EventSource::User,
            target,
            tick(0),
            EventData::None,
        );
        let events = vec![copy, click, scroll];
        let info = InputInterpreterInfo {
            seat_focus: &[],
            events: &events,
            hit_test: Some(&ht),
            keyboard_state: &kb,
            mouse_state: &mouse,
            state: InputInterpreterState {
                focused_node: Some(target),
                click_count: 1,
                drag_start_position: None,
                has_selection: false,
                focus_is_editable: true,
            },
        };
        let r = default_input_interpreter(&info);
        assert_eq!(r.system_changes.len(), 2, "copy + selection click");
        assert!(r.system_changes.contains(&SystemChange::CopyToClipboard));
        assert!(r
            .system_changes
            .iter()
            .any(|c| matches!(c, SystemChange::TextSelectionClick { .. })));
        assert_eq!(
            r.user_events.len(),
            2,
            "the consumed KeyDown must not be forwarded"
        );
        assert!(!r
            .user_events
            .iter()
            .any(|e| e.event_type == EventType::KeyDown));
        assert!(r
            .user_events
            .iter()
            .any(|e| e.event_type == EventType::MouseDown));
        assert!(r
            .user_events
            .iter()
            .any(|e| e.event_type == EventType::Scroll));
    }
    #[test]
    fn default_input_interpreter_extern_survives_a_null_info_pointer() {
        // The C-ABI trampoline must null-check rather than deref garbage.
        let user_data = crate::refany::RefAny::new(0u8);
        let r = default_input_interpreter_extern(user_data, core::ptr::null());
        assert!(r.system_changes.is_empty());
        assert!(r.user_events.is_empty());
    }
    // ==================================================== post-callback filter
    #[test]
    fn post_filter_with_prevent_default_only_lets_focus_changes_through() {
        let old = Some(dnid(0, 1));
        let new = Some(dnid(0, 2));
        let pre = vec![
            SystemChange::TextSelectionClick {
                position: LogicalPosition::zero(),
                timestamp: tick(0),
            },
            SystemChange::PasteFromClipboard,
        ];
        // prevent_default + no focus change -> absolutely nothing (not even the
        // usual ApplyPendingTextInput).
        let out = default_post_filter(true, &pre, old, old);
        assert!(
            out.is_empty(),
            "preventDefault must suppress every side effect"
        );
        // prevent_default + a focus change -> ONLY the focus change.
        let out = default_post_filter(true, &pre, old, new);
        assert_eq!(
            out,
            vec![SystemChange::SetFocus {
                new_focus: new,
                old_focus: old,
                visible: false
            }]
        );
    }
    #[test]
    fn post_filter_maps_pre_changes_to_their_follow_ups() {
        // No pre-changes, no focus change -> just the text-input flush.
        let out = default_post_filter(false, &[], None, None);
        assert_eq!(out, vec![SystemChange::ApplyPendingTextInput]);
        // Cursor-moving ops schedule a scroll-into-view.
        for change in [
            SystemChange::TextSelectionClick {
                position: LogicalPosition::zero(),
                timestamp: tick(0),
            },
            SystemChange::ApplySelectionOp {
                target: dnid(0, 1),
                seat_id: crate::window::PRIMARY_POINTER_SEAT,
                op: SelectionOp::new(
                    SelectionDirection::Forward,
                    SelectionStep::Character,
                    SelectionMode::Move,
                ),
            },
            SystemChange::AddCursorAtClick {
                position: LogicalPosition::zero(),
            },
            SystemChange::SelectNextOccurrence { target: dnid(0, 1) },
            SystemChange::CutToClipboard { target: dnid(0, 1) },
            SystemChange::PasteFromClipboard,
            SystemChange::UndoTextEdit { target: dnid(0, 1) },
            SystemChange::RedoTextEdit { target: dnid(0, 1) },
            SystemChange::SelectAllText,
        ] {
            let out = default_post_filter(false, core::slice::from_ref(&change), None, None);
            assert!(
                out.contains(&SystemChange::ScrollSelectionIntoView),
                "{change:?} must schedule a scroll-into-view"
            );
            assert_eq!(out[0], SystemChange::ApplyPendingTextInput);
        }
        // A drag starts the auto-scroll timer instead.
        let drag = SystemChange::TextSelectionDrag {
            start_position: LogicalPosition::zero(),
            current_position: LogicalPosition::new(1.0, 1.0),
        };
        let out = default_post_filter(false, core::slice::from_ref(&drag), None, None);
        assert!(out.contains(&SystemChange::StartAutoScrollTimer));
        assert!(!out.contains(&SystemChange::ScrollSelectionIntoView));
        // Changes with no follow-up add nothing beyond the text-input flush.
        let out = default_post_filter(false, &[SystemChange::CopyToClipboard], None, None);
        assert_eq!(out, vec![SystemChange::ApplyPendingTextInput]);
    }
    #[test]
    fn post_filter_emits_set_focus_only_when_focus_actually_moved() {
        let a = Some(dnid(0, 1));
        let b = Some(dnid(0, 2));
        // Unchanged (both Some, both None) -> no SetFocus.
        for (old, new) in [(a, a), (None, None)] {
            let out = default_post_filter(false, &[], old, new);
            assert!(!out
                .iter()
                .any(|c| matches!(c, SystemChange::SetFocus { .. })));
        }
        // Changed (including to/from None) -> exactly one SetFocus, and it is last.
        for (old, new) in [(a, b), (None, a), (a, None)] {
            let out = default_post_filter(false, &[], old, new);
            assert_eq!(
                out.last(),
                Some(&SystemChange::SetFocus {
                    new_focus: new,
                    old_focus: old,
                    visible: false
                })
            );
            assert_eq!(
                out.iter()
                    .filter(|c| matches!(c, SystemChange::SetFocus { .. }))
                    .count(),
                1
            );
        }
    }
    #[test]
    fn post_filter_handles_a_large_pre_change_list_without_blowing_up() {
        // 5000 cursor ops -> 1 flush + 5000 scroll-into-views. Bounded, no overflow.
        let pre: Vec<SystemChange> = (0..5000)
            .map(|_| SystemChange::AddCursorAtClick {
                position: LogicalPosition::zero(),
            })
            .collect();
        let out = default_post_filter(false, &pre, None, None);
        assert_eq!(out.len(), 5001);
        assert_eq!(out[0], SystemChange::ApplyPendingTextInput);
        assert!(out[1..]
            .iter()
            .all(|c| *c == SystemChange::ScrollSelectionIntoView));
    }
    #[test]
    fn default_post_filter_delegates_to_post_callback_filter_system_changes() {
        let pre = vec![
            SystemChange::TextSelectionDrag {
                start_position: LogicalPosition::zero(),
                current_position: LogicalPosition::new(2.0, 2.0),
            },
            SystemChange::SelectAllText,
        ];
        for prevent in [false, true] {
            for (old, new) in [(None, None), (Some(dnid(0, 1)), Some(dnid(0, 2)))] {
                assert_eq!(
                    default_post_filter(prevent, &pre, old, new),
                    post_callback_filter_system_changes(prevent, &pre, old, new),
                    "the two entry points must stay in lock-step"
                );
            }
        }
    }
    /// The default schema must be an empty op LIST, not null and not a string.
    ///
    /// A plugin deciding whether a host is usable has to distinguish "this app
    /// advertises no ops" from "this app returned nothing parseable". Those
    /// are different answers and only one of them means "move on".
    #[test]
    fn default_op_schema_is_an_empty_list_not_a_null() {
        let cb = CustomE2eOpCallback::default();
        assert!(cb.op_schema.is_object(), "schema must be an object");
        assert!(!cb.op_schema.is_null());
        let text = cb.op_schema.internal.string_value.as_str();
        assert!(text.contains("\"ops\""), "got {text}");
        // Positive control: a NON-empty schema must serialize its contents,
        // so this test cannot pass by everything being empty.
        let schema = E2eOpSchema {
            ops: alloc::vec![E2eOpDef {
                name: "load_document".to_string(),
                summary: "Open a file".to_string(),
                description: "Loads a markdown file into the editor.".to_string(),
                args: alloc::vec![E2eOpArg {
                    name: "path".to_string(),
                    arg_type: E2eOpArgType::String,
                    required: true,
                    description: "Absolute path.".to_string(),
                }],
                examples: alloc::vec![E2eOpExample {
                    description: "Open big.md".to_string(),
                    args: crate::json::Json::parse(r#"{"path":"/tmp/big.md"}"#).unwrap(),
                    returns: crate::json::Json::parse(r#"{"success":true,"pages":40}"#).unwrap(),
                }],
            }],
        };
        let j = schema.to_json();
        let t = j.internal.string_value.as_str();
        for needle in [
            "load_document",
            "Open a file",
            "\"type\":\"string\"",
            "big.md",
            "pages",
        ] {
            assert!(t.contains(needle), "missing {needle} in {t}");
        }
    }
    fn sample_op(returns: &str) -> E2eOpDef {
        E2eOpDef {
            name: "load_document".to_string(),
            summary: "Open a markdown file".to_string(),
            description: "Reads and paginates a file.".to_string(),
            args: alloc::vec![E2eOpArg {
                name: "path".to_string(),
                arg_type: E2eOpArgType::String,
                required: true,
                description: "Absolute path.".to_string(),
            }],
            examples: alloc::vec![E2eOpExample {
                description: "Open big.md".to_string(),
                args: crate::json::Json::parse(r#"{"path":"/tmp/big.md"}"#).unwrap(),
                returns: crate::json::Json::parse(returns).unwrap(),
            }],
        }
    }
    /// `success` is REQUIRED, and checked when the schema is installed.
    #[test]
    fn schema_validation_requires_a_success_boolean_in_every_example() {
        let ok = E2eOpSchema {
            ops: alloc::vec![sample_op(r#"{"success":true,"pages":40}"#)],
        };
        assert_eq!(ok.validate(), Ok(()));
        // NEGATIVE CONTROL: drop `success` and validation must reject.
        let missing = E2eOpSchema {
            ops: alloc::vec![sample_op(r#"{"pages":40}"#)],
        };
        assert!(matches!(
            missing.validate(),
            Err(E2eSchemaError::ExampleMissingSuccess { .. })
        ));
        // Present but not a BOOLEAN is still a reject — `"success":"yes"` is
        // what a hand-written schema actually produces and tells a consumer
        // nothing.
        let stringy = E2eOpSchema {
            ops: alloc::vec![sample_op(r#"{"success":"yes"}"#)],
        };
        assert!(stringy.validate().is_err());
        let dupe = E2eOpSchema {
            ops: alloc::vec![
                sample_op(r#"{"success":true}"#),
                sample_op(r#"{"success":true}"#),
            ],
        };
        assert!(matches!(
            dupe.validate(),
            Err(E2eSchemaError::DuplicateOpName { .. })
        ));
    }
    /// Identity fields lead, and examples are NESTED JSON not escaped strings.
    #[test]
    fn schema_json_keeps_declaration_order_and_nests_examples() {
        let schema = E2eOpSchema {
            ops: alloc::vec![sample_op(r#"{"success":true,"pages":40}"#)],
        };
        let text = schema.to_json().internal.string_value.as_str().to_string();
        let name_at = text.find("\"name\"").expect("name present");
        let args_at = text.find("\"args\"").expect("args present");
        assert!(name_at < args_at, "identity fields must lead: {text}");
        // Nested, so NO backslash-escaped quotes anywhere in the payload.
        assert!(
            !text.contains("\\\""),
            "examples must nest, not escape: {text}"
        );
        assert!(text.contains("\"success\":true"), "{text}");
    }
    #[test]
    fn default_custom_op_handler_recognises_nothing() {
        // handled=false is the load-bearing default: an app with no handler
        // must make a scenario naming a custom op FAIL, not pass quietly.
        let r = default_custom_e2e_op_extern(
            crate::refany::RefAny::new(0u8),
            AzString::from_const_str("anything"),
            AzString::from_const_str("{}"),
        );
        assert!(!r.handled);
    }
    fn default_post_filter_extern_decodes_the_none_focus_sentinel() {
        // old_focus = the `None` sentinel, new_focus = a real node => a focus change.
        let pre: Vec<SystemChange> = Vec::new();
        let slice = SystemChangeVecSlice {
            ptr: pre.as_ptr(),
            len: pre.len(),
        };
        let out = default_post_filter_extern(
            crate::refany::RefAny::new(0u8),
            false,
            slice,
            dnid_none(0),
            dnid(0, 4),
        );
        let changes = out.as_slice();
        assert_eq!(changes.first(), Some(&SystemChange::ApplyPendingTextInput));
        assert_eq!(
            changes.last(),
            Some(&SystemChange::SetFocus {
                new_focus: Some(dnid(0, 4)),
                old_focus: None,
                visible: false,
            }),
            "a `NONE` node id must decode to `None`, not to node 0"
        );
        // An empty C-slice must be accepted (ptr may be dangling-but-aligned).
        let out = default_post_filter_extern(
            crate::refany::RefAny::new(0u8),
            true,
            SystemChangeVecSlice::empty(),
            dnid_none(0),
            dnid_none(0),
        );
        assert!(out.as_slice().is_empty());
    }
}
/// KEYBOARD ACTIVATION: Enter / Space on a focused element dispatch a
/// synthetic `EventType::Click`, but every widget in the toolkit registers
/// `HoverEventFilter::MouseUp` (the pointer spelling of "activate"). Without a
/// Click -> MouseUp arm the synthetic event matched NOTHING, so Enter and
/// Space silently did nothing on every focusable widget - the device report
/// of 2026-08-31 ("space did nothing when the item was focused").
#[test]
1
fn a_synthetic_click_activates_a_click_listener_and_nothing_else() {
    use crate::{
        dom::{DomId, DomNodeId},
        events::{
            EventData, EventFilter, EventPhase, EventSource, EventType, HoverEventFilter,
            SyntheticEvent,
        },
        styled_dom::NodeHierarchyItemId,
        task::{Instant, SystemTick},
    };
1
    let target = DomNodeId {
1
        dom: DomId::ROOT_ID,
1
        node: NodeHierarchyItemId::from_crate_internal(Some(crate::dom::NodeId::new(1))),
1
    };
1
    let click = SyntheticEvent::new(
1
        EventType::Click,
1
        EventSource::User,
1
        target,
1
        Instant::Tick(SystemTick::new(0)),
1
        EventData::None,
    );
1
    assert!(
1
        super::matches_filter_phase(
1
            EventFilter::Hover(HoverEventFilter::Click),
1
            &click,
1
            EventPhase::Bubble
        ),
        "a keyboard-activation Click must reach a Click listener",
    );
    // ...and must NOT reach a raw-release listener, or every activation runs
    // twice for a real pointer click (which emits MouseUp AND Click).
2
    for filter in [HoverEventFilter::MouseUp, HoverEventFilter::LeftMouseUp] {
2
        assert!(
2
            !super::matches_filter_phase(EventFilter::Hover(filter), &click, EventPhase::Bubble),
            "a Click must not also fire {filter:?}",
        );
    }
    // It must NOT masquerade as anything else - a Click is an activation, not
    // a press, and a real pointer click still arrives as MouseDown + MouseUp.
1
    assert!(
1
        !super::matches_filter_phase(
1
            EventFilter::Hover(HoverEventFilter::MouseDown),
1
            &click,
1
            EventPhase::Bubble
1
        ),
        "a Click must not fire MouseDown listeners",
    );
1
}
/// ARCHITECTURAL INVARIANT: the engine keeps TWO tables that map an event
/// onto listeners, and nothing but discipline kept them in agreement.
///
///  * `event_type_to_filters` drives dispatch PLANNING - which callbacks get collected for an
///    event.
///  * `matches_filter_phase` drives phase MATCHING - whether a collected callback actually fires.
///
/// A de-sync is SILENT: the event simply never reaches the callback. That is
/// exactly how Enter/Space died on every focusable widget - planning listed
/// only `LeftMouseUp` for `EventType::Click` while every widget registers the
/// generic `MouseUp`, so activation collected nothing, and separately the
/// matcher had no `Click` arm at all.
///
/// This test makes the invariant mechanical instead of a comment: for every
/// pointer-ish event, the two tables must agree on every hover filter.
#[test]
1
fn dispatch_planning_and_phase_matching_agree_on_every_hover_filter() {
    use crate::{
        dom::{DomId, DomNodeId, NodeId},
        events::{
            event_type_to_filters, EventData, EventFilter, EventPhase, EventSource, EventType,
            HoverEventFilter, SyntheticEvent,
        },
        styled_dom::NodeHierarchyItemId,
        task::{Instant, SystemTick},
    };
    const FILTERS: &[HoverEventFilter] = &[
        HoverEventFilter::MouseOver,
        HoverEventFilter::MouseDown,
        HoverEventFilter::LeftMouseDown,
        HoverEventFilter::RightMouseDown,
        HoverEventFilter::MiddleMouseDown,
        HoverEventFilter::MouseUp,
        HoverEventFilter::LeftMouseUp,
        HoverEventFilter::RightMouseUp,
        HoverEventFilter::MiddleMouseUp,
        HoverEventFilter::MouseEnter,
        HoverEventFilter::MouseLeave,
        HoverEventFilter::Scroll,
    ];
    const EVENTS: &[EventType] = &[
        EventType::Click,
        EventType::MouseUp,
        EventType::MouseDown,
        EventType::MouseOver,
        EventType::MouseEnter,
        EventType::MouseLeave,
        EventType::Scroll,
    ];
1
    let target = DomNodeId {
1
        dom: DomId::ROOT_ID,
1
        node: NodeHierarchyItemId::from_crate_internal(Some(NodeId::new(1))),
1
    };
8
    for &event_type in EVENTS {
7
        let event = SyntheticEvent::new(
7
            event_type,
7
            EventSource::User,
7
            target,
7
            Instant::Tick(SystemTick::new(0)),
7
            EventData::None,
        );
7
        let planned = event_type_to_filters(event_type, &event.data);
91
        for &f in FILTERS {
84
            let filter = EventFilter::Hover(f);
84
            let is_planned = planned.contains(&filter);
84
            let does_match = super::matches_filter_phase(filter, &event, EventPhase::Bubble);
84
            assert_eq!(
                is_planned,
                does_match,
                "de-sync for {event_type:?} x {f:?}: planning says {is_planned}, matching says \
                 {does_match} - a callback registered on {f:?} would {} (see the doc on this test)",
                if is_planned {
                    "be collected and then ignored"
                } else {
                    "never be collected"
                },
            );
        }
    }
1
}
/// EXHAUSTIVE INVARIANT over the WHOLE event pipeline.
///
/// The engine keeps two tables that map an event onto listeners and nothing
/// but discipline kept them in agreement:
///
///  * `event_type_to_filters` drives dispatch PLANNING - which callbacks get collected for an
///    event.
///  * `matches_filter_phase` drives phase MATCHING - whether a collected callback actually fires.
///
/// A de-sync is SILENT in both directions: a filter planned but not matched
/// collects a callback and drops it; a filter matched but not planned can
/// never fire at all. That is how Enter/Space died on every widget, and it is
/// how a real pointer click briefly fired activation handlers TWICE (the
/// ColorInput opened its picker and instantly closed it again).
///
/// The narrow version of this test covered pointer events only. This one
/// covers EVERY unit event type against EVERY filter of all FOUR kinds, so
/// the next such bug fails here instead of on a device. Four, not three: the
/// derivation shipped probing Hover/Focus/Window only, and this test - which
/// then knew the same three families - stayed green while every lifecycle
/// callback in the engine was dead (`Mount` planned no filter at all). A
/// family this test does not enumerate is a family the derivation can forget.
#[test]
1
fn planning_and_matching_agree_for_every_event_and_filter() {
    use crate::{
        dom::{DomId, DomNodeId, NodeId},
        events::{
            event_type_to_filters, ComponentEventFilter, EventData, EventFilter, EventPhase,
            EventSource, EventType, FocusEventFilter, HoverEventFilter, SyntheticEvent,
            WindowEventFilter,
        },
        styled_dom::NodeHierarchyItemId,
        task::{Instant, SystemTick},
    };
    const HOVER: &[HoverEventFilter] = &[
        HoverEventFilter::MouseOver,
        HoverEventFilter::MouseDown,
        HoverEventFilter::LeftMouseDown,
        HoverEventFilter::RightMouseDown,
        HoverEventFilter::MiddleMouseDown,
        HoverEventFilter::Click,
        HoverEventFilter::MouseUp,
        HoverEventFilter::LeftMouseUp,
        HoverEventFilter::RightMouseUp,
        HoverEventFilter::MiddleMouseUp,
        HoverEventFilter::MouseEnter,
        HoverEventFilter::MouseLeave,
        HoverEventFilter::Scroll,
        HoverEventFilter::ScrollStart,
        HoverEventFilter::ScrollEnd,
        HoverEventFilter::TextInput,
        HoverEventFilter::VirtualKeyDown,
        HoverEventFilter::VirtualKeyUp,
        HoverEventFilter::HoveredFile,
        HoverEventFilter::DroppedFile,
        HoverEventFilter::HoveredFileCancelled,
        HoverEventFilter::TouchStart,
        HoverEventFilter::TouchMove,
        HoverEventFilter::TouchEnd,
        HoverEventFilter::TouchCancel,
        HoverEventFilter::PenDown,
        HoverEventFilter::PenMove,
        HoverEventFilter::PenUp,
        HoverEventFilter::PenEnter,
        HoverEventFilter::PenLeave,
        HoverEventFilter::PenSqueeze,
        HoverEventFilter::PenDoubleTap,
        HoverEventFilter::PenHover,
        HoverEventFilter::GeolocationFix,
        HoverEventFilter::GeolocationError,
        HoverEventFilter::SensorChanged,
        HoverEventFilter::GamepadInput,
        HoverEventFilter::DragStart,
        HoverEventFilter::Drag,
        HoverEventFilter::DragEnd,
        HoverEventFilter::DragEnter,
        HoverEventFilter::DragOver,
        HoverEventFilter::DragLeave,
        HoverEventFilter::Drop,
        HoverEventFilter::DoubleClick,
        HoverEventFilter::LongPress,
        HoverEventFilter::SwipeLeft,
        HoverEventFilter::SwipeRight,
        HoverEventFilter::SwipeUp,
        HoverEventFilter::SwipeDown,
        HoverEventFilter::PinchIn,
        HoverEventFilter::PinchOut,
        HoverEventFilter::RotateClockwise,
        HoverEventFilter::RotateCounterClockwise,
        HoverEventFilter::MouseOut,
        HoverEventFilter::FocusIn,
        HoverEventFilter::FocusOut,
        HoverEventFilter::CompositionStart,
        HoverEventFilter::CompositionUpdate,
        HoverEventFilter::CompositionEnd,
        HoverEventFilter::SystemTextSingleClick,
        HoverEventFilter::SystemTextDoubleClick,
        HoverEventFilter::SystemTextTripleClick,
        HoverEventFilter::PermissionChanged,
        HoverEventFilter::BiometricResult,
        HoverEventFilter::ScreenColorPicked,
        HoverEventFilter::KeyringResult,
    ];
    const FOCUS: &[FocusEventFilter] = &[
        FocusEventFilter::MouseOver,
        FocusEventFilter::MouseDown,
        FocusEventFilter::LeftMouseDown,
        FocusEventFilter::RightMouseDown,
        FocusEventFilter::MiddleMouseDown,
        FocusEventFilter::MouseUp,
        FocusEventFilter::LeftMouseUp,
        FocusEventFilter::RightMouseUp,
        FocusEventFilter::MiddleMouseUp,
        FocusEventFilter::MouseEnter,
        FocusEventFilter::MouseLeave,
        FocusEventFilter::Scroll,
        FocusEventFilter::ScrollStart,
        FocusEventFilter::ScrollEnd,
        FocusEventFilter::TextInput,
        FocusEventFilter::VirtualKeyDown,
        FocusEventFilter::VirtualKeyUp,
        FocusEventFilter::FocusReceived,
        FocusEventFilter::FocusLost,
        FocusEventFilter::PenDown,
        FocusEventFilter::PenMove,
        FocusEventFilter::PenUp,
        FocusEventFilter::DragStart,
        FocusEventFilter::Drag,
        FocusEventFilter::DragEnd,
        FocusEventFilter::DragEnter,
        FocusEventFilter::DragOver,
        FocusEventFilter::DragLeave,
        FocusEventFilter::Drop,
        FocusEventFilter::DoubleClick,
        FocusEventFilter::LongPress,
        FocusEventFilter::SwipeLeft,
        FocusEventFilter::SwipeRight,
        FocusEventFilter::SwipeUp,
        FocusEventFilter::SwipeDown,
        FocusEventFilter::PinchIn,
        FocusEventFilter::PinchOut,
        FocusEventFilter::RotateClockwise,
        FocusEventFilter::RotateCounterClockwise,
        FocusEventFilter::FocusIn,
        FocusEventFilter::FocusOut,
        FocusEventFilter::CompositionStart,
        FocusEventFilter::CompositionUpdate,
        FocusEventFilter::CompositionEnd,
        FocusEventFilter::Copy,
        FocusEventFilter::Cut,
        FocusEventFilter::Paste,
        FocusEventFilter::DocumentEdit,
        FocusEventFilter::TextChanged,
    ];
    const WINDOW: &[WindowEventFilter] = &[
        WindowEventFilter::MouseOver,
        WindowEventFilter::MouseDown,
        WindowEventFilter::LeftMouseDown,
        WindowEventFilter::RightMouseDown,
        WindowEventFilter::MiddleMouseDown,
        WindowEventFilter::MouseUp,
        WindowEventFilter::LeftMouseUp,
        WindowEventFilter::RightMouseUp,
        WindowEventFilter::MiddleMouseUp,
        WindowEventFilter::MouseEnter,
        WindowEventFilter::MouseLeave,
        WindowEventFilter::Scroll,
        WindowEventFilter::ScrollStart,
        WindowEventFilter::ScrollEnd,
        WindowEventFilter::TextInput,
        WindowEventFilter::VirtualKeyDown,
        WindowEventFilter::VirtualKeyUp,
        WindowEventFilter::HoveredFile,
        WindowEventFilter::DroppedFile,
        WindowEventFilter::HoveredFileCancelled,
        WindowEventFilter::Resized,
        WindowEventFilter::Moved,
        WindowEventFilter::FrameChanged,
        WindowEventFilter::TouchStart,
        WindowEventFilter::TouchMove,
        WindowEventFilter::TouchEnd,
        WindowEventFilter::TouchCancel,
        WindowEventFilter::FocusReceived,
        WindowEventFilter::FocusLost,
        WindowEventFilter::CloseRequested,
        WindowEventFilter::ThemeChanged,
        WindowEventFilter::WindowFocusReceived,
        WindowEventFilter::WindowFocusLost,
        WindowEventFilter::PenDown,
        WindowEventFilter::PenMove,
        WindowEventFilter::PenUp,
        WindowEventFilter::PenEnter,
        WindowEventFilter::PenLeave,
        WindowEventFilter::PenSqueeze,
        WindowEventFilter::PenDoubleTap,
        WindowEventFilter::PenHover,
        WindowEventFilter::GeolocationFix,
        WindowEventFilter::GeolocationError,
        WindowEventFilter::SensorChanged,
        WindowEventFilter::GamepadInput,
        WindowEventFilter::DragStart,
        WindowEventFilter::Drag,
        WindowEventFilter::DragEnd,
        WindowEventFilter::DragEnter,
        WindowEventFilter::DragOver,
        WindowEventFilter::DragLeave,
        WindowEventFilter::Drop,
        WindowEventFilter::DoubleClick,
        WindowEventFilter::LongPress,
        WindowEventFilter::SwipeLeft,
        WindowEventFilter::SwipeRight,
        WindowEventFilter::SwipeUp,
        WindowEventFilter::SwipeDown,
        WindowEventFilter::PinchIn,
        WindowEventFilter::PinchOut,
        WindowEventFilter::RotateClockwise,
        WindowEventFilter::RotateCounterClockwise,
        WindowEventFilter::DpiChanged,
        WindowEventFilter::MonitorChanged,
        WindowEventFilter::PermissionChanged,
        WindowEventFilter::BiometricResult,
        WindowEventFilter::ScreenColorPicked,
        WindowEventFilter::KeyringResult,
    ];
    const COMPONENT: &[ComponentEventFilter] = &[
        ComponentEventFilter::AfterMount,
        ComponentEventFilter::BeforeUnmount,
        ComponentEventFilter::NodeResized,
        ComponentEventFilter::DefaultAction,
        ComponentEventFilter::Selected,
        ComponentEventFilter::Updated,
        ComponentEventFilter::Dismissed,
        ComponentEventFilter::TornOff,
        ComponentEventFilter::Docked,
    ];
    const EVENTS: &[EventType] = &[
        EventType::MouseOver,
        EventType::MouseEnter,
        EventType::MouseLeave,
        EventType::MouseOut,
        EventType::MouseDown,
        EventType::MouseUp,
        EventType::Click,
        EventType::DoubleClick,
        EventType::ContextMenu,
        EventType::KeyDown,
        EventType::KeyUp,
        EventType::KeyPress,
        EventType::CompositionStart,
        EventType::CompositionUpdate,
        EventType::CompositionEnd,
        EventType::Focus,
        EventType::Blur,
        EventType::FocusIn,
        EventType::FocusOut,
        EventType::Input,
        EventType::Change,
        EventType::Submit,
        EventType::Reset,
        EventType::Invalid,
        EventType::Scroll,
        EventType::ScrollStart,
        EventType::ScrollEnd,
        EventType::DragStart,
        EventType::Drag,
        EventType::DragEnd,
        EventType::DragEnter,
        EventType::DragOver,
        EventType::DragLeave,
        EventType::Drop,
        EventType::TouchStart,
        EventType::TouchMove,
        EventType::TouchEnd,
        EventType::TouchCancel,
        EventType::PenDown,
        EventType::PenMove,
        EventType::PenUp,
        EventType::PenEnter,
        EventType::PenLeave,
        EventType::LongPress,
        EventType::SwipeLeft,
        EventType::SwipeRight,
        EventType::SwipeUp,
        EventType::SwipeDown,
        EventType::PinchIn,
        EventType::PinchOut,
        EventType::RotateClockwise,
        EventType::RotateCounterClockwise,
        EventType::Copy,
        EventType::Cut,
        EventType::Paste,
        EventType::Play,
        EventType::Pause,
        EventType::Ended,
        EventType::TimeUpdate,
        EventType::VolumeChange,
        EventType::MediaError,
        EventType::Mount,
        EventType::Unmount,
        EventType::Update,
        EventType::Resize,
        EventType::Dismiss,
        EventType::TearOff,
        EventType::Dock,
        EventType::WindowResize,
        EventType::WindowMove,
        EventType::WindowClose,
        EventType::WindowFrameChanged,
        EventType::WindowFocusIn,
        EventType::WindowFocusOut,
        EventType::ThemeChange,
        EventType::WindowDpiChanged,
        EventType::WindowMonitorChanged,
        EventType::MonitorConnected,
        EventType::MonitorDisconnected,
        EventType::FileHover,
        EventType::FileDrop,
        EventType::FileHoverCancel,
        EventType::SensorChanged,
        EventType::GamepadInput,
        EventType::GeolocationFix,
        EventType::GeolocationError,
        EventType::PermissionChanged,
        EventType::BiometricResult,
        EventType::ScreenColorPicked,
        EventType::KeyringResult,
        EventType::DocumentEdit,
        EventType::TextChanged,
    ];
1
    let target = DomNodeId {
1
        dom: DomId::ROOT_ID,
1
        node: NodeHierarchyItemId::from_crate_internal(Some(NodeId::new(1))),
1
    };
1
    let mut desyncs: Vec<String> = Vec::new();
93
    for &event_type in EVENTS {
92
        let event = SyntheticEvent::new(
92
            event_type,
92
            EventSource::User,
92
            target,
92
            Instant::Tick(SystemTick::new(0)),
92
            EventData::None,
        );
92
        let planned = event_type_to_filters(event_type, &event.data);
17756
        let mut check = |filter: EventFilter| {
17756
            let is_planned = planned.contains(&filter);
17756
            let does_match = super::matches_filter_phase(filter, &event, EventPhase::Bubble);
17756
            if is_planned != does_match {
                desyncs.push(format!(
                    "{event_type:?} x {filter:?}: planned={is_planned} matched={does_match}"
                ));
17756
            }
17756
        };
6256
        for &f in HOVER {
6164
            check(EventFilter::Hover(f));
6164
        }
4600
        for &f in FOCUS {
4508
            check(EventFilter::Focus(f));
4508
        }
6348
        for &f in WINDOW {
6256
            check(EventFilter::Window(f));
6256
        }
920
        for &f in COMPONENT {
828
            check(EventFilter::Component(f));
828
        }
    }
    // ZERO de-syncs, permanently: planning is DERIVED from matching
    // (`event_type_to_filters` probes `matches_filter_phase` over the whole
    // filter universe), so the two cannot drift. Before that they were two
    // hand-written tables and this cross-product found 61 disagreements - in
    // both directions: filters the matcher accepted but planning never
    // emitted (pen enter/leave, scroll start/end, every focus-scoped drag
    // event - listeners that could never fire), and filters planned but not
    // matched (IME composition, window double-click - collected, then
    // dropped). If this ever fails again, the two tables have been split
    // apart once more.
1
    assert!(
1
        desyncs.is_empty(),
        "dispatch planning and phase matching disagree on {} pair(s):\n{}",
        desyncs.len(),
        desyncs.join("\n"),
    );
1
}
/// Every lifecycle event plans exactly its own `Component` listener.
///
/// The cross-product test above proves planning and matching agree; this one
/// pins WHAT they agree on for the lifecycle family, in the terms a widget
/// author uses: an `AfterMount` callback is reached by a `Mount` event and by
/// nothing else. It is the unit-level shadow of `dll/tests/headless_lifecycle`,
/// which drives the same contract through a real window and was the test that
/// found the family missing (mount=0 on the very first frame).
#[test]
1
fn every_lifecycle_event_plans_exactly_its_component_listener() {
    use crate::events::{
        event_type_to_filters, ComponentEventFilter, EventData, EventFilter, EventType,
    };
    const PAIRS: &[(EventType, ComponentEventFilter)] = &[
        (EventType::Mount, ComponentEventFilter::AfterMount),
        (EventType::Unmount, ComponentEventFilter::BeforeUnmount),
        (EventType::Update, ComponentEventFilter::Updated),
        (EventType::Resize, ComponentEventFilter::NodeResized),
        (EventType::Dismiss, ComponentEventFilter::Dismissed),
        (EventType::TearOff, ComponentEventFilter::TornOff),
        (EventType::Dock, ComponentEventFilter::Docked),
    ];
8
    for &(event_type, listener) in PAIRS {
7
        let planned = event_type_to_filters(event_type, &EventData::None);
7
        let components: Vec<ComponentEventFilter> = planned
7
            .iter()
7
            .filter_map(|f| match f {
7
                EventFilter::Component(c) => Some(*c),
                _ => None,
7
            })
7
            .collect();
7
        assert_eq!(
            components,
7
            vec![listener],
            "{event_type:?} must reach exactly the {listener:?} listener; planned {planned:?}"
        );
    }
    // And the other direction: a pointer event never wakes a lifecycle
    // listener (a Mount callback firing on a click would be as wrong as one
    // never firing).
4
    for ty in [
1
        EventType::Click,
1
        EventType::MouseUp,
1
        EventType::KeyDown,
1
        EventType::Scroll,
    ] {
4
        let planned = event_type_to_filters(ty, &EventData::None);
4
        assert!(
4
            !planned
4
                .iter()
10
                .any(|f| matches!(f, EventFilter::Component(_))),
            "{ty:?} must not plan a lifecycle listener; planned {planned:?}"
        );
    }
1
}
/// A REAL POINTER CLICK must activate a control exactly ONCE.
///
/// `event_determination` emits BOTH `MouseUp` and `Click` for one release (as
/// browsers do). While `Click` also mapped onto MouseUp listeners, every
/// activation handler ran twice - the ColorInput's toggle opened its picker
/// and immediately closed it, so clicking the swatch appeared to do nothing
/// (device report, 2026-09-01).
#[test]
1
fn a_pointer_release_activates_a_click_listener_exactly_once() {
    use crate::{
        dom::{DomId, DomNodeId, NodeId},
        events::{
            event_type_to_filters, EventData, EventFilter, HoverEventFilter, MouseButton,
            MouseEventData,
        },
        styled_dom::NodeHierarchyItemId,
    };
1
    let _target = DomNodeId {
1
        dom: DomId::ROOT_ID,
1
        node: NodeHierarchyItemId::from_crate_internal(Some(NodeId::new(1))),
1
    };
1
    let left = EventData::Mouse(MouseEventData {
1
        position: crate::geom::LogicalPosition::zero(),
1
        button: MouseButton::Left,
1
        buttons: 0,
1
        modifiers: crate::events::KeyModifiers::new(),
1
        ..Default::default()
1
    });
1
    let activation = EventFilter::Hover(HoverEventFilter::Click);
1
    let up_filters = event_type_to_filters(EventType::MouseUp, &left);
1
    let click_filters = event_type_to_filters(EventType::Click, &left);
    // The pair of events a single release produces must together reach the
    // activation listener EXACTLY once.
1
    let hits = usize::from(up_filters.contains(&activation))
1
        + usize::from(click_filters.contains(&activation));
1
    assert_eq!(
        hits, 1,
        "one release must activate once: MouseUp planned {up_filters:?}, Click planned \
         {click_filters:?}",
    );
    // And symmetrically, a raw-release listener must fire once too.
1
    let raw = EventFilter::Hover(HoverEventFilter::MouseUp);
1
    let raw_hits =
1
        usize::from(up_filters.contains(&raw)) + usize::from(click_filters.contains(&raw));
1
    assert_eq!(
        raw_hits, 1,
        "a MouseUp listener must also fire exactly once"
    );
1
}
/// Keyboard and assistive-technology activation reach the SAME listener a
/// pointer click does - one activation concept, not three near-misses.
#[test]
1
fn keyboard_and_a11y_activation_reach_the_same_listener_as_a_click() {
    use crate::{
        dom::On,
        events::{event_type_to_filters, EventData, EventFilter, HoverEventFilter},
    };
1
    let activation = EventFilter::Hover(HoverEventFilter::Click);
    // Enter / Space dispatch a synthetic Click.
1
    assert!(event_type_to_filters(EventType::Click, &EventData::None).contains(&activation));
    // The a11y default action maps to the same filter.
1
    assert_eq!(EventFilter::from(On::Default), activation);
    // ...and so does the public `On::Click` sugar.
1
    assert_eq!(EventFilter::from(On::Click), activation);
1
}
/// A press is NOT an activation: it must not reach a Click listener, or a
/// control would fire before the user could release somewhere else.
#[test]
1
fn a_press_alone_never_activates() {
    use crate::events::{event_type_to_filters, EventData, EventFilter, HoverEventFilter};
1
    let planned = event_type_to_filters(EventType::MouseDown, &EventData::None);
1
    assert!(
1
        !planned.contains(&EventFilter::Hover(HoverEventFilter::Click)),
        "MouseDown must not activate: {planned:?}",
    );
1
}
/// ARROW KEYS BELONG TO THE FOCUSED WIDGET unless a TEXT EDITOR has focus.
///
/// The input interpreter used to claim every arrow for a caret op and return
/// `AddAndSkip`, which SWALLOWS the event - it never reached user callbacks.
/// A focused Slider or colour-picker plane therefore could not implement
/// arrow keys at all: the handler was never called (device report,
/// 2026-09-01, "the arrow keys for sliders do not work at all, nor the four
/// arrow keys for navigating the color gradient"). F1 and letter keys worked,
/// which is what pointed at the interpreter rather than at dispatch.
#[test]
1
fn arrows_are_claimed_for_the_caret_only_while_editing() {
    use crate::{
        dom::{DomId, DomNodeId},
        events::{
            EventData, EventSource, EventType, KeyModifiers, KeyboardEventData, SyntheticEvent,
        },
        id::NodeId,
        styled_dom::NodeHierarchyItemId,
        task::{Instant, SystemTick},
        window::{KeyboardState, OptionVirtualKeyCode, VirtualKeyCode, VirtualKeyCodeVec},
    };
1
    let target = DomNodeId {
1
        dom: DomId { inner: 0 },
1
        node: NodeHierarchyItemId::from_crate_internal(Some(NodeId::new(2))),
1
    };
2
    let arrow = || {
2
        SyntheticEvent::new(
2
            EventType::KeyDown,
2
            EventSource::User,
2
            target,
2
            Instant::Tick(SystemTick::new(0)),
2
            EventData::Keyboard(KeyboardEventData {
2
                key_code: VirtualKeyCode::Left as u32,
2
                char_code: None,
2
                modifiers: KeyModifiers::default(),
2
                repeat: false,
2
                ..Default::default()
2
            }),
        )
2
    };
1
    let kb = KeyboardState {
1
        current_virtual_keycode: OptionVirtualKeyCode::Some(VirtualKeyCode::Left),
1
        pressed_virtual_keycodes: VirtualKeyCodeVec::from_vec(vec![VirtualKeyCode::Left]),
1
        ..KeyboardState::default()
1
    };
    // EDITING: the caret owns the arrow.
1
    assert!(
1
        super::handle_key_down(&arrow(), &kb, Some(target), true).is_some(),
        "a text-editing focus must still take the arrow for caret movement",
    );
    // NOT EDITING: the interpreter must keep its hands off, so the event
    // reaches the focused widget (and, failing that, the scroll default).
1
    assert!(
1
        super::handle_key_down(&arrow(), &kb, Some(target), false).is_none(),
        "outside a text editor the arrow must pass through to the widget",
    );
1
}
/// EVERY filter category must be probed by planning.
///
/// Planning is DERIVED: `event_type_to_filters` tests each filter in a set of
/// `ALL_*` lists against a probe event. A category that has no list, or a
/// filter missing from one, can never be planned however correct its matcher
/// is — and the failure is silent, because the callback is simply never
/// collected.
///
/// Component and Application had NO list at all, so every lifecycle callback
/// (`AfterMount`, `NodeResized`, `Updated`, `Dismissed`, `TornOff`, `Docked`)
/// and every device/monitor callback in every app was dead. This asserts the
/// round trip for each of them, so a new filter variant cannot be added
/// without being plannable.
#[test]
1
fn every_component_and_application_filter_is_reachable_from_planning() {
    use crate::events::{
        event_type_to_filters, ApplicationEventFilter as A, ComponentEventFilter as C, EventData,
        EventFilter, EventType,
    };
1
    let component: &[(C, EventType)] = &[
1
        (C::AfterMount, EventType::Mount),
1
        (C::BeforeUnmount, EventType::Unmount),
1
        (C::NodeResized, EventType::Resize),
1
        (C::DefaultAction, EventType::DefaultAction),
1
        (C::Selected, EventType::Selected),
1
        (C::Updated, EventType::Update),
1
        (C::Dismissed, EventType::Dismiss),
1
        (C::TornOff, EventType::TearOff),
1
        (C::Docked, EventType::Dock),
1
    ];
10
    for (filter, event_type) in component {
9
        let planned = event_type_to_filters(*event_type, &EventData::None);
9
        assert!(
9
            planned.contains(&EventFilter::Component(*filter)),
            "{event_type:?} must plan {filter:?}, got {planned:?}",
        );
    }
1
    let application: &[(A, EventType)] = &[
1
        (A::DeviceConnected, EventType::DeviceConnected),
1
        (A::DeviceDisconnected, EventType::DeviceDisconnected),
1
        (A::MonitorConnected, EventType::MonitorConnected),
1
        (A::MonitorDisconnected, EventType::MonitorDisconnected),
1
        (A::MediaControl, EventType::MediaControl),
1
        (A::SystemAudioChange, EventType::SystemAudioChange),
1
    ];
7
    for (filter, event_type) in application {
6
        let planned = event_type_to_filters(*event_type, &EventData::None);
6
        assert!(
6
            planned.contains(&EventFilter::Application(*filter)),
            "{event_type:?} must plan {filter:?}, got {planned:?}",
        );
    }
1
}
/// The dial's four layers agree: the filters are PLANNABLE and they match.
///
/// `DialState` was readable through `CallbackInfo::get_dial_state()` from the
/// day the type landed, with no `EventType` and no filter behind it — so a
/// dial could only be POLLED from an unrelated callback that happened to run.
/// This pins the round trip, so a filter that exists but is absent from
/// `ALL_HOVER`/`ALL_WINDOW` (and is therefore unplannable) fails here.
#[test]
1
fn the_dial_filters_are_reachable_from_planning() {
    use crate::events::{
        event_type_to_filters, EventData, EventFilter, EventType, HoverEventFilter as H,
        WindowEventFilter as W,
    };
2
    for (event_type, hover, window) in [
1
        (EventType::DialRotate, H::DialRotate, W::DialRotate),
1
        (EventType::DialClick, H::DialClick, W::DialClick),
    ] {
2
        let planned = event_type_to_filters(event_type, &EventData::None);
2
        assert!(
2
            planned.contains(&EventFilter::Window(window)),
            "{event_type:?} must plan {window:?}, got {planned:?}",
        );
2
        assert!(
2
            planned.contains(&EventFilter::Hover(hover)),
            "{event_type:?} must plan {hover:?}, got {planned:?}",
        );
    }
1
}
#[cfg(test)]
mod seat_dedup_tests {
    use crate::{
        dom::{DomId, DomNodeId},
        events::{
            deduplicate_synthetic_events, EventData, EventSource, EventType, KeyModifiers,
            MouseButton, MouseEventData, SyntheticEvent,
        },
        geom::LogicalPosition,
        id::NodeId,
        styled_dom::NodeHierarchyItemId,
        task::{Instant, SystemTick},
    };
4
    fn press(seat_id: u64) -> SyntheticEvent {
4
        SyntheticEvent::new(
4
            EventType::MouseDown,
4
            EventSource::User,
4
            DomNodeId {
4
                dom: DomId::ROOT_ID,
4
                node: NodeHierarchyItemId::from_crate_internal(Some(NodeId::new(4))),
4
            },
4
            Instant::Tick(SystemTick { tick_counter: 0 }),
4
            EventData::Mouse(MouseEventData {
4
                position: LogicalPosition::zero(),
4
                button: MouseButton::Left,
4
                buttons: 1,
4
                modifiers: KeyModifiers::default(),
4
                seat_id,
4
                ..Default::default()
4
            }),
        )
4
    }
    #[test]
1
    fn two_seats_pressing_one_node_are_two_presses() {
        // The touch path's documented limit, no longer shared by seats
        // (9b-ii-b): coalescing by (target, type) alone folded them into one.
1
        let out = deduplicate_synthetic_events(vec![press(0), press(7)]);
1
        assert_eq!(out.len(), 2, "{out:?}");
        // One seat pressing twice in a pass still coalesces.
1
        let out = deduplicate_synthetic_events(vec![press(7), press(7)]);
1
        assert_eq!(out.len(), 1);
1
    }
}