rsigma-eval 0.12.0

Evaluator for Sigma detection and correlation rules — match rules against events
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
use super::*;
use crate::event::JsonEvent;
use serde_json::json;

#[test]
fn test_group_key_extract() {
    let v = json!({"User": "admin", "Host": "srv01"});
    let event = JsonEvent::borrow(&v);
    let group_by = vec![
        GroupByField::Direct("User".to_string()),
        GroupByField::Direct("Host".to_string()),
    ];
    let key = GroupKey::extract(&event, &group_by, &["rule1"]);
    assert_eq!(
        key.0,
        vec![Some("admin".to_string()), Some("srv01".to_string())]
    );
}

#[test]
fn test_group_key_missing_field() {
    let v = json!({"User": "admin"});
    let event = JsonEvent::borrow(&v);
    let group_by = vec![
        GroupByField::Direct("User".to_string()),
        GroupByField::Direct("Host".to_string()),
    ];
    let key = GroupKey::extract(&event, &group_by, &["rule1"]);
    assert_eq!(key.0, vec![Some("admin".to_string()), None]);
}

#[test]
fn test_group_key_aliased() {
    let v = json!({"source.ip": "10.0.0.1"});
    let event = JsonEvent::borrow(&v);
    let group_by = vec![GroupByField::Aliased {
        alias: "internal_ip".to_string(),
        mapping: HashMap::from([
            ("rule_a".to_string(), "source.ip".to_string()),
            ("rule_b".to_string(), "destination.ip".to_string()),
        ]),
    }];
    let key = GroupKey::extract(&event, &group_by, &["rule_a"]);
    assert_eq!(key.0, vec![Some("10.0.0.1".to_string())]);
}

#[test]
fn test_condition_check() {
    let cond = CompiledCondition {
        field: None,
        predicates: vec![(ConditionOperator::Gte, 100.0)],
        percentile: None,
    };
    assert!(!cond.check(99.0));
    assert!(cond.check(100.0));
    assert!(cond.check(101.0));
}

#[test]
fn test_condition_check_range() {
    let cond = CompiledCondition {
        field: None,
        predicates: vec![
            (ConditionOperator::Gt, 100.0),
            (ConditionOperator::Lte, 200.0),
        ],
        percentile: None,
    };
    assert!(!cond.check(100.0));
    assert!(cond.check(101.0));
    assert!(cond.check(200.0));
    assert!(!cond.check(201.0));
}

#[test]
fn test_window_event_count() {
    let mut state = WindowState::new_for(CorrelationType::EventCount);
    for i in 0..5 {
        state.push_event_count(1000 + i);
    }
    let cond = CompiledCondition {
        field: None,
        predicates: vec![(ConditionOperator::Gte, 5.0)],
        percentile: None,
    };
    assert_eq!(
        state.check_condition(&cond, CorrelationType::EventCount, &[], None),
        Some(5.0)
    );
}

#[test]
fn test_window_event_count_eviction() {
    let mut state = WindowState::new_for(CorrelationType::EventCount);
    for i in 0..10 {
        state.push_event_count(1000 + i);
    }
    // Evict events before ts=1005
    state.evict(1005);
    let cond = CompiledCondition {
        field: None,
        predicates: vec![(ConditionOperator::Gte, 5.0)],
        percentile: None,
    };
    assert_eq!(
        state.check_condition(&cond, CorrelationType::EventCount, &[], None),
        Some(5.0)
    );
}

#[test]
fn test_window_value_count() {
    let mut state = WindowState::new_for(CorrelationType::ValueCount);
    state.push_value_count(1000, "user1".to_string());
    state.push_value_count(1001, "user2".to_string());
    state.push_value_count(1002, "user1".to_string()); // duplicate
    state.push_value_count(1003, "user3".to_string());

    let cond = CompiledCondition {
        field: Some(vec!["User".to_string()]),
        predicates: vec![(ConditionOperator::Gte, 3.0)],
        percentile: None,
    };
    assert_eq!(
        state.check_condition(&cond, CorrelationType::ValueCount, &[], None),
        Some(3.0)
    );
}

