pathland-core 0.2.2

Pathland 16-byte opcode protocol: fixed-size opcodes, ring buffer, arena, events, and linear-memory layout (no_std)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
//! Typed raw-input events (host → guest).
//!
//! Events are the **raw inputs** the renderer reports: pointer down/move/up and
//! key down/up, each carrying a host-resolved `targetId`. High-level
//! interpretations (tap, drag, long-press) are built by the guest/app from
//! these raw inputs and are NOT part of the protocol.
//!
//! Encode a raw input with `Event::encode`, decode a ring opcode with
//! `Event::try_from`.

use alloc::string::String;
use alloc::string::ToString;
use crate::category;
use crate::flag;
use crate::Opcode;

/// A raw-input event (host → guest).
///
/// `Event` is **not** `Copy`: [`Event::TextChanged`] owns the new field text
/// and [`Event::Navigate`] owns an optional URL. Simple events round-trip
/// through `encode`/`try_from` as pure 16-byte opcodes; `TextChanged` and
/// `Navigate` with a URL are encoded/decoded at the batch level (their strings
/// live in the string section / event arena, referenced by a relative/absolute
/// offset).
#[derive(Debug, Clone, PartialEq)]
pub enum Event {
    /// Pointer button pressed at a viewport-relative point.
    PointerDown {
        target: u32,
        x: f32,
        y: f32,
        secondary: bool,
    },
    /// Pointer moved; optionally entered/left the node (hover).
    PointerMove {
        target: u32,
        x: f32,
        y: f32,
        hovering: bool,
        leaving: bool,
    },
    /// Pointer button released at a viewport-relative point.
    PointerUp {
        target: u32,
        x: f32,
        y: f32,
        secondary: bool,
    },
    /// Key pressed (possibly an auto-repeat).
    KeyDown {
        target: u32,
        key_code: u16,
        modifiers: u8,
        repeat: bool,
    },
    /// Key released.
    KeyUp {
        target: u32,
        key_code: u16,
        modifiers: u8,
    },
    /// A control's value changed (host → guest). The renderer resolves the
    /// semantic value (e.g. a slider thumb dragged along its track) and reports
    /// it here; the guest/app writes it into its bound state.
    ValueChanged {
        target: u32,
        value: f32,
    },
    /// A text field's value changed (host → guest). The new text is resolved
    /// from the batch's string section (see the doc comment on this enum).
    TextChanged {
        target: u32,
        value: String,
    },
    /// A node gained/lost focus (host → guest). Draft command 0x08.
    FocusChanged { target: u32, focused: bool },
    /// An editing session began/ended (host → guest). Draft command 0x09.
    EditingChanged { target: u32, editing: bool },
    /// The user committed a field (host → guest). Draft command 0x0A.
    Submit { target: u32 },
    /// A scroll container's offset changed (host → guest). Draft command 0x0B.
    Scroll {
        target: u32,
        offset_x: f32,
        offset_y: f32,
    },
    /// Raw wheel/trackpad deltas (host → guest). Draft command 0x0C.
    Wheel {
        target: u32,
        delta_x: f32,
        delta_y: f32,
    },
    /// A `DATE_PICKER`'s value changed (host → guest). Draft command 0x0D;
    /// `days` is I32 (pre-1970 negative), `millis` is millis of day.
    DateChanged {
        target: u32,
        days: i32,
        millis: u32,
    },
    /// A global navigation request (host → guest). Draft command 0x0E; never
    /// node-keyed. `url: Some(...)` carries the destination URL the platform
    /// navigated to (browser `popstate`/back/forward/deep-link), resolved from
    /// the string section / event arena; `url: None` is a native back request.
    Navigate { url: Option<String> },
    /// A bound `MEDIA_CONTROLLER`'s playback state changed (host → guest).
    /// Draft command 0x0F; reported only when `PLAYBACK_STATE` is bound.
    MediaPlayStateChanged { target: u32, playing: bool },
    /// A bound controller's current playback position (host → guest). Draft
    /// command 0x10; `seconds` is the position in seconds (f32), reported while
    /// playing. An app-initiated seek (`MEDIA_POSITION` change) is not echoed.
    MediaTimeUpdated { target: u32, seconds: f32 },
    /// A bound controller reached the end of its media (host → guest). Draft
    /// command 0x11.
    MediaEnded { target: u32 },
    /// A bound controller's volume changed (host → guest). Draft command 0x12;
    /// `volume` is in the range 0..1 (f32).
    MediaVolumeChanged { target: u32, volume: f32 },
    /// A bound controller's media duration became known/changed (host → guest).
    /// Draft command 0x14; `seconds` is the duration (f32). Reported when the
    /// resource loads and only when the value changes.
    MediaDurationChanged { target: u32, seconds: f32 },
    /// A `SIZE_THAT_FITS` slot's selected candidate changed (host → guest).
    /// Draft command 0x13; `index` is the derived fit index (f32), reported once
    /// after the first measure and then only when it changes.
    FitChanged { target: u32, index: f32 },
}

