sz-rust-state-facade 0.6.3

State facade for sz-rust framework — session, cookie, env, event, i18n, mail, notify
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
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
//! Notify 模块 — 通知抽象层(对齐 PHP `think\facade\Notify`)
//!
//! 提供通知发送的统一抽象,支持多渠道(Slack、短信、邮件等)扩展。
//!
//! ## PHP 对齐
//!
//! ### 核心 API 映射
//!
//! | PHP 方法 | Rust 方法 | 说明 |
//! |---------|-----------|------|
//! | `Notify::channel($name)` | [`Notification::channel`] | 设置通知渠道 |
//! | `Notify::title($title)` | [`Notification::title`] | 设置通知标题 |
//! | `Notify::content($content)` | [`Notification::content`] | 设置通知内容 |
//! | `Notify::level($level)` | [`Notification::level`] | 设置通知级别 |
//! | `Notify::send()` | [`Notifier::send`] | 发送通知 |
//!
//! ### PHP 行为对齐
//!
//! - **Builder 模式**:PHP `Notify::channel()->title()->content()->send()` 链式调用。
//!   Rust 通过 [`Notification`] builder 实现相同链式 API。
//! - **多级别**:PHP 支持 info/warning/error/critical 四种级别。Rust 通过 [`NotifyLevel`] 表达。
//! - **多渠道**:PHP 支持按渠道分发(slack/sms/mail)。Rust 通过 [`Notifier`] trait 抽象。
//!
//! ## 架构说明
//!
//! - **Notifier trait 抽象**:业务方实现具体发送逻辑(Slack Webhook / 短信 API / 日志等)
//! - **MemoryNotifier**:内置内存实现,将通知暂存到 Vec,用于测试和开发环境
//! - **SlackNotifier**:Slack Webhook 实现,通过 [`HttpTransport`] trait 抽象 HTTP 发送,
//!   业务方注入具体 HTTP 客户端(如 reqwest)即可投入生产
//! - **HttpTransport trait**:HTTP 传输抽象,解耦 SlackNotifier 与具体 HTTP 库
//! - **MemoryHttpTransport**:内存 HTTP 传输实现,记录所有请求供测试断言

use parking_lot::Mutex;
use std::sync::Arc;
use thiserror::Error;

// ============================================================================
// 错误类型
// ============================================================================

/// Notify 错误
#[derive(Debug, Error)]
pub enum NotifyError {
    /// 缺少必填字段(渠道、标题、内容等)
    #[error("通知字段缺失: {0}")]
    MissingField(String),
    /// 通知发送失败
    #[error("通知发送失败: {0}")]
    SendFailed(String),
    /// HTTP 传输失败
    #[error("HTTP 传输失败: {0}")]
    HttpTransport(String),
    /// 序列化失败
    #[error("序列化失败: {0}")]
    Serialize(String),
}

// ============================================================================
// 通知级别
// ============================================================================

/// 通知级别 — 对齐 PHP `think\notify\Level`
///
/// 支持四种级别,按严重程度递增。
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[repr(u8)]
pub enum NotifyLevel {
    /// 信息(最低级别,蓝色)
    #[default]
    Info = 0,
    /// 警告(黄色)
    Warning = 1,
    /// 错误(红色)
    Error = 2,
    /// 严重(最高级别,加粗红色)
    Critical = 3,
}

impl NotifyLevel {
    /// 转换为 Slack 颜色(hex 颜色码或 Slack 内置颜色名)
    ///
    /// - `Info` → `#36a64f`(绿色)
    /// - `Warning` → `#ffcc00`(黄色)
    /// - `Error` → `#ff0000`(红色)
    /// - `Critical` → `#b22222`(深红)
    pub fn slack_color(self) -> &'static str {
        match self {
            Self::Info => "#36a64f",
            Self::Warning => "#ffcc00",
            Self::Error => "#ff0000",
            Self::Critical => "#b22222",
        }
    }

    /// 转换为字符串标识(对齐 PHP `strtolower(Level::class)`)
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Info => "info",
            Self::Warning => "warning",
            Self::Error => "error",
            Self::Critical => "critical",
        }
    }
}

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

impl std::str::FromStr for NotifyLevel {
    type Err = NotifyError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "info" => Ok(Self::Info),
            "warning" | "warn" => Ok(Self::Warning),
            "error" | "err" => Ok(Self::Error),
            "critical" | "crit" => Ok(Self::Critical),
            other => Err(NotifyError::MissingField(format!("未知通知级别: {other}"))),
        }
    }
}

// ============================================================================
// 通知消息(Builder 模式)
// ============================================================================

/// 通知消息 — 对齐 PHP `think\notify\Message`
///
/// 使用 Builder 模式构建通知内容,通过 [`Notifier::send`] 发送。
///
/// # PHP 对齐
///
/// ```php
/// // PHP think\facade\Notify
/// Notify::channel('slack')
///     ->title('部署完成')
///     ->content('服务已成功部署到生产环境')
///     ->level('info')
///     ->send();
/// ```
///
/// # Rust 用法
///
/// ```rust,ignore
/// use sz_rust_state_facade::notify::{Notification, MemoryNotifier, NotifyLevel};
///
/// let msg = Notification::new()
///     .channel("slack")
///     .title("部署完成")
///     .content("服务已成功部署到生产环境")
///     .level(NotifyLevel::Info);
///
/// let notifier = MemoryNotifier::new();
/// notifier.send(msg).unwrap();
/// ```
#[derive(Debug, Clone, Default)]
pub struct Notification {
    /// 通知渠道(如 `slack`、`sms`、`mail`)
    pub channel: String,
    /// 通知标题
    pub title: String,
    /// 通知正文
    pub content: String,
    /// 通知级别
    pub level: NotifyLevel,
    /// 附加元数据(JSON 值,供具体 Notifier 使用)
    pub metadata: serde_json::Value,
}

impl Notification {
    /// 创建空通知消息
    pub fn new() -> Self {
        Self::default()
    }

    /// 设置通知渠道
    pub fn channel(mut self, channel: impl Into<String>) -> Self {
        self.channel = channel.into();
        self
    }

    /// 设置通知标题
    pub fn title(mut self, title: impl Into<String>) -> Self {
        self.title = title.into();
        self
    }

    /// 设置通知正文
    pub fn content(mut self, content: impl Into<String>) -> Self {
        self.content = content.into();
        self
    }

    /// 设置通知级别
    pub fn level(mut self, level: NotifyLevel) -> Self {
        self.level = level;
        self
    }

    /// 设置附加元数据
    pub fn metadata(mut self, metadata: serde_json::Value) -> Self {
        self.metadata = metadata;
        self
    }

    /// 校验必填字段
    ///
    /// # 返回
    ///
    /// - 渠道为空 → [`NotifyError::MissingField`]("channel")
    /// - 标题为空 → [`NotifyError::MissingField`]("title")
    /// - 内容为空 → [`NotifyError::MissingField`]("content")
    pub fn validate(&self) -> Result<(), NotifyError> {
        if self.channel.is_empty() {
            return Err(NotifyError::MissingField("channel".into()));
        }
        if self.title.is_empty() {
            return Err(NotifyError::MissingField("title".into()));
        }
        if self.content.is_empty() {
            return Err(NotifyError::MissingField("content".into()));
        }
        Ok(())
    }
}

// ============================================================================
// Notifier trait
// ============================================================================

/// 通知发送器 trait — 对齐 PHP `think\notify\Notifier`
///
/// 抽象通知发送行为,业务方实现具体发送逻辑(Slack Webhook / 短信 API / 日志等)。
///
/// # PHP 对齐
///
/// ```php
/// // PHP think\notify\Notifier 接口
/// interface Notifier {
///     public function send(Message $message): bool;
/// }
/// ```
pub trait Notifier: Send + Sync {
    /// 发送通知
    ///
    /// # 参数
    ///
    /// - `notification`: 通知消息
    ///
    /// # 返回
    ///
    /// 成功返回 `Ok(())`,失败返回 [`NotifyError`]。
    fn send(&self, notification: Notification) -> Result<(), NotifyError>;
}

