sdforge 0.3.0

Multi-protocol SDK framework with unified macro configuration
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
// Copyright (c) 2026 Kirky.X
// SPDX-License-Identifier: MIT
//! Tests for core audit logger functionality.

use super::super::*;
use super::make_test_audit_log;

#[test]
fn test_sanitize_error_message() {
    // JWT must have three parts (header.payload.signature) to match the pattern
    let message = "Error with JWT: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c";
    let sanitized = sanitize_error_message(message);
    assert!(!sanitized.contains("eyJ"));
    assert!(sanitized.contains("[REDACTED_JWT]"));
}

#[test]
fn test_sanitize_error_message_removes_secrets() {
    // Test secret pattern removal
    let message = "Failed with password=secret123 and token: abc456";
    let sanitized = sanitize_error_message(message);
    assert!(sanitized.contains("password=[REDACTED]"));
    assert!(sanitized.contains("token=[REDACTED]"));
}

#[test]
fn test_sanitize_error_message_removes_api_keys() {
    // Test API key removal (20+ characters)
    // Note: API keys are caught by the secret pattern first, then by API key pattern.
    // Uses a clearly fake test key to avoid triggering secret scanners.
    let message = "API key: testapikey1234567890abcdefghij";
    let sanitized = sanitize_error_message(message);
    // The key should be redacted (either as [REDACTED] or [REDACTED_API_KEY])
    assert!(
        !sanitized.contains("testapikey1234567890"),
        "API key should be redacted, got: {}",
        sanitized
    );
}

#[test]
fn test_sanitize_error_message_removes_credit_cards() {
    // Test credit card number removal
    let message = "Card number: 1234-5678-9012-3456";
    let sanitized = sanitize_error_message(message);
    assert!(sanitized.contains("[REDACTED_CREDIT_CARD]"));
}

#[test]
fn test_sanitize_error_message_removes_ssn() {
    // Test SSN removal
    let message = "SSN: 123-45-6789";
    let sanitized = sanitize_error_message(message);
    assert!(sanitized.contains("[REDACTED_SSN]"));
}

#[test]
fn test_sanitize_error_message_removes_certificate_paths() {
    // Test certificate path removal
    let message = "Cannot load /etc/ssl/certs/server.pem";
    let sanitized = sanitize_error_message(message);
    assert!(sanitized.contains("[REDACTED_PATH]"));
}

#[test]
fn test_sanitize_error_message_truncation() {
    // Test truncation of long messages
    let long_message = "x".repeat(1000);
    let sanitized = sanitize_error_message(&long_message);
    assert!(sanitized.len() <= 520); // 500 + "...[TRUNCATED]"
    assert!(sanitized.ends_with("...[TRUNCATED]"));
}

#[test]
fn test_global_regex_patterns_are_initialized() {
    // Verify global regex patterns are properly initialized
    assert!(JWT_PATTERN.is_match("eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.SflKxw"));
    assert!(SECRET_PATTERN.is_match("password=secret123"));
    assert!(PATH_PATTERN.is_match("/path/to/cert.pem"));
}

#[tokio::test]
async fn test_audit_logger() {
    let logger = AppAuditLogger::with_limit(10);
    let context = AuthContext {
        user_id: Some("test_user".to_string()),
        permissions: vec![],
        metadata: crate::security::types::AuthMetadata::default(),
    };

    logger
        .log(&context, "test_action", "test_resource", true, None)
        .await;

    let logs = logger.get_logs("test_user");
    assert_eq!(logs.len(), 1);
    assert_eq!(logs[0].action(), "test_action");
}

#[tokio::test]
async fn test_get_logs_empty() {
    let logger = AppAuditLogger::new();
    let logs = logger.get_logs("nonexistent_user");
    assert_eq!(logs.len(), 0);
}

#[tokio::test]
async fn test_clear_logs() {
    let logger = AppAuditLogger::with_limit(10);
    let context = AuthContext {
        user_id: Some("test_user".to_string()),
        permissions: vec![],
        metadata: crate::security::types::AuthMetadata::default(),
    };

    logger
        .log(&context, "test_action", "test_resource", true, None)
        .await;
    assert_eq!(logger.get_logs("test_user").len(), 1);

    logger.clear_logs("test_user");
    assert_eq!(logger.get_logs("test_user").len(), 0);
}

// ============================================================================
// AppAuditLogger::log_key_rotation Tests
// ============================================================================

#[tokio::test]
async fn test_log_key_rotation_success() {
    let logger = AppAuditLogger::with_limit(10);
    logger
        .log_key_rotation("key-123", "v1", "v2", true, None)
        .await;

    let logs = logger.get_logs("system");
    assert_eq!(logs.len(), 1);
    assert_eq!(logs[0].action(), "key_rotation");
    assert_eq!(logs[0].resource(), "api_key");
}

#[tokio::test]
async fn test_log_key_rotation_failure() {
    let logger = AppAuditLogger::with_limit(10);
    logger
        .log_key_rotation(
            "key-456",
            "v1",
            "v2",
            false,
            Some("Rotation failed".to_string()),
        )
        .await;

    let logs = logger.get_logs("system");
    assert_eq!(logs.len(), 1);
    assert!(matches!(logs[0].result(), AuditResult::Failure { .. }));
}

