guardian-db 0.15.0

High-performance, local-first decentralized database built on Rust and Iroh
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
//! Testes robustos para o módulo OptimizedCache
//!
//! Este arquivo contém testes abrangentes para validar:
//! - Criação e configuração do cache
//! - Operações get/put de dados
//! - Compressão e descompressão automática
//! - Estatísticas e métricas
//! - Evicção inteligente
//! - Hash de integridade
//! - Performance e concorrência

use crate::p2p::network::core::optimized_cache::{
    AccessPattern, CacheConfig, CacheEntry, CacheStats, CompressedEntry, MetadataEntry,
    OptimizedCache, PatternType,
};
use bytes::Bytes;
use iroh_blobs::BlobFormat;
use std::time::{Duration, Instant};
use tokio::time::sleep;

/// Helper para criar dados de teste
fn create_test_data(size: usize) -> Bytes {
    Bytes::from(vec![0xAB; size])
}

/// Helper para criar CID de teste
fn create_test_cid(id: u32) -> String {
    format!(
        "bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi_{}",
        id
    )
}

#[tokio::test]
async fn test_cache_creation_with_default_config() {
    let cache = OptimizedCache::new(CacheConfig::default());
    let stats = cache.get_stats().await;

    assert_eq!(stats.data_cache_hits, 0);
    assert_eq!(stats.data_cache_misses, 0);
    assert_eq!(stats.compressed_cache_hits, 0);
    assert_eq!(stats.compressed_cache_misses, 0);
    assert_eq!(stats.total_bytes_cached, 0);
}

#[tokio::test]
async fn test_cache_creation_with_custom_config() {
    let cache_config = CacheConfig {
        max_data_cache_size: 128 * 1024 * 1024,
        max_data_entries: 5000,
        max_compressed_cache_size: 512 * 1024 * 1024,
        max_compressed_entries: 25000,
        default_ttl_secs: 7200,
        compression_threshold: 32 * 1024,
        compression_level: 9,
        eviction_threshold: 0.9,
        enable_access_prediction: false,
    };

    let cache = OptimizedCache::new(cache_config.clone());
    let stats = cache.get_stats().await;

    assert_eq!(stats.data_cache_hits, 0);
    assert_eq!(stats.total_bytes_cached, 0);
}

#[tokio::test]
async fn test_cache_config_default_values() {
    let cache_config = CacheConfig::default();

    assert_eq!(cache_config.max_data_cache_size, 256 * 1024 * 1024);
    assert_eq!(cache_config.max_data_entries, 10_000);
    assert_eq!(cache_config.max_compressed_cache_size, 1024 * 1024 * 1024);
    assert_eq!(cache_config.max_compressed_entries, 50_000);
    assert_eq!(cache_config.default_ttl_secs, 3600);
    assert_eq!(cache_config.compression_threshold, 64 * 1024);
    assert_eq!(cache_config.compression_level, 6);
    assert_eq!(cache_config.eviction_threshold, 0.85);
    assert!(cache_config.enable_access_prediction);
}

#[tokio::test]
async fn test_put_and_get_small_data() {
    let cache = OptimizedCache::new(CacheConfig::default());
    let cid = create_test_cid(1);
    let data = create_test_data(1024); // 1KB - não deve ser comprimido

    // Put
    let put_result = cache.put(&cid, data.clone()).await;
    assert!(put_result.is_ok());

    // Get
    let retrieved = cache.get(&cid).await;
    assert!(retrieved.is_some());
    assert_eq!(retrieved.unwrap(), data);

    // Verifica estatísticas
    let stats = cache.get_stats().await;
    assert_eq!(stats.data_cache_hits, 1);
    assert_eq!(stats.data_cache_misses, 0);
    assert!(stats.total_bytes_cached >= 1024);
}

#[tokio::test]
async fn test_put_and_get_large_data_with_compression() {
    let cache = OptimizedCache::new(CacheConfig::default());
    let cid = create_test_cid(2);
    let data = create_test_data(128 * 1024); // 128KB - deve ser comprimido

    // Put
    let put_result = cache.put(&cid, data.clone()).await;
    assert!(put_result.is_ok());

    // Get (pode vir do cache comprimido ou descomprimido)
    let retrieved = cache.get(&cid).await;
    assert!(retrieved.is_some());
    assert_eq!(retrieved.unwrap(), data);

    // Verifica que houve compressão
    let stats = cache.get_stats().await;
    assert!(
        stats.compressions_count >= 1,
        "Dados grandes devem ser comprimidos"
    );
}

