pmat 3.11.0

PMAT - Zero-config AI context generation and code quality toolkit (CLI, MCP, HTTP)
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
//! Tests for advanced caching strategies

#[cfg_attr(coverage_nightly, coverage(off))]
#[cfg(test)]
mod tests {
    use super::super::advanced_strategies_cache::*;
    use super::super::advanced_strategies_predictor::*;
    use super::super::advanced_strategies_types::*;
    use anyhow::Result;
    use chrono::Utc;
    use rustc_hash::FxHashMap;
    use std::sync::atomic::Ordering;
    use std::sync::Arc;
    use std::time::Duration;

    // === EvictionPolicy tests ===

    #[test]
    fn test_eviction_policy_equality() {
        assert_eq!(EvictionPolicy::LRU, EvictionPolicy::LRU);
        assert_ne!(EvictionPolicy::LRU, EvictionPolicy::LFU);
    }

    #[test]
    fn test_eviction_policy_clone() {
        let policy = EvictionPolicy::Adaptive;
        let cloned = policy;
        assert_eq!(policy, cloned);
    }

    #[test]
    fn test_eviction_policy_debug() {
        let debug = format!("{:?}", EvictionPolicy::TTL);
        assert!(debug.contains("TTL"));
    }

    #[test]
    fn test_eviction_policy_all_variants() {
        let variants = [
            EvictionPolicy::LRU,
            EvictionPolicy::LFU,
            EvictionPolicy::TTL,
            EvictionPolicy::FIFO,
            EvictionPolicy::Random,
            EvictionPolicy::Adaptive,
        ];
        assert_eq!(variants.len(), 6);
    }

    #[test]
    fn test_eviction_policy_serialization() {
        let policy = EvictionPolicy::LRU;
        let json = serde_json::to_string(&policy).unwrap();
        let parsed: EvictionPolicy = serde_json::from_str(&json).unwrap();
        assert_eq!(policy, parsed);
    }

    // === CacheTier tests ===

    #[test]
    fn test_cache_tier_equality() {
        assert_eq!(CacheTier::L1, CacheTier::L1);
        assert_ne!(CacheTier::L1, CacheTier::L2);
    }

    #[test]
    fn test_cache_tier_hash() {
        use std::collections::HashSet;
        let mut set = HashSet::new();
        set.insert(CacheTier::L1);
        set.insert(CacheTier::L2);
        set.insert(CacheTier::L3);
        assert_eq!(set.len(), 3);
    }

    #[test]
    fn test_cache_tier_all_variants() {
        let variants = [CacheTier::L1, CacheTier::L2, CacheTier::L3];
        assert_eq!(variants.len(), 3);
    }

    #[test]
    fn test_cache_tier_serialization() {
        let tier = CacheTier::L2;
        let json = serde_json::to_string(&tier).unwrap();
        let parsed: CacheTier = serde_json::from_str(&json).unwrap();
        assert_eq!(tier, parsed);
    }

    // === AccessPattern tests ===

    #[test]
    fn test_access_pattern_creation() {
        let pattern = AccessPattern {
            frequency: 0.5,
            temporal_locality: 0.7,
            spatial_locality: 0.3,
            entropy: 0.2,
            last_access: Utc::now(),
            access_count: 10,
        };
        assert!((pattern.frequency - 0.5).abs() < 0.001);
        assert_eq!(pattern.access_count, 10);
    }

    #[test]
    fn test_access_pattern_clone() {
        let pattern = AccessPattern {
            frequency: 0.5,
            temporal_locality: 0.7,
            spatial_locality: 0.3,
            entropy: 0.2,
            last_access: Utc::now(),
            access_count: 10,
        };
        let cloned = pattern.clone();
        assert_eq!(pattern.access_count, cloned.access_count);
    }

    #[test]
    fn test_access_pattern_serialization() {
        let pattern = AccessPattern {
            frequency: 0.5,
            temporal_locality: 0.7,
            spatial_locality: 0.3,
            entropy: 0.2,
            last_access: Utc::now(),
            access_count: 10,
        };
        let json = serde_json::to_string(&pattern).unwrap();
        assert!(json.contains("frequency"));
        assert!(json.contains("temporal_locality"));
    }

    // === CacheWarmingConfig tests ===

    #[test]
    fn test_cache_warming_config_default() {
        let config = AdvancedCacheConfig::default();
        assert!(config.warming_config.auto_warm);
        assert!(config.warming_config.dependency_warming);
        assert!(!config.warming_config.warm_patterns.is_empty());
    }