// ============================================================================
// AppAuditLogger::total_log_count Tests
// ============================================================================

#[tokio::test]
async fn test_total_log_count_returns_zero() {
    let logger = AppAuditLogger::new();
    assert_eq!(logger.total_log_count(), 0);
}

#[tokio::test]
async fn test_total_log_count_after_logging() {
    let logger = AppAuditLogger::with_limit(10);
    let context = AuthContext {
        user_id: Some("test_user".to_string()),
        permissions: vec![],
        metadata: AuthMetadata::default(),
    };
    logger
        .log(&context, "action1", "resource1", true, None)
        .await;
    // total_log_count now returns the actual count (previously always 0).
    assert_eq!(logger.total_log_count(), 1);
}

// ============================================================================
// AppAuditLogger::dropped_log_count Tests
// ============================================================================

#[tokio::test]
async fn test_dropped_log_count_initial_value() {
    let logger = AppAuditLogger::new();
    assert_eq!(logger.dropped_log_count(), 0);
}

#[tokio::test]
async fn test_dropped_log_count_after_logging() {
    let logger = AppAuditLogger::with_limit(10);
    let context = AuthContext {
        user_id: Some("test_user".to_string()),
        permissions: vec![],
        metadata: AuthMetadata::default(),
    };
    logger.log(&context, "action", "resource", true, None).await;
    // Channel is not full, so dropped count should still be 0
    assert_eq!(logger.dropped_log_count(), 0);
}

// ============================================================================
// AppAuditLogger::log() with Failure + Error Sanitization Tests
// ============================================================================

#[tokio::test]
async fn test_log_failure_with_message() {
    let logger = AppAuditLogger::with_limit(10);
    let context = AuthContext {
        user_id: Some("test_user".to_string()),
        permissions: vec![],
        metadata: AuthMetadata::default(),
    };

    logger
        .log(
            &context,
            "delete_resource",
            "/api/resource/123",
            false,
            Some("Permission denied".to_string()),
        )
        .await;

    let logs = logger.get_logs("test_user");
    assert_eq!(logs.len(), 1);
    match logs[0].result() {
        AuditResult::Failure { message } => {
            assert_eq!(message, "Permission denied");
        }
        _ => panic!("Expected Failure result"),
    }
}

#[tokio::test]
async fn test_log_failure_sanitizes_sensitive_data() {
    let logger = AppAuditLogger::with_limit(10);
    let context = AuthContext {
        user_id: Some("test_user".to_string()),
        permissions: vec![],
        metadata: AuthMetadata::default(),
    };

    // Error message with password should be sanitized
    logger
        .log(
            &context,
            "auth",
            "/api/login",
            false,
            Some("Failed with password=secret123".to_string()),
        )
        .await;

    let logs = logger.get_logs("test_user");
    assert_eq!(logs.len(), 1);
    match logs[0].result() {
        AuditResult::Failure { message } => {
            assert!(
                !message.contains("secret123"),
                "Password should be sanitized"
            );
            assert!(
                message.contains("[REDACTED]"),
                "Should contain redacted marker"
            );
        }
        _ => panic!("Expected Failure result"),
    }
}

#[tokio::test]
async fn test_log_failure_default_message() {
    let logger = AppAuditLogger::with_limit(10);
    let context = AuthContext {
        user_id: Some("test_user".to_string()),
        permissions: vec![],
        metadata: AuthMetadata::default(),
    };

    logger
        .log(&context, "action", "resource", false, None)
        .await;

    let logs = logger.get_logs("test_user");
    match logs[0].result() {
        AuditResult::Failure { message } => {
            assert_eq!(message, "Unknown error");
        }
        _ => panic!("Expected Failure result"),
    }
}

// ============================================================================
// AuditLogger Trait Implementation Tests
// ============================================================================

#[test]
fn test_audit_logger_trait_log() {
    use crate::security::traits::AuditLogger;

    // Use a runtime to ensure tokio tasks are properly scheduled
    let rt = tokio::runtime::Runtime::new().unwrap();
    rt.block_on(async {
        let logger = AppAuditLogger::with_limit(10);
        let log = AuditLog {
            id: "trait-test-id".to_string(),
            timestamp: chrono::Utc::now().timestamp(),
            user_id: Some("trait_user".to_string()),
            action: "trait_action".to_string(),
            resource: "trait_resource".to_string(),
            result: AuditResult::Success,
            metadata: AuthMetadata::default(),
            signature: None,
        };

        // Call the trait method - it spawns a background thread
        AuditLogger::log(&logger, log);

        // Give the background thread time to complete
        tokio::time::sleep(tokio::time::Duration::from_millis(500)).await;

        let logs = logger.get_logs("trait_user");
        assert_eq!(logs.len(), 1);
        assert_eq!(logs[0].action(), "trait_action");
    });
}