#[tokio::test]
async fn test_cache_miss() {
    let cache = OptimizedCache::new(CacheConfig::default());
    let cid = create_test_cid(999);

    let result = cache.get(&cid).await;
    assert!(result.is_none());

    let stats = cache.get_stats().await;
    assert_eq!(stats.data_cache_misses, 1);
    assert_eq!(stats.compressed_cache_misses, 1);
}

#[tokio::test]
async fn test_multiple_puts_and_gets() {
    let cache = OptimizedCache::new(CacheConfig::default());

    // Coloca 10 entradas
    for i in 0..10 {
        let cid = create_test_cid(i);
        let data = create_test_data(1024 * (i as usize + 1));
        cache.put(&cid, data).await.unwrap();
    }

    // Recupera todas
    for i in 0..10 {
        let cid = create_test_cid(i);
        let result = cache.get(&cid).await;
        assert!(result.is_some(), "CID {} deve estar no cache", i);
    }

    let stats = cache.get_stats().await;
    assert_eq!(stats.data_cache_hits, 10);
}

#[tokio::test]
async fn test_cache_stats_tracking() {
    let cache = OptimizedCache::new(CacheConfig::default());
    let initial_stats = cache.get_stats().await;

    assert_eq!(initial_stats.data_cache_hits, 0);
    assert_eq!(initial_stats.data_cache_misses, 0);
    assert_eq!(initial_stats.total_bytes_cached, 0);

    // Adiciona dados
    let cid = create_test_cid(1);
    let data = create_test_data(2048);
    cache.put(&cid, data).await.unwrap();

    let stats_after_put = cache.get_stats().await;
    assert!(stats_after_put.total_bytes_cached >= 2048);

    // Acessa os dados (hit)
    cache.get(&cid).await;
    let stats_after_get = cache.get_stats().await;
    assert_eq!(stats_after_get.data_cache_hits, 1);

    // Tenta acessar dados inexistentes (miss)
    cache.get("nonexistent_cid").await;
    let stats_after_miss = cache.get_stats().await;
    assert_eq!(stats_after_miss.data_cache_misses, 1);
}

#[tokio::test]
async fn test_hit_rate_calculation() {
    let cache = OptimizedCache::new(CacheConfig::default());

    // Adiciona alguns dados
    for i in 0..5 {
        let cid = create_test_cid(i);
        let data = create_test_data(1024);
        cache.put(&cid, data).await.unwrap();
    }

    // Faz 5 hits
    for i in 0..5 {
        let cid = create_test_cid(i);
        cache.get(&cid).await;
    }

    // Faz 5 misses
    for i in 100..105 {
        let cid = create_test_cid(i);
        cache.get(&cid).await;
    }

    let stats = cache.get_stats().await;
    // Hit rate deve ser 5/(5+10) = 0.33... (5 hits, 10 misses - 5 data + 5 compressed)
    assert!(stats.hit_rate > 0.0 && stats.hit_rate < 1.0);
}

#[tokio::test]
async fn test_compression_threshold() {
    let cache_config = CacheConfig {
        compression_threshold: 10 * 1024, // 10KB
        ..Default::default()
    };
    let cache = OptimizedCache::new(cache_config);

    // Dados pequenos (não deve comprimir)
    let cid_small = create_test_cid(1);
    let small_data = create_test_data(5 * 1024); // 5KB
    cache.put(&cid_small, small_data).await.unwrap();

    // Dados grandes (deve comprimir)
    let cid_large = create_test_cid(2);
    let large_data = create_test_data(20 * 1024); // 20KB
    cache.put(&cid_large, large_data).await.unwrap();

    let stats = cache.get_stats().await;
    assert!(
        stats.compressions_count >= 1,
        "Dados acima do threshold devem ser comprimidos"
    );
}

#[tokio::test]
async fn test_compression_saves_space() {
    let cache = OptimizedCache::new(CacheConfig::default());
    let cid = create_test_cid(1);

    // Dados altamente compressíveis (repetitivos)
    let data = Bytes::from(vec![0x42; 256 * 1024]); // 256KB de dados repetidos

    cache.put(&cid, data.clone()).await.unwrap();

    let stats = cache.get_stats().await;
    assert!(
        stats.bytes_saved_compression > 0,
        "Compressão deve economizar bytes"
    );
    assert_eq!(stats.compressions_count, 1);
}