    #[test]
    fn test_cache_warming_config_max_warm_time() {
        let config = AdvancedCacheConfig::default();
        assert_eq!(config.warming_config.max_warm_time, Duration::from_secs(30));
    }

    #[test]
    fn test_cache_warming_config_patterns() {
        let config = AdvancedCacheConfig::default();
        assert!(config
            .warming_config
            .warm_patterns
            .contains(&"**/*.rs".to_string()));
    }

    // === PerformanceConfig tests ===

    #[test]
    fn test_performance_config_default() {
        let config = AdvancedCacheConfig::default();
        assert!(config.performance_config.compression_enabled);
        assert_eq!(config.performance_config.compression_level, 6);
        assert!(config.performance_config.background_cleanup);
        assert!(config.performance_config.stats_enabled);
    }

    #[test]
    fn test_performance_config_cleanup_interval() {
        let config = AdvancedCacheConfig::default();
        assert_eq!(
            config.performance_config.cleanup_interval,
            Duration::from_secs(60)
        );
    }

    // === TierStats tests ===

    #[test]
    fn test_tier_stats_default() {
        let stats = TierStats::default();
        assert_eq!(stats.entry_count, 0);
        assert_eq!(stats.memory_usage, 0);
    }

    #[test]
    fn test_tier_stats_atomic_operations() {
        let stats = TierStats::default();
        stats.hits.fetch_add(5, Ordering::Relaxed);
        assert_eq!(stats.hits.load(Ordering::Relaxed), 5);
    }

    // === PatternStats tests ===

    #[test]
    fn test_pattern_stats_default() {
        let stats = PatternStats::default();
        assert_eq!(stats.avg_frequency, 0.0);
        assert_eq!(stats.avg_temporal_locality, 0.0);
    }

    // === PerformanceStats tests ===

    #[test]
    fn test_performance_stats_default() {
        let stats = PerformanceStats::default();
        assert_eq!(stats.avg_lookup_time, Duration::default());
        assert_eq!(stats.compression_efficiency, 0.0);
    }

    // === WarmingStats tests ===

    #[test]
    fn test_warming_stats_default() {
        let stats = WarmingStats::default();
        assert_eq!(stats.warming_success_rate, 0.0);
        assert_eq!(stats.total_warming_time, Duration::default());
    }

    // === AdaptiveCacheStats tests ===

    #[test]
    fn test_adaptive_cache_stats_default() {
        let stats = AdaptiveCacheStats::default();
        assert!(stats.tier_stats.is_empty());
    }

    // === AdaptiveCache tests ===

    #[tokio::test]
    async fn test_adaptive_cache_basic_operations() -> Result<()> {
        let config = AdvancedCacheConfig::default();
        let cache: AdaptiveCache<String, String> = AdaptiveCache::new(config);

        // Test put and get
        cache.put("key1".to_string(), "value1".to_string()).await?;
        let result = cache.get(&"key1".to_string()).await;
        assert!(result.is_some());
        assert_eq!(result.expect("internal error").as_ref(), "value1");

        Ok(())
    }

    #[tokio::test]
    async fn test_adaptive_cache_remove() -> Result<()> {
        let config = AdvancedCacheConfig::default();
        let cache: AdaptiveCache<String, String> = AdaptiveCache::new(config);

        cache.put("key1".to_string(), "value1".to_string()).await?;
        let removed = cache.remove(&"key1".to_string()).await;
        assert!(removed.is_some());

        let result = cache.get(&"key1".to_string()).await;
        assert!(result.is_none());

        Ok(())
    }

    #[tokio::test]
    async fn test_adaptive_cache_clear() -> Result<()> {
        let config = AdvancedCacheConfig::default();
        let cache: AdaptiveCache<String, String> = AdaptiveCache::new(config);

        cache.put("key1".to_string(), "value1".to_string()).await?;
        cache.put("key2".to_string(), "value2".to_string()).await?;

        cache.clear().await?;

        assert!(cache.get(&"key1".to_string()).await.is_none());
        assert!(cache.get(&"key2".to_string()).await.is_none());

        Ok(())
    }

