torsh-package 0.1.2

Model packaging and distribution utilities for ToRSh
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
//! Package Replication and High Availability
//!
//! This module provides comprehensive replication and high availability capabilities
//! for package distribution across multiple regions and nodes, including consistency
//! management, automatic failover, load balancing, and conflict resolution.
//!
//! # Features
//!
//! - **Multi-Region Replication**: Replicate packages across geographic regions
//! - **Consistency Models**: Support for eventual, strong, and causal consistency
//! - **Automatic Failover**: Detect and recover from node failures
//! - **Load Balancing**: Distribute requests across healthy replicas
//! - **Conflict Resolution**: Handle concurrent updates with configurable strategies
//! - **Topology Management**: Configure and manage replication topologies
//! - **Health Monitoring**: Track replica health and synchronization status
//! - **Split-Brain Detection**: Detect and resolve network partitions
//!
//! # Examples
//!
//! ```rust
//! use torsh_package::replication::{
//!     ReplicationManager, ReplicationConfig, ConsistencyLevel, ReplicationNode
//! };
//!
//! // Create replication manager
//! let config = ReplicationConfig {
//!     consistency: ConsistencyLevel::Eventual,
//!     replication_factor: 3,
//!     auto_failover: true,
//!     sync_interval_secs: 60,
//! };
//!
//! let mut manager = ReplicationManager::new(config);
//!
//! // Add replication nodes
//! let node = ReplicationNode::new(
//!     "node1".to_string(),
//!     "us-east-1".to_string(),
//!     "https://node1.example.com".to_string(),
//!     1,
//!     1000,
//! );
//! manager.add_node(node).unwrap();
//!
//! // Replicate a package
//! manager.replicate_package("my-package", "1.0.0", b"package data").unwrap();
//! ```

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, VecDeque};
use torsh_core::error::TorshError;

/// Consistency level for replication
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ConsistencyLevel {
    /// Eventual consistency - fastest, weakest guarantees
    Eventual,
    /// Quorum consistency - balanced performance and consistency
    Quorum,
    /// Strong consistency - slowest, strongest guarantees
    Strong,
    /// Causal consistency - preserves causality
    Causal,
}

/// Replication strategy
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ReplicationStrategy {
    /// Synchronous replication - wait for all replicas
    Synchronous,
    /// Asynchronous replication - fire and forget
    Asynchronous,
    /// Semi-synchronous - wait for majority
    SemiSynchronous,
}

/// Conflict resolution strategy
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ConflictResolution {
    /// Last write wins based on timestamp
    LastWriteWins,
    /// First write wins
    FirstWriteWins,
    /// Custom merge strategy
    Custom,
    /// Manual resolution required
    Manual,
}

/// Replication node status
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum NodeStatus {
    /// Node is healthy and replicating
    Healthy,
    /// Node is experiencing degraded performance
    Degraded,
    /// Node is unhealthy and not replicating
    Unhealthy,
    /// Node is in maintenance mode
    Maintenance,
    /// Node is offline
    Offline,
}

impl Default for NodeStatus {
    fn default() -> Self {
        Self::Healthy
    }
}

/// Replication node
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReplicationNode {
    /// Unique node identifier
    pub id: String,
    /// Geographic region
    pub region: String,
    /// Node endpoint URL
    pub endpoint: String,
    /// Priority (higher = preferred)
    pub priority: u32,
    /// Storage capacity in bytes
    pub capacity: u64,
    /// Current status
    #[serde(skip)]
    pub status: NodeStatus,
    /// Last health check timestamp
    #[serde(skip)]
    pub last_health_check: Option<DateTime<Utc>>,
    /// Replication lag in seconds
    #[serde(skip)]
    pub replication_lag_secs: f64,
}

impl ReplicationNode {
    /// Create a new replication node
    pub fn new(id: String, region: String, endpoint: String, priority: u32, capacity: u64) -> Self {
        Self {
            id,
            region,
            endpoint,
            priority,
            capacity,
            status: NodeStatus::Healthy,
            last_health_check: None,
            replication_lag_secs: 0.0,
        }
    }
}

