opencrates 3.0.1

Enterprise-grade AI-powered Rust development companion with comprehensive automation, monitoring, and deployment capabilities
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
//! Comprehensive health check system with circuit breakers and monitoring

use crate::utils::config::OpenCratesConfig;
use crate::utils::metrics::MetricRegistry;
use anyhow::Result;
use async_trait::async_trait;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fmt;
use std::sync::Arc;
use std::time::Duration;
use std::time::Instant;
use tokio::sync::{Mutex, RwLock};
use tokio::time::timeout;
use tracing::{debug, error, info, warn};

const DEFAULT_HEALTH_CHECK_INTERVAL: u64 = 30; // seconds
const DEFAULT_SERVICE_TIMEOUT: u64 = 5; // seconds

/// Health status of a component
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum HealthStatus {
    Healthy,
    Degraded,
    Unhealthy,
    Warning,
    Critical,
}

impl fmt::Display for HealthStatus {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{self:?}")
    }
}

impl HealthStatus {
    #[must_use]
    pub fn combine(&self, other: &HealthStatus) -> HealthStatus {
        match (self, other) {
            (HealthStatus::Unhealthy, _) | (_, HealthStatus::Unhealthy) => HealthStatus::Unhealthy,
            (HealthStatus::Critical, _) | (_, HealthStatus::Critical) => HealthStatus::Critical,
            (HealthStatus::Degraded, _) | (_, HealthStatus::Degraded) => HealthStatus::Degraded,
            (HealthStatus::Warning, _) | (_, HealthStatus::Warning) => HealthStatus::Warning,
            (HealthStatus::Healthy, HealthStatus::Healthy) => HealthStatus::Healthy,
        }
    }
}

/// Result of a single health check
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CheckResult {
    pub name: String,
    pub status: HealthStatus,
    pub message: Option<String>,
    pub duration: Duration,
}

impl CheckResult {
    #[must_use]
    pub fn as_str(&self) -> &'static str {
        match self.status {
            HealthStatus::Healthy => "Healthy",
            HealthStatus::Unhealthy => "Unhealthy",
            HealthStatus::Degraded => "Degraded",
            HealthStatus::Critical => "Critical",
            HealthStatus::Warning => "Warning",
        }
    }
}

/// Trait for a health-checkable component
#[async_trait::async_trait]
pub trait HealthCheck: Send + Sync {
    fn name(&self) -> &str;
    async fn check(&self) -> CheckResult;
}

/// Overall health information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HealthInfo {
    pub overall_status: HealthStatus,
    pub checks: Vec<CheckResult>,
    pub uptime: Duration,
}

impl HealthInfo {
    #[must_use]
    pub fn healthy() -> Self {
        Self {
            overall_status: HealthStatus::Healthy,
            checks: Vec::new(),
            uptime: Duration::from_secs(0),
        }
    }
}

/// Manages and runs health checks for the application
pub struct HealthManager {
    checks: Vec<Box<dyn HealthCheck + Send + Sync>>,
    statuses: tokio::sync::RwLock<HealthInfo>,
    metrics: MetricRegistry,
    check_interval: Duration,
    timeout: Duration,
    start_time: Instant,
}

impl HealthManager {
    pub async fn new() -> anyhow::Result<Self> {
        Ok(Self {
            checks: Vec::new(),
            statuses: tokio::sync::RwLock::new(HealthInfo::healthy()),
            metrics: MetricRegistry::default(),
            check_interval: Duration::from_secs(30),
            timeout: Duration::from_secs(5),
            start_time: Instant::now(),
        })
    }

    pub async fn new_with_registry(registry: MetricRegistry) -> anyhow::Result<Self> {
        Ok(Self {
            checks: Vec::new(),
            statuses: tokio::sync::RwLock::new(HealthInfo::healthy()),
            metrics: registry,
            check_interval: Duration::from_secs(30),
            timeout: Duration::from_secs(5),
            start_time: Instant::now(),
        })
    }

    /// zero-arg convenience ctor used by tests
    pub async fn new_default() -> anyhow::Result<Self> {
        Self::new().await
    }

    pub async fn get_health_status(&self) -> HealthStatus {
        self.statuses.read().await.overall_status.clone()
    }

