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
//! Multi-region routing for LLM requests based on data residency rules.
//!
//! Routes LLM requests to region-appropriate providers by enforcing data
//! classification and tenancy constraints. Integrates with
//! [`argentor_core::data_residency::DataResidencyConfig`] to provide
//! gateway-level enforcement of residency policies.
//!
//! # Example
//!
//! ```rust
//! use argentor_gateway::region_router::{
//! DataClassification, RegionRouter, RegionRule,
//! };
//!
//! let mut router = RegionRouter::new("us-east-1");
//! router.add_rule(RegionRule {
//! tenant_id: None,
//! data_classification: DataClassification::Restricted,
//! allowed_regions: vec!["eu-west-1".into(), "eu-central-1".into()],
//! allowed_providers: vec!["claude".into()],
//! blocked_providers: vec!["openai".into()],
//! });
//!
//! // openai is blocked, but claude is available as alternative
//! let decision = router.route(None, &DataClassification::Restricted, "openai");
//! assert!(decision.allowed);
//! assert_eq!(decision.provider, Some("claude".to_string()));
//! ```
use serde::{Deserialize, Serialize};
// ---------------------------------------------------------------------------
// Data types
// ---------------------------------------------------------------------------
/// Classification level for the data being sent to an LLM provider.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum DataClassification {
/// Publicly available data with no restrictions.
Public,
/// Internal organizational data, limited external sharing.
Internal,
/// Confidential business data requiring access controls.
Confidential,
/// Restricted data: PII, financial, health records, etc.
Restricted,
}
impl DataClassification {
/// Returns the sensitivity level as an integer (higher = more sensitive).
pub fn sensitivity(&self) -> u8 {
match self {
DataClassification::Public => 0,
DataClassification::Internal => 1,
DataClassification::Confidential => 2,
DataClassification::Restricted => 3,
}
}
}
impl std::fmt::Display for DataClassification {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
DataClassification::Public => write!(f, "Public"),
DataClassification::Internal => write!(f, "Internal"),
DataClassification::Confidential => write!(f, "Confidential"),
DataClassification::Restricted => write!(f, "Restricted"),
}
}
}
/// A rule that constrains which providers and regions are allowed for a given
/// tenant and data classification level.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RegionRule {
/// Tenant identifier. `None` means the rule applies to all tenants.
pub tenant_id: Option<String>,
/// The data classification level this rule applies to.
pub data_classification: DataClassification,
/// Regions where data processing is allowed (e.g., `"eu-west-1"`).
pub allowed_regions: Vec<String>,
/// Providers explicitly allowed. Empty means no explicit allowlist (all non-blocked are ok).
pub allowed_providers: Vec<String>,
/// Providers explicitly blocked. Takes precedence over allowed list.
pub blocked_providers: Vec<String>,
}
/// The outcome of a routing decision.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RoutingDecision {
/// Whether the request is allowed to proceed.
pub allowed: bool,
/// The selected provider, if the request is allowed.
pub provider: Option<String>,
/// The region selected for processing.
pub region: String,
/// Human-readable explanation of the decision.
pub reason: String,
}
// ---------------------------------------------------------------------------
// RegionRouter
// ---------------------------------------------------------------------------
/// Routes LLM requests to region-appropriate providers based on data residency rules.
///
/// Rules are evaluated in order. More specific rules (tenant-scoped) are checked
/// before global rules. The first matching rule determines the routing decision.
pub struct RegionRouter {
rules: Vec<RegionRule>,
default_region: String,
}
impl RegionRouter {
/// Create a new router with the given default region.
pub fn new(default_region: impl Into<String>) -> Self {
Self {
rules: Vec::new(),
default_region: default_region.into(),
}
}
/// Add a routing rule. Rules are evaluated in insertion order, with
/// tenant-specific rules taking priority over global ones during matching.
pub fn add_rule(&mut self, rule: RegionRule) {
self.rules.push(rule);
}
/// Check if a request is allowed and determine which provider/region to use.
///
/// Matching logic:
/// 1. Find all rules matching the classification (exact match).
/// 2. Among those, prefer tenant-specific rules if `tenant_id` is provided.
/// 3. Fall back to global rules (`tenant_id: None`).
/// 4. If no rule matches, the request is allowed with the preferred provider
/// in the default region.
pub fn route(
&self,
tenant_id: Option<&str>,
classification: &DataClassification,
preferred_provider: &str,
) -> RoutingDecision {
// Collect matching rules, split into tenant-specific and global
let mut tenant_rules: Vec<&RegionRule> = Vec::new();
let mut global_rules: Vec<&RegionRule> = Vec::new();
for rule in &self.rules {
if rule.data_classification != *classification {
continue;
}
match (&rule.tenant_id, tenant_id) {
(Some(rule_tid), Some(req_tid)) if rule_tid == req_tid => {
tenant_rules.push(rule);
}
(None, _) => {
global_rules.push(rule);
}
_ => {}
}
}
// Use tenant-specific rules first, then global
let applicable = if !tenant_rules.is_empty() {
tenant_rules
} else {
global_rules
};
if applicable.is_empty() {
// No rule matches — allow with defaults
return RoutingDecision {
allowed: true,
provider: Some(preferred_provider.to_string()),
region: self.default_region.clone(),
reason: format!(
"No rules matched for classification={classification}; using defaults"
),
};
}
// Evaluate against the first matching rule
let rule = applicable[0];
// Check blocked providers first (takes precedence)
let provider_lower = preferred_provider.to_lowercase();
if rule
.blocked_providers
.iter()
.any(|p| p.to_lowercase() == provider_lower)
{
// Provider is blocked. Try to find an alternative from allowed list.
let alternative = rule
.allowed_providers
.iter()
.find(|p| {
!rule
.blocked_providers
.iter()
.any(|b| b.to_lowercase() == p.to_lowercase())
})
.cloned();
if let Some(alt) = alternative {
let region = rule
.allowed_regions
.first()
.cloned()
.unwrap_or_else(|| self.default_region.clone());
return RoutingDecision {
allowed: true,
provider: Some(alt.clone()),
region,
reason: format!(
"Provider '{preferred_provider}' blocked for {classification} data; \
rerouted to '{alt}'"
),
};
}
// No alternative available
return RoutingDecision {
allowed: false,
provider: None,
region: rule
.allowed_regions
.first()
.cloned()
.unwrap_or_else(|| self.default_region.clone()),
reason: format!(
"Provider '{preferred_provider}' blocked for {classification} data \
and no alternative providers available"
),
};
}
// Check allowed providers (if the allowlist is non-empty, provider must be in it)
if !rule.allowed_providers.is_empty()
&& !rule
.allowed_providers
.iter()
.any(|p| p.to_lowercase() == provider_lower)
{
// Provider is not in the allowlist. Suggest the first allowed provider.
let suggestion = rule.allowed_providers.first().cloned();
if let Some(ref alt) = suggestion {
let region = rule
.allowed_regions
.first()
.cloned()
.unwrap_or_else(|| self.default_region.clone());
return RoutingDecision {
allowed: true,
provider: Some(alt.clone()),
region,
reason: format!(
"Provider '{preferred_provider}' not in allowlist for {classification} data; \
rerouted to '{alt}'"
),
};
}
return RoutingDecision {
allowed: false,
provider: None,
region: self.default_region.clone(),
reason: format!(
"Provider '{preferred_provider}' not in allowlist for {classification} data"
),
};
}
// Provider is acceptable — pick a region
let region = rule
.allowed_regions
.first()
.cloned()
.unwrap_or_else(|| self.default_region.clone());
RoutingDecision {
allowed: true,
provider: Some(preferred_provider.to_string()),
region,
reason: format!(
"Allowed: provider='{preferred_provider}', classification={classification}"
),
}
}
/// Get all allowed providers for a given tenant + classification combination.
///
/// If no rule matches, returns an empty list (meaning no restrictions).
pub fn allowed_providers(
&self,
tenant_id: Option<&str>,
classification: &DataClassification,
) -> Vec<String> {
// Collect matching rules (same priority logic as route())
let mut tenant_rules: Vec<&RegionRule> = Vec::new();
let mut global_rules: Vec<&RegionRule> = Vec::new();
for rule in &self.rules {
if rule.data_classification != *classification {
continue;
}
match (&rule.tenant_id, tenant_id) {
(Some(rule_tid), Some(req_tid)) if rule_tid == req_tid => {
tenant_rules.push(rule);
}
(None, _) => {
global_rules.push(rule);
}
_ => {}
}
}
let applicable = if !tenant_rules.is_empty() {
tenant_rules
} else {
global_rules
};
if applicable.is_empty() {
return Vec::new();
}
// Aggregate allowed providers from all matching rules, removing blocked ones
let mut allowed: Vec<String> = Vec::new();
let mut blocked: Vec<String> = Vec::new();
for rule in &applicable {
for p in &rule.allowed_providers {
if !allowed.iter().any(|a| a.to_lowercase() == p.to_lowercase()) {
allowed.push(p.clone());
}
}
for p in &rule.blocked_providers {
if !blocked.iter().any(|b| b.to_lowercase() == p.to_lowercase()) {
blocked.push(p.clone());
}
}
}
// Remove blocked from allowed
allowed.retain(|a| !blocked.iter().any(|b| b.to_lowercase() == a.to_lowercase()));
allowed
}
/// Validate the router configuration and return a list of issues found.
///
/// Checks for:
/// - Rules with empty allowed_regions
/// - Providers that appear in both allowed and blocked lists
/// - Duplicate rules for the same tenant + classification
/// - Rules with no allowed providers and non-empty blocked list
pub fn validate(&self) -> Vec<String> {
let mut issues = Vec::new();
for (i, rule) in self.rules.iter().enumerate() {
let label = match &rule.tenant_id {
Some(tid) => format!(
"Rule #{} (tenant={}, class={})",
i + 1,
tid,
rule.data_classification
),
None => format!(
"Rule #{} (global, class={})",
i + 1,
rule.data_classification
),
};
// Empty allowed_regions
if rule.allowed_regions.is_empty() {
issues.push(format!("{label}: allowed_regions is empty"));
}
// Provider in both allowed and blocked
for p in &rule.allowed_providers {
if rule
.blocked_providers
.iter()
.any(|b| b.to_lowercase() == p.to_lowercase())
{
issues.push(format!(
"{label}: provider '{p}' appears in both allowed and blocked lists"
));
}
}
}
// Check for duplicate rules (same tenant + classification)
for i in 0..self.rules.len() {
for j in (i + 1)..self.rules.len() {
let a = &self.rules[i];
let b = &self.rules[j];
if a.tenant_id == b.tenant_id && a.data_classification == b.data_classification {
let label = match &a.tenant_id {
Some(tid) => format!("tenant={tid}, class={}", a.data_classification),
None => format!("global, class={}", a.data_classification),
};
issues.push(format!(
"Duplicate rules #{} and #{} for ({label})",
i + 1,
j + 1
));
}
}
}
issues
}
/// Return a reference to the default region.
pub fn default_region(&self) -> &str {
&self.default_region
}
/// Return the number of configured rules.
pub fn rule_count(&self) -> usize {
self.rules.len()
}
}
// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------
#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
use super::*;
// -- Helpers --
fn eu_restricted_rule() -> RegionRule {
RegionRule {
tenant_id: None,
data_classification: DataClassification::Restricted,
allowed_regions: vec!["eu-west-1".into(), "eu-central-1".into()],
allowed_providers: vec!["claude".into(), "gemini".into()],
blocked_providers: vec!["openai".into()],
}
}
fn public_any_rule() -> RegionRule {
RegionRule {
tenant_id: None,
data_classification: DataClassification::Public,
allowed_regions: vec!["us-east-1".into(), "eu-west-1".into()],
allowed_providers: vec![],
blocked_providers: vec![],
}
}
fn tenant_acme_confidential() -> RegionRule {
RegionRule {
tenant_id: Some("acme-corp".into()),
data_classification: DataClassification::Confidential,
allowed_regions: vec!["eu-west-1".into()],
allowed_providers: vec!["claude".into()],
blocked_providers: vec![],
}
}
// -- Test: default routing with no rules --
#[test]
fn test_default_routing_no_rules() {
let router = RegionRouter::new("us-east-1");
let decision = router.route(None, &DataClassification::Public, "openai");
assert!(decision.allowed);
assert_eq!(decision.provider, Some("openai".to_string()));
assert_eq!(decision.region, "us-east-1");
}
// -- Test: restricted data blocks US-only providers --
#[test]
fn test_restricted_data_blocks_openai() {
let mut router = RegionRouter::new("us-east-1");
router.add_rule(eu_restricted_rule());
let decision = router.route(None, &DataClassification::Restricted, "openai");
// openai is blocked, but claude is available as alternative
assert!(decision.allowed);
assert_eq!(decision.provider, Some("claude".to_string()));
assert_eq!(decision.region, "eu-west-1");
assert!(decision.reason.contains("blocked"));
}
// -- Test: restricted data allows claude --
#[test]
fn test_restricted_data_allows_claude() {
let mut router = RegionRouter::new("us-east-1");
router.add_rule(eu_restricted_rule());
let decision = router.route(None, &DataClassification::Restricted, "claude");
assert!(decision.allowed);
assert_eq!(decision.provider, Some("claude".to_string()));
assert_eq!(decision.region, "eu-west-1");
}
// -- Test: public data with no restrictions --
#[test]
fn test_public_data_any_provider() {
let mut router = RegionRouter::new("us-east-1");
router.add_rule(public_any_rule());
let decision = router.route(None, &DataClassification::Public, "openai");
assert!(decision.allowed);
assert_eq!(decision.provider, Some("openai".to_string()));
assert_eq!(decision.region, "us-east-1");
}
// -- Test: tenant-specific rules override global --
#[test]
fn test_tenant_specific_overrides_global() {
let mut router = RegionRouter::new("us-east-1");
// Global rule: confidential data can use any provider
router.add_rule(RegionRule {
tenant_id: None,
data_classification: DataClassification::Confidential,
allowed_regions: vec!["us-east-1".into()],
allowed_providers: vec![],
blocked_providers: vec![],
});
// Tenant rule: acme-corp confidential data must use claude in eu-west-1
router.add_rule(tenant_acme_confidential());
// Non-acme tenant gets global rule
let d1 = router.route(
Some("other-corp"),
&DataClassification::Confidential,
"openai",
);
assert!(d1.allowed);
assert_eq!(d1.provider, Some("openai".to_string()));
assert_eq!(d1.region, "us-east-1");
// Acme tenant gets tenant-specific rule
let d2 = router.route(
Some("acme-corp"),
&DataClassification::Confidential,
"openai",
);
assert!(d2.allowed);
// openai is not in allowed list, so gets rerouted to claude
assert_eq!(d2.provider, Some("claude".to_string()));
assert_eq!(d2.region, "eu-west-1");
}
// -- Test: classification levels are independent --
#[test]
fn test_classification_levels_independent() {
let mut router = RegionRouter::new("us-east-1");
router.add_rule(eu_restricted_rule()); // Only applies to Restricted
// Internal data should have no restrictions (no matching rule)
let decision = router.route(None, &DataClassification::Internal, "openai");
assert!(decision.allowed);
assert_eq!(decision.provider, Some("openai".to_string()));
assert_eq!(decision.region, "us-east-1");
}
// -- Test: blocked provider with no alternatives --
#[test]
fn test_blocked_no_alternatives_denied() {
let mut router = RegionRouter::new("us-east-1");
router.add_rule(RegionRule {
tenant_id: None,
data_classification: DataClassification::Restricted,
allowed_regions: vec!["eu-west-1".into()],
allowed_providers: vec![],
blocked_providers: vec!["openai".into()],
});
let decision = router.route(None, &DataClassification::Restricted, "openai");
assert!(!decision.allowed);
assert!(decision.provider.is_none());
assert!(decision.reason.contains("blocked"));
}
// -- Test: validation detects empty regions --
#[test]
fn test_validate_empty_regions() {
let mut router = RegionRouter::new("us-east-1");
router.add_rule(RegionRule {
tenant_id: None,
data_classification: DataClassification::Public,
allowed_regions: vec![],
allowed_providers: vec![],
blocked_providers: vec![],
});
let issues = router.validate();
assert!(!issues.is_empty());
assert!(issues[0].contains("allowed_regions is empty"));
}
// -- Test: validation detects provider in both lists --
#[test]
fn test_validate_provider_in_both_lists() {
let mut router = RegionRouter::new("us-east-1");
router.add_rule(RegionRule {
tenant_id: None,
data_classification: DataClassification::Internal,
allowed_regions: vec!["us-east-1".into()],
allowed_providers: vec!["openai".into()],
blocked_providers: vec!["openai".into()],
});
let issues = router.validate();
assert!(issues
.iter()
.any(|i| i.contains("both allowed and blocked")));
}
// -- Test: validation detects duplicate rules --
#[test]
fn test_validate_duplicate_rules() {
let mut router = RegionRouter::new("us-east-1");
router.add_rule(RegionRule {
tenant_id: None,
data_classification: DataClassification::Public,
allowed_regions: vec!["us-east-1".into()],
allowed_providers: vec![],
blocked_providers: vec![],
});
router.add_rule(RegionRule {
tenant_id: None,
data_classification: DataClassification::Public,
allowed_regions: vec!["eu-west-1".into()],
allowed_providers: vec![],
blocked_providers: vec![],
});
let issues = router.validate();
assert!(issues.iter().any(|i| i.contains("Duplicate rules")));
}
// -- Test: allowed_providers returns correct list --
#[test]
fn test_allowed_providers_list() {
let mut router = RegionRouter::new("us-east-1");
router.add_rule(eu_restricted_rule());
let providers = router.allowed_providers(None, &DataClassification::Restricted);
assert_eq!(providers.len(), 2);
assert!(providers.contains(&"claude".to_string()));
assert!(providers.contains(&"gemini".to_string()));
}
// -- Test: allowed_providers empty when no rule matches --
#[test]
fn test_allowed_providers_empty_no_match() {
let router = RegionRouter::new("us-east-1");
let providers = router.allowed_providers(None, &DataClassification::Public);
assert!(providers.is_empty());
}
// -- Test: data classification display --
#[test]
fn test_data_classification_display() {
assert_eq!(DataClassification::Public.to_string(), "Public");
assert_eq!(DataClassification::Internal.to_string(), "Internal");
assert_eq!(DataClassification::Confidential.to_string(), "Confidential");
assert_eq!(DataClassification::Restricted.to_string(), "Restricted");
}
// -- Test: data classification sensitivity ordering --
#[test]
fn test_data_classification_sensitivity() {
assert!(
DataClassification::Public.sensitivity() < DataClassification::Internal.sensitivity()
);
assert!(
DataClassification::Internal.sensitivity()
< DataClassification::Confidential.sensitivity()
);
assert!(
DataClassification::Confidential.sensitivity()
< DataClassification::Restricted.sensitivity()
);
}
// -- Test: multiple rules for different classifications --
#[test]
fn test_multiple_classification_rules() {
let mut router = RegionRouter::new("us-east-1");
router.add_rule(eu_restricted_rule());
router.add_rule(RegionRule {
tenant_id: None,
data_classification: DataClassification::Confidential,
allowed_regions: vec!["us-east-1".into(), "eu-west-1".into()],
allowed_providers: vec!["claude".into(), "openai".into()],
blocked_providers: vec![],
});
// Restricted: openai blocked
let d1 = router.route(None, &DataClassification::Restricted, "openai");
assert!(d1.allowed);
assert_eq!(d1.provider, Some("claude".to_string()));
// Confidential: openai allowed
let d2 = router.route(None, &DataClassification::Confidential, "openai");
assert!(d2.allowed);
assert_eq!(d2.provider, Some("openai".to_string()));
}
// -- Test: provider not in allowlist gets rerouted --
#[test]
fn test_provider_not_in_allowlist_rerouted() {
let mut router = RegionRouter::new("us-east-1");
router.add_rule(RegionRule {
tenant_id: None,
data_classification: DataClassification::Internal,
allowed_regions: vec!["eu-west-1".into()],
allowed_providers: vec!["claude".into(), "gemini".into()],
blocked_providers: vec![],
});
let decision = router.route(None, &DataClassification::Internal, "mistral");
assert!(decision.allowed);
assert_eq!(decision.provider, Some("claude".to_string()));
assert!(decision.reason.contains("not in allowlist"));
}
// -- Test: serialization round-trip --
#[test]
fn test_serialization_roundtrip() {
let rule = eu_restricted_rule();
let json = serde_json::to_string(&rule).unwrap();
let deserialized: RegionRule = serde_json::from_str(&json).unwrap();
assert_eq!(
deserialized.data_classification,
DataClassification::Restricted
);
assert_eq!(deserialized.allowed_regions.len(), 2);
assert_eq!(deserialized.blocked_providers.len(), 1);
}
// -- Test: routing decision serialization --
#[test]
fn test_routing_decision_serialization() {
let decision = RoutingDecision {
allowed: true,
provider: Some("claude".to_string()),
region: "eu-west-1".to_string(),
reason: "Allowed by rule".to_string(),
};
let json = serde_json::to_string(&decision).unwrap();
let d: RoutingDecision = serde_json::from_str(&json).unwrap();
assert!(d.allowed);
assert_eq!(d.provider.unwrap(), "claude");
}
// -- Test: valid configuration has no issues --
#[test]
fn test_valid_config_no_issues() {
let mut router = RegionRouter::new("us-east-1");
router.add_rule(eu_restricted_rule());
router.add_rule(public_any_rule());
let issues = router.validate();
assert!(issues.is_empty(), "Expected no issues, got: {issues:?}");
}
// -- Test: case-insensitive provider matching --
#[test]
fn test_case_insensitive_provider() {
let mut router = RegionRouter::new("us-east-1");
router.add_rule(eu_restricted_rule()); // blocks "openai"
let decision = router.route(None, &DataClassification::Restricted, "OpenAI");
// Should still be blocked despite different casing
assert!(decision.allowed);
assert_eq!(decision.provider, Some("claude".to_string()));
assert!(decision.reason.contains("blocked"));
}
}