reddb-io-server 1.2.0

RedDB server-side engine: storage, runtime, replication, MCP, AI, and the gRPC/HTTP/RedWire/PG-wire dispatchers. Re-exported by the umbrella `reddb` crate.
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
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
//! Config-driven per-variant routing for [`OperatorEvent`].
//!
//! See `docs/operations/logging.md` § "Operator event routing" for the TOML
//! config schema and examples.
//!
//! # Routing resolution order
//!
//! 1. Per-variant block (`variant_routes["AuthBypass"]`) — most specific.
//! 2. `default_handlers` in config — user-supplied default.
//! 3. Code default `["audit_log", "tracing"]` — zero upgrade burden.
//!
//! Empty config = identical to the current `OperatorEvent::emit()` behaviour.

use std::collections::{HashMap, VecDeque};
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Condvar, Mutex};
use std::thread;
use std::time::{Duration, Instant};

use super::operator_event::OperatorEvent;
use crate::runtime::audit_log::{AuditAuthSource, AuditEvent, AuditLogger, Outcome};

// ---------------------------------------------------------------------------
// Validation helpers
// ---------------------------------------------------------------------------

const KNOWN_HANDLERS: &[&str] = &[
    "audit_log",
    "tracing",
    "stderr",
    "pagerduty",
    "generic_webhook",
];

fn closest_match(input: &str, candidates: &[&str]) -> Option<String> {
    candidates
        .iter()
        .map(|c| (*c, strsim::levenshtein(input, c)))
        .min_by_key(|(_, d)| *d)
        .filter(|(_, d)| *d <= 4)
        .map(|(c, _)| c.to_string())
}

fn validate_handler_names(names: &[String]) -> Result<(), ConfigError> {
    for name in names {
        if !KNOWN_HANDLERS.contains(&name.as_str()) {
            return Err(ConfigError::UnknownHandler { key: name.clone() });
        }
    }
    Ok(())
}

// ---------------------------------------------------------------------------
// Config types
// ---------------------------------------------------------------------------

/// Token-bucket rate limit for a webhook handler.
#[derive(Debug, Clone, Default)]
pub struct RateLimitConfig {
    /// Number of requests allowed per `window_sec`.
    pub requests: u32,
    pub window_sec: u64,
}

/// Config for a webhook handler (PagerDuty or generic).
#[derive(Debug, Clone)]
pub struct WebhookHandlerConfig {
    pub url: String,
    /// Name of the environment variable holding the bearer token.
    /// Resolved at router construction time; boot fails if unset.
    pub auth_env: String,
    pub rate_limit: Option<RateLimitConfig>,
}

/// Full router configuration (`[telemetry.operator_event]` in TOML).
#[derive(Debug, Default)]
pub struct RouterConfig {
    /// Handler list applied when no per-variant route matches.
    /// `None` → code default `["audit_log", "tracing"]`.
    pub default_handlers: Option<Vec<String>>,
    /// Per-variant overrides. Keys are CamelCase variant names.
    pub variant_routes: HashMap<String, Vec<String>>,
    /// PagerDuty webhook — referenced as `"pagerduty"` in handler lists.
    pub pagerduty: Option<WebhookHandlerConfig>,
    /// Generic webhook — referenced as `"generic_webhook"` in handler lists.
    pub generic_webhook: Option<WebhookHandlerConfig>,
}

// ---------------------------------------------------------------------------
// Config error
// ---------------------------------------------------------------------------

#[derive(Debug)]
pub enum ConfigError {
    UnknownVariant {
        key: String,
        suggestion: Option<String>,
    },
    UnknownHandler {
        key: String,
    },
    MissingEnvVar {
        handler: String,
        var: String,
    },
}

impl std::fmt::Display for ConfigError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::UnknownVariant { key, suggestion } => {
                write!(f, "unknown OperatorEvent variant '{key}'")?;
                if let Some(s) = suggestion {
                    write!(f, "; did you mean '{s}'?")?;
                }
                Ok(())
            }
            Self::UnknownHandler { key } => write!(
                f,
                "unknown handler name '{key}'; known: {}",
                KNOWN_HANDLERS.join(", ")
            ),
            Self::MissingEnvVar { handler, var } => write!(
                f,
                "handler '{handler}' requires env var '{var}' which is not set"
            ),
        }
    }
}