    #[tokio::test]
    async fn test_adaptive_cache_get_stats() -> Result<()> {
        let config = AdvancedCacheConfig::default();
        let cache: AdaptiveCache<String, String> = AdaptiveCache::new(config);

        cache.put("key1".to_string(), "value1".to_string()).await?;
        let _ = cache.get(&"key1".to_string()).await;

        let stats = cache.get_stats();
        // Stats should be retrievable
        assert!(stats.tier_stats.is_empty() || !stats.tier_stats.is_empty());

        Ok(())
    }

    #[tokio::test]
    async fn test_adaptive_cache_miss() -> Result<()> {
        let config = AdvancedCacheConfig::default();
        let cache: AdaptiveCache<String, String> = AdaptiveCache::new(config);

        let result = cache.get(&"nonexistent".to_string()).await;
        assert!(result.is_none());

        Ok(())
    }

    #[tokio::test]
    async fn test_adaptive_cache_warm_cache() -> Result<()> {
        let config = AdvancedCacheConfig::default();
        let cache: AdaptiveCache<String, String> = AdaptiveCache::new(config);

        let keys = vec!["key1".to_string(), "key2".to_string()];
        let warmed = cache.warm_cache(keys).await?;
        // Warming may not succeed without actual values
        assert!(warmed >= 0);

        Ok(())
    }

    #[tokio::test]
    async fn test_adaptive_cache_background_maintenance() -> Result<()> {
        let config = AdvancedCacheConfig::default();
        let cache: AdaptiveCache<String, String> = AdaptiveCache::new(config);

        cache.put("key1".to_string(), "value1".to_string()).await?;
        cache.background_maintenance().await?;

        Ok(())
    }

    #[tokio::test]
    async fn test_cache_tiering() -> Result<()> {
        let config = AdvancedCacheConfig::default();
        let cache: AdaptiveCache<String, Vec<u8>> = AdaptiveCache::new(config);

        // Small value should go to L1
        let small_value = vec![0u8; 1024];
        cache.put("small".to_string(), small_value).await?;

        // Large value should go to L3
        let large_value = vec![0u8; 2 * 1024 * 1024];
        cache.put("large".to_string(), large_value).await?;

        // Both should be retrievable
        assert!(cache.get(&"small".to_string()).await.is_some());
        assert!(cache.get(&"large".to_string()).await.is_some());

        Ok(())
    }

    #[tokio::test]
    async fn test_cache_medium_size_to_l2() -> Result<()> {
        let config = AdvancedCacheConfig::default();
        let cache: AdaptiveCache<String, Vec<u8>> = AdaptiveCache::new(config);

        // Medium value (64KB - 1MB) should go to L2
        let medium_value = vec![0u8; 128 * 1024]; // 128KB
        cache.put("medium".to_string(), medium_value).await?;

        assert!(cache.get(&"medium".to_string()).await.is_some());

        Ok(())
    }

    // === Eviction policy tests ===

    #[test]
    fn test_eviction_policies() {
        let mut cache = FxHashMap::default();
        let adaptive_cache: AdaptiveCache<String, String> =
            AdaptiveCache::new(AdvancedCacheConfig::default());

        // Add some test entries
        for i in 0..3 {
            let entry = AdaptiveCacheEntry {
                value: Arc::new(format!("value{}", i)),
                pattern: AccessPattern {
                    frequency: i as f64 * 0.3,
                    temporal_locality: 0.5,
                    spatial_locality: 0.5,
                    entropy: 0.0,
                    last_access: Utc::now(),
                    access_count: i * 10,
                },
                size: 1024,
                tier: CacheTier::L1,
                created_at: Utc::now(),
                expires_at: None,
            };
            cache.insert(format!("key{}", i), entry);
        }

        // Test LRU eviction
        adaptive_cache.evict_lru(&mut cache);
        assert_eq!(cache.len(), 2);

        // Test compression ratio access
        if let Some(_entry) = cache.get("key0") {
            // Compression ratio functionality removed
        }
    }

    #[test]
    fn test_evict_lfu() {
        let mut cache = FxHashMap::default();
        let adaptive_cache: AdaptiveCache<String, String> =
            AdaptiveCache::new(AdvancedCacheConfig::default());

        for i in 0..3 {
            let entry = AdaptiveCacheEntry {
                value: Arc::new(format!("value{}", i)),
                pattern: AccessPattern {
                    frequency: 0.5,
                    temporal_locality: 0.5,
                    spatial_locality: 0.5,
                    entropy: 0.0,
                    last_access: Utc::now(),
                    access_count: (i + 1) * 10, // Different access counts
                },
                size: 1024,
                tier: CacheTier::L1,
                created_at: Utc::now(),
                expires_at: None,
            };
            cache.insert(format!("key{}", i), entry);
        }

        adaptive_cache.evict_lfu(&mut cache);
        assert_eq!(cache.len(), 2);
        // Entry with lowest access count should be evicted
        assert!(cache.get("key0").is_none());
    }