    pub async fn get_health_info(&self) -> HealthInfo {
        self.statuses.read().await.clone()
    }

    pub fn register_check(&mut self, check: Box<dyn HealthCheck + Send + Sync>) {
        info!("Registering health check: {}", check.name());
        self.checks.push(check);
    }

    pub async fn run_checks(&self) {
        let mut results = Vec::new();
        let mut overall_status = HealthStatus::Healthy;

        let check_futures = self.checks.iter().map(|check| {
            let start = Instant::now();
            let timeout = self.timeout;
            async move {
                let check_future = check.check();
                match tokio::time::timeout(timeout, check_future).await {
                    Ok(mut result) => {
                        result.duration = start.elapsed();
                        result
                    }
                    Err(_) => {
                        // Timeout occurred
                        CheckResult {
                            name: check.name().to_string(),
                            status: HealthStatus::Degraded,
                            message: Some(format!("Health check timed out after {timeout:?}")),
                            duration: start.elapsed(),
                        }
                    }
                }
            }
        });

        let check_results: Vec<_> = futures::future::join_all(check_futures).await;

        for result in check_results {
            match result.status {
                HealthStatus::Healthy => {
                    // Keep current overall status
                }
                HealthStatus::Degraded => {
                    if overall_status == HealthStatus::Healthy {
                        overall_status = HealthStatus::Degraded;
                    }
                }
                HealthStatus::Unhealthy => {
                    overall_status = HealthStatus::Unhealthy;
                }
                HealthStatus::Critical => {
                    overall_status = HealthStatus::Unhealthy;
                }
                HealthStatus::Warning => {
                    if overall_status == HealthStatus::Healthy {
                        overall_status = HealthStatus::Degraded;
                    }
                }
            }
            results.push(result);
        }

        let mut statuses = self.statuses.write().await;
        statuses.checks = results;
        statuses.overall_status = overall_status.clone();
        statuses.uptime = self.start_time.elapsed();

        debug!(
            "Health checks completed. Overall status: {:?}",
            overall_status
        );
    }

    pub fn start_periodic_checks(self: Arc<Self>) {
        let self_clone = self.clone();
        tokio::spawn(async move {
            let mut interval = tokio::time::interval(self_clone.check_interval);
            loop {
                interval.tick().await;
                self_clone.run_checks().await;
            }
        });
    }
}

/// Health check middleware for HTTP endpoints
// TODO: document this
// TODO: document this
// TODO: document this
pub struct HealthMiddleware {
    manager: Arc<HealthManager>,
    endpoint: String,
}

impl HealthMiddleware {
    // TODO: document this
    // TODO: document this
    // TODO: document this
    // TODO: document this
    pub fn new(manager: Arc<HealthManager>, endpoint: String) -> Self {
        Self { manager, endpoint }
    }

    /// Handle health check request
    // TODO: document this
    // TODO: document this
    // TODO: document this
    // TODO: document this
    pub async fn handle_request(&self, path: &str) -> Result<(u16, String)> {
        if path != self.endpoint {
            return Ok((404, "Not Found".to_string()));
        }

        let health = self.manager.get_health_status().await;
        let status_code = match health {
            HealthStatus::Healthy => 200,
            HealthStatus::Degraded => 200, // Still serving traffic
            HealthStatus::Unhealthy => 503,
            HealthStatus::Critical => 503,
            HealthStatus::Warning => 200,
        };

        let response = serde_json::to_string_pretty(&health)
            .unwrap_or_else(|_| "Internal Server Error".to_string());

        Ok((status_code, response))
    }

    /// Handle detailed health check request
    // TODO: document this
    // TODO: document this
    // TODO: document this
    // TODO: document this
    pub async fn handle_detailed_request(&self, check_name: Option<&str>) -> Result<(u16, String)> {
        match check_name {
            Some(name) => {
                let health_info = self.manager.get_health_info().await;
                let result = health_info.checks.iter().find(|r| r.name == *name);
                if let Some(result) = result {
                    let status_code = match result.status {
                        HealthStatus::Healthy => 200,
                        HealthStatus::Degraded => 200,
                        HealthStatus::Unhealthy => 503,
                        HealthStatus::Critical => 503,
                        HealthStatus::Warning => 200,
                    };

                    let response = serde_json::to_string_pretty(result)
                        .unwrap_or_else(|_| "Internal Server Error".to_string());

                    Ok((status_code, response))
                } else {
                    Ok((404, format!("Health check '{name}' not found")))
                }
            }
            None => self.handle_request(&self.endpoint).await,
        }
    }
}