impl std::error::Error for ConfigError {}

// ---------------------------------------------------------------------------
// Token bucket (per-handler rate limit)
// ---------------------------------------------------------------------------

#[derive(Debug)]
struct TokenBucket {
    tokens: f64,
    rate: f64,
    burst: f64,
    last: Instant,
}

impl TokenBucket {
    fn new(cfg: &RateLimitConfig) -> Self {
        let rate = if cfg.window_sec > 0 {
            cfg.requests as f64 / cfg.window_sec as f64
        } else {
            cfg.requests as f64
        };
        let burst = rate.max(1.0);
        Self {
            tokens: burst,
            rate,
            burst,
            last: Instant::now(),
        }
    }

    fn try_consume(&mut self) -> bool {
        let now = Instant::now();
        let elapsed = now.saturating_duration_since(self.last).as_secs_f64();
        self.tokens = (self.tokens + elapsed * self.rate).min(self.burst);
        self.last = now;
        if self.tokens >= 1.0 {
            self.tokens -= 1.0;
            true
        } else {
            false
        }
    }
}

// ---------------------------------------------------------------------------
// Webhook queue (bounded, drop-oldest on saturation)
// ---------------------------------------------------------------------------

const QUEUE_CAPACITY: usize = 1_000;

#[derive(Debug)]
struct WebhookQueue {
    inner: Mutex<VecDeque<WebhookPayload>>,
    not_empty: Condvar,
    dropped_queue_full: AtomicU64,
    dropped_rate_limit: AtomicU64,
    dropped_max_retries: AtomicU64,
    sent: AtomicU64,
}

impl WebhookQueue {
    fn new() -> Arc<Self> {
        Arc::new(Self {
            inner: Mutex::new(VecDeque::with_capacity(QUEUE_CAPACITY)),
            not_empty: Condvar::new(),
            dropped_queue_full: AtomicU64::new(0),
            dropped_rate_limit: AtomicU64::new(0),
            dropped_max_retries: AtomicU64::new(0),
            sent: AtomicU64::new(0),
        })
    }

    fn push(&self, payload: WebhookPayload) {
        let mut q = self.inner.lock().expect("webhook queue mutex");
        if q.len() >= QUEUE_CAPACITY {
            q.pop_front(); // drop oldest
            self.dropped_queue_full.fetch_add(1, Ordering::Relaxed);
        }
        q.push_back(payload);
        drop(q);
        self.not_empty.notify_one();
    }

    fn pop_blocking(&self) -> WebhookPayload {
        let mut q = self.inner.lock().expect("webhook queue mutex");
        loop {
            if let Some(item) = q.pop_front() {
                return item;
            }
            q = self.not_empty.wait(q).expect("webhook queue condvar");
        }
    }
}

// ---------------------------------------------------------------------------
// Webhook payload
// ---------------------------------------------------------------------------

#[derive(Clone, Debug)]
struct WebhookPayload {
    action: String,
    summary: String,
    ts_ms: u64,
}

impl WebhookPayload {
    fn to_json_body(&self) -> String {
        // Build JSON via the serde_json encoder so special chars in event/summary
        // are escaped correctly (RFC 8259 §7 per ADR 0010).
        use crate::serde_json::Value;
        let event_json = Value::String(self.action.clone()).to_string_compact();
        let summary_json = Value::String(self.summary.clone()).to_string_compact();
        format!(
            r#"{{"event":{event_json},"summary":{summary_json},"ts":{}}}"#,
            self.ts_ms
        )
    }
}

// ---------------------------------------------------------------------------
// Background webhook worker
// ---------------------------------------------------------------------------

