limiteron 0.2.10

Unified flow control framework for 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
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
// Copyright (c) 2026 Kirky.X
// SPDX-License-Identifier: MIT
//! 设备类型匹配器
//!
//! 基于User-Agent解析设备类型、浏览器和操作系统信息。
//!
//! # 特性
//!
//! - 识别设备类型(移动端/桌面端/平板/API/未知)
//! - 识别浏览器类型
//! - 识别操作系统
//! - 内置缓存(性能开销 < 500μs)
//! - 支持自定义规则
//!
//! # 性能
//!
//! - 识别准确率 > 90%
//! - 性能开销 P99 < 500μs
//! - 缓存命中率 > 90%
//!
//! # 使用示例
//!
//! ```rust
//! use limiteron::matchers::device::DeviceMatcher;
//!
//! #[tokio::main]
//! async fn main() {
//!     let matcher = DeviceMatcher::new().await.unwrap();
//!     let user_agent = "Mozilla/5.0 (iPhone; CPU iPhone OS 14_0 like Mac OS X) AppleWebKit/605.1.15";
//!     let info = matcher.parse(user_agent).await.unwrap();
//! }
//! ```

#[cfg(feature = "device-matching")]
use crate::error::LimiteronError;
use log::{debug, info};
use oxcache::Cache;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};
use woothee::parser::Parser;

// ============================================================================
// 输入验证常量
// ============================================================================

/// 最大 User-Agent 长度
const MAX_USER_AGENT_LENGTH: usize = 2048;

// ============================================================================
// 输入验证函数
// ============================================================================

/// 清理 User-Agent 字符串
///
/// # 参数
/// - `user_agent`: User-Agent 字符串
///
/// # 返回
/// - 清理后的字符串
fn sanitize_user_agent(user_agent: &str) -> String {
    user_agent
        .chars()
        .filter(|&c| c.is_ascii_graphic() || c == ' ')
        .collect()
}

// ============================================================================
// 设备类型
// ============================================================================

#[cfg(feature = "device-matching")]
/// 设备类型
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum DeviceType {
    /// 移动设备
    Mobile,
    /// 桌面设备
    Desktop,
    /// 平板设备
    Tablet,
    /// API客户端
    API,
    /// 未知设备
    Unknown,
}

impl DeviceType {
    /// 从字符串解析设备类型
    pub fn parse(s: &str) -> Self {
        match s.to_lowercase().as_str() {
            "mobile" | "smartphone" => DeviceType::Mobile,
            "desktop" | "pc" => DeviceType::Desktop,
            "tablet" | "ipad" => DeviceType::Tablet,
            "api" | "bot" | "crawler" => DeviceType::API,
            _ => DeviceType::Unknown,
        }
    }

    /// 转换为字符串
    pub fn as_str(&self) -> &'static str {
        match self {
            DeviceType::Mobile => "mobile",
            DeviceType::Desktop => "desktop",
            DeviceType::Tablet => "tablet",
            DeviceType::API => "api",
            DeviceType::Unknown => "unknown",
        }
    }

    /// 检查是否为移动设备(包括平板)
    pub fn is_mobile(&self) -> bool {
        matches!(self, DeviceType::Mobile | DeviceType::Tablet)
    }

    /// 检查是否为桌面设备
    pub fn is_desktop(&self) -> bool {
        matches!(self, DeviceType::Desktop)
    }

    /// 检查是否为API客户端
    pub fn is_api(&self) -> bool {
        matches!(self, DeviceType::API)
    }
}

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

// ============================================================================
// 设备信息
// ============================================================================

#[cfg(feature = "device-matching")]
/// 设备信息
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct DeviceInfo {
    /// 设备类型
    pub device_type: DeviceType,
    /// 浏览器名称
    pub browser: Option<String>,
    /// 浏览器版本
    pub browser_version: Option<String>,
    /// 操作系统
    pub os: Option<String>,
    /// 操作系统版本
    pub os_version: Option<String>,
    /// 原始User-Agent
    pub user_agent: Option<String>,
}

impl DeviceInfo {
    /// 创建空的设备信息
    pub fn empty() -> Self {
        Self {
            device_type: DeviceType::Unknown,
            browser: None,
            browser_version: None,
            os: None,
            os_version: None,
            user_agent: None,
        }
    }

    /// 检查是否为空
    pub fn is_empty(&self) -> bool {
        self.device_type == DeviceType::Unknown && self.browser.is_none() && self.os.is_none()
    }

    /// 获取设备描述
    pub fn description(&self) -> String {
        match (&self.device_type, &self.browser, &self.os) {
            (DeviceType::API, _, _) => "API Client".to_string(),
            (_dt, Some(browser), Some(os)) => {
                format!(
                    "{} {} on {} {}",
                    browser,
                    self.browser_version.as_deref().unwrap_or(""),
                    os,
                    self.os_version.as_deref().unwrap_or("")
                )
            }
            (dt, Some(browser), None) => {
                format!(
                    "{} {} on {}",
                    browser,
                    self.browser_version.as_deref().unwrap_or(""),
                    dt
                )
            }
            (dt, None, Some(os)) => {
                format!("{} on {}", os, dt)
            }
            (dt, None, None) => dt.to_string(),
        }
    }

    /// 从woothee结果创建设备信息
    fn from_woothee(result: &woothee::parser::WootheeResult) -> Self {
        let device_type = Self::map_woothee_device_type(result.category);

        let browser = if device_type != DeviceType::API {
            Some(result.name.to_string())
        } else {
            None
        };

        let browser_version = if device_type != DeviceType::API {
            Some(result.version.to_string())
        } else {
            None
        };

        let os = if device_type != DeviceType::API {
            Some(result.os.to_string())
        } else {
            None
        };

        let os_version = if device_type != DeviceType::API {
            Some(result.os_version.to_string())
        } else {
            None
        };

        Self {
            device_type,
            browser,
            browser_version,
            os,
            os_version,
            user_agent: None,
        }
    }