impl Event {
    /// Encode this event as a 16-byte opcode.
    ///
    /// `TextChanged` and `Navigate` with a URL cannot be fully encoded here —
    /// their strings live in the batch's string section / event arena — so this
    /// emits the opcode with a zero `B` offset; the batch-aware
    /// `encode_events`/`decode_events` pair in `pathland-core-transport` writes
    /// the string and its real offset.
    pub fn encode(&self) -> Opcode {
        match self {
            Event::PointerDown {
                target,
                x,
                y,
                secondary,
            } => Opcode::new(
                category::EVENT,
                crate::event::POINTER_DOWN,
                flag_for(*secondary, false, false),
                *target,
                x.to_bits(),
                y.to_bits(),
            ),
            Event::PointerMove {
                target,
                x,
                y,
                hovering,
                leaving,
            } => Opcode::new(
                category::EVENT,
                crate::event::POINTER_MOVE,
                flag_for(false, *hovering, *leaving),
                *target,
                x.to_bits(),
                y.to_bits(),
            ),
            Event::PointerUp {
                target,
                x,
                y,
                secondary,
            } => Opcode::new(
                category::EVENT,
                crate::event::POINTER_UP,
                flag_for(*secondary, false, false),
                *target,
                x.to_bits(),
                y.to_bits(),
            ),
            Event::KeyDown {
                target,
                key_code,
                modifiers,
                repeat,
            } => Opcode::new(
                category::EVENT,
                crate::event::KEY_DOWN,
                if *repeat { flag::KEY_REPEAT } else { 0 },
                *target,
                *key_code as u32,
                *modifiers as u32,
            ),
            Event::KeyUp {
                target,
                key_code,
                modifiers,
            } => Opcode::new(
                category::EVENT,
                crate::event::KEY_UP,
                0,
                *target,
                *key_code as u32,
                *modifiers as u32,
            ),
            Event::ValueChanged { target, value } => Opcode::new(
                category::EVENT,
                crate::event::VALUE_CHANGED,
                0,
                *target,
                value.to_bits(),
                0,
            ),
            Event::TextChanged { target, .. } => Opcode::new(
                category::EVENT,
                crate::event::TEXT_CHANGED,
                0,
                *target,
                0,
                0,
            ),
            Event::FocusChanged { target, focused } => Opcode::new(
                category::EVENT,
                crate::event::FOCUS_CHANGED,
                0,
                *target,
                u32::from(*focused),
                0,
            ),
            Event::EditingChanged { target, editing } => Opcode::new(
                category::EVENT,
                crate::event::EDITING_CHANGED,
                0,
                *target,
                u32::from(*editing),
                0,
            ),
            Event::Submit { target } => Opcode::new(
                category::EVENT,
                crate::event::SUBMIT,
                0,
                *target,
                0,
                0,
            ),
            Event::Scroll {
                target,
                offset_x,
                offset_y,
            } => Opcode::new(
                category::EVENT,
                crate::event::SCROLL,
                0,
                *target,
                offset_x.to_bits(),
                offset_y.to_bits(),
            ),
            Event::Wheel {
                target,
                delta_x,
                delta_y,
            } => Opcode::new(
                category::EVENT,
                crate::event::WHEEL,
                0,
                *target,
                delta_x.to_bits(),
                delta_y.to_bits(),
            ),
            Event::DateChanged {
                target,
                days,
                millis,
            } => Opcode::new(
                category::EVENT,
                crate::event::DATE_CHANGED,
                0,
                *target,
                *days as u32,
                *millis,
            ),
            Event::Navigate { url } => Opcode::new(
                category::EVENT,
                crate::event::NAVIGATE,
                if url.is_some() { flag::NAVIGATE_URL } else { 0 },
                0,
                0,
                0,
            ),
            Event::MediaPlayStateChanged { target, playing } => Opcode::new(
                category::EVENT,
                crate::event::MEDIA_PLAY_STATE_CHANGED,
                0,
                *target,
                u32::from(*playing),
                0,
            ),
            Event::MediaTimeUpdated { target, seconds } => Opcode::new(
                category::EVENT,
                crate::event::MEDIA_TIME_UPDATED,
                0,
                *target,
                seconds.to_bits(),
                0,
            ),
            Event::MediaEnded { target } => Opcode::new(
                category::EVENT,
                crate::event::MEDIA_ENDED,
                0,
                *target,
                0,
                0,
            ),
            Event::MediaVolumeChanged { target, volume } => Opcode::new(
                category::EVENT,
                crate::event::MEDIA_VOLUME_CHANGED,
                0,
                *target,
                volume.to_bits(),
                0,
            ),
            Event::MediaDurationChanged { target, seconds } => Opcode::new(
                category::EVENT,
                crate::event::MEDIA_DURATION_CHANGED,
                0,
                *target,
                seconds.to_bits(),
                0,
            ),
            Event::FitChanged { target, index } => Opcode::new(
                category::EVENT,
                crate::event::FIT_CHANGED,
                0,
                *target,
                index.to_bits(),
                0,
            ),
        }
    }
}

