mockforge-ui 0.3.88

Admin UI for MockForge - web-based interface for managing mock servers
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
//! Audit logging for Admin UI actions
//!
//! This module provides comprehensive audit logging for all administrative actions
//! performed through the Admin UI, ensuring compliance and security monitoring.

use chrono::Utc;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::RwLock;
use tracing::{info, warn};
use uuid::Uuid;

/// Admin action types that should be audited
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum AdminActionType {
    // Configuration changes
    ConfigLatencyUpdated,
    ConfigFaultsUpdated,
    ConfigProxyUpdated,
    ConfigTrafficShapingUpdated,
    ConfigValidationUpdated,

    // Server management
    ServerRestarted,
    ServerShutdown,
    ServerStatusChecked,

    // Log management
    LogsCleared,
    LogsExported,
    LogsFiltered,

    // Fixture management
    FixtureCreated,
    FixtureUpdated,
    FixtureDeleted,
    FixtureBulkDeleted,
    FixtureMoved,

    // Route management
    RouteEnabled,
    RouteDisabled,
    RouteCreated,
    RouteDeleted,
    RouteUpdated,

    // Service management
    ServiceEnabled,
    ServiceDisabled,
    ServiceConfigUpdated,

    // Metrics and monitoring
    MetricsExported,
    MetricsConfigUpdated,

    // User and access management
    UserCreated,
    UserUpdated,
    UserDeleted,
    RoleChanged,
    PermissionGranted,
    PermissionRevoked,

    // System operations
    SystemConfigBackedUp,
    SystemConfigRestored,
    SystemHealthChecked,

    // Security operations
    ApiKeyCreated,
    ApiKeyDeleted,
    ApiKeyRotated,
    SecurityPolicyUpdated,
}

/// Audit log entry for admin actions
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AdminAuditLog {
    /// Unique audit log ID
    pub id: Uuid,
    /// Timestamp of the action
    pub timestamp: chrono::DateTime<Utc>,
    /// Type of action performed
    pub action_type: AdminActionType,
    /// User who performed the action (if authenticated)
    pub user_id: Option<String>,
    /// Username (if available)
    pub username: Option<String>,
    /// IP address of the requester
    pub ip_address: Option<String>,
    /// User agent string
    pub user_agent: Option<String>,
    /// Action description
    pub description: String,
    /// Resource affected (e.g., endpoint path, fixture ID)
    pub resource: Option<String>,
    /// Success status
    pub success: bool,
    /// Error message (if failed)
    pub error_message: Option<String>,
    /// Additional metadata
    pub metadata: Option<serde_json::Value>,
}

/// Audit log storage
#[derive(Debug, Clone)]
pub struct AuditLogStore {
    /// In-memory audit logs (max 10000 entries)
    logs: Arc<RwLock<Vec<AdminAuditLog>>>,
    /// Maximum number of logs to keep
    max_logs: usize,
}

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

impl AuditLogStore {
    /// Create a new audit log store
    pub fn new(max_logs: usize) -> Self {
        Self {
            logs: Arc::new(RwLock::new(Vec::new())),
            max_logs,
        }
    }

    /// Record an audit log entry
    pub async fn record(&self, log: AdminAuditLog) {
        let mut logs = self.logs.write().await;
        logs.push(log.clone());

        // Trim to max_logs if necessary
        if logs.len() > self.max_logs {
            let remove_count = logs.len() - self.max_logs;
            logs.drain(0..remove_count);
        }

        // Log to tracing for external log aggregation
        if log.success {
            info!(
                action = ?log.action_type,
                user_id = ?log.user_id,
                resource = ?log.resource,
                "Admin action: {}",
                log.description
            );
        } else {
            warn!(
                action = ?log.action_type,
                user_id = ?log.user_id,
                resource = ?log.resource,
                error = ?log.error_message,
                "Admin action failed: {}",
                log.description
            );
        }
    }

    /// Get audit logs with optional filtering
    pub async fn get_logs(
        &self,
        action_type: Option<AdminActionType>,
        user_id: Option<&str>,
        limit: Option<usize>,
        offset: Option<usize>,
    ) -> Vec<AdminAuditLog> {
        let logs = self.logs.read().await;
        let mut filtered: Vec<_> = logs.iter().cloned().collect();

        // Filter by action type
        if let Some(action_type) = action_type {
            filtered.retain(|log| log.action_type == action_type);
        }

        // Filter by user ID
        if let Some(user_id) = user_id {
            filtered.retain(|log| log.user_id.as_deref() == Some(user_id));
        }

        // Sort by timestamp (newest first)
        filtered.sort_by(|a, b| b.timestamp.cmp(&a.timestamp));

        // Apply offset
        let start = offset.unwrap_or(0);
        let end = limit.map(|l| start + l).unwrap_or(filtered.len());

        filtered.into_iter().skip(start).take(end - start).collect()
    }