#[test]
fn test_audit_logger_trait_log_failure() {
    use crate::security::traits::AuditLogger;

    let rt = tokio::runtime::Runtime::new().unwrap();
    rt.block_on(async {
        let logger = AppAuditLogger::with_limit(10);
        let log = AuditLog {
            id: "trait-fail-id".to_string(),
            timestamp: chrono::Utc::now().timestamp(),
            user_id: Some("trait_fail_user".to_string()),
            action: "trait_fail_action".to_string(),
            resource: "trait_fail_resource".to_string(),
            result: AuditResult::Failure {
                message: "Trait test failure".to_string(),
            },
            metadata: AuthMetadata::default(),
            signature: None,
        };

        AuditLogger::log(&logger, log);

        // Give the background thread time to complete
        tokio::time::sleep(tokio::time::Duration::from_millis(500)).await;

        let logs = logger.get_logs("trait_fail_user");
        assert_eq!(logs.len(), 1);
        match logs[0].result() {
            AuditResult::Failure { message } => {
                assert_eq!(message, "Trait test failure");
            }
            _ => panic!("Expected Failure result"),
        }
    });
}

// ============================================================================
// Multiple Log Entries and Truncation Tests
// ============================================================================

#[tokio::test]
async fn test_multiple_log_entries_per_user() {
    let logger = AppAuditLogger::with_limit(10);
    let context = AuthContext {
        user_id: Some("multi_user".to_string()),
        permissions: vec![],
        metadata: AuthMetadata::default(),
    };

    for i in 0..5 {
        logger
            .log(
                &context,
                format!("action_{}", i),
                format!("resource_{}", i),
                true,
                None,
            )
            .await;
    }

    let logs = logger.get_logs("multi_user");
    assert_eq!(logs.len(), 5);
}

#[tokio::test]
async fn test_log_truncation_when_exceeding_limit() {
    let logger = AppAuditLogger::with_limit(3);
    let context = AuthContext {
        user_id: Some("trunc_user".to_string()),
        permissions: vec![],
        metadata: AuthMetadata::default(),
    };

    for i in 0..5 {
        logger
            .log(
                &context,
                format!("action_{}", i),
                format!("resource_{}", i),
                true,
                None,
            )
            .await;
    }

    let logs = logger.get_logs("trunc_user");
    // Should be truncated to max_logs_per_user (3)
    assert_eq!(logs.len(), 3);
}

#[tokio::test]
async fn test_multiple_users_independent_logs() {
    let logger = AppAuditLogger::with_limit(10);

    let context1 = AuthContext {
        user_id: Some("user_a".to_string()),
        permissions: vec![],
        metadata: AuthMetadata::default(),
    };
    let context2 = AuthContext {
        user_id: Some("user_b".to_string()),
        permissions: vec![],
        metadata: AuthMetadata::default(),
    };

    logger
        .log(&context1, "action_a", "resource_a", true, None)
        .await;
    logger
        .log(&context2, "action_b", "resource_b", true, None)
        .await;

    let logs_a = logger.get_logs("user_a");
    let logs_b = logger.get_logs("user_b");

    assert_eq!(logs_a.len(), 1);
    assert_eq!(logs_a[0].action(), "action_a");
    assert_eq!(logs_b.len(), 1);
    assert_eq!(logs_b[0].action(), "action_b");
}

#[tokio::test]
async fn test_log_with_anonymous_user() {
    let logger = AppAuditLogger::with_limit(10);
    let context = AuthContext {
        user_id: None,
        permissions: vec![],
        metadata: AuthMetadata::default(),
    };

    logger
        .log(&context, "anonymous_action", "resource", true, None)
        .await;

    let logs = logger.get_logs("anonymous");
    assert_eq!(logs.len(), 1);
    assert_eq!(logs[0].action(), "anonymous_action");
}

// ============================================================================
// Additional Boundary Tests for sanitize_error_message
// ============================================================================

#[test]
fn test_sanitize_jwt_token() {
    let message = "Authentication failed for JWT: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c";
    let sanitized = sanitize_error_message(message);
    assert!(sanitized.contains("[REDACTED_JWT]"));
    assert!(!sanitized.contains("eyJ"));
}

#[test]
fn test_sanitize_password_in_error() {
    let message = "Connection failed: password=my_secret_123";
    let sanitized = sanitize_error_message(message);
    assert!(sanitized.contains("password=[REDACTED]"));
    assert!(!sanitized.contains("my_secret_123"));
}

#[test]
fn test_sanitize_secret_in_json() {
    let message = r#"Error: password="secret123" token="abc456""#;
    let sanitized = sanitize_error_message(message);
    assert!(
        !sanitized.contains("secret123"),
        "Password should be redacted, got: {}",
        sanitized
    );
    assert!(
        !sanitized.contains("abc456"),
        "Token should be redacted, got: {}",
        sanitized
    );
}

#[test]
fn test_sanitize_certificate_path() {
    let message = "Failed to load certificate from /etc/ssl/certs/server.pem";
    let sanitized = sanitize_error_message(message);
    assert!(sanitized.contains("[REDACTED_PATH]"));
    assert!(!sanitized.contains("server.pem"));
}

#[test]
fn test_sanitize_multiple_secrets() {
    let message = "Auth failed with password=secret123 and token: abcdef123456 and api_key: testapikey1234567890abcdefghij";
    let sanitized = sanitize_error_message(message);
    assert!(sanitized.contains("password=[REDACTED]"));
    assert!(sanitized.contains("token=[REDACTED]"));
    assert!(!sanitized.contains("secret123"));
    assert!(!sanitized.contains("abcdef123456"));
}

