policy-rs 1.6.0

Policy library for working with protobuf-defined policy objects
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
//! Policy registry for managing policies from multiple providers.
//!
//! The registry aggregates policies from multiple providers and provides
//! lock-free access to an immutable snapshot of all policies.

use std::collections::HashMap;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, RwLock};

use crate::Policy;
use crate::engine::CompiledMatchers;
use crate::engine::signal::{LogSignal, MetricSignal, TraceSignal};
use crate::error::PolicyError;
use crate::provider::{PolicyProvider, StatsCollector};

/// Unique identifier for a registered provider.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ProviderId(u64);

/// Statistics for a transform stage (remove, redact, rename, add).
#[derive(Debug, Default)]
pub struct TransformStageStats {
    /// Number of times this stage was applied successfully.
    pub hits: AtomicU64,
    /// Number of times this stage was evaluated but the field selected nothing.
    pub misses: AtomicU64,
}

impl TransformStageStats {
    /// Record a successful transform application.
    pub fn record_hit(&self) {
        self.hits.fetch_add(1, Ordering::Relaxed);
    }

    /// Record a failed transform application (field not found).
    pub fn record_miss(&self) {
        self.misses.fetch_add(1, Ordering::Relaxed);
    }

    /// Get current hit count.
    pub fn hits(&self) -> u64 {
        self.hits.load(Ordering::Relaxed)
    }

    /// Get current miss count.
    pub fn misses(&self) -> u64 {
        self.misses.load(Ordering::Relaxed)
    }

    /// Reset stats and return previous values.
    pub fn reset(&self) -> (u64, u64) {
        let hits = self.hits.swap(0, Ordering::Relaxed);
        let misses = self.misses.swap(0, Ordering::Relaxed);
        (hits, misses)
    }
}

/// Statistics for a single policy.
#[derive(Debug, Default)]
pub struct PolicyStats {
    /// Number of times this policy matched telemetry.
    pub match_hits: AtomicU64,
    /// Number of times this policy was evaluated but did not match.
    pub match_misses: AtomicU64,
    /// Statistics for the remove transform stage.
    pub remove: TransformStageStats,
    /// Statistics for the redact transform stage.
    pub redact: TransformStageStats,
    /// Statistics for the rename transform stage.
    pub rename: TransformStageStats,
    /// Statistics for the add transform stage.
    pub add: TransformStageStats,
}

impl PolicyStats {
    /// Increment match hits.
    pub fn record_hit(&self) {
        self.match_hits.fetch_add(1, Ordering::Relaxed);
    }

    /// Increment match misses.
    pub fn record_miss(&self) {
        self.match_misses.fetch_add(1, Ordering::Relaxed);
    }

    /// Get current hit count.
    pub fn hits(&self) -> u64 {
        self.match_hits.load(Ordering::Relaxed)
    }

    /// Get current miss count.
    pub fn misses(&self) -> u64 {
        self.match_misses.load(Ordering::Relaxed)
    }

    /// Reset match stats and return previous values.
    pub fn reset(&self) -> (u64, u64) {
        let hits = self.match_hits.swap(0, Ordering::Relaxed);
        let misses = self.match_misses.swap(0, Ordering::Relaxed);
        (hits, misses)
    }

    /// Reset all stats (match + transform stages) and return previous values.
    pub fn reset_all(&self) -> PolicyStatsSnapshot {
        PolicyStatsSnapshot {
            match_hits: self.match_hits.swap(0, Ordering::Relaxed),
            match_misses: self.match_misses.swap(0, Ordering::Relaxed),
            remove: self.remove.reset(),
            redact: self.redact.reset(),
            rename: self.rename.reset(),
            add: self.add.reset(),
        }
    }
}

/// A snapshot of all policy stats for reporting.
#[derive(Debug, Clone, Default)]
pub struct PolicyStatsSnapshot {
    pub match_hits: u64,
    pub match_misses: u64,
    pub remove: (u64, u64),
    pub redact: (u64, u64),
    pub rename: (u64, u64),
    pub add: (u64, u64),
}

/// A policy with its associated provider and stats.
#[derive(Debug)]
pub struct PolicyEntry {
    /// The policy itself.
    pub policy: Policy,
    /// The provider this policy came from.
    pub provider_id: ProviderId,
    /// Statistics for this policy.
    pub stats: Arc<PolicyStats>,
}

