voirs-evaluation 0.1.0-rc.1

Quality evaluation and assessment framework for VoiRS
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
// Copyright (c) 2024 VoiRS Contributors
// Licensed under the MIT License

//! Multi-Region Deployment Configuration for Global Distribution
//!
//! This module extends the Kubernetes deployment capabilities to support multi-region
//! deployments with geographic distribution, failover, and data sovereignty compliance.
//!
//! # Features
//!
//! - **Geographic Distribution**: Deploy across multiple cloud regions globally
//! - **Active-Active/Active-Passive**: Flexible deployment patterns for high availability
//! - **Geo-Routing**: Intelligent traffic routing based on user location
//! - **Data Sovereignty**: Compliance with regional data residency requirements
//! - **Cross-Region Failover**: Automatic failover between regions
//! - **Replication Strategy**: Configurable data replication across regions
//! - **Latency Optimization**: Route users to nearest region for best performance
//! - **Cost Optimization**: Balance between performance and infrastructure costs
//!
//! # Architecture
//!
//! ```text
//! ┌──────────────────────────────────────────────────────────┐
//! │              Global Load Balancer                        │
//! │         (GeoDNS / Traffic Manager)                       │
//! └──────────────────────────────────────────────────────────┘
//!          │                  │                  │
//!          ▼                  ▼                  ▼
//! ┌──────────────┐   ┌──────────────┐   ┌──────────────┐
//! │  US-West-2   │   │  EU-Central  │   │  AP-Southeast│
//! │  (Primary)   │   │  (Secondary) │   │  (Secondary) │
//! ├──────────────┤   ├──────────────┤   ├──────────────┤
//! │ Kubernetes   │   │ Kubernetes   │   │ Kubernetes   │
//! │ Cluster      │←─→│ Cluster      │←─→│ Cluster      │
//! │              │   │              │   │              │
//! │ ┌──────────┐ │   │ ┌──────────┐ │   │ ┌──────────┐ │
//! │ │ Workers  │ │   │ │ Workers  │ │   │ │ Workers  │ │
//! │ │ (HPA)    │ │   │ │ (HPA)    │ │   │ │ (HPA)    │ │
//! │ └──────────┘ │   │ └──────────┘ │   │ └──────────┘ │
//! └──────────────┘   └──────────────┘   └──────────────┘
//!       │ Replication   │ Replication   │
//!       └───────────────┴───────────────┘
//! ```
//!
//! # Example Usage
//!
//! ```rust
//! use voirs_evaluation::multiregion::{
//!     MultiRegionConfig, Region, DeploymentPattern, GeoRoutingStrategy
//! };
//!
//! # fn example() -> Result<(), Box<dyn std::error::Error>> {
//! // Configure multi-region deployment
//! let config = MultiRegionConfig::builder()
//!     .add_region(Region::UsWest2, true)  // Primary
//!     .add_region(Region::EuCentral1, false)  // Secondary
//!     .add_region(Region::ApSoutheast1, false)  // Secondary
//!     .deployment_pattern(DeploymentPattern::ActiveActive)
//!     .geo_routing_strategy(GeoRoutingStrategy::LatencyBased)
//!     .enable_cross_region_replication(true)
//!     .build()?;
//!
//! println!("Configured {} regions", config.regions.len());
//! println!("Primary region: {:?}", config.get_primary_region());
//! # Ok(())
//! # }
//! ```

use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use thiserror::Error;

/// Multi-region deployment errors
#[derive(Debug, Error)]
pub enum MultiRegionError {
    #[error("Configuration error: {0}")]
    ConfigError(String),

    #[error("Region error: {0}")]
    RegionError(String),

    #[error("Replication error: {0}")]
    ReplicationError(String),

    #[error("Failover error: {0}")]
    FailoverError(String),

    #[error("No primary region configured")]
    NoPrimaryRegion,
}

pub type Result<T> = std::result::Result<T, MultiRegionError>;

/// Cloud regions
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Region {
    // AWS Regions
    UsEast1,
    UsEast2,
    UsWest1,
    UsWest2,
    CaCentral1,
    EuWest1,
    EuWest2,
    EuCentral1,
    EuNorth1,
    ApSouth1,
    ApNortheast1,
    ApNortheast2,
    ApSoutheast1,
    ApSoutheast2,
    SaEast1,

    // Azure Regions
    EastUs,
    WestUs,
    NorthEurope,
    WestEurope,
    SoutheastAsia,
    EastAsia,

    // GCP Regions
    UsEast4,
    UsWest4,
    EuropeWest1,
    AsiaEast1,
    AsiaSoutheast1,
}