#[test]
fn test_sanitize_no_secrets() {
    let message = "Database connection timeout after 30 seconds";
    let sanitized = sanitize_error_message(message);
    assert_eq!(sanitized, message);
}

#[test]
fn test_sanitize_empty_message() {
    let message = "";
    let sanitized = sanitize_error_message(message);
    assert_eq!(sanitized, "");
}

#[test]
fn test_sanitize_bearer_token() {
    let message = "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0In0.signature validation failed";
    let sanitized = sanitize_error_message(message);
    assert!(sanitized.contains("[REDACTED_JWT]"));
    assert!(!sanitized.contains("eyJ"));
}

// ============================================================================
// AuditLog Structure Tests
// ============================================================================

#[test]
fn test_audit_log_creation() {
    let log = AuditLog {
        id: "test-id-123".to_string(),
        timestamp: 1234567890,
        user_id: Some("user_123".to_string()),
        action: "login".to_string(),
        resource: "/auth/login".to_string(),
        result: AuditResult::Success,
        metadata: AuthMetadata::default(),
        signature: None,
    };

    assert_eq!(log.id(), "test-id-123");
    assert_eq!(log.timestamp(), 1234567890);
    assert_eq!(log.user_id(), Some("user_123"));
    assert_eq!(log.action(), "login");
    assert_eq!(log.resource(), "/auth/login");
    assert!(matches!(log.result(), AuditResult::Success));
}

#[test]
fn test_audit_log_serialization() {
    let log = AuditLog {
        id: "test-id-456".to_string(),
        timestamp: 1234567890,
        user_id: Some("user_456".to_string()),
        action: "logout".to_string(),
        resource: "/auth/logout".to_string(),
        result: AuditResult::Success,
        metadata: AuthMetadata::default(),
        signature: None,
    };

    let json = serde_json::to_string(&log).unwrap();
    assert!(json.contains("\"id\":\"test-id-456\""));
    assert!(json.contains("\"action\":\"logout\""));
    assert!(json.contains("\"status\":\"success\""));
}

#[test]
fn test_audit_log_deserialization() {
    let json = r#"[{
        "id": "test-id-789",
        "timestamp": 1234567890,
        "user_id": "user_789",
        "action": "delete",
        "resource": "/api/resource/123",
        "result": {"status": "failure", "message": "Permission denied"},
        "metadata": {
            "client_ip": "192.168.1.1",
            "user_agent": "Mozilla/5.0",
            "request_id": "req-123",
            "timestamp": 1234567890
        },
        "signature": null
    }]"#;

    let logs = deserialize_audit_logs(json.as_bytes());
    assert_eq!(logs.len(), 1);
    assert_eq!(logs[0].id(), "test-id-789");
    assert_eq!(logs[0].action(), "delete");
    match logs[0].result() {
        AuditResult::Failure { message } => {
            assert_eq!(message, "Permission denied");
        }
        _ => panic!("Expected Failure result"),
    }
}

#[test]
fn test_audit_log_serialization_roundtrip() {
    let original = AuditLog {
        id: "test-id-roundtrip".to_string(),
        timestamp: 1234567890,
        user_id: Some("user_roundtrip".to_string()),
        action: "update".to_string(),
        resource: "/api/resource/456".to_string(),
        result: AuditResult::Failure {
            message: "Validation failed".to_string(),
        },
        metadata: AuthMetadata {
            client_ip: Some("10.0.0.1".to_string()),
            user_agent: Some("TestClient/1.0".to_string()),
            request_id: "req-456".to_string(),
            timestamp: 1234567890,
        },
        signature: Some("test-signature".to_string()),
    };

    let serialized = serialize_audit_logs(std::slice::from_ref(&original));
    let deserialized = deserialize_audit_logs(&serialized);

    assert_eq!(deserialized.len(), 1);
    let log = &deserialized[0];
    assert_eq!(log.id(), original.id());
    assert_eq!(log.timestamp(), original.timestamp());
    assert_eq!(log.user_id(), original.user_id());
    assert_eq!(log.action(), original.action());
    assert_eq!(log.resource(), original.resource());
    assert_eq!(log.metadata().request_id, original.metadata().request_id);
}

// ============================================================================
// Edge Case Tests for sanitize_error_message
// ============================================================================

#[test]
fn test_sanitize_very_long_jwt_token() {
    let long_payload = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9".to_string()
        + ".eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ"
        + &"A".repeat(600)
        + ".SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c";

    let message = format!("Auth error: {}", long_payload);
    let sanitized = sanitize_error_message(&message);

    assert!(sanitized.contains("[REDACTED_JWT]") || sanitized.contains("...[TRUNCATED]"));
    assert!(!sanitized.contains("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9"));
}

#[test]
fn test_sanitize_token_in_url() {
    let message = "Request failed: https://api.example.com/data?token=secret_token_123&user=test";
    let sanitized = sanitize_error_message(message);
    assert!(sanitized.contains("token=[REDACTED]"));
    assert!(!sanitized.contains("secret_token_123"));
}

