garrison 0.7.2

Garrison - 面向 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
//! Copyright (c) 2026 Kirky.X. All rights reserved.
//! See LICENSE for full license text.

//! AbacEngine — Cedar 策略求值器实现。
//!
//! 基于 `cedar-policy` crate,提供 principal-action-resource 三元组策略求值。
//! 策略集使用 `Arc<RwLock<PolicySet>>` 支持读写分离热加载。

use crate::abac::EntityLoader;
use crate::core::permission::{Decision, DecisionReason};
use crate::error::{GarrisonError, GarrisonResult};
use cedar_policy::{Authorizer, Context, EntityUid, Policy, PolicyId, PolicySet, Request, Schema};
use oxcache::traits::CacheKey;
use oxcache::Cache;
use std::collections::HashMap;
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::RwLock;

/// ABAC 决策缓存 key:(principal, action, resource) 三元组。
///
/// # 设计权衡
///
/// `context_json` 不参与缓存 key。原因:
/// - 任务规格 T016 明确 `key = (principal, action, resource)`
/// - ABAC 策略热加载时通过 `clear()` 清空缓存,保证策略变更后决策一致
/// - 调用方若需 context 敏感的求值,应使用 `evaluate_with_temp_policy`(不走缓存)
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct DecisionKey(String, String, String);

impl CacheKey for DecisionKey {
    fn to_key_string(&self) -> String {
        // 使用 \x1F (ASCII Unit Separator) 作为字段分隔符,避免与 Cedar EntityUid 中的 ::" 冲突
        format!("{}\x1F{}\x1F{}", self.0, self.1, self.2)
    }
}

/// ABAC 决策缓存 TTL(秒)。
const DECISION_CACHE_TTL_SECS: u64 = 60;

/// ABAC 决策缓存最大容量。
const DECISION_CACHE_MAX_CAPACITY: u64 = 10_000;

/// ABAC 策略求值器,基于 Cedar 策略语言。
///
/// 提供 principal-action-resource 三元组策略求值,支持策略的热加载。
/// ABAC 作为 RBAC 的增量校验层,不替换 RBAC。RBAC 通过后再检查 ABAC。
///
/// # 设计
///
/// - `authorizer`:Cedar 授权器(无状态,可共享)
/// - `policies`:策略集(`Arc<RwLock<PolicySet>>` 支持读写分离热加载)
/// - `schema`:Cedar schema(定义实体类型、属性、动作)
/// - `cache`:决策缓存(`oxcache::Cache`,全局 TTL 60s, max 10000),key 为 DecisionKey
/// - `entity_loader`:实体加载器
///
/// # 线程安全
///
/// `AbacEngine` 内部使用 `Arc<RwLock<PolicySet>>`,可安全共享。
/// 求值时获取读锁,热加载时获取写锁,互不阻塞。
pub struct AbacEngine {
    /// Cedar 授权器(无状态)。
    authorizer: Authorizer,
    /// 策略集(RwLock 支持热加载时读写分离)。
    policies: Arc<RwLock<PolicySet>>,
    /// Cedar schema(定义实体类型、属性、动作)。
    schema: Schema,
    /// 决策缓存(`oxcache::Cache<DecisionKey, Decision>`,全局 TTL 60s, max 10000)。
    ///
    /// 策略热加载(load/unload/reload_all 成功)时调用 `clear()` 清空。
    cache: Cache<DecisionKey, Decision>,
    /// 实体加载器。
    ///
    /// 每次 `evaluate` 时调用 `load_entities` 获取 Cedar Entities 集合,
    /// 支持基于实体属性的策略(如 `resource.owner == principal.id`)。
    /// 决策缓存不因实体加载而失效——调用方需保证 `EntityLoader` 返回稳定实体集合。
    entity_loader: Arc<dyn EntityLoader>,
}

impl AbacEngine {
    /// 从 JSON schema 创建 AbacEngine。
    ///
    /// # 参数
    /// - `schema_json`:Cedar schema JSON 字符串
    /// - `entity_loader`:实体加载器(`Arc<dyn EntityLoader>`)。
    ///   传 `Arc::new(EmptyEntityLoader)` 保持空实体行为,传 `Arc::new(StaticEntityLoader::new(...))`
    ///   支持基于属性的策略。
    ///
    /// # 错误
    /// - schema JSON 解析失败:`GarrisonError::InvalidParam`
    /// - oxcache 初始化失败:`GarrisonError::InvalidParam`
    pub async fn new(
        schema_json: &str,
        entity_loader: Arc<dyn EntityLoader>,
    ) -> GarrisonResult<Self> {
        let schema = Schema::from_json_str(schema_json)
            .map_err(|e| GarrisonError::InvalidParam(format!("abac-cedar-schema-parse::{}", e)))?;
        let cache = Cache::builder()
            .ttl(Duration::from_secs(DECISION_CACHE_TTL_SECS))
            .capacity(DECISION_CACHE_MAX_CAPACITY)
            .build()
            .await
            .map_err(|e| GarrisonError::InvalidParam(format!("abac-decision-cache-init::{}", e)))?;
        Ok(Self {
            authorizer: Authorizer::new(),
            policies: Arc::new(RwLock::new(PolicySet::new())),
            schema,
            cache,
            entity_loader,
        })
    }