// ============================================================================
// MemoryNotifier(测试/开发用实现)
// ============================================================================

/// 内存通知发送器 — 用于测试和开发环境
///
/// 不实际发送通知,而是将通知暂存到内部 Vec,供测试断言使用。
///
/// # 线程安全
///
/// 通过 `Arc<Mutex<Vec<Notification>>>` 保护,支持并发写入。
#[derive(Debug, Clone, Default)]
pub struct MemoryNotifier {
    /// 已"发送"的通知列表
    sent: Arc<Mutex<Vec<Notification>>>,
}

impl MemoryNotifier {
    /// 创建新的内存通知发送器
    pub fn new() -> Self {
        Self::default()
    }

    /// 获取已发送通知数量
    pub fn count(&self) -> usize {
        self.sent.lock().len()
    }

    /// 获取所有已发送通知(快照)
    pub fn all(&self) -> Vec<Notification> {
        self.sent.lock().clone()
    }

    /// 获取最后发送的通知
    pub fn last(&self) -> Option<Notification> {
        self.sent.lock().last().cloned()
    }

    /// 清空已发送通知
    pub fn clear(&self) {
        self.sent.lock().clear();
    }
}

impl Notifier for MemoryNotifier {
    fn send(&self, notification: Notification) -> Result<(), NotifyError> {
        // 校验必要字段
        notification.validate()?;

        // 暂存到内存
        self.sent.lock().push(notification);
        Ok(())
    }
}

// ============================================================================
// HttpTransport trait(HTTP 传输抽象)
// ============================================================================

/// HTTP 传输 trait — 用于解耦 Notifier 与具体 HTTP 库
///
/// 业务方实现此 trait 注入 reqwest / hyper / etc.,即可让 [`SlackNotifier`] 等基于 HTTP 的
/// 通知器投入生产。
///
/// # 线程安全
///
/// 实现者必须保证 `Send + Sync`,因为 Notifier 通常作为单例在多线程下使用。
// R5 超时兜底说明:实现者必须在 HTTP 客户端中配置超时(建议 ≤ 10s),
// 防止 Slack Webhook 无响应时阻塞通知线程。见 trait 文档中的示例。
pub trait HttpTransport: Send + Sync {
    /// 发送 POST 请求,Content-Type: application/json
    ///
    /// # 参数
    ///
    /// - `url`: 目标 URL
    /// - `body`: 请求体(JSON 字符串)
    ///
    /// # 返回
    ///
    /// 成功返回 `Ok(())`,失败返回 [`NotifyError`]。
    fn post_json(&self, url: &str, body: &str) -> Result<(), NotifyError>;
}

// ============================================================================
// MemoryHttpTransport(测试/开发用 HTTP 传输实现)
// ============================================================================

/// 内存 HTTP 传输 — 用于测试和开发环境
///
/// 不实际发送 HTTP 请求,而是将请求暂存到内部 Vec,供测试断言使用。
#[derive(Debug, Default)]
pub struct MemoryHttpTransport {
    /// 已"发送"的 HTTP 请求列表(url, body)
    requests: Mutex<Vec<(String, String)>>,
}

impl MemoryHttpTransport {
    /// 创建新的内存 HTTP 传输
    pub fn new() -> Self {
        Self::default()
    }

    /// 获取已发送请求数量
    pub fn count(&self) -> usize {
        self.requests.lock().len()
    }

    /// 获取所有已发送请求(快照)
    pub fn all(&self) -> Vec<(String, String)> {
        self.requests.lock().clone()
    }

    /// 获取最后发送的请求
    pub fn last(&self) -> Option<(String, String)> {
        self.requests.lock().last().cloned()
    }

    /// 清空已发送请求
    pub fn clear(&self) {
        self.requests.lock().clear();
    }
}

impl HttpTransport for MemoryHttpTransport {
    fn post_json(&self, url: &str, body: &str) -> Result<(), NotifyError> {
        self.requests
            .lock()
            .push((url.to_string(), body.to_string()));
        Ok(())
    }
}

// ============================================================================
// Slack 配置
// ============================================================================

/// Slack Webhook 配置
///
/// 对齐 PHP `think\notify\driver\Slack` 的配置项。
#[derive(Debug, Clone)]
pub struct SlackConfig {
    /// Slack Webhook URL(必填,格式:`https://hooks.slack.com/services/...`)
    pub webhook_url: String,
    /// 目标频道(可选,对齐 PHP `channel` 配置;Webhook URL 已绑定频道时可为 None)
    pub channel: Option<String>,
    /// 发送者显示名(可选,对齐 PHP `username` 配置)
    pub username: Option<String>,
    /// 发送者图标 emoji(可选,对齐 PHP `icon_emoji` 配置,如 `:alarm_clock:`)
    pub icon_emoji: Option<String>,
}

impl SlackConfig {
    /// 创建 Slack 配置
    ///
    /// # 参数
    ///
    /// - `webhook_url`: Slack Webhook URL
    pub fn new(webhook_url: impl Into<String>) -> Self {
        Self {
            webhook_url: webhook_url.into(),
            channel: None,
            username: None,
            icon_emoji: None,
        }
    }

    /// 设置目标频道
    pub fn with_channel(mut self, channel: impl Into<String>) -> Self {
        self.channel = Some(channel.into());
        self
    }

    /// 设置发送者显示名
    pub fn with_username(mut self, username: impl Into<String>) -> Self {
        self.username = Some(username.into());
        self
    }

    /// 设置发送者图标 emoji
    pub fn with_icon_emoji(mut self, icon_emoji: impl Into<String>) -> Self {
        self.icon_emoji = Some(icon_emoji.into());
        self
    }
}

// ============================================================================
// Slack Webhook 请求体
// ============================================================================

/// Slack Webhook 请求体 — 对齐 Slack API 的 `chat.postMessage` 兼容格式
///
/// 参考:https://api.slack.com/messaging/webhooks
#[derive(Debug, Clone, serde::Serialize)]
struct SlackPayload {
    /// 通知文本(必填,作为消息预览和 fallback)
    text: String,
    /// 目标频道(可选)
    #[serde(skip_serializing_if = "Option::is_none")]
    channel: Option<String>,
    /// 发送者显示名(可选)
    #[serde(skip_serializing_if = "Option::is_none")]
    username: Option<String>,
    /// 发送者图标 emoji(可选)
    #[serde(skip_serializing_if = "Option::is_none")]
    icon_emoji: Option<String>,
    /// Slack Block Kit 附件(用于富文本展示,含颜色条)
    attachments: Vec<SlackAttachment>,
}

/// Slack 附件 — 用于在消息旁显示颜色条(按通知级别着色)
#[derive(Debug, Clone, serde::Serialize)]
struct SlackAttachment {
    /// 颜色条颜色(hex 颜色码,如 `#ff0000`)
    color: String,
    /// 附件标题
    title: String,
    /// 附件正文
    text: String,
    /// 时间戳(Unix 秒,用于 Slack 消息时间显示)
    ts: i64,
}

// ============================================================================
// SlackNotifier
// ============================================================================

/// Slack 通知发送器 — 基于 Slack Webhook API
///
/// 通过 [`HttpTransport`] trait 抽象 HTTP 发送,业务方注入具体 HTTP 客户端即可使用。
///
/// # 用法
///
/// ```rust,ignore
/// use sz_rust_state_facade::notify::{
///     SlackConfig, SlackNotifier, MemoryHttpTransport, Notification, NotifyLevel, Notifier,
/// };
///
/// let transport = std::sync::Arc::new(MemoryHttpTransport::new());
/// let config = SlackConfig::new("https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXX")
///     .with_channel("#alerts")
///     .with_username("SZ-Rust Bot")
///     .with_icon_emoji(":alarm_clock:");
/// let notifier = SlackNotifier::new(config, transport);
///
/// let msg = Notification::new()
///     .channel("slack")
///     .title("部署完成")
///     .content("服务已成功部署到生产环境")
///     .level(NotifyLevel::Info);
///
/// notifier.send(msg).unwrap();
/// ```
pub struct SlackNotifier {
    /// Slack 配置
    config: SlackConfig,
    /// HTTP 传输实现
    transport: Arc<dyn HttpTransport>,
}