/// Build a pointer-opcode flags word from the pointer/hover bits.
#[inline]
fn flag_for(secondary: bool, hovering: bool, leaving: bool) -> u16 {
    let mut flags = 0u16;
    if secondary {
        flags |= flag::POINTER_SECONDARY;
    }
    if hovering {
        flags |= flag::HOVER_ENTER;
    }
    if leaving {
        flags |= flag::HOVER_LEAVE;
    }
    flags
}

/// Error decoding a raw-input event from an opcode.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EventError {
    /// The opcode is not an `EVENT` category opcode.
    NotAnEvent,
    /// The opcode is an EVENT category opcode but an unknown command.
    UnknownCommand,
    /// The opcode is `TEXT_CHANGED`, which needs the batch's string section to
    /// resolve its text; decode it with `pathland-core-transport::decode_events`
    /// instead of a bare opcode.
    NeedsStringSection,
}

impl TryFrom<Opcode> for Event {
    type Error = EventError;

    fn try_from(op: Opcode) -> Result<Self, Self::Error> {
        if op.category() != category::EVENT {
            return Err(EventError::NotAnEvent);
        }
        let flags = op.flags();
        match op.command() {
            crate::event::POINTER_DOWN => Ok(Event::PointerDown {
                target: op.a(),
                x: op.b_f32(),
                y: op.c_f32(),
                secondary: flags & flag::POINTER_SECONDARY != 0,
            }),
            crate::event::POINTER_MOVE => Ok(Event::PointerMove {
                target: op.a(),
                x: op.b_f32(),
                y: op.c_f32(),
                hovering: flags & flag::HOVER_ENTER != 0,
                leaving: flags & flag::HOVER_LEAVE != 0,
            }),
            crate::event::POINTER_UP => Ok(Event::PointerUp {
                target: op.a(),
                x: op.b_f32(),
                y: op.c_f32(),
                secondary: flags & flag::POINTER_SECONDARY != 0,
            }),
            crate::event::KEY_DOWN => Ok(Event::KeyDown {
                target: op.a(),
                key_code: op.b() as u16,
                modifiers: op.c() as u8,
                repeat: flags & flag::KEY_REPEAT != 0,
            }),
            crate::event::KEY_UP => Ok(Event::KeyUp {
                target: op.a(),
                key_code: op.b() as u16,
                modifiers: op.c() as u8,
            }),
            crate::event::VALUE_CHANGED => Ok(Event::ValueChanged {
                target: op.a(),
                value: op.b_f32(),
            }),
            crate::event::FOCUS_CHANGED => Ok(Event::FocusChanged {
                target: op.a(),
                focused: op.b() != 0,
            }),
            crate::event::EDITING_CHANGED => Ok(Event::EditingChanged {
                target: op.a(),
                editing: op.b() != 0,
            }),
            crate::event::SUBMIT => Ok(Event::Submit { target: op.a() }),
            crate::event::SCROLL => Ok(Event::Scroll {
                target: op.a(),
                offset_x: op.b_f32(),
                offset_y: op.c_f32(),
            }),
            crate::event::WHEEL => Ok(Event::Wheel {
                target: op.a(),
                delta_x: op.b_f32(),
                delta_y: op.c_f32(),
            }),
            crate::event::DATE_CHANGED => Ok(Event::DateChanged {
                target: op.a(),
                days: op.b() as i32,
                millis: op.c(),
            }),
            crate::event::TEXT_CHANGED => Err(EventError::NeedsStringSection),
            crate::event::NAVIGATE => {
                if flags & flag::NAVIGATE_URL != 0 {
                    Err(EventError::NeedsStringSection)
                } else {
                    Ok(Event::Navigate { url: None })
                }
            }
            crate::event::MEDIA_PLAY_STATE_CHANGED => Ok(Event::MediaPlayStateChanged {
                target: op.a(),
                playing: op.b() != 0,
            }),
            crate::event::MEDIA_TIME_UPDATED => Ok(Event::MediaTimeUpdated {
                target: op.a(),
                seconds: op.b_f32(),
            }),
            crate::event::MEDIA_ENDED => Ok(Event::MediaEnded { target: op.a() }),
            crate::event::MEDIA_VOLUME_CHANGED => Ok(Event::MediaVolumeChanged {
                target: op.a(),
                volume: op.b_f32(),
            }),
            crate::event::MEDIA_DURATION_CHANGED => Ok(Event::MediaDurationChanged {
                target: op.a(),
                seconds: op.b_f32(),
            }),
            crate::event::FIT_CHANGED => Ok(Event::FitChanged {
                target: op.a(),
                index: op.b_f32(),
            }),
            _ => Err(EventError::UnknownCommand),
        }
    }
}