impl Region {
    /// Get region name
    pub fn name(&self) -> &str {
        match self {
            Region::UsEast1 => "us-east-1",
            Region::UsEast2 => "us-east-2",
            Region::UsWest1 => "us-west-1",
            Region::UsWest2 => "us-west-2",
            Region::CaCentral1 => "ca-central-1",
            Region::EuWest1 => "eu-west-1",
            Region::EuWest2 => "eu-west-2",
            Region::EuCentral1 => "eu-central-1",
            Region::EuNorth1 => "eu-north-1",
            Region::ApSouth1 => "ap-south-1",
            Region::ApNortheast1 => "ap-northeast-1",
            Region::ApNortheast2 => "ap-northeast-2",
            Region::ApSoutheast1 => "ap-southeast-1",
            Region::ApSoutheast2 => "ap-southeast-2",
            Region::SaEast1 => "sa-east-1",
            Region::EastUs => "eastus",
            Region::WestUs => "westus",
            Region::NorthEurope => "northeurope",
            Region::WestEurope => "westeurope",
            Region::SoutheastAsia => "southeastasia",
            Region::EastAsia => "eastasia",
            Region::UsEast4 => "us-east4",
            Region::UsWest4 => "us-west4",
            Region::EuropeWest1 => "europe-west1",
            Region::AsiaEast1 => "asia-east1",
            Region::AsiaSoutheast1 => "asia-southeast1",
        }
    }

    /// Get geographic location
    pub fn geographic_location(&self) -> GeographicLocation {
        match self {
            Region::UsEast1
            | Region::UsEast2
            | Region::UsWest1
            | Region::UsWest2
            | Region::EastUs
            | Region::WestUs
            | Region::UsEast4
            | Region::UsWest4 => GeographicLocation::NorthAmerica,
            Region::CaCentral1 => GeographicLocation::NorthAmerica,
            Region::EuWest1
            | Region::EuWest2
            | Region::EuCentral1
            | Region::EuNorth1
            | Region::NorthEurope
            | Region::WestEurope
            | Region::EuropeWest1 => GeographicLocation::Europe,
            Region::ApSouth1 => GeographicLocation::SouthAsia,
            Region::ApNortheast1 | Region::ApNortheast2 => GeographicLocation::EastAsia,
            Region::ApSoutheast1
            | Region::ApSoutheast2
            | Region::SoutheastAsia
            | Region::EastAsia
            | Region::AsiaEast1
            | Region::AsiaSoutheast1 => GeographicLocation::SoutheastAsia,
            Region::SaEast1 => GeographicLocation::SouthAmerica,
        }
    }

    /// Get approximate latency to another region (milliseconds)
    pub fn estimated_latency_to(&self, other: &Region) -> f64 {
        let same_location = self.geographic_location() == other.geographic_location();

        if self == other {
            1.0 // Same region
        } else if same_location {
            20.0 // Same geographic location
        } else {
            // Cross-region latency estimates
            match (self.geographic_location(), other.geographic_location()) {
                (GeographicLocation::NorthAmerica, GeographicLocation::Europe) => 100.0,
                (GeographicLocation::NorthAmerica, GeographicLocation::EastAsia) => 150.0,
                (GeographicLocation::Europe, GeographicLocation::EastAsia) => 200.0,
                _ => 120.0,
            }
        }
    }
}

/// Geographic locations
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum GeographicLocation {
    NorthAmerica,
    SouthAmerica,
    Europe,
    EastAsia,
    SoutheastAsia,
    SouthAsia,
    MiddleEast,
    Africa,
    Oceania,
}

/// Deployment patterns for multi-region
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum DeploymentPattern {
    /// All regions serve traffic simultaneously
    ActiveActive,

    /// One primary region, others serve only on failover
    ActivePassive,

    /// Primary handles writes, secondaries handle reads
    PrimarySecondary,

    /// Traffic distributed based on geographic proximity
    GeoDistributed,
}

/// Geo-routing strategy
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum GeoRoutingStrategy {
    /// Route to geographically nearest region
    Geographic,

    /// Route based on measured latency
    LatencyBased,

    /// Route based on current load
    LoadBalanced,

    /// Route to region with best performance score
    PerformanceBased,

    /// Round-robin across regions
    RoundRobin,
}

/// Replication strategy
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ReplicationStrategy {
    /// Synchronous replication (strong consistency)
    Synchronous,

    /// Asynchronous replication (eventual consistency)
    Asynchronous,

    /// Semi-synchronous (one sync, others async)
    SemiSynchronous,

    /// No cross-region replication
    None,
}