/// An immutable snapshot of all policies.
///
/// This is cheap to clone (just an Arc) and provides lock-free read access.
#[derive(Debug, Clone)]
pub struct PolicySnapshot {
    inner: Arc<SnapshotInner>,
}

#[derive(Debug)]
struct SnapshotInner {
    /// All policies indexed by ID.
    policies: Vec<PolicyEntry>,
    /// Index from policy ID to position in policies vec.
    index: HashMap<String, usize>,
    /// Compiled matchers for log policies.
    compiled_logs: Option<CompiledMatchers<LogSignal>>,
    /// Compiled matchers for metric policies.
    compiled_metrics: Option<CompiledMatchers<MetricSignal>>,
    /// Compiled matchers for trace policies.
    compiled_traces: Option<CompiledMatchers<TraceSignal>>,
}

impl PolicySnapshot {
    /// Create an empty snapshot.
    fn empty() -> Self {
        Self {
            inner: Arc::new(SnapshotInner {
                policies: Vec::new(),
                index: HashMap::new(),
                compiled_logs: None,
                compiled_metrics: None,
                compiled_traces: None,
            }),
        }
    }

    /// Get all policies.
    pub fn policies(&self) -> &[PolicyEntry] {
        &self.inner.policies
    }

    /// Get a policy by ID.
    pub fn get(&self, id: &str) -> Option<&PolicyEntry> {
        self.inner
            .index
            .get(id)
            .map(|&idx| &self.inner.policies[idx])
    }

    /// Get the number of policies.
    pub fn len(&self) -> usize {
        self.inner.policies.len()
    }

    /// Check if the snapshot is empty.
    pub fn is_empty(&self) -> bool {
        self.inner.policies.is_empty()
    }

    /// Iterate over all policies.
    pub fn iter(&self) -> impl Iterator<Item = &PolicyEntry> {
        self.inner.policies.iter()
    }

    /// Get the compiled log matchers for efficient evaluation.
    pub fn compiled_log_matchers(&self) -> Option<&CompiledMatchers<LogSignal>> {
        self.inner.compiled_logs.as_ref()
    }

    /// Get the compiled metric matchers for efficient evaluation.
    pub fn compiled_metric_matchers(&self) -> Option<&CompiledMatchers<MetricSignal>> {
        self.inner.compiled_metrics.as_ref()
    }

    /// Get the compiled trace matchers for efficient evaluation.
    pub fn compiled_trace_matchers(&self) -> Option<&CompiledMatchers<TraceSignal>> {
        self.inner.compiled_traces.as_ref()
    }
}

/// A policy with its stats, used internally by the registry.
type PolicyWithStats = (Policy, Arc<PolicyStats>);

/// Policies grouped by provider.
type ProviderPolicies = HashMap<ProviderId, Vec<PolicyWithStats>>;

/// Callback handle returned when registering a provider.
///
/// The provider uses this to notify the registry of policy updates.
#[derive(Clone)]
pub struct ProviderHandle {
    provider_id: ProviderId,
    registry: Arc<RegistryInner>,
}

impl ProviderHandle {
    /// Update the policies for this provider.
    ///
    /// This replaces all policies from this provider with the new set.
    pub fn update(&self, policies: Vec<Policy>) {
        self.registry.update_provider(self.provider_id, policies);
    }

    /// Get the provider ID.
    pub fn provider_id(&self) -> ProviderId {
        self.provider_id
    }
}

/// Internal state of the registry.
struct RegistryInner {
    /// Counter for generating unique provider IDs.
    next_provider_id: AtomicU64,
    /// Policies grouped by provider, protected by a lock for writes.
    providers: RwLock<ProviderPolicies>,
    /// The current snapshot, atomically swapped on updates.
    snapshot: RwLock<PolicySnapshot>,
}

impl RegistryInner {
    fn new() -> Self {
        Self {
            next_provider_id: AtomicU64::new(0),
            providers: RwLock::new(HashMap::new()),
            snapshot: RwLock::new(PolicySnapshot::empty()),
        }
    }

    fn register_provider(&self) -> ProviderId {
        let id = ProviderId(self.next_provider_id.fetch_add(1, Ordering::Relaxed));
        let mut providers = self.providers.write().unwrap();
        providers.insert(id, Vec::new());
        id
    }