// ============================================================================
// Signing key environment variable tests
//
// The log() method checks SDFORGE_AUDIT_SIGNING_KEY env var to optionally
// sign audit logs. These tests use #[serial] to safely set/unset the env
// var without interfering with other tests.
// ============================================================================

#[tokio::test]
#[serial_test::serial]
async fn test_log_with_signing_key_generates_signature() {
    // Set a non-empty signing key — log.generate_signature should be called
    // and the resulting log should have a non-None signature.
    std::env::set_var(
        "SDFORGE_AUDIT_SIGNING_KEY",
        "test_signing_key_min_32_bytes_long!!!",
    );

    let logger = AppAuditLogger::with_limit(10);
    let context = AuthContext {
        user_id: Some("signing_user".to_string()),
        permissions: vec![],
        metadata: AuthMetadata::default(),
    };

    logger
        .log(&context, "signed_action", "resource", true, None)
        .await;

    let logs = logger.get_logs("signing_user");
    assert_eq!(logs.len(), 1);
    assert!(
        logs[0].signature.is_some(),
        "Audit log should have a signature when signing key is set"
    );

    // Clean up
    std::env::remove_var("SDFORGE_AUDIT_SIGNING_KEY");
}

#[tokio::test]
#[serial_test::serial]
async fn test_log_with_empty_signing_key_warns() {
    // Set an empty signing key — the empty-key warning branch should be
    // taken and the log should NOT have a signature.
    std::env::set_var("SDFORGE_AUDIT_SIGNING_KEY", "");

    let logger = AppAuditLogger::with_limit(10);
    let context = AuthContext {
        user_id: Some("empty_key_user".to_string()),
        permissions: vec![],
        metadata: AuthMetadata::default(),
    };

    logger
        .log(&context, "unsigned_action", "resource", true, None)
        .await;

    let logs = logger.get_logs("empty_key_user");
    assert_eq!(logs.len(), 1);
    assert!(
        logs[0].signature.is_none(),
        "Audit log should NOT have a signature when signing key is empty"
    );

    // Clean up
    std::env::remove_var("SDFORGE_AUDIT_SIGNING_KEY");
}

#[tokio::test]
#[serial_test::serial]
async fn test_log_without_signing_key_warns() {
    // Ensure the env var is NOT set — the "not set" warning branch should
    // be taken and the log should NOT have a signature.
    std::env::remove_var("SDFORGE_AUDIT_SIGNING_KEY");

    let logger = AppAuditLogger::with_limit(10);
    let context = AuthContext {
        user_id: Some("no_key_user".to_string()),
        permissions: vec![],
        metadata: AuthMetadata::default(),
    };

    logger
        .log(&context, "unsigned_action", "resource", true, None)
        .await;

    let logs = logger.get_logs("no_key_user");
    assert_eq!(logs.len(), 1);
    assert!(
        logs[0].signature.is_none(),
        "Audit log should NOT have a signature when signing key is not set"
    );
}

// ============================================================================
// log() fallback merge tests
//
// log() synchronously merges fallback logs into primary storage before
// sending to the queue. These tests manually populate fallback_logs to
// exercise that merge path (lines 324-333).
// ============================================================================

#[tokio::test]
async fn test_log_merges_fallback_logs_synchronously() {
    let logger = AppAuditLogger::with_limit(100);

    // Manually populate fallback_logs with a serialized AuditLog
    let fallback_log = AuditLog {
        id: "fallback-log-1".to_string(),
        timestamp: chrono::Utc::now().timestamp() - 100,
        user_id: Some("merge_user".to_string()),
        action: "fallback_action".to_string(),
        resource: "fallback_resource".to_string(),
        result: AuditResult::Success,
        metadata: AuthMetadata::default(),
        signature: None,
    };
    logger.fallback_logs.set(
        "merge_user",
        serialize_audit_logs(std::slice::from_ref(&fallback_log)),
    );

    // Verify fallback was stored
    assert!(logger.fallback_logs.get("merge_user").is_some());

    // Now call log() for the same user — this should merge the fallback
    // into primary storage and delete the fallback.
    let context = AuthContext {
        user_id: Some("merge_user".to_string()),
        permissions: vec![],
        metadata: AuthMetadata::default(),
    };
    logger
        .log(&context, "new_action", "new_resource", true, None)
        .await;

    // The fallback should have been merged and deleted
    assert!(
        logger.fallback_logs.get("merge_user").is_none(),
        "Fallback logs should be deleted after merge"
    );

    // Primary storage should contain both the fallback log and the new log
    let logs = logger.get_logs("merge_user");
    assert_eq!(logs.len(), 2, "Should have 2 logs after merge");

    // Verify both logs are present
    let actions: Vec<&str> = logs.iter().map(|l| l.action()).collect();
    assert!(
        actions.contains(&"fallback_action"),
        "Fallback action should be present"
    );
    assert!(
        actions.contains(&"new_action"),
        "New action should be present"
    );
}