    #[test]
    fn test_evict_fifo() {
        let mut cache = FxHashMap::default();
        let adaptive_cache: AdaptiveCache<String, String> =
            AdaptiveCache::new(AdvancedCacheConfig::default());

        for i in 0..3 {
            let entry = AdaptiveCacheEntry {
                value: Arc::new(format!("value{}", i)),
                pattern: AccessPattern {
                    frequency: 0.5,
                    temporal_locality: 0.5,
                    spatial_locality: 0.5,
                    entropy: 0.0,
                    last_access: Utc::now(),
                    access_count: 10,
                },
                size: 1024,
                tier: CacheTier::L1,
                created_at: Utc::now() + chrono::Duration::seconds(i as i64),
                expires_at: None,
            };
            cache.insert(format!("key{}", i), entry);
        }

        adaptive_cache.evict_fifo(&mut cache);
        assert_eq!(cache.len(), 2);
    }

    #[test]
    fn test_evict_random() {
        let mut cache = FxHashMap::default();
        let adaptive_cache: AdaptiveCache<String, String> =
            AdaptiveCache::new(AdvancedCacheConfig::default());

        for i in 0..3 {
            let entry = AdaptiveCacheEntry {
                value: Arc::new(format!("value{}", i)),
                pattern: AccessPattern {
                    frequency: 0.5,
                    temporal_locality: 0.5,
                    spatial_locality: 0.5,
                    entropy: 0.0,
                    last_access: Utc::now(),
                    access_count: 10,
                },
                size: 1024,
                tier: CacheTier::L1,
                created_at: Utc::now(),
                expires_at: None,
            };
            cache.insert(format!("key{}", i), entry);
        }

        adaptive_cache.evict_random(&mut cache);
        assert_eq!(cache.len(), 2);
    }

    #[test]
    fn test_evict_adaptive() {
        let mut cache = FxHashMap::default();
        let adaptive_cache: AdaptiveCache<String, String> =
            AdaptiveCache::new(AdvancedCacheConfig::default());

        for i in 0..3 {
            let entry = AdaptiveCacheEntry {
                value: Arc::new(format!("value{}", i)),
                pattern: AccessPattern {
                    frequency: i as f64 * 0.3,
                    temporal_locality: i as f64 * 0.2,
                    spatial_locality: 0.5,
                    entropy: 0.0,
                    last_access: Utc::now(),
                    access_count: i * 10,
                },
                size: 1024,
                tier: CacheTier::L1,
                created_at: Utc::now(),
                expires_at: None,
            };
            cache.insert(format!("key{}", i), entry);
        }

        adaptive_cache.evict_adaptive(&mut cache);
        assert_eq!(cache.len(), 2);
    }

    #[test]
    fn test_evict_ttl() {
        let mut cache = FxHashMap::default();
        let adaptive_cache: AdaptiveCache<String, String> =
            AdaptiveCache::new(AdvancedCacheConfig::default());

        // Add expired entry
        let expired_entry = AdaptiveCacheEntry {
            value: Arc::new("expired".to_string()),
            pattern: AccessPattern {
                frequency: 0.5,
                temporal_locality: 0.5,
                spatial_locality: 0.5,
                entropy: 0.0,
                last_access: Utc::now(),
                access_count: 10,
            },
            size: 1024,
            tier: CacheTier::L1,
            created_at: Utc::now(),
            expires_at: Some(Utc::now() - chrono::Duration::hours(1)),
        };
        cache.insert("expired".to_string(), expired_entry);

        // Add non-expired entry
        let valid_entry = AdaptiveCacheEntry {
            value: Arc::new("valid".to_string()),
            pattern: AccessPattern {
                frequency: 0.5,
                temporal_locality: 0.5,
                spatial_locality: 0.5,
                entropy: 0.0,
                last_access: Utc::now(),
                access_count: 10,
            },
            size: 1024,
            tier: CacheTier::L1,
            created_at: Utc::now(),
            expires_at: Some(Utc::now() + chrono::Duration::hours(1)),
        };
        cache.insert("valid".to_string(), valid_entry);

        adaptive_cache.evict_ttl(&mut cache);
        assert!(cache.get("expired").is_none());
    }

