crawlrs 0.2.0

crawlrs - Enterprise-grade web scraping and crawling platform built with Rust
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
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
// Copyright (c) 2025-2026 Kirky.X🌠
// SPDX-License-Identifier: Apache-2.0

use async_trait::async_trait;
use chrono::{DateTime, Utc};
use log::warn;
use std::collections::HashMap;
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::RwLock;

use crate::engines::engine_client::{
    EngineError, InternalScrapeRequest, InternalScrapeResponse, ScraperEngine,
};

/// 引擎健康状态
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EngineHealth {
    /// 健康
    Healthy,
    /// 降级
    Degraded,
    /// 不可用
    Unhealthy,
}

/// 引擎健康检查信息
#[derive(Debug, Clone)]
pub struct HealthCheckInfo {
    /// 引擎名称
    pub engine_name: String,
    /// 健康状态
    pub health: EngineHealth,
    /// 最后检查时间
    pub last_check: DateTime<Utc>,
    /// 连续失败次数
    pub consecutive_failures: u32,
    /// 平均响应时间(毫秒)
    pub avg_response_time_ms: Option<u64>,
    /// 错误信息
    pub error_message: Option<String>,
}

/// 健康检查配置
#[derive(Debug, Clone)]
pub struct HealthCheckConfig {
    /// 检查间隔
    pub check_interval: Duration,
    /// 超时时间
    pub timeout: Duration,
    /// 最大连续失败次数
    pub max_consecutive_failures: u32,
    /// 降级阈值(平均响应时间,毫秒)
    pub degraded_threshold_ms: u64,
    /// 不健康阈值(平均响应时间,毫秒)
    pub unhealthy_threshold_ms: u64,
    /// 目标URL
    pub target_url: String,
}

impl Default for HealthCheckConfig {
    fn default() -> Self {
        // 使用配置服务获取健康检查 URL,如果不可用则回退到环境变量
        let target_url = std::env::var("CRAWLRS_HEALTH_CHECK_URL")
            .or_else(|_| std::env::var("APP_ENVIRONMENT"))
            .unwrap_or_else(|_| "https://www.google.com".to_string());

        Self {
            check_interval: Duration::from_secs(60),
            timeout: Duration::from_secs(10),
            max_consecutive_failures: 3,
            degraded_threshold_ms: 2000,
            unhealthy_threshold_ms: 5000,
            target_url,
        }
    }
}

/// 引擎健康监控器
pub struct EngineHealthMonitor {
    /// 引擎列表
    engines: Vec<Arc<dyn ScraperEngine>>,
    /// 健康状态
    health_status: Arc<RwLock<HashMap<String, HealthCheckInfo>>>,
    /// 配置
    config: HealthCheckConfig,
}

impl EngineHealthMonitor {
    /// 创建新的健康监控器
    pub fn new(engines: Vec<Arc<dyn ScraperEngine>>) -> Self {
        let health_status = Self::initialize_health_status(&engines);

        Self {
            engines,
            health_status: Arc::new(RwLock::new(health_status)),
            config: HealthCheckConfig::default(),
        }
    }

    /// 使用自定义配置创建新的健康监控器
    pub fn new_with_config(
        engines: Vec<Arc<dyn ScraperEngine>>,
        config: HealthCheckConfig,
    ) -> Self {
        let health_status = Self::initialize_health_status(&engines);

        Self {
            engines,
            health_status: Arc::new(RwLock::new(health_status)),
            config,
        }
    }

    /// 内部初始化方法 - 提取公共的 health_status 初始化逻辑
    fn initialize_health_status(
        engines: &[Arc<dyn ScraperEngine>],
    ) -> HashMap<String, HealthCheckInfo> {
        let mut health_status = HashMap::with_capacity(8);

        for engine in engines {
            let engine_name = engine.name().to_string();
            health_status.insert(
                engine_name.clone(),
                HealthCheckInfo {
                    engine_name,
                    health: EngineHealth::Healthy,
                    last_check: Utc::now(),
                    consecutive_failures: 0,
                    avg_response_time_ms: None,
                    error_message: None,
                },
            );
        }

        health_status
    }

    /// 获取所有引擎的健康状态
    pub async fn get_all_health_status(&self) -> HashMap<String, HealthCheckInfo> {
        self.health_status.read().await.clone()
    }

    /// 获取特定引擎的健康状态
    pub async fn get_engine_health(&self, engine_name: &str) -> Option<HealthCheckInfo> {
        self.health_status.read().await.get(engine_name).cloned()
    }

    /// 执行健康检查
    pub async fn perform_health_check(&self) {
        for engine in &self.engines {
            let engine_name = engine.name().to_string();
            let health_info = self.check_engine_health(engine).await;

            let mut status = self.health_status.write().await;
            status.insert(engine_name, health_info);
        }
    }

    /// 检查特定引擎的健康状态
    async fn check_engine_health(&self, engine: &Arc<dyn ScraperEngine>) -> HealthCheckInfo {
        let engine_name = engine.name().to_string();
        let start_time = std::time::Instant::now();

        let test_request = InternalScrapeRequest {
            url: self.config.target_url.clone(),
            method: crate::engines::engine_client::HttpMethod::Get,
            headers: std::collections::HashMap::new(),
            timeout: self.config.timeout,
            needs_js: false,
            needs_screenshot: false,
            screenshot_config: None,
            mobile: false,
            proxy: None,
            skip_tls_verification: false,
            needs_tls_fingerprint: false,
            use_fire_engine: false,
            actions: Vec::new(),
            body: None,
            sync_wait_ms: 0,
            block_ads: false,
            block_media: false,
            session_id: None,
            wait_for: None,
            needs_mllm: false,
        };

        match engine.scrape(&test_request).await {
            Ok(response) => {
                let response_time = start_time.elapsed().as_millis() as u64;
                let health = self.determine_health_status(response_time, response.status_code);

                HealthCheckInfo {
                    engine_name,
                    health,
                    last_check: Utc::now(),
                    consecutive_failures: 0,
                    avg_response_time_ms: Some(response_time),
                    error_message: None,
                }
            }
            Err(error) => {
                let current_status = self.health_status.read().await;
                let current_info = current_status.get(&engine_name);
                let consecutive_failures = current_info
                    .map(|info| info.consecutive_failures + 1)
                    .unwrap_or(1);

                let health = if consecutive_failures >= self.config.max_consecutive_failures {
                    // Alert P3-Low: Single Engine Failure
                    warn!(
                        "ALARM: Engine {} is unhealthy after {} consecutive failures",
                        engine_name, consecutive_failures
                    );
                    EngineHealth::Unhealthy
                } else {
                    EngineHealth::Degraded
                };

                HealthCheckInfo {
                    engine_name,
                    health,
                    last_check: Utc::now(),
                    consecutive_failures,
                    avg_response_time_ms: None,
                    error_message: Some(error.to_string()),
                }
            }
        }
    }