#[test]
fn test_window_temporal() {
    let refs = vec!["rule_a".to_string(), "rule_b".to_string()];
    let mut state = WindowState::new_for(CorrelationType::Temporal);
    state.push_temporal(1000, "rule_a");
    // Only rule_a fired — condition: all refs must fire
    let cond = CompiledCondition {
        field: None,
        predicates: vec![(ConditionOperator::Gte, 2.0)],
        percentile: None,
    };
    assert!(
        state
            .check_condition(&cond, CorrelationType::Temporal, &refs, None)
            .is_none()
    );

    // Now rule_b fires too
    state.push_temporal(1001, "rule_b");
    assert_eq!(
        state.check_condition(&cond, CorrelationType::Temporal, &refs, None),
        Some(2.0)
    );
}

#[test]
fn test_window_temporal_ordered() {
    let refs = vec![
        "rule_a".to_string(),
        "rule_b".to_string(),
        "rule_c".to_string(),
    ];
    let mut state = WindowState::new_for(CorrelationType::TemporalOrdered);
    // Fire in order: a, b, c
    state.push_temporal(1000, "rule_a");
    state.push_temporal(1001, "rule_b");
    state.push_temporal(1002, "rule_c");

    let cond = CompiledCondition {
        field: None,
        predicates: vec![(ConditionOperator::Gte, 3.0)],
        percentile: None,
    };
    assert!(
        state
            .check_condition(&cond, CorrelationType::TemporalOrdered, &refs, None)
            .is_some()
    );
}

#[test]
fn test_window_temporal_ordered_wrong_order() {
    let refs = vec!["rule_a".to_string(), "rule_b".to_string()];
    let mut state = WindowState::new_for(CorrelationType::TemporalOrdered);
    // Fire in wrong order: b before a
    state.push_temporal(1000, "rule_b");
    state.push_temporal(1001, "rule_a");

    let cond = CompiledCondition {
        field: None,
        predicates: vec![(ConditionOperator::Gte, 2.0)],
        percentile: None,
    };
    assert!(
        state
            .check_condition(&cond, CorrelationType::TemporalOrdered, &refs, None)
            .is_none()
    );
}

#[test]
fn test_window_value_sum() {
    let mut state = WindowState::new_for(CorrelationType::ValueSum);
    state.push_numeric(1000, 500.0);
    state.push_numeric(1001, 600.0);

    let cond = CompiledCondition {
        field: Some(vec!["bytes_sent".to_string()]),
        predicates: vec![(ConditionOperator::Gt, 1000.0)],
        percentile: None,
    };
    assert_eq!(
        state.check_condition(&cond, CorrelationType::ValueSum, &[], None),
        Some(1100.0)
    );
}

#[test]
fn test_window_value_avg() {
    let mut state = WindowState::new_for(CorrelationType::ValueAvg);
    state.push_numeric(1000, 100.0);
    state.push_numeric(1001, 200.0);
    state.push_numeric(1002, 300.0);

    let cond = CompiledCondition {
        field: Some(vec!["bytes".to_string()]),
        predicates: vec![(ConditionOperator::Gte, 200.0)],
        percentile: None,
    };
    assert_eq!(
        state.check_condition(&cond, CorrelationType::ValueAvg, &[], None),
        Some(200.0)
    );
}

#[test]
fn test_window_value_median() {
    let mut state = WindowState::new_for(CorrelationType::ValueMedian);
    state.push_numeric(1000, 10.0);
    state.push_numeric(1001, 20.0);
    state.push_numeric(1002, 30.0);

    let cond = CompiledCondition {
        field: Some(vec!["latency".to_string()]),
        predicates: vec![(ConditionOperator::Gte, 20.0)],
        percentile: None,
    };
    assert_eq!(
        state.check_condition(&cond, CorrelationType::ValueMedian, &[], None),
        Some(20.0)
    );
}