    /// 映射woothee设备类型
    fn map_woothee_device_type(category: &str) -> DeviceType {
        match category.to_lowercase().as_str() {
            "pc" => DeviceType::Desktop,
            "smartphone" => DeviceType::Mobile,
            "mobilephone" => DeviceType::Mobile,
            "tablet" => DeviceType::Tablet,
            "appliance" => DeviceType::API,
            "crawler" => DeviceType::API,
            "misc" => DeviceType::API,
            _ => DeviceType::Unknown,
        }
    }
}

impl Default for DeviceInfo {
    fn default() -> Self {
        Self::empty()
    }
}

// ============================================================================
// 设备匹配条件
// ============================================================================

#[cfg(feature = "device-matching")]
/// 设备匹配条件
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct DeviceCondition {
    /// 设备类型列表
    pub device_types: Vec<DeviceType>,
    /// 浏览器名称列表
    pub browsers: Vec<String>,
    /// 操作系统列表
    pub os: Vec<String>,
}

impl DeviceCondition {
    /// 创建空的匹配条件
    pub fn empty() -> Self {
        Self {
            device_types: vec![],
            browsers: vec![],
            os: vec![],
        }
    }

    /// 创建设备类型匹配条件
    pub fn device_types(device_types: Vec<DeviceType>) -> Self {
        Self {
            device_types,
            browsers: vec![],
            os: vec![],
        }
    }

    /// 创建浏览器匹配条件
    pub fn browsers(browsers: Vec<String>) -> Self {
        Self {
            device_types: vec![],
            browsers,
            os: vec![],
        }
    }

    /// 创建操作系统匹配条件
    pub fn os(os: Vec<String>) -> Self {
        Self {
            device_types: vec![],
            browsers: vec![],
            os,
        }
    }

    /// 检查是否为空
    pub fn is_empty(&self) -> bool {
        self.device_types.is_empty() && self.browsers.is_empty() && self.os.is_empty()
    }

    /// 检查设备信息是否匹配条件
    pub fn matches(&self, info: &DeviceInfo) -> bool {
        if self.is_empty() {
            return true;
        }

        // 检查设备类型匹配
        if !self.device_types.is_empty() {
            if self.device_types.contains(&info.device_type) {
                return true;
            }
            return false;
        }

        // 检查浏览器匹配
        if !self.browsers.is_empty() {
            if let Some(browser) = &info.browser {
                if self.browsers.iter().any(|b| {
                    browser.to_lowercase().contains(&b.to_lowercase())
                        || b.to_lowercase().contains(&browser.to_lowercase())
                }) {
                    return true;
                }
            }
            return false;
        }

        // 检查操作系统匹配
        if !self.os.is_empty() {
            if let Some(os) = &info.os {
                if self.os.iter().any(|o| {
                    os.to_lowercase().contains(&o.to_lowercase())
                        || o.to_lowercase().contains(&os.to_lowercase())
                }) {
                    return true;
                }
            }
            return false;
        }

        false
    }
}

impl Default for DeviceCondition {
    fn default() -> Self {
        Self::empty()
    }
}

// ============================================================================
// 设备匹配器
// ============================================================================

/// 设备匹配器
///
/// 解析User-Agent并识别设备类型、浏览器和操作系统。
#[cfg(feature = "device-matching")]
pub struct DeviceMatcher {
    /// Woothee解析器
    parser: Arc<Parser>,
    /// 查询缓存
    cache: Arc<Cache<String, DeviceInfo>>,
    /// 缓存大小限制
    cache_size_limit: usize,
    /// 缓存命中次数
    cache_hits: AtomicU64,
    /// 缓存未命中次数
    cache_misses: AtomicU64,
    /// 自定义规则
    custom_rules: Vec<DeviceCustomRule>,
}

/// 自定义设备规则
#[derive(Debug, Clone)]
struct DeviceCustomRule {
    /// 规则名称
    name: String,
    /// 匹配模式(正则表达式)
    pattern: String,
    /// 设备类型
    device_type: DeviceType,
    /// 浏览器名称
    browser: Option<String>,
    /// 操作系统
    os: Option<String>,
}

impl DeviceMatcher {
    /// 创建新的设备匹配器(保持向后兼容)
    ///
    /// # 返回
    /// - `Ok(DeviceMatcher)`: 成功创建匹配器
    /// - `Err(LimiteronError)`: 创建失败
    ///
    /// # 示例
    /// ```rust
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// use limiteron::matchers::device::DeviceMatcher;
    ///
    /// let matcher = DeviceMatcher::new().await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn new() -> Result<Self, LimiteronError> {
        info!(target: "device", "创建DeviceMatcher");

        let parser = Parser::new();
        let cache = Cache::builder()
            .build()
            .await
            .map_err(|e| LimiteronError::ConfigError(e.to_string()))?;

        let matcher = Self {
            parser: Arc::new(parser),
            cache: Arc::new(cache),
            cache_size_limit: 10_000,
            cache_hits: AtomicU64::new(0),
            cache_misses: AtomicU64::new(0),
            custom_rules: Self::default_custom_rules(),
        };