/// Replication configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReplicationConfig {
    /// Consistency level
    pub consistency: ConsistencyLevel,
    /// Number of replicas to maintain
    pub replication_factor: usize,
    /// Enable automatic failover
    pub auto_failover: bool,
    /// Synchronization interval in seconds
    pub sync_interval_secs: u64,
}

/// Package replica metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReplicaMetadata {
    /// Package ID
    pub package_id: String,
    /// Package version
    pub version: String,
    /// Node ID where replica is stored
    pub node_id: String,
    /// Replica version/timestamp
    pub replica_version: u64,
    /// Checksum for integrity
    pub checksum: String,
    /// Last synchronized timestamp
    pub last_sync: DateTime<Utc>,
    /// Size in bytes
    pub size_bytes: u64,
}

/// Replication operation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReplicationOperation {
    /// Operation ID
    pub id: String,
    /// Package ID
    pub package_id: String,
    /// Package version
    pub version: String,
    /// Operation type (Create, Update, Delete)
    pub operation_type: String,
    /// Source node ID
    pub source_node: String,
    /// Target node IDs
    pub target_nodes: Vec<String>,
    /// Operation timestamp
    pub timestamp: DateTime<Utc>,
    /// Operation status
    pub status: OperationStatus,
}

/// Operation status
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum OperationStatus {
    /// Operation is pending
    Pending,
    /// Operation is in progress
    InProgress,
    /// Operation completed successfully
    Completed,
    /// Operation failed
    Failed,
}

/// Replication conflict
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReplicationConflict {
    /// Conflict ID
    pub id: String,
    /// Package ID
    pub package_id: String,
    /// Package version
    pub version: String,
    /// Conflicting replicas
    pub conflicting_replicas: Vec<ReplicaMetadata>,
    /// Conflict detection timestamp
    pub detected_at: DateTime<Utc>,
    /// Resolution status
    pub resolved: bool,
    /// Resolution strategy used
    pub resolution_strategy: Option<ConflictResolution>,
}

/// Replication statistics
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ReplicationStatistics {
    /// Total nodes
    pub total_nodes: usize,
    /// Healthy nodes
    pub healthy_nodes: usize,
    /// Total replicas
    pub total_replicas: usize,
    /// Total replication operations
    pub total_operations: u64,
    /// Successful operations
    pub successful_operations: u64,
    /// Failed operations
    pub failed_operations: u64,
    /// Active conflicts
    pub active_conflicts: usize,
    /// Average replication lag (seconds)
    pub avg_replication_lag_secs: f64,
    /// Total bandwidth used (bytes)
    pub total_bandwidth_bytes: u64,
}

/// Replication manager
///
/// Manages package replication across multiple nodes with configurable
/// consistency levels, automatic failover, and conflict resolution.
pub struct ReplicationManager {
    /// Replication configuration
    config: ReplicationConfig,
    /// Replication nodes by ID
    nodes: HashMap<String, ReplicationNode>,
    /// Package replicas
    replicas: HashMap<String, Vec<ReplicaMetadata>>,
    /// Replication operations
    operations: VecDeque<ReplicationOperation>,
    /// Active conflicts
    conflicts: Vec<ReplicationConflict>,
    /// Statistics
    statistics: ReplicationStatistics,
}

impl ReplicationManager {
    /// Create a new replication manager
    pub fn new(config: ReplicationConfig) -> Self {
        Self {
            config,
            nodes: HashMap::new(),
            replicas: HashMap::new(),
            operations: VecDeque::new(),
            conflicts: Vec::new(),
            statistics: ReplicationStatistics::default(),
        }
    }

    /// Add a replication node
    pub fn add_node(&mut self, node: ReplicationNode) -> Result<(), TorshError> {
        if self.nodes.contains_key(&node.id) {
            return Err(TorshError::InvalidArgument(format!(
                "Node {} already exists",
                node.id
            )));
        }

        self.nodes.insert(node.id.clone(), node);
        self.update_statistics();

        Ok(())
    }