#[tokio::test]
async fn test_data_integrity_after_compression() {
    let cache = OptimizedCache::new(CacheConfig::default());
    let cid = create_test_cid(1);

    // Dados grandes para forçar compressão
    let original_data = Bytes::from(vec![0x55; 128 * 1024]);

    cache.put(&cid, original_data.clone()).await.unwrap();
    let retrieved_data = cache.get(&cid).await;

    assert!(retrieved_data.is_some());
    assert_eq!(
        retrieved_data.unwrap(),
        original_data,
        "Dados devem ser idênticos após compressão/descompressão"
    );
}

#[tokio::test]
async fn test_clear_cache() {
    let cache = OptimizedCache::new(CacheConfig::default());

    // Adiciona alguns dados
    for i in 0..5 {
        let cid = create_test_cid(i);
        let data = create_test_data(1024);
        cache.put(&cid, data).await.unwrap();
    }

    let stats_before = cache.get_stats().await;
    assert!(stats_before.total_bytes_cached > 0);

    // Limpa o cache
    cache.clear().await.unwrap();

    let stats_after = cache.get_stats().await;
    assert_eq!(stats_after.total_bytes_cached, 0);
    assert_eq!(stats_after.data_cache_hits, 0);
    assert_eq!(stats_after.data_cache_misses, 0);

    // Verifica que os dados realmente foram removidos
    let result = cache.get(&create_test_cid(0)).await;
    assert!(result.is_none());
}

#[tokio::test]
async fn test_concurrent_puts() {
    let cache = std::sync::Arc::new(OptimizedCache::new(CacheConfig::default()));
    let mut handles = vec![];

    // 10 threads colocando dados simultaneamente
    for i in 0..10 {
        let cache_clone = cache.clone();
        let handle = tokio::spawn(async move {
            let cid = create_test_cid(i);
            let data = create_test_data(1024 * (i as usize + 1));
            cache_clone.put(&cid, data).await
        });
        handles.push(handle);
    }

    // Aguarda todas as operações
    for handle in handles {
        assert!(handle.await.unwrap().is_ok());
    }

    let stats = cache.get_stats().await;
    assert!(stats.total_bytes_cached > 0);
}

#[tokio::test]
async fn test_concurrent_gets() {
    let cache = std::sync::Arc::new(OptimizedCache::new(CacheConfig::default()));

    // Adiciona dados primeiro
    for i in 0..10 {
        let cid = create_test_cid(i);
        let data = create_test_data(1024);
        cache.put(&cid, data).await.unwrap();
    }

    let mut handles = vec![];

    // 20 threads lendo simultaneamente
    for i in 0..20 {
        let cache_clone = cache.clone();
        let handle = tokio::spawn(async move {
            let cid = create_test_cid(i % 10); // Acessa os 10 CIDs
            cache_clone.get(&cid).await
        });
        handles.push(handle);
    }

    // Aguarda todas as operações
    let mut hits = 0;
    for handle in handles {
        if handle.await.unwrap().is_some() {
            hits += 1;
        }
    }

    assert_eq!(hits, 20, "Todas as leituras devem ter sucesso");
}

#[tokio::test]
async fn test_cache_stats_default() {
    let stats = CacheStats::default();

    assert_eq!(stats.data_cache_hits, 0);
    assert_eq!(stats.data_cache_misses, 0);
    assert_eq!(stats.compressed_cache_hits, 0);
    assert_eq!(stats.compressed_cache_misses, 0);
    assert_eq!(stats.total_bytes_cached, 0);
    assert_eq!(stats.bytes_saved_compression, 0);
    assert_eq!(stats.bytes_saved_network, 0);
    assert_eq!(stats.avg_access_time_us, 0.0);
    assert_eq!(stats.hit_rate, 0.0);
    assert_eq!(stats.evictions_count, 0);
    assert_eq!(stats.compressions_count, 0);
}

#[tokio::test]
async fn test_cache_entry_structure() {
    let data = create_test_data(1024);
    let now = Instant::now();

    let entry = CacheEntry {
        data: data.clone(),
        created_at: now,
        last_accessed: now,
        access_count: 5,
        priority: 8,
        original_size: 1024,
        integrity_hash: [0xAB; 32],
    };

    assert_eq!(entry.data, data);
    assert_eq!(entry.access_count, 5);
    assert_eq!(entry.priority, 8);
    assert_eq!(entry.original_size, 1024);
}