#[tokio::test]
async fn test_log_fallback_merge_respects_max_logs() {
    // When merging fallback + primary exceeds max_logs_per_user, the
    // merged result should be truncated.
    let logger = AppAuditLogger::with_limit(2); // Very small limit

    // Populate primary with 1 log
    let primary_log = AuditLog {
        id: "primary-1".to_string(),
        timestamp: chrono::Utc::now().timestamp(),
        user_id: Some("trunc_user".to_string()),
        action: "primary_action".to_string(),
        resource: "res".to_string(),
        result: AuditResult::Success,
        metadata: AuthMetadata::default(),
        signature: None,
    };
    logger
        .logs
        .set("trunc_user", serialize_audit_logs(&[primary_log]));

    // Populate fallback with 2 logs
    let fallback_logs = vec![
        AuditLog {
            id: "fallback-1".to_string(),
            timestamp: chrono::Utc::now().timestamp() - 200,
            user_id: Some("trunc_user".to_string()),
            action: "fb1".to_string(),
            resource: "res".to_string(),
            result: AuditResult::Success,
            metadata: AuthMetadata::default(),
            signature: None,
        },
        AuditLog {
            id: "fallback-2".to_string(),
            timestamp: chrono::Utc::now().timestamp() - 100,
            user_id: Some("trunc_user".to_string()),
            action: "fb2".to_string(),
            resource: "res".to_string(),
            result: AuditResult::Success,
            metadata: AuthMetadata::default(),
            signature: None,
        },
    ];
    logger
        .fallback_logs
        .set("trunc_user", serialize_audit_logs(&fallback_logs));

    // Call log() which merges fallback into primary
    let context = AuthContext {
        user_id: Some("trunc_user".to_string()),
        permissions: vec![],
        metadata: AuthMetadata::default(),
    };
    logger.log(&context, "new", "res", true, None).await;

    // After merge + truncate, should have at most max_logs_per_user (2)
    let logs = logger.get_logs("trunc_user");
    assert!(
        logs.len() <= 2,
        "Logs should be truncated to max_logs_per_user (2), got {}",
        logs.len()
    );
}

// ============================================================================
// get_logs dedup tests
//
// get_logs merges primary and fallback, deduplicating by log ID.
// These tests populate both stores with overlapping IDs (lines 386-389).
// ============================================================================

#[tokio::test]
async fn test_get_logs_deduplicates_by_id() {
    let logger = AppAuditLogger::with_limit(100);

    // Create a log that appears in BOTH primary and fallback (same ID)
    let shared_log = AuditLog {
        id: "shared-id-1".to_string(),
        timestamp: chrono::Utc::now().timestamp(),
        user_id: Some("dedup_user".to_string()),
        action: "shared_action".to_string(),
        resource: "res".to_string(),
        result: AuditResult::Success,
        metadata: AuthMetadata::default(),
        signature: None,
    };

    // A log that only appears in fallback
    let fallback_only = AuditLog {
        id: "fallback-only-1".to_string(),
        timestamp: chrono::Utc::now().timestamp() - 50,
        user_id: Some("dedup_user".to_string()),
        action: "fallback_only_action".to_string(),
        resource: "res".to_string(),
        result: AuditResult::Success,
        metadata: AuthMetadata::default(),
        signature: None,
    };

    // Populate primary with the shared log
    logger.logs.set(
        "dedup_user",
        serialize_audit_logs(std::slice::from_ref(&shared_log)),
    );

    // Populate fallback with both the shared log and the fallback-only log
    logger.fallback_logs.set(
        "dedup_user",
        serialize_audit_logs(&[shared_log, fallback_only]),
    );

    // get_logs should deduplicate: shared log appears once, fallback-only appears once
    let logs = logger.get_logs("dedup_user");
    assert_eq!(
        logs.len(),
        2,
        "Should have 2 logs after dedup (shared + fallback-only), got {}",
        logs.len()
    );

    // Verify the shared log appears only once
    let shared_count = logs.iter().filter(|l| l.id() == "shared-id-1").count();
    assert_eq!(shared_count, 1, "Shared log should appear exactly once");

    // Verify the fallback-only log appears
    let fallback_count = logs.iter().filter(|l| l.id() == "fallback-only-1").count();
    assert_eq!(fallback_count, 1, "Fallback-only log should appear once");
}

#[tokio::test]
async fn test_get_logs_with_only_fallback() {
    let logger = AppAuditLogger::with_limit(100);

    // Populate only fallback (no primary)
    let fallback_log = AuditLog {
        id: "fb-only-2".to_string(),
        timestamp: chrono::Utc::now().timestamp(),
        user_id: Some("fb_user".to_string()),
        action: "fb_action".to_string(),
        resource: "res".to_string(),
        result: AuditResult::Success,
        metadata: AuthMetadata::default(),
        signature: None,
    };
    logger
        .fallback_logs
        .set("fb_user", serialize_audit_logs(&[fallback_log]));

    let logs = logger.get_logs("fb_user");
    assert_eq!(
        logs.len(),
        1,
        "Should return fallback log when primary is empty"
    );
    assert_eq!(logs[0].id(), "fb-only-2");
}

// ============================================================================
// Worker fallback merge tests
//
// The spawned worker task in with_limit() and build() drains the queue and
// merges any remaining fallback logs. Since log() already merges fallback
// synchronously, the worker's fallback merge is only triggered when
// fallback data exists at the time the worker processes a batch. These
// tests manually inject fallback data and send a batch to trigger the
// worker's merge path.
// ============================================================================