impl SlackNotifier {
    /// 创建 Slack 通知发送器
    ///
    /// # 参数
    ///
    /// - `config`: Slack 配置
    /// - `transport`: HTTP 传输实现(业务方注入 reqwest / hyper / etc.)
    pub fn new(config: SlackConfig, transport: Arc<dyn HttpTransport>) -> Self {
        Self { config, transport }
    }

    /// 构造 Slack Webhook 请求体
    ///
    /// # 参数
    ///
    /// - `notification`: 通知消息
    ///
    /// # 返回
    ///
    /// 成功返回 JSON 字符串,失败返回 [`NotifyError`]。
    fn build_payload(&self, notification: &Notification) -> Result<String, NotifyError> {
        let ts = chrono::Utc::now().timestamp();
        let payload = SlackPayload {
            // Slack 要求 text 字段非空(作为消息预览和 fallback)
            text: format!(
                "[{}] {}{}",
                notification.level.as_str().to_uppercase(),
                notification.title,
                notification.content
            ),
            channel: self.config.channel.clone(),
            username: self.config.username.clone(),
            icon_emoji: self.config.icon_emoji.clone(),
            attachments: vec![SlackAttachment {
                color: notification.level.slack_color().to_string(),
                title: notification.title.clone(),
                text: notification.content.clone(),
                ts,
            }],
        };

        serde_json::to_string(&payload).map_err(|e| NotifyError::Serialize(e.to_string()))
    }
}

impl Notifier for SlackNotifier {
    fn send(&self, notification: Notification) -> Result<(), NotifyError> {
        // 1. 校验通知字段
        notification.validate()?;

        // 2. 校验 webhook_url 非空
        if self.config.webhook_url.is_empty() {
            return Err(NotifyError::MissingField("webhook_url".into()));
        }

        // 3. 构造 Slack Webhook 请求体
        let body = self.build_payload(&notification)?;

        // 4. 通过 HttpTransport 发送
        self.transport
            .post_json(&self.config.webhook_url, &body)
            .map_err(|e| NotifyError::HttpTransport(format!("Slack Webhook 发送失败: {e}")))?;

        Ok(())
    }
}

// ============================================================================
// 短信消息(SmsMessage + SmsNotifier + MemorySmsNotifier)
// ============================================================================

/// 短信消息 — 对齐 PHP `think\notify\SmsMessage`
///
/// 使用 Builder 模式构建短信内容,通过 [`SmsNotifier::send_sms`] 发送。
///
/// # PHP 对齐
///
/// ```php
/// // PHP think\notify\SmsMessage
/// $sms = (new SmsMessage())
///     ->setPhone('13800138000')
///     ->setTemplateId('123456')
///     ->setTemplateParams(['1234']);
/// ```
///
/// # Rust 用法
///
/// ```rust,ignore
/// use sz_rust_state_facade::notify::{SmsMessage, MemorySmsNotifier, SmsNotifier};
///
/// let msg = SmsMessage::new()
///     .phone("+8613800138000")
///     .template_id("123456")
///     .template_param("1234");
///
/// let notifier = MemorySmsNotifier::new();
/// notifier.send_sms(msg).unwrap();
/// ```
#[derive(Debug, Clone, Default)]
pub struct SmsMessage {
    /// 手机号(必填)
    pub phone: String,
    /// 短信模板 ID(必填,对齐腾讯云 TemplateId)
    pub template_id: String,
    /// 模板参数(按顺序匹配模板中的 {1}、{2}... 占位符)
    pub template_params: Vec<String>,
    /// 短信签名(可选,默认使用配置中的签名)
    pub sign_name: Option<String>,
    /// 附加元数据
    pub metadata: serde_json::Value,
}

impl SmsMessage {
    /// 创建空短信消息
    pub fn new() -> Self {
        Self::default()
    }

    /// 设置手机号
    pub fn phone(mut self, phone: impl Into<String>) -> Self {
        self.phone = phone.into();
        self
    }

    /// 设置短信模板 ID
    pub fn template_id(mut self, template_id: impl Into<String>) -> Self {
        self.template_id = template_id.into();
        self
    }

    /// 追加单个模板参数
    pub fn template_param(mut self, param: impl Into<String>) -> Self {
        self.template_params.push(param.into());
        self
    }

    /// 替换全部模板参数
    pub fn template_params(mut self, params: Vec<String>) -> Self {
        self.template_params = params;
        self
    }

    /// 设置短信签名
    pub fn sign_name(mut self, sign_name: impl Into<String>) -> Self {
        self.sign_name = Some(sign_name.into());
        self
    }

    /// 设置附加元数据
    pub fn metadata(mut self, metadata: serde_json::Value) -> Self {
        self.metadata = metadata;
        self
    }

    /// 校验必填字段
    ///
    /// # 返回
    ///
    /// - 手机号为空 → [`NotifyError::MissingField`]("phone")
    /// - 模板 ID 为空 → [`NotifyError::MissingField`]("template_id")
    pub fn validate(&self) -> Result<(), NotifyError> {
        if self.phone.is_empty() {
            return Err(NotifyError::MissingField("phone".into()));
        }
        if self.template_id.is_empty() {
            return Err(NotifyError::MissingField("template_id".into()));
        }
        Ok(())
    }
}

/// 短信通知发送器 trait — 对齐 PHP `think\notify\SmsNotifier`
///
/// 抽象短信发送行为,业务方实现具体发送逻辑(腾讯云 / 阿里云 / etc.)。
///
/// # PHP 对齐
///
/// ```php
/// // PHP think\notify\SmsNotifier 接口
/// interface SmsNotifier {
///     public function sendSms(SmsMessage $message): bool;
/// }
/// ```
pub trait SmsNotifier: Send + Sync {
    /// 发送短信
    ///
    /// # 参数
    ///
    /// - `message`: 短信消息
    ///
    /// # 返回
    ///
    /// 成功返回 `Ok(())`,失败返回 [`NotifyError`]。
    fn send_sms(&self, message: SmsMessage) -> Result<(), NotifyError>;
}

/// 内存短信通知发送器 — 用于测试和开发环境
///
/// 不实际发送短信,而是将消息暂存到内部 Vec,供测试断言使用。
///
/// # 线程安全
///
/// 通过 `Arc<Mutex<Vec<SmsMessage>>>` 保护,支持并发写入。
#[derive(Debug, Clone, Default)]
pub struct MemorySmsNotifier {
    /// 已"发送"的短信列表
    sent: Arc<Mutex<Vec<SmsMessage>>>,
}

impl MemorySmsNotifier {
    /// 创建新的内存短信通知发送器
    pub fn new() -> Self {
        Self::default()
    }

    /// 获取已发送短信数量
    pub fn count(&self) -> usize {
        self.sent.lock().len()
    }

    /// 获取所有已发送短信(快照)
    pub fn all(&self) -> Vec<SmsMessage> {
        self.sent.lock().clone()
    }

    /// 获取最后发送的短信
    pub fn last(&self) -> Option<SmsMessage> {
        self.sent.lock().last().cloned()
    }

    /// 清空已发送短信
    pub fn clear(&self) {
        self.sent.lock().clear();
    }
}

impl SmsNotifier for MemorySmsNotifier {
    fn send_sms(&self, message: SmsMessage) -> Result<(), NotifyError> {
        // 校验必要字段
        message.validate()?;
        // 暂存到内存
        self.sent.lock().push(message);
        Ok(())
    }
}

// ============================================================================
// 腾讯云短信配置
// ============================================================================