#[test]
fn test_compile_correlation_basic() {
    use rsigma_parser::parse_sigma_yaml;

    let yaml = r#"
title: Base Rule
id: f305fd62-beca-47da-ad95-7690a0620084
logsource:
    product: aws
    service: cloudtrail
detection:
    selection:
        eventSource: "s3.amazonaws.com"
    condition: selection
level: low
---
title: Multiple AWS bucket enumerations
id: be246094-01d3-4bba-88de-69e582eba0cc
status: experimental
correlation:
    type: event_count
    rules:
        - f305fd62-beca-47da-ad95-7690a0620084
    group-by:
        - userIdentity.arn
    timespan: 1h
    condition:
        gte: 100
level: high
"#;
    let collection = parse_sigma_yaml(yaml).unwrap();
    assert_eq!(collection.correlations.len(), 1);

    let compiled = compile_correlation(&collection.correlations[0]).unwrap();
    assert_eq!(compiled.correlation_type, CorrelationType::EventCount);
    assert_eq!(compiled.timespan_secs, 3600);
    assert_eq!(compiled.rule_refs.len(), 1);
    assert_eq!(compiled.group_by.len(), 1);
    assert!(compiled.condition.check(100.0));
    assert!(!compiled.condition.check(99.0));
}

// =========================================================================
// Extended temporal condition tests
// =========================================================================

#[test]
fn test_eval_temporal_expr_and() {
    let mut rule_hits = HashMap::new();
    rule_hits.insert("rule_a".to_string(), VecDeque::from([1000]));
    rule_hits.insert("rule_b".to_string(), VecDeque::from([1001]));

    let expr = ConditionExpr::And(vec![
        ConditionExpr::Identifier("rule_a".to_string()),
        ConditionExpr::Identifier("rule_b".to_string()),
    ]);
    assert!(eval_temporal_expr(&expr, &rule_hits));
}

#[test]
fn test_eval_temporal_expr_and_incomplete() {
    let mut rule_hits = HashMap::new();
    rule_hits.insert("rule_a".to_string(), VecDeque::from([1000]));
    // rule_b not fired

    let expr = ConditionExpr::And(vec![
        ConditionExpr::Identifier("rule_a".to_string()),
        ConditionExpr::Identifier("rule_b".to_string()),
    ]);
    assert!(!eval_temporal_expr(&expr, &rule_hits));
}

#[test]
fn test_eval_temporal_expr_or() {
    let mut rule_hits = HashMap::new();
    rule_hits.insert("rule_a".to_string(), VecDeque::from([1000]));

    let expr = ConditionExpr::Or(vec![
        ConditionExpr::Identifier("rule_a".to_string()),
        ConditionExpr::Identifier("rule_b".to_string()),
    ]);
    assert!(eval_temporal_expr(&expr, &rule_hits));
}

#[test]
fn test_eval_temporal_expr_not() {
    let rule_hits = HashMap::new();

    let expr = ConditionExpr::Not(Box::new(ConditionExpr::Identifier("rule_a".to_string())));
    assert!(eval_temporal_expr(&expr, &rule_hits));
}

#[test]
fn test_eval_temporal_expr_complex() {
    let mut rule_hits = HashMap::new();
    rule_hits.insert("rule_a".to_string(), VecDeque::from([1000]));
    rule_hits.insert("rule_b".to_string(), VecDeque::from([1001]));
    // rule_c NOT fired

    // (rule_a and rule_b) and not rule_c
    let expr = ConditionExpr::And(vec![
        ConditionExpr::And(vec![
            ConditionExpr::Identifier("rule_a".to_string()),
            ConditionExpr::Identifier("rule_b".to_string()),
        ]),
        ConditionExpr::Not(Box::new(ConditionExpr::Identifier("rule_c".to_string()))),
    ]);
    assert!(eval_temporal_expr(&expr, &rule_hits));
}