fn spawn_webhook_worker(
    name: &str,
    url: String,
    auth_token: String,
    queue: Arc<WebhookQueue>,
) -> thread::JoinHandle<()> {
    let name = name.to_string();
    thread::Builder::new()
        .name(format!("reddb-webhook-{name}"))
        .spawn(move || {
            let agent: ureq::Agent = ureq::Agent::config_builder()
                .timeout_connect(Some(Duration::from_secs(3)))
                .timeout_send_request(Some(Duration::from_secs(5)))
                .timeout_recv_response(Some(Duration::from_secs(5)))
                .http_status_as_error(false)
                .build()
                .into();

            loop {
                let payload = queue.pop_blocking();
                let body = payload.to_json_body();
                let bearer = format!("Bearer {auth_token}");

                let mut success = false;
                for attempt in 1u32..=3 {
                    let result = agent
                        .post(&url)
                        .header("content-type", "application/json")
                        .header("authorization", &bearer)
                        .send(body.as_bytes());

                    match result {
                        Ok(_) => {
                            queue.sent.fetch_add(1, Ordering::Relaxed);
                            success = true;
                            break;
                        }
                        Err(_) if attempt < 3 => {
                            thread::sleep(Duration::from_millis(100 * (1u64 << attempt)));
                        }
                        Err(_) => {}
                    }
                }

                if !success {
                    queue.dropped_max_retries.fetch_add(1, Ordering::Relaxed);
                    tracing::warn!(
                        target: "reddb::operator_router",
                        handler = %name,
                        "webhook delivery failed after 3 attempts; event dropped"
                    );
                }
            }
        })
        .expect("spawn webhook worker thread")
}

// ---------------------------------------------------------------------------
// Effective handler (runtime form)
// ---------------------------------------------------------------------------

#[derive(Debug)]
enum EffectiveHandler {
    AuditLog,
    Tracing,
    Stderr,
    Webhook {
        name: String,
        queue: Arc<WebhookQueue>,
        rate_limiter: Option<Mutex<TokenBucket>>,
    },
}

impl EffectiveHandler {
    fn name(&self) -> &str {
        match self {
            Self::AuditLog => "audit_log",
            Self::Tracing => "tracing",
            Self::Stderr => "stderr",
            Self::Webhook { name, .. } => name,
        }
    }
}

// ---------------------------------------------------------------------------
// Metrics snapshot
// ---------------------------------------------------------------------------

/// Prometheus-style counters snapshot for external scraping.
#[derive(Debug, Default)]
pub struct RouterMetricsSnapshot {
    /// `(handler_name, dropped_count)` — aggregated across all drop reasons.
    pub dropped: Vec<(String, u64)>,
    /// `(handler_name, sent_count)` for webhook handlers.
    pub sent: Vec<(String, u64)>,
}

// ---------------------------------------------------------------------------
// OperatorEventRouter
// ---------------------------------------------------------------------------

/// Config-driven dispatcher for [`OperatorEvent`].
///
/// Build with [`OperatorEventRouter::new`]; the construction validates the
/// config strictly (unknown variant names / handler names / missing env vars
/// all return `Err`). With an empty config the router behaves identically to
/// the current `OperatorEvent::emit()`.
#[derive(Debug)]
pub struct OperatorEventRouter {
    audit_logger: Option<Arc<AuditLogger>>,
    default_route: Vec<Arc<EffectiveHandler>>,
    variant_routes: HashMap<&'static str, Vec<Arc<EffectiveHandler>>>,
    webhook_queues: HashMap<String, Arc<WebhookQueue>>,
    // Mutex makes the field Sync (JoinHandle is Send but !Sync); the workers
    // run until process exit and are never joined during normal operation.
    _workers: Mutex<Vec<thread::JoinHandle<()>>>,
}