        info!(target: "device", "DeviceMatcher创建成功");
        Ok(matcher)
    }

    /// 创建设置器(Builder模式)
    ///
    /// # 示例
    /// ```rust
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// use limiteron::matchers::device::DeviceMatcher;
    /// use oxcache::Cache;
    ///
    /// let matcher = DeviceMatcher::builder()
    ///     .cache_size_limit(5000)
    ///     .build()
    ///     .await?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn builder() -> DeviceMatcherBuilder {
        DeviceMatcherBuilder::new()
    }

    /// 使用依赖注入创建(完整依赖模式)
    ///
    /// 接受外部依赖,实现完全的依赖注入。
    ///
    /// # 参数
    /// - `parser`: Woothee解析器
    /// - `cache`: 查询缓存
    /// - `cache_size_limit`: 缓存大小限制
    pub fn with_dependencies(
        parser: Arc<Parser>,
        cache: Arc<Cache<String, DeviceInfo>>,
        cache_size_limit: usize,
    ) -> Self {
        let mut matcher = Self {
            parser,
            cache,
            cache_size_limit,
            cache_hits: AtomicU64::new(0),
            cache_misses: AtomicU64::new(0),
            custom_rules: Self::default_custom_rules(),
        };
        matcher.cache_size_limit = cache_size_limit;
        matcher
    }

    /// 创建带缓存大小限制的设备匹配器
    ///
    /// # 参数
    /// - `cache_size_limit`: 缓存大小限制
    ///
    /// # 示例
    /// ```rust
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// use limiteron::matchers::device::DeviceMatcher;
    ///
    /// let matcher = DeviceMatcher::with_cache_limit(5000).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn with_cache_limit(cache_size_limit: usize) -> Result<Self, LimiteronError> {
        let mut matcher = Self::new().await?;
        matcher.cache_size_limit = cache_size_limit;
        Ok(matcher)
    }

    /// 解析User-Agent
    ///
    /// # 参数
    /// - `user_agent`: User-Agent字符串
    ///
    /// # 返回
    /// - `Ok(DeviceInfo)`: 设备信息
    /// - `Err(LimiteronError)`: 解析失败
    ///
    /// # 性能
    /// - 首次解析: ~100μs
    /// - 缓存命中: < 10μs
    ///
    /// # 示例
    /// ```rust
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// use limiteron::matchers::device::DeviceMatcher;
    ///
    /// let matcher = DeviceMatcher::new().await?;
    /// let user_agent = "Mozilla/5.0 (iPhone; CPU iPhone OS 14_0 like Mac OS X)";
    /// let info = matcher.parse(user_agent).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn parse(&self, user_agent: &str) -> Result<DeviceInfo, LimiteronError> {
        // 清理 User-Agent
        let sanitized = sanitize_user_agent(user_agent);
        let user_agent = sanitized.trim();

        // 如果清理后为空,直接返回空的 DeviceInfo
        if user_agent.is_empty() {
            return Ok(DeviceInfo::empty());
        }

        // 验证 User-Agent 长度
        if user_agent.len() > MAX_USER_AGENT_LENGTH {
            return Err(LimiteronError::ConfigError(format!(
                "User-Agent 长度超过限制(最大 {} 字符)",
                MAX_USER_AGENT_LENGTH
            )));
        }

        // 检查缓存
        let cache_key = user_agent.to_string();
        if let Ok(Some(cached)) = self.cache.get(&cache_key).await {
            log::debug!(target: "device", "缓存命中: {}", user_agent);
            self.cache_hits.fetch_add(1, Ordering::Relaxed);
            return Ok(cached);
        }

        // 记录缓存未命中
        self.cache_misses.fetch_add(1, Ordering::Relaxed);

        log::debug!(target: "device", "解析User-Agent: {}", user_agent);

        // 检查自定义规则
        for rule in &self.custom_rules {
            if let Ok(re) = regex::Regex::new(&rule.pattern) {
                if re.is_match(user_agent) {
                    let info = DeviceInfo {
                        device_type: rule.device_type,
                        browser: rule.browser.clone(),
                        browser_version: None,
                        os: rule.os.clone(),
                        os_version: None,
                        user_agent: Some(user_agent.to_string()),
                    };
                    self.update_cache(user_agent, &info).await;
                    log::debug!(target: "device", "自定义规则匹配: {}", rule.name);
                    return Ok(info);
                }
            }
        }

        // 使用woothee解析
        let result = self.parser.parse(user_agent);
        let mut info = if let Some(res) = result {
            DeviceInfo::from_woothee(&res)
        } else {
            DeviceInfo::empty()
        };
        info.user_agent = Some(user_agent.to_string());

        // 更新缓存
        self.update_cache(user_agent, &info).await;

        log::debug!(
            target: "device",
            "User-Agent解析成功: {} -> {}",
            user_agent,
            info.description()
        );
        Ok(info)
    }

    /// 批量解析User-Agent
    ///
    /// # 参数
    /// - `user_agents`: User-Agent字符串列表
    ///
    /// # 返回
    /// - `Vec<Result<DeviceInfo>>`: 设备信息列表
    ///
    /// # 示例
    /// ```rust
    /// use limiteron::matchers::device::DeviceMatcher;
    ///
    /// #[tokio::main]
    /// async fn main() {
    ///     let matcher = DeviceMatcher::new().await.unwrap();
    ///     let user_agents: Vec<String> = vec![
    ///         "Mozilla/5.0 (iPhone; CPU iPhone OS 14_0 like Mac OS X)".to_string(),
    ///         "Mozilla/5.0 (Windows NT 10.0; Win64; x64)".to_string(),
    ///     ];
    ///     let results = matcher.batch_parse(&user_agents).await;
    /// }
    /// ```
    pub async fn batch_parse(
        &self,
        user_agents: &[String],
    ) -> Vec<Result<DeviceInfo, LimiteronError>> {
        let mut results = Vec::with_capacity(user_agents.len());
        for ua in user_agents {
            results.push(self.parse(ua).await);
        }
        results
    }

    /// 检查User-Agent是否匹配设备条件
    ///
    /// # 参数
    /// - `user_agent`: User-Agent字符串
    /// - `condition`: 设备匹配条件
    ///
    /// # 返回
    /// - `Ok(true)`: 匹配
    /// - `Ok(false)`: 不匹配
    /// - `Err(LimiteronError)`: 解析失败
    ///
    /// # 示例
    /// ```rust
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// use limiteron::matchers::device::{DeviceMatcher, DeviceCondition, DeviceType};
    ///
    /// let matcher = DeviceMatcher::new().await?;
    /// let condition = DeviceCondition::device_types(vec![DeviceType::Mobile]);
    /// let user_agent = "Mozilla/5.0 (iPhone; CPU iPhone OS 14_0 like Mac OS X)";
    /// let matched = matcher.matches_user_agent(user_agent, &condition).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn matches_user_agent(
        &self,
        user_agent: &str,
        condition: &DeviceCondition,
    ) -> Result<bool, LimiteronError> {
        let info = self.parse(user_agent).await?;
        Ok(condition.matches(&info))
    }

    /// 检查设备信息是否匹配条件
    ///
    /// # 参数
    /// - `info`: 设备信息
    /// - `condition`: 设备匹配条件
    ///
    /// # 返回
    /// - `true`: 匹配
    /// - `false`: 不匹配
    ///
    /// # 示例
    /// ```rust
    /// use limiteron::matchers::device::{DeviceInfo, DeviceCondition, DeviceType};
    ///
    /// let info = DeviceInfo {
    ///     device_type: DeviceType::Mobile,
    ///     browser: Some("Safari".to_string()),
    ///     browser_version: Some("14.0".to_string()),
    ///     os: Some("iOS".to_string()),
    ///     os_version: Some("14.0".to_string()),
    ///     user_agent: None,
    /// };
    ///
    /// let condition = DeviceCondition::device_types(vec![DeviceType::Mobile]);
    /// let matched = condition.matches(&info);
    /// ```
    pub fn matches(&self, info: &DeviceInfo, condition: &DeviceCondition) -> bool {
        condition.matches(info)
    }

    /// 添加自定义规则
    ///
    /// # 参数
    /// - `name`: 规则名称
    /// - `pattern`: 匹配模式(正则表达式)
    /// - `device_type`: 设备类型
    /// - `browser`: 浏览器名称(可选)
    /// - `os`: 操作系统(可选)
    ///
    /// # 示例
    /// ```rust
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// use limiteron::matchers::device::{DeviceMatcher, DeviceType};
    ///
    /// let mut matcher = DeviceMatcher::new().await?;
    /// matcher.add_custom_rule(
    ///     "MyCustomApp",
    ///     r"MyCustomApp/\d+\.\d+",
    ///     DeviceType::Mobile,
    ///     Some("MyCustomApp".to_string()),
    ///     Some("Android".to_string()),
    /// );
    /// # Ok(())
    /// # }
    /// ```
    pub fn add_custom_rule(
        &mut self,
        name: &str,
        pattern: &str,
        device_type: DeviceType,
        browser: Option<String>,
        os: Option<String>,
    ) {
        let rule = DeviceCustomRule {
            name: name.to_string(),
            pattern: pattern.to_string(),
            device_type,
            browser,
            os,
        };

        // 验证正则表达式
        if regex::Regex::new(&rule.pattern).is_err() {
            log::warn!(target: "device", "无效的正则表达式: {}", pattern);
            return;
        }

        self.custom_rules.push(rule);
        log::info!(target: "device", "添加自定义规则: {}", name);
    }

    /// 移除自定义规则
    ///
    /// # 参数
    /// - `name`: 规则名称
    ///
    /// # 返回
    /// - `true`: 成功移除
    /// - `false`: 规则不存在
    pub fn remove_custom_rule(&mut self, name: &str) -> bool {
        let original_len = self.custom_rules.len();
        self.custom_rules.retain(|r| r.name != name);
        let removed = self.custom_rules.len() < original_len;
        if removed {
            log::info!(target: "device", "移除自定义规则: {}", name);
        }
        removed
    }

    /// 清空缓存
    pub async fn clear_cache(&self) {
        let size = self.cache.len().await.unwrap_or(0);
        let _ = self.cache.clear().await;
        log::info!(target: "device", "缓存已清空,移除 {} 条记录", size);
    }

    /// 获取缓存统计信息
    pub async fn cache_stats(&self) -> DeviceCacheStats {
        let hits = self.cache_hits.load(Ordering::Relaxed);
        let misses = self.cache_misses.load(Ordering::Relaxed);
        let total = hits.saturating_add(misses);
        let hit_rate = if total > 0 {
            (hits as f64 / total as f64) * 100.0
        } else {
            0.0
        };

        DeviceCacheStats {
            size: self.cache.len().await.unwrap_or(0) as usize,
            limit: self.cache_size_limit,
            hit_rate,
            hits,
            misses,
        }
    }

    /// 更新缓存
    async fn update_cache(&self, user_agent: &str, info: &DeviceInfo) {
        let cache_len = self.cache.len().await.unwrap_or(0);
        if cache_len >= self.cache_size_limit as u64 {
            let _maybe_first = (0..(self.cache_size_limit / 10)).next();
            debug!(target: "device", "缓存接近限制 ({}/{})", cache_len, self.cache_size_limit);
        }

        let _ = self.cache.set(&user_agent.to_string(), info).await;
    }

    /// 默认自定义规则
    fn default_custom_rules() -> Vec<DeviceCustomRule> {
        vec![
            // 常见的API客户端
            DeviceCustomRule {
                name: "curl".to_string(),
                pattern: r"^curl/".to_string(),
                device_type: DeviceType::API,
                browser: Some("curl".to_string()),
                os: None,
            },
            DeviceCustomRule {
                name: "wget".to_string(),
                pattern: r"^Wget/".to_string(),
                device_type: DeviceType::API,
                browser: Some("wget".to_string()),
                os: None,
            },
            // 常见的爬虫
            DeviceCustomRule {
                name: "googlebot".to_string(),
                pattern: r"Googlebot".to_string(),
                device_type: DeviceType::API,
                browser: Some("Googlebot".to_string()),
                os: None,
            },
            DeviceCustomRule {
                name: "bingbot".to_string(),
                pattern: r"Bingbot".to_string(),
                device_type: DeviceType::API,
                browser: Some("Bingbot".to_string()),
                os: None,
            },
        ]
    }
}