#[tokio::test]
async fn test_compressed_entry_structure() {
    let compressed_data = create_test_data(512);
    let now = Instant::now();

    let entry = CompressedEntry {
        compressed_data: compressed_data.clone(),
        original_size: 1024,
        compression_level: 6,
        compressed_at: now,
        compression_ratio: 0.5,
    };

    assert_eq!(entry.compressed_data, compressed_data);
    assert_eq!(entry.original_size, 1024);
    assert_eq!(entry.compression_level, 6);
    assert_eq!(entry.compression_ratio, 0.5);
}

#[tokio::test]
async fn test_metadata_entry_structure() {
    let now = Instant::now();

    let metadata = MetadataEntry {
        size: 1024 * 1024,
        format: BlobFormat::Raw,
        providers: vec!["peer1".to_string(), "peer2".to_string()],
        discovered_at: now,
        avg_access_latency_ms: 15.5,
        popularity_score: 0.85,
    };

    assert_eq!(metadata.size, 1024 * 1024);
    assert_eq!(metadata.format, BlobFormat::Raw);
    assert_eq!(metadata.providers.len(), 2);
    assert_eq!(metadata.avg_access_latency_ms, 15.5);
    assert_eq!(metadata.popularity_score, 0.85);
}

#[tokio::test]
async fn test_access_pattern_structure() {
    let pattern = AccessPattern {
        avg_frequency: 10.5,
        peak_hours: vec![9, 10, 11, 14, 15, 16],
        reaccess_probability: 0.75,
        pattern_type: PatternType::Regular,
    };

    assert_eq!(pattern.avg_frequency, 10.5);
    assert_eq!(pattern.peak_hours.len(), 6);
    assert_eq!(pattern.reaccess_probability, 0.75);
    assert_eq!(pattern.pattern_type, PatternType::Regular);
}

#[tokio::test]
async fn test_pattern_type_variants() {
    assert_eq!(PatternType::OneTime, PatternType::OneTime);
    assert_eq!(PatternType::Regular, PatternType::Regular);
    assert_eq!(PatternType::Burst, PatternType::Burst);
    assert_eq!(PatternType::Seasonal, PatternType::Seasonal);

    assert_ne!(PatternType::OneTime, PatternType::Regular);
    assert_ne!(PatternType::Burst, PatternType::Seasonal);
}

#[tokio::test]
async fn test_cache_with_access_prediction_enabled() {
    let cache_config = CacheConfig {
        enable_access_prediction: true,
        ..Default::default()
    };
    let cache = OptimizedCache::new(cache_config);

    let cid = create_test_cid(1);
    let data = create_test_data(1024);

    cache.put(&cid, data).await.unwrap();

    // Acessa múltiplas vezes para criar histórico
    for _ in 0..5 {
        cache.get(&cid).await;
        sleep(Duration::from_millis(10)).await;
    }

    let stats = cache.get_stats().await;
    assert_eq!(stats.data_cache_hits, 5);
}

#[tokio::test]
async fn test_cache_with_access_prediction_disabled() {
    let cache_config = CacheConfig {
        enable_access_prediction: false,
        ..Default::default()
    };
    let cache = OptimizedCache::new(cache_config);

    let cid = create_test_cid(1);
    let data = create_test_data(1024);

    cache.put(&cid, data).await.unwrap();
    cache.get(&cid).await;

    let stats = cache.get_stats().await;
    assert_eq!(stats.data_cache_hits, 1);
}

#[tokio::test]
async fn test_different_compression_levels() {
    // Teste com nível baixo (rápido)
    let config_fast = CacheConfig {
        compression_level: 1,
        compression_threshold: 10 * 1024,
        ..Default::default()
    };
    let cache_fast = OptimizedCache::new(config_fast);

    let cid_fast = create_test_cid(1);
    let data = create_test_data(50 * 1024);
    cache_fast.put(&cid_fast, data.clone()).await.unwrap();

    // Teste com nível alto (melhor compressão)
    let config_best = CacheConfig {
        compression_level: 19,
        compression_threshold: 10 * 1024,
        ..Default::default()
    };
    let cache_best = OptimizedCache::new(config_best);

    let cid_best = create_test_cid(2);
    cache_best.put(&cid_best, data).await.unwrap();

    // Ambos devem funcionar
    assert!(cache_fast.get(&cid_fast).await.is_some());
    assert!(cache_best.get(&cid_best).await.is_some());
}