    /// 根据响应时间和状态码确定健康状态
    fn determine_health_status(&self, response_time_ms: u64, status_code: u16) -> EngineHealth {
        if status_code >= 500 || response_time_ms > self.config.unhealthy_threshold_ms {
            EngineHealth::Unhealthy
        } else if response_time_ms > self.config.degraded_threshold_ms {
            EngineHealth::Degraded
        } else {
            EngineHealth::Healthy
        }
    }

    /// 获取健康的引擎列表
    pub async fn get_healthy_engines(&self) -> Vec<Arc<dyn ScraperEngine>> {
        let status = self.health_status.read().await;
        self.engines
            .iter()
            .filter(|engine| {
                status
                    .get(engine.name())
                    .map(|info| info.health == EngineHealth::Healthy)
                    .unwrap_or(true)
            })
            .cloned()
            .collect()
    }

    /// 获取所有可用的引擎(包括降级状态的引擎)
    pub async fn get_available_engines(&self) -> Vec<Arc<dyn ScraperEngine>> {
        let status = self.health_status.read().await;
        self.engines
            .iter()
            .filter(|engine| {
                status
                    .get(engine.name())
                    .map(|info| info.health != EngineHealth::Unhealthy)
                    .unwrap_or(true)
            })
            .cloned()
            .collect()
    }

    /// 执行所有引擎的健康检查
    pub async fn perform_all_health_checks(&self) {
        self.perform_health_check().await;
    }

    /// 获取聚合的健康状态
    pub async fn get_aggregate_status(&self) -> AggregateHealthStatus {
        let status = self.health_status.read().await;

        let mut healthy_count = 0;
        let mut degraded_count = 0;
        let mut unhealthy_count = 0;
        let mut unhealthy_engines = Vec::new();

        for engine in &self.engines {
            if let Some(info) = status.get(engine.name()) {
                match info.health {
                    EngineHealth::Healthy => healthy_count += 1,
                    EngineHealth::Degraded => {
                        degraded_count += 1;
                        unhealthy_engines.push(engine.name().to_string());
                    }
                    EngineHealth::Unhealthy => {
                        unhealthy_count += 1;
                        unhealthy_engines.push(engine.name().to_string());
                    }
                }
            } else {
                // Engine not in status map, count as unknown
                healthy_count += 1;
            }
        }

        if unhealthy_count > 0 && healthy_count == 0 {
            AggregateHealthStatus::Unavailable
        } else if degraded_count > 0 || unhealthy_count > 0 {
            AggregateHealthStatus::Degraded(unhealthy_engines)
        } else {
            AggregateHealthStatus::Healthy
        }
    }
}

/// Aggregate health status for EngineClient
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub enum AggregateHealthStatus {
    #[default]
    Healthy,
    Degraded(Vec<String>),
    Unavailable,
}

// garrison 健康面吸收(S2,`auth` feature 门控)
//
// 职责划分:探活逻辑(HTTP 探测 target_url + timeout、响应时长/状态码阈值判定、
// 连续失败计数)保留在本模块——这是引擎领域逻辑,库不提供;状态登记/聚合/
// check_timeout 护栏/路由暴露交给 garrison `HealthRegistry` +
// `axum_routes::health_routes`。auth-off 构建不引入 garrison(依赖树成本),
// 保留 `EngineHealthMonitor` 自有聚合查询(`get_aggregate_status` 语义不变)。

#[cfg(feature = "auth")]
pub mod garrison_health {
    use super::{AggregateHealthStatus, EngineHealth, EngineHealthMonitor, HealthCheckInfo};
    use garrison::health::{HealthCheck, HealthRegistry, HealthReport, HealthResult, HealthStatus};
    use std::future::Future;
    use std::pin::Pin;
    use std::sync::Arc;

    /// crawlrs 三态引擎健康 → garrison `HealthStatus`(三态阈值语义不变)
    pub fn map_engine_health(health: EngineHealth) -> HealthStatus {
        match health {
            EngineHealth::Healthy => HealthStatus::Healthy,
            EngineHealth::Degraded => HealthStatus::Degraded,
            EngineHealth::Unhealthy => HealthStatus::Unhealthy,
        }
    }

    /// garrison `HealthReport` → crawlrs `AggregateHealthStatus`
    ///
    /// 聚合口径与迁移前 `EngineHealthMonitor::get_aggregate_status` 逐字对齐:
    /// 存在非健康项且无健康项 → Unavailable;存在非健康项 → Degraded(列出名称);
    /// 全部健康 → Healthy。注意 garrison `overall` 为 worst-of(任一 Unhealthy
    /// 即 Unhealthy),故此处按 checks 明细重算而非直接采信 overall,
    /// 保证混合healthy/unhealthy 场景下与迁移前判定一致。
    pub fn map_report(report: &HealthReport) -> AggregateHealthStatus {
        let mut healthy_count = 0usize;
        let mut non_healthy_names = Vec::new();
        for check in &report.checks {
            if check.status == HealthStatus::Healthy {
                healthy_count += 1;
            } else {
                non_healthy_names.push(check.name.clone());
            }
        }

        if !non_healthy_names.is_empty() && healthy_count == 0 {
            AggregateHealthStatus::Unavailable
        } else if !non_healthy_names.is_empty() {
            AggregateHealthStatus::Degraded(non_healthy_names)
        } else {
            AggregateHealthStatus::Healthy
        }
    }