/// 设备匹配器设置器
#[derive(Debug, Clone)]
pub struct DeviceMatcherBuilder {
    cache_size_limit: usize,
    custom_rules: Vec<DeviceCustomRule>,
}

impl DeviceMatcherBuilder {
    /// 创建新的设置器
    pub fn new() -> Self {
        Self {
            cache_size_limit: 10_000,
            custom_rules: Vec::new(),
        }
    }

    /// 设置缓存大小限制
    pub fn cache_size_limit(mut self, cache_size_limit: usize) -> Self {
        self.cache_size_limit = cache_size_limit;
        self
    }

    /// 添加自定义规则
    pub fn add_custom_rule(
        mut self,
        name: &str,
        pattern: &str,
        device_type: DeviceType,
        browser: Option<String>,
        os: Option<String>,
    ) -> Self {
        let rule = DeviceCustomRule {
            name: name.to_string(),
            pattern: pattern.to_string(),
            device_type,
            browser,
            os,
        };
        self.custom_rules.push(rule);
        self
    }

    /// 构建DeviceMatcher
    ///
    /// # 返回
    /// - `Ok(DeviceMatcher)`: 成功创建设备匹配器
    /// - `Err(LimiteronError)`: 创建失败
    pub async fn build(self) -> Result<DeviceMatcher, LimiteronError> {
        let mut matcher = DeviceMatcher::new().await?;
        matcher.cache_size_limit = self.cache_size_limit;
        matcher.custom_rules = self.custom_rules;
        Ok(matcher)
    }
}

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