#[test]
fn test_check_condition_with_extended_expr() {
    let refs = vec!["rule_a".to_string(), "rule_b".to_string()];
    let mut state = WindowState::new_for(CorrelationType::Temporal);
    state.push_temporal(1000, "rule_a");
    state.push_temporal(1001, "rule_b");

    let cond = CompiledCondition {
        field: None,
        predicates: vec![(ConditionOperator::Gte, 1.0)],
        percentile: None,
    };
    let expr = ConditionExpr::And(vec![
        ConditionExpr::Identifier("rule_a".to_string()),
        ConditionExpr::Identifier("rule_b".to_string()),
    ]);

    // With expression: should match (both rules fired)
    assert!(
        state
            .check_condition(&cond, CorrelationType::Temporal, &refs, Some(&expr))
            .is_some()
    );

    // Now test with only rule_a: expression should fail
    let mut state2 = WindowState::new_for(CorrelationType::Temporal);
    state2.push_temporal(1000, "rule_a");
    assert!(
        state2
            .check_condition(&cond, CorrelationType::Temporal, &refs, Some(&expr))
            .is_none()
    );
}

// =========================================================================
// Percentile linear interpolation tests
// =========================================================================

#[test]
fn test_percentile_linear_interp_single() {
    assert!((percentile_linear_interp(&[42.0], 50.0) - 42.0).abs() < f64::EPSILON);
}

#[test]
fn test_percentile_linear_interp_basic() {
    // Values: [1, 2, 3, 4, 5]
    let values = &[1.0, 2.0, 3.0, 4.0, 5.0];
    // 0th percentile = 1.0
    assert!((percentile_linear_interp(values, 0.0) - 1.0).abs() < f64::EPSILON);
    // 25th percentile = 2.0
    assert!((percentile_linear_interp(values, 25.0) - 2.0).abs() < f64::EPSILON);
    // 50th percentile = 3.0
    assert!((percentile_linear_interp(values, 50.0) - 3.0).abs() < f64::EPSILON);
    // 75th percentile = 4.0
    assert!((percentile_linear_interp(values, 75.0) - 4.0).abs() < f64::EPSILON);
    // 100th percentile = 5.0
    assert!((percentile_linear_interp(values, 100.0) - 5.0).abs() < f64::EPSILON);
}

#[test]
fn test_percentile_linear_interp_interpolation() {
    // Values: [10, 20, 30, 40]
    let values = &[10.0, 20.0, 30.0, 40.0];
    // 50th percentile: rank = 0.5 * 3 = 1.5, interp between 20 and 30 = 25
    assert!((percentile_linear_interp(values, 50.0) - 25.0).abs() < f64::EPSILON);
}

#[test]
fn test_percentile_linear_interp_1st_percentile() {
    // Values: [1, 2, 3, ..., 100]
    let values: Vec<f64> = (1..=100).map(|x| x as f64).collect();
    // 1st percentile = 1.0 + 0.01 * 99 * (2.0 - 1.0) ~ 1.99
    let p1 = percentile_linear_interp(&values, 1.0);
    assert!((p1 - 1.99).abs() < 0.01);
}

#[test]
fn test_value_percentile_check_condition() {
    let mut state = WindowState::new_for(CorrelationType::ValuePercentile);
    // Push 100 values: 1.0, 2.0, ..., 100.0
    for i in 1..=100 {
        state.push_numeric(1000 + i, i as f64);
    }

    let cond = CompiledCondition {
        field: Some(vec!["latency".to_string()]),
        predicates: vec![(ConditionOperator::Lte, 50.0)],
        percentile: None,
    };
    // 50th percentile of 1..100 should be ~50.5
    let result = state.check_condition(&cond, CorrelationType::ValuePercentile, &[], None);
    assert!(result.is_some());
    let val = result.unwrap();
    assert!((val - 50.5).abs() < 1.0, "expected ~50.5, got {val}");
}

#[test]
fn test_percentile_0th_and_100th() {
    let values = &[5.0, 10.0, 15.0, 20.0];
    assert!((percentile_linear_interp(values, 0.0) - 5.0).abs() < f64::EPSILON);
    assert!((percentile_linear_interp(values, 100.0) - 20.0).abs() < f64::EPSILON);
}

#[test]
fn test_percentile_two_values() {
    let values = &[10.0, 20.0];
    // 50th percentile between 10 and 20 = 15
    assert!((percentile_linear_interp(values, 50.0) - 15.0).abs() < f64::EPSILON);
    // 25th percentile = 12.5
    assert!((percentile_linear_interp(values, 25.0) - 12.5).abs() < f64::EPSILON);
}