impl OperatorEventRouter {
    /// Build a router.
    ///
    /// Validation order (boot fail-fast on any error):
    /// 1. Parse → struct (caller's responsibility for TOML).
    /// 2. Strict variant name check with Levenshtein suggestion.
    /// 3. Strict handler name check (closed set).
    /// 4. Webhook env-var presence check.
    pub fn new(
        config: RouterConfig,
        audit_logger: Option<Arc<AuditLogger>>,
    ) -> Result<Self, ConfigError> {
        let known_variants = OperatorEvent::all_variant_names();

        // --- Validate variant names ---
        for key in config.variant_routes.keys() {
            if !known_variants.contains(&key.as_str()) {
                let suggestion = closest_match(key, known_variants);
                return Err(ConfigError::UnknownVariant {
                    key: key.clone(),
                    suggestion,
                });
            }
        }

        // --- Validate handler names ---
        for names in config.variant_routes.values() {
            validate_handler_names(names)?;
        }
        if let Some(ref dh) = config.default_handlers {
            validate_handler_names(dh)?;
        }

        // --- Build webhook handlers (env-var check happens here) ---
        let mut webhook_queues: HashMap<String, Arc<WebhookQueue>> = HashMap::new();
        let mut workers: Vec<thread::JoinHandle<()>> = Vec::new();

        let pd_handler = config
            .pagerduty
            .as_ref()
            .map(|cfg| -> Result<Arc<EffectiveHandler>, ConfigError> {
                let token =
                    std::env::var(&cfg.auth_env).map_err(|_| ConfigError::MissingEnvVar {
                        handler: "pagerduty".into(),
                        var: cfg.auth_env.clone(),
                    })?;
                let queue = WebhookQueue::new();
                webhook_queues.insert("pagerduty".into(), Arc::clone(&queue));
                workers.push(spawn_webhook_worker(
                    "pagerduty",
                    cfg.url.clone(),
                    token,
                    Arc::clone(&queue),
                ));
                let rate_limiter = cfg
                    .rate_limit
                    .as_ref()
                    .map(|rl| Mutex::new(TokenBucket::new(rl)));
                Ok(Arc::new(EffectiveHandler::Webhook {
                    name: "pagerduty".into(),
                    queue,
                    rate_limiter,
                }))
            })
            .transpose()?;

        let gw_handler = config
            .generic_webhook
            .as_ref()
            .map(|cfg| -> Result<Arc<EffectiveHandler>, ConfigError> {
                let token =
                    std::env::var(&cfg.auth_env).map_err(|_| ConfigError::MissingEnvVar {
                        handler: "generic_webhook".into(),
                        var: cfg.auth_env.clone(),
                    })?;
                let queue = WebhookQueue::new();
                webhook_queues.insert("generic_webhook".into(), Arc::clone(&queue));
                workers.push(spawn_webhook_worker(
                    "generic_webhook",
                    cfg.url.clone(),
                    token,
                    Arc::clone(&queue),
                ));
                let rate_limiter = cfg
                    .rate_limit
                    .as_ref()
                    .map(|rl| Mutex::new(TokenBucket::new(rl)));
                Ok(Arc::new(EffectiveHandler::Webhook {
                    name: "generic_webhook".into(),
                    queue,
                    rate_limiter,
                }))
            })
            .transpose()?;

        // Helper: resolve handler name list → runtime handler vec.
        // Webhook handlers reference shared Arcs so the queue counters are
        // shared between the router and the worker threads.
        let resolve = |names: &[String]| -> Vec<Arc<EffectiveHandler>> {
            names
                .iter()
                .filter_map(|n| match n.as_str() {
                    "audit_log" => Some(Arc::new(EffectiveHandler::AuditLog)),
                    "tracing" => Some(Arc::new(EffectiveHandler::Tracing)),
                    "stderr" => Some(Arc::new(EffectiveHandler::Stderr)),
                    "pagerduty" => pd_handler.clone(),
                    "generic_webhook" => gw_handler.clone(),
                    _ => None,
                })
                .collect()
        };

        let code_default = vec!["audit_log".to_string(), "tracing".to_string()];
        let default_names = config.default_handlers.as_deref().unwrap_or(&code_default);
        let default_route = resolve(default_names);

        let mut variant_routes: HashMap<&'static str, Vec<Arc<EffectiveHandler>>> = HashMap::new();
        for (key, names) in &config.variant_routes {
            if let Some(static_key) = known_variants.iter().copied().find(|v| *v == key.as_str()) {
                variant_routes.insert(static_key, resolve(names));
            }
        }

        Ok(Self {
            audit_logger,
            default_route,
            variant_routes,
            webhook_queues,
            _workers: Mutex::new(workers),
        })
    }

    /// Dispatch `event` through the configured handlers.
    ///
    /// Always synchronous — safe to call from `Drop` impls, signal handlers,
    /// and crash paths.
    pub fn route(&self, event: OperatorEvent) {
        let variant = event.variant_name();
        let handlers = self
            .variant_routes
            .get(variant)
            .unwrap_or(&self.default_route);

        let (action, fields, summary) = event.decompose();
        let ts_ms = crate::utils::now_unix_millis();

        for handler in handlers {
            match handler.as_ref() {
                EffectiveHandler::AuditLog => {
                    if let Some(audit) = &self.audit_logger {
                        let ev = AuditEvent::builder(action)
                            .source(AuditAuthSource::System)
                            .outcome(Outcome::Error)
                            .fields(fields.clone())
                            .build();
                        audit.record_event(ev);
                    }
                }
                EffectiveHandler::Tracing => {
                    tracing::warn!(target: "reddb::operator", "{summary}");
                }
                EffectiveHandler::Stderr => {
                    eprintln!("[reddb::operator] {summary}");
                }
                EffectiveHandler::Webhook {
                    name,
                    queue,
                    rate_limiter,
                } => {
                    if let Some(rl) = rate_limiter {
                        let allowed = rl.lock().expect("rate limiter mutex").try_consume();
                        if !allowed {
                            queue.dropped_rate_limit.fetch_add(1, Ordering::Relaxed);
                            tracing::debug!(
                                target: "reddb::operator_router",
                                handler = %name,
                                "event rate-limited; skipping webhook"
                            );
                            continue;
                        }
                    }
                    queue.push(WebhookPayload {
                        action: action.to_string(),
                        summary: summary.clone(),
                        ts_ms,
                    });
                }
            }
        }
    }