/// Circuit breaker for health checks
// TODO: document this
// TODO: document this
// TODO: document this
pub struct HealthCircuitBreaker {
    failure_threshold: usize,
    recovery_timeout: Duration,
    failure_count: Arc<Mutex<usize>>,
    last_failure: Arc<Mutex<Option<Instant>>>,
    state: Arc<Mutex<CircuitBreakerState>>,
}

#[derive(Debug, Clone, Copy, PartialEq)]
enum CircuitBreakerState {
    // TODO: document this
    Closed, // Normal operation
    // TODO: document this
    Open, // Failing, not executing checks
    // TODO: document this
    HalfOpen, // Testing if service recovered
}

impl HealthCircuitBreaker {
    // TODO: document this
    // TODO: document this
    // TODO: document this
    // TODO: document this
    #[must_use]
    pub fn new(failure_threshold: usize, recovery_timeout: Duration) -> Self {
        Self {
            failure_threshold,
            recovery_timeout,
            failure_count: Arc::new(Mutex::new(0)),
            last_failure: Arc::new(Mutex::new(None)),
            state: Arc::new(Mutex::new(CircuitBreakerState::Closed)),
        }
    }

    /// Execute health check with circuit breaker protection
    // TODO: document this
    // TODO: document this
    // TODO: document this
    // TODO: document this
    pub async fn execute<F, Fut>(&self, check_fn: F) -> CheckResult
    where
        F: FnOnce() -> Fut,
        Fut: std::future::Future<Output = CheckResult>,
    {
        let state = {
            let state_guard = self.state.lock().await;
            *state_guard
        };

        match state {
            CircuitBreakerState::Open => {
                // Check if we should transition to half-open
                let should_try = {
                    let last_failure_guard = self.last_failure.lock().await;
                    if let Some(last_failure) = *last_failure_guard {
                        last_failure.elapsed() >= self.recovery_timeout
                    } else {
                        true
                    }
                };

                if should_try {
                    let mut state_guard = self.state.lock().await;
                    *state_guard = CircuitBreakerState::HalfOpen;
                    drop(state_guard);

                    self.execute_and_handle_result(check_fn).await
                } else {
                    CheckResult {
                        name: "circuit_breaker".to_string(),
                        status: HealthStatus::Unhealthy,
                        message: Some("Circuit breaker is open".to_string()),
                        duration: Duration::from_millis(0),
                    }
                }
            }
            CircuitBreakerState::HalfOpen | CircuitBreakerState::Closed => {
                self.execute_and_handle_result(check_fn).await
            }
        }
    }

    async fn execute_and_handle_result<F, Fut>(&self, check_fn: F) -> CheckResult
    where
        F: FnOnce() -> Fut,
        Fut: std::future::Future<Output = CheckResult>,
    {
        let result = check_fn().await;

        match result.status {
            HealthStatus::Healthy => {
                // Reset failure count and close circuit
                let mut failure_count_guard = self.failure_count.lock().await;
                *failure_count_guard = 0;
                drop(failure_count_guard);

                let mut state_guard = self.state.lock().await;
                *state_guard = CircuitBreakerState::Closed;
            }
            HealthStatus::Unhealthy | HealthStatus::Critical => {
                // Increment failure count
                let mut failure_count_guard = self.failure_count.lock().await;
                *failure_count_guard += 1;
                let failures = *failure_count_guard;
                drop(failure_count_guard);

                // Update last failure time
                let mut last_failure_guard = self.last_failure.lock().await;
                *last_failure_guard = Some(Instant::now());
                drop(last_failure_guard);

                // Open circuit if threshold exceeded
                if failures >= self.failure_threshold {
                    let mut state_guard = self.state.lock().await;
                    *state_guard = CircuitBreakerState::Open;
                }
            }
            HealthStatus::Degraded | HealthStatus::Warning => {
                // Degraded status doesn't affect circuit breaker
            }
        }

        result
    }
}

