oxirs-stream 0.2.4

Real-time streaming support with Kafka/NATS/MQTT/OPC-UA I/O, RDF Patch, and SPARQL Update delta
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
//! Stream event filtering with composable predicates.
//!
//! Provides a `StreamEvent` type, a `FilterPredicate` enum with logical combinators
//! (And, Or, Not), and an `EventFilter` utility for applying predicates to event slices.

use std::collections::HashMap;

// ────────────────────────────────────────────────────────────────────────────
// StreamEvent
// ────────────────────────────────────────────────────────────────────────────

/// A stream event with a key, value, topic, timestamp, and optional headers.
#[derive(Debug, Clone, PartialEq)]
pub struct StreamEvent {
    /// The event key (partition routing hint or entity identifier).
    pub key: String,
    /// The event payload value.
    pub value: String,
    /// The topic (channel) this event was published on.
    pub topic: String,
    /// Timestamp in milliseconds since the Unix epoch.
    pub timestamp_ms: u64,
    /// Arbitrary key-value metadata headers.
    pub headers: HashMap<String, String>,
}

impl StreamEvent {
    /// Create a new event with no headers.
    pub fn new(key: &str, value: &str, topic: &str, timestamp_ms: u64) -> Self {
        Self {
            key: key.to_string(),
            value: value.to_string(),
            topic: topic.to_string(),
            timestamp_ms,
            headers: HashMap::new(),
        }
    }

    /// Builder-style method to add a header.
    pub fn with_header(
        mut self,
        header_key: impl Into<String>,
        header_val: impl Into<String>,
    ) -> Self {
        self.headers.insert(header_key.into(), header_val.into());
        self
    }
}

// ────────────────────────────────────────────────────────────────────────────
// FilterPredicate
// ────────────────────────────────────────────────────────────────────────────

/// A composable filter predicate for `StreamEvent` matching.
#[derive(Debug, Clone)]
pub enum FilterPredicate {
    /// Key must equal this exact string.
    KeyEquals(String),
    /// Value must contain this substring.
    ValueContains(String),
    /// Topic must match exactly.
    TopicIs(String),
    /// Timestamp must be greater than or equal to this value (inclusive lower bound).
    TimestampAfter(u64),
    /// Timestamp must be less than or equal to this value (inclusive upper bound).
    TimestampBefore(u64),
    /// A header with the given key must have the given value.
    HeaderMatches(String, String),
    /// Logical AND: both sub-predicates must match.
    And(Box<FilterPredicate>, Box<FilterPredicate>),
    /// Logical OR: at least one sub-predicate must match.
    Or(Box<FilterPredicate>, Box<FilterPredicate>),
    /// Logical NOT: the sub-predicate must not match.
    Not(Box<FilterPredicate>),
}

impl FilterPredicate {
    /// Evaluate this predicate against a single `StreamEvent`.
    pub fn matches(&self, event: &StreamEvent) -> bool {
        match self {
            FilterPredicate::KeyEquals(k) => &event.key == k,
            FilterPredicate::ValueContains(sub) => event.value.contains(sub.as_str()),
            FilterPredicate::TopicIs(t) => &event.topic == t,
            FilterPredicate::TimestampAfter(ts) => event.timestamp_ms >= *ts,
            FilterPredicate::TimestampBefore(ts) => event.timestamp_ms <= *ts,
            FilterPredicate::HeaderMatches(hk, hv) => event
                .headers
                .get(hk.as_str())
                .map(|v| v == hv)
                .unwrap_or(false),
            FilterPredicate::And(left, right) => left.matches(event) && right.matches(event),
            FilterPredicate::Or(left, right) => left.matches(event) || right.matches(event),
            FilterPredicate::Not(inner) => !inner.matches(event),
        }
    }

    /// Combine this predicate with another using logical AND.
    pub fn and(self, other: FilterPredicate) -> FilterPredicate {
        FilterPredicate::And(Box::new(self), Box::new(other))
    }