    /// Clear all audit logs
    pub async fn clear(&self) {
        let mut logs = self.logs.write().await;
        logs.clear();
    }

    /// Get statistics about audit logs
    pub async fn get_stats(&self) -> AuditLogStats {
        let logs = self.logs.read().await;

        let total_actions = logs.len();
        let successful_actions = logs.iter().filter(|log| log.success).count();
        let failed_actions = total_actions - successful_actions;

        // Count by action type
        let mut actions_by_type: HashMap<String, usize> = HashMap::new();
        for log in logs.iter() {
            let key = format!("{:?}", log.action_type);
            *actions_by_type.entry(key).or_insert(0) += 1;
        }

        // Get most recent action
        let most_recent = logs.iter().max_by_key(|log| log.timestamp).cloned();

        AuditLogStats {
            total_actions,
            successful_actions,
            failed_actions,
            actions_by_type,
            most_recent_timestamp: most_recent.map(|log| log.timestamp),
        }
    }
}

/// Audit log statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AuditLogStats {
    /// Total number of audit log entries
    pub total_actions: usize,
    /// Number of successful actions
    pub successful_actions: usize,
    /// Number of failed actions
    pub failed_actions: usize,
    /// Count of actions by type
    pub actions_by_type: HashMap<String, usize>,
    /// Timestamp of most recent action
    pub most_recent_timestamp: Option<chrono::DateTime<Utc>>,
}

/// Helper function to create an audit log entry
pub fn create_audit_log(
    action_type: AdminActionType,
    description: String,
    resource: Option<String>,
    success: bool,
    error_message: Option<String>,
    metadata: Option<serde_json::Value>,
) -> AdminAuditLog {
    AdminAuditLog {
        id: Uuid::new_v4(),
        timestamp: Utc::now(),
        action_type,
        user_id: None,    // Will be set by middleware
        username: None,   // Will be set by middleware
        ip_address: None, // Will be set by middleware
        user_agent: None, // Will be set by middleware
        description,
        resource,
        success,
        error_message,
        metadata,
    }
}

/// Global audit log store instance
static GLOBAL_AUDIT_STORE: std::sync::OnceLock<Arc<AuditLogStore>> = std::sync::OnceLock::new();

/// Initialize the global audit log store
pub fn init_global_audit_store(max_logs: usize) -> Arc<AuditLogStore> {
    GLOBAL_AUDIT_STORE
        .get_or_init(|| Arc::new(AuditLogStore::new(max_logs)))
        .clone()
}