/// Health check aggregator for multiple instances
// TODO: document this
// TODO: document this
// TODO: document this
pub struct HealthAggregator {
    managers: Vec<Arc<HealthManager>>,
    weights: HashMap<String, f64>,
}

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

impl HealthAggregator {
    // TODO: document this
    // TODO: document this
    // TODO: document this
    // TODO: document this
    #[must_use]
    pub fn new() -> Self {
        Self {
            managers: Vec::new(),
            weights: HashMap::new(),
        }
    }

    // TODO: document this

    // TODO: document this

    // TODO: document this

    // TODO: document this

    pub fn add_manager(&mut self, manager: Arc<HealthManager>, _weight: f64) {
        self.managers.push(manager);
        // Weight could be used for weighted health calculations
    }

    // TODO: document this

    // TODO: document this

    // TODO: document this

    // TODO: document this

    pub fn set_check_weight(&mut self, check_name: String, weight: f64) {
        self.weights.insert(check_name, weight);
    }

    /// Get aggregated health across all managers
    // TODO: document this
    // TODO: document this
    // TODO: document this
    // TODO: document this
    pub async fn get_aggregated_health(&self) -> HealthStatus {
        let mut overall_status = HealthStatus::Healthy;

        for manager in &self.managers {
            let health = manager.get_health_status().await;

            match health {
                HealthStatus::Unhealthy => return HealthStatus::Unhealthy,
                HealthStatus::Degraded => overall_status = HealthStatus::Degraded,
                _ => {}
            }
        }

        overall_status
    }
}

#[derive(Debug, Clone, Default, Serialize)]
pub struct SystemStatus {
    pub data: Option<serde_json::Value>,
    pub healthy: bool,
}

impl SystemStatus {
    #[must_use]
    pub fn healthy(data: serde_json::Value) -> Self {
        Self {
            data: Some(data),
            healthy: true,
        }
    }