#[tokio::test]
async fn test_worker_merges_fallback_from_queue() {
    let logger = AppAuditLogger::with_limit(100);

    // Populate fallback_logs for "worker_user"
    let fallback_log = AuditLog {
        id: "worker-fb-1".to_string(),
        timestamp: chrono::Utc::now().timestamp() - 100,
        user_id: Some("worker_user".to_string()),
        action: "worker_fb_action".to_string(),
        resource: "res".to_string(),
        result: AuditResult::Success,
        metadata: AuthMetadata::default(),
        signature: None,
    };
    logger
        .fallback_logs
        .set("worker_user", serialize_audit_logs(&[fallback_log]));

    // Populate primary with an existing log
    let primary_log = AuditLog {
        id: "worker-primary-1".to_string(),
        timestamp: chrono::Utc::now().timestamp(),
        user_id: Some("worker_user".to_string()),
        action: "worker_primary_action".to_string(),
        resource: "res".to_string(),
        result: AuditResult::Success,
        metadata: AuthMetadata::default(),
        signature: None,
    };
    logger
        .logs
        .set("worker_user", serialize_audit_logs(&[primary_log]));

    // Send a batch to the queue to trigger the worker's fallback merge
    let batch = AuditLogBatch {
        user_id: "worker_user".to_string(),
        log: make_test_audit_log("worker_user", "queued_action"),
    };
    let _ = logger.queue_sender.send(batch).await;

    // Wait for the worker to process the batch
    tokio::time::sleep(tokio::time::Duration::from_millis(200)).await;

    // The worker should have merged the fallback into primary and deleted it
    assert!(
        logger.fallback_logs.get("worker_user").is_none(),
        "Worker should have deleted fallback after merging"
    );

    // Primary should contain the merged logs (primary + fallback)
    let logs = logger.get_logs("worker_user");
    let actions: Vec<&str> = logs.iter().map(|l| l.action()).collect();
    assert!(
        actions.contains(&"worker_fb_action"),
        "Merged logs should contain the fallback action"
    );
}

#[tokio::test]
async fn test_worker_no_fallback_does_nothing() {
    // When there's no fallback data, the worker should just drain the
    // queue without modifying primary storage.
    let logger = AppAuditLogger::with_limit(100);

    // Populate primary only (no fallback)
    let primary_log = AuditLog {
        id: "no-fb-primary".to_string(),
        timestamp: chrono::Utc::now().timestamp(),
        user_id: Some("no_fb_user".to_string()),
        action: "primary_action".to_string(),
        resource: "res".to_string(),
        result: AuditResult::Success,
        metadata: AuthMetadata::default(),
        signature: None,
    };
    logger
        .logs
        .set("no_fb_user", serialize_audit_logs(&[primary_log]));

    // Send a batch
    let batch = AuditLogBatch {
        user_id: "no_fb_user".to_string(),
        log: make_test_audit_log("no_fb_user", "queued"),
    };
    let _ = logger.queue_sender.send(batch).await;

    tokio::time::sleep(tokio::time::Duration::from_millis(200)).await;

    // Primary should be unchanged (worker found no fallback to merge)
    let logs = logger.get_logs("no_fb_user");
    assert_eq!(
        logs.len(),
        1,
        "Primary should have 1 log (no merge occurred)"
    );
}

// ============================================================================
// AuditLogger trait no-runtime path test
//
// The AuditLogger trait impl's log() method checks for a tokio runtime. If
// none is available, it falls back to printing to stderr (lines 498-503).
// This test calls the trait method from a plain std::thread (no runtime).
// ============================================================================

#[tokio::test]
async fn test_audit_logger_trait_no_runtime_path() {
    use crate::security::traits::AuditLogger as AuditLoggerTrait;

    // with_limit() calls tokio::spawn(), so we need a runtime.
    // The spawned thread below has NO runtime, testing the no-runtime fallback path.
    let logger = AppAuditLogger::with_limit(10);
    let log = make_test_audit_log("no_rt_user", "no_runtime_action");

    // Spawn a plain OS thread with NO tokio runtime. The trait impl's
    // log() should detect the missing runtime and print to stderr instead
    // of panicking.
    let logger_clone = logger.clone();
    let handle = std::thread::spawn(move || {
        // This calls the AuditLogger trait method, not the async log()
        AuditLoggerTrait::log(&logger_clone, log);
    });

    // The thread should complete without panicking
    handle.join().expect("Thread should not panic");

    // No logs should be stored (they were dropped to stderr)
    let logs = logger.get_logs("no_rt_user");
    assert_eq!(
        logs.len(),
        0,
        "No logs should be stored when no runtime is available"
    );
}

#[tokio::test]
async fn test_audit_logger_trait_with_runtime_spawns_task() {
    use crate::security::traits::AuditLogger as AuditLoggerTrait;

    let logger = AppAuditLogger::with_limit(10);
    let log = make_test_audit_log("rt_user", "runtime_action");

    // Call the trait method from within a tokio runtime — it should
    // spawn a task that calls the async log() method.
    AuditLoggerTrait::log(&logger, log);

    // Wait for the spawned task to complete
    tokio::time::sleep(tokio::time::Duration::from_millis(200)).await;

    // The log should have been stored
    let logs = logger.get_logs("rt_user");
    assert_eq!(
        logs.len(),
        1,
        "Log should be stored when runtime is available"
    );
    assert_eq!(logs[0].action(), "runtime_action");
}