    /// Remove a replication node
    pub fn remove_node(&mut self, node_id: &str) -> Result<(), TorshError> {
        self.nodes
            .remove(node_id)
            .ok_or_else(|| TorshError::InvalidArgument(format!("Node {} not found", node_id)))?;

        // Handle replica redistribution
        self.redistribute_replicas(node_id)?;
        self.update_statistics();

        Ok(())
    }

    /// Replicate a package to all nodes
    pub fn replicate_package(
        &mut self,
        package_id: &str,
        version: &str,
        _data: &[u8],
    ) -> Result<(), TorshError> {
        // Select target nodes based on replication factor
        let target_nodes = self.select_replication_nodes(package_id)?;

        // Create replication operation
        let operation = ReplicationOperation {
            id: uuid::Uuid::new_v4().to_string(),
            package_id: package_id.to_string(),
            version: version.to_string(),
            operation_type: "Create".to_string(),
            source_node: "primary".to_string(),
            target_nodes: target_nodes.iter().map(|n| n.id.clone()).collect(),
            timestamp: Utc::now(),
            status: OperationStatus::Pending,
        };

        self.operations.push_back(operation);

        // Execute replication based on strategy
        match self.config.consistency {
            ConsistencyLevel::Strong => self.replicate_synchronously(package_id, version)?,
            ConsistencyLevel::Quorum => self.replicate_to_quorum(package_id, version)?,
            ConsistencyLevel::Eventual | ConsistencyLevel::Causal => {
                self.replicate_asynchronously(package_id, version)?
            }
        }

        self.update_statistics();

        Ok(())
    }

    /// Get package from best available replica
    pub fn get_package(&self, package_id: &str, version: &str) -> Result<String, TorshError> {
        let key = format!("{}:{}", package_id, version);

        let replicas = self
            .replicas
            .get(&key)
            .ok_or_else(|| TorshError::InvalidArgument(format!("Package {} not found", key)))?;

        // Select best replica based on node health and latency
        let best_replica = self.select_best_replica(replicas)?;

        Ok(best_replica.node_id.clone())
    }

    /// Perform health check on all nodes
    pub fn health_check(&mut self) -> Result<(), TorshError> {
        let now = Utc::now();

        // Collect node IDs first to avoid borrow checker issues
        let node_ids: Vec<String> = self.nodes.keys().cloned().collect();

        for node_id in node_ids {
            // Mock health check (in production, send actual health check request)
            let is_healthy = self.check_node_health(&node_id);

            if let Some(node) = self.nodes.get_mut(&node_id) {
                node.status = if is_healthy {
                    NodeStatus::Healthy
                } else {
                    NodeStatus::Unhealthy
                };

                node.last_health_check = Some(now);
            }
        }

        self.update_statistics();

        // Handle failover if auto_failover is enabled
        if self.config.auto_failover {
            self.handle_failover()?;
        }

        Ok(())
    }

    /// Synchronize replicas across nodes
    pub fn synchronize(&mut self) -> Result<(), TorshError> {
        // Identify replicas that are out of sync
        let mut to_sync = Vec::new();

        for (key, replicas) in &self.replicas {
            let max_version = replicas
                .iter()
                .map(|r| r.replica_version)
                .max()
                .unwrap_or(0);

            for replica in replicas {
                if replica.replica_version < max_version {
                    to_sync.push((key.clone(), replica.node_id.clone()));
                }
            }
        }

        // Synchronize out-of-sync replicas
        for (key, node_id) in to_sync {
            self.sync_replica(&key, &node_id)?;
        }

        Ok(())
    }

