allsource-core 0.29.0

High-performance event store core built in Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
use crate::{
    domain::value_objects::{EntityId, EventType, TenantId},
    error::{AllSourceError, Result},
};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use uuid::Uuid;

/// Domain Entity: Event
///
/// Core event structure representing a domain event in the event store.
/// This is an immutable, timestamped record of something that happened.
///
/// Domain Rules:
/// - Events are immutable once created
/// - Event type must follow naming convention (enforced by EventType value object)
/// - Entity ID cannot be empty (enforced by EntityId value object)
/// - Tenant ID cannot be empty (enforced by TenantId value object)
/// - Timestamp must not be in the future
/// - Version starts at 1
// `Clone` is derived in normal builds. Under `cfg(test)` it is hand-written so
// that `crate::clone_probe` can count materialized events — that counter is how
// the issue #251 guards observe that a bounded query clones the page and not
// the entity's whole history (see `clone_probe`). The impl below is a
// field-for-field copy; adding a field to `Event` without adding it there is a
// compile error, not a silent divergence.
#[cfg_attr(not(test), derive(Clone))]
#[derive(Debug, Serialize, Deserialize, PartialEq)]
pub struct Event {
    pub id: Uuid,
    pub event_type: EventType,
    pub entity_id: EntityId,
    #[serde(default = "default_tenant_id")]
    pub tenant_id: TenantId,
    pub payload: serde_json::Value,
    pub timestamp: DateTime<Utc>,
    pub metadata: Option<serde_json::Value>,
    pub version: i64,
    /// When the fact became true. `None` reads as `timestamp`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub valid_from: Option<DateTime<Utc>>,
    /// When the fact stopped being true. `None` is open-ended.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub valid_to: Option<DateTime<Utc>>,
}

fn default_tenant_id() -> TenantId {
    TenantId::default_tenant()
}

/// Test-only `Clone` that tallies materialized events (see `crate::clone_probe`
/// and the issue #251 guards in `store.rs`). Behaviourally identical to the
/// derived impl used in real builds.
#[cfg(test)]
impl Clone for Event {
    fn clone(&self) -> Self {
        crate::clone_probe::record();
        let Event {
            id,
            event_type,
            entity_id,
            tenant_id,
            payload,
            timestamp,
            metadata,
            version,
            valid_from,
            valid_to,
        } = self;
        Event {
            id: *id,
            event_type: event_type.clone(),
            entity_id: entity_id.clone(),
            tenant_id: tenant_id.clone(),
            payload: payload.clone(),
            timestamp: *timestamp,
            metadata: metadata.clone(),
            version: *version,
            valid_from: *valid_from,
            valid_to: *valid_to,
        }
    }
}

impl Event {
    /// Create a new Event with value objects (recommended)
    pub fn new(
        event_type: EventType,
        entity_id: EntityId,
        tenant_id: TenantId,
        payload: serde_json::Value,
    ) -> Self {
        Self {
            id: Uuid::new_v4(),
            event_type,
            entity_id,
            tenant_id,
            payload,
            timestamp: Utc::now(),
            metadata: None,
            version: 1,
            valid_from: None,
            valid_to: None,
        }
    }

    /// Create event with optional metadata
    pub fn with_metadata(
        event_type: EventType,
        entity_id: EntityId,
        tenant_id: TenantId,
        payload: serde_json::Value,
        metadata: serde_json::Value,
    ) -> Self {
        Self {
            id: Uuid::new_v4(),
            event_type,
            entity_id,
            tenant_id,
            payload,
            timestamp: Utc::now(),
            metadata: Some(metadata),
            version: 1,
            valid_from: None,
            valid_to: None,
        }
    }

    /// Create event with default tenant (for single-tenant use)
    pub fn with_default_tenant(
        event_type: EventType,
        entity_id: EntityId,
        payload: serde_json::Value,
    ) -> Self {
        Self::new(event_type, entity_id, TenantId::default_tenant(), payload)
    }

    /// Create event from strings (for backward compatibility)
    ///
    /// This validates the strings and creates value objects.
    /// Use the value object constructor for new code.
    pub fn from_strings(
        event_type: String,
        entity_id: String,
        tenant_id: String,
        payload: serde_json::Value,
        metadata: Option<serde_json::Value>,
    ) -> Result<Self> {
        let event_type = EventType::new(event_type)?;
        let entity_id = EntityId::new(entity_id)?;
        let tenant_id = TenantId::new(tenant_id)?;

        Ok(Self {
            id: Uuid::new_v4(),
            event_type,
            entity_id,
            tenant_id,
            payload,
            timestamp: Utc::now(),
            metadata,
            version: 1,
            valid_from: None,
            valid_to: None,
        })
    }