// ============================================================================
// Channel Full/Closed and semaphore timeout branch tests
//
// log() sends an AuditLogBatch via try_send(). The Full and Closed error
// arms are hard to reach in normal operation because log() also writes
// synchronously and the channel is created by the logger. These tests
// exercise both arms by constructing a logger with a controlled channel
// state (no spawned worker to drain the queue).
// ============================================================================

/// Verify that log() still stores to primary storage when the async queue
/// is full, hitting the `TrySendError::Full(_)` arm (lines 370-373).
///
/// Constructs a logger with a capacity-1 channel that is pre-filled, so
/// the next try_send from log() returns Full. The receiver is held (not
/// dropped) to keep the channel open, and no worker is spawned so the
/// queue stays full deterministically.
#[tokio::test]
async fn test_log_handles_full_queue() {
    let (sender, _receiver) = tokio::sync::mpsc::channel::<AuditLogBatch>(1);

    // Fill the queue to capacity
    let filler = AuditLogBatch {
        user_id: "filler_user".to_string(),
        log: make_test_audit_log("filler_user", "filler"),
    };
    assert!(sender.try_send(filler).is_ok());

    let logger = AppAuditLogger {
        logs: Arc::new(crate::cache::DashMapCache::new()),
        max_logs_per_user: 100,
        semaphore: Arc::new(tokio::sync::Semaphore::new(10)),
        queue_sender: Arc::new(sender),
        fallback_logs: Arc::new(crate::cache::DashMapCache::new()),
        dropped_log_count: Arc::new(std::sync::atomic::AtomicU64::new(0)),
        total_log_count: Arc::new(std::sync::atomic::AtomicU64::new(0)),
    };

    let context = AuthContext {
        user_id: Some("full_user".to_string()),
        permissions: vec![],
        metadata: AuthMetadata::default(),
    };
    logger.log(&context, "full_action", "res", true, None).await;

    // The log should still be stored in primary storage (synchronous path)
    let logs = logger.get_logs("full_user");
    assert_eq!(
        logs.len(),
        1,
        "Log should be stored in primary storage even when queue is full"
    );
    assert_eq!(logs[0].action(), "full_action");
}

/// Verify that log() still stores to primary storage when the async
/// channel is closed (receiver dropped), hitting the
/// `TrySendError::Closed(_)` arm (lines 374-376).
#[tokio::test]
async fn test_log_handles_closed_channel() {
    let (sender, receiver) = tokio::sync::mpsc::channel::<AuditLogBatch>(1);
    drop(receiver); // closes the channel

    let logger = AppAuditLogger {
        logs: Arc::new(crate::cache::DashMapCache::new()),
        max_logs_per_user: 100,
        semaphore: Arc::new(tokio::sync::Semaphore::new(10)),
        queue_sender: Arc::new(sender),
        fallback_logs: Arc::new(crate::cache::DashMapCache::new()),
        dropped_log_count: Arc::new(std::sync::atomic::AtomicU64::new(0)),
        total_log_count: Arc::new(std::sync::atomic::AtomicU64::new(0)),
    };

    let context = AuthContext {
        user_id: Some("closed_user".to_string()),
        permissions: vec![],
        metadata: AuthMetadata::default(),
    };
    logger
        .log(&context, "closed_action", "res", true, None)
        .await;

    // The log should still be stored in primary storage
    let logs = logger.get_logs("closed_user");
    assert_eq!(
        logs.len(),
        1,
        "Log should be stored in primary storage even when channel is closed"
    );
    assert_eq!(logs[0].action(), "closed_action");
}

/// Verify that log() skips storage entirely when the semaphore permit
/// cannot be acquired within the 1-second timeout (lines 271-274).
///
/// Builds a logger with max_concurrent_ops=1, acquires the only permit,
/// then calls log() which must wait and eventually time out.
#[tokio::test]
async fn test_log_skips_when_semaphore_times_out() {
    let logger = AppAuditLogger::builder()
        .max_logs_per_user(100)
        .max_concurrent_ops(1)
        .queue_size(100)
        .build();

    // Acquire the only permit and hold it for the duration of the test
    let _held_permit = logger.semaphore.clone().acquire_owned().await.unwrap();

    let context = AuthContext {
        user_id: Some("timeout_user".to_string()),
        permissions: vec![],
        metadata: AuthMetadata::default(),
    };

    let start = std::time::Instant::now();
    logger
        .log(&context, "timeout_action", "res", true, None)
        .await;
    let elapsed = start.elapsed();

    // Should have waited approximately 1 second (the timeout duration)
    assert!(
        elapsed >= std::time::Duration::from_millis(900),
        "Should have waited ~1s for the semaphore timeout, elapsed: {:?}",
        elapsed
    );

    // No log should be stored because the permit was never acquired
    let logs = logger.get_logs("timeout_user");
    assert_eq!(
        logs.len(),
        0,
        "No log should be stored on semaphore timeout"
    );
}