    /// 求值策略。
    ///
    /// # 参数
    /// - `principal`:主体 EntityUid 字符串(如 `User::"alice"`)
    /// - `action`:动作 EntityUid 字符串(如 `Action::"access"`)
    /// - `resource`:资源 EntityUid 字符串(如 `Resource::"doc1"`)
    /// - `context_json`:可选的上下文 JSON 字符串
    ///
    /// # 返回
    /// - `Decision::allow()`:Cedar 允许
    /// - `Decision::deny(...)`:Cedar 拒绝(默认拒绝或显式 forbid)
    ///
    /// # 错误
    /// - EntityUid 解析失败:`GarrisonError::InvalidParam`
    /// - Context 解析失败:`GarrisonError::InvalidParam`
    /// - Request 构造失败:`GarrisonError::InvalidParam`
    pub async fn evaluate(
        &self,
        principal: &str,
        action: &str,
        resource: &str,
        context_json: Option<&str>,
    ) -> GarrisonResult<Decision> {
        // 缓存查询:命中则直接返回(不调用 Cedar)
        let cache_key = DecisionKey(
            principal.to_string(),
            action.to_string(),
            resource.to_string(),
        );
        if let Some(cached) =
            self.cache.get(&cache_key).await.map_err(|e| {
                GarrisonError::InvalidParam(format!("abac-decision-cache-read::{}", e))
            })?
        {
            return Ok(cached);
        }

        // 缓存未命中,调用 Cedar 求值
        let principal_uid: EntityUid = principal
            .parse()
            .map_err(|e| GarrisonError::InvalidParam(format!("abac-principal-parse::{}", e)))?;
        let action_uid: EntityUid = action
            .parse()
            .map_err(|e| GarrisonError::InvalidParam(format!("abac-action-parse::{}", e)))?;
        let resource_uid: EntityUid = resource
            .parse()
            .map_err(|e| GarrisonError::InvalidParam(format!("abac-resource-parse::{}", e)))?;
        let context = match context_json {
            Some(json) => Context::from_json_str(json, Some((&self.schema, &action_uid)))
                .map_err(|e| GarrisonError::InvalidParam(format!("abac-context-parse::{}", e)))?,
            None => Context::empty(),
        };
        let request = Request::new(
            principal_uid,
            action_uid,
            resource_uid,
            context,
            Some(&self.schema),
        )
        .map_err(|e| GarrisonError::InvalidParam(format!("abac-cedar-request-build::{}", e)))?;
        let policies = self.policies.read().await;
        // 通过 EntityLoader 加载实体。若返回错误,通过 ? 传播,缓存不受污染(未到达 set)。
        let entities = self.entity_loader.load_entities().await?;
        let response = self
            .authorizer
            .is_authorized(&request, &policies, &entities);
        let decision = match response.decision() {
            cedar_policy::Decision::Allow => Decision::allow(),
            cedar_policy::Decision::Deny => Decision::deny(DecisionReason::NoMatchingPermission),
        };

        // 写入缓存(仅在求值成功后)
        self.cache.set(&cache_key, &decision).await.map_err(|e| {
            GarrisonError::InvalidParam(format!("abac-decision-cache-write::{}", e))
        })?;

        Ok(decision)
    }

    /// 加载策略。
    ///
    /// # 参数
    /// - `policy_id`:策略 ID(用于卸载)
    /// - `policy_src`:Cedar DSL 策略文本
    ///
    /// # 错误
    /// - 策略语法错误:`GarrisonError::InvalidParam`
    /// - 策略 ID 冲突:`GarrisonError::InvalidParam`
    pub async fn load_policy(&self, policy_id: &str, policy_src: &str) -> GarrisonResult<()> {
        let policy = Policy::parse(Some(PolicyId::new(policy_id)), policy_src)
            .map_err(|e| GarrisonError::InvalidParam(format!("abac-cedar-policy-parse::{}", e)))?;
        let mut policies = self.policies.write().await;
        policies
            .add(policy)
            .map_err(|e| GarrisonError::InvalidParam(format!("abac-cedar-policy-add::{}", e)))?;
        // 策略变更后清空决策缓存(新策略可能改变现有 key 的决策)
        self.cache.clear().await.map_err(|e| {
            GarrisonError::InvalidParam(format!("abac-decision-cache-clear::{}", e))
        })?;
        Ok(())
    }

    /// 卸载策略。
    ///
    /// # 参数
    /// - `policy_id`:策略 ID
    ///
    /// # 错误
    /// - 策略不存在:`GarrisonError::InvalidParam`
    pub async fn unload_policy(&self, policy_id: &str) -> GarrisonResult<()> {
        let policy_id = PolicyId::new(policy_id);
        let mut policies = self.policies.write().await;
        policies
            .remove_static(policy_id)
            .map_err(|e| GarrisonError::InvalidParam(format!("abac-cedar-policy-delete::{}", e)))?;
        // 策略变更后清空决策缓存(移除策略可能改变现有 key 的决策)
        self.cache.clear().await.map_err(|e| {
            GarrisonError::InvalidParam(format!("abac-decision-cache-clear::{}", e))
        })?;
        Ok(())
    }

    /// 原子替换全部策略。
    ///
    /// 先解析所有策略,全部成功后原子替换 PolicySet。
    /// 任一策略语法错误则不替换,保持现有策略集不变。
    ///
    /// # 参数
    /// - `policies`:策略 ID → 策略文本的映射
    ///
    /// # 错误
    /// - 任一策略语法错误:`GarrisonError::InvalidParam`(不部分加载)
    pub async fn reload_all(&self, policies: HashMap<String, String>) -> GarrisonResult<()> {
        let mut new_set = PolicySet::new();
        for (id, src) in &policies {
            let policy = Policy::parse(Some(PolicyId::new(id)), src).map_err(|e| {
                GarrisonError::InvalidParam(format!("abac-cedar-policy-parse-id::{}::{}", id, e))
            })?;
            new_set.add(policy).map_err(|e| {
                GarrisonError::InvalidParam(format!("abac-cedar-policy-add-id::{}::{}", id, e))
            })?;
        }
        let mut guard = self.policies.write().await;
        *guard = new_set;
        // 策略集原子替换后清空决策缓存(新策略集可能改变现有 key 的决策)
        // 仅在替换成功后执行;任一策略解析失败时提前 return Err,不会到达此处
        self.cache.clear().await.map_err(|e| {
            GarrisonError::InvalidParam(format!("abac-decision-cache-clear::{}", e))
        })?;
        Ok(())
    }