    fn update_provider(&self, provider_id: ProviderId, policies: Vec<Policy>) {
        // Create new stats for each policy, preserving existing stats where possible
        let mut providers = self.providers.write().unwrap();

        // Build a map of existing stats by policy ID
        let existing_stats: HashMap<String, Arc<PolicyStats>> = providers
            .get(&provider_id)
            .map(|entries| {
                entries
                    .iter()
                    .map(|(p, s)| (p.id().to_string(), Arc::clone(s)))
                    .collect()
            })
            .unwrap_or_default();

        // Create new entries, reusing stats where policy ID matches
        let new_entries: Vec<(Policy, Arc<PolicyStats>)> = policies
            .into_iter()
            .map(|policy| {
                let stats = existing_stats.get(policy.id()).cloned().unwrap_or_default();
                (policy, stats)
            })
            .collect();

        providers.insert(provider_id, new_entries);

        // Rebuild the snapshot
        self.rebuild_snapshot(&providers);
    }

    fn rebuild_snapshot(&self, providers: &ProviderPolicies) {
        let mut policies = Vec::new();
        let mut index = HashMap::new();

        // Collect all policies with their stats, partitioned by signal type
        let mut log_policies: Vec<(Policy, Arc<PolicyStats>)> = Vec::new();
        let mut metric_policies: Vec<(Policy, Arc<PolicyStats>)> = Vec::new();
        let mut trace_policies: Vec<(Policy, Arc<PolicyStats>)> = Vec::new();

        for (&provider_id, entries) in providers {
            for (policy, stats) in entries {
                let idx = policies.len();
                index.insert(policy.id().to_string(), idx);
                policies.push(PolicyEntry {
                    policy: policy.clone(),
                    provider_id,
                    stats: Arc::clone(stats),
                });

                if policy.log_target().is_some() {
                    log_policies.push((policy.clone(), Arc::clone(stats)));
                } else if policy.metric_target().is_some() {
                    metric_policies.push((policy.clone(), Arc::clone(stats)));
                } else if policy.trace_target().is_some() {
                    trace_policies.push((policy.clone(), Arc::clone(stats)));
                }
            }
        }

        // Sort by policy ID for deterministic transform ordering (spec requirement)
        log_policies.sort_by(|a, b| a.0.id().cmp(b.0.id()));
        metric_policies.sort_by(|a, b| a.0.id().cmp(b.0.id()));
        trace_policies.sort_by(|a, b| a.0.id().cmp(b.0.id()));

        // Compile log matchers
        let compiled_logs = if !log_policies.is_empty() {
            match CompiledMatchers::<LogSignal>::build(log_policies.into_iter()) {
                Ok(matchers) => Some(matchers),
                Err(e) => {
                    eprintln!("Failed to compile log policy matchers: {}", e);
                    None
                }
            }
        } else {
            None
        };

        // Compile metric matchers
        let compiled_metrics = if !metric_policies.is_empty() {
            match CompiledMatchers::<MetricSignal>::build(metric_policies.into_iter()) {
                Ok(matchers) => Some(matchers),
                Err(e) => {
                    eprintln!("Failed to compile metric policy matchers: {}", e);
                    None
                }
            }
        } else {
            None
        };

        // Compile trace matchers
        let compiled_traces = if !trace_policies.is_empty() {
            match CompiledMatchers::<TraceSignal>::build(trace_policies.into_iter()) {
                Ok(matchers) => Some(matchers),
                Err(e) => {
                    eprintln!("Failed to compile trace policy matchers: {}", e);
                    None
                }
            }
        } else {
            None
        };

        let new_snapshot = PolicySnapshot {
            inner: Arc::new(SnapshotInner {
                policies,
                index,
                compiled_logs,
                compiled_metrics,
                compiled_traces,
            }),
        };

        let mut snapshot = self.snapshot.write().unwrap();
        *snapshot = new_snapshot;
    }

    fn snapshot(&self) -> PolicySnapshot {
        self.snapshot.read().unwrap().clone()
    }
}