/// Decode an event opcode for the **shared-memory path**, resolving
/// `TEXT_CHANGED` text and `NAVIGATE` URLs from the host → guest event arena
/// (`B` is an absolute offset into that region). Non-string events decode as
/// usual.
///
/// Returns `None` for non-event opcodes, unknown commands, or string events
/// whose offset cannot be resolved (invalid or out of range).
pub fn decode_event(op: &Opcode, event_arena: &[u8]) -> Option<Event> {
    if op.category() != category::EVENT {
        return None;
    }
    if op.command() == crate::event::TEXT_CHANGED {
        let text = crate::arena::get_str(event_arena, op.b()).ok()?;
        return Some(Event::TextChanged {
            target: op.a(),
            value: text.to_string(),
        });
    }
    if op.command() == crate::event::NAVIGATE && op.flags() & flag::NAVIGATE_URL != 0 {
        let url = crate::arena::get_str(event_arena, op.b()).ok()?;
        return Some(Event::Navigate {
            url: Some(url.to_string()),
        });
    }
    Event::try_from(*op).ok()
}

#[cfg(test)]
mod tests {
    use super::*;

    fn round_trip(e: Event) -> Event {
        Event::try_from(Opcode::from_bytes(&e.encode().to_bytes())).unwrap()
    }