    /// 单引擎 garrison 健康检查项
    ///
    /// 内部委托 `EngineHealthMonitor::check_engine_health` 的领域探活逻辑,
    /// 结果写回监控器共享状态表(`get_healthy_engines`/`get_engine_health`
    /// 等既有消费方无需改动)。引擎列表构造后不可变,index 稳定。
    pub struct EngineHealthCheck {
        monitor: Arc<EngineHealthMonitor>,
        engine_index: usize,
    }

    impl EngineHealthCheck {
        /// 绑定监控器与引擎下标构造检查项
        pub fn new(monitor: Arc<EngineHealthMonitor>, engine_index: usize) -> Self {
            Self {
                monitor,
                engine_index,
            }
        }
    }

    impl HealthCheck for EngineHealthCheck {
        fn name(&self) -> &str {
            self.monitor.engines[self.engine_index].name()
        }

        fn check(&self) -> Pin<Box<dyn Future<Output = HealthResult<HealthStatus>> + Send>> {
            let monitor = Arc::clone(&self.monitor);
            let engine_index = self.engine_index;
            Box::pin(async move {
                let engine = Arc::clone(&monitor.engines[engine_index]);
                let info: HealthCheckInfo = monitor.check_engine_health(&engine).await;
                let status = info.health;
                monitor
                    .health_status
                    .write()
                    .await
                    .insert(info.engine_name.clone(), info);
                Ok(map_engine_health(status))
            })
        }
    }

    /// 由 [`EngineHealthMonitor`] 构建 garrison `HealthRegistry`(每引擎一个检查项)
    ///
    /// 单项检查超时护栏取探活配置 `timeout`(与领域配置对齐,替代库默认 5s)。
    /// 空引擎列表 → 空 registry:`check_all` 返回 Healthy 空报告,
    /// 与迁移前空引擎列表的聚合结果一致。
    pub fn build_health_registry(monitor: &Arc<EngineHealthMonitor>) -> HealthRegistry {
        let mut registry = HealthRegistry::new().with_check_timeout(monitor.config.timeout);
        for index in 0..monitor.engines.len() {
            registry.register(Box::new(EngineHealthCheck::new(Arc::clone(monitor), index)));
        }
        registry
    }
}

#[async_trait]
impl ScraperEngine for EngineHealthMonitor {
    async fn scrape(
        &self,
        _request: &InternalScrapeRequest,
    ) -> Result<InternalScrapeResponse, EngineError> {
        // 健康监控器本身不执行抓取,只提供监控功能
        Err(EngineError::Other(
            "Health monitor cannot perform scraping".to_string(),
        ))
    }

    fn support_score(&self, _request: &InternalScrapeRequest) -> u8 {
        0
    }

    fn name(&self) -> &'static str {
        "health_monitor"
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::engines::engine_client::{
        EngineError, HttpMethod, InternalScrapeRequest, InternalScrapeResponse, ScraperEngine,
    };
    use async_trait::async_trait;
    use std::collections::HashMap;
    use std::sync::Arc;
    use std::time::Duration;

    // Test helper: mock engines

    /// A mock engine that always succeeds with a 200 response
    struct MockOkEngine {
        engine_name: &'static str,
        status_code: u16,
    }

    impl MockOkEngine {
        fn new(name: &'static str) -> Self {
            Self {
                engine_name: name,
                status_code: 200,
            }
        }
    }

    #[async_trait]
    impl ScraperEngine for MockOkEngine {
        async fn scrape(
            &self,
            _request: &InternalScrapeRequest,
        ) -> Result<InternalScrapeResponse, EngineError> {
            Ok(InternalScrapeResponse {
                js_degraded: false,
                captured_responses: Vec::new(),
                status_code: self.status_code,
                content: "ok".to_string(),
                screenshot: None,
                content_type: "text/html".to_string(),
                headers: HashMap::new(),
                response_time_ms: 5,
                final_url: None,
            })
        }

        fn support_score(&self, _request: &InternalScrapeRequest) -> u8 {
            100
        }