/// Registry for managing policies from multiple providers.
///
/// The registry aggregates policies from multiple providers and provides
/// lock-free access to an immutable snapshot of all policies.
///
/// # Example
///
/// ```ignore
/// let registry = PolicyRegistry::new();
///
/// // Subscribe to a provider - the registry will receive updates automatically
/// let provider = FileProvider::new("policies.json");
/// registry.subscribe(&provider)?;
///
/// // Engine gets a snapshot for evaluation (O(1), lock-free)
/// let snapshot = registry.snapshot();
/// for entry in snapshot.iter() {
///     // evaluate policy...
///     entry.stats.record_hit();
/// }
/// ```
pub struct PolicyRegistry {
    inner: Arc<RegistryInner>,
}

impl PolicyRegistry {
    /// Create a new empty registry.
    pub fn new() -> Self {
        Self {
            inner: Arc::new(RegistryInner::new()),
        }
    }

    /// Subscribe to a policy provider.
    ///
    /// The registry will receive policy updates from this provider automatically.
    /// The provider's callback will be invoked immediately with the current policies,
    /// and again whenever the provider detects changes.
    ///
    /// Returns the provider ID on success, or an error if the initial load fails.
    pub fn subscribe(&self, provider: &dyn PolicyProvider) -> Result<ProviderId, PolicyError> {
        let handle = self.register_provider();
        let provider_id = handle.provider_id();

        // Wire up stats collection so providers can report policy statistics
        let inner = Arc::clone(&self.inner);
        let collector: StatsCollector = Arc::new(move || {
            let snapshot = inner.snapshot();
            snapshot
                .iter()
                .map(|entry| (entry.policy.id().to_string(), entry.stats.reset_all()))
                .collect()
        });
        provider.set_stats_collector(collector);

        // Create a callback that updates the registry
        let callback = {
            let handle = handle.clone();
            Arc::new(move |policies: Vec<Policy>| {
                handle.update(policies);
            })
        };

        // Subscribe to the provider - this will invoke the callback immediately
        provider.subscribe(callback)?;

        Ok(provider_id)
    }

    /// Register a new provider and return a handle for manual updates.
    ///
    /// This is a lower-level API for providers that don't implement the
    /// subscription model. Prefer `subscribe()` when possible.
    ///
    /// The handle can be cloned and used from any thread to push
    /// policy updates to the registry.
    pub fn register_provider(&self) -> ProviderHandle {
        let provider_id = self.inner.register_provider();
        ProviderHandle {
            provider_id,
            registry: Arc::clone(&self.inner),
        }
    }

    /// Get a snapshot of all policies.
    ///
    /// This is O(1) and lock-free - it just clones an Arc.
    /// The snapshot is immutable and can be used without blocking
    /// the registry from receiving updates.
    pub fn snapshot(&self) -> PolicySnapshot {
        self.inner.snapshot()
    }

    /// Get the number of registered providers.
    pub fn provider_count(&self) -> usize {
        self.inner.providers.read().unwrap().len()
    }
}

impl Default for PolicyRegistry {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::proto::tero::policy::v1::Policy as ProtoPolicy;

    fn make_policy(id: &str) -> Policy {
        Policy::new(ProtoPolicy {
            id: id.to_string(),
            name: id.to_string(),
            enabled: true,
            ..Default::default()
        })
    }

    #[test]
    fn empty_registry() {
        let registry = PolicyRegistry::new();
        let snapshot = registry.snapshot();
        assert!(snapshot.is_empty());
        assert_eq!(snapshot.len(), 0);
    }

    #[test]
    fn register_provider() {
        let registry = PolicyRegistry::new();
        let handle = registry.register_provider();
        assert_eq!(registry.provider_count(), 1);
        assert_eq!(handle.provider_id(), ProviderId(0));

        let handle2 = registry.register_provider();
        assert_eq!(registry.provider_count(), 2);
        assert_eq!(handle2.provider_id(), ProviderId(1));
    }

    #[test]
    fn update_policies() {
        let registry = PolicyRegistry::new();
        let handle = registry.register_provider();

        let policies = vec![make_policy("policy-1"), make_policy("policy-2")];
        handle.update(policies);

        let snapshot = registry.snapshot();
        assert_eq!(snapshot.len(), 2);
        assert!(snapshot.get("policy-1").is_some());
        assert!(snapshot.get("policy-2").is_some());
    }