/// 腾讯云短信配置 — 对齐 `tencentcloud/tencentcloud-sdk-php` SmsClient
///
/// 对齐 PHP SDK `TencentCloud\Sms\V20210111\Models\SendSmsRequest` 的配置项。
///
/// # 安全
///
/// `secret_id` / `secret_key` 为敏感字段,Debug 输出已脱敏为 `"***REDACTED***"`,
/// 防止日志泄露密钥(铁律 7)。字段值仍可通过 `config.secret_id` 正常访问。
#[derive(Clone)]
pub struct TencentSmsConfig {
    /// SecretId(必填)
    pub secret_id: String,
    /// SecretKey(必填)
    pub secret_key: String,
    /// 短信 AppId(必填,如 `1400000000`)
    pub app_id: String,
    /// 默认短信签名(可选,如 `鲜视达科技`)
    pub default_sign_name: Option<String>,
    /// 地域(默认 `ap-guangzhou`)
    pub region: String,
    /// API 端点(默认 `sms.tencentcloudapi.com`)
    pub endpoint: String,
}

impl std::fmt::Debug for TencentSmsConfig {
    /// 自定义 Debug:`secret_id` / `secret_key` 脱敏为 `"***REDACTED***"`(铁律 7)
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("TencentSmsConfig")
            .field("secret_id", &"***REDACTED***")
            .field("secret_key", &"***REDACTED***")
            .field("app_id", &self.app_id)
            .field("default_sign_name", &self.default_sign_name)
            .field("region", &self.region)
            .field("endpoint", &self.endpoint)
            .finish()
    }
}

impl TencentSmsConfig {
    /// 创建腾讯云短信配置
    ///
    /// # 参数
    ///
    /// - `secret_id`: 腾讯云 SecretId
    /// - `secret_key`: 腾讯云 SecretKey
    /// - `app_id`: 短信 AppId
    pub fn new(
        secret_id: impl Into<String>,
        secret_key: impl Into<String>,
        app_id: impl Into<String>,
    ) -> Self {
        Self {
            secret_id: secret_id.into(),
            secret_key: secret_key.into(),
            app_id: app_id.into(),
            default_sign_name: None,
            region: "ap-guangzhou".to_string(),
            endpoint: "sms.tencentcloudapi.com".to_string(),
        }
    }

    /// 设置默认短信签名
    pub fn with_default_sign_name(mut self, sign_name: impl Into<String>) -> Self {
        self.default_sign_name = Some(sign_name.into());
        self
    }

    /// 设置地域
    pub fn with_region(mut self, region: impl Into<String>) -> Self {
        self.region = region.into();
        self
    }

    /// 设置 API 端点
    pub fn with_endpoint(mut self, endpoint: impl Into<String>) -> Self {
        self.endpoint = endpoint.into();
        self
    }
}

// ============================================================================
// 腾讯云短信请求体
// ============================================================================

/// 腾讯云短信请求体 — 对齐 `SendSms` action
///
/// 参考:https://cloud.tencent.com/document/product/382/55981
///
/// 注意:真实签名由业务方在实现 [`HttpTransport`] 时注入(如通过 header 携带
/// `Authorization`、`X-TC-Action`、`X-TC-Region` 等),此处仅构造 JSON body。
#[derive(Debug, Clone, serde::Serialize)]
struct TencentSmsPayload {
    /// 手机号集合(对齐腾讯云 `PhoneNumbers`)
    #[serde(rename = "PhoneNumbers")]
    phone_numbers: Vec<String>,
    /// 模板 ID(对齐腾讯云 `TemplateId`)
    #[serde(rename = "TemplateId")]
    template_id: String,
    /// 模板参数集合(对齐腾讯云 `TemplateParamSet`)
    #[serde(rename = "TemplateParamSet")]
    template_param_set: Vec<String>,
    /// 短信 SDK AppId(对齐腾讯云 `SmsSdkAppId`)
    #[serde(rename = "SmsSdkAppId")]
    sms_sdk_app_id: String,
    /// 短信签名(对齐腾讯云 `SignName`,可选)
    #[serde(rename = "SignName", skip_serializing_if = "Option::is_none")]
    sign_name: Option<String>,
}

// ============================================================================
// TencentSmsNotifier
// ============================================================================

/// 腾讯云短信通知发送器 — 基于腾讯云短信 API(`SendSms` action)
///
/// 通过 [`HttpTransport`] trait 抽象 HTTP 发送,业务方注入具体 HTTP 客户端即可使用。
/// 真实签名由业务方在实现 [`HttpTransport`] 时注入(如通过 header 携带
/// `Authorization`、`X-TC-Action` 等),本实现仅构造请求 JSON body。
///
/// # 用法
///
/// ```rust,ignore
/// use sz_rust_state_facade::notify::{
///     TencentSmsConfig, TencentSmsNotifier, MemoryHttpTransport, SmsMessage, SmsNotifier,
/// };
/// use std::sync::Arc;
///
/// let transport = Arc::new(MemoryHttpTransport::new());
/// let config = TencentSmsConfig::new("AKIDxxx", "SKxxx", "1400000000")
///     .with_default_sign_name("鲜视达科技");
/// let notifier = TencentSmsNotifier::new(config, transport);
///
/// let msg = SmsMessage::new()
///     .phone("+8613800138000")
///     .template_id("123456")
///     .template_param("1234");
///
/// notifier.send_sms(msg).unwrap();
/// ```
pub struct TencentSmsNotifier {
    /// 腾讯云短信配置
    config: TencentSmsConfig,
    /// HTTP 传输实现
    transport: Arc<dyn HttpTransport>,
}

impl TencentSmsNotifier {
    /// 创建腾讯云短信通知发送器
    ///
    /// # 参数
    ///
    /// - `config`: 腾讯云短信配置
    /// - `transport`: HTTP 传输实现(业务方注入 reqwest / hyper / etc.)
    pub fn new(config: TencentSmsConfig, transport: Arc<dyn HttpTransport>) -> Self {
        Self { config, transport }
    }

    /// 构造腾讯云短信请求体
    ///
    /// # 参数
    ///
    /// - `message`: 短信消息
    ///
    /// # 返回
    ///
    /// 成功返回 JSON 字符串,失败返回 [`NotifyError`]。
    ///
    /// # 说明
    ///
    /// 若 `message.sign_name` 为 `None`,则回退使用配置中的默认签名
    /// `config.default_sign_name`;两者均为 `None` 时,请求体中不包含 `SignName` 字段。
    fn build_payload(&self, message: &SmsMessage) -> Result<String, NotifyError> {
        let sign_name = message
            .sign_name
            .clone()
            .or_else(|| self.config.default_sign_name.clone());

        let payload = TencentSmsPayload {
            phone_numbers: vec![message.phone.clone()],
            template_id: message.template_id.clone(),
            template_param_set: message.template_params.clone(),
            sms_sdk_app_id: self.config.app_id.clone(),
            sign_name,
        };

        serde_json::to_string(&payload).map_err(|e| NotifyError::Serialize(e.to_string()))
    }
}