        fn name(&self) -> &'static str {
            self.engine_name
        }
    }

    /// A mock engine that always fails
    struct MockFailEngine {
        engine_name: &'static str,
        error_msg: String,
    }

    impl MockFailEngine {
        fn new(name: &'static str) -> Self {
            Self {
                engine_name: name,
                error_msg: "connection refused".to_string(),
            }
        }
    }

    #[async_trait]
    impl ScraperEngine for MockFailEngine {
        async fn scrape(
            &self,
            _request: &InternalScrapeRequest,
        ) -> Result<InternalScrapeResponse, EngineError> {
            Err(EngineError::RequestFailed(self.error_msg.clone()))
        }

        fn support_score(&self, _request: &InternalScrapeRequest) -> u8 {
            100
        }

        fn name(&self) -> &'static str {
            self.engine_name
        }
    }

    fn test_config() -> HealthCheckConfig {
        HealthCheckConfig {
            check_interval: Duration::from_secs(60),
            timeout: Duration::from_secs(10),
            max_consecutive_failures: 3,
            degraded_threshold_ms: 2000,
            unhealthy_threshold_ms: 5000,
            target_url: "https://example.com".to_string(),
        }
    }

    // EngineHealth enum tests

    #[test]
    fn test_engine_health_equality() {
        assert_eq!(EngineHealth::Healthy, EngineHealth::Healthy);
        assert_eq!(EngineHealth::Degraded, EngineHealth::Degraded);
        assert_eq!(EngineHealth::Unhealthy, EngineHealth::Unhealthy);
        assert_ne!(EngineHealth::Healthy, EngineHealth::Degraded);
        assert_ne!(EngineHealth::Degraded, EngineHealth::Unhealthy);
        assert_ne!(EngineHealth::Healthy, EngineHealth::Unhealthy);
    }

    #[test]
    fn test_engine_health_copy() {
        let h1 = EngineHealth::Healthy;
        let h2 = h1; // Copy
        assert_eq!(h1, h2);
    }

    // AggregateHealthStatus tests

    #[test]
    fn test_aggregate_health_status_default() {
        let status = AggregateHealthStatus::default();
        assert_eq!(status, AggregateHealthStatus::Healthy);
    }

    #[test]
    fn test_aggregate_health_status_equality() {
        assert_eq!(
            AggregateHealthStatus::Healthy,
            AggregateHealthStatus::Healthy
        );
        assert_eq!(
            AggregateHealthStatus::Unavailable,
            AggregateHealthStatus::Unavailable
        );
        assert_eq!(
            AggregateHealthStatus::Degraded(vec!["e1".to_string()]),
            AggregateHealthStatus::Degraded(vec!["e1".to_string()])
        );
        assert_ne!(
            AggregateHealthStatus::Healthy,
            AggregateHealthStatus::Unavailable
        );
    }

    // HealthCheckConfig tests

    #[test]
    fn test_health_check_config_fields() {
        let config = test_config();
        assert_eq!(config.check_interval, Duration::from_secs(60));
        assert_eq!(config.timeout, Duration::from_secs(10));
        assert_eq!(config.max_consecutive_failures, 3);
        assert_eq!(config.degraded_threshold_ms, 2000);
        assert_eq!(config.unhealthy_threshold_ms, 5000);
        assert_eq!(config.target_url, "https://example.com");
    }

    // EngineHealthMonitor creation tests

    #[tokio::test]
    async fn test_monitor_new_initializes_health_status() {
        let engines: Vec<Arc<dyn ScraperEngine>> = vec![Arc::new(MockOkEngine::new("engine_a"))];
        let monitor = EngineHealthMonitor::new(engines);

        let status = monitor.get_all_health_status().await;
        assert_eq!(status.len(), 1);
        assert!(status.contains_key("engine_a"));

        let info = status.get("engine_a").unwrap();
        assert_eq!(info.health, EngineHealth::Healthy);
        assert_eq!(info.consecutive_failures, 0);
        assert!(info.error_message.is_none());
        assert!(info.avg_response_time_ms.is_none());
    }

    #[tokio::test]
    async fn test_monitor_new_with_multiple_engines() {
        let engines: Vec<Arc<dyn ScraperEngine>> = vec![
            Arc::new(MockOkEngine::new("engine_a")),
            Arc::new(MockOkEngine::new("engine_b")),
            Arc::new(MockOkEngine::new("engine_c")),
        ];
        let monitor = EngineHealthMonitor::new(engines);

        let status = monitor.get_all_health_status().await;
        assert_eq!(status.len(), 3);
        assert!(status.contains_key("engine_a"));
        assert!(status.contains_key("engine_b"));
        assert!(status.contains_key("engine_c"));
    }

    #[tokio::test]
    async fn test_monitor_new_with_config() {
        let engines: Vec<Arc<dyn ScraperEngine>> = vec![Arc::new(MockOkEngine::new("engine_a"))];
        let config = test_config();
        let monitor = EngineHealthMonitor::new_with_config(engines, config);

        let status = monitor.get_all_health_status().await;
        assert_eq!(status.len(), 1);
        assert!(status.contains_key("engine_a"));
    }

    #[tokio::test]
    async fn test_monitor_new_empty_engines() {
        let engines: Vec<Arc<dyn ScraperEngine>> = vec![];
        let monitor = EngineHealthMonitor::new(engines);

        let status = monitor.get_all_health_status().await;
        assert!(status.is_empty());
    }

    // get_engine_health tests

    #[tokio::test]
    async fn test_get_engine_health_existing() {
        let engines: Vec<Arc<dyn ScraperEngine>> = vec![Arc::new(MockOkEngine::new("engine_a"))];
        let monitor = EngineHealthMonitor::new(engines);

        let health = monitor.get_engine_health("engine_a").await;
        assert!(health.is_some());
        let info = health.unwrap();
        assert_eq!(info.engine_name, "engine_a");
        assert_eq!(info.health, EngineHealth::Healthy);
    }

    #[tokio::test]
    async fn test_get_engine_health_nonexistent() {
        let engines: Vec<Arc<dyn ScraperEngine>> = vec![];
        let monitor = EngineHealthMonitor::new(engines);

        let health = monitor.get_engine_health("nonexistent").await;
        assert!(health.is_none());
    }

    // determine_health_status tests

    #[tokio::test]
    async fn test_determine_health_status_healthy() {
        let monitor = EngineHealthMonitor::new_with_config(vec![], test_config());

        // Fast response, 200 status
        assert_eq!(
            monitor.determine_health_status(100, 200),
            EngineHealth::Healthy
        );
        assert_eq!(
            monitor.determine_health_status(0, 200),
            EngineHealth::Healthy
        );
        assert_eq!(
            monitor.determine_health_status(1999, 200),
            EngineHealth::Healthy
        );
    }

    #[tokio::test]
    async fn test_determine_health_status_degraded() {
        let monitor = EngineHealthMonitor::new_with_config(vec![], test_config());

        // Response time between degraded_threshold and unhealthy_threshold
        assert_eq!(
            monitor.determine_health_status(2001, 200),
            EngineHealth::Degraded
        );
        assert_eq!(
            monitor.determine_health_status(3000, 200),
            EngineHealth::Degraded
        );
        assert_eq!(
            monitor.determine_health_status(4999, 200),
            EngineHealth::Degraded
        );
    }

    #[tokio::test]
    async fn test_determine_health_status_unhealthy_by_response_time() {
        let monitor = EngineHealthMonitor::new_with_config(vec![], test_config());

        // Response time exceeds unhealthy_threshold
        assert_eq!(
            monitor.determine_health_status(5001, 200),
            EngineHealth::Unhealthy
        );
        assert_eq!(
            monitor.determine_health_status(10000, 200),
            EngineHealth::Unhealthy
        );
    }

    #[tokio::test]
    async fn test_determine_health_status_unhealthy_by_status_code() {
        let monitor = EngineHealthMonitor::new_with_config(vec![], test_config());

        // 5xx status codes are unhealthy regardless of response time
        assert_eq!(
            monitor.determine_health_status(10, 500),
            EngineHealth::Unhealthy
        );
        assert_eq!(
            monitor.determine_health_status(10, 503),
            EngineHealth::Unhealthy
        );
        assert_eq!(
            monitor.determine_health_status(10, 599),
            EngineHealth::Unhealthy
        );
    }

    #[tokio::test]
    async fn test_determine_health_status_4xx_not_unhealthy() {
        let monitor = EngineHealthMonitor::new_with_config(vec![], test_config());

        // 4xx status codes are not treated as unhealthy by this logic
        assert_eq!(
            monitor.determine_health_status(10, 404),
            EngineHealth::Healthy
        );
        assert_eq!(
            monitor.determine_health_status(10, 429),
            EngineHealth::Healthy
        );
    }

    // perform_health_check tests

    #[tokio::test]
    async fn test_perform_health_check_success() {
        let engines: Vec<Arc<dyn ScraperEngine>> = vec![Arc::new(MockOkEngine::new("engine_a"))];
        let monitor = EngineHealthMonitor::new_with_config(engines, test_config());

        monitor.perform_health_check().await;

        let health = monitor.get_engine_health("engine_a").await.unwrap();
        assert_eq!(health.health, EngineHealth::Healthy);
        assert_eq!(health.consecutive_failures, 0);
        assert!(health.error_message.is_none());
        assert!(health.avg_response_time_ms.is_some());
    }

    #[tokio::test]
    async fn test_perform_health_check_failure_degraded() {
        let engines: Vec<Arc<dyn ScraperEngine>> = vec![Arc::new(MockFailEngine::new("engine_a"))];
        let monitor = EngineHealthMonitor::new_with_config(engines, test_config());

        monitor.perform_health_check().await;

        let health = monitor.get_engine_health("engine_a").await.unwrap();
        // First failure -> Degraded (below max_consecutive_failures=3)
        assert_eq!(health.health, EngineHealth::Degraded);
        assert_eq!(health.consecutive_failures, 1);
        assert!(health.error_message.is_some());
        assert!(health.avg_response_time_ms.is_none());
    }

    #[tokio::test]
    async fn test_perform_health_check_failure_unhealthy_after_threshold() {
        let engines: Vec<Arc<dyn ScraperEngine>> = vec![Arc::new(MockFailEngine::new("engine_a"))];
        let monitor = EngineHealthMonitor::new_with_config(engines, test_config());

        // Perform health checks until consecutive_failures reaches max (3)
        monitor.perform_health_check().await; // failures = 1 -> Degraded
        monitor.perform_health_check().await; // failures = 2 -> Degraded
        monitor.perform_health_check().await; // failures = 3 -> Unhealthy

        let health = monitor.get_engine_health("engine_a").await.unwrap();
        assert_eq!(health.health, EngineHealth::Unhealthy);
        assert_eq!(health.consecutive_failures, 3);
        assert!(health.error_message.is_some());
    }

    #[tokio::test]
    async fn test_perform_health_check_multiple_engines() {
        let engines: Vec<Arc<dyn ScraperEngine>> = vec![
            Arc::new(MockOkEngine::new("healthy_engine")),
            Arc::new(MockFailEngine::new("failing_engine")),
        ];
        let monitor = EngineHealthMonitor::new_with_config(engines, test_config());

        monitor.perform_health_check().await;

        let healthy = monitor.get_engine_health("healthy_engine").await.unwrap();
        assert_eq!(healthy.health, EngineHealth::Healthy);

        let failing = monitor.get_engine_health("failing_engine").await.unwrap();
        assert_eq!(failing.health, EngineHealth::Degraded);
        assert_eq!(failing.consecutive_failures, 1);
    }

    #[tokio::test]
    async fn test_perform_all_health_checks_alias() {
        let engines: Vec<Arc<dyn ScraperEngine>> = vec![Arc::new(MockOkEngine::new("engine_a"))];
        let monitor = EngineHealthMonitor::new_with_config(engines, test_config());

        // perform_all_health_checks should behave same as perform_health_check
        monitor.perform_all_health_checks().await;

        let health = monitor.get_engine_health("engine_a").await.unwrap();
        assert_eq!(health.health, EngineHealth::Healthy);
    }

    // get_healthy_engines tests

    #[tokio::test]
    async fn test_get_healthy_engines_all_healthy() {
        let engines: Vec<Arc<dyn ScraperEngine>> = vec![
            Arc::new(MockOkEngine::new("engine_a")),
            Arc::new(MockOkEngine::new("engine_b")),
        ];
        let monitor = EngineHealthMonitor::new_with_config(engines, test_config());

        let healthy = monitor.get_healthy_engines().await;
        assert_eq!(healthy.len(), 2);
    }

    #[tokio::test]
    async fn test_get_healthy_engines_after_check() {
        let engines: Vec<Arc<dyn ScraperEngine>> = vec![
            Arc::new(MockOkEngine::new("healthy_engine")),
            Arc::new(MockFailEngine::new("failing_engine")),
        ];
        let monitor = EngineHealthMonitor::new_with_config(engines, test_config());

        monitor.perform_health_check().await;

        let healthy = monitor.get_healthy_engines().await;
        assert_eq!(healthy.len(), 1);
        assert_eq!(healthy[0].name(), "healthy_engine");
    }

    #[tokio::test]
    async fn test_get_healthy_engines_empty() {
        let monitor = EngineHealthMonitor::new_with_config(vec![], test_config());
        let healthy = monitor.get_healthy_engines().await;
        assert!(healthy.is_empty());
    }

    // get_available_engines tests

    #[tokio::test]
    async fn test_get_available_engines_includes_degraded() {
        let engines: Vec<Arc<dyn ScraperEngine>> = vec![
            Arc::new(MockOkEngine::new("healthy_engine")),
            Arc::new(MockFailEngine::new("degraded_engine")),
        ];
        let monitor = EngineHealthMonitor::new_with_config(engines, test_config());

        monitor.perform_health_check().await;

        let available = monitor.get_available_engines().await;
        // Both healthy and degraded engines are available
        assert_eq!(available.len(), 2);
    }

    #[tokio::test]
    async fn test_get_available_engines_excludes_unhealthy() {
        let engines: Vec<Arc<dyn ScraperEngine>> = vec![
            Arc::new(MockOkEngine::new("healthy_engine")),
            Arc::new(MockFailEngine::new("unhealthy_engine")),
        ];
        let monitor = EngineHealthMonitor::new_with_config(engines, test_config());

        // Trigger enough failures to make the failing engine unhealthy
        monitor.perform_health_check().await; // 1 failure
        monitor.perform_health_check().await; // 2 failures
        monitor.perform_health_check().await; // 3 failures -> Unhealthy

        let available = monitor.get_available_engines().await;
        assert_eq!(available.len(), 1);
        assert_eq!(available[0].name(), "healthy_engine");
    }

    // get_aggregate_status tests

    #[tokio::test]
    async fn test_get_aggregate_status_all_healthy() {
        let engines: Vec<Arc<dyn ScraperEngine>> = vec![
            Arc::new(MockOkEngine::new("engine_a")),
            Arc::new(MockOkEngine::new("engine_b")),
        ];
        let monitor = EngineHealthMonitor::new_with_config(engines, test_config());

        let status = monitor.get_aggregate_status().await;
        assert_eq!(status, AggregateHealthStatus::Healthy);
    }

    #[tokio::test]
    async fn test_get_aggregate_status_with_degraded() {
        let engines: Vec<Arc<dyn ScraperEngine>> = vec![
            Arc::new(MockOkEngine::new("healthy_engine")),
            Arc::new(MockFailEngine::new("degraded_engine")),
        ];
        let monitor = EngineHealthMonitor::new_with_config(engines, test_config());

        monitor.perform_health_check().await;

        let status = monitor.get_aggregate_status().await;
        match status {
            AggregateHealthStatus::Degraded(engines) => {
                assert!(engines.contains(&"degraded_engine".to_string()));
            }
            _ => panic!("Expected Degraded status"),
        }
    }

    #[tokio::test]
    async fn test_get_aggregate_status_all_unhealthy() {
        let engines: Vec<Arc<dyn ScraperEngine>> = vec![Arc::new(MockFailEngine::new("engine_a"))];
        let monitor = EngineHealthMonitor::new_with_config(engines, test_config());

        // Trigger enough failures to make engine unhealthy
        monitor.perform_health_check().await;
        monitor.perform_health_check().await;
        monitor.perform_health_check().await;

        let status = monitor.get_aggregate_status().await;
        assert_eq!(status, AggregateHealthStatus::Unavailable);
    }

    #[tokio::test]
    async fn test_get_aggregate_status_mixed_healthy_and_unhealthy() {
        let engines: Vec<Arc<dyn ScraperEngine>> = vec![
            Arc::new(MockOkEngine::new("healthy_engine")),
            Arc::new(MockFailEngine::new("unhealthy_engine")),
        ];
        let monitor = EngineHealthMonitor::new_with_config(engines, test_config());

        monitor.perform_health_check().await;
        monitor.perform_health_check().await;
        monitor.perform_health_check().await;

        let status = monitor.get_aggregate_status().await;
        // Has both healthy and unhealthy, so should be Degraded (not Unavailable)
        match status {
            AggregateHealthStatus::Degraded(engines) => {
                assert!(engines.contains(&"unhealthy_engine".to_string()));
            }
            _ => panic!("Expected Degraded status"),
        }
    }

    #[tokio::test]
    async fn test_get_aggregate_status_empty_engines() {
        let monitor = EngineHealthMonitor::new_with_config(vec![], test_config());
        let status = monitor.get_aggregate_status().await;
        // No engines, so healthy_count stays 0, but no unhealthy either
        // The logic: unhealthy_count == 0 && healthy_count == 0 -> falls through to Healthy
        assert_eq!(status, AggregateHealthStatus::Healthy);
    }

    // ScraperEngine trait impl tests

    #[tokio::test]
    async fn test_health_monitor_scrape_returns_error() {
        let monitor = EngineHealthMonitor::new(vec![]);
        let request = InternalScrapeRequest {
            url: "https://example.com".to_string(),
            method: HttpMethod::Get,
            headers: HashMap::new(),
            timeout: Duration::from_secs(10),
            needs_js: false,
            needs_screenshot: false,
            screenshot_config: None,
            mobile: false,
            proxy: None,
            skip_tls_verification: false,
            needs_tls_fingerprint: false,
            use_fire_engine: false,
            actions: Vec::new(),
            body: None,
            sync_wait_ms: 0,
            block_ads: false,
            block_media: false,
            session_id: None,
            wait_for: None,
            needs_mllm: false,
        };

        let result = monitor.scrape(&request).await;
        assert!(result.is_err());
        match result.unwrap_err() {
            EngineError::Other(msg) => {
                assert!(msg.contains("Health monitor cannot perform scraping"));
            }
            other => panic!("Expected EngineError::Other, got {:?}", other),
        }
    }

    #[test]
    fn test_health_monitor_support_score_is_zero() {
        let monitor = EngineHealthMonitor::new(vec![]);
        let request = InternalScrapeRequest {
            url: "https://example.com".to_string(),
            method: HttpMethod::Get,
            headers: HashMap::new(),
            timeout: Duration::from_secs(10),
            needs_js: false,
            needs_screenshot: false,
            screenshot_config: None,
            mobile: false,
            proxy: None,
            skip_tls_verification: false,
            needs_tls_fingerprint: false,
            use_fire_engine: false,
            actions: Vec::new(),
            body: None,
            sync_wait_ms: 0,
            block_ads: false,
            block_media: false,
            session_id: None,
            wait_for: None,
            needs_mllm: false,
        };
        assert_eq!(monitor.support_score(&request), 0);
    }

    #[test]
    fn test_health_monitor_name() {
        let monitor = EngineHealthMonitor::new(vec![]);
        assert_eq!(monitor.name(), "health_monitor");
    }

    // Recovery scenario test

    // HealthCheckConfig::default tests

    #[test]
    fn test_health_check_config_default_uses_google_url() {
        let _guard = crate::common::test_support::ENV_MUTEX
            .lock()
            .unwrap_or_else(|e| e.into_inner());
        std::env::remove_var("CRAWLRS_HEALTH_CHECK_URL");
        std::env::remove_var("APP_ENVIRONMENT");

        let config = HealthCheckConfig::default();
        assert_eq!(config.check_interval, Duration::from_secs(60));
        assert_eq!(config.timeout, Duration::from_secs(10));
        assert_eq!(config.max_consecutive_failures, 3);
        assert_eq!(config.degraded_threshold_ms, 2000);
        assert_eq!(config.unhealthy_threshold_ms, 5000);
        assert_eq!(config.target_url, "https://www.google.com");
    }

    #[test]
    fn test_health_check_config_default_with_health_check_url_env() {
        let _guard = crate::common::test_support::ENV_MUTEX
            .lock()
            .unwrap_or_else(|e| e.into_inner());
        std::env::set_var("CRAWLRS_HEALTH_CHECK_URL", "https://health.example.com");

        let config = HealthCheckConfig::default();
        assert_eq!(config.target_url, "https://health.example.com");

        std::env::remove_var("CRAWLRS_HEALTH_CHECK_URL");
    }

    #[test]
    fn test_health_check_config_default_falls_back_to_app_environment() {
        let _guard = crate::common::test_support::ENV_MUTEX
            .lock()
            .unwrap_or_else(|e| e.into_inner());
        std::env::remove_var("CRAWLRS_HEALTH_CHECK_URL");
        std::env::set_var("APP_ENVIRONMENT", "https://staging.example.com");

        let config = HealthCheckConfig::default();
        assert_eq!(config.target_url, "https://staging.example.com");

        std::env::remove_var("APP_ENVIRONMENT");
    }

    #[tokio::test]
    async fn test_recovery_resets_consecutive_failures() {
        // An engine that fails first, then succeeds
        struct RecoveringEngine {
            call_count: Arc<std::sync::atomic::AtomicU32>,
        }
        #[async_trait]
        impl ScraperEngine for RecoveringEngine {
            async fn scrape(
                &self,
                _request: &InternalScrapeRequest,
            ) -> Result<InternalScrapeResponse, EngineError> {
                let count = self
                    .call_count
                    .fetch_add(1, std::sync::atomic::Ordering::SeqCst);
                if count < 2 {
                    Err(EngineError::RequestFailed("temporary".to_string()))
                } else {
                    Ok(InternalScrapeResponse {
                        js_degraded: false,
                        captured_responses: Vec::new(),
                        status_code: 200,
                        content: "recovered".to_string(),
                        screenshot: None,
                        content_type: "text/html".to_string(),
                        headers: HashMap::new(),
                        response_time_ms: 5,
                        final_url: None,
                    })
                }
            }

            fn support_score(&self, _request: &InternalScrapeRequest) -> u8 {
                100
            }

            fn name(&self) -> &'static str {
                "recovering_engine"
            }
        }

        let call_count = Arc::new(std::sync::atomic::AtomicU32::new(0));
        let engines: Vec<Arc<dyn ScraperEngine>> = vec![Arc::new(RecoveringEngine { call_count })];
        let monitor = EngineHealthMonitor::new_with_config(engines, test_config());

        // First check: fails -> Degraded, consecutive_failures = 1
        monitor.perform_health_check().await;
        let health = monitor
            .get_engine_health("recovering_engine")
            .await
            .unwrap();
        assert_eq!(health.health, EngineHealth::Degraded);
        assert_eq!(health.consecutive_failures, 1);

        // Second check: fails -> Degraded, consecutive_failures = 2
        monitor.perform_health_check().await;
        let health = monitor
            .get_engine_health("recovering_engine")
            .await
            .unwrap();
        assert_eq!(health.health, EngineHealth::Degraded);
        assert_eq!(health.consecutive_failures, 2);

        // Third check: succeeds -> Healthy, consecutive_failures reset to 0
        monitor.perform_health_check().await;
        let health = monitor
            .get_engine_health("recovering_engine")
            .await
            .unwrap();
        assert_eq!(health.health, EngineHealth::Healthy);
        assert_eq!(health.consecutive_failures, 0);
        assert!(health.error_message.is_none());
        assert!(health.avg_response_time_ms.is_some());
    }

    // garrison 健康面吸收(S2,auth 面):EngineHealthCheck / HealthRegistry /
    // map_report 口径一致性 + 库 check_timeout 护栏 + health_routes 端点语义

    #[cfg(feature = "auth")]
    #[tokio::test]
    async fn test_garrison_registry_report_matches_crawlrs_aggregate() {
        let engines: Vec<Arc<dyn ScraperEngine>> = vec![
            Arc::new(MockOkEngine::new("healthy-engine")),
            Arc::new(MockFailEngine::new("failing-engine")),
        ];
        let monitor = Arc::new(EngineHealthMonitor::new_with_config(engines, test_config()));
        let registry = super::garrison_health::build_health_registry(&monitor);

        let report = registry.check_all().await;
        // 每引擎一个检查项;单项状态与领域探活判定一致(healthy / 首次失败 Degraded)
        assert_eq!(report.checks.len(), 2);
        let healthy = report
            .checks
            .iter()
            .find(|c| c.name == "healthy-engine")
            .expect("healthy-engine check");
        assert_eq!(
            healthy.status,
            garrison::health::HealthStatus::Healthy,
            "healthy 引擎必须映射为 Healthy"
        );
        let failing = report
            .checks
            .iter()
            .find(|c| c.name == "failing-engine")
            .expect("failing-engine check");
        assert_eq!(
            failing.status,
            garrison::health::HealthStatus::Degraded,
            "首次失败(未达 max_consecutive_failures)必须映射为 Degraded"
        );

        // 聚合口径与迁移前 get_aggregate_status 逐字一致(混合 healthy/degraded → Degraded)
        let mapped = super::garrison_health::map_report(&report);
        assert_eq!(mapped, monitor.get_aggregate_status().await);
        match mapped {
            AggregateHealthStatus::Degraded(names) => {
                assert!(names.contains(&"failing-engine".to_string()));
            }
            other => panic!("expected Degraded, got {other:?}"),
        }
    }

    /// 引擎探活失败状态与映射层行为验证:全失败聚合为 Unavailable,名称列表完整。
    #[cfg(feature = "auth")]
    #[tokio::test]
    async fn test_garrison_registry_all_failing_maps_unavailable() {
        let engines: Vec<Arc<dyn ScraperEngine>> = vec![
            Arc::new(MockFailEngine::new("engine_a")),
            Arc::new(MockFailEngine::new("engine_b")),
        ];
        let config = HealthCheckConfig {
            max_consecutive_failures: 1, // 单次失败即 Unhealthy
            ..test_config()
        };
        let monitor = Arc::new(EngineHealthMonitor::new_with_config(engines, config));
        let registry = super::garrison_health::build_health_registry(&monitor);

        let report = registry.check_all().await;
        assert_eq!(report.overall, garrison::health::HealthStatus::Unhealthy);
        let mapped = super::garrison_health::map_report(&report);
        assert_eq!(mapped, AggregateHealthStatus::Unavailable);
        // 与迁移前聚合一致
        assert_eq!(mapped, monitor.get_aggregate_status().await);
    }

    /// 库 check_timeout 护栏:单项探测超过 registry 超时即按 Unhealthy 聚合,
    /// 不无限阻塞 readiness 探针。
    #[cfg(feature = "auth")]
    #[tokio::test]
    async fn test_garrison_registry_probe_timeout_marks_unhealthy() {
        struct SlowEngine;

        #[async_trait]
        impl ScraperEngine for SlowEngine {
            async fn scrape(
                &self,
                _request: &InternalScrapeRequest,
            ) -> Result<InternalScrapeResponse, EngineError> {
                tokio::time::sleep(Duration::from_millis(300)).await;
                Ok(InternalScrapeResponse {
                    js_degraded: false,
                    captured_responses: Vec::new(),
                    status_code: 200,
                    content: "slow".to_string(),
                    screenshot: None,
                    content_type: "text/html".to_string(),
                    headers: HashMap::new(),
                    response_time_ms: 300,
                    final_url: None,
                })
            }

            fn support_score(&self, _request: &InternalScrapeRequest) -> u8 {
                100
            }

            fn name(&self) -> &'static str {
                "slow-engine"
            }
        }

        let config = HealthCheckConfig {
            timeout: Duration::from_millis(50), // 探活与库护栏同为 50ms
            max_consecutive_failures: 1,
            ..test_config()
        };
        let engines: Vec<Arc<dyn ScraperEngine>> = vec![Arc::new(SlowEngine)];
        let monitor = Arc::new(EngineHealthMonitor::new_with_config(engines, config));
        let registry = super::garrison_health::build_health_registry(&monitor);

        let report = registry.check_all().await;
        assert_eq!(
            report.overall,
            garrison::health::HealthStatus::Unhealthy,
            "探测超过 check_timeout 护栏必须按 Unhealthy 聚合"
        );
        assert_eq!(report.checks.len(), 1);
        assert_eq!(report.checks[0].name, "slow-engine");
        assert_eq!(
            super::garrison_health::map_report(&report),
            AggregateHealthStatus::Unavailable
        );
    }

    /// health_routes 端点语义(库承载):全部检查不健康时 readiness 返回 503
    /// (K8s 摘流语义),liveness 恒 200。
    #[cfg(feature = "auth")]
    #[tokio::test]
    async fn test_garrison_health_routes_readiness_503_when_unhealthy() {
        use axum::body::Body;
        use axum::http::{Request, StatusCode};
        use garrison::health::{HealthCheck, HealthRegistry, HealthResult, HealthStatus};
        use std::future::Future;
        use std::pin::Pin;
        use tower::ServiceExt;

        struct AlwaysUnhealthy;

        impl HealthCheck for AlwaysUnhealthy {
            fn name(&self) -> &str {
                "downstream"
            }

            fn check(&self) -> Pin<Box<dyn Future<Output = HealthResult<HealthStatus>> + Send>> {
                Box::pin(async { Ok(HealthStatus::Unhealthy) })
            }
        }

        let mut registry = HealthRegistry::new();
        registry.register(Box::new(AlwaysUnhealthy));
        let app = garrison::health::axum_routes::health_routes(Arc::new(registry));

        let response = app
            .oneshot(
                Request::builder()
                    .uri("/health/ready")
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(
            response.status(),
            StatusCode::SERVICE_UNAVAILABLE,
            "全部下线时 readiness 必须返回非 2xx"
        );

        let live_app = {
            let mut registry = HealthRegistry::new();
            registry.register(Box::new(AlwaysUnhealthy));
            garrison::health::axum_routes::health_routes(Arc::new(registry))
        };
        let live = live_app
            .oneshot(
                Request::builder()
                    .uri("/health/live")
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(live.status(), StatusCode::OK, "liveness 必须恒 200");
    }
}