    #[test]
    fn multiple_providers() {
        let registry = PolicyRegistry::new();
        let handle1 = registry.register_provider();
        let handle2 = registry.register_provider();

        handle1.update(vec![make_policy("provider1-policy")]);
        handle2.update(vec![make_policy("provider2-policy")]);

        let snapshot = registry.snapshot();
        assert_eq!(snapshot.len(), 2);

        let entry1 = snapshot.get("provider1-policy").unwrap();
        let entry2 = snapshot.get("provider2-policy").unwrap();
        assert_eq!(entry1.provider_id, ProviderId(0));
        assert_eq!(entry2.provider_id, ProviderId(1));
    }

    #[test]
    fn update_replaces_policies() {
        let registry = PolicyRegistry::new();
        let handle = registry.register_provider();

        handle.update(vec![make_policy("old-policy")]);
        assert_eq!(registry.snapshot().len(), 1);
        assert!(registry.snapshot().get("old-policy").is_some());

        handle.update(vec![make_policy("new-policy")]);
        assert_eq!(registry.snapshot().len(), 1);
        assert!(registry.snapshot().get("old-policy").is_none());
        assert!(registry.snapshot().get("new-policy").is_some());
    }

    #[test]
    fn stats_preserved_on_update() {
        let registry = PolicyRegistry::new();
        let handle = registry.register_provider();

        handle.update(vec![make_policy("policy-1")]);
        let snapshot1 = registry.snapshot();
        let entry1 = snapshot1.get("policy-1").unwrap();
        entry1.stats.record_hit();
        entry1.stats.record_hit();
        assert_eq!(entry1.stats.hits(), 2);

        // Update with same policy ID - stats should be preserved
        handle.update(vec![make_policy("policy-1")]);
        let snapshot2 = registry.snapshot();
        let entry2 = snapshot2.get("policy-1").unwrap();
        assert_eq!(entry2.stats.hits(), 2);
    }

    #[test]
    fn snapshot_is_immutable() {
        let registry = PolicyRegistry::new();
        let handle = registry.register_provider();

        handle.update(vec![make_policy("policy-1")]);
        let snapshot1 = registry.snapshot();
        assert_eq!(snapshot1.len(), 1);

        // Update registry
        handle.update(vec![make_policy("policy-1"), make_policy("policy-2")]);

        // Original snapshot unchanged
        assert_eq!(snapshot1.len(), 1);

        // New snapshot has updates
        let snapshot2 = registry.snapshot();
        assert_eq!(snapshot2.len(), 2);
    }

    #[test]
    fn snapshot_clone_is_cheap() {
        let registry = PolicyRegistry::new();
        let handle = registry.register_provider();
        handle.update(vec![make_policy("policy-1")]);

        let snapshot1 = registry.snapshot();
        let snapshot2 = snapshot1.clone();

        // Both point to same underlying data
        assert!(Arc::ptr_eq(&snapshot1.inner, &snapshot2.inner));
    }

    #[test]
    fn stats_recording() {
        let registry = PolicyRegistry::new();
        let handle = registry.register_provider();
        handle.update(vec![make_policy("policy-1")]);

        let snapshot = registry.snapshot();
        let entry = snapshot.get("policy-1").unwrap();

        entry.stats.record_hit();
        entry.stats.record_hit();
        entry.stats.record_miss();

        assert_eq!(entry.stats.hits(), 2);
        assert_eq!(entry.stats.misses(), 1);

        let (hits, misses) = entry.stats.reset();
        assert_eq!(hits, 2);
        assert_eq!(misses, 1);
        assert_eq!(entry.stats.hits(), 0);
        assert_eq!(entry.stats.misses(), 0);
    }

    #[test]
    fn iterate_policies() {
        let registry = PolicyRegistry::new();
        let handle = registry.register_provider();
        handle.update(vec![
            make_policy("policy-1"),
            make_policy("policy-2"),
            make_policy("policy-3"),
        ]);

        let snapshot = registry.snapshot();
        let ids: Vec<&str> = snapshot.iter().map(|e| e.policy.id()).collect();
        assert_eq!(ids.len(), 3);
        assert!(ids.contains(&"policy-1"));
        assert!(ids.contains(&"policy-2"));
        assert!(ids.contains(&"policy-3"));
    }