/// Region configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RegionConfig {
    /// Region identifier
    pub region: Region,

    /// Whether this is the primary region
    pub is_primary: bool,

    /// Region weight for load balancing (0.0 to 1.0)
    pub weight: f64,

    /// Region enabled
    pub enabled: bool,

    /// Kubernetes cluster endpoint
    pub cluster_endpoint: Option<String>,

    /// Maximum capacity (requests per second)
    pub max_capacity: u32,

    /// Current utilization percentage
    pub current_utilization: f64,

    /// Health status
    pub healthy: bool,

    /// Data sovereignty restrictions
    pub data_sovereignty: Vec<String>,
}

impl RegionConfig {
    pub fn new(region: Region, is_primary: bool) -> Self {
        Self {
            region,
            is_primary,
            weight: 1.0,
            enabled: true,
            cluster_endpoint: None,
            max_capacity: 1000,
            current_utilization: 0.0,
            healthy: true,
            data_sovereignty: vec![],
        }
    }
}

/// Multi-region configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MultiRegionConfig {
    /// Regions in deployment
    pub regions: HashMap<Region, RegionConfig>,

    /// Deployment pattern
    pub deployment_pattern: DeploymentPattern,

    /// Geo-routing strategy
    pub geo_routing_strategy: GeoRoutingStrategy,

    /// Replication strategy
    pub replication_strategy: ReplicationStrategy,

    /// Enable automatic failover
    pub auto_failover_enabled: bool,

    /// Failover threshold (health check failures)
    pub failover_threshold: u32,

    /// Enable cross-region replication
    pub cross_region_replication: bool,

    /// Replication lag tolerance (seconds)
    pub max_replication_lag_secs: u64,

    /// Enable data sovereignty enforcement
    pub data_sovereignty_enabled: bool,

    /// Global load balancer endpoint
    pub global_endpoint: Option<String>,
}

impl Default for MultiRegionConfig {
    fn default() -> Self {
        let mut regions = HashMap::new();
        regions.insert(Region::UsWest2, RegionConfig::new(Region::UsWest2, true));

        Self {
            regions,
            deployment_pattern: DeploymentPattern::ActivePassive,
            geo_routing_strategy: GeoRoutingStrategy::LatencyBased,
            replication_strategy: ReplicationStrategy::Asynchronous,
            auto_failover_enabled: true,
            failover_threshold: 3,
            cross_region_replication: true,
            max_replication_lag_secs: 30,
            data_sovereignty_enabled: false,
            global_endpoint: None,
        }
    }
}

impl MultiRegionConfig {
    pub fn builder() -> MultiRegionConfigBuilder {
        MultiRegionConfigBuilder::default()
    }

    /// Get primary region
    pub fn get_primary_region(&self) -> Option<&RegionConfig> {
        self.regions.values().find(|r| r.is_primary)
    }

    /// Get healthy regions
    pub fn get_healthy_regions(&self) -> Vec<&RegionConfig> {
        self.regions
            .values()
            .filter(|r| r.healthy && r.enabled)
            .collect()
    }

    /// Route request to optimal region
    pub fn route_request(&self, client_region: Option<Region>) -> Option<Region> {
        let healthy_regions = self.get_healthy_regions();

        if healthy_regions.is_empty() {
            return None;
        }

        match self.geo_routing_strategy {
            GeoRoutingStrategy::Geographic | GeoRoutingStrategy::LatencyBased => {
                if let Some(client_reg) = client_region {
                    // Find region with lowest latency
                    healthy_regions
                        .iter()
                        .min_by(|a, b| {
                            let latency_a = client_reg.estimated_latency_to(&a.region);
                            let latency_b = client_reg.estimated_latency_to(&b.region);
                            latency_a
                                .partial_cmp(&latency_b)
                                .unwrap_or(std::cmp::Ordering::Equal)
                        })
                        .map(|r| r.region)
                } else {
                    // Default to primary
                    self.get_primary_region().map(|r| r.region)
                }
            }
            GeoRoutingStrategy::LoadBalanced => {
                // Route to least utilized region
                healthy_regions
                    .iter()
                    .min_by(|a, b| {
                        a.current_utilization
                            .partial_cmp(&b.current_utilization)
                            .expect("value should be present")
                    })
                    .map(|r| r.region)
            }
            GeoRoutingStrategy::PerformanceBased => {
                // Route to region with best capacity/utilization ratio
                healthy_regions
                    .iter()
                    .max_by(|a, b| {
                        let score_a = a.max_capacity as f64 / (a.current_utilization + 1.0);
                        let score_b = b.max_capacity as f64 / (b.current_utilization + 1.0);
                        score_a
                            .partial_cmp(&score_b)
                            .unwrap_or(std::cmp::Ordering::Equal)
                    })
                    .map(|r| r.region)
            }
            GeoRoutingStrategy::RoundRobin => {
                // Simple: return first healthy region
                healthy_regions.first().map(|r| r.region)
            }
        }
    }