#[test]
fn test_percentile_clamps_out_of_range() {
    let values = &[1.0, 2.0, 3.0];
    // Negative percentile clamps to 0
    assert!((percentile_linear_interp(values, -10.0) - 1.0).abs() < f64::EPSILON);
    // > 100 clamps to 100
    assert!((percentile_linear_interp(values, 150.0) - 3.0).abs() < f64::EPSILON);
}

#[test]
fn test_value_percentile_empty_window() {
    let state = WindowState::new_for(CorrelationType::ValuePercentile);
    let cond = CompiledCondition {
        field: Some(vec!["latency".to_string()]),
        predicates: vec![(ConditionOperator::Lte, 50.0)],
        percentile: None,
    };
    // Empty window should return None
    assert!(
        state
            .check_condition(&cond, CorrelationType::ValuePercentile, &[], None)
            .is_none()
    );
}

#[test]
fn test_extended_temporal_or_single_rule() {
    // "rule_a or rule_b" — only rule_a fired
    let mut rule_hits = HashMap::new();
    rule_hits.insert("rule_a".to_string(), VecDeque::from([1000]));

    let expr = ConditionExpr::Or(vec![
        ConditionExpr::Identifier("rule_a".to_string()),
        ConditionExpr::Identifier("rule_b".to_string()),
    ]);
    assert!(eval_temporal_expr(&expr, &rule_hits));
}

#[test]
fn test_extended_temporal_empty_hits() {
    let rule_hits = HashMap::new();

    // "rule_a and rule_b" — nothing fired
    let expr = ConditionExpr::And(vec![
        ConditionExpr::Identifier("rule_a".to_string()),
        ConditionExpr::Identifier("rule_b".to_string()),
    ]);
    assert!(!eval_temporal_expr(&expr, &rule_hits));

    // "rule_a or rule_b" — nothing fired
    let expr_or = ConditionExpr::Or(vec![
        ConditionExpr::Identifier("rule_a".to_string()),
        ConditionExpr::Identifier("rule_b".to_string()),
    ]);
    assert!(!eval_temporal_expr(&expr_or, &rule_hits));
}

#[test]
fn test_extended_temporal_with_empty_deque() {
    // Rule exists in map but with empty deque (all evicted)
    let mut rule_hits = HashMap::new();
    rule_hits.insert("rule_a".to_string(), VecDeque::new());
    rule_hits.insert("rule_b".to_string(), VecDeque::from([1000]));

    let expr = ConditionExpr::And(vec![
        ConditionExpr::Identifier("rule_a".to_string()),
        ConditionExpr::Identifier("rule_b".to_string()),
    ]);
    // rule_a has empty deque — should be treated as not fired
    assert!(!eval_temporal_expr(&expr, &rule_hits));
}

#[test]
fn test_check_condition_temporal_no_extended_expr() {
    // Standard temporal without extended expr: uses threshold count
    let refs = vec![
        "rule_a".to_string(),
        "rule_b".to_string(),
        "rule_c".to_string(),
    ];
    let mut state = WindowState::new_for(CorrelationType::Temporal);
    state.push_temporal(1000, "rule_a");
    state.push_temporal(1001, "rule_b");

    // Threshold: at least 2 rules must fire
    let cond = CompiledCondition {
        field: None,
        predicates: vec![(ConditionOperator::Gte, 2.0)],
        percentile: None,
    };
    // Without extended expr: 2 of 3 rules fired, meets gte 2
    assert_eq!(
        state.check_condition(&cond, CorrelationType::Temporal, &refs, None),
        Some(2.0)
    );

    // With threshold 3: not enough
    let cond3 = CompiledCondition {
        field: None,
        predicates: vec![(ConditionOperator::Gte, 3.0)],
        percentile: None,
    };
    assert!(
        state
            .check_condition(&cond3, CorrelationType::Temporal, &refs, None)
            .is_none()
    );
}

// =========================================================================
// EventBuffer tests
// =========================================================================