    // === CachePredictor tests ===

    #[test]
    fn test_cache_predictor() {
        let predictor: CachePredictor<String> = CachePredictor::new(0.5);

        // Record some access patterns
        predictor.record_access("file1.rs".to_string());
        predictor.record_access("file2.rs".to_string());
        predictor.record_access("file3.rs".to_string());
        predictor.record_access("file1.rs".to_string());
        predictor.record_access("file2.rs".to_string());

        // Test prediction
        let predictions = predictor.predict_next(&["file1.rs".to_string()]);
        // Predictor may not have enough data yet
        let _ = predictions;
    }

    #[test]
    fn test_cache_predictor_high_confidence() {
        let predictor: CachePredictor<String> = CachePredictor::new(0.9);
        assert!(predictor.predict_value(&"any".to_string()).is_none());
    }

    #[test]
    fn test_cache_predictor_low_confidence() {
        let predictor: CachePredictor<String> = CachePredictor::new(0.1);

        // Record many accesses to build patterns
        for _ in 0..20 {
            predictor.record_access("a".to_string());
            predictor.record_access("b".to_string());
            predictor.record_access("c".to_string());
        }

        let predictions = predictor.predict_next(&["a".to_string()]);
        // Low confidence should produce more predictions
        let _ = predictions;
    }

    #[test]
    fn test_cache_predictor_history_limit() {
        let predictor: CachePredictor<String> = CachePredictor::new(0.5);

        // Record more than 1000 accesses
        for i in 0..1100 {
            predictor.record_access(format!("key{}", i));
        }

        // History should be limited to 1000
        let history_len = predictor.access_history_len();
        assert!(history_len <= 1000);
    }

    // === AdvancedCacheConfig tests ===

    #[test]
    fn test_cache_config() {
        let config = AdvancedCacheConfig::default();
        assert_eq!(config.eviction_policy, EvictionPolicy::Adaptive);
        assert!(config.enable_multi_tier);
        assert!(config.enable_predictive);
        assert!(!config.enable_collaborative); // Should be disabled by default
    }

    #[test]
    fn test_cache_config_tier_limits() {
        let config = AdvancedCacheConfig::default();
        assert_eq!(
            *config.tier_memory_limits.get(&CacheTier::L1).unwrap(),
            64 * 1024 * 1024
        );
        assert_eq!(
            *config.tier_memory_limits.get(&CacheTier::L2).unwrap(),
            256 * 1024 * 1024
        );
        assert_eq!(
            *config.tier_memory_limits.get(&CacheTier::L3).unwrap(),
            1024 * 1024 * 1024
        );
    }

    #[test]
    fn test_cache_config_serialization() {
        let config = AdvancedCacheConfig::default();
        let json = serde_json::to_string(&config).unwrap();
        assert!(json.contains("eviction_policy"));
        assert!(json.contains("enable_multi_tier"));
    }

    // === Helper function tests ===

    #[test]
    fn test_should_promote_high_frequency() {
        let config = AdvancedCacheConfig::default();
        let cache: AdaptiveCache<String, String> = AdaptiveCache::new(config);

        let pattern = AccessPattern {
            frequency: 0.8,
            temporal_locality: 0.3,
            spatial_locality: 0.3,
            entropy: 0.0,
            last_access: Utc::now(),
            access_count: 10,
        };

        assert!(cache.should_promote(&pattern));
    }

    #[test]
    fn test_should_promote_high_temporal_locality() {
        let config = AdvancedCacheConfig::default();
        let cache: AdaptiveCache<String, String> = AdaptiveCache::new(config);

        let pattern = AccessPattern {
            frequency: 0.3,
            temporal_locality: 0.8,
            spatial_locality: 0.3,
            entropy: 0.0,
            last_access: Utc::now(),
            access_count: 10,
        };

        assert!(cache.should_promote(&pattern));
    }

    #[test]
    fn test_should_not_promote_low_scores() {
        let config = AdvancedCacheConfig::default();
        let cache: AdaptiveCache<String, String> = AdaptiveCache::new(config);

        let pattern = AccessPattern {
            frequency: 0.3,
            temporal_locality: 0.3,
            spatial_locality: 0.3,
            entropy: 0.0,
            last_access: Utc::now(),
            access_count: 10,
        };

        assert!(!cache.should_promote(&pattern));
    }