    /// Validate configuration
    pub fn validate(&self) -> Result<()> {
        if self.regions.is_empty() {
            return Err(MultiRegionError::ConfigError(
                "At least one region must be configured".to_string(),
            ));
        }

        let primary_count = self.regions.values().filter(|r| r.is_primary).count();
        if primary_count == 0 {
            return Err(MultiRegionError::NoPrimaryRegion);
        }

        if primary_count > 1 && self.deployment_pattern == DeploymentPattern::ActivePassive {
            return Err(MultiRegionError::ConfigError(
                "Active-Passive pattern requires exactly one primary region".to_string(),
            ));
        }

        Ok(())
    }
}

/// Builder for multi-region configuration
#[derive(Debug, Default)]
pub struct MultiRegionConfigBuilder {
    regions: HashMap<Region, RegionConfig>,
    deployment_pattern: Option<DeploymentPattern>,
    geo_routing_strategy: Option<GeoRoutingStrategy>,
    replication_strategy: Option<ReplicationStrategy>,
    auto_failover_enabled: bool,
    failover_threshold: u32,
    cross_region_replication: bool,
    max_replication_lag_secs: u64,
    data_sovereignty_enabled: bool,
    global_endpoint: Option<String>,
}

impl MultiRegionConfigBuilder {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn add_region(mut self, region: Region, is_primary: bool) -> Self {
        self.regions
            .insert(region, RegionConfig::new(region, is_primary));
        self
    }

    pub fn deployment_pattern(mut self, pattern: DeploymentPattern) -> Self {
        self.deployment_pattern = Some(pattern);
        self
    }

    pub fn geo_routing_strategy(mut self, strategy: GeoRoutingStrategy) -> Self {
        self.geo_routing_strategy = Some(strategy);
        self
    }

    pub fn replication_strategy(mut self, strategy: ReplicationStrategy) -> Self {
        self.replication_strategy = Some(strategy);
        self
    }

    pub fn enable_auto_failover(mut self, enable: bool) -> Self {
        self.auto_failover_enabled = enable;
        self
    }

    pub fn failover_threshold(mut self, threshold: u32) -> Self {
        self.failover_threshold = threshold;
        self
    }

    pub fn enable_cross_region_replication(mut self, enable: bool) -> Self {
        self.cross_region_replication = enable;
        self
    }

    pub fn max_replication_lag(mut self, seconds: u64) -> Self {
        self.max_replication_lag_secs = seconds;
        self
    }

    pub fn enable_data_sovereignty(mut self, enable: bool) -> Self {
        self.data_sovereignty_enabled = enable;
        self
    }

    pub fn global_endpoint(mut self, endpoint: impl Into<String>) -> Self {
        self.global_endpoint = Some(endpoint.into());
        self
    }