    /// Reconstruct an Event from storage (bypasses validation for stored events)
    pub fn reconstruct(
        id: Uuid,
        event_type: EventType,
        entity_id: EntityId,
        tenant_id: TenantId,
        payload: serde_json::Value,
        timestamp: DateTime<Utc>,
        metadata: Option<serde_json::Value>,
        version: i64,
    ) -> Self {
        Self {
            id,
            event_type,
            entity_id,
            tenant_id,
            payload,
            timestamp,
            metadata,
            version,
            valid_from: None,
            valid_to: None,
        }
    }

    /// Reconstruct from raw strings (for loading from old storage)
    pub fn reconstruct_from_strings(
        id: Uuid,
        event_type: String,
        entity_id: String,
        tenant_id: String,
        payload: serde_json::Value,
        timestamp: DateTime<Utc>,
        metadata: Option<serde_json::Value>,
        version: i64,
    ) -> Self {
        Self {
            id,
            event_type: EventType::new_unchecked(event_type),
            entity_id: EntityId::new_unchecked(entity_id),
            tenant_id: TenantId::new_unchecked(tenant_id),
            payload,
            timestamp,
            metadata,
            version,
            valid_from: None,
            valid_to: None,
        }
    }

    /// Attach a valid-time interval to an event built from a request.
    ///
    /// `valid_to` must be strictly after the effective `valid_from`, which is
    /// `valid_from` when given and `timestamp` otherwise.
    pub fn with_valid_time(
        mut self,
        valid_from: Option<DateTime<Utc>>,
        valid_to: Option<DateTime<Utc>>,
    ) -> Result<Self> {
        if let Some(to) = valid_to {
            let from = valid_from.unwrap_or(self.timestamp);
            if to <= from {
                return Err(AllSourceError::ValidationError(format!(
                    "valid_to ({to}) must be after valid_from ({from})"
                )));
            }
        }
        self.valid_from = valid_from;
        self.valid_to = valid_to;
        Ok(self)
    }

    /// Start of the valid interval, with `None` read as `timestamp`.
    pub fn effective_valid_from(&self) -> DateTime<Utc> {
        self.valid_from.unwrap_or(self.timestamp)
    }

    // Getters (Events are immutable)

    pub fn id(&self) -> Uuid {
        self.id
    }

    pub fn event_type(&self) -> &EventType {
        &self.event_type
    }

    pub fn event_type_str(&self) -> &str {
        self.event_type.as_str()
    }

    pub fn entity_id(&self) -> &EntityId {
        &self.entity_id
    }

    pub fn entity_id_str(&self) -> &str {
        self.entity_id.as_str()
    }

    pub fn tenant_id(&self) -> &TenantId {
        &self.tenant_id
    }

    pub fn tenant_id_str(&self) -> &str {
        self.tenant_id.as_str()
    }

    pub fn payload(&self) -> &serde_json::Value {
        &self.payload
    }

    pub fn timestamp(&self) -> DateTime<Utc> {
        self.timestamp
    }

    pub fn metadata(&self) -> Option<&serde_json::Value> {
        self.metadata.as_ref()
    }

    pub fn version(&self) -> i64 {
        self.version
    }

    /// Approximate resident size of this event in memory, in bytes.
    /// Used by the per-tenant memory cache (Step 3 of the
    /// sustainable data strategy) to make eviction decisions
    /// against a configurable byte budget.
    ///
    /// The estimator uses serialized-JSON payload length plus a
    /// fixed 256-byte overhead for the rest of the Event struct
    /// (UUID, event_type, entity_id, tenant_id, timestamp,
    /// version, plus DashMap and Vec slot overhead). It's
    /// intentionally cheap and order-of-magnitude correct rather
    /// than bytes-precise — the budget logic compares against a
    /// 2 GiB target, so an estimator that's off by 30% per event
    /// still produces a sensible cap.
    ///
    /// Cost: O(payload size) — one serde serialization per
    /// event. Called once per event on the cache append path,
    /// not on the query path.
    pub fn estimated_size_bytes(&self) -> u64 {
        const FIXED_OVERHEAD: u64 = 256;
        let payload_bytes = serde_json::to_vec(&self.payload).map_or(0, |v| v.len() as u64);
        let metadata_bytes = self
            .metadata
            .as_ref()
            .and_then(|m| serde_json::to_vec(m).ok())
            .map_or(0, |v| v.len() as u64);
        FIXED_OVERHEAD + payload_bytes + metadata_bytes
    }