    /// Combine this predicate with another using logical OR.
    pub fn or(self, other: FilterPredicate) -> FilterPredicate {
        FilterPredicate::Or(Box::new(self), Box::new(other))
    }
}

impl std::ops::Not for FilterPredicate {
    type Output = FilterPredicate;

    /// Negate this predicate.
    fn not(self) -> FilterPredicate {
        FilterPredicate::Not(Box::new(self))
    }
}

// ────────────────────────────────────────────────────────────────────────────
// EventFilter
// ────────────────────────────────────────────────────────────────────────────

/// Applies `FilterPredicate`s to slices of `StreamEvent`s.
///
/// This is a stateless utility struct — all methods are pure functions.
#[derive(Debug, Clone, Default)]
pub struct EventFilter;

impl EventFilter {
    /// Create a new `EventFilter`.
    pub fn new() -> Self {
        Self
    }

    /// Return references to all events in `events` that satisfy `predicate`.
    pub fn filter<'a>(
        &self,
        events: &'a [StreamEvent],
        predicate: &FilterPredicate,
    ) -> Vec<&'a StreamEvent> {
        events.iter().filter(|e| predicate.matches(e)).collect()
    }

    /// Return the count of events that satisfy `predicate`.
    pub fn count(&self, events: &[StreamEvent], predicate: &FilterPredicate) -> usize {
        events.iter().filter(|e| predicate.matches(e)).count()
    }

    /// Return `true` if at least one event satisfies `predicate`.
    pub fn any(&self, events: &[StreamEvent], predicate: &FilterPredicate) -> bool {
        events.iter().any(|e| predicate.matches(e))
    }

    /// Return `true` if every event satisfies `predicate`.
    ///
    /// Returns `true` for an empty slice (vacuous truth).
    pub fn all(&self, events: &[StreamEvent], predicate: &FilterPredicate) -> bool {
        events.iter().all(|e| predicate.matches(e))
    }
}