    #[test]
    fn test_calculate_eviction_score() {
        let config = AdvancedCacheConfig::default();
        let cache: AdaptiveCache<String, String> = AdaptiveCache::new(config);

        let pattern = AccessPattern {
            frequency: 0.5,
            temporal_locality: 0.5,
            spatial_locality: 0.5,
            entropy: 0.0,
            last_access: Utc::now(),
            access_count: 10,
        };

        let score = cache.calculate_eviction_score(&pattern);
        assert!(score > 0.0);
        assert!(score <= 1.0);
    }

    #[test]
    fn test_determine_initial_tier_small() {
        let config = AdvancedCacheConfig::default();
        let cache: AdaptiveCache<String, String> = AdaptiveCache::new(config);

        let tier = cache.determine_initial_tier(&"key".to_string(), 1024);
        assert_eq!(tier, CacheTier::L1);
    }

    #[test]
    fn test_determine_initial_tier_medium() {
        let config = AdvancedCacheConfig::default();
        let cache: AdaptiveCache<String, String> = AdaptiveCache::new(config);

        let tier = cache.determine_initial_tier(&"key".to_string(), 128 * 1024);
        assert_eq!(tier, CacheTier::L2);
    }

    #[test]
    fn test_determine_initial_tier_large() {
        let config = AdvancedCacheConfig::default();
        let cache: AdaptiveCache<String, String> = AdaptiveCache::new(config);

        let tier = cache.determine_initial_tier(&"key".to_string(), 2 * 1024 * 1024);
        assert_eq!(tier, CacheTier::L3);
    }

    #[test]
    fn test_get_or_create_pattern_new() {
        let config = AdvancedCacheConfig::default();
        let cache: AdaptiveCache<String, String> = AdaptiveCache::new(config);

        let pattern = cache.get_or_create_pattern(&"new_key".to_string());
        assert_eq!(pattern.access_count, 0);
        assert_eq!(pattern.frequency, 0.0);
    }

    #[test]
    fn test_calculate_expiration_ttl_policy() {
        let mut config = AdvancedCacheConfig::default();
        config.eviction_policy = EvictionPolicy::TTL;
        let cache: AdaptiveCache<String, String> = AdaptiveCache::new(config);

        let expiration_l1 = cache.calculate_expiration(CacheTier::L1);
        assert!(expiration_l1.is_some());

        let expiration_l3 = cache.calculate_expiration(CacheTier::L3);
        assert!(expiration_l3.is_some());

        // L3 should have longer TTL than L1
        assert!(expiration_l3.unwrap() > expiration_l1.unwrap());
    }

    #[test]
    fn test_calculate_expiration_non_ttl_policy() {
        let config = AdvancedCacheConfig::default(); // Adaptive policy
        let cache: AdaptiveCache<String, String> = AdaptiveCache::new(config);

        let expiration = cache.calculate_expiration(CacheTier::L1);
        assert!(expiration.is_none());
    }

    #[tokio::test]
    async fn test_background_maintenance_disabled() -> Result<()> {
        let mut config = AdvancedCacheConfig::default();
        config.performance_config.background_cleanup = false;
        let cache: AdaptiveCache<String, String> = AdaptiveCache::new(config);

        // Should return Ok without doing anything
        cache.background_maintenance().await?;

        Ok(())
    }

    #[test]
    fn test_evict_from_empty_cache() {
        let mut cache: FxHashMap<String, AdaptiveCacheEntry<String>> = FxHashMap::default();
        let adaptive_cache: AdaptiveCache<String, String> =
            AdaptiveCache::new(AdvancedCacheConfig::default());

        // Should not panic on empty cache
        let result = adaptive_cache.evict_from_tier(&mut cache, CacheTier::L1);
        assert!(result.is_ok());
    }
}

#[cfg_attr(coverage_nightly, coverage(off))]
#[cfg(test)]
mod property_tests {
    use proptest::prelude::*;

    proptest! {
        #[test]
        fn basic_property_stability(_input in ".*") {
            // Basic property test for coverage
            prop_assert!(true);
        }

        #[test]
        fn module_consistency_check(_x in 0u32..1000) {
            // Module consistency verification
            prop_assert!(_x < 1001);
        }
    }
}