// ============================================================================
// 缓存统计信息
// ============================================================================

/// 设备缓存统计信息
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DeviceCacheStats {
    /// 当前缓存大小
    pub size: usize,
    /// 缓存大小限制
    pub limit: usize,
    /// 缓存命中率(百分比)
    pub hit_rate: f64,
    /// 缓存命中次数
    pub hits: u64,
    /// 缓存未命中次数
    pub misses: u64,
}

// ============================================================================
// 单元测试
// ============================================================================

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

    #[test]
    fn test_device_type_from_str() {
        assert_eq!(DeviceType::parse("mobile"), DeviceType::Mobile);
        assert_eq!(DeviceType::parse("desktop"), DeviceType::Desktop);
        assert_eq!(DeviceType::parse("tablet"), DeviceType::Tablet);
        assert_eq!(DeviceType::parse("api"), DeviceType::API);
        assert_eq!(DeviceType::parse("unknown"), DeviceType::Unknown);
        assert_eq!(DeviceType::parse("invalid"), DeviceType::Unknown);
    }

    #[test]
    fn test_device_type_as_str() {
        assert_eq!(DeviceType::Mobile.as_str(), "mobile");
        assert_eq!(DeviceType::Desktop.as_str(), "desktop");
        assert_eq!(DeviceType::Tablet.as_str(), "tablet");
        assert_eq!(DeviceType::API.as_str(), "api");
        assert_eq!(DeviceType::Unknown.as_str(), "unknown");
    }

    #[test]
    fn test_device_type_is_mobile() {
        assert!(DeviceType::Mobile.is_mobile());
        assert!(DeviceType::Tablet.is_mobile());
        assert!(!DeviceType::Desktop.is_mobile());
        assert!(!DeviceType::API.is_mobile());
    }

    #[test]
    fn test_device_info_empty() {
        let info = DeviceInfo::empty();
        assert!(info.is_empty());
        assert_eq!(info.description(), "unknown");
    }

    #[test]
    fn test_device_info_description() {
        let info1 = DeviceInfo {
            device_type: DeviceType::Mobile,
            browser: Some("Safari".to_string()),
            browser_version: Some("14.0".to_string()),
            os: Some("iOS".to_string()),
            os_version: Some("14.0".to_string()),
            user_agent: None,
        };
        assert!(info1.description().contains("Safari"));
        assert!(info1.description().contains("iOS"));

        let info2 = DeviceInfo {
            device_type: DeviceType::API,
            browser: None,
            browser_version: None,
            os: None,
            os_version: None,
            user_agent: None,
        };
        assert_eq!(info2.description(), "API Client");
    }

    #[test]
    fn test_device_condition_empty() {
        let condition = DeviceCondition::empty();
        assert!(condition.is_empty());

        let info = DeviceInfo::empty();
        assert!(condition.matches(&info));
    }

    #[test]
    fn test_device_condition_device_types() {
        let condition = DeviceCondition::device_types(vec![DeviceType::Mobile]);

        let info1 = DeviceInfo {
            device_type: DeviceType::Mobile,
            browser: None,
            browser_version: None,
            os: None,
            os_version: None,
            user_agent: None,
        };
        assert!(condition.matches(&info1));

        let info2 = DeviceInfo {
            device_type: DeviceType::Desktop,
            browser: None,
            browser_version: None,
            os: None,
            os_version: None,
            user_agent: None,
        };
        assert!(!condition.matches(&info2));
    }

    #[test]
    fn test_device_condition_browsers() {
        let condition = DeviceCondition::browsers(vec!["Safari".to_string(), "Chrome".to_string()]);

        let info1 = DeviceInfo {
            device_type: DeviceType::Mobile,
            browser: Some("Safari".to_string()),
            browser_version: None,
            os: None,
            os_version: None,
            user_agent: None,
        };
        assert!(condition.matches(&info1));

        let info2 = DeviceInfo {
            device_type: DeviceType::Mobile,
            browser: Some("Firefox".to_string()),
            browser_version: None,
            os: None,
            os_version: None,
            user_agent: None,
        };
        assert!(!condition.matches(&info2));
    }

    #[test]
    fn test_device_condition_os() {
        let condition = DeviceCondition::os(vec!["iOS".to_string(), "Android".to_string()]);

        let info1 = DeviceInfo {
            device_type: DeviceType::Mobile,
            browser: None,
            browser_version: None,
            os: Some("iOS".to_string()),
            os_version: None,
            user_agent: None,
        };
        assert!(condition.matches(&info1));

        let info2 = DeviceInfo {
            device_type: DeviceType::Desktop,
            browser: None,
            browser_version: None,
            os: Some("Windows".to_string()),
            os_version: None,
            user_agent: None,
        };
        assert!(!condition.matches(&info2));
    }

    #[test]
    fn test_device_condition_default() {
        let condition = DeviceCondition::default();
        assert!(condition.is_empty());
    }

    #[test]
    fn test_device_info_serialization() {
        let info = DeviceInfo {
            device_type: DeviceType::Mobile,
            browser: Some("Safari".to_string()),
            browser_version: Some("14.0".to_string()),
            os: Some("iOS".to_string()),
            os_version: Some("14.0".to_string()),
            user_agent: Some("Test".to_string()),
        };

        let json = serde_json::to_string(&info).unwrap();
        let deserialized: DeviceInfo = serde_json::from_str(&json).unwrap();

        assert_eq!(info, deserialized);
    }

    #[test]
    fn test_device_condition_serialization() {
        let condition = DeviceCondition {
            device_types: vec![DeviceType::Mobile, DeviceType::Tablet],
            browsers: vec!["Safari".to_string()],
            os: vec!["iOS".to_string()],
        };

        let json = serde_json::to_string(&condition).unwrap();
        let deserialized: DeviceCondition = serde_json::from_str(&json).unwrap();

        assert_eq!(condition, deserialized);
    }

    #[tokio::test]
    async fn test_device_matcher_parse_empty() {
        let matcher = DeviceMatcher::new().await.unwrap();
        // 空字符串应该被清理为空,然后返回空的 DeviceInfo
        let info = matcher.parse("").await.unwrap();
        assert!(info.is_empty());

        // 只有空格的字符串也应该返回空的 DeviceInfo
        let info = matcher.parse("   ").await.unwrap();
        assert!(info.is_empty());
    }

    #[tokio::test]
    async fn test_device_matcher_parse_iphone() {
        let matcher = DeviceMatcher::new().await.unwrap();
        let user_agent = "Mozilla/5.0 (iPhone; CPU iPhone OS 14_0 like Mac OS X) \
                          AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0 Mobile/15E148 \
                          Safari/604.1";
        let info = matcher.parse(user_agent).await.unwrap();
        assert_eq!(info.device_type, DeviceType::Mobile);
        assert!(info.browser.as_ref().unwrap().contains("Safari"));
        // woothee可能返回不同的OS名称,所以只检查不为空
        assert!(info.os.is_some());
    }

    #[tokio::test]
    async fn test_device_matcher_parse_desktop() {
        let matcher = DeviceMatcher::new().await.unwrap();
        let user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, \
                          like Gecko) Chrome/91.0.4472.124 Safari/537.36";
        let info = matcher.parse(user_agent).await.unwrap();
        assert_eq!(info.device_type, DeviceType::Desktop);
        assert!(info.browser.as_ref().unwrap().contains("Chrome"));
        assert!(info.os.as_ref().unwrap().contains("Windows"));
    }

    #[tokio::test]
    async fn test_device_matcher_parse_curl() {
        let matcher = DeviceMatcher::new().await.unwrap();
        let user_agent = "curl/7.68.0";
        let info = matcher.parse(user_agent).await.unwrap();
        assert_eq!(info.device_type, DeviceType::API);
        assert_eq!(info.browser, Some("curl".to_string()));
    }

    #[tokio::test]
    async fn test_device_matcher_cache() {
        let matcher = DeviceMatcher::new().await.unwrap();
        let user_agent = "Mozilla/5.0 (iPhone; CPU iPhone OS 14_0 like Mac OS X)";

        // 第一次解析 - 应该缓存未命中
        let info1 = matcher.parse(user_agent).await.unwrap();
        let stats1 = matcher.cache_stats().await;
        // 第一次解析后应该有1次缓存未命中,0次命中
        assert_eq!(stats1.misses, 1);
        assert_eq!(stats1.hits, 0);

        // 第二次解析(应该命中缓存)
        let info2 = matcher.parse(user_agent).await.unwrap();
        assert_eq!(info1, info2);
        let stats2 = matcher.cache_stats().await;
        // 第二次解析后应该有1次缓存命中,1次未命中
        assert_eq!(stats2.hits, 1);
        assert_eq!(stats2.misses, 1);

        // 清空缓存 - 注意:清空缓存不会重置计数器
        matcher.clear_cache().await;
        let stats3 = matcher.cache_stats().await;
        // 计数器保持不变
        assert_eq!(stats3.hits, 1);
        assert_eq!(stats3.misses, 1);
    }

    #[tokio::test]
    async fn test_device_matcher_custom_rule() {
        let mut matcher = DeviceMatcher::new().await.unwrap();

        matcher.add_custom_rule(
            "TestApp",
            r"TestApp/\d+\.\d+",
            DeviceType::Mobile,
            Some("TestApp".to_string()),
            Some("Android".to_string()),
        );

        let user_agent = "TestApp/1.0.0";
        let info = matcher.parse(user_agent).await.unwrap();
        assert_eq!(info.device_type, DeviceType::Mobile);
        assert_eq!(info.browser, Some("TestApp".to_string()));
    }

    #[tokio::test]
    async fn test_device_matcher_remove_custom_rule() {
        let mut matcher = DeviceMatcher::new().await.unwrap();

        matcher.add_custom_rule(
            "TestApp",
            r"TestApp/\d+\.\d+",
            DeviceType::Mobile,
            Some("TestApp".to_string()),
            Some("Android".to_string()),
        );

        assert!(matcher.remove_custom_rule("TestApp"));
        assert!(!matcher.remove_custom_rule("NonExistent"));
    }

    #[tokio::test]
    async fn test_device_matcher_batch_parse() {
        let matcher = DeviceMatcher::new().await.unwrap();

        let user_agents = vec![
            "Mozilla/5.0 (iPhone; CPU iPhone OS 14_0 like Mac OS X)".to_string(),
            "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36".to_string(),
            "curl/7.68.0".to_string(),
        ];

        let results = matcher.batch_parse(&user_agents).await;
        assert_eq!(results.len(), 3);
        assert!(results.iter().all(|r| r.is_ok()));

        assert_eq!(results[0].as_ref().unwrap().device_type, DeviceType::Mobile);
        assert_eq!(
            results[1].as_ref().unwrap().device_type,
            DeviceType::Desktop
        );
        assert_eq!(results[2].as_ref().unwrap().device_type, DeviceType::API);
    }

    #[tokio::test]
    async fn test_device_matcher_matches_user_agent() {
        let matcher = DeviceMatcher::new().await.unwrap();
        let condition = DeviceCondition::device_types(vec![DeviceType::Mobile]);

        let user_agent = "Mozilla/5.0 (iPhone; CPU iPhone OS 14_0 like Mac OS X)";
        let matched = matcher
            .matches_user_agent(user_agent, &condition)
            .await
            .unwrap();
        assert!(matched);

        let user_agent2 = "Mozilla/5.0 (Windows NT 10.0; Win64; x64)";
        let matched2 = matcher
            .matches_user_agent(user_agent2, &condition)
            .await
            .unwrap();
        assert!(!matched2);
    }

    // === 新增覆盖率测试 ===

    #[test]
    fn test_device_type_parse_aliases() {
        assert_eq!(DeviceType::parse("smartphone"), DeviceType::Mobile);
        assert_eq!(DeviceType::parse("pc"), DeviceType::Desktop);
        assert_eq!(DeviceType::parse("ipad"), DeviceType::Tablet);
        assert_eq!(DeviceType::parse("bot"), DeviceType::API);
        assert_eq!(DeviceType::parse("crawler"), DeviceType::API);
    }

    #[test]
    fn test_device_type_helpers() {
        assert!(DeviceType::Desktop.is_desktop());
        assert!(!DeviceType::Mobile.is_desktop());
        assert!(!DeviceType::Tablet.is_desktop());
        assert!(DeviceType::API.is_api());
        assert!(!DeviceType::Mobile.is_api());
        assert!(!DeviceType::Desktop.is_api());
    }

    #[test]
    fn test_device_type_display() {
        assert_eq!(format!("{}", DeviceType::Mobile), "mobile");
        assert_eq!(format!("{}", DeviceType::Desktop), "desktop");
        assert_eq!(format!("{}", DeviceType::Tablet), "tablet");
        assert_eq!(format!("{}", DeviceType::API), "api");
        assert_eq!(format!("{}", DeviceType::Unknown), "unknown");
    }

    #[test]
    fn test_device_info_description_browser_only() {
        let info = DeviceInfo {
            device_type: DeviceType::Desktop,
            browser: Some("Chrome".to_string()),
            browser_version: Some("91".to_string()),
            os: None,
            os_version: None,
            user_agent: None,
        };
        assert_eq!(info.description(), "Chrome 91 on desktop");
    }

    #[test]
    fn test_device_info_description_os_only() {
        let info = DeviceInfo {
            device_type: DeviceType::Desktop,
            browser: None,
            browser_version: None,
            os: Some("Windows".to_string()),
            os_version: Some("10".to_string()),
            user_agent: None,
        };
        assert_eq!(info.description(), "Windows on desktop");
    }

    #[test]
    fn test_device_info_description_device_only() {
        let info = DeviceInfo {
            device_type: DeviceType::Desktop,
            browser: None,
            browser_version: None,
            os: None,
            os_version: None,
            user_agent: None,
        };
        assert_eq!(info.description(), "desktop");
    }

    #[test]
    fn test_device_condition_browser_none() {
        let condition = DeviceCondition::browsers(vec!["Safari".to_string()]);
        let info = DeviceInfo {
            device_type: DeviceType::Desktop,
            browser: None,
            browser_version: None,
            os: None,
            os_version: None,
            user_agent: None,
        };
        assert!(!condition.matches(&info));
    }

    #[test]
    fn test_device_condition_os_none() {
        let condition = DeviceCondition::os(vec!["iOS".to_string()]);
        let info = DeviceInfo {
            device_type: DeviceType::Desktop,
            browser: None,
            browser_version: None,
            os: None,
            os_version: None,
            user_agent: None,
        };
        assert!(!condition.matches(&info));
    }

    #[tokio::test]
    async fn test_device_matcher_builder() {
        let matcher = DeviceMatcher::builder()
            .cache_size_limit(500)
            .add_custom_rule(
                "CustomBot",
                r"CustomBot/\d+",
                DeviceType::API,
                Some("CustomBot".to_string()),
                None,
            )
            .build()
            .await
            .unwrap();
        assert_eq!(matcher.cache_stats().await.limit, 500);
        let info = matcher.parse("CustomBot/2.0").await.unwrap();
        assert_eq!(info.device_type, DeviceType::API);
    }

    #[tokio::test]
    async fn test_device_matcher_with_cache_limit() {
        let matcher = DeviceMatcher::with_cache_limit(500).await.unwrap();
        assert_eq!(matcher.cache_stats().await.limit, 500);
    }

    #[tokio::test]
    async fn test_device_matcher_parse_long_ua() {
        let matcher = DeviceMatcher::new().await.unwrap();
        let long_ua = "X".repeat(3000);
        let result = matcher.parse(&long_ua).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_device_matcher_parse_unrecognized() {
        let matcher = DeviceMatcher::new().await.unwrap();
        let info = matcher
            .parse("zzz1nvalid-th1ng-th4t-w00thee-c4nt-p4rs3")
            .await
            .unwrap();
        assert_eq!(info.device_type, DeviceType::Unknown);
        assert!(info.os.is_none());
    }

    #[tokio::test]
    async fn test_device_matcher_matches_direct() {
        let matcher = DeviceMatcher::new().await.unwrap();
        let info = DeviceInfo {
            device_type: DeviceType::Tablet,
            browser: Some("Safari".to_string()),
            browser_version: None,
            os: Some("iOS".to_string()),
            os_version: None,
            user_agent: None,
        };
        let condition = DeviceCondition::device_types(vec![DeviceType::Tablet]);
        assert!(matcher.matches(&info, &condition));
        let no_match = DeviceCondition::device_types(vec![DeviceType::Desktop]);
        assert!(!matcher.matches(&info, &no_match));
    }

    #[tokio::test]
    async fn test_device_matcher_invalid_custom_rule() {
        let mut matcher = DeviceMatcher::new().await.unwrap();
        let before = matcher.remove_custom_rule("non-existent");
        assert!(!before);
        matcher.add_custom_rule(
            "BadRegex",
            r"[invalid(unclosed",
            DeviceType::Mobile,
            None,
            None,
        );
        let info = matcher.parse("anything").await.unwrap();
        assert_eq!(info.device_type, DeviceType::Unknown);
    }

    #[tokio::test]
    async fn test_device_matcher_parse_cache_full() {
        let matcher = DeviceMatcher::with_cache_limit(2).await.unwrap();
        let _ = matcher
            .parse("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36")
            .await
            .unwrap();
        let _ = matcher.parse("curl/7.68.0").await.unwrap();
        let _ = matcher.parse("Wget/1.21").await.unwrap();
        let stats = matcher.cache_stats().await;
        assert!(stats.hits + stats.misses >= 3);
    }

    #[tokio::test]
    async fn test_device_matcher_clear_cache_with_data() {
        let matcher = DeviceMatcher::new().await.unwrap();
        let _ = matcher
            .parse("Mozilla/5.0 (iPhone; CPU iPhone OS 14_0 like Mac OS X) AppleWebKit/605.1.15")
            .await
            .unwrap();
        let _ = matcher
            .parse("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36")
            .await
            .unwrap();
        let stats_before = matcher.cache_stats().await;
        assert!(stats_before.hits == 0);
        matcher.clear_cache().await;
        let stats_after = matcher.cache_stats().await;
        assert_eq!(stats_after.size, 0);
    }

    #[tokio::test]
    async fn test_device_matcher_parse_tablet() {
        let matcher = DeviceMatcher::new().await.unwrap();
        let info = matcher.parse("Mozilla/5.0 (iPad; CPU OS 14_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0 Mobile/15E148 Safari/604.1").await.unwrap();
        assert_eq!(info.device_type, DeviceType::Mobile);
    }

    #[tokio::test]
    async fn test_device_matcher_parse_crawler() {
        let matcher = DeviceMatcher::new().await.unwrap();
        let info = matcher
            .parse("Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)")
            .await
            .unwrap();
        assert_eq!(info.device_type, DeviceType::API);
    }

    #[tokio::test]
    async fn test_device_matcher_parse_wget() {
        let matcher = DeviceMatcher::new().await.unwrap();
        let info = matcher.parse("Wget/1.21.1").await.unwrap();
        assert_eq!(info.device_type, DeviceType::API);
        assert_eq!(info.browser, Some("wget".to_string()));
    }

    #[tokio::test]
    async fn test_device_matcher_parse_bingbot() {
        let matcher = DeviceMatcher::new().await.unwrap();
        let info = matcher
            .parse("Mozilla/5.0 (compatible; Bingbot/2.0; +http://www.bing.com/bingbot.htm)")
            .await
            .unwrap();
        assert_eq!(info.device_type, DeviceType::API);
        assert_eq!(info.browser, Some("Bingbot".to_string()));
    }

    #[test]
    fn test_sanitize_user_agent_filters_non_ascii() {
        let result = sanitize_user_agent("Mozilla/5.0 \u{00e9}\u{00e0}iPhone");
        assert!(!result.contains('\u{00e9}'));
        assert!(!result.contains('\u{00e0}'));
        assert!(result.contains("Mozilla/5.0 iPhone"));
    }

    #[tokio::test]
    async fn test_device_matcher_with_dependencies() {
        use std::sync::Arc;
        let parser = Arc::new(Parser::new());
        let cache = Arc::new(Cache::builder().build().await.unwrap());
        let matcher = DeviceMatcher::with_dependencies(parser, cache, 1000);
        assert_eq!(matcher.cache_stats().await.limit, 1000);
        let info = matcher.parse("curl/7.68.0").await.unwrap();
        assert_eq!(info.device_type, DeviceType::API);
    }

    #[test]
    fn test_map_woothee_device_type_all_variants() {
        assert_eq!(
            DeviceInfo::map_woothee_device_type("pc"),
            DeviceType::Desktop
        );
        assert_eq!(
            DeviceInfo::map_woothee_device_type("smartphone"),
            DeviceType::Mobile
        );
        assert_eq!(
            DeviceInfo::map_woothee_device_type("mobilephone"),
            DeviceType::Mobile
        );
        assert_eq!(
            DeviceInfo::map_woothee_device_type("tablet"),
            DeviceType::Tablet
        );
        assert_eq!(
            DeviceInfo::map_woothee_device_type("appliance"),
            DeviceType::API
        );
        assert_eq!(
            DeviceInfo::map_woothee_device_type("crawler"),
            DeviceType::API
        );
        assert_eq!(DeviceInfo::map_woothee_device_type("misc"), DeviceType::API);
        assert_eq!(
            DeviceInfo::map_woothee_device_type("unknown_category"),
            DeviceType::Unknown
        );
    }

    #[test]
    fn test_device_info_from_woothee_api_category() {
        use woothee::parser::WootheeResult;
        let result = WootheeResult {
            name: "Googlebot",
            category: "crawler",
            os: "-",
            os_version: "-".into(),
            version: "2.1",
            browser_type: "-",
            vendor: "-",
        };
        let info = DeviceInfo::from_woothee(&result);
        assert_eq!(info.device_type, DeviceType::API);
        assert!(info.browser.is_none());
        assert!(info.os.is_none());
    }

    #[test]
    fn test_device_info_from_woothee_non_api() {
        use woothee::parser::WootheeResult;
        let result = WootheeResult {
            name: "Chrome",
            category: "pc",
            os: "Windows",
            os_version: "10".into(),
            version: "91.0",
            browser_type: "-",
            vendor: "-",
        };
        let info = DeviceInfo::from_woothee(&result);
        assert_eq!(info.device_type, DeviceType::Desktop);
        assert_eq!(info.browser, Some("Chrome".to_string()));
        assert_eq!(info.os, Some("Windows".to_string()));
    }

    #[test]
    fn test_device_info_default() {
        let info = DeviceInfo::default();
        assert!(info.is_empty());
    }

    #[cfg(feature = "device-matching")]
    #[test]
    fn test_device_matcher_builder_default() {
        let builder = DeviceMatcherBuilder::default();
        assert_eq!(builder.cache_size_limit, 10_000);
    }

    #[cfg(feature = "device-matching")]
    #[tokio::test]
    async fn test_device_matcher_cache_near_limit() {
        // 设置很小的缓存限制,解析多个不同 UA 触发缓存接近限制分支
        let matcher = DeviceMatcher::with_cache_limit(5).await.unwrap();
        // 解析 6 个不同的 UA,使缓存长度达到或超过限制
        let user_agents = vec![
            "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/91.0",
            "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15) Safari/605",
            "Mozilla/5.0 (X11; Linux x86_64; rv:89.0) Firefox/89.0",
            "Mozilla/5.0 (iPhone; CPU iPhone OS 14_0) Safari/605",
            "Mozilla/5.0 (Linux; Android 11) Chrome/91.0",
            "curl/7.68.0",
        ];
        for ua in &user_agents {
            let _ = matcher.parse(ua).await;
        }
        // 验证缓存已接近或达到限制
        let stats = matcher.cache_stats().await;
        assert!(stats.limit == 5);
    }
}