impl SmsNotifier for TencentSmsNotifier {
    fn send_sms(&self, message: SmsMessage) -> Result<(), NotifyError> {
        // 1. 校验短信消息字段
        message.validate()?;

        // 2. 校验腾讯云凭据
        if self.config.secret_id.is_empty() {
            return Err(NotifyError::MissingField("secret_id".into()));
        }
        if self.config.secret_key.is_empty() {
            return Err(NotifyError::MissingField("secret_key".into()));
        }
        if self.config.app_id.is_empty() {
            return Err(NotifyError::MissingField("app_id".into()));
        }

        // 3. 构造请求体
        let body = self.build_payload(&message)?;

        // 4. 构造请求 URL(对齐腾讯云 API 端点格式)
        let url = format!("https://{}/", self.config.endpoint);

        // 5. 通过 HttpTransport 发送
        self.transport
            .post_json(&url, &body)
            .map_err(|e| NotifyError::HttpTransport(format!("腾讯云短信发送失败: {e}")))?;

        Ok(())
    }
}

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

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

    // ------------------------------------------------------------------------
    // TencentSmsConfig Debug 脱敏测试(铁律 7)
    // ------------------------------------------------------------------------

    /// 测试 TencentSmsConfig 的 Debug 输出不含真实密钥(脱敏)
    #[test]
    fn test_tencent_sms_config_debug_redacted() {
        let config = TencentSmsConfig {
            secret_id: "AKIDxxx-test-secret-id".to_string(),
            secret_key: "SKxxx-test-secret-key".to_string(),
            app_id: "1400000000".to_string(),
            default_sign_name: Some("鲜视达科技".to_string()),
            region: "ap-guangzhou".to_string(),
            endpoint: "sms.tencentcloudapi.com".to_string(),
        };
        let debug_output = format!("{:?}", config);
        assert!(
            debug_output.contains("***REDACTED***"),
            "Debug 输出应含脱敏占位符"
        );
        assert!(
            !debug_output.contains("AKIDxxx-test-secret-id"),
            "Debug 输出不应含真实 secret_id"
        );
        assert!(
            !debug_output.contains("SKxxx-test-secret-key"),
            "Debug 输出不应含真实 secret_key"
        );
    }

    /// 测试脱敏不影响字段访问与 Clone 行为(铁律 7 不破坏功能)
    #[test]
    fn test_tencent_sms_config_field_access_and_clone() {
        let config = TencentSmsConfig {
            secret_id: "AKIDxxx".to_string(),
            secret_key: "SKxxx".to_string(),
            app_id: "1400000000".to_string(),
            default_sign_name: None,
            region: "ap-guangzhou".to_string(),
            endpoint: "sms.tencentcloudapi.com".to_string(),
        };
        // 字段值仍为真实密钥,可正常用于 API 调用
        assert_eq!(config.secret_id, "AKIDxxx");
        assert_eq!(config.secret_key, "SKxxx");
        assert!(!config.secret_id.is_empty());
        // Clone 行为不变
        let cloned = config.clone();
        assert_eq!(cloned.secret_id, "AKIDxxx");
        assert_eq!(cloned.secret_key, "SKxxx");
    }

    // ------------------------------------------------------------------------
    // NotifyLevel 测试
    // ------------------------------------------------------------------------

    /// 测试 NotifyLevel 默认值为 Info
    #[test]
    fn test_notify_level_default() {
        let level = NotifyLevel::default();
        assert_eq!(level, NotifyLevel::Info);
    }

    /// 测试 NotifyLevel::slack_color 返回正确的颜色码
    #[test]
    fn test_notify_level_slack_color() {
        assert_eq!(NotifyLevel::Info.slack_color(), "#36a64f");
        assert_eq!(NotifyLevel::Warning.slack_color(), "#ffcc00");
        assert_eq!(NotifyLevel::Error.slack_color(), "#ff0000");
        assert_eq!(NotifyLevel::Critical.slack_color(), "#b22222");
    }

    /// 测试 NotifyLevel::as_str 返回正确的字符串标识
    #[test]
    fn test_notify_level_as_str() {
        assert_eq!(NotifyLevel::Info.as_str(), "info");
        assert_eq!(NotifyLevel::Warning.as_str(), "warning");
        assert_eq!(NotifyLevel::Error.as_str(), "error");
        assert_eq!(NotifyLevel::Critical.as_str(), "critical");
    }

    /// 测试 NotifyLevel::Display 实现
    #[test]
    fn test_notify_level_display() {
        assert_eq!(format!("{}", NotifyLevel::Info), "info");
        assert_eq!(format!("{}", NotifyLevel::Warning), "warning");
        assert_eq!(format!("{}", NotifyLevel::Error), "error");
        assert_eq!(format!("{}", NotifyLevel::Critical), "critical");
    }

    /// 测试 NotifyLevel::FromStr 实现(含别名和大小写不敏感)
    #[test]
    fn test_notify_level_from_str() {
        // 标准名称
        assert_eq!("info".parse::<NotifyLevel>().unwrap(), NotifyLevel::Info);
        assert_eq!(
            "warning".parse::<NotifyLevel>().unwrap(),
            NotifyLevel::Warning
        );
        assert_eq!("error".parse::<NotifyLevel>().unwrap(), NotifyLevel::Error);
        assert_eq!(
            "critical".parse::<NotifyLevel>().unwrap(),
            NotifyLevel::Critical
        );

        // 别名
        assert_eq!("warn".parse::<NotifyLevel>().unwrap(), NotifyLevel::Warning);
        assert_eq!("err".parse::<NotifyLevel>().unwrap(), NotifyLevel::Error);
        assert_eq!(
            "crit".parse::<NotifyLevel>().unwrap(),
            NotifyLevel::Critical
        );

        // 大小写不敏感
        assert_eq!("INFO".parse::<NotifyLevel>().unwrap(), NotifyLevel::Info);
        assert_eq!(
            "Critical".parse::<NotifyLevel>().unwrap(),
            NotifyLevel::Critical
        );

        // 未知级别
        assert!("unknown".parse::<NotifyLevel>().is_err());
    }

    // ------------------------------------------------------------------------
    // Notification 测试
    // ------------------------------------------------------------------------

    /// 测试 Notification builder 模式
    #[test]
    fn test_notification_builder() {
        let notification = Notification::new()
            .channel("slack")
            .title("部署完成")
            .content("服务已成功部署到生产环境")
            .level(NotifyLevel::Info)
            .metadata(serde_json::json!({"env": "prod"}));

        assert_eq!(notification.channel, "slack");
        assert_eq!(notification.title, "部署完成");
        assert_eq!(notification.content, "服务已成功部署到生产环境");
        assert_eq!(notification.level, NotifyLevel::Info);
        assert_eq!(notification.metadata["env"], "prod");
    }

    /// 测试 Notification 默认值
    #[test]
    fn test_notification_default() {
        let notification = Notification::default();
        assert!(notification.channel.is_empty());
        assert!(notification.title.is_empty());
        assert!(notification.content.is_empty());
        assert_eq!(notification.level, NotifyLevel::Info);
        assert!(notification.metadata.is_null());
    }

    /// 测试 Notification::validate 校验必填字段
    #[test]
    fn test_notification_validate_ok() {
        let notification = Notification::new()
            .channel("slack")
            .title("标题")
            .content("内容");
        assert!(notification.validate().is_ok());
    }

    /// 测试 Notification::validate 缺少渠道
    #[test]
    fn test_notification_validate_missing_channel() {
        let notification = Notification::new().title("标题").content("内容");
        let err = notification.validate().unwrap_err();
        match err {
            NotifyError::MissingField(field) => assert_eq!(field, "channel"),
            other => panic!("期望 MissingField, 实际 {other:?}"),
        }
    }

    /// 测试 Notification::validate 缺少标题
    #[test]
    fn test_notification_validate_missing_title() {
        let notification = Notification::new().channel("slack").content("内容");
        let err = notification.validate().unwrap_err();
        match err {
            NotifyError::MissingField(field) => assert_eq!(field, "title"),
            other => panic!("期望 MissingField, 实际 {other:?}"),
        }
    }

    /// 测试 Notification::validate 缺少内容
    #[test]
    fn test_notification_validate_missing_content() {
        let notification = Notification::new().channel("slack").title("标题");
        let err = notification.validate().unwrap_err();
        match err {
            NotifyError::MissingField(field) => assert_eq!(field, "content"),
            other => panic!("期望 MissingField, 实际 {other:?}"),
        }
    }

    // ------------------------------------------------------------------------
    // MemoryNotifier 测试
    // ------------------------------------------------------------------------

    /// 测试 MemoryNotifier 发送通知
    #[test]
    fn test_memory_notifier_send() {
        let notifier = MemoryNotifier::new();
        let notification = Notification::new()
            .channel("slack")
            .title("标题")
            .content("内容")
            .level(NotifyLevel::Warning);

        notifier.send(notification).unwrap();
        assert_eq!(notifier.count(), 1);

        let last = notifier.last().unwrap();
        assert_eq!(last.channel, "slack");
        assert_eq!(last.title, "标题");
        assert_eq!(last.content, "内容");
        assert_eq!(last.level, NotifyLevel::Warning);
    }

    /// 测试 MemoryNotifier 发送多封通知
    #[test]
    fn test_memory_notifier_send_multiple() {
        let notifier = MemoryNotifier::new();
        for i in 0..5 {
            notifier
                .send(
                    Notification::new()
                        .channel("slack")
                        .title(format!("标题{i}"))
                        .content("内容"),
                )
                .unwrap();
        }
        assert_eq!(notifier.count(), 5);

        let all = notifier.all();
        assert_eq!(all[0].title, "标题0");
        assert_eq!(all[4].title, "标题4");
    }

    /// 测试 MemoryNotifier 发送无效通知返回错误
    #[test]
    fn test_memory_notifier_send_invalid() {
        let notifier = MemoryNotifier::new();
        let notification = Notification::new().title("标题").content("内容");
        // 缺少 channel
        assert!(notifier.send(notification).is_err());
        assert_eq!(notifier.count(), 0);
    }

    /// 测试 MemoryNotifier clear
    #[test]
    fn test_memory_notifier_clear() {
        let notifier = MemoryNotifier::new();
        notifier
            .send(
                Notification::new()
                    .channel("slack")
                    .title("标题")
                    .content("内容"),
            )
            .unwrap();
        assert_eq!(notifier.count(), 1);

        notifier.clear();
        assert_eq!(notifier.count(), 0);
        assert!(notifier.last().is_none());
    }

    // ------------------------------------------------------------------------
    // MemoryHttpTransport 测试
    // ------------------------------------------------------------------------

    /// 测试 MemoryHttpTransport 记录请求
    #[test]
    fn test_memory_http_transport_post_json() {
        let transport = MemoryHttpTransport::new();
        transport
            .post_json("https://hooks.slack.com/services/xxx", r#"{"text":"hi"}"#)
            .unwrap();

        assert_eq!(transport.count(), 1);
        let (url, body) = transport.last().unwrap();
        assert_eq!(url, "https://hooks.slack.com/services/xxx");
        assert_eq!(body, r#"{"text":"hi"}"#);
    }

    /// 测试 MemoryHttpTransport clear
    #[test]
    fn test_memory_http_transport_clear() {
        let transport = MemoryHttpTransport::new();
        transport.post_json("url", "body").unwrap();
        assert_eq!(transport.count(), 1);

        transport.clear();
        assert_eq!(transport.count(), 0);
    }

    // ------------------------------------------------------------------------
    // SlackConfig 测试
    // ------------------------------------------------------------------------

    /// 测试 SlackConfig builder 模式
    #[test]
    fn test_slack_config_builder() {
        let config = SlackConfig::new("https://hooks.slack.com/services/T/B/X")
            .with_channel("#alerts")
            .with_username("SZ-Rust Bot")
            .with_icon_emoji(":alarm_clock:");

        assert_eq!(config.webhook_url, "https://hooks.slack.com/services/T/B/X");
        assert_eq!(config.channel.as_deref(), Some("#alerts"));
        assert_eq!(config.username.as_deref(), Some("SZ-Rust Bot"));
        assert_eq!(config.icon_emoji.as_deref(), Some(":alarm_clock:"));
    }

    /// 测试 SlackConfig 默认值(仅 webhook_url)
    #[test]
    fn test_slack_config_minimal() {
        let config = SlackConfig::new("https://hooks.slack.com/services/T/B/X");
        assert_eq!(config.webhook_url, "https://hooks.slack.com/services/T/B/X");
        assert!(config.channel.is_none());
        assert!(config.username.is_none());
        assert!(config.icon_emoji.is_none());
    }

    // ------------------------------------------------------------------------
    // SlackNotifier 测试
    // ------------------------------------------------------------------------

    /// 测试 SlackNotifier 发送通知(通过 MemoryHttpTransport 验证请求体)
    #[test]
    fn test_slack_notifier_send() {
        let transport = Arc::new(MemoryHttpTransport::new());
        let config = SlackConfig::new("https://hooks.slack.com/services/T/B/X")
            .with_channel("#alerts")
            .with_username("SZ-Rust Bot")
            .with_icon_emoji(":alarm_clock:");
        let notifier = SlackNotifier::new(config, transport.clone());

        let notification = Notification::new()
            .channel("slack")
            .title("部署完成")
            .content("服务已成功部署到生产环境")
            .level(NotifyLevel::Info);

        notifier.send(notification).unwrap();

        // 验证 HTTP 请求
        assert_eq!(transport.count(), 1);
        let (url, body) = transport.last().unwrap();
        assert_eq!(url, "https://hooks.slack.com/services/T/B/X");

        // 解析 body 验证结构
        let payload: serde_json::Value = serde_json::from_str(&body).unwrap();
        assert!(payload["text"].as_str().unwrap().contains("部署完成"));
        assert!(payload["text"]
            .as_str()
            .unwrap()
            .contains("服务已成功部署到生产环境"));
        assert!(payload["text"].as_str().unwrap().contains("[INFO]"));
        assert_eq!(payload["channel"], "#alerts");
        assert_eq!(payload["username"], "SZ-Rust Bot");
        assert_eq!(payload["icon_emoji"], ":alarm_clock:");

        // 验证 attachments
        let attachments = payload["attachments"].as_array().unwrap();
        assert_eq!(attachments.len(), 1);
        assert_eq!(attachments[0]["color"], "#36a64f"); // Info level color
        assert_eq!(attachments[0]["title"], "部署完成");
        assert_eq!(attachments[0]["text"], "服务已成功部署到生产环境");
        assert!(attachments[0]["ts"].as_i64().is_some());
    }

    /// 测试 SlackNotifier 不同级别使用不同颜色
    #[test]
    fn test_slack_notifier_level_colors() {
        let transport = Arc::new(MemoryHttpTransport::new());
        let config = SlackConfig::new("https://hooks.slack.com/services/T/B/X");
        let notifier = SlackNotifier::new(config, transport.clone());

        // Warning
        notifier
            .send(
                Notification::new()
                    .channel("slack")
                    .title("w")
                    .content("c")
                    .level(NotifyLevel::Warning),
            )
            .unwrap();
        let (_, body) = transport.last().unwrap();
        let payload: serde_json::Value = serde_json::from_str(&body).unwrap();
        assert_eq!(payload["attachments"][0]["color"], "#ffcc00");

        // Error
        notifier
            .send(
                Notification::new()
                    .channel("slack")
                    .title("w")
                    .content("c")
                    .level(NotifyLevel::Error),
            )
            .unwrap();
        let (_, body) = transport.last().unwrap();
        let payload: serde_json::Value = serde_json::from_str(&body).unwrap();
        assert_eq!(payload["attachments"][0]["color"], "#ff0000");

        // Critical
        notifier
            .send(
                Notification::new()
                    .channel("slack")
                    .title("w")
                    .content("c")
                    .level(NotifyLevel::Critical),
            )
            .unwrap();
        let (_, body) = transport.last().unwrap();
        let payload: serde_json::Value = serde_json::from_str(&body).unwrap();
        assert_eq!(payload["attachments"][0]["color"], "#b22222");

        assert_eq!(transport.count(), 3);
    }

    /// 测试 SlackNotifier 缺少 channel 字段返回错误
    #[test]
    fn test_slack_notifier_missing_channel() {
        let transport = Arc::new(MemoryHttpTransport::new());
        let config = SlackConfig::new("https://hooks.slack.com/services/T/B/X");
        let notifier = SlackNotifier::new(config, transport.clone());

        let notification = Notification::new().title("标题").content("内容");
        let err = notifier.send(notification).unwrap_err();
        match err {
            NotifyError::MissingField(field) => assert_eq!(field, "channel"),
            other => panic!("期望 MissingField, 实际 {other:?}"),
        }

        // 不应发送任何 HTTP 请求
        assert_eq!(transport.count(), 0);
    }

    /// 测试 SlackNotifier 缺少 webhook_url 返回错误
    #[test]
    fn test_slack_notifier_missing_webhook_url() {
        let transport = Arc::new(MemoryHttpTransport::new());
        let config = SlackConfig::new(""); // 空 webhook_url
        let notifier = SlackNotifier::new(config, transport.clone());

        let notification = Notification::new()
            .channel("slack")
            .title("标题")
            .content("内容");
        let err = notifier.send(notification).unwrap_err();
        match err {
            NotifyError::MissingField(field) => assert_eq!(field, "webhook_url"),
            other => panic!("期望 MissingField, 实际 {other:?}"),
        }

        assert_eq!(transport.count(), 0);
    }

    /// 测试 SlackNotifier 在 HttpTransport 失败时返回错误
    #[test]
    fn test_slack_notifier_http_failure() {
        // 自定义失败 HttpTransport
        struct FailingTransport;
        impl HttpTransport for FailingTransport {
            fn post_json(&self, _url: &str, _body: &str) -> Result<(), NotifyError> {
                Err(NotifyError::HttpTransport("connection refused".to_string()))
            }
        }

        let transport = Arc::new(FailingTransport);
        let config = SlackConfig::new("https://hooks.slack.com/services/T/B/X");
        let notifier = SlackNotifier::new(config, transport);

        let notification = Notification::new()
            .channel("slack")
            .title("标题")
            .content("内容");
        let err = notifier.send(notification).unwrap_err();
        match err {
            NotifyError::HttpTransport(msg) => assert!(msg.contains("connection refused")),
            other => panic!("期望 HttpTransport, 实际 {other:?}"),
        }
    }

    /// 测试 SlackNotifier build_payload 序列化结果正确
    #[test]
    fn test_slack_notifier_build_payload() {
        let transport: Arc<dyn HttpTransport> = Arc::new(MemoryHttpTransport::new());
        let config = SlackConfig::new("https://hooks.slack.com/services/T/B/X")
            .with_channel("#alerts")
            .with_username("Bot")
            .with_icon_emoji(":bell:");
        let notifier = SlackNotifier::new(config, transport.clone());

        let notification = Notification::new()
            .channel("slack")
            .title("Test Title")
            .content("Test Content")
            .level(NotifyLevel::Error);

        let payload_json = notifier.build_payload(&notification).unwrap();
        let payload: serde_json::Value = serde_json::from_str(&payload_json).unwrap();

        // 验证 text 字段格式
        assert_eq!(
            payload["text"].as_str().unwrap(),
            "[ERROR] Test Title — Test Content"
        );

        // 验证可选字段
        assert_eq!(payload["channel"], "#alerts");
        assert_eq!(payload["username"], "Bot");
        assert_eq!(payload["icon_emoji"], ":bell:");

        // 验证 attachments
        let attachments = payload["attachments"].as_array().unwrap();
        assert_eq!(attachments.len(), 1);
        assert_eq!(attachments[0]["color"], "#ff0000");
        assert_eq!(attachments[0]["title"], "Test Title");
        assert_eq!(attachments[0]["text"], "Test Content");
        assert!(attachments[0]["ts"].as_i64().is_some());
    }

    /// 测试 SlackNotifier 多次发送计数
    #[test]
    fn test_slack_notifier_send_multiple() {
        let transport = Arc::new(MemoryHttpTransport::new());
        let config = SlackConfig::new("https://hooks.slack.com/services/T/B/X");
        let notifier = SlackNotifier::new(config, transport.clone());

        for i in 0..3 {
            notifier
                .send(
                    Notification::new()
                        .channel("slack")
                        .title(format!("Title {i}"))
                        .content("content"),
                )
                .unwrap();
        }
        assert_eq!(transport.count(), 3);
    }

    /// 测试 SlackNotifier 使用最小配置(仅 webhook_url)
    #[test]
    fn test_slack_notifier_minimal_config() {
        let transport = Arc::new(MemoryHttpTransport::new());
        let config = SlackConfig::new("https://hooks.slack.com/services/T/B/X");
        let notifier = SlackNotifier::new(config, transport.clone());

        notifier
            .send(
                Notification::new()
                    .channel("slack")
                    .title("Title")
                    .content("Content"),
            )
            .unwrap();

        let (_, body) = transport.last().unwrap();
        let payload: serde_json::Value = serde_json::from_str(&body).unwrap();

        // 可选字段不应出现在序列化结果中(skip_serializing_if)
        assert!(payload.get("channel").is_none());
        assert!(payload.get("username").is_none());
        assert!(payload.get("icon_emoji").is_none());
    }

    /// 测试 SlackNotifier metadata 字段不影响发送
    #[test]
    fn test_slack_notifier_with_metadata() {
        let transport = Arc::new(MemoryHttpTransport::new());
        let config = SlackConfig::new("https://hooks.slack.com/services/T/B/X");
        let notifier = SlackNotifier::new(config, transport.clone());

        notifier
            .send(
                Notification::new()
                    .channel("slack")
                    .title("Title")
                    .content("Content")
                    .metadata(serde_json::json!({"env": "prod", "version": "1.0.0"})),
            )
            .unwrap();

        assert_eq!(transport.count(), 1);
    }

    // ------------------------------------------------------------------------
    // SmsMessage 测试
    // ------------------------------------------------------------------------

    /// 测试 SmsMessage builder 模式
    #[test]
    fn test_sms_message_builder() {
        let msg = SmsMessage::new()
            .phone("+8613800138000")
            .template_id("123456")
            .template_param("1234")
            .template_param("5")
            .sign_name("鲜视达科技")
            .metadata(serde_json::json!({"scene": "login"}));

        assert_eq!(msg.phone, "+8613800138000");
        assert_eq!(msg.template_id, "123456");
        assert_eq!(msg.template_params, vec!["1234", "5"]);
        assert_eq!(msg.sign_name.as_deref(), Some("鲜视达科技"));
        assert_eq!(msg.metadata["scene"], "login");
    }

    /// 测试 SmsMessage::validate 成功
    #[test]
    fn test_sms_message_validate_ok() {
        let msg = SmsMessage::new()
            .phone("+8613800138000")
            .template_id("123456");
        assert!(msg.validate().is_ok());
    }

    /// 测试 SmsMessage::validate 缺少手机号
    #[test]
    fn test_sms_message_validate_missing_phone() {
        let msg = SmsMessage::new().template_id("123456");
        let err = msg.validate().unwrap_err();
        match err {
            NotifyError::MissingField(field) => assert_eq!(field, "phone"),
            other => panic!("期望 MissingField, 实际 {other:?}"),
        }
    }

    /// 测试 SmsMessage::validate 缺少模板 ID
    #[test]
    fn test_sms_message_validate_missing_template_id() {
        let msg = SmsMessage::new().phone("+8613800138000");
        let err = msg.validate().unwrap_err();
        match err {
            NotifyError::MissingField(field) => assert_eq!(field, "template_id"),
            other => panic!("期望 MissingField, 实际 {other:?}"),
        }
    }

    // ------------------------------------------------------------------------
    // MemorySmsNotifier 测试
    // ------------------------------------------------------------------------

    /// 测试 MemorySmsNotifier 发送短信
    #[test]
    fn test_memory_sms_notifier_send() {
        let notifier = MemorySmsNotifier::new();
        let msg = SmsMessage::new()
            .phone("+8613800138000")
            .template_id("123456")
            .template_param("1234");

        notifier.send_sms(msg).unwrap();
        assert_eq!(notifier.count(), 1);

        let last = notifier.last().unwrap();
        assert_eq!(last.phone, "+8613800138000");
        assert_eq!(last.template_id, "123456");
        assert_eq!(last.template_params, vec!["1234"]);
    }

    /// 测试 MemorySmsNotifier 发送多条短信
    #[test]
    fn test_memory_sms_notifier_send_multiple() {
        let notifier = MemorySmsNotifier::new();
        for i in 0..5 {
            notifier
                .send_sms(
                    SmsMessage::new()
                        .phone(format!("+861380013{i:04}"))
                        .template_id("123456"),
                )
                .unwrap();
        }
        assert_eq!(notifier.count(), 5);

        let all = notifier.all();
        assert_eq!(all[0].phone, "+8613800130000");
        assert_eq!(all[4].phone, "+8613800130004");
    }

    /// 测试 MemorySmsNotifier 发送无效短信返回错误
    #[test]
    fn test_memory_sms_notifier_send_invalid() {
        let notifier = MemorySmsNotifier::new();
        let msg = SmsMessage::new().template_id("123456");
        // 缺少 phone
        assert!(notifier.send_sms(msg).is_err());
        assert_eq!(notifier.count(), 0);
    }

    /// 测试 MemorySmsNotifier clear
    #[test]
    fn test_memory_sms_notifier_clear() {
        let notifier = MemorySmsNotifier::new();
        notifier
            .send_sms(
                SmsMessage::new()
                    .phone("+8613800138000")
                    .template_id("123456"),
            )
            .unwrap();
        assert_eq!(notifier.count(), 1);

        notifier.clear();
        assert_eq!(notifier.count(), 0);
        assert!(notifier.last().is_none());
    }

    // ------------------------------------------------------------------------
    // TencentSmsConfig 测试
    // ------------------------------------------------------------------------

    /// 测试 TencentSmsConfig builder 模式
    #[test]
    fn test_tencent_sms_config_builder() {
        let config = TencentSmsConfig::new("AKIDxxx", "SKxxx", "1400000000")
            .with_default_sign_name("鲜视达科技")
            .with_region("ap-beijing")
            .with_endpoint("sms.tencentcloudapi.com");

        assert_eq!(config.secret_id, "AKIDxxx");
        assert_eq!(config.secret_key, "SKxxx");
        assert_eq!(config.app_id, "1400000000");
        assert_eq!(config.default_sign_name.as_deref(), Some("鲜视达科技"));
        assert_eq!(config.region, "ap-beijing");
        assert_eq!(config.endpoint, "sms.tencentcloudapi.com");
    }

    /// 测试 TencentSmsConfig 默认值(仅必填项)
    #[test]
    fn test_tencent_sms_config_minimal() {
        let config = TencentSmsConfig::new("AKIDxxx", "SKxxx", "1400000000");
        assert_eq!(config.secret_id, "AKIDxxx");
        assert_eq!(config.secret_key, "SKxxx");
        assert_eq!(config.app_id, "1400000000");
        assert!(config.default_sign_name.is_none());
        assert_eq!(config.region, "ap-guangzhou");
        assert_eq!(config.endpoint, "sms.tencentcloudapi.com");
    }

    // ------------------------------------------------------------------------
    // TencentSmsNotifier 测试
    // ------------------------------------------------------------------------

    /// 测试 TencentSmsNotifier 发送短信(通过 MemoryHttpTransport 验证请求体)
    #[test]
    fn test_tencent_sms_notifier_send() {
        let transport = Arc::new(MemoryHttpTransport::new());
        let config = TencentSmsConfig::new("AKIDxxx", "SKxxx", "1400000000")
            .with_default_sign_name("鲜视达科技");
        let notifier = TencentSmsNotifier::new(config, transport.clone());

        let msg = SmsMessage::new()
            .phone("+8613800138000")
            .template_id("123456")
            .template_param("1234")
            .template_param("5");

        notifier.send_sms(msg).unwrap();

        // 验证 HTTP 请求
        assert_eq!(transport.count(), 1);
        let (url, body) = transport.last().unwrap();
        assert_eq!(url, "https://sms.tencentcloudapi.com/");

        // 解析 body 验证结构
        let payload: serde_json::Value = serde_json::from_str(&body).unwrap();
        assert_eq!(payload["PhoneNumbers"][0], "+8613800138000");
        assert_eq!(payload["TemplateId"], "123456");
        assert_eq!(payload["TemplateParamSet"][0], "1234");
        assert_eq!(payload["TemplateParamSet"][1], "5");
        assert_eq!(payload["SmsSdkAppId"], "1400000000");
        assert_eq!(payload["SignName"], "鲜视达科技");
    }

    /// 测试 TencentSmsNotifier 缺少手机号返回错误
    #[test]
    fn test_tencent_sms_notifier_missing_phone() {
        let transport = Arc::new(MemoryHttpTransport::new());
        let config = TencentSmsConfig::new("AKIDxxx", "SKxxx", "1400000000");
        let notifier = TencentSmsNotifier::new(config, transport.clone());

        let msg = SmsMessage::new().template_id("123456");
        let err = notifier.send_sms(msg).unwrap_err();
        match err {
            NotifyError::MissingField(field) => assert_eq!(field, "phone"),
            other => panic!("期望 MissingField, 实际 {other:?}"),
        }

        // 不应发送任何 HTTP 请求
        assert_eq!(transport.count(), 0);
    }

    /// 测试 TencentSmsNotifier 缺少凭据返回错误
    #[test]
    fn test_tencent_sms_notifier_missing_credentials() {
        let transport = Arc::new(MemoryHttpTransport::new());
        // 空 secret_id
        let config = TencentSmsConfig::new("", "SKxxx", "1400000000");
        let notifier = TencentSmsNotifier::new(config, transport.clone());

        let msg = SmsMessage::new()
            .phone("+8613800138000")
            .template_id("123456");
        let err = notifier.send_sms(msg).unwrap_err();
        match err {
            NotifyError::MissingField(field) => assert_eq!(field, "secret_id"),
            other => panic!("期望 MissingField, 实际 {other:?}"),
        }

        // 不应发送任何 HTTP 请求
        assert_eq!(transport.count(), 0);

        // 验证空 secret_key
        let config2 = TencentSmsConfig::new("AKIDxxx", "", "1400000000");
        let notifier2 = TencentSmsNotifier::new(config2, transport.clone());
        let msg2 = SmsMessage::new()
            .phone("+8613800138000")
            .template_id("123456");
        let err2 = notifier2.send_sms(msg2).unwrap_err();
        match err2 {
            NotifyError::MissingField(field) => assert_eq!(field, "secret_key"),
            other => panic!("期望 MissingField, 实际 {other:?}"),
        }

        // 验证空 app_id
        let config3 = TencentSmsConfig::new("AKIDxxx", "SKxxx", "");
        let notifier3 = TencentSmsNotifier::new(config3, transport.clone());
        let msg3 = SmsMessage::new()
            .phone("+8613800138000")
            .template_id("123456");
        let err3 = notifier3.send_sms(msg3).unwrap_err();
        match err3 {
            NotifyError::MissingField(field) => assert_eq!(field, "app_id"),
            other => panic!("期望 MissingField, 实际 {other:?}"),
        }

        // 全程不应发送任何 HTTP 请求
        assert_eq!(transport.count(), 0);
    }

    /// 测试 TencentSmsNotifier 使用默认签名
    #[test]
    fn test_tencent_sms_notifier_uses_default_sign_name() {
        let transport = Arc::new(MemoryHttpTransport::new());
        let config = TencentSmsConfig::new("AKIDxxx", "SKxxx", "1400000000")
            .with_default_sign_name("鲜视达科技");
        let notifier = TencentSmsNotifier::new(config, transport.clone());

        // 消息未设置 sign_name,应使用 config 的 default_sign_name
        let msg = SmsMessage::new()
            .phone("+8613800138000")
            .template_id("123456");

        notifier.send_sms(msg).unwrap();

        let (_, body) = transport.last().unwrap();
        let payload: serde_json::Value = serde_json::from_str(&body).unwrap();
        assert_eq!(payload["SignName"], "鲜视达科技");

        // 验证消息级 sign_name 优先于 config 的 default_sign_name
        let msg2 = SmsMessage::new()
            .phone("+8613800138000")
            .template_id("123456")
            .sign_name("覆盖签名");
        notifier.send_sms(msg2).unwrap();

        let (_, body2) = transport.last().unwrap();
        let payload2: serde_json::Value = serde_json::from_str(&body2).unwrap();
        assert_eq!(payload2["SignName"], "覆盖签名");

        assert_eq!(transport.count(), 2);
    }

    /// 测试 TencentSmsNotifier 无签名时请求体不包含 SignName 字段
    #[test]
    fn test_tencent_sms_notifier_no_sign_name() {
        let transport = Arc::new(MemoryHttpTransport::new());
        let config = TencentSmsConfig::new("AKIDxxx", "SKxxx", "1400000000");
        let notifier = TencentSmsNotifier::new(config, transport.clone());

        let msg = SmsMessage::new()
            .phone("+8613800138000")
            .template_id("123456");

        notifier.send_sms(msg).unwrap();

        let (_, body) = transport.last().unwrap();
        let payload: serde_json::Value = serde_json::from_str(&body).unwrap();
        // 无签名时 SignName 字段不应出现在请求体中
        assert!(payload.get("SignName").is_none());
    }
}