    /// Prometheus-style metrics snapshot.
    pub fn metrics(&self) -> RouterMetricsSnapshot {
        let mut snap = RouterMetricsSnapshot::default();
        for (name, q) in &self.webhook_queues {
            let dropped = q.dropped_queue_full.load(Ordering::Relaxed)
                + q.dropped_rate_limit.load(Ordering::Relaxed)
                + q.dropped_max_retries.load(Ordering::Relaxed);
            snap.dropped.push((name.clone(), dropped));
            snap.sent
                .push((name.clone(), q.sent.load(Ordering::Relaxed)));
        }
        snap
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use std::io::Read;
    use std::net::TcpListener;
    use std::sync::Arc;

    use super::*;
    use crate::runtime::audit_log::AuditLogger;

    fn make_audit_logger() -> (Arc<AuditLogger>, std::path::PathBuf) {
        let mut dir = std::env::temp_dir();
        dir.push(format!(
            "reddb-router-test-{}-{}",
            std::process::id(),
            crate::utils::now_unix_nanos()
        ));
        std::fs::create_dir_all(&dir).unwrap();
        let path = dir.join(".audit.log");
        let logger = Arc::new(AuditLogger::with_path(path.clone()));
        (logger, path)
    }

    fn drain(logger: &AuditLogger) {
        assert!(
            logger.wait_idle(Duration::from_secs(2)),
            "audit logger drain timed out"
        );
    }

    fn last_audit_action(path: &std::path::Path) -> Option<String> {
        let body = std::fs::read_to_string(path).ok()?;
        let line = body.lines().last()?;
        let v: crate::serde_json::Value = crate::serde_json::from_str(line).ok()?;
        v.get("action")
            .and_then(|x| x.as_str())
            .map(|s| s.to_string())
    }

    // -----------------------------------------------------------------------
    // Default routing: empty config = audit_log + tracing
    // -----------------------------------------------------------------------

    #[test]
    fn empty_config_routes_all_variants_to_audit_and_tracing() {
        let (audit, path) = make_audit_logger();
        let router = OperatorEventRouter::new(RouterConfig::default(), Some(Arc::clone(&audit)))
            .expect("router build");

        let variants: &[OperatorEvent] = &[
            OperatorEvent::ReplicationBroken {
                peer: "p".into(),
                reason: "r".into(),
            },
            OperatorEvent::Divergence {
                peer: "p".into(),
                leader_lsn: 1,
                follower_lsn: 0,
            },
            OperatorEvent::WalFsyncFailed {
                path: "/d".into(),
                error: "e".into(),
            },
            OperatorEvent::DiskSpaceCritical {
                path: "/d".into(),
                available_bytes: 1,
                threshold_bytes: 2,
            },
            OperatorEvent::AuthBypass {
                principal: "a".into(),
                resource: "r".into(),
                detail: "d".into(),
            },
            OperatorEvent::AdminCapabilityGranted {
                granted_to: "a".into(),
                capability: "c".into(),
                granted_by: "b".into(),
            },
            OperatorEvent::SecretRotationFailed {
                secret_ref: "s".into(),
                error: "e".into(),
            },
            OperatorEvent::ConfigChanged {
                key: "k".into(),
                old_value: "o".into(),
                new_value: "n".into(),
                changed_by: "b".into(),
            },
            OperatorEvent::StartupFailed {
                phase: "p".into(),
                error: "e".into(),
            },
            OperatorEvent::ShutdownForced { reason: "r".into() },
            OperatorEvent::SchemaCorruption {
                collection: "c".into(),
                detail: "d".into(),
            },
            OperatorEvent::CheckpointFailed {
                lsn: 1,
                error: "e".into(),
            },
            OperatorEvent::ConfigChangeRequiresRestart {
                fields_changed: "f".into(),
            },
            OperatorEvent::SubscriptionSchemaChange {
                collection: "c".into(),
                subscription_names: "sub1".into(),
                fields_added: "phone".into(),
                fields_removed: "".into(),
                lsn: 42,
            },
            OperatorEvent::OutboxDlqActivated {
                queue: "users_events".into(),
                dlq: "users_events_outbox_dlq".into(),
                reason: "queue_full".into(),
            },
        ];

        // Verify count matches all_variant_names minus DanglingAdminIntent
        // (which requires crypto types — tested separately).
        for event in variants {
            // Clone variant_name before routing (event is consumed by route).
            let vname = event.variant_name();
            router.route(clone_event(event));
            let _ = vname; // just ensure it compiled
        }

        drain(&audit);
        // At least one line written: the last event.
        let action = last_audit_action(&path).expect("at least one audit line");
        assert!(action.starts_with("operator/"), "action={action}");
    }

    // Clone helper for test variants that don't use crypto types.
    fn clone_event(e: &OperatorEvent) -> OperatorEvent {
        match e {
            OperatorEvent::ReplicationBroken { peer, reason } => OperatorEvent::ReplicationBroken {
                peer: peer.clone(),
                reason: reason.clone(),
            },
            OperatorEvent::Divergence {
                peer,
                leader_lsn,
                follower_lsn,
            } => OperatorEvent::Divergence {
                peer: peer.clone(),
                leader_lsn: *leader_lsn,
                follower_lsn: *follower_lsn,
            },
            OperatorEvent::WalFsyncFailed { path, error } => OperatorEvent::WalFsyncFailed {
                path: path.clone(),
                error: error.clone(),
            },
            OperatorEvent::DiskSpaceCritical {
                path,
                available_bytes,
                threshold_bytes,
            } => OperatorEvent::DiskSpaceCritical {
                path: path.clone(),
                available_bytes: *available_bytes,
                threshold_bytes: *threshold_bytes,
            },
            OperatorEvent::AuthBypass {
                principal,
                resource,
                detail,
            } => OperatorEvent::AuthBypass {
                principal: principal.clone(),
                resource: resource.clone(),
                detail: detail.clone(),
            },
            OperatorEvent::AdminCapabilityGranted {
                granted_to,
                capability,
                granted_by,
            } => OperatorEvent::AdminCapabilityGranted {
                granted_to: granted_to.clone(),
                capability: capability.clone(),
                granted_by: granted_by.clone(),
            },
            OperatorEvent::SecretRotationFailed { secret_ref, error } => {
                OperatorEvent::SecretRotationFailed {
                    secret_ref: secret_ref.clone(),
                    error: error.clone(),
                }
            }
            OperatorEvent::ConfigChanged {
                key,
                old_value,
                new_value,
                changed_by,
            } => OperatorEvent::ConfigChanged {
                key: key.clone(),
                old_value: old_value.clone(),
                new_value: new_value.clone(),
                changed_by: changed_by.clone(),
            },
            OperatorEvent::StartupFailed { phase, error } => OperatorEvent::StartupFailed {
                phase: phase.clone(),
                error: error.clone(),
            },
            OperatorEvent::ShutdownForced { reason } => OperatorEvent::ShutdownForced {
                reason: reason.clone(),
            },
            OperatorEvent::SchemaCorruption { collection, detail } => {
                OperatorEvent::SchemaCorruption {
                    collection: collection.clone(),
                    detail: detail.clone(),
                }
            }
            OperatorEvent::CheckpointFailed { lsn, error } => OperatorEvent::CheckpointFailed {
                lsn: *lsn,
                error: error.clone(),
            },
            OperatorEvent::ConfigChangeRequiresRestart { fields_changed } => {
                OperatorEvent::ConfigChangeRequiresRestart {
                    fields_changed: fields_changed.clone(),
                }
            }
            OperatorEvent::DanglingAdminIntent { .. } => {
                // Not cloneable without crypto type impls; skip.
                OperatorEvent::ShutdownForced {
                    reason: "clone_placeholder".into(),
                }
            }
            OperatorEvent::SubscriptionSchemaChange {
                collection,
                subscription_names,
                fields_added,
                fields_removed,
                lsn,
            } => OperatorEvent::SubscriptionSchemaChange {
                collection: collection.clone(),
                subscription_names: subscription_names.clone(),
                fields_added: fields_added.clone(),
                fields_removed: fields_removed.clone(),
                lsn: *lsn,
            },
            OperatorEvent::OutboxDlqActivated { queue, dlq, reason } => {
                OperatorEvent::OutboxDlqActivated {
                    queue: queue.clone(),
                    dlq: dlq.clone(),
                    reason: reason.clone(),
                }
            }
        }
    }

    // -----------------------------------------------------------------------
    // Config validation: unknown variant name → Levenshtein suggestion
    // -----------------------------------------------------------------------

    #[test]
    fn unknown_variant_gives_suggestion() {
        let mut config = RouterConfig::default();
        // "AuthBypas" is one edit away from "AuthBypass" (missing 's').
        config
            .variant_routes
            .insert("AuthBypas".into(), vec!["audit_log".into()]);
        let err = OperatorEventRouter::new(config, None).unwrap_err();
        match err {
            ConfigError::UnknownVariant { key, suggestion } => {
                assert_eq!(key, "AuthBypas");
                // strsim should suggest "AuthBypass" (levenshtein distance 1)
                assert_eq!(suggestion.as_deref(), Some("AuthBypass"));
            }
            other => panic!("expected UnknownVariant, got: {other}"),
        }
    }

    #[test]
    fn unknown_handler_name_is_rejected() {
        let mut config = RouterConfig::default();
        config.variant_routes.insert(
            "AuthBypass".into(),
            vec!["slack".into()], // not a known handler
        );
        let err = OperatorEventRouter::new(config, None).unwrap_err();
        assert!(matches!(err, ConfigError::UnknownHandler { .. }));
    }

    // -----------------------------------------------------------------------
    // Per-variant route override
    // -----------------------------------------------------------------------

    #[test]
    fn per_variant_route_overrides_default() {
        let (audit, path) = make_audit_logger();
        let mut config = RouterConfig::default();
        // Route AuthBypass to stderr-only (no audit log for this test variant).
        config
            .variant_routes
            .insert("AuthBypass".into(), vec!["stderr".into()]);
        let router = OperatorEventRouter::new(config, Some(Arc::clone(&audit))).unwrap();

        // Emit AuthBypass — should NOT go to audit (only stderr per config).
        router.route(OperatorEvent::AuthBypass {
            principal: "test".into(),
            resource: "/secret".into(),
            detail: "test override".into(),
        });
        drain(&audit);
        // Audit log should still be empty (or unchanged from no lines).
        let body = std::fs::read_to_string(&path).unwrap_or_default();
        assert!(
            body.lines().all(|l| !l.contains("auth_bypass")),
            "auth_bypass should not appear in audit (stderr-only route)"
        );

        // Emit a different variant — should still go to default (audit+tracing).
        router.route(OperatorEvent::ShutdownForced {
            reason: "test".into(),
        });
        drain(&audit);
        let action = last_audit_action(&path).expect("shutdown_forced in audit");
        assert_eq!(action, "operator/shutdown_forced");
    }

    // -----------------------------------------------------------------------
    // Token bucket rate limit
    // -----------------------------------------------------------------------

    #[test]
    fn token_bucket_throttles_after_burst() {
        let mut bucket = TokenBucket::new(&RateLimitConfig {
            requests: 3,
            window_sec: 60,
        });
        // rate = 3/60 = 0.05 t/s, burst = max(0.05, 1.0) = 1.0
        // First consume should succeed.
        assert!(bucket.try_consume(), "first consume should succeed");
        // Second should fail (burst exhausted, no time passed).
        assert!(!bucket.try_consume(), "second consume should be throttled");
    }

    #[test]
    fn token_bucket_refills_over_time() {
        let mut bucket = TokenBucket::new(&RateLimitConfig {
            requests: 100,
            window_sec: 1,
        });
        // rate = 100/s, burst = 100
        for _ in 0..100 {
            assert!(bucket.try_consume());
        }
        assert!(!bucket.try_consume(), "burst exhausted");
        thread::sleep(Duration::from_millis(20));
        // After 20ms at 100/s we get ~2 tokens.
        assert!(bucket.try_consume(), "should refill after sleep");
    }

    // -----------------------------------------------------------------------
    // Webhook queue: drop oldest on saturation
    // -----------------------------------------------------------------------

    #[test]
    fn queue_drops_oldest_on_saturation() {
        let queue = WebhookQueue::new();
        for i in 0..QUEUE_CAPACITY {
            queue.push(WebhookPayload {
                action: format!("ev/{i}"),
                summary: format!("s{i}"),
                ts_ms: i as u64,
            });
        }
        assert_eq!(queue.dropped_queue_full.load(Ordering::Relaxed), 0);

        // One more push → drops oldest (ev/0).
        queue.push(WebhookPayload {
            action: "ev/overflow".into(),
            summary: "overflow".into(),
            ts_ms: QUEUE_CAPACITY as u64,
        });
        assert_eq!(queue.dropped_queue_full.load(Ordering::Relaxed), 1);

        // Oldest item in queue should now be ev/1 (ev/0 was dropped).
        let first = queue.pop_blocking();
        assert_eq!(first.action, "ev/1");
    }

    // -----------------------------------------------------------------------
    // Integration: mock webhook server receives payload
    // -----------------------------------------------------------------------

    #[test]
    fn webhook_delivers_payload_to_mock_server() {
        // Bind a local TCP server to act as the webhook endpoint.
        let listener = TcpListener::bind("127.0.0.1:0").unwrap();
        let addr = listener.local_addr().unwrap();
        let url = format!("http://{addr}/webhook");

        // Accept one connection in a background thread.
        let server_thread = thread::spawn(move || {
            let (mut stream, _) = listener.accept().unwrap();
            let mut buf = vec![0u8; 4096];
            let n = stream.read(&mut buf).unwrap_or(0);
            String::from_utf8_lossy(&buf[..n]).to_string()
        });

        // Set up env var for auth.
        std::env::set_var("TEST_WEBHOOK_TOKEN_ROUTER", "test-token-42");

        let config = RouterConfig {
            default_handlers: None,
            variant_routes: {
                let mut m = HashMap::new();
                m.insert("ShutdownForced".into(), vec!["generic_webhook".into()]);
                m
            },
            pagerduty: None,
            generic_webhook: Some(WebhookHandlerConfig {
                url,
                auth_env: "TEST_WEBHOOK_TOKEN_ROUTER".into(),
                rate_limit: None,
            }),
        };

        let router = OperatorEventRouter::new(config, None).unwrap();
        router.route(OperatorEvent::ShutdownForced {
            reason: "integration-test".into(),
        });

        let raw = server_thread.join().expect("server thread");
        // The request should contain our auth header and JSON body.
        assert!(raw.contains("Bearer test-token-42"), "missing auth header");
        assert!(raw.contains("shutdown_forced"), "missing event in body");
    }

    // -----------------------------------------------------------------------
    // Race: concurrent route() calls don't corrupt rate limiter
    // -----------------------------------------------------------------------

    #[test]
    fn concurrent_route_calls_safe() {
        let router = Arc::new(OperatorEventRouter::new(RouterConfig::default(), None).unwrap());
        let handles: Vec<_> = (0..16)
            .map(|_| {
                let r = Arc::clone(&router);
                thread::spawn(move || {
                    for _ in 0..50 {
                        r.route(OperatorEvent::ShutdownForced {
                            reason: "stress".into(),
                        });
                    }
                })
            })
            .collect();
        for h in handles {
            h.join().unwrap();
        }
        // No panic = pass.
    }

    // -----------------------------------------------------------------------
    // Missing env var → boot fail-fast
    // -----------------------------------------------------------------------

    #[test]
    fn missing_env_var_fails_at_construction() {
        let config = RouterConfig {
            default_handlers: None,
            variant_routes: HashMap::new(),
            pagerduty: Some(WebhookHandlerConfig {
                url: "http://localhost/pd".into(),
                auth_env: "REDDB_TEST_PD_KEY_DEFINITELY_NOT_SET_12345".into(),
                rate_limit: None,
            }),
            generic_webhook: None,
        };
        let err = OperatorEventRouter::new(config, None).unwrap_err();
        assert!(matches!(err, ConfigError::MissingEnvVar { .. }));
    }
}