#[tokio::test]
async fn test_cache_stats_clone() {
    let stats = CacheStats {
        data_cache_hits: 100,
        data_cache_misses: 20,
        compressed_cache_hits: 50,
        compressed_cache_misses: 10,
        total_bytes_cached: 1024 * 1024,
        bytes_saved_compression: 512 * 1024,
        bytes_saved_network: 2 * 1024 * 1024,
        avg_access_time_us: 150.5,
        hit_rate: 0.83,
        evictions_count: 5,
        compressions_count: 25,
    };

    let cloned = stats.clone();

    assert_eq!(cloned.data_cache_hits, 100);
    assert_eq!(cloned.data_cache_misses, 20);
    assert_eq!(cloned.compressed_cache_hits, 50);
    assert_eq!(cloned.total_bytes_cached, 1024 * 1024);
    assert_eq!(cloned.hit_rate, 0.83);
}

#[tokio::test]
async fn test_cache_config_clone() {
    let cache_config = CacheConfig {
        max_data_cache_size: 512 * 1024 * 1024,
        max_data_entries: 20_000,
        max_compressed_cache_size: 2048 * 1024 * 1024,
        max_compressed_entries: 100_000,
        default_ttl_secs: 7200,
        compression_threshold: 128 * 1024,
        compression_level: 9,
        eviction_threshold: 0.9,
        enable_access_prediction: false,
    };

    let cloned = cache_config.clone();

    assert_eq!(cloned.max_data_cache_size, 512 * 1024 * 1024);
    assert_eq!(cloned.max_data_entries, 20_000);
    assert_eq!(cloned.compression_level, 9);
    assert!(!cloned.enable_access_prediction);
}

#[tokio::test]
async fn test_large_data_handling() {
    let cache = OptimizedCache::new(CacheConfig::default());
    let cid = create_test_cid(1);

    // 1MB de dados
    let large_data = create_test_data(1024 * 1024);

    let put_result = cache.put(&cid, large_data.clone()).await;
    assert!(put_result.is_ok());

    let retrieved = cache.get(&cid).await;
    assert!(retrieved.is_some());
    assert_eq!(retrieved.unwrap().len(), large_data.len());
}

#[tokio::test]
async fn test_empty_data() {
    let cache = OptimizedCache::new(CacheConfig::default());
    let cid = create_test_cid(1);
    let empty_data = Bytes::new();

    let put_result = cache.put(&cid, empty_data.clone()).await;
    assert!(put_result.is_ok());

    let retrieved = cache.get(&cid).await;
    assert!(retrieved.is_some());
    assert_eq!(retrieved.unwrap().len(), 0);
}

#[tokio::test]
async fn test_overwrite_existing_entry() {
    let cache = OptimizedCache::new(CacheConfig::default());
    let cid = create_test_cid(1);

    // Primeira versão
    let data_v1 = Bytes::from(vec![0x11; 1024]);
    cache.put(&cid, data_v1).await.unwrap();

    // Segunda versão (sobrescreve)
    let data_v2 = Bytes::from(vec![0x22; 1024]);
    cache.put(&cid, data_v2.clone()).await.unwrap();

    let retrieved = cache.get(&cid).await;
    assert!(retrieved.is_some());
    assert_eq!(retrieved.unwrap(), data_v2);
}

#[tokio::test]
async fn test_access_count_increment() {
    let cache = OptimizedCache::new(CacheConfig::default());
    let cid = create_test_cid(1);
    let data = create_test_data(1024);

    cache.put(&cid, data).await.unwrap();

    // Acessa múltiplas vezes
    for _ in 0..10 {
        cache.get(&cid).await;
    }

    let stats = cache.get_stats().await;
    assert_eq!(stats.data_cache_hits, 10);
}

#[tokio::test]
async fn test_stress_many_small_entries() {
    let cache = OptimizedCache::new(CacheConfig::default());

    // Adiciona 1000 pequenas entradas
    for i in 0..1000 {
        let cid = create_test_cid(i);
        let data = create_test_data(100);
        cache.put(&cid, data).await.unwrap();
    }

    // Verifica algumas aleatórias
    for i in (0..1000).step_by(100) {
        let cid = create_test_cid(i);
        let result = cache.get(&cid).await;
        assert!(result.is_some(), "CID {} deve estar no cache", i);
    }

    let stats = cache.get_stats().await;
    assert!(stats.total_bytes_cached > 0);
}