    /// Heap footprint of this event as held in memory: the inline `Event` plus
    /// the capacity of every owned allocation. Object maps are charged by
    /// the map type the build compiles, see `map_bytes`.
    pub fn resident_bytes(&self) -> u64 {
        let heap = self.event_type.heap_capacity()
            + self.entity_id.heap_capacity()
            + self.tenant_id.heap_capacity()
            + value_heap_bytes(&self.payload)
            + self.metadata.as_ref().map_or(0, value_heap_bytes);
        (std::mem::size_of::<Event>() + heap) as u64
    }

    // Domain behavior methods

    /// Check if this event belongs to a specific tenant
    pub fn belongs_to_tenant(&self, tenant_id: &TenantId) -> bool {
        &self.tenant_id == tenant_id
    }

    /// Check if this event belongs to a tenant (by string)
    pub fn belongs_to_tenant_str(&self, tenant_id: &str) -> bool {
        self.tenant_id.as_str() == tenant_id
    }

    /// Check if this event relates to a specific entity
    pub fn relates_to_entity(&self, entity_id: &EntityId) -> bool {
        &self.entity_id == entity_id
    }

    /// Check if this event relates to an entity (by string)
    pub fn relates_to_entity_str(&self, entity_id: &str) -> bool {
        self.entity_id.as_str() == entity_id
    }

    /// Check if this event is of a specific type
    pub fn is_type(&self, event_type: &EventType) -> bool {
        &self.event_type == event_type
    }

    /// Check if this event is of a type (by string)
    pub fn is_type_str(&self, event_type: &str) -> bool {
        self.event_type.as_str() == event_type
    }

    /// Check if this event is in a specific namespace
    pub fn is_in_namespace(&self, namespace: &str) -> bool {
        self.event_type.is_in_namespace(namespace)
    }

    /// Check if this event occurred within a time range
    pub fn occurred_between(&self, start: DateTime<Utc>, end: DateTime<Utc>) -> bool {
        self.timestamp >= start && self.timestamp <= end
    }

    /// Check if event occurred before a specific time
    pub fn occurred_before(&self, time: DateTime<Utc>) -> bool {
        self.timestamp < time
    }

    /// Check if event occurred after a specific time
    pub fn occurred_after(&self, time: DateTime<Utc>) -> bool {
        self.timestamp > time
    }
}

/// Keys per node of `std`'s `BTreeMap`, which `serde_json::Map` uses without
/// `preserve_order`. Decoding inserts keys in sorted order, and a full node
/// split by an insert at its right end keeps `BTREE_SPLIT_KEPT` keys, so every
/// node but the last holds that many.
const BTREE_NODE_KEYS: usize = 11;
const BTREE_NODE_EDGES: usize = 12;
const BTREE_NODE_HEADER: usize = 16;
const BTREE_SPLIT_KEPT: usize = 6;

/// Nodes holding `keys` keys inserted in order.
fn btree_nodes(keys: usize) -> usize {
    if keys <= BTREE_NODE_KEYS {
        1
    } else {
        1 + (keys - BTREE_NODE_KEYS).div_ceil(BTREE_SPLIT_KEPT + 1)
    }
}

fn btree_map_bytes(len: usize) -> usize {
    if len == 0 {
        return 0;
    }
    let leaf =
        BTREE_NODE_HEADER + BTREE_NODE_KEYS * std::mem::size_of::<(String, serde_json::Value)>();
    let internal = leaf + BTREE_NODE_EDGES * std::mem::size_of::<usize>();
    let mut nodes = btree_nodes(len);
    let mut total = nodes * leaf;
    while nodes > 1 {
        nodes = btree_nodes(nodes - 1);
        total += nodes * internal;
    }
    total
}

/// `serde_json::Map` is an `IndexMap` when any crate in the build enables
/// `serde_json/preserve_order`; datafusion does, so server builds use it.
static MAP_PRESERVES_ORDER: std::sync::LazyLock<bool> = std::sync::LazyLock::new(|| {
    let mut map = serde_json::Map::new();
    map.insert("b".into(), serde_json::Value::Null);
    map.insert("a".into(), serde_json::Value::Null);
    map.keys().next().is_some_and(|key| key == "b")
});