    pub fn build(self) -> Result<MultiRegionConfig> {
        let config = MultiRegionConfig {
            regions: self.regions,
            deployment_pattern: self
                .deployment_pattern
                .unwrap_or(DeploymentPattern::ActivePassive),
            geo_routing_strategy: self
                .geo_routing_strategy
                .unwrap_or(GeoRoutingStrategy::LatencyBased),
            replication_strategy: self
                .replication_strategy
                .unwrap_or(ReplicationStrategy::Asynchronous),
            auto_failover_enabled: self.auto_failover_enabled,
            failover_threshold: self.failover_threshold,
            cross_region_replication: self.cross_region_replication,
            max_replication_lag_secs: self.max_replication_lag_secs,
            data_sovereignty_enabled: self.data_sovereignty_enabled,
            global_endpoint: self.global_endpoint,
        };

        config.validate()?;
        Ok(config)
    }
}

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

    #[test]
    fn test_region_names() {
        assert_eq!(Region::UsWest2.name(), "us-west-2");
        assert_eq!(Region::EuCentral1.name(), "eu-central-1");
        assert_eq!(Region::ApSoutheast1.name(), "ap-southeast-1");
    }

    #[test]
    fn test_geographic_location() {
        assert_eq!(
            Region::UsWest2.geographic_location(),
            GeographicLocation::NorthAmerica
        );
        assert_eq!(
            Region::EuCentral1.geographic_location(),
            GeographicLocation::Europe
        );
        assert_eq!(
            Region::ApSoutheast1.geographic_location(),
            GeographicLocation::SoutheastAsia
        );
    }

    #[test]
    fn test_estimated_latency() {
        assert_eq!(Region::UsWest2.estimated_latency_to(&Region::UsWest2), 1.0);
        assert_eq!(Region::UsWest2.estimated_latency_to(&Region::UsEast1), 20.0);
        assert_eq!(
            Region::UsWest2.estimated_latency_to(&Region::EuCentral1),
            100.0
        );
    }

    #[test]
    fn test_default_config() {
        let config = MultiRegionConfig::default();
        assert_eq!(config.regions.len(), 1);
        assert!(config.auto_failover_enabled);
        assert!(config.cross_region_replication);
    }

    #[test]
    fn test_builder() {
        let config = MultiRegionConfig::builder()
            .add_region(Region::UsWest2, true)
            .add_region(Region::EuCentral1, false)
            .deployment_pattern(DeploymentPattern::ActiveActive)
            .geo_routing_strategy(GeoRoutingStrategy::LatencyBased)
            .build()
            .unwrap();

        assert_eq!(config.regions.len(), 2);
        assert_eq!(config.deployment_pattern, DeploymentPattern::ActiveActive);
        assert_eq!(
            config.geo_routing_strategy,
            GeoRoutingStrategy::LatencyBased
        );
    }

    #[test]
    fn test_get_primary_region() {
        let config = MultiRegionConfig::builder()
            .add_region(Region::UsWest2, true)
            .add_region(Region::EuCentral1, false)
            .build()
            .unwrap();

        let primary = config.get_primary_region().unwrap();
        assert_eq!(primary.region, Region::UsWest2);
        assert!(primary.is_primary);
    }

    #[test]
    fn test_no_primary_region_error() {
        let result = MultiRegionConfig::builder()
            .add_region(Region::UsWest2, false)
            .add_region(Region::EuCentral1, false)
            .build();

        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            MultiRegionError::NoPrimaryRegion
        ));
    }

    #[test]
    fn test_route_request_latency_based() {
        let config = MultiRegionConfig::builder()
            .add_region(Region::UsWest2, true)
            .add_region(Region::EuCentral1, false)
            .add_region(Region::ApSoutheast1, false)
            .geo_routing_strategy(GeoRoutingStrategy::LatencyBased)
            .build()
            .unwrap();

        // Client in US should route to UsWest2
        let routed = config.route_request(Some(Region::UsEast1));
        assert_eq!(routed, Some(Region::UsWest2));

        // Client in Europe should route to EuCentral1
        let routed = config.route_request(Some(Region::EuWest1));
        assert_eq!(routed, Some(Region::EuCentral1));
    }

    #[test]
    fn test_healthy_regions_filter() {
        let mut config = MultiRegionConfig::builder()
            .add_region(Region::UsWest2, true)
            .add_region(Region::EuCentral1, false)
            .build()
            .unwrap();

        // Mark one region unhealthy
        config.regions.get_mut(&Region::EuCentral1).unwrap().healthy = false;

        let healthy = config.get_healthy_regions();
        assert_eq!(healthy.len(), 1);
        assert_eq!(healthy[0].region, Region::UsWest2);
    }

    #[test]
    fn test_validation() {
        let config = MultiRegionConfig::builder()
            .add_region(Region::UsWest2, true)
            .build()
            .unwrap();

        assert!(config.validate().is_ok());
    }

    #[test]
    fn test_multiple_primaries_active_passive_error() {
        let result = MultiRegionConfig::builder()
            .add_region(Region::UsWest2, true)
            .add_region(Region::EuCentral1, true)
            .deployment_pattern(DeploymentPattern::ActivePassive)
            .build();

        assert!(result.is_err());
    }

    #[test]
    fn test_active_active_allows_multiple_primaries() {
        let config = MultiRegionConfig::builder()
            .add_region(Region::UsWest2, true)
            .add_region(Region::EuCentral1, true)
            .deployment_pattern(DeploymentPattern::ActiveActive)
            .build()
            .unwrap();

        let primaries: Vec<_> = config.regions.values().filter(|r| r.is_primary).collect();

        assert_eq!(primaries.len(), 2);
    }
}