#[tokio::test]
async fn test_mixed_sized_data() {
    let cache = OptimizedCache::new(CacheConfig::default());

    // Vários tamanhos diferentes
    let sizes = [100, 1024, 10 * 1024, 64 * 1024, 128 * 1024, 256 * 1024];

    for (i, size) in sizes.iter().enumerate() {
        let cid = create_test_cid(i as u32);
        let data = create_test_data(*size);
        cache.put(&cid, data).await.unwrap();
    }

    // Recupera todos
    for (i, _) in sizes.iter().enumerate() {
        let cid = create_test_cid(i as u32);
        let result = cache.get(&cid).await;
        assert!(result.is_some());
    }

    let stats = cache.get_stats().await;
    assert!(
        stats.compressions_count > 0,
        "Alguns dados devem ter sido comprimidos"
    );
}

#[tokio::test]
async fn test_cache_clear_resets_stats() {
    let cache = OptimizedCache::new(CacheConfig::default());

    // Adiciona dados e gera estatísticas
    for i in 0..10 {
        let cid = create_test_cid(i);
        let data = create_test_data(1024);
        cache.put(&cid, data).await.unwrap();
        cache.get(&cid).await;
    }

    let stats_before = cache.get_stats().await;
    assert!(stats_before.data_cache_hits > 0);

    // Limpa
    cache.clear().await.unwrap();

    let stats_after = cache.get_stats().await;
    assert_eq!(stats_after.data_cache_hits, 0);
    assert_eq!(stats_after.data_cache_misses, 0);
    assert_eq!(stats_after.total_bytes_cached, 0);
}

#[tokio::test]
async fn test_avg_access_time_tracking() {
    let cache = OptimizedCache::new(CacheConfig::default());
    let cid = create_test_cid(1);
    let data = create_test_data(1024);

    cache.put(&cid, data).await.unwrap();

    // Faz alguns acessos
    for _ in 0..5 {
        cache.get(&cid).await;
    }

    let stats = cache.get_stats().await;
    // Deve ter registrado tempo de acesso
    assert!(stats.avg_access_time_us >= 0.0);
}

#[tokio::test]
async fn test_bytes_saved_tracking() {
    let cache = OptimizedCache::new(CacheConfig::default());
    let cid = create_test_cid(1);

    // Dados altamente compressíveis
    let data = Bytes::from(vec![0xFF; 256 * 1024]);

    cache.put(&cid, data).await.unwrap();

    let stats = cache.get_stats().await;
    if stats.compressions_count > 0 {
        assert!(
            stats.bytes_saved_compression > 0,
            "Deve ter economizado bytes com compressão"
        );
    }
}

#[tokio::test]
async fn test_compressed_entry_clone() {
    let compressed_data = create_test_data(256);
    let entry = CompressedEntry {
        compressed_data: compressed_data.clone(),
        original_size: 512,
        compression_level: 9,
        compressed_at: Instant::now(),
        compression_ratio: 0.5,
    };

    let cloned = entry.clone();
    assert_eq!(cloned.compressed_data, compressed_data);
    assert_eq!(cloned.original_size, 512);
    assert_eq!(cloned.compression_level, 9);
}

#[tokio::test]
async fn test_metadata_entry_clone() {
    let metadata = MetadataEntry {
        size: 2048,
        format: BlobFormat::Raw,
        providers: vec!["peer1".to_string()],
        discovered_at: Instant::now(),
        avg_access_latency_ms: 25.0,
        popularity_score: 0.6,
    };

    let cloned = metadata.clone();
    assert_eq!(cloned.size, 2048);
    assert_eq!(cloned.format, BlobFormat::Raw);
    assert_eq!(cloned.providers.len(), 1);
}

#[tokio::test]
async fn test_access_pattern_clone() {
    let pattern = AccessPattern {
        avg_frequency: 5.5,
        peak_hours: vec![10, 11, 12],
        reaccess_probability: 0.8,
        pattern_type: PatternType::Burst,
    };

    let cloned = pattern.clone();
    assert_eq!(cloned.avg_frequency, 5.5);
    assert_eq!(cloned.peak_hours, vec![10, 11, 12]);
    assert_eq!(cloned.pattern_type, PatternType::Burst);
}