    #[test]
    fn subscribe_to_file_provider() {
        use crate::provider::FileProvider;

        let registry = PolicyRegistry::new();
        let provider = FileProvider::new("testdata/policies.json");

        let provider_id = registry.subscribe(&provider).unwrap();
        assert_eq!(provider_id, ProviderId(0));
        assert_eq!(registry.provider_count(), 1);

        let snapshot = registry.snapshot();
        assert_eq!(snapshot.len(), 16);

        // Verify policies came from the correct provider
        for entry in snapshot.iter() {
            assert_eq!(entry.provider_id, provider_id);
        }
    }

    #[test]
    fn subscribe_to_multiple_providers() {
        use crate::provider::FileProvider;

        let registry = PolicyRegistry::new();

        // Subscribe to same file twice (simulating multiple providers)
        let provider1 = FileProvider::new("testdata/policies.json");
        let provider2 = FileProvider::new("testdata/policies.json");

        let id1 = registry.subscribe(&provider1).unwrap();
        let id2 = registry.subscribe(&provider2).unwrap();

        assert_ne!(id1, id2);
        assert_eq!(registry.provider_count(), 2);

        // Each provider contributes its own policies, so we have 32 total
        // (16 from each provider). The snapshot index will map each policy ID
        // to the last occurrence, but all 32 entries are in the policies vec.
        let snapshot = registry.snapshot();
        assert_eq!(snapshot.len(), 32);

        // Verify we have policies from both providers
        let provider1_count = snapshot.iter().filter(|e| e.provider_id == id1).count();
        let provider2_count = snapshot.iter().filter(|e| e.provider_id == id2).count();
        assert_eq!(provider1_count, 16);
        assert_eq!(provider2_count, 16);
    }

    #[test]
    fn snapshot_policies_method() {
        let registry = PolicyRegistry::new();
        let handle = registry.register_provider();
        handle.update(vec![make_policy("policy-1"), make_policy("policy-2")]);

        let snapshot = registry.snapshot();
        let policies = snapshot.policies();

        assert_eq!(policies.len(), 2);
        assert!(policies.iter().any(|e| e.policy.id() == "policy-1"));
        assert!(policies.iter().any(|e| e.policy.id() == "policy-2"));
    }

    #[test]
    fn registry_default() {
        let registry = PolicyRegistry::default();
        assert!(registry.snapshot().is_empty());
        assert_eq!(registry.provider_count(), 0);
    }

    #[test]
    fn subscribe_auto_wires_stats_collector() {
        use crate::provider::{PolicyCallback, PolicyProvider, StatsCollector};
        use std::sync::RwLock;

        /// A test provider that captures the stats collector set by the registry.
        struct SpyProvider {
            policies: Vec<Policy>,
            collector: RwLock<Option<StatsCollector>>,
        }

        impl PolicyProvider for SpyProvider {
            fn set_stats_collector(&self, collector: StatsCollector) {
                *self.collector.write().unwrap() = Some(collector);
            }

            fn subscribe(&self, callback: PolicyCallback) -> Result<(), PolicyError> {
                callback(self.policies.clone());
                Ok(())
            }
        }

        let provider = SpyProvider {
            policies: vec![make_policy("policy-1"), make_policy("policy-2")],
            collector: RwLock::new(None),
        };

        let registry = PolicyRegistry::new();
        registry.subscribe(&provider).unwrap();

        // Verify the collector was set
        let collector = provider.collector.read().unwrap();
        assert!(collector.is_some(), "stats collector should be auto-wired");

        // Record some stats
        let snapshot = registry.snapshot();
        let entry = snapshot.get("policy-1").unwrap();
        entry.stats.record_hit();
        entry.stats.record_hit();
        entry.stats.record_miss();

        // Call the collector and verify it returns the stats
        let stats = collector.as_ref().unwrap()();
        assert_eq!(stats.len(), 2);

        let p1_stats = stats.iter().find(|(id, _)| id == "policy-1").unwrap();
        assert_eq!(p1_stats.1.match_hits, 2);
        assert_eq!(p1_stats.1.match_misses, 1);

        // Stats should be reset after collection
        let stats_again = collector.as_ref().unwrap()();
        let p1_again = stats_again.iter().find(|(id, _)| id == "policy-1").unwrap();
        assert_eq!(p1_again.1.match_hits, 0);
        assert_eq!(p1_again.1.match_misses, 0);
    }
}