#[test]
fn test_event_buffer_push_and_decompress() {
    let mut buf = EventBuffer::new(10);
    let event = json!({"User": "admin", "action": "login", "src_ip": "10.0.0.1"});
    buf.push(1000, &event);

    assert_eq!(buf.len(), 1);
    assert!(!buf.is_empty());

    let events = buf.decompress_all();
    assert_eq!(events.len(), 1);
    assert_eq!(events[0], event);
}

#[test]
fn test_event_buffer_compression_saves_memory() {
    let mut buf = EventBuffer::new(100);
    // Push a realistic-sized event (~500 bytes JSON)
    let event = json!({
        "User": "admin",
        "action": "login",
        "src_ip": "192.168.1.100",
        "dst_ip": "10.0.0.1",
        "EventTime": "2024-07-10T12:30:00Z",
        "process": "sshd",
        "host": "production-server-01.example.com",
        "message": "Accepted password for admin from 192.168.1.100 port 22 ssh2",
        "severity": "info",
        "tags": ["authentication", "network", "linux"]
    });

    let raw_size = serde_json::to_vec(&event).unwrap().len();
    buf.push(1000, &event);
    let compressed_size = buf.compressed_bytes();

    // Compressed should be notably smaller than raw
    assert!(
        compressed_size < raw_size,
        "Compressed {compressed_size}B should be less than raw {raw_size}B"
    );

    // Verify roundtrip
    let events = buf.decompress_all();
    assert_eq!(events[0], event);
}

#[test]
fn test_event_buffer_max_events_cap() {
    let mut buf = EventBuffer::new(3);

    for i in 0..5 {
        buf.push(1000 + i, &json!({"idx": i}));
    }

    // Only the last 3 should remain
    assert_eq!(buf.len(), 3);
    let events = buf.decompress_all();
    assert_eq!(events[0], json!({"idx": 2}));
    assert_eq!(events[1], json!({"idx": 3}));
    assert_eq!(events[2], json!({"idx": 4}));
}

#[test]
fn test_event_buffer_eviction() {
    let mut buf = EventBuffer::new(10);
    for i in 0..5 {
        buf.push(1000 + i, &json!({"idx": i}));
    }
    assert_eq!(buf.len(), 5);

    // Evict everything before ts 1003
    buf.evict(1003);
    assert_eq!(buf.len(), 2);

    let events = buf.decompress_all();
    assert_eq!(events[0], json!({"idx": 3}));
    assert_eq!(events[1], json!({"idx": 4}));
}

#[test]
fn test_event_buffer_clear() {
    let mut buf = EventBuffer::new(10);
    buf.push(1000, &json!({"a": 1}));
    buf.push(1001, &json!({"b": 2}));
    assert_eq!(buf.len(), 2);

    buf.clear();
    assert!(buf.is_empty());
    assert_eq!(buf.len(), 0);
    assert_eq!(buf.compressed_bytes(), 0);
}

#[test]
fn test_compress_decompress_roundtrip() {
    // Test various JSON shapes
    let values = vec![
        json!(null),
        json!(42),
        json!("hello world"),
        json!({"nested": {"deep": [1, 2, 3]}}),
        json!([1, "two", null, true, {"five": 5}]),
    ];
    for val in values {
        let compressed = compress_event(&val).unwrap();
        let decompressed = decompress_event(&compressed).unwrap();
        assert_eq!(decompressed, val, "Roundtrip failed for {val}");
    }
}

// =========================================================================
// EventRefBuffer tests
// =========================================================================

#[test]
fn test_event_ref_buffer_push_and_refs() {
    let mut buf = EventRefBuffer::new(10);
    buf.push(1000, &json!({"id": "evt-1", "data": "hello"}));
    buf.push(1001, &json!({"_id": 42, "data": "world"}));
    buf.push(1002, &json!({"data": "no-id"}));

    assert_eq!(buf.len(), 3);
    let refs = buf.refs();
    assert_eq!(refs[0].timestamp, 1000);
    assert_eq!(refs[0].id, Some("evt-1".to_string()));
    assert_eq!(refs[1].timestamp, 1001);
    assert_eq!(refs[1].id, Some("42".to_string()));
    assert_eq!(refs[2].timestamp, 1002);
    assert_eq!(refs[2].id, None);
}