/// hashbrown's SIMD group width, the trailing control bytes of every table.
const HASHBROWN_GROUP_WIDTH: usize = if cfg!(any(target_arch = "x86", target_arch = "x86_64")) {
    16
} else {
    8
};

/// An `IndexMap<String, Value>` filled by inserts from empty: a hashbrown
/// table of `usize` indices and an entry `Vec` reserved to the table's
/// capacity, each entry a cached hash, the key and the value.
fn index_map_bytes(len: usize) -> usize {
    if len == 0 {
        return 0;
    }
    let mut buckets = 4;
    while bucket_capacity(buckets) < len {
        buckets *= 2;
    }
    let entry = std::mem::size_of::<usize>() + std::mem::size_of::<(String, serde_json::Value)>();
    let table = buckets * std::mem::size_of::<usize>() + buckets + HASHBROWN_GROUP_WIDTH;
    table + bucket_capacity(buckets) * entry
}

fn bucket_capacity(buckets: usize) -> usize {
    if buckets < 8 {
        buckets - 1
    } else {
        buckets / 8 * 7
    }
}

fn map_bytes(len: usize) -> usize {
    if *MAP_PRESERVES_ORDER {
        index_map_bytes(len)
    } else {
        btree_map_bytes(len)
    }
}

pub(crate) fn value_heap_bytes(value: &serde_json::Value) -> usize {
    use serde_json::Value;
    match value {
        Value::Null | Value::Bool(_) | Value::Number(_) => 0,
        Value::String(text) => text.capacity(),
        Value::Array(items) => {
            items.capacity() * std::mem::size_of::<Value>()
                + items.iter().map(value_heap_bytes).sum::<usize>()
        }
        Value::Object(map) => {
            map_bytes(map.len())
                + map
                    .iter()
                    .map(|(key, child)| key.capacity() + value_heap_bytes(child))
                    .sum::<usize>()
        }
    }
}

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

    fn test_event_type() -> EventType {
        EventType::new("user.created".to_string()).unwrap()
    }

    fn test_entity_id() -> EntityId {
        EntityId::new("user-123".to_string()).unwrap()
    }

    fn test_tenant_id() -> TenantId {
        TenantId::new("tenant-1".to_string()).unwrap()
    }

    #[test]
    fn test_estimated_size_bytes_grows_with_payload() {
        // Two events, identical except payload size — the larger
        // payload must produce a larger estimate. Exact bytes
        // aren't specified; ordering is.
        let small = Event::new(
            test_event_type(),
            test_entity_id(),
            test_tenant_id(),
            json!({"k": "v"}),
        );
        let large_payload = json!({"data": "x".repeat(10_000)});
        let large = Event::new(
            test_event_type(),
            test_entity_id(),
            test_tenant_id(),
            large_payload,
        );

        assert!(small.estimated_size_bytes() < large.estimated_size_bytes());
        // The fixed overhead floors a tiny event at 256+ bytes;
        // the large one with a 10k-char string must clear 10k.
        assert!(small.estimated_size_bytes() >= 256);
        assert!(large.estimated_size_bytes() > 10_000);
    }

    #[test]
    fn test_estimated_size_bytes_includes_metadata() {
        let no_meta = Event::new(
            test_event_type(),
            test_entity_id(),
            test_tenant_id(),
            json!({"k": "v"}),
        );
        let with_meta = Event::with_metadata(
            test_event_type(),
            test_entity_id(),
            test_tenant_id(),
            json!({"k": "v"}),
            json!({"correlation_id": "abc-123-def-456-very-long"}),
        );
        assert!(with_meta.estimated_size_bytes() > no_meta.estimated_size_bytes());
    }

    #[test]
    fn test_event_creation_with_value_objects() {
        let event = Event::new(
            test_event_type(),
            test_entity_id(),
            test_tenant_id(),
            json!({"name": "Alice"}),
        );

        assert_eq!(event.event_type_str(), "user.created");
        assert_eq!(event.entity_id_str(), "user-123");
        assert_eq!(event.tenant_id_str(), "tenant-1");
        assert_eq!(event.version(), 1);
    }

    #[test]
    fn test_event_creation_from_strings() {
        let event = Event::from_strings(
            "user.created".to_string(),
            "user-123".to_string(),
            "tenant-1".to_string(),
            json!({"name": "Alice"}),
            None,
        );

        assert!(event.is_ok());
        let event = event.unwrap();
        assert_eq!(event.event_type_str(), "user.created");
        assert_eq!(event.entity_id_str(), "user-123");
        assert_eq!(event.tenant_id_str(), "tenant-1");
    }

    #[test]
    fn test_event_with_metadata() {
        let event = Event::with_metadata(
            test_event_type(),
            test_entity_id(),
            test_tenant_id(),
            json!({"name": "Bob"}),
            json!({"source": "api"}),
        );

        assert!(event.metadata().is_some());
        assert_eq!(event.metadata().unwrap(), &json!({"source": "api"}));
    }

    #[test]
    fn test_event_with_default_tenant() {
        let event = Event::with_default_tenant(test_event_type(), test_entity_id(), json!({}));

        assert_eq!(event.tenant_id_str(), "default");
    }

    #[test]
    fn test_from_strings_validates_event_type() {
        // Invalid: uppercase
        let result = Event::from_strings(
            "User.Created".to_string(),
            "e1".to_string(),
            "t1".to_string(),
            json!({}),
            None,
        );
        assert!(result.is_err());

        // Invalid: empty
        let result = Event::from_strings(
            String::new(),
            "e1".to_string(),
            "t1".to_string(),
            json!({}),
            None,
        );
        assert!(result.is_err());
    }

    #[test]
    fn test_from_strings_validates_entity_id() {
        // Invalid: empty entity_id
        let result = Event::from_strings(
            "user.created".to_string(),
            String::new(),
            "t1".to_string(),
            json!({}),
            None,
        );
        assert!(result.is_err());
    }

    #[test]
    fn test_from_strings_validates_tenant_id() {
        // Invalid: empty tenant_id
        let result = Event::from_strings(
            "user.created".to_string(),
            "e1".to_string(),
            String::new(),
            json!({}),
            None,
        );
        assert!(result.is_err());
    }

    #[test]
    fn test_belongs_to_tenant() {
        let tenant1 = TenantId::new("tenant-1".to_string()).unwrap();
        let tenant2 = TenantId::new("tenant-2".to_string()).unwrap();

        let event = Event::new(
            test_event_type(),
            test_entity_id(),
            tenant1.clone(),
            json!({}),
        );

        assert!(event.belongs_to_tenant(&tenant1));
        assert!(!event.belongs_to_tenant(&tenant2));
    }

    #[test]
    fn test_belongs_to_tenant_str() {
        let event = Event::new(
            test_event_type(),
            test_entity_id(),
            test_tenant_id(),
            json!({}),
        );

        assert!(event.belongs_to_tenant_str("tenant-1"));
        assert!(!event.belongs_to_tenant_str("tenant-2"));
    }

    #[test]
    fn test_relates_to_entity() {
        let entity1 = EntityId::new("order-456".to_string()).unwrap();
        let entity2 = EntityId::new("order-789".to_string()).unwrap();

        let event = Event::new(
            EventType::new("order.placed".to_string()).unwrap(),
            entity1.clone(),
            test_tenant_id(),
            json!({}),
        );

        assert!(event.relates_to_entity(&entity1));
        assert!(!event.relates_to_entity(&entity2));
    }

    #[test]
    fn test_relates_to_entity_str() {
        let event = Event::new(
            EventType::new("order.placed".to_string()).unwrap(),
            EntityId::new("order-456".to_string()).unwrap(),
            test_tenant_id(),
            json!({}),
        );

        assert!(event.relates_to_entity_str("order-456"));
        assert!(!event.relates_to_entity_str("order-789"));
    }

    #[test]
    fn test_is_type() {
        let type1 = EventType::new("order.placed".to_string()).unwrap();
        let type2 = EventType::new("order.cancelled".to_string()).unwrap();

        let event = Event::new(type1.clone(), test_entity_id(), test_tenant_id(), json!({}));

        assert!(event.is_type(&type1));
        assert!(!event.is_type(&type2));
    }

    #[test]
    fn test_is_type_str() {
        let event = Event::new(
            EventType::new("order.placed".to_string()).unwrap(),
            test_entity_id(),
            test_tenant_id(),
            json!({}),
        );

        assert!(event.is_type_str("order.placed"));
        assert!(!event.is_type_str("order.cancelled"));
    }

    #[test]
    fn test_is_in_namespace() {
        let event = Event::new(
            EventType::new("order.placed".to_string()).unwrap(),
            test_entity_id(),
            test_tenant_id(),
            json!({}),
        );

        assert!(event.is_in_namespace("order"));
        assert!(!event.is_in_namespace("user"));
    }

    #[test]
    fn test_time_range_queries() {
        let event = Event::new(
            test_event_type(),
            test_entity_id(),
            test_tenant_id(),
            json!({}),
        );

        let past = Utc::now() - chrono::Duration::hours(1);
        let future = Utc::now() + chrono::Duration::hours(1);

        assert!(event.occurred_after(past));
        assert!(event.occurred_before(future));
        assert!(event.occurred_between(past, future));
    }

    #[test]
    fn test_serde_serialization() {
        let event = Event::new(
            test_event_type(),
            test_entity_id(),
            test_tenant_id(),
            json!({"test": "data"}),
        );

        // Should be able to serialize
        let json = serde_json::to_string(&event);
        assert!(json.is_ok());

        // Should be able to deserialize
        let deserialized = serde_json::from_str::<Event>(&json.unwrap());
        assert!(deserialized.is_ok());

        let deserialized = deserialized.unwrap();
        assert_eq!(deserialized.event_type_str(), "user.created");
        assert_eq!(deserialized.entity_id_str(), "user-123");
    }

    #[test]
    fn test_event_without_valid_time_keeps_pre_axis_wire_shape() {
        let event = Event::new(
            test_event_type(),
            test_entity_id(),
            test_tenant_id(),
            json!({}),
        );
        let wire = serde_json::to_string(&event).unwrap();
        assert!(!wire.contains("valid_from"));
        assert!(!wire.contains("valid_to"));

        let old: Event = serde_json::from_str(&wire).unwrap();
        assert_eq!(old.valid_from, None);
        assert_eq!(old.valid_to, None);
        assert_eq!(old.effective_valid_from(), old.timestamp());
    }

    #[test]
    fn test_with_valid_time_round_trips_through_serde() {
        let start = Utc::now() - chrono::Duration::days(2);
        let end = start + chrono::Duration::days(1);
        let event = Event::new(
            test_event_type(),
            test_entity_id(),
            test_tenant_id(),
            json!({}),
        )
        .with_valid_time(Some(start), Some(end))
        .unwrap();

        let wire = serde_json::to_string(&event).unwrap();
        let back: Event = serde_json::from_str(&wire).unwrap();
        assert_eq!(back.valid_from, Some(start));
        assert_eq!(back.valid_to, Some(end));
        assert_eq!(back.effective_valid_from(), start);
    }

    #[test]
    fn test_with_valid_time_accepts_open_ended_interval() {
        let start = Utc::now() - chrono::Duration::days(2);
        let event = Event::new(
            test_event_type(),
            test_entity_id(),
            test_tenant_id(),
            json!({}),
        )
        .with_valid_time(Some(start), None)
        .unwrap();

        assert_eq!(event.valid_from, Some(start));
        assert_eq!(event.valid_to, None);
    }

    #[test]
    fn test_with_valid_time_rejects_valid_to_not_after_valid_from() {
        let start = Utc::now() - chrono::Duration::days(2);
        let before = start - chrono::Duration::hours(1);

        for to in [start, before] {
            let result = Event::new(
                test_event_type(),
                test_entity_id(),
                test_tenant_id(),
                json!({}),
            )
            .with_valid_time(Some(start), Some(to));
            assert!(matches!(result, Err(AllSourceError::ValidationError(_))));
        }
    }

    #[test]
    fn test_with_valid_time_defaults_valid_from_to_record_time() {
        let past = Utc::now() - chrono::Duration::hours(1);
        let result = Event::new(
            test_event_type(),
            test_entity_id(),
            test_tenant_id(),
            json!({}),
        )
        .with_valid_time(None, Some(past));
        assert!(matches!(result, Err(AllSourceError::ValidationError(_))));
    }

    #[test]
    fn test_reconstruct_from_strings() {
        let id = Uuid::new_v4();
        let timestamp = Utc::now();

        let event = Event::reconstruct_from_strings(
            id,
            "order.placed".to_string(),
            "order-123".to_string(),
            "tenant-1".to_string(),
            json!({"amount": 100}),
            timestamp,
            Some(json!({"source": "api"})),
            1,
        );

        assert_eq!(event.id(), id);
        assert_eq!(event.event_type_str(), "order.placed");
        assert_eq!(event.entity_id_str(), "order-123");
        assert_eq!(event.tenant_id_str(), "tenant-1");
        assert_eq!(event.version(), 1);
    }
}