    #[test]
    fn pointer_down_round_trips() {
        let e = Event::PointerDown {
            target: 5,
            x: 10.0,
            y: 20.0,
            secondary: true,
        };
        assert_eq!(round_trip(e.clone()), e);
    }

    #[test]
    fn pointer_move_round_trips_with_hover_flags() {
        let e = Event::PointerMove {
            target: 5,
            x: 10.0,
            y: 20.0,
            hovering: true,
            leaving: false,
        };
        assert_eq!(round_trip(e.clone()), e);

        let leaving = Event::PointerMove {
            target: 5,
            x: 10.0,
            y: 20.0,
            hovering: false,
            leaving: true,
        };
        assert_eq!(round_trip(leaving.clone()), leaving);
    }

    #[test]
    fn pointer_up_round_trips() {
        let e = Event::PointerUp {
            target: 5,
            x: 10.0,
            y: 20.0,
            secondary: false,
        };
        assert_eq!(round_trip(e.clone()), e);
    }

    #[test]
    fn key_down_round_trips_with_repeat() {
        let e = Event::KeyDown {
            target: 5,
            key_code: 0x41,
            modifiers: 0x01,
            repeat: true,
        };
        assert_eq!(round_trip(e.clone()), e);
    }

    #[test]
    fn key_up_round_trips() {
        let e = Event::KeyUp {
            target: 5,
            key_code: 0x41,
            modifiers: 0x01,
        };
        assert_eq!(round_trip(e.clone()), e);
    }

    #[test]
    fn value_changed_round_trips() {
        let e = Event::ValueChanged {
            target: 7,
            value: 0.625,
        };
        assert_eq!(round_trip(e.clone()), e);
    }

    #[test]
    fn draft_events_round_trip() {
        for e in [
            Event::FocusChanged { target: 1, focused: true },
            Event::FocusChanged { target: 1, focused: false },
            Event::EditingChanged { target: 2, editing: true },
            Event::Submit { target: 3 },
            Event::Scroll { target: 4, offset_x: 10.0, offset_y: 20.5 },
            Event::Wheel { target: 5, delta_x: -1.5, delta_y: 2.0 },
            Event::DateChanged { target: 6, days: 19_723, millis: 3_600_000 },
            Event::DateChanged { target: 6, days: -1, millis: 0 },
        ] {
            assert_eq!(round_trip(e.clone()), e);
        }
    }