/// Get the global audit log store
pub fn get_global_audit_store() -> Option<Arc<AuditLogStore>> {
    GLOBAL_AUDIT_STORE.get().cloned()
}

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

    #[tokio::test]
    async fn test_audit_log_store_creation() {
        let store = AuditLogStore::new(100);
        let stats = store.get_stats().await;
        assert_eq!(stats.total_actions, 0);
        assert_eq!(stats.successful_actions, 0);
        assert_eq!(stats.failed_actions, 0);
    }

    #[tokio::test]
    async fn test_audit_log_store_default() {
        let store = AuditLogStore::default();
        let stats = store.get_stats().await;
        assert_eq!(stats.total_actions, 0);
    }

    #[tokio::test]
    async fn test_record_audit_log() {
        let store = AuditLogStore::new(100);

        let log = create_audit_log(
            AdminActionType::ConfigLatencyUpdated,
            "Updated latency config".to_string(),
            Some("/api/config/latency".to_string()),
            true,
            None,
            None,
        );

        store.record(log).await;

        let stats = store.get_stats().await;
        assert_eq!(stats.total_actions, 1);
        assert_eq!(stats.successful_actions, 1);
        assert_eq!(stats.failed_actions, 0);
    }

    #[tokio::test]
    async fn test_record_failed_audit_log() {
        let store = AuditLogStore::new(100);

        let log = create_audit_log(
            AdminActionType::ConfigLatencyUpdated,
            "Failed to update latency config".to_string(),
            Some("/api/config/latency".to_string()),
            false,
            Some("Permission denied".to_string()),
            None,
        );

        store.record(log).await;

        let stats = store.get_stats().await;
        assert_eq!(stats.total_actions, 1);
        assert_eq!(stats.successful_actions, 0);
        assert_eq!(stats.failed_actions, 1);
    }

    #[tokio::test]
    async fn test_audit_log_with_metadata() {
        let store = AuditLogStore::new(100);

        let metadata = serde_json::json!({
            "old_value": 100,
            "new_value": 200,
            "reason": "Performance optimization"
        });

        let log = create_audit_log(
            AdminActionType::ConfigLatencyUpdated,
            "Updated latency config".to_string(),
            Some("/api/config/latency".to_string()),
            true,
            None,
            Some(metadata.clone()),
        );

        store.record(log).await;

        let logs = store.get_logs(None, None, None, None).await;
        assert_eq!(logs.len(), 1);
        assert_eq!(logs[0].metadata, Some(metadata));
    }

    #[tokio::test]
    async fn test_max_logs_limit() {
        let store = AuditLogStore::new(5);

        // Add 10 logs
        for i in 0..10 {
            let log = create_audit_log(
                AdminActionType::ConfigLatencyUpdated,
                format!("Action {}", i),
                None,
                true,
                None,
                None,
            );
            store.record(log).await;
        }

        let logs = store.get_logs(None, None, None, None).await;
        assert_eq!(logs.len(), 5, "Should only keep last 5 logs");
    }

    #[tokio::test]
    async fn test_get_logs_filtering_by_action_type() {
        let store = AuditLogStore::new(100);

        // Add different action types
        store
            .record(create_audit_log(
                AdminActionType::ConfigLatencyUpdated,
                "Latency updated".to_string(),
                None,
                true,
                None,
                None,
            ))
            .await;

        store
            .record(create_audit_log(
                AdminActionType::FixtureCreated,
                "Fixture created".to_string(),
                None,
                true,
                None,
                None,
            ))
            .await;

        store
            .record(create_audit_log(
                AdminActionType::ConfigLatencyUpdated,
                "Latency updated again".to_string(),
                None,
                true,
                None,
                None,
            ))
            .await;

        let logs = store
            .get_logs(Some(AdminActionType::ConfigLatencyUpdated), None, None, None)
            .await;
        assert_eq!(logs.len(), 2);
        assert!(logs.iter().all(|log| log.action_type == AdminActionType::ConfigLatencyUpdated));
    }

    #[tokio::test]
    async fn test_get_logs_filtering_by_user() {
        let store = AuditLogStore::new(100);

        let mut log1 = create_audit_log(
            AdminActionType::ConfigLatencyUpdated,
            "Action by user1".to_string(),
            None,
            true,
            None,
            None,
        );
        log1.user_id = Some("user1".to_string());
        store.record(log1).await;

        let mut log2 = create_audit_log(
            AdminActionType::FixtureCreated,
            "Action by user2".to_string(),
            None,
            true,
            None,
            None,
        );
        log2.user_id = Some("user2".to_string());
        store.record(log2).await;

        let mut log3 = create_audit_log(
            AdminActionType::ConfigFaultsUpdated,
            "Action by user1".to_string(),
            None,
            true,
            None,
            None,
        );
        log3.user_id = Some("user1".to_string());
        store.record(log3).await;

        let logs = store.get_logs(None, Some("user1"), None, None).await;
        assert_eq!(logs.len(), 2);
        assert!(logs.iter().all(|log| log.user_id.as_deref() == Some("user1")));
    }

    #[tokio::test]
    async fn test_get_logs_with_limit_and_offset() {
        let store = AuditLogStore::new(100);

        // Add 10 logs
        for i in 0..10 {
            let log = create_audit_log(
                AdminActionType::ConfigLatencyUpdated,
                format!("Action {}", i),
                None,
                true,
                None,
                None,
            );
            store.record(log).await;
        }

        // Get logs with limit
        let logs = store.get_logs(None, None, Some(5), None).await;
        assert_eq!(logs.len(), 5);

        // Get logs with offset
        let logs = store.get_logs(None, None, Some(3), Some(2)).await;
        assert_eq!(logs.len(), 3);

        // Get logs with offset beyond limit
        let logs = store.get_logs(None, None, Some(5), Some(8)).await;
        assert_eq!(logs.len(), 2);
    }

    #[tokio::test]
    async fn test_logs_sorted_by_timestamp_newest_first() {
        let store = AuditLogStore::new(100);

        // Add logs with delays
        for i in 0..5 {
            let log = create_audit_log(
                AdminActionType::ConfigLatencyUpdated,
                format!("Action {}", i),
                None,
                true,
                None,
                None,
            );
            store.record(log).await;
            tokio::time::sleep(tokio::time::Duration::from_millis(10)).await;
        }

        let logs = store.get_logs(None, None, None, None).await;
        assert_eq!(logs.len(), 5);

        // Verify newest first
        for i in 0..logs.len() - 1 {
            assert!(logs[i].timestamp >= logs[i + 1].timestamp);
        }
    }

    #[tokio::test]
    async fn test_clear_logs() {
        let store = AuditLogStore::new(100);

        // Add logs
        for _ in 0..5 {
            store
                .record(create_audit_log(
                    AdminActionType::ConfigLatencyUpdated,
                    "Action".to_string(),
                    None,
                    true,
                    None,
                    None,
                ))
                .await;
        }

        let stats_before = store.get_stats().await;
        assert_eq!(stats_before.total_actions, 5);

        store.clear().await;

        let stats_after = store.get_stats().await;
        assert_eq!(stats_after.total_actions, 0);
    }

    #[tokio::test]
    async fn test_audit_stats_actions_by_type() {
        let store = AuditLogStore::new(100);

        // Add various action types
        store
            .record(create_audit_log(
                AdminActionType::ConfigLatencyUpdated,
                "".to_string(),
                None,
                true,
                None,
                None,
            ))
            .await;
        store
            .record(create_audit_log(
                AdminActionType::ConfigLatencyUpdated,
                "".to_string(),
                None,
                true,
                None,
                None,
            ))
            .await;
        store
            .record(create_audit_log(
                AdminActionType::FixtureCreated,
                "".to_string(),
                None,
                true,
                None,
                None,
            ))
            .await;
        store
            .record(create_audit_log(
                AdminActionType::RouteEnabled,
                "".to_string(),
                None,
                true,
                None,
                None,
            ))
            .await;
        store
            .record(create_audit_log(
                AdminActionType::FixtureCreated,
                "".to_string(),
                None,
                true,
                None,
                None,
            ))
            .await;

        let stats = store.get_stats().await;
        assert_eq!(stats.total_actions, 5);
        assert_eq!(stats.actions_by_type.get("ConfigLatencyUpdated"), Some(&2));
        assert_eq!(stats.actions_by_type.get("FixtureCreated"), Some(&2));
        assert_eq!(stats.actions_by_type.get("RouteEnabled"), Some(&1));
    }

    #[tokio::test]
    async fn test_audit_stats_most_recent_timestamp() {
        let store = AuditLogStore::new(100);

        // Add first log
        store
            .record(create_audit_log(
                AdminActionType::ConfigLatencyUpdated,
                "First".to_string(),
                None,
                true,
                None,
                None,
            ))
            .await;

        let stats1 = store.get_stats().await;
        let first_timestamp = stats1.most_recent_timestamp.unwrap();

        // Wait a bit and add another log
        tokio::time::sleep(tokio::time::Duration::from_millis(10)).await;

        store
            .record(create_audit_log(
                AdminActionType::FixtureCreated,
                "Second".to_string(),
                None,
                true,
                None,
                None,
            ))
            .await;

        let stats2 = store.get_stats().await;
        let second_timestamp = stats2.most_recent_timestamp.unwrap();

        assert!(second_timestamp > first_timestamp);
    }

    #[test]
    fn test_create_audit_log_helper() {
        let log = create_audit_log(
            AdminActionType::UserCreated,
            "Created new user".to_string(),
            Some("users/123".to_string()),
            true,
            None,
            Some(serde_json::json!({"username": "testuser"})),
        );

        assert_eq!(log.action_type, AdminActionType::UserCreated);
        assert_eq!(log.description, "Created new user");
        assert_eq!(log.resource, Some("users/123".to_string()));
        assert!(log.success);
        assert_eq!(log.error_message, None);
        assert!(log.metadata.is_some());
        assert_eq!(log.user_id, None);
        assert_eq!(log.username, None);
        assert_eq!(log.ip_address, None);
        assert_eq!(log.user_agent, None);
    }

    #[test]
    fn test_admin_action_type_serialization() {
        let action = AdminActionType::ConfigLatencyUpdated;
        let serialized = serde_json::to_string(&action).unwrap();
        assert_eq!(serialized, "\"config_latency_updated\"");

        let deserialized: AdminActionType = serde_json::from_str(&serialized).unwrap();
        assert_eq!(deserialized, action);
    }

    #[test]
    fn test_audit_log_serialization() {
        let log = AdminAuditLog {
            id: Uuid::new_v4(),
            timestamp: Utc::now(),
            action_type: AdminActionType::FixtureCreated,
            user_id: Some("user123".to_string()),
            username: Some("testuser".to_string()),
            ip_address: Some("192.168.1.1".to_string()),
            user_agent: Some("Mozilla/5.0".to_string()),
            description: "Created fixture".to_string(),
            resource: Some("/fixtures/test".to_string()),
            success: true,
            error_message: None,
            metadata: Some(serde_json::json!({"key": "value"})),
        };

        let serialized = serde_json::to_string(&log).unwrap();
        let deserialized: AdminAuditLog = serde_json::from_str(&serialized).unwrap();

        assert_eq!(deserialized.id, log.id);
        assert_eq!(deserialized.action_type, log.action_type);
        assert_eq!(deserialized.user_id, log.user_id);
        assert_eq!(deserialized.description, log.description);
    }

    #[test]
    fn test_all_admin_action_types_covered() {
        // Test that all action types can be serialized and deserialized
        let actions = vec![
            AdminActionType::ConfigLatencyUpdated,
            AdminActionType::ConfigFaultsUpdated,
            AdminActionType::ConfigProxyUpdated,
            AdminActionType::ConfigTrafficShapingUpdated,
            AdminActionType::ConfigValidationUpdated,
            AdminActionType::ServerRestarted,
            AdminActionType::ServerShutdown,
            AdminActionType::ServerStatusChecked,
            AdminActionType::LogsCleared,
            AdminActionType::LogsExported,
            AdminActionType::LogsFiltered,
            AdminActionType::FixtureCreated,
            AdminActionType::FixtureUpdated,
            AdminActionType::FixtureDeleted,
            AdminActionType::FixtureBulkDeleted,
            AdminActionType::FixtureMoved,
            AdminActionType::RouteEnabled,
            AdminActionType::RouteDisabled,
            AdminActionType::RouteCreated,
            AdminActionType::RouteDeleted,
            AdminActionType::RouteUpdated,
            AdminActionType::ServiceEnabled,
            AdminActionType::ServiceDisabled,
            AdminActionType::ServiceConfigUpdated,
            AdminActionType::MetricsExported,
            AdminActionType::MetricsConfigUpdated,
            AdminActionType::UserCreated,
            AdminActionType::UserUpdated,
            AdminActionType::UserDeleted,
            AdminActionType::RoleChanged,
            AdminActionType::PermissionGranted,
            AdminActionType::PermissionRevoked,
            AdminActionType::SystemConfigBackedUp,
            AdminActionType::SystemConfigRestored,
            AdminActionType::SystemHealthChecked,
            AdminActionType::ApiKeyCreated,
            AdminActionType::ApiKeyDeleted,
            AdminActionType::ApiKeyRotated,
            AdminActionType::SecurityPolicyUpdated,
        ];

        for action in actions {
            let serialized = serde_json::to_string(&action).unwrap();
            let deserialized: AdminActionType = serde_json::from_str(&serialized).unwrap();
            assert_eq!(deserialized, action);
        }
    }

    #[tokio::test]
    async fn test_global_audit_store_initialization() {
        let store1 = init_global_audit_store(100);
        let store2 = get_global_audit_store();

        assert!(store2.is_some());

        // Both should point to the same store
        let store2 = store2.unwrap();

        // Add log via store1
        store1
            .record(create_audit_log(
                AdminActionType::ConfigLatencyUpdated,
                "Test".to_string(),
                None,
                true,
                None,
                None,
            ))
            .await;

        // Should be visible via store2
        let stats = store2.get_stats().await;
        assert_eq!(stats.total_actions, 1);
    }

    #[tokio::test]
    async fn test_concurrent_audit_log_writes() {
        let store = Arc::new(AuditLogStore::new(1000));
        let mut handles = vec![];

        // Spawn multiple tasks writing logs concurrently
        for i in 0..10 {
            let store_clone = store.clone();
            let handle = tokio::spawn(async move {
                for j in 0..10 {
                    let log = create_audit_log(
                        AdminActionType::ConfigLatencyUpdated,
                        format!("Task {} - Log {}", i, j),
                        None,
                        true,
                        None,
                        None,
                    );
                    store_clone.record(log).await;
                }
            });
            handles.push(handle);
        }

        // Wait for all tasks to complete
        for handle in handles {
            handle.await.unwrap();
        }

        let stats = store.get_stats().await;
        assert_eq!(stats.total_actions, 100);
    }
}