    /// 使用临时策略求值(不修改共享策略集)。
    ///
    /// 供 `check_abac_with_policy` 调用:宏生成的 Cedar 条件表达式包装为完整策略后,
    /// 通过本方法求值,避免临时策略污染全局策略集。
    ///
    /// 临时策略独立求值,不与 `load_policy` 加载的共享策略合并。
    ///
    /// # 参数
    /// - `principal`:主体 EntityUid 字符串
    /// - `action`:动作 EntityUid 字符串
    /// - `resource`:资源 EntityUid 字符串
    /// - `context_json`:可选的上下文 JSON
    /// - `temp_policy_src`:临时 Cedar 策略文本
    ///
    /// # 错误
    /// - EntityUid/Context/Request 解析失败:`GarrisonError::InvalidParam`
    /// - 临时策略语法错误:`GarrisonError::InvalidParam`
    pub async fn evaluate_with_temp_policy(
        &self,
        principal: &str,
        action: &str,
        resource: &str,
        context_json: Option<&str>,
        temp_policy_src: &str,
    ) -> GarrisonResult<Decision> {
        let principal_uid: EntityUid = principal
            .parse()
            .map_err(|e| GarrisonError::InvalidParam(format!("abac-principal-parse::{}", e)))?;
        let action_uid: EntityUid = action
            .parse()
            .map_err(|e| GarrisonError::InvalidParam(format!("abac-action-parse::{}", e)))?;
        let resource_uid: EntityUid = resource
            .parse()
            .map_err(|e| GarrisonError::InvalidParam(format!("abac-resource-parse::{}", e)))?;
        let context = match context_json {
            Some(json) => Context::from_json_str(json, Some((&self.schema, &action_uid)))
                .map_err(|e| GarrisonError::InvalidParam(format!("abac-context-parse::{}", e)))?,
            None => Context::empty(),
        };
        let request = Request::new(
            principal_uid,
            action_uid,
            resource_uid,
            context,
            Some(&self.schema),
        )
        .map_err(|e| GarrisonError::InvalidParam(format!("abac-cedar-request-build::{}", e)))?;
        let temp_policy = Policy::parse(None, temp_policy_src).map_err(|e| {
            GarrisonError::InvalidParam(format!("abac-temp-cedar-policy-parse::{}", e))
        })?;
        let mut temp_set = PolicySet::new();
        temp_set.add(temp_policy).map_err(|e| {
            GarrisonError::InvalidParam(format!("abac-temp-cedar-policy-add::{}", e))
        })?;
        // 通过 EntityLoader 加载实体;evaluate_with_temp_policy 不写入缓存,错误通过 ? 传播。
        let entities = self.entity_loader.load_entities().await?;
        let response = self
            .authorizer
            .is_authorized(&request, &temp_set, &entities);
        match response.decision() {
            cedar_policy::Decision::Allow => Ok(Decision::allow()),
            cedar_policy::Decision::Deny => {
                Ok(Decision::deny(DecisionReason::NoMatchingPermission))
            },
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::abac::{EmptyEntityLoader, StaticEntityLoader};
    use cedar_policy::Entities;

    /// 测试辅助:判断指定 (principal, action, resource) 决策是否已在缓存中。
    ///
    /// 使用 oxcache `get()` 而非 `len()`:oxcache `len()` 是
    /// 最终一致估算值,写入后未必立即可见;`get()` 强制检索 key,可靠。
    async fn cache_has_entry(
        engine: &AbacEngine,
        principal: &str,
        action: &str,
        resource: &str,
    ) -> bool {
        let key = DecisionKey(
            principal.to_string(),
            action.to_string(),
            resource.to_string(),
        );
        engine.cache.get(&key).await.unwrap_or(None).is_some()
    }

    /// 测试用 Cedar schema JSON(空 namespace,EntityUid 直接用 `User::"alice"` 格式)。
    ///
    /// 定义 User 和 Resource 实体类型,以及 access 动作。
    const SCHEMA_JSON: &str = r#"{
        "": {
            "entityTypes": {
                "User": {
                    "shape": {
                        "type": "Record",
                        "attributes": {
                            "department": { "type": "String" }
                        }
                    }
                },
                "Resource": {
                    "shape": {
                        "type": "Record",
                        "attributes": {
                            "owner": { "type": "String" }
                        }
                    }
                }
            },
            "actions": {
                "access": {
                    "appliesTo": {
                        "principalTypes": ["User"],
                        "resourceTypes": ["Resource"]
                    }
                }
            }
        }
    }"#;

    /// T121: AbacEngine::new 从 JSON schema 初始化成功。
    #[tokio::test]
    async fn t121_new_from_json_schema_success() {
        let engine = AbacEngine::new(SCHEMA_JSON, Arc::new(EmptyEntityLoader)).await;
        assert!(engine.is_ok(), "AbacEngine::new 应成功: {:?}", engine.err());
    }

    /// T121: 无效 JSON schema 返回 InvalidParam。
    #[tokio::test]
    async fn t121_new_invalid_schema_returns_invalid_param() {
        let result = AbacEngine::new("not a valid json", Arc::new(EmptyEntityLoader)).await;
        assert!(result.is_err());
        assert!(
            matches!(result, Err(GarrisonError::InvalidParam(_))),
            "应为 InvalidParam"
        );
    }

    /// T123: evaluate — 匹配 permit 策略时返回 Allow。
    #[tokio::test]
    async fn t123_evaluate_permit_returns_allow() {
        let engine = AbacEngine::new(SCHEMA_JSON, Arc::new(EmptyEntityLoader))
            .await
            .expect("schema valid");
        engine
            .load_policy(
                "p1",
                r#"permit(principal == User::"alice", action == Action::"access", resource);"#,
            )
            .await
            .expect("load policy");
        let decision = engine
            .evaluate(
                r#"User::"alice""#,
                r#"Action::"access""#,
                r#"Resource::"doc1""#,
                None,
            )
            .await
            .expect("evaluate");
        assert!(decision.allowed, "alice 应被允许");
        assert_eq!(decision.reason, DecisionReason::ExplicitAllow);
    }

    /// T123: evaluate — 不匹配 permit 策略时返回 Deny(默认拒绝)。
    #[tokio::test]
    async fn t123_evaluate_no_match_returns_deny() {
        let engine = AbacEngine::new(SCHEMA_JSON, Arc::new(EmptyEntityLoader))
            .await
            .expect("schema valid");
        engine
            .load_policy(
                "p1",
                r#"permit(principal == User::"alice", action == Action::"access", resource);"#,
            )
            .await
            .expect("load policy");
        let decision = engine
            .evaluate(
                r#"User::"bob""#,
                r#"Action::"access""#,
                r#"Resource::"doc1""#,
                None,
            )
            .await
            .expect("evaluate");
        assert!(!decision.allowed, "bob 应被拒绝");
        assert_eq!(decision.reason, DecisionReason::NoMatchingPermission);
    }

    /// T123: evaluate — 无策略时默认 Deny。
    #[tokio::test]
    async fn t123_evaluate_no_policies_returns_deny() {
        let engine = AbacEngine::new(SCHEMA_JSON, Arc::new(EmptyEntityLoader))
            .await
            .expect("schema valid");
        let decision = engine
            .evaluate(
                r#"User::"alice""#,
                r#"Action::"access""#,
                r#"Resource::"doc1""#,
                None,
            )
            .await
            .expect("evaluate");
        assert!(!decision.allowed, "无策略时应默认拒绝");
    }

    /// T123: evaluate — 无效 principal 格式返回 InvalidParam。
    #[tokio::test]
    async fn t123_evaluate_invalid_principal_returns_error() {
        let engine = AbacEngine::new(SCHEMA_JSON, Arc::new(EmptyEntityLoader))
            .await
            .expect("schema valid");
        let result = engine
            .evaluate(
                "invalid",
                r#"Action::"access""#,
                r#"Resource::"doc1""#,
                None,
            )
            .await;
        assert!(result.is_err());
        assert!(
            matches!(result, Err(GarrisonError::InvalidParam(_))),
            "应为 InvalidParam"
        );
    }

    /// T125: Decision 映射 — Cedar Allow → Decision::allow()。
    #[tokio::test]
    async fn t125_decision_mapping_allow() {
        let engine = AbacEngine::new(SCHEMA_JSON, Arc::new(EmptyEntityLoader))
            .await
            .expect("schema valid");
        engine
            .load_policy("p1", r#"permit(principal, action, resource);"#)
            .await
            .expect("load policy");
        let decision = engine
            .evaluate(
                r#"User::"alice""#,
                r#"Action::"access""#,
                r#"Resource::"doc1""#,
                None,
            )
            .await
            .expect("evaluate");
        assert!(decision.allowed);
        assert_eq!(decision.reason, DecisionReason::ExplicitAllow);
    }

    /// T125: Decision 映射 — Cedar Deny → Decision::deny()。
    #[tokio::test]
    async fn t125_decision_mapping_deny() {
        let engine = AbacEngine::new(SCHEMA_JSON, Arc::new(EmptyEntityLoader))
            .await
            .expect("schema valid");
        // 空策略集,默认 Deny
        let decision = engine
            .evaluate(
                r#"User::"alice""#,
                r#"Action::"access""#,
                r#"Resource::"doc1""#,
                None,
            )
            .await
            .expect("evaluate");
        assert!(!decision.allowed);
        assert_eq!(decision.reason, DecisionReason::NoMatchingPermission);
    }

    /// T125: forbid 策略覆盖 permit — Cedar 语义验证。
    #[tokio::test]
    async fn t125_forbid_overrides_permit() {
        let engine = AbacEngine::new(SCHEMA_JSON, Arc::new(EmptyEntityLoader))
            .await
            .expect("schema valid");
        engine
            .load_policy("p1", r#"permit(principal, action, resource);"#)
            .await
            .expect("load permit");
        engine
            .load_policy(
                "p2",
                r#"forbid(principal == User::"alice", action, resource);"#,
            )
            .await
            .expect("load forbid");
        let decision = engine
            .evaluate(
                r#"User::"alice""#,
                r#"Action::"access""#,
                r#"Resource::"doc1""#,
                None,
            )
            .await
            .expect("evaluate");
        assert!(!decision.allowed, "forbid 应覆盖 permit");
    }

    /// T125: evaluate 带 context JSON 不报错。
    #[tokio::test]
    async fn t125_evaluate_with_context() {
        let engine = AbacEngine::new(SCHEMA_JSON, Arc::new(EmptyEntityLoader))
            .await
            .expect("schema valid");
        engine
            .load_policy("p1", r#"permit(principal, action, resource);"#)
            .await
            .expect("load policy");
        let decision = engine
            .evaluate(
                r#"User::"alice""#,
                r#"Action::"access""#,
                r#"Resource::"doc1""#,
                Some(r#"{}"#),
            )
            .await
            .expect("evaluate");
        assert!(decision.allowed);
    }

    /// 策略语法错误返回 InvalidParam。
    #[tokio::test]
    async fn load_policy_syntax_error_returns_invalid_param() {
        let engine = AbacEngine::new(SCHEMA_JSON, Arc::new(EmptyEntityLoader))
            .await
            .expect("schema valid");
        let result = engine
            .load_policy("p1", "this is not a valid cedar policy")
            .await;
        assert!(result.is_err());
        assert!(
            matches!(result, Err(GarrisonError::InvalidParam(_))),
            "应为 InvalidParam"
        );
    }

    /// unload_policy 删除策略后求值变化。
    #[tokio::test]
    async fn unload_policy_changes_decision() {
        let engine = AbacEngine::new(SCHEMA_JSON, Arc::new(EmptyEntityLoader))
            .await
            .expect("schema valid");
        engine
            .load_policy("p1", r#"permit(principal, action, resource);"#)
            .await
            .expect("load");
        let before = engine
            .evaluate(
                r#"User::"alice""#,
                r#"Action::"access""#,
                r#"Resource::"doc1""#,
                None,
            )
            .await
            .expect("evaluate");
        assert!(before.allowed, "卸载前应允许");
        engine.unload_policy("p1").await.expect("unload");
        let after = engine
            .evaluate(
                r#"User::"alice""#,
                r#"Action::"access""#,
                r#"Resource::"doc1""#,
                None,
            )
            .await
            .expect("evaluate");
        assert!(!after.allowed, "卸载后应拒绝");
    }

    /// reload_all 原子替换全部策略。
    #[tokio::test]
    async fn reload_all_atomically_replaces_policies() {
        let engine = AbacEngine::new(SCHEMA_JSON, Arc::new(EmptyEntityLoader))
            .await
            .expect("schema valid");
        engine
            .load_policy("p1", r#"permit(principal, action, resource);"#)
            .await
            .expect("load");
        let mut new_policies = HashMap::new();
        new_policies.insert(
            "p2".to_string(),
            r#"permit(principal == User::"bob", action, resource);"#.to_string(),
        );
        engine.reload_all(new_policies).await.expect("reload");
        // p1 已被替换,alice 不再匹配
        let alice = engine
            .evaluate(
                r#"User::"alice""#,
                r#"Action::"access""#,
                r#"Resource::"doc1""#,
                None,
            )
            .await
            .expect("evaluate");
        assert!(!alice.allowed, "alice 应被拒绝(p1 已被替换)");
        // p2 匹配 bob
        let bob = engine
            .evaluate(
                r#"User::"bob""#,
                r#"Action::"access""#,
                r#"Resource::"doc1""#,
                None,
            )
            .await
            .expect("evaluate");
        assert!(bob.allowed, "bob 应被允许(p2 匹配)");
    }

    /// reload_all 任一策略语法错误时不替换(原子性)。
    #[tokio::test]
    async fn reload_all_syntax_error_keeps_existing() {
        let engine = AbacEngine::new(SCHEMA_JSON, Arc::new(EmptyEntityLoader))
            .await
            .expect("schema valid");
        engine
            .load_policy("p1", r#"permit(principal, action, resource);"#)
            .await
            .expect("load");
        let mut bad_policies = HashMap::new();
        bad_policies.insert("p2".to_string(), "invalid policy".to_string());
        let result = engine.reload_all(bad_policies).await;
        assert!(result.is_err(), "reload_all 应失败");
        // 现有策略 p1 仍应有效
        let decision = engine
            .evaluate(
                r#"User::"alice""#,
                r#"Action::"access""#,
                r#"Resource::"doc1""#,
                None,
            )
            .await
            .expect("evaluate");
        assert!(decision.allowed, "现有策略应保持不变");
    }

    /// 并发求值不阻塞(RwLock 读写分离验证)。
    #[tokio::test]
    async fn concurrent_evaluate_does_not_block() {
        let engine = Arc::new(
            AbacEngine::new(SCHEMA_JSON, Arc::new(EmptyEntityLoader))
                .await
                .expect("schema valid"),
        );
        engine
            .load_policy("p1", r#"permit(principal, action, resource);"#)
            .await
            .expect("load");
        // 并发 10 个求值
        let mut handles = Vec::new();
        for i in 0..10 {
            let e = engine.clone();
            handles.push(tokio::spawn(async move {
                e.evaluate(
                    r#"User::"alice""#,
                    r#"Action::"access""#,
                    &format!(r#"Resource::"doc{i}""#),
                    None,
                )
                .await
                .expect("evaluate")
            }));
        }
        for handle in handles {
            let decision = handle.await.expect("task complete");
            assert!(decision.allowed, "并发求值应全部允许");
        }
    }

    // ============================================================
    // evaluate_with_temp_policy 测试(T138/T139)
    // ============================================================

    /// T139: evaluate_with_temp_policy — 匹配的临时策略返回 Allow。
    #[tokio::test]
    async fn t139_evaluate_with_temp_policy_permit_returns_allow() {
        let engine = AbacEngine::new(SCHEMA_JSON, Arc::new(EmptyEntityLoader))
            .await
            .expect("schema valid");
        let temp_policy = r#"permit(principal, action == Action::"access", resource);"#;
        let decision = engine
            .evaluate_with_temp_policy(
                r#"User::"alice""#,
                r#"Action::"access""#,
                r#"Resource::"doc1""#,
                None,
                temp_policy,
            )
            .await
            .expect("evaluate temp policy");
        assert!(decision.allowed, "匹配的临时策略应 Allow");
    }

    /// T139: evaluate_with_temp_policy — 不匹配的临时策略返回 Deny。
    #[tokio::test]
    async fn t139_evaluate_with_temp_policy_no_match_returns_deny() {
        let engine = AbacEngine::new(SCHEMA_JSON, Arc::new(EmptyEntityLoader))
            .await
            .expect("schema valid");
        let temp_policy =
            r#"permit(principal == User::"bob", action == Action::"access", resource);"#;
        let decision = engine
            .evaluate_with_temp_policy(
                r#"User::"alice""#,
                r#"Action::"access""#,
                r#"Resource::"doc1""#,
                None,
                temp_policy,
            )
            .await
            .expect("evaluate temp policy");
        assert!(!decision.allowed, "不匹配的临时策略应 Deny");
    }

    /// T139: evaluate_with_temp_policy — 无效策略返回 InvalidParam。
    #[tokio::test]
    async fn t139_evaluate_with_temp_policy_invalid_returns_error() {
        let engine = AbacEngine::new(SCHEMA_JSON, Arc::new(EmptyEntityLoader))
            .await
            .expect("schema valid");
        let result = engine
            .evaluate_with_temp_policy(
                r#"User::"alice""#,
                r#"Action::"access""#,
                r#"Resource::"doc1""#,
                None,
                "not a valid cedar policy",
            )
            .await;
        assert!(result.is_err());
        assert!(
            matches!(result, Err(GarrisonError::InvalidParam(_))),
            "应为 InvalidParam"
        );
    }

    /// T139: evaluate_with_temp_policy — 不修改共享策略集。
    #[tokio::test]
    async fn t139_evaluate_with_temp_policy_does_not_modify_shared() {
        let engine = AbacEngine::new(SCHEMA_JSON, Arc::new(EmptyEntityLoader))
            .await
            .expect("schema valid");
        // 共享策略集初始为空
        let before = engine
            .evaluate(
                r#"User::"alice""#,
                r#"Action::"access""#,
                r#"Resource::"doc1""#,
                None,
            )
            .await
            .expect("evaluate");
        assert!(!before.allowed, "共享策略集为空时应 Deny");

        // 临时策略求值
        let temp_policy = r#"permit(principal, action == Action::"access", resource);"#;
        let temp_decision = engine
            .evaluate_with_temp_policy(
                r#"User::"alice""#,
                r#"Action::"access""#,
                r#"Resource::"doc1""#,
                None,
                temp_policy,
            )
            .await
            .expect("evaluate temp");
        assert!(temp_decision.allowed, "临时策略应 Allow");

        // 共享策略集仍为空
        let after = engine
            .evaluate(
                r#"User::"alice""#,
                r#"Action::"access""#,
                r#"Resource::"doc1""#,
                None,
            )
            .await
            .expect("evaluate");
        assert!(!after.allowed, "临时策略求值后共享策略集应仍为空(Deny)");
    }

    /// T139: evaluate_with_temp_policy — 带 when 条件的策略。
    #[tokio::test]
    async fn t139_evaluate_with_temp_policy_when_condition() {
        let engine = AbacEngine::new(SCHEMA_JSON, Arc::new(EmptyEntityLoader))
            .await
            .expect("schema valid");
        // when 条件为 true(1 == 1)→ Allow
        let temp_policy_true =
            r#"permit(principal, action == Action::"access", resource) when { 1 == 1 };"#;
        let decision_true = engine
            .evaluate_with_temp_policy(
                r#"User::"alice""#,
                r#"Action::"access""#,
                r#"Resource::"doc1""#,
                None,
                temp_policy_true,
            )
            .await
            .expect("evaluate");
        assert!(decision_true.allowed, "when {{ 1 == 1 }} 应 Allow");

        // when 条件为 false(1 == 2)→ Deny
        let temp_policy_false =
            r#"permit(principal, action == Action::"access", resource) when { 1 == 2 };"#;
        let decision_false = engine
            .evaluate_with_temp_policy(
                r#"User::"alice""#,
                r#"Action::"access""#,
                r#"Resource::"doc1""#,
                None,
                temp_policy_false,
            )
            .await
            .expect("evaluate");
        assert!(!decision_false.allowed, "when {{ 1 == 2 }} 应 Deny");
    }

    // ============================================================
    // T016: ABAC 决策缓存 oxcache TTL 60s 测试
    // ============================================================

    /// T016: 同 key 两次 evaluate,第二次命中缓存(len == 1)。
    #[tokio::test]
    async fn t016_cache_hit_on_second_evaluate_same_key() {
        let engine = AbacEngine::new(SCHEMA_JSON, Arc::new(EmptyEntityLoader))
            .await
            .expect("schema valid");
        engine
            .load_policy("p1", r#"permit(principal, action, resource);"#)
            .await
            .expect("load");

        let principal = r#"User::"alice""#;
        let action = r#"Action::"access""#;
        let resource = r#"Resource::"doc1""#;

        let d1 = engine
            .evaluate(principal, action, resource, None)
            .await
            .expect("evaluate 1");
        assert!(
            cache_has_entry(&engine, principal, action, resource).await,
            "首次求值后缓存应包含该 key"
        );

        let d2 = engine
            .evaluate(principal, action, resource, None)
            .await
            .expect("evaluate 2");
        assert!(
            cache_has_entry(&engine, principal, action, resource).await,
            "第二次求值后缓存仍应包含该 key"
        );
        assert_eq!(d1.allowed, d2.allowed, "两次求值结果应一致");
    }

    /// T016: 不同 key 两次 evaluate,缓存各存一条(len == 2)。
    #[tokio::test]
    async fn t016_cache_miss_on_different_keys() {
        let engine = AbacEngine::new(SCHEMA_JSON, Arc::new(EmptyEntityLoader))
            .await
            .expect("schema valid");
        engine
            .load_policy("p1", r#"permit(principal, action, resource);"#)
            .await
            .expect("load");

        engine
            .evaluate(
                r#"User::"alice""#,
                r#"Action::"access""#,
                r#"Resource::"doc1""#,
                None,
            )
            .await
            .expect("evaluate 1");
        engine
            .evaluate(
                r#"User::"bob""#,
                r#"Action::"access""#,
                r#"Resource::"doc1""#,
                None,
            )
            .await
            .expect("evaluate 2");
        assert!(
            cache_has_entry(
                &engine,
                r#"User::"alice""#,
                r#"Action::"access""#,
                r#"Resource::"doc1""#
            )
            .await
                && cache_has_entry(
                    &engine,
                    r#"User::"bob""#,
                    r#"Action::"access""#,
                    r#"Resource::"doc1""#
                )
                .await,
            "不同 key 应分别缓存"
        );
    }

    /// T016: unload_policy 后缓存失效(len == 0)。
    #[tokio::test]
    async fn t016_cache_invalidated_on_unload_policy() {
        let engine = AbacEngine::new(SCHEMA_JSON, Arc::new(EmptyEntityLoader))
            .await
            .expect("schema valid");
        engine
            .load_policy("p1", r#"permit(principal, action, resource);"#)
            .await
            .expect("load");

        engine
            .evaluate(
                r#"User::"alice""#,
                r#"Action::"access""#,
                r#"Resource::"doc1""#,
                None,
            )
            .await
            .expect("evaluate");
        assert!(
            cache_has_entry(
                &engine,
                r#"User::"alice""#,
                r#"Action::"access""#,
                r#"Resource::"doc1""#
            )
            .await,
            "求值后缓存应包含该 key"
        );

        engine.unload_policy("p1").await.expect("unload");
        assert!(
            !cache_has_entry(
                &engine,
                r#"User::"alice""#,
                r#"Action::"access""#,
                r#"Resource::"doc1""#
            )
            .await,
            "unload 后缓存应清空"
        );

        // 再次求值,应重新调用 Cedar(结果变为 Deny)
        let after = engine
            .evaluate(
                r#"User::"alice""#,
                r#"Action::"access""#,
                r#"Resource::"doc1""#,
                None,
            )
            .await
            .expect("evaluate after");
        assert!(!after.allowed, "unload 后应 Deny");
    }

    /// T016: reload_all 成功后缓存失效(len == 0)。
    #[tokio::test]
    async fn t016_cache_invalidated_on_reload_all() {
        let engine = AbacEngine::new(SCHEMA_JSON, Arc::new(EmptyEntityLoader))
            .await
            .expect("schema valid");
        engine
            .load_policy("p1", r#"permit(principal, action, resource);"#)
            .await
            .expect("load");

        engine
            .evaluate(
                r#"User::"alice""#,
                r#"Action::"access""#,
                r#"Resource::"doc1""#,
                None,
            )
            .await
            .expect("evaluate");
        assert!(
            cache_has_entry(
                &engine,
                r#"User::"alice""#,
                r#"Action::"access""#,
                r#"Resource::"doc1""#
            )
            .await,
            "求值后缓存应包含该 key"
        );

        let mut new_policies = HashMap::new();
        new_policies.insert(
            "p2".to_string(),
            r#"permit(principal == User::"bob", action, resource);"#.to_string(),
        );
        engine.reload_all(new_policies).await.expect("reload");
        assert!(
            !cache_has_entry(
                &engine,
                r#"User::"alice""#,
                r#"Action::"access""#,
                r#"Resource::"doc1""#
            )
            .await,
            "reload 后缓存应清空"
        );

        // alice 应被拒绝(p1 已替换为 p2)
        let alice = engine
            .evaluate(
                r#"User::"alice""#,
                r#"Action::"access""#,
                r#"Resource::"doc1""#,
                None,
            )
            .await
            .expect("evaluate");
        assert!(!alice.allowed, "alice 应被拒绝");
    }

    /// T016: reload_all 失败时缓存保持不变(不失效)。
    #[tokio::test]
    async fn t016_cache_kept_on_reload_all_failure() {
        let engine = AbacEngine::new(SCHEMA_JSON, Arc::new(EmptyEntityLoader))
            .await
            .expect("schema valid");
        engine
            .load_policy("p1", r#"permit(principal, action, resource);"#)
            .await
            .expect("load");

        engine
            .evaluate(
                r#"User::"alice""#,
                r#"Action::"access""#,
                r#"Resource::"doc1""#,
                None,
            )
            .await
            .expect("evaluate");
        assert!(
            cache_has_entry(
                &engine,
                r#"User::"alice""#,
                r#"Action::"access""#,
                r#"Resource::"doc1""#
            )
            .await,
            "求值后缓存应包含该 key"
        );

        let mut bad_policies = HashMap::new();
        bad_policies.insert("p2".to_string(), "invalid policy".to_string());
        let result = engine.reload_all(bad_policies).await;
        assert!(result.is_err(), "reload_all 应失败");
        assert!(
            cache_has_entry(
                &engine,
                r#"User::"alice""#,
                r#"Action::"access""#,
                r#"Resource::"doc1""#
            )
            .await,
            "reload 失败时缓存应保持不变"
        );

        // alice 仍应被允许(缓存命中)
        let alice = engine
            .evaluate(
                r#"User::"alice""#,
                r#"Action::"access""#,
                r#"Resource::"doc1""#,
                None,
            )
            .await
            .expect("evaluate");
        assert!(alice.allowed, "reload 失败后 alice 应仍被允许(缓存命中)");
    }

    /// T016: load_policy 后缓存失效(新策略可能改变现有 key 的决策)。
    #[tokio::test]
    async fn t016_cache_invalidated_on_load_policy() {
        let engine = AbacEngine::new(SCHEMA_JSON, Arc::new(EmptyEntityLoader))
            .await
            .expect("schema valid");

        // 初始无策略,evaluate 返回 Deny
        let before = engine
            .evaluate(
                r#"User::"alice""#,
                r#"Action::"access""#,
                r#"Resource::"doc1""#,
                None,
            )
            .await
            .expect("evaluate");
        assert!(!before.allowed, "无策略时应 Deny");
        assert!(
            cache_has_entry(
                &engine,
                r#"User::"alice""#,
                r#"Action::"access""#,
                r#"Resource::"doc1""#
            )
            .await,
            "首次求值后缓存应包含该 key"
        );

        // load_policy 后缓存失效
        engine
            .load_policy("p1", r#"permit(principal, action, resource);"#)
            .await
            .expect("load");
        assert!(
            !cache_has_entry(
                &engine,
                r#"User::"alice""#,
                r#"Action::"access""#,
                r#"Resource::"doc1""#
            )
            .await,
            "load 后缓存应清空"
        );

        // 再次 evaluate 应返回 Allow(重新调用 Cedar)
        let after = engine
            .evaluate(
                r#"User::"alice""#,
                r#"Action::"access""#,
                r#"Resource::"doc1""#,
                None,
            )
            .await
            .expect("evaluate");
        assert!(after.allowed, "load 后应 Allow");
    }

    /// T016: evaluate_with_temp_policy 不写入缓存。
    #[tokio::test]
    async fn t016_evaluate_with_temp_policy_does_not_cache() {
        let engine = AbacEngine::new(SCHEMA_JSON, Arc::new(EmptyEntityLoader))
            .await
            .expect("schema valid");

        let temp_policy = r#"permit(principal, action == Action::"access", resource);"#;
        engine
            .evaluate_with_temp_policy(
                r#"User::"alice""#,
                r#"Action::"access""#,
                r#"Resource::"doc1""#,
                None,
                temp_policy,
            )
            .await
            .expect("temp evaluate");
        assert!(
            !cache_has_entry(
                &engine,
                r#"User::"alice""#,
                r#"Action::"access""#,
                r#"Resource::"doc1""#
            )
            .await,
            "temp policy 不应写入缓存"
        );
    }

    // ============================================================
    // EntityLoader 集成测试
    // 验证带属性的实体能被 ABAC 策略正确求值
    // ============================================================

    /// 带属性实体集合的 Cedar schema:User 带 id 属性,Resource 带 owner 属性。
    ///
    /// 策略 `resource.owner == principal.id` 需访问实体属性,
    /// 必须通过 EntityLoader 提供带属性的 Entities 才能正确求值。
    const ATTR_SCHEMA_JSON: &str = r#"{
        "": {
            "entityTypes": {
                "User": {
                    "shape": {
                        "type": "Record",
                        "attributes": {
                            "id": { "type": "String" }
                        }
                    }
                },
                "Resource": {
                    "shape": {
                        "type": "Record",
                        "attributes": {
                            "owner": { "type": "String" }
                        }
                    }
                }
            },
            "actions": {
                "access": {
                    "appliesTo": {
                        "principalTypes": ["User"],
                        "resourceTypes": ["Resource"]
                    }
                }
            }
        }
    }"#;

    /// StaticEntityLoader + 带属性实体 + `resource.owner == principal.id` 策略应 Allow(属性匹配)。
    /// 通过 EntityLoader 注入带属性实体,策略能正确求值。
    #[tokio::test]
    async fn test_evaluate_with_static_entity_loader_attribute_based_policy() {
        // 构造带属性的实体:User "alice" {id: "alice"}, Resource "doc1" {owner: "alice"}。
        // Cedar 4.x Entities JSON 格式要求 uid 为对象形式 `{"__entity": {"type", "id"}}`。
        let entities_json = r#"[
            {"uid": {"__entity": {"type": "User", "id": "alice"}}, "attrs": {"id": "alice"}, "parents": []},
            {"uid": {"__entity": {"type": "Resource", "id": "doc1"}}, "attrs": {"owner": "alice"}, "parents": []}
        ]"#;
        let schema = cedar_policy::Schema::from_json_str(ATTR_SCHEMA_JSON).expect("schema valid");
        let entities =
            Entities::from_json_str(entities_json, Some(&schema)).expect("构造带属性实体应成功");

        let engine = AbacEngine::new(
            ATTR_SCHEMA_JSON,
            Arc::new(StaticEntityLoader::new(entities)),
        )
        .await
        .expect("schema valid");

        // 策略:当 resource.owner == principal.id 时允许(基于实体属性的条件)
        let policy = r#"permit(principal, action == Action::"access", resource) when { resource.owner == principal.id };"#;
        engine.load_policy("p1", policy).await.expect("load policy");

        // alice 访问 doc1:owner == id == "alice" → Allow
        let decision_allow = engine
            .evaluate(
                r#"User::"alice""#,
                r#"Action::"access""#,
                r#"Resource::"doc1""#,
                None,
            )
            .await
            .expect("evaluate");
        assert!(
            decision_allow.allowed,
            "属性匹配时应 Allow(EntityLoader 提供实体属性后应能求值基于属性的策略)"
        );

        // 切换为 owner 不匹配的实体 → Deny
        let entities_mismatch_json = r#"[
            {"uid": {"__entity": {"type": "User", "id": "alice"}}, "attrs": {"id": "alice"}, "parents": []},
            {"uid": {"__entity": {"type": "Resource", "id": "doc1"}}, "attrs": {"owner": "bob"}, "parents": []}
        ]"#;
        let entities_mismatch = Entities::from_json_str(entities_mismatch_json, Some(&schema))
            .expect("构造不匹配实体应成功");
        let engine_deny = AbacEngine::new(
            ATTR_SCHEMA_JSON,
            Arc::new(StaticEntityLoader::new(entities_mismatch)),
        )
        .await
        .expect("schema valid");
        engine_deny
            .load_policy("p1", policy)
            .await
            .expect("load policy");
        let decision_deny = engine_deny
            .evaluate(
                r#"User::"alice""#,
                r#"Action::"access""#,
                r#"Resource::"doc1""#,
                None,
            )
            .await
            .expect("evaluate");
        assert!(
            !decision_deny.allowed,
            "属性不匹配时应 Deny(owner=bob != id=alice)"
        );
    }

    /// EntityLoader 返回错误时,evaluate 通过 ? 传播错误,缓存不污染。
    /// 若 load_entities 失败(如数据源不可达),错误必须显式传播,禁止默认成功。
    #[tokio::test]
    async fn test_evaluate_with_entity_loader_error_propagates() {
        /// 总是返回错误的 EntityLoader(模拟数据源不可达)。
        struct ErrorEntityLoader;

        #[async_trait::async_trait]
        impl crate::abac::EntityLoader for ErrorEntityLoader {
            async fn load_entities(&self) -> GarrisonResult<Entities> {
                Err(GarrisonError::Config(
                    "EntityLoader 故意返回错误(模拟数据源不可达)".into(),
                ))
            }
        }

        let engine = AbacEngine::new(SCHEMA_JSON, Arc::new(ErrorEntityLoader))
            .await
            .expect("schema valid");
        // 即使加载了 permit 策略,evaluate 也应因 EntityLoader 错误而失败
        engine
            .load_policy("p1", r#"permit(principal, action, resource);"#)
            .await
            .expect("load policy");

        let result = engine
            .evaluate(
                r#"User::"alice""#,
                r#"Action::"access""#,
                r#"Resource::"doc1""#,
                None,
            )
            .await;
        assert!(
            result.is_err(),
            "EntityLoader 错误应通过 ? 传播,而非默认成功(规则 12:失败必须显性化)"
        );
        match result {
            Err(GarrisonError::Config(msg)) => {
                assert!(
                    msg.contains("EntityLoader"),
                    "错误消息应包含 'EntityLoader',实际: {}",
                    msg
                );
            },
            Err(other) => panic!(
                "期望 Config 错误(EntityLoader 错误传播),实际: {:?}",
                other
            ),
            Ok(_) => panic!("期望 Err,实际 Ok(错误被吞掉,违反规则 12)"),
        }

        // 验证缓存未受污染(错误传播路径不应写入缓存)
        assert!(
            !cache_has_entry(
                &engine,
                r#"User::"alice""#,
                r#"Action::"access""#,
                r#"Resource::"doc1""#
            )
            .await,
            "EntityLoader 错误传播时缓存不应被污染"
        );

        // evaluate_with_temp_policy 同样应传播 EntityLoader 错误
        let temp_result = engine
            .evaluate_with_temp_policy(
                r#"User::"alice""#,
                r#"Action::"access""#,
                r#"Resource::"doc1""#,
                None,
                r#"permit(principal, action, resource);"#,
            )
            .await;
        assert!(
            temp_result.is_err(),
            "evaluate_with_temp_policy 也应传播 EntityLoader 错误"
        );
        match temp_result {
            Err(GarrisonError::Config(msg)) => {
                assert!(
                    msg.contains("EntityLoader"),
                    "temp policy 错误消息应包含 'EntityLoader',实际: {}",
                    msg
                );
            },
            Err(other) => panic!(
                "evaluate_with_temp_policy 期望 Config 错误,实际: {:?}",
                other
            ),
            Ok(_) => panic!("evaluate_with_temp_policy 期望 Err,实际 Ok"),
        }
    }
}