    #[test]
    fn media_events_round_trip() {
        for e in [
            Event::MediaPlayStateChanged { target: 7, playing: true },
            Event::MediaPlayStateChanged { target: 7, playing: false },
            Event::MediaTimeUpdated { target: 7, seconds: 12.5 },
            Event::MediaEnded { target: 7 },
            Event::MediaVolumeChanged { target: 7, volume: 0.5 },
            Event::MediaDurationChanged { target: 7, seconds: 210.0 },
            Event::FitChanged { target: 42, index: 2.0 },
        ] {
            assert_eq!(round_trip(e.clone()), e);
        }
        // Conformance vector bytes (spec/CONFORMANCE.md vectors 23–26, 26a).
        assert_eq!(
            Event::MediaPlayStateChanged { target: 7, playing: true }.encode().to_bytes(),
            [0x03, 0x0F, 0, 0, 7, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0]
        );
        assert_eq!(
            Event::MediaTimeUpdated { target: 7, seconds: 12.5 }.encode().to_bytes(),
            [0x03, 0x10, 0, 0, 7, 0, 0, 0, 0, 0, 0x48, 0x41, 0, 0, 0, 0]
        );
        assert_eq!(
            Event::MediaEnded { target: 7 }.encode().to_bytes(),
            [0x03, 0x11, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
        );
        assert_eq!(
            Event::MediaVolumeChanged { target: 7, volume: 0.5 }.encode().to_bytes(),
            [0x03, 0x12, 0, 0, 7, 0, 0, 0, 0, 0, 0x00, 0x3F, 0, 0, 0, 0]
        );
        assert_eq!(
            Event::MediaDurationChanged { target: 7, seconds: 210.0 }.encode().to_bytes(),
            [0x03, 0x14, 0, 0, 7, 0, 0, 0, 0, 0, 0x52, 0x43, 0, 0, 0, 0]
        );
        assert_eq!(
            Event::FitChanged { target: 42, index: 2.0 }.encode().to_bytes(),
            [0x03, 0x13, 0, 0, 0x2A, 0, 0, 0, 0, 0, 0, 0x40, 0, 0, 0, 0]
        );
    }

    #[test]
    fn non_event_opcode_is_rejected() {
        let op = Opcode::new(crate::category::TREE, 0x01, 0, 1, 0, 0);
        assert_eq!(Event::try_from(op), Err(EventError::NotAnEvent));
    }

    #[test]
    fn unknown_event_command_is_rejected() {
        // EVENT category with a command that does not exist in the protocol.
        let op = Opcode::new(crate::category::EVENT, 0xFF, 0, 1, 0, 0);
        assert_eq!(Event::try_from(op), Err(EventError::UnknownCommand));
    }

    #[test]
    fn text_changed_needs_string_section() {
        // `TextChanged` is only decodable from a batch (its text lives in the
        // string section); a bare opcode cannot resolve it.
        let op = Opcode::new(crate::category::EVENT, crate::event::TEXT_CHANGED, 0, 7, 0, 0);
        assert_eq!(Event::try_from(op), Err(EventError::NeedsStringSection));
    }

    #[test]
    fn navigate_back_round_trips() {
        let e = Event::Navigate { url: None };
        assert_eq!(round_trip(e.clone()), e);
    }

    #[test]
    fn navigate_with_url_needs_string_section() {
        // `Navigate` with a URL is only decodable where the URL can be resolved
        // (batch string section / shared-memory event arena); a bare opcode
        // cannot resolve it.
        let op = Opcode::new(
            crate::category::EVENT,
            crate::event::NAVIGATE,
            flag::NAVIGATE_URL,
            0,
            0,
            0,
        );
        assert_eq!(Event::try_from(op), Err(EventError::NeedsStringSection));
    }

    #[test]
    fn navigate_back_matches_conformance_vector_bytes() {
        // EVENT:NAVIGATE (back, no URL) — vector 22.
        let e = Event::Navigate { url: None };
        assert_eq!(
            e.encode().to_bytes(),
            [0x03, 0x0E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
        );
    }

    #[test]
    fn decode_event_resolves_navigate_url_from_arena() {
        // The shared-memory path resolves a `Navigate` URL from the event arena.
        use alloc::vec;
        let mut arena = vec![0u8; 32];
        // "[u32 len=9][/users/42]" at offset 0.
        arena[0..4].copy_from_slice(&9u32.to_le_bytes());
        arena[4..13].copy_from_slice(b"/users/42");
        let op = Opcode::new(
            crate::category::EVENT,
            crate::event::NAVIGATE,
            flag::NAVIGATE_URL,
            0,
            0,
            0,
        );
        assert_eq!(
            decode_event(&op, &arena),
            Some(Event::Navigate {
                url: Some("/users/42".into())
            })
        );
    }

    #[test]
    fn pointer_move_round_trips_with_combined_hover_flags() {
        // Both hovering and leaving set simultaneously must survive the round trip.
        let e = Event::PointerMove {
            target: 5,
            x: 10.0,
            y: 20.0,
            hovering: true,
            leaving: true,
        };
        assert_eq!(round_trip(e.clone()), e);
        // The flags word carries both bits.
        assert_eq!(e.encode().flags(), flag::HOVER_ENTER | flag::HOVER_LEAVE);
    }

    #[test]
    fn matches_conformance_vector_bytes() {
        // EVENT:POINTER_DOWN (target=5, x=10.0, y=20.0)
        let down = Event::PointerDown {
            target: 5,
            x: 10.0,
            y: 20.0,
            secondary: false,
        };
        assert_eq!(
            down.encode().to_bytes(),
            [
                0x03, 0x01, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x41, 0x00,
                0x00, 0xA0, 0x41,
            ]
        );
    }
}