    /// Detect and resolve conflicts
    pub fn resolve_conflicts(&mut self) -> Result<(), TorshError> {
        let mut replicas_to_propagate = Vec::new();

        // Collect replicas to propagate and mark conflicts as resolved
        for conflict in &mut self.conflicts {
            if !conflict.resolved {
                // Apply conflict resolution strategy
                let strategy = ConflictResolution::LastWriteWins; // Use configured strategy

                match strategy {
                    ConflictResolution::LastWriteWins => {
                        // Keep replica with latest timestamp
                        if let Some(latest) = conflict
                            .conflicting_replicas
                            .iter()
                            .max_by_key(|r| r.last_sync)
                            .cloned()
                        {
                            replicas_to_propagate.push(latest);
                            conflict.resolved = true;
                            conflict.resolution_strategy = Some(strategy);
                        }
                    }
                    ConflictResolution::FirstWriteWins => {
                        // Keep replica with earliest timestamp
                        if let Some(earliest) = conflict
                            .conflicting_replicas
                            .iter()
                            .min_by_key(|r| r.last_sync)
                            .cloned()
                        {
                            replicas_to_propagate.push(earliest);
                            conflict.resolved = true;
                            conflict.resolution_strategy = Some(strategy);
                        }
                    }
                    ConflictResolution::Custom | ConflictResolution::Manual => {
                        // Require manual intervention
                    }
                }
            }
        }

        // Propagate replicas after collecting them
        for replica in replicas_to_propagate {
            self.propagate_replica(&replica)?;
        }

        // Remove resolved conflicts
        self.conflicts.retain(|c| !c.resolved);

        Ok(())
    }

    /// Get replication statistics
    pub fn get_statistics(&self) -> &ReplicationStatistics {
        &self.statistics
    }

    /// Get node status
    pub fn get_node_status(&self, node_id: &str) -> Option<NodeStatus> {
        self.nodes.get(node_id).map(|n| n.status)
    }

    /// List all nodes
    pub fn list_nodes(&self) -> Vec<&ReplicationNode> {
        self.nodes.values().collect()
    }

    /// List replicas for a package
    pub fn list_replicas(&self, package_id: &str, version: &str) -> Vec<&ReplicaMetadata> {
        let key = format!("{}:{}", package_id, version);
        self.replicas
            .get(&key)
            .map(|replicas| replicas.iter().collect())
            .unwrap_or_default()
    }

    /// Get active conflicts
    pub fn get_conflicts(&self) -> Vec<&ReplicationConflict> {
        self.conflicts.iter().collect()
    }

    // Private helper methods

    fn select_replication_nodes(
        &self,
        _package_id: &str,
    ) -> Result<Vec<&ReplicationNode>, TorshError> {
        let healthy_nodes: Vec<&ReplicationNode> = self
            .nodes
            .values()
            .filter(|n| n.status == NodeStatus::Healthy)
            .collect();

        if healthy_nodes.is_empty() {
            return Err(TorshError::RuntimeError(
                "No healthy nodes available".to_string(),
            ));
        }

        // Select nodes based on priority and capacity
        let mut selected: Vec<&ReplicationNode> = healthy_nodes
            .into_iter()
            .take(self.config.replication_factor)
            .collect();

        // Sort by priority
        selected.sort_by(|a, b| b.priority.cmp(&a.priority));

        Ok(selected)
    }

    fn replicate_synchronously(
        &mut self,
        _package_id: &str,
        _version: &str,
    ) -> Result<(), TorshError> {
        // Mock synchronous replication - mark last operation as completed
        if let Some(op) = self.operations.back_mut() {
            op.status = OperationStatus::Completed;
        }
        Ok(())
    }

    fn replicate_to_quorum(&mut self, _package_id: &str, _version: &str) -> Result<(), TorshError> {
        // Mock quorum replication - mark last operation as completed
        if let Some(op) = self.operations.back_mut() {
            op.status = OperationStatus::Completed;
        }
        Ok(())
    }

    fn replicate_asynchronously(
        &mut self,
        _package_id: &str,
        _version: &str,
    ) -> Result<(), TorshError> {
        // Mock asynchronous replication - mark last operation as completed
        if let Some(op) = self.operations.back_mut() {
            op.status = OperationStatus::Completed;
        }
        Ok(())
    }