    #[must_use]
    pub fn unhealthy() -> Self {
        Self {
            data: None,
            healthy: false,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::atomic::{AtomicBool, Ordering};
    use tokio::time::{sleep, Duration as TokioDuration};

    #[derive(Debug)]
    struct MockHealthCheck {
        name: String,
        should_fail: Arc<AtomicBool>,
        should_timeout: Arc<AtomicBool>,
        dependencies: Vec<String>,
    }

    impl MockHealthCheck {
        fn new(name: String) -> Self {
            Self {
                name,
                should_fail: Arc::new(AtomicBool::new(false)),
                should_timeout: Arc::new(AtomicBool::new(false)),
                dependencies: Vec::new(),
            }
        }

        fn with_dependencies(mut self, dependencies: Vec<String>) -> Self {
            self.dependencies = dependencies;
            self
        }

        fn set_should_fail(self: Arc<Self>, should_fail: bool) -> Arc<Self> {
            self.should_fail.store(should_fail, Ordering::Relaxed);
            self
        }

        fn set_should_timeout(self: Arc<Self>, should_timeout: bool) -> Arc<Self> {
            self.should_timeout.store(should_timeout, Ordering::Relaxed);
            self
        }
    }

    #[async_trait]
    impl HealthCheck for MockHealthCheck {
        fn name(&self) -> &str {
            &self.name
        }

        async fn check(&self) -> CheckResult {
            if self.should_timeout.load(Ordering::Relaxed) {
                sleep(TokioDuration::from_secs(10)).await;
                return CheckResult {
                    name: self.name.clone(),
                    status: HealthStatus::Healthy,
                    message: Some("Mock check (should have timed out)".to_string()),
                    duration: TokioDuration::from_secs(10),
                };
            }
            if self.should_fail.load(Ordering::Relaxed) {
                CheckResult {
                    name: self.name.clone(),
                    status: HealthStatus::Unhealthy,
                    message: Some("Mock failure".to_string()),
                    duration: TokioDuration::from_millis(10),
                }
            } else {
                CheckResult {
                    name: self.name.clone(),
                    status: HealthStatus::Healthy,
                    message: Some("Mock check successful".to_string()),
                    duration: TokioDuration::from_millis(10),
                }
            }
        }
    }

    #[async_trait]
    impl HealthCheck for Arc<MockHealthCheck> {
        fn name(&self) -> &str {
            &self.name
        }

        async fn check(&self) -> CheckResult {
            self.as_ref().check().await
        }
    }

    #[tokio::test]
    async fn test_health_status_combine() {
        assert_eq!(
            HealthStatus::Healthy.combine(&HealthStatus::Healthy),
            HealthStatus::Healthy
        );
        assert_eq!(
            HealthStatus::Healthy.combine(&HealthStatus::Degraded),
            HealthStatus::Degraded
        );
        assert_eq!(
            HealthStatus::Degraded.combine(&HealthStatus::Unhealthy),
            HealthStatus::Unhealthy
        );
        assert_eq!(
            HealthStatus::Healthy.combine(&HealthStatus::Degraded),
            HealthStatus::Degraded
        );
    }

    #[tokio::test]
    async fn test_health_check_result_creation() {
        let result = CheckResult {
            name: "test".to_string(),
            status: HealthStatus::Healthy,
            message: None,
            duration: TokioDuration::from_millis(50),
        };
        assert_eq!(result.status, HealthStatus::Healthy);
        assert_eq!(result.duration, TokioDuration::from_millis(50));
        assert!(result.message.is_none());
    }

    #[tokio::test]
    async fn test_mock_health_check() {
        let check = Arc::new(MockHealthCheck::new("test".to_string())).set_should_fail(true);
        let result = check.check().await;
        assert_eq!(result.status, HealthStatus::Unhealthy);
        assert!(result.message.as_ref().unwrap().contains("Mock failure"));
    }

    #[tokio::test]
    async fn test_health_manager_basic() {
        let registry = crate::utils::metrics::MetricRegistry::new();
        let mut manager = HealthManager::new_with_registry(registry).await.unwrap();

        let check = Arc::new(MockHealthCheck::new("test_check".to_string()));
        manager.register_check(Box::new(check));

        manager.run_checks().await;
        let health_status = manager.get_health_status().await;
        assert_eq!(health_status, HealthStatus::Healthy);
    }

    #[tokio::test]
    async fn test_health_manager_with_failure() {
        let registry = crate::utils::metrics::MetricRegistry::new();
        let mut manager = HealthManager::new_with_registry(registry).await.unwrap();

        let check =
            Arc::new(MockHealthCheck::new("failing_check".to_string())).set_should_fail(true);
        manager.register_check(Box::new(check));

        manager.run_checks().await;
        let health_status = manager.get_health_status().await;
        assert_eq!(health_status, HealthStatus::Unhealthy);
    }

    #[tokio::test]
    async fn test_health_manager_timeout() {
        let registry = crate::utils::metrics::MetricRegistry::new();
        let mut manager = HealthManager::new_with_registry(registry).await.unwrap();

        let check =
            Arc::new(MockHealthCheck::new("timeout_check".to_string())).set_should_timeout(true);
        manager.register_check(Box::new(check));

        manager.run_checks().await;
        let health_status = manager.get_health_status().await;
        assert_eq!(health_status, HealthStatus::Degraded); // Note: timeout results in Degraded, not Unhealthy
    }

    #[tokio::test]
    async fn test_health_manager_dependencies() {
        let registry = crate::utils::metrics::MetricRegistry::new();
        let mut manager = HealthManager::new_with_registry(registry).await.unwrap();

        // Create checks with dependencies: check_b depends on check_a
        let check_a = Arc::new(MockHealthCheck::new("check_a".to_string()));
        let check_b = Arc::new(
            MockHealthCheck::new("check_b".to_string())
                .with_dependencies(vec!["check_a".to_string()]),
        );

        manager.register_check(Box::new(check_b));
        manager.register_check(Box::new(check_a));

        manager.run_checks().await;
        let health_status = manager.get_health_status().await;
        assert_eq!(health_status, HealthStatus::Healthy);
    }

    #[tokio::test]
    async fn test_health_manager_periodic_checks() {
        let registry = crate::utils::metrics::MetricRegistry::new();
        let mut manager = HealthManager::new_with_registry(registry).await.unwrap();

        let check = Arc::new(MockHealthCheck::new("periodic_check".to_string()));
        manager.register_check(Box::new(check));

        let manager = Arc::new(manager);
        // Start periodic checks
        manager.clone().start_periodic_checks();

        // Wait for a few cycles
        sleep(TokioDuration::from_millis(200)).await;

        let health_status = manager.get_health_status().await;
        assert_eq!(health_status, HealthStatus::Healthy);
    }

    #[tokio::test]
    async fn test_health_info() {
        let mut checks = Vec::new();

        checks.push(CheckResult {
            name: "healthy_check".to_string(),
            status: HealthStatus::Healthy,
            message: None,
            duration: TokioDuration::from_millis(10),
        });

        checks.push(CheckResult {
            name: "degraded_check".to_string(),
            status: HealthStatus::Degraded,
            message: Some("Warning".to_string()),
            duration: TokioDuration::from_millis(20),
        });

        checks.push(CheckResult {
            name: "unhealthy_check".to_string(),
            status: HealthStatus::Unhealthy,
            message: Some("Error".to_string()),
            duration: TokioDuration::from_millis(30),
        });

        let health_info = HealthInfo {
            overall_status: HealthStatus::Unhealthy,
            checks,
            uptime: TokioDuration::from_secs(100),
        };

        assert_eq!(health_info.overall_status, HealthStatus::Unhealthy);
        assert_eq!(health_info.checks.len(), 3);
    }

    #[tokio::test]
    async fn test_circuit_breaker() {
        let circuit_breaker = HealthCircuitBreaker::new(2, TokioDuration::from_millis(100));

        // First failure
        let result = circuit_breaker
            .execute(|| async {
                CheckResult {
                    name: "test_check".to_string(),
                    status: HealthStatus::Unhealthy,
                    message: Some("Failure 1".to_string()),
                    duration: TokioDuration::from_millis(10),
                }
            })
            .await;
        assert_eq!(result.status, HealthStatus::Unhealthy);

        // Second failure - should open circuit
        let result = circuit_breaker
            .execute(|| async {
                CheckResult {
                    name: "test_check".to_string(),
                    status: HealthStatus::Unhealthy,
                    message: Some("Failure 2".to_string()),
                    duration: TokioDuration::from_millis(10),
                }
            })
            .await;
        assert_eq!(result.status, HealthStatus::Unhealthy);

        // Third attempt - circuit should be open
        let result = circuit_breaker
            .execute(|| async {
                CheckResult {
                    name: "test_check".to_string(),
                    status: HealthStatus::Healthy,
                    message: None,
                    duration: TokioDuration::from_millis(10),
                }
            })
            .await;
        assert_eq!(result.status, HealthStatus::Unhealthy);
        assert!(result
            .message
            .as_ref()
            .unwrap()
            .contains("Circuit breaker is open"));

        // Wait for recovery timeout
        sleep(TokioDuration::from_millis(150)).await;

        // Should transition to half-open and allow one attempt
        let result = circuit_breaker
            .execute(|| async {
                CheckResult {
                    name: "test_check".to_string(),
                    status: HealthStatus::Healthy,
                    message: None,
                    duration: TokioDuration::from_millis(10),
                }
            })
            .await;
        assert_eq!(result.status, HealthStatus::Healthy);
    }

    #[tokio::test]
    async fn test_health_aggregator() {
        let registry1 = MetricRegistry::new();
        let registry2 = MetricRegistry::new();
        let mut manager1 = HealthManager::new_with_registry(registry1).await.unwrap();
        let mut manager2 = HealthManager::new_with_registry(registry2).await.unwrap();

        let check1 = Arc::new(MockHealthCheck::new("check1".to_string()));
        let check2 = Arc::new(MockHealthCheck::new("check2".to_string()));

        manager1.register_check(Box::new(check1));
        manager2.register_check(Box::new(check2));

        // Run checks
        manager1.run_checks().await;
        manager2.run_checks().await;

        let mut aggregator = HealthAggregator::new();
        aggregator.add_manager(Arc::new(manager1), 1.0);
        aggregator.add_manager(Arc::new(manager2), 1.0);

        let aggregated_health = aggregator.get_aggregated_health().await;
        assert_eq!(aggregated_health, HealthStatus::Healthy);
    }
}