#[test]
fn test_event_ref_buffer_max_cap() {
    let mut buf = EventRefBuffer::new(3);
    for i in 0..5 {
        buf.push(1000 + i, &json!({"id": format!("e-{i}")}));
    }
    assert_eq!(buf.len(), 3);
    let refs = buf.refs();
    assert_eq!(refs[0].id, Some("e-2".to_string()));
    assert_eq!(refs[1].id, Some("e-3".to_string()));
    assert_eq!(refs[2].id, Some("e-4".to_string()));
}

#[test]
fn test_event_ref_buffer_eviction() {
    let mut buf = EventRefBuffer::new(10);
    for i in 0..5 {
        buf.push(1000 + i, &json!({"id": format!("e-{i}")}));
    }
    buf.evict(1003);
    assert_eq!(buf.len(), 2);
    let refs = buf.refs();
    assert_eq!(refs[0].timestamp, 1003);
    assert_eq!(refs[1].timestamp, 1004);
}

#[test]
fn test_event_ref_buffer_clear() {
    let mut buf = EventRefBuffer::new(10);
    buf.push(1000, &json!({"id": "a"}));
    buf.push(1001, &json!({"id": "b"}));
    assert_eq!(buf.len(), 2);

    buf.clear();
    assert!(buf.is_empty());
    assert_eq!(buf.len(), 0);
}

#[test]
fn test_extract_event_id_common_fields() {
    assert_eq!(
        extract_event_id(&json!({"id": "abc"})),
        Some("abc".to_string())
    );
    assert_eq!(
        extract_event_id(&json!({"_id": 123})),
        Some("123".to_string())
    );
    assert_eq!(
        extract_event_id(&json!({"event_id": "x-1"})),
        Some("x-1".to_string())
    );
    assert_eq!(
        extract_event_id(&json!({"EventRecordID": 999})),
        Some("999".to_string())
    );
    assert_eq!(extract_event_id(&json!({"no_id_field": true})), None);
}

#[test]
fn test_compile_correlation_with_custom_attributes() {
    use rsigma_parser::*;

    let mut custom_attributes: HashMap<String, yaml_serde::Value> =
        std::collections::HashMap::new();
    custom_attributes.insert(
        "rsigma.correlation_event_mode".to_string(),
        yaml_serde::Value::String("refs".to_string()),
    );
    custom_attributes.insert(
        "rsigma.max_correlation_events".to_string(),
        yaml_serde::Value::String("25".to_string()),
    );
    custom_attributes.insert(
        "rsigma.suppress".to_string(),
        yaml_serde::Value::String("5m".to_string()),
    );
    custom_attributes.insert(
        "rsigma.action".to_string(),
        yaml_serde::Value::String("reset".to_string()),
    );

    let rule = CorrelationRule {
        title: "Test Corr".to_string(),
        id: Some("corr-1".to_string()),
        name: None,
        status: None,
        description: None,
        author: None,
        date: None,
        modified: None,
        related: vec![],
        references: vec![],
        taxonomy: None,
        license: None,
        tags: vec![],
        fields: vec![],
        falsepositives: vec![],
        level: Some(Level::High),
        scope: vec![],
        correlation_type: CorrelationType::EventCount,
        rules: vec!["rule-1".to_string()],
        group_by: vec!["User".to_string()],
        timespan: Timespan::parse("60s").unwrap(),
        condition: CorrelationCondition::Threshold {
            predicates: vec![(ConditionOperator::Gte, 5)],
            field: None,
            percentile: None,
        },
        aliases: vec![],
        generate: false,
        custom_attributes,
    };

    let compiled = compile_correlation(&rule).unwrap();

    // Per-correlation overrides should be resolved from custom_attributes
    assert_eq!(
        compiled.event_mode,
        Some(crate::correlation_engine::CorrelationEventMode::Refs)
    );
    assert_eq!(compiled.max_events, Some(25));
    assert_eq!(compiled.suppress_secs, Some(300)); // 5m = 300s
    assert_eq!(
        compiled.action,
        Some(crate::correlation_engine::CorrelationAction::Reset)
    );
}