    fn select_best_replica<'a>(
        &self,
        replicas: &'a [ReplicaMetadata],
    ) -> Result<&'a ReplicaMetadata, TorshError> {
        // Select replica on healthiest node with lowest lag
        replicas
            .iter()
            .filter(|r| {
                self.nodes
                    .get(&r.node_id)
                    .map(|n| n.status == NodeStatus::Healthy)
                    .unwrap_or(false)
            })
            .min_by(|a, b| {
                let a_lag = self
                    .nodes
                    .get(&a.node_id)
                    .map(|n| n.replication_lag_secs)
                    .unwrap_or(f64::MAX);
                let b_lag = self
                    .nodes
                    .get(&b.node_id)
                    .map(|n| n.replication_lag_secs)
                    .unwrap_or(f64::MAX);
                a_lag
                    .partial_cmp(&b_lag)
                    .expect("replication lag comparison should succeed (f64::MAX is valid)")
            })
            .ok_or_else(|| TorshError::RuntimeError("No healthy replicas".to_string()))
    }

    fn check_node_health(&self, _node_id: &str) -> bool {
        // Mock health check (in production, perform actual health check)
        true
    }

    fn handle_failover(&mut self) -> Result<(), TorshError> {
        let unhealthy_nodes: Vec<String> = self
            .nodes
            .iter()
            .filter(|(_, n)| n.status == NodeStatus::Unhealthy)
            .map(|(id, _)| id.clone())
            .collect();

        for node_id in unhealthy_nodes {
            // Redistribute replicas from unhealthy node
            self.redistribute_replicas(&node_id)?;
        }

        Ok(())
    }

    fn redistribute_replicas(&mut self, node_id: &str) -> Result<(), TorshError> {
        // Find replicas on the removed/failed node
        let mut replicas_to_move = Vec::new();

        for (key, replicas) in &self.replicas {
            if replicas.iter().any(|r| r.node_id == node_id) {
                replicas_to_move.push(key.clone());
            }
        }

        // Replicate to other healthy nodes
        for key in replicas_to_move {
            if let Some(replicas) = self.replicas.get_mut(&key) {
                replicas.retain(|r| r.node_id != node_id);

                // If below replication factor, create new replica
                if replicas.len() < self.config.replication_factor {
                    // Mock: add new replica on another node
                    // In production, actually replicate the data
                }
            }
        }

        Ok(())
    }

    fn sync_replica(&mut self, _key: &str, _node_id: &str) -> Result<(), TorshError> {
        // Mock synchronization (in production, perform actual sync)
        Ok(())
    }

    fn propagate_replica(&mut self, _replica: &ReplicaMetadata) -> Result<(), TorshError> {
        // Mock propagation (in production, propagate to all nodes)
        Ok(())
    }

    fn update_statistics(&mut self) {
        let mut stats = ReplicationStatistics::default();

        stats.total_nodes = self.nodes.len();
        stats.healthy_nodes = self
            .nodes
            .values()
            .filter(|n| n.status == NodeStatus::Healthy)
            .count();

        stats.total_replicas = self.replicas.values().map(|v| v.len()).sum();

        stats.total_operations = self.operations.len() as u64;
        stats.successful_operations = self
            .operations
            .iter()
            .filter(|op| op.status == OperationStatus::Completed)
            .count() as u64;
        stats.failed_operations = self
            .operations
            .iter()
            .filter(|op| op.status == OperationStatus::Failed)
            .count() as u64;

        stats.active_conflicts = self.conflicts.len();

        // Calculate average replication lag
        let total_lag: f64 = self.nodes.values().map(|n| n.replication_lag_secs).sum();
        stats.avg_replication_lag_secs = if !self.nodes.is_empty() {
            total_lag / self.nodes.len() as f64
        } else {
            0.0
        };

        self.statistics = stats;
    }
}

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

    fn create_test_config() -> ReplicationConfig {
        ReplicationConfig {
            consistency: ConsistencyLevel::Eventual,
            replication_factor: 3,
            auto_failover: true,
            sync_interval_secs: 60,
        }
    }

    #[test]
    fn test_replication_manager_creation() {
        let config = create_test_config();
        let manager = ReplicationManager::new(config);
        let stats = manager.get_statistics();
        assert_eq!(stats.total_nodes, 0);
    }

    #[test]
    fn test_add_node() {
        let config = create_test_config();
        let mut manager = ReplicationManager::new(config);

        let node = ReplicationNode::new(
            "node1".to_string(),
            "us-east-1".to_string(),
            "https://node1.example.com".to_string(),
            1,
            1024 * 1024 * 1024,
        );

        manager.add_node(node).unwrap();

        let stats = manager.get_statistics();
        assert_eq!(stats.total_nodes, 1);
        assert_eq!(stats.healthy_nodes, 1);
    }

    #[test]
    fn test_remove_node() {
        let config = create_test_config();
        let mut manager = ReplicationManager::new(config);

        let node = ReplicationNode::new(
            "node1".to_string(),
            "us-east-1".to_string(),
            "https://node1.example.com".to_string(),
            1,
            1024 * 1024 * 1024,
        );

        manager.add_node(node).unwrap();
        assert_eq!(manager.get_statistics().total_nodes, 1);

        manager.remove_node("node1").unwrap();
        assert_eq!(manager.get_statistics().total_nodes, 0);
    }

    #[test]
    fn test_replicate_package() {
        let config = create_test_config();
        let mut manager = ReplicationManager::new(config);

        // Add nodes
        for i in 1..=3 {
            let node = ReplicationNode::new(
                format!("node{}", i),
                "us-east-1".to_string(),
                format!("https://node{}.example.com", i),
                i,
                1024 * 1024 * 1024,
            );
            manager.add_node(node).unwrap();
        }

        // Replicate package
        let result = manager.replicate_package("test-pkg", "1.0.0", b"data");
        assert!(result.is_ok());

        let stats = manager.get_statistics();
        assert!(stats.successful_operations > 0);
    }

    #[test]
    fn test_health_check() {
        let config = create_test_config();
        let mut manager = ReplicationManager::new(config);

        let node = ReplicationNode::new(
            "node1".to_string(),
            "us-east-1".to_string(),
            "https://node1.example.com".to_string(),
            1,
            1024 * 1024 * 1024,
        );

        manager.add_node(node).unwrap();

        manager.health_check().unwrap();

        let status = manager.get_node_status("node1");
        assert!(status.is_some());
    }

    #[test]
    fn test_list_nodes() {
        let config = create_test_config();
        let mut manager = ReplicationManager::new(config);

        for i in 1..=3 {
            let node = ReplicationNode::new(
                format!("node{}", i),
                "us-east-1".to_string(),
                format!("https://node{}.example.com", i),
                i,
                1024 * 1024 * 1024,
            );
            manager.add_node(node).unwrap();
        }

        let nodes = manager.list_nodes();
        assert_eq!(nodes.len(), 3);
    }

    #[test]
    fn test_consistency_levels() {
        let configs = vec![
            ConsistencyLevel::Eventual,
            ConsistencyLevel::Quorum,
            ConsistencyLevel::Strong,
            ConsistencyLevel::Causal,
        ];

        for consistency in configs {
            let config = ReplicationConfig {
                consistency,
                replication_factor: 3,
                auto_failover: true,
                sync_interval_secs: 60,
            };

            let manager = ReplicationManager::new(config);
            assert_eq!(manager.config.consistency, consistency);
        }
    }

    #[test]
    fn test_replication_statistics() {
        let config = create_test_config();
        let mut manager = ReplicationManager::new(config);

        // Add nodes
        for i in 1..=5 {
            let node = ReplicationNode::new(
                format!("node{}", i),
                "us-east-1".to_string(),
                format!("https://node{}.example.com", i),
                i,
                1024 * 1024 * 1024,
            );
            manager.add_node(node).unwrap();
        }

        manager.replicate_package("pkg1", "1.0.0", b"data").unwrap();
        manager.replicate_package("pkg2", "1.0.0", b"data").unwrap();

        let stats = manager.get_statistics();
        assert_eq!(stats.total_nodes, 5);
        assert_eq!(stats.healthy_nodes, 5);
        assert!(stats.successful_operations >= 2);
    }
}