// ────────────────────────────────────────────────────────────────────────────
// Tests
// ────────────────────────────────────────────────────────────────────────────

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

    // ── Helpers ──────────────────────────────────────────────────────────────

    fn make_event(key: &str, value: &str, topic: &str, ts: u64) -> StreamEvent {
        StreamEvent::new(key, value, topic, ts)
    }

    fn make_event_with_header(
        key: &str,
        value: &str,
        topic: &str,
        ts: u64,
        hk: &str,
        hv: &str,
    ) -> StreamEvent {
        StreamEvent::new(key, value, topic, ts).with_header(hk, hv)
    }

    fn sample_events() -> Vec<StreamEvent> {
        vec![
            make_event("k1", "hello world", "topic-a", 1000),
            make_event("k2", "foo bar", "topic-b", 2000),
            make_event("k1", "hello again", "topic-a", 3000),
            make_event("k3", "testing", "topic-c", 4000),
            make_event_with_header("k4", "header test", "topic-d", 5000, "x-type", "rdf"),
        ]
    }

    // ── StreamEvent construction ──────────────────────────────────────────

    #[test]
    fn test_stream_event_new_fields() {
        let e = StreamEvent::new("mykey", "myvalue", "mytopic", 9999);
        assert_eq!(e.key, "mykey");
        assert_eq!(e.value, "myvalue");
        assert_eq!(e.topic, "mytopic");
        assert_eq!(e.timestamp_ms, 9999);
        assert!(e.headers.is_empty());
    }

    #[test]
    fn test_stream_event_with_header() {
        let e = StreamEvent::new("k", "v", "t", 1).with_header("content-type", "json");
        assert_eq!(
            e.headers.get("content-type").map(|s| s.as_str()),
            Some("json")
        );
    }

    #[test]
    fn test_stream_event_multiple_headers() {
        let e = StreamEvent::new("k", "v", "t", 1)
            .with_header("a", "1")
            .with_header("b", "2");
        assert_eq!(e.headers.len(), 2);
    }

    #[test]
    fn test_stream_event_equality() {
        let e1 = make_event("k", "v", "t", 10);
        let e2 = make_event("k", "v", "t", 10);
        assert_eq!(e1, e2);
    }

    // ── KeyEquals predicate ───────────────────────────────────────────────

    #[test]
    fn test_key_equals_match() {
        let e = make_event("my-key", "val", "topic", 0);
        assert!(FilterPredicate::KeyEquals("my-key".to_string()).matches(&e));
    }

    #[test]
    fn test_key_equals_no_match() {
        let e = make_event("other-key", "val", "topic", 0);
        assert!(!FilterPredicate::KeyEquals("my-key".to_string()).matches(&e));
    }

    #[test]
    fn test_key_equals_case_sensitive() {
        let e = make_event("Key", "val", "topic", 0);
        assert!(!FilterPredicate::KeyEquals("key".to_string()).matches(&e));
    }

    // ── ValueContains predicate ───────────────────────────────────────────

    #[test]
    fn test_value_contains_match() {
        let e = make_event("k", "hello world", "t", 0);
        assert!(FilterPredicate::ValueContains("hello".to_string()).matches(&e));
    }

    #[test]
    fn test_value_contains_no_match() {
        let e = make_event("k", "hello world", "t", 0);
        assert!(!FilterPredicate::ValueContains("xyz".to_string()).matches(&e));
    }

    #[test]
    fn test_value_contains_exact_match() {
        let e = make_event("k", "exact", "t", 0);
        assert!(FilterPredicate::ValueContains("exact".to_string()).matches(&e));
    }

    #[test]
    fn test_value_contains_empty_substring() {
        // Empty substring always matches
        let e = make_event("k", "anything", "t", 0);
        assert!(FilterPredicate::ValueContains(String::new()).matches(&e));
    }

    // ── TopicIs predicate ─────────────────────────────────────────────────

    #[test]
    fn test_topic_is_match() {
        let e = make_event("k", "v", "events.rdf", 0);
        assert!(FilterPredicate::TopicIs("events.rdf".to_string()).matches(&e));
    }

    #[test]
    fn test_topic_is_no_match() {
        let e = make_event("k", "v", "events.rdf", 0);
        assert!(!FilterPredicate::TopicIs("other".to_string()).matches(&e));
    }

    // ── TimestampAfter predicate ──────────────────────────────────────────

    #[test]
    fn test_timestamp_after_match() {
        let e = make_event("k", "v", "t", 5000);
        assert!(FilterPredicate::TimestampAfter(4999).matches(&e));
    }

    #[test]
    fn test_timestamp_after_equal_is_match() {
        let e = make_event("k", "v", "t", 5000);
        assert!(FilterPredicate::TimestampAfter(5000).matches(&e));
    }

    #[test]
    fn test_timestamp_after_no_match() {
        let e = make_event("k", "v", "t", 3000);
        assert!(!FilterPredicate::TimestampAfter(5000).matches(&e));
    }

    // ── TimestampBefore predicate ─────────────────────────────────────────

    #[test]
    fn test_timestamp_before_match() {
        let e = make_event("k", "v", "t", 1000);
        assert!(FilterPredicate::TimestampBefore(2000).matches(&e));
    }

    #[test]
    fn test_timestamp_before_equal_is_match() {
        let e = make_event("k", "v", "t", 2000);
        assert!(FilterPredicate::TimestampBefore(2000).matches(&e));
    }

    #[test]
    fn test_timestamp_before_no_match() {
        let e = make_event("k", "v", "t", 9000);
        assert!(!FilterPredicate::TimestampBefore(2000).matches(&e));
    }

    // ── HeaderMatches predicate ───────────────────────────────────────────

    #[test]
    fn test_header_matches_match() {
        let e = make_event_with_header("k", "v", "t", 0, "x-source", "sensor-1");
        assert!(
            FilterPredicate::HeaderMatches("x-source".to_string(), "sensor-1".to_string())
                .matches(&e)
        );
    }

    #[test]
    fn test_header_matches_wrong_value() {
        let e = make_event_with_header("k", "v", "t", 0, "x-source", "sensor-1");
        assert!(
            !FilterPredicate::HeaderMatches("x-source".to_string(), "sensor-2".to_string())
                .matches(&e)
        );
    }

    #[test]
    fn test_header_matches_missing_key() {
        let e = make_event("k", "v", "t", 0);
        assert!(
            !FilterPredicate::HeaderMatches("x-source".to_string(), "anything".to_string())
                .matches(&e)
        );
    }

    // ── And combinator ────────────────────────────────────────────────────

    #[test]
    fn test_and_both_true() {
        let e = make_event("k1", "hello world", "topic-a", 1500);
        let pred = FilterPredicate::KeyEquals("k1".to_string())
            .and(FilterPredicate::ValueContains("hello".to_string()));
        assert!(pred.matches(&e));
    }

    #[test]
    fn test_and_first_false() {
        let e = make_event("k2", "hello world", "topic-a", 1500);
        let pred = FilterPredicate::KeyEquals("k1".to_string())
            .and(FilterPredicate::ValueContains("hello".to_string()));
        assert!(!pred.matches(&e));
    }

    #[test]
    fn test_and_second_false() {
        let e = make_event("k1", "goodbye world", "topic-a", 1500);
        let pred = FilterPredicate::KeyEquals("k1".to_string())
            .and(FilterPredicate::ValueContains("hello".to_string()));
        assert!(!pred.matches(&e));
    }

    #[test]
    fn test_and_both_false() {
        let e = make_event("k2", "goodbye world", "topic-a", 1500);
        let pred = FilterPredicate::KeyEquals("k1".to_string())
            .and(FilterPredicate::ValueContains("hello".to_string()));
        assert!(!pred.matches(&e));
    }

    // ── Or combinator ─────────────────────────────────────────────────────

    #[test]
    fn test_or_first_true() {
        let e = make_event("k1", "no match", "topic-a", 0);
        let pred = FilterPredicate::KeyEquals("k1".to_string())
            .or(FilterPredicate::ValueContains("hello".to_string()));
        assert!(pred.matches(&e));
    }

    #[test]
    fn test_or_second_true() {
        let e = make_event("k2", "hello there", "topic-a", 0);
        let pred = FilterPredicate::KeyEquals("k1".to_string())
            .or(FilterPredicate::ValueContains("hello".to_string()));
        assert!(pred.matches(&e));
    }

    #[test]
    fn test_or_both_false() {
        let e = make_event("k2", "nothing", "topic-a", 0);
        let pred = FilterPredicate::KeyEquals("k1".to_string())
            .or(FilterPredicate::ValueContains("hello".to_string()));
        assert!(!pred.matches(&e));
    }

    // ── Not combinator ────────────────────────────────────────────────────

    #[test]
    fn test_not_negates_match() {
        let e = make_event("k1", "v", "t", 0);
        let pred = FilterPredicate::KeyEquals("k1".to_string()).not();
        assert!(!pred.matches(&e));
    }

    #[test]
    fn test_not_negates_non_match() {
        let e = make_event("k2", "v", "t", 0);
        let pred = FilterPredicate::KeyEquals("k1".to_string()).not();
        assert!(pred.matches(&e));
    }

    // ── Nested combinators ────────────────────────────────────────────────

    #[test]
    fn test_nested_and_or() {
        // (key == "k1" OR key == "k2") AND timestamp >= 2000
        let e1 = make_event("k1", "v", "t", 2500);
        let e2 = make_event("k2", "v", "t", 1500);
        let e3 = make_event("k3", "v", "t", 3000);

        let pred = FilterPredicate::KeyEquals("k1".to_string())
            .or(FilterPredicate::KeyEquals("k2".to_string()))
            .and(FilterPredicate::TimestampAfter(2000));

        assert!(pred.matches(&e1)); // k1 AND ts >= 2000
        assert!(!pred.matches(&e2)); // k2 but ts < 2000
        assert!(!pred.matches(&e3)); // ts >= 2000 but key is k3
    }

    #[test]
    fn test_nested_not_and() {
        // NOT (topic == "topic-a") AND timestamp <= 3000
        let e_pass = make_event("k", "v", "topic-b", 2000);
        let e_fail_topic = make_event("k", "v", "topic-a", 2000);
        let e_fail_ts = make_event("k", "v", "topic-b", 5000);

        let pred = FilterPredicate::TopicIs("topic-a".to_string())
            .not()
            .and(FilterPredicate::TimestampBefore(3000));

        assert!(pred.matches(&e_pass));
        assert!(!pred.matches(&e_fail_topic));
        assert!(!pred.matches(&e_fail_ts));
    }

    #[test]
    fn test_triple_nested_predicate() {
        // key == "k1" AND (value contains "hello" OR header x-type == "rdf")
        let e1 = make_event("k1", "hello there", "t", 0);
        let e2 = make_event_with_header("k1", "data", "t", 0, "x-type", "rdf");
        let e3 = make_event("k1", "data", "t", 0); // no hello, no header
        let e4 = make_event("k2", "hello there", "t", 0); // wrong key

        let pred = FilterPredicate::KeyEquals("k1".to_string()).and(
            FilterPredicate::ValueContains("hello".to_string()).or(FilterPredicate::HeaderMatches(
                "x-type".to_string(),
                "rdf".to_string(),
            )),
        );

        assert!(pred.matches(&e1));
        assert!(pred.matches(&e2));
        assert!(!pred.matches(&e3));
        assert!(!pred.matches(&e4));
    }

    // ── EventFilter::filter ───────────────────────────────────────────────

    #[test]
    fn test_filter_returns_matching_subset() {
        let events = sample_events();
        let ef = EventFilter::new();
        let pred = FilterPredicate::TopicIs("topic-a".to_string());
        let result = ef.filter(&events, &pred);
        assert_eq!(result.len(), 2);
        assert!(result.iter().all(|e| e.topic == "topic-a"));
    }

    #[test]
    fn test_filter_empty_input() {
        let ef = EventFilter::new();
        let pred = FilterPredicate::KeyEquals("anything".to_string());
        let result = ef.filter(&[], &pred);
        assert!(result.is_empty());
    }

    #[test]
    fn test_filter_no_matches() {
        let events = sample_events();
        let ef = EventFilter::new();
        let pred = FilterPredicate::KeyEquals("nonexistent".to_string());
        let result = ef.filter(&events, &pred);
        assert!(result.is_empty());
    }

    #[test]
    fn test_filter_all_match() {
        let events = sample_events();
        let ef = EventFilter::new();
        let pred = FilterPredicate::TimestampAfter(0);
        let result = ef.filter(&events, &pred);
        assert_eq!(result.len(), events.len());
    }

    #[test]
    fn test_filter_returns_references_to_original() {
        let events = sample_events();
        let ef = EventFilter::new();
        let pred = FilterPredicate::KeyEquals("k1".to_string());
        let result = ef.filter(&events, &pred);
        assert_eq!(result.len(), 2);
        // Verify the references point into the original slice
        assert_eq!(result[0].key, "k1");
        assert_eq!(result[1].key, "k1");
    }

    // ── EventFilter::count ────────────────────────────────────────────────

    #[test]
    fn test_count_matching_events() {
        let events = sample_events();
        let ef = EventFilter::new();
        let pred = FilterPredicate::KeyEquals("k1".to_string());
        assert_eq!(ef.count(&events, &pred), 2);
    }

    #[test]
    fn test_count_empty_input() {
        let ef = EventFilter::new();
        let pred = FilterPredicate::KeyEquals("k".to_string());
        assert_eq!(ef.count(&[], &pred), 0);
    }

    #[test]
    fn test_count_zero_matches() {
        let events = sample_events();
        let ef = EventFilter::new();
        let pred = FilterPredicate::KeyEquals("ghost".to_string());
        assert_eq!(ef.count(&events, &pred), 0);
    }

    #[test]
    fn test_count_all_match() {
        let events = vec![
            make_event("k", "v", "t", 100),
            make_event("k", "v", "t", 200),
            make_event("k", "v", "t", 300),
        ];
        let ef = EventFilter::new();
        let pred = FilterPredicate::TopicIs("t".to_string());
        assert_eq!(ef.count(&events, &pred), 3);
    }

    // ── EventFilter::any ──────────────────────────────────────────────────

    #[test]
    fn test_any_true_when_one_matches() {
        let events = sample_events();
        let ef = EventFilter::new();
        let pred = FilterPredicate::TopicIs("topic-c".to_string());
        assert!(ef.any(&events, &pred));
    }

    #[test]
    fn test_any_false_when_none_match() {
        let events = sample_events();
        let ef = EventFilter::new();
        let pred = FilterPredicate::TopicIs("topic-z".to_string());
        assert!(!ef.any(&events, &pred));
    }

    #[test]
    fn test_any_empty_returns_false() {
        let ef = EventFilter::new();
        let pred = FilterPredicate::KeyEquals("k".to_string());
        assert!(!ef.any(&[], &pred));
    }

    // ── EventFilter::all ──────────────────────────────────────────────────

    #[test]
    fn test_all_true_when_all_match() {
        let events = vec![
            make_event("k", "hello", "t", 100),
            make_event("k", "hello there", "t", 200),
        ];
        let ef = EventFilter::new();
        let pred = FilterPredicate::ValueContains("hello".to_string());
        assert!(ef.all(&events, &pred));
    }

    #[test]
    fn test_all_false_when_one_fails() {
        let events = vec![
            make_event("k", "hello", "t", 100),
            make_event("k", "goodbye", "t", 200),
        ];
        let ef = EventFilter::new();
        let pred = FilterPredicate::ValueContains("hello".to_string());
        assert!(!ef.all(&events, &pred));
    }

    #[test]
    fn test_all_empty_returns_true() {
        let ef = EventFilter::new();
        let pred = FilterPredicate::KeyEquals("impossible".to_string());
        assert!(ef.all(&[], &pred));
    }

    // ── Timestamp window (After + Before combined) ────────────────────────

    #[test]
    fn test_timestamp_window_filter() {
        let events = sample_events(); // ts: 1000, 2000, 3000, 4000, 5000
        let ef = EventFilter::new();
        let pred =
            FilterPredicate::TimestampAfter(2000).and(FilterPredicate::TimestampBefore(4000));
        let result = ef.filter(&events, &pred);
        assert_eq!(result.len(), 3); // ts 2000, 3000, 4000
        for e in &result {
            assert!(e.timestamp_ms >= 2000 && e.timestamp_ms <= 4000);
        }
    }

    // ── Default impl ──────────────────────────────────────────────────────

    #[test]
    fn test_event_filter_default() {
        let ef = EventFilter;
        let events = vec![make_event("k", "v", "t", 0)];
        let pred = FilterPredicate::KeyEquals("k".to_string());
        assert_eq!(ef.count(&events, &pred), 1);
    }

    // ── Header with multiple entries ──────────────────────────────────────

    #[test]
    fn test_header_matches_with_multiple_headers() {
        let e = make_event_with_header("k", "v", "t", 0, "h1", "v1")
            .with_header("h2", "v2")
            .with_header("h3", "v3");
        assert!(FilterPredicate::HeaderMatches("h2".to_string(), "v2".to_string()).matches(&e));
        assert!(!FilterPredicate::HeaderMatches("h2".to_string(), "wrong".to_string()).matches(&e));
    }

    // ── TopicIs + KeyEquals combined ─────────────────────────────────────

    #[test]
    fn test_topic_and_key_combined() {
        let events = sample_events();
        let ef = EventFilter::new();
        let pred = FilterPredicate::TopicIs("topic-a".to_string())
            .and(FilterPredicate::KeyEquals("k1".to_string()));
        let result = ef.filter(&events, &pred);
        assert_eq!(result.len(), 2);
    }
}