oauth2-passkey 0.6.0

OAuth2 and Passkey authentication library for Rust web applications
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
use super::*;

#[test]
fn test_make_key() {
    // Given a prefix and key
    let prefix = CachePrefix::session();
    let key = CacheKey::new("user123".to_string()).unwrap();

    // When creating a key
    let result = InMemoryCacheStore::make_key(prefix, key);

    // Then it should be formatted correctly
    assert_eq!(result, "cache:session:user123");
}

/// Test for putting and getting a value in the in-memory cache store.
/// This test checks that a value can be stored and retrieved correctly.
#[tokio::test]
async fn test_put_and_get() {
    // Given an in-memory cache store
    let mut store = InMemoryCacheStore::new();
    let prefix = CachePrefix::new("test".to_string()).unwrap();
    let key = CacheKey::new("key1".to_string()).unwrap();
    let value = CacheData {
        value: "test value".to_string(),
    };

    // When putting a value
    let put_result = store.put(prefix, key, value.clone()).await;

    // Then it should succeed
    assert!(put_result.is_ok());

    // And when getting the value
    let prefix = CachePrefix::new("test".to_string()).unwrap();
    let key = CacheKey::new("key1".to_string()).unwrap();
    let get_result = store.get(prefix, key).await;

    // Then it should return the stored value
    assert!(get_result.is_ok());
    let retrieved = get_result.unwrap();
    assert!(retrieved.is_some());
    assert_eq!(retrieved.unwrap().value, "test value");
}

/// Test for putting a value with TTL in the in-memory cache store.
/// This test checks that a value can be stored with a TTL and retrieved before expiration.
#[tokio::test]
async fn test_put_with_ttl() {
    // Given an in-memory cache store
    let mut store = InMemoryCacheStore::new();
    let prefix = CachePrefix::new("test".to_string()).unwrap();
    let key = CacheKey::new("key2".to_string()).unwrap();
    let value = CacheData {
        value: "test value with ttl".to_string(),
    };

    // When putting a value with TTL
    let put_result = store.put_with_ttl(prefix, key, value.clone(), 60).await;

    // Then it should succeed
    assert!(put_result.is_ok());

    // And when getting the value before expiration
    let prefix = CachePrefix::new("test".to_string()).unwrap();
    let key = CacheKey::new("key2".to_string()).unwrap();
    let get_result = store.get(prefix, key).await;

    // Then it should return the stored value
    assert!(get_result.is_ok());
    let retrieved = get_result.unwrap();
    assert!(retrieved.is_some());
    assert_eq!(retrieved.unwrap().value, "test value with ttl");
}

/// Test for removing a value from the in-memory cache store.
/// This test checks that a value can be removed successfully and that subsequent retrieval returns None.
#[tokio::test]
async fn test_remove() {
    // Given an in-memory cache store with a stored value
    let mut store = InMemoryCacheStore::new();
    let prefix = CachePrefix::new("test".to_string()).unwrap();
    let key = CacheKey::new("key3".to_string()).unwrap();
    let value = CacheData {
        value: "value to remove".to_string(),
    };

    // When storing and then removing a value
    let _ = store.put(prefix, key, value).await;
    let prefix = CachePrefix::new("test".to_string()).unwrap();
    let key = CacheKey::new("key3".to_string()).unwrap();
    let remove_result = store.remove(prefix, key).await;

    // Then the removal should succeed
    assert!(remove_result.is_ok());

    // And when getting the removed value
    let prefix = CachePrefix::new("test".to_string()).unwrap();
    let key = CacheKey::new("key3".to_string()).unwrap();
    let get_result = store.get(prefix, key).await;

    // Then it should return None
    assert!(get_result.is_ok());
    let retrieved = get_result.unwrap();
    assert!(retrieved.is_none());
}

/// Test for getting a non-existent key from the in-memory cache store.
/// This test checks that attempting to retrieve a key that does not exist returns None without error.
#[tokio::test]
async fn test_get_nonexistent_key() {
    // Given an in-memory cache store
    let store = InMemoryCacheStore::new();
    let prefix = CachePrefix::new("test".to_string()).unwrap();
    let key = CacheKey::new("nonexistent".to_string()).unwrap();

    // When getting a non-existent key
    let get_result = store.get(prefix, key).await;

    // Then it should return None without error
    assert!(get_result.is_ok());
    let retrieved = get_result.unwrap();
    assert!(retrieved.is_none());
}

/// Test for removing a key that does not exist in the in-memory cache store.
/// This test checks that removing a non-existent key does not result in an error.
#[tokio::test]
async fn test_multiple_prefixes() {
    // Given an in-memory cache store
    let mut store = InMemoryCacheStore::new();

    // When storing values with different prefixes but same key
    let key1 = CacheKey::new("same_key".to_string()).unwrap();
    let key2 = CacheKey::new("same_key".to_string()).unwrap();
    let value1 = CacheData {
        value: "value for prefix1".to_string(),
    };
    let value2 = CacheData {
        value: "value for prefix2".to_string(),
    };

    let prefix1 = CachePrefix::new("prefix1".to_string()).unwrap();
    let prefix2 = CachePrefix::new("prefix2".to_string()).unwrap();
    let _ = store.put(prefix1, key1, value1).await;
    let _ = store.put(prefix2, key2, value2).await;

    // Then retrieving with different prefixes should get different values
    let prefix1 = CachePrefix::new("prefix1".to_string()).unwrap();
    let prefix2 = CachePrefix::new("prefix2".to_string()).unwrap();
    let key1 = CacheKey::new("same_key".to_string()).unwrap();
    let key2 = CacheKey::new("same_key".to_string()).unwrap();
    let get1 = store.get(prefix1, key1).await.unwrap().unwrap();
    let get2 = store.get(prefix2, key2).await.unwrap().unwrap();

    assert_eq!(get1.value, "value for prefix1");
    assert_eq!(get2.value, "value for prefix2");
}

/// Test for overwriting an existing key in the in-memory cache store.
/// This test checks that when a key is overwritten, the new value is returned upon retrieval.
#[tokio::test]
async fn test_overwrite_existing_key() {
    // Given an in-memory cache store with an existing value
    let mut store = InMemoryCacheStore::new();
    let prefix = CachePrefix::new("test".to_string()).unwrap();
    let key = CacheKey::new("key1".to_string()).unwrap();

    let original_value = CacheData {
        value: "original value".to_string(),
    };
    let new_value = CacheData {
        value: "new value".to_string(),
    };

    // When storing the original value and then overwriting it
    let _ = store.put(prefix, key, original_value).await;
    let prefix = CachePrefix::new("test".to_string()).unwrap();
    let key = CacheKey::new("key1".to_string()).unwrap();
    let _ = store.put(prefix, key, new_value).await;

    // Then the retrieved value should be the new one
    let prefix = CachePrefix::new("test".to_string()).unwrap();
    let key = CacheKey::new("key1".to_string()).unwrap();
    let retrieved = store.get(prefix, key).await.unwrap().unwrap();
    assert_eq!(retrieved.value, "new value");
}

/// Test for removing a non-existent key from the in-memory cache store.
/// This test checks that attempting to remove a key that does not exist does not result in an error.
#[tokio::test]
async fn test_remove_nonexistent_key() {
    // Given an in-memory cache store
    let mut store = InMemoryCacheStore::new();

    // When removing a non-existent key
    let prefix = CachePrefix::new("test".to_string()).unwrap();
    let key = CacheKey::new("nonexistent".to_string()).unwrap();
    let result = store.remove(prefix, key).await;

    // Then it should succeed without error
    assert!(result.is_ok());
}

/// Test for using empty strings as prefix and key in the in-memory cache store.
/// This test checks that the store can handle empty strings correctly.
#[tokio::test]
async fn test_empty_prefix_and_key() {
    // Given an in-memory cache store
    let mut store = InMemoryCacheStore::new();

    let value = CacheData {
        value: "test with empty strings".to_string(),
    };

    // When using empty strings for prefix and key
    let prefix = CachePrefix::new("".to_string()).unwrap();
    let key = CacheKey::new("".to_string()).unwrap();
    let put_result = store.put(prefix, key, value.clone()).await;

    // Then it should work correctly
    assert!(put_result.is_ok());

    let prefix = CachePrefix::new("".to_string()).unwrap();
    let key = CacheKey::new("".to_string()).unwrap();
    let get_result = store.get(prefix, key).await.unwrap().unwrap();
    assert_eq!(get_result.value, "test with empty strings");
}

/// Test that `put()` without TTL creates entries that never expire.
#[tokio::test]
async fn test_put_without_ttl_never_expires() {
    let mut store = InMemoryCacheStore::new();
    let prefix = CachePrefix::new("test".to_string()).unwrap();
    let key = CacheKey::new("no_ttl".to_string()).unwrap();
    let value = CacheData {
        value: "persistent".to_string(),
    };

    store.put(prefix, key, value).await.unwrap();

    // Verify the internal entry has no expiration
    let internal_key = InMemoryCacheStore::make_key(
        CachePrefix::new("test".to_string()).unwrap(),
        CacheKey::new("no_ttl".to_string()).unwrap(),
    );
    let entry = store.entry.get(&internal_key).unwrap();
    assert!(entry.expires_at.is_none(), "put() should set no expiration");
    assert!(!entry.is_expired(), "Entry without TTL should never expire");
}

/// Test that `put_with_ttl()` with non-zero TTL sets an expiration.
#[tokio::test]
async fn test_put_with_ttl_sets_expiration() {
    let mut store = InMemoryCacheStore::new();
    let prefix = CachePrefix::new("test".to_string()).unwrap();
    let key = CacheKey::new("with_ttl".to_string()).unwrap();
    let value = CacheData {
        value: "expires soon".to_string(),
    };

    store.put_with_ttl(prefix, key, value, 300).await.unwrap();

    // Verify the internal entry has an expiration set
    let internal_key = InMemoryCacheStore::make_key(
        CachePrefix::new("test".to_string()).unwrap(),
        CacheKey::new("with_ttl".to_string()).unwrap(),
    );
    let entry = store.entry.get(&internal_key).unwrap();
    assert!(
        entry.expires_at.is_some(),
        "put_with_ttl(300) should set an expiration"
    );
    assert!(
        !entry.is_expired(),
        "Entry with 300s TTL should not be expired yet"
    );
}

/// Test that `put_with_ttl()` with TTL=0 means "no expiration".
#[tokio::test]
async fn test_put_with_ttl_zero_means_no_expiration() {
    let mut store = InMemoryCacheStore::new();
    let prefix = CachePrefix::new("test".to_string()).unwrap();
    let key = CacheKey::new("zero_ttl".to_string()).unwrap();
    let value = CacheData {
        value: "no expiration".to_string(),
    };

    store.put_with_ttl(prefix, key, value, 0).await.unwrap();

    // Verify the internal entry has no expiration (same as put() without TTL)
    let internal_key = InMemoryCacheStore::make_key(
        CachePrefix::new("test".to_string()).unwrap(),
        CacheKey::new("zero_ttl".to_string()).unwrap(),
    );
    let entry = store.entry.get(&internal_key).unwrap();
    assert!(
        entry.expires_at.is_none(),
        "TTL=0 should mean no expiration"
    );
    assert!(!entry.is_expired());

    // Value should be retrievable
    let prefix = CachePrefix::new("test".to_string()).unwrap();
    let key = CacheKey::new("zero_ttl".to_string()).unwrap();
    let result = store.get(prefix, key).await.unwrap();
    assert_eq!(result.unwrap().value, "no expiration");
}

/// Test that `get()` returns `None` for expired entries (lazy expiration).
#[tokio::test]
async fn test_get_returns_none_for_expired_entry() {
    let mut store = InMemoryCacheStore::new();

    // Manually insert an already-expired entry
    let internal_key = InMemoryCacheStore::make_key(
        CachePrefix::new("test".to_string()).unwrap(),
        CacheKey::new("expired".to_string()).unwrap(),
    );
    store.entry.insert(
        internal_key,
        CacheEntry {
            data: CacheData {
                value: "old data".to_string(),
            },
            expires_at: Some(Instant::now() - Duration::from_secs(1)),
        },
    );

    // get() should return None for the expired entry
    let prefix = CachePrefix::new("test".to_string()).unwrap();
    let key = CacheKey::new("expired".to_string()).unwrap();
    let result = store.get(prefix, key).await.unwrap();
    assert!(
        result.is_none(),
        "get() should return None for expired entries"
    );
}

/// Test that `put_if_not_exists()` treats expired entries as non-existent.
#[tokio::test]
async fn test_put_if_not_exists_replaces_expired_entry() {
    let mut store = InMemoryCacheStore::new();

    // Manually insert an already-expired entry
    let internal_key = InMemoryCacheStore::make_key(
        CachePrefix::new("test".to_string()).unwrap(),
        CacheKey::new("contested".to_string()).unwrap(),
    );
    store.entry.insert(
        internal_key,
        CacheEntry {
            data: CacheData {
                value: "expired data".to_string(),
            },
            expires_at: Some(Instant::now() - Duration::from_secs(1)),
        },
    );

    // put_if_not_exists should succeed (expired entry treated as absent)
    let prefix = CachePrefix::new("test".to_string()).unwrap();
    let key = CacheKey::new("contested".to_string()).unwrap();
    let new_value = CacheData {
        value: "fresh data".to_string(),
    };
    let inserted = store
        .put_if_not_exists(prefix, key, new_value, 600)
        .await
        .unwrap();
    assert!(inserted, "Should insert over expired entry");

    // Verify the new value is retrievable
    let prefix = CachePrefix::new("test".to_string()).unwrap();
    let key = CacheKey::new("contested".to_string()).unwrap();
    let result = store.get(prefix, key).await.unwrap();
    assert_eq!(result.unwrap().value, "fresh data");
}

/// Test that `put_if_not_exists()` does NOT replace a live (non-expired) entry.
#[tokio::test]
async fn test_put_if_not_exists_rejects_when_live_entry_exists() {
    let mut store = InMemoryCacheStore::new();
    let prefix = CachePrefix::new("test".to_string()).unwrap();
    let key = CacheKey::new("live".to_string()).unwrap();
    let original = CacheData {
        value: "original".to_string(),
    };

    // Store a live entry with long TTL
    store
        .put_if_not_exists(prefix, key, original, 3600)
        .await
        .unwrap();

    // Attempt to overwrite with put_if_not_exists
    let prefix = CachePrefix::new("test".to_string()).unwrap();
    let key = CacheKey::new("live".to_string()).unwrap();
    let replacement = CacheData {
        value: "replacement".to_string(),
    };
    let inserted = store
        .put_if_not_exists(prefix, key, replacement, 3600)
        .await
        .unwrap();
    assert!(!inserted, "Should NOT insert when live entry exists");

    // Original value should be preserved
    let prefix = CachePrefix::new("test".to_string()).unwrap();
    let key = CacheKey::new("live".to_string()).unwrap();
    let result = store.get(prefix, key).await.unwrap();
    assert_eq!(result.unwrap().value, "original");
}

// Integration tests for the global GENERIC_CACHE_STORE
mod integration_tests {
    use crate::storage::{CacheData, CacheKey, CachePrefix, GENERIC_CACHE_STORE};
    use crate::test_utils::init_test_environment;

    /// Test for the global GENERIC_CACHE_STORE integration.
    /// This test checks that the global cache store can be used to put, get, and remove data.
    /// It verifies that data can be stored and retrieved correctly, and that removing data works as expected.
    #[tokio::test]
    async fn test_cache_store_integration() {
        // Initialize test environment with in-memory stores
        init_test_environment().await;

        let prefix = "integration_test";
        let key = "test_key";
        let value = CacheData {
            value: "integration test value".to_string(),
        };

        // Test storing data in the global cache store
        {
            let mut cache = GENERIC_CACHE_STORE.lock().await;
            let cache_prefix = CachePrefix::new(prefix.to_string()).unwrap();
            let cache_key = CacheKey::new(key.to_string()).unwrap();
            let put_result = cache.put(cache_prefix, cache_key, value.clone()).await;
            assert!(put_result.is_ok(), "Should be able to store data in cache");
        }

        // Test retrieving data from the global cache store
        {
            let cache = GENERIC_CACHE_STORE.lock().await;
            let cache_prefix = CachePrefix::new(prefix.to_string()).unwrap();
            let cache_key = CacheKey::new(key.to_string()).unwrap();
            let get_result = cache.get(cache_prefix, cache_key).await;
            assert!(
                get_result.is_ok(),
                "Should be able to retrieve data from cache"
            );

            let retrieved = get_result.unwrap();
            assert!(retrieved.is_some(), "Data should exist in cache");
            assert_eq!(retrieved.unwrap().value, "integration test value");
        }

        // Test removing data from the global cache store
        {
            let mut cache = GENERIC_CACHE_STORE.lock().await;
            let cache_prefix = CachePrefix::new(prefix.to_string()).unwrap();
            let cache_key = CacheKey::new(key.to_string()).unwrap();
            let remove_result = cache.remove(cache_prefix, cache_key).await;
            assert!(
                remove_result.is_ok(),
                "Should be able to remove data from cache"
            );
        }

        // Verify data was actually removed
        {
            let cache = GENERIC_CACHE_STORE.lock().await;
            let cache_prefix = CachePrefix::new(prefix.to_string()).unwrap();
            let cache_key = CacheKey::new(key.to_string()).unwrap();
            let get_result = cache.get(cache_prefix, cache_key).await;
            assert!(get_result.is_ok(), "Get operation should succeed");
            assert!(
                get_result.unwrap().is_none(),
                "Data should be removed from cache"
            );
        }
    }

    /// Test for concurrent access to the global GENERIC_CACHE_STORE.
    /// This test checks that multiple tasks can access the cache concurrently without issues.
    /// It verifies that data can be stored and retrieved correctly from multiple concurrent tasks.
    #[tokio::test]
    async fn test_cache_store_concurrent_access() {
        // Initialize test environment
        init_test_environment().await;

        let prefix = "concurrent_test";

        // Create multiple concurrent tasks that access the cache
        let mut handles = vec![];

        for i in 0..5 {
            let task_key = format!("key_{i}");
            let task_value = CacheData {
                value: format!("concurrent_value_{i}"),
            };

            let handle = tokio::spawn(async move {
                // Store data
                {
                    let mut cache = GENERIC_CACHE_STORE.lock().await;
                    let cache_prefix = CachePrefix::new(prefix.to_string()).unwrap();
                    let cache_key = CacheKey::new(task_key.clone()).unwrap();
                    cache
                        .put(cache_prefix, cache_key, task_value)
                        .await
                        .unwrap();
                }

                // Retrieve data
                {
                    let cache = GENERIC_CACHE_STORE.lock().await;
                    let cache_prefix = CachePrefix::new(prefix.to_string()).unwrap();
                    let cache_key = CacheKey::new(task_key).unwrap();
                    let result = cache.get(cache_prefix, cache_key).await.unwrap();
                    assert!(result.is_some());
                    result.unwrap().value
                }
            });

            handles.push(handle);
        }

        // Wait for all tasks to complete and verify results
        for (i, handle) in handles.into_iter().enumerate() {
            let result = handle.await.unwrap();
            assert_eq!(result, format!("concurrent_value_{i}"));
        }
    }

    /// Test for prefix isolation in the global GENERIC_CACHE_STORE.
    /// This test checks that different prefixes do not interfere with each other.
    /// It verifies that values stored under different prefixes can coexist without conflict.
    #[tokio::test]
    async fn test_cache_store_prefix_isolation() {
        // Initialize test environment
        init_test_environment().await;

        let key = "shared_key";
        let value1 = CacheData {
            value: "value_for_prefix1".to_string(),
        };
        let value2 = CacheData {
            value: "value_for_prefix2".to_string(),
        };
        let value3 = CacheData {
            value: "value_for_prefix3".to_string(),
        };

        // Store values with different prefixes
        {
            let mut cache = GENERIC_CACHE_STORE.lock().await;
            let prefix1 = CachePrefix::new("prefix1".to_string()).unwrap();
            let prefix2 = CachePrefix::new("prefix2".to_string()).unwrap();
            let prefix3 = CachePrefix::new("prefix3".to_string()).unwrap();
            let key1 = CacheKey::new(key.to_string()).unwrap();
            let key2 = CacheKey::new(key.to_string()).unwrap();
            let key3 = CacheKey::new(key.to_string()).unwrap();
            cache.put(prefix1, key1, value1).await.unwrap();
            cache.put(prefix2, key2, value2).await.unwrap();
            cache.put(prefix3, key3, value3).await.unwrap();
        }

        // Verify each prefix maintains its own value
        {
            let cache = GENERIC_CACHE_STORE.lock().await;

            let prefix1 = CachePrefix::new("prefix1".to_string()).unwrap();
            let prefix2 = CachePrefix::new("prefix2".to_string()).unwrap();
            let prefix3 = CachePrefix::new("prefix3".to_string()).unwrap();
            let key1 = CacheKey::new(key.to_string()).unwrap();
            let key2 = CacheKey::new(key.to_string()).unwrap();
            let key3 = CacheKey::new(key.to_string()).unwrap();

            let result1 = cache.get(prefix1, key1).await.unwrap().unwrap();
            assert_eq!(result1.value, "value_for_prefix1");

            let result2 = cache.get(prefix2, key2).await.unwrap().unwrap();
            assert_eq!(result2.value, "value_for_prefix2");

            let result3 = cache.get(prefix3, key3).await.unwrap().unwrap();
            assert_eq!(result3.value, "value_for_prefix3");
        }

        // Remove from one prefix and verify others are unaffected
        {
            let mut cache = GENERIC_CACHE_STORE.lock().await;
            let prefix2 = CachePrefix::new("prefix2".to_string()).unwrap();
            let key2 = CacheKey::new(key.to_string()).unwrap();
            cache.remove(prefix2, key2).await.unwrap();
        }

        {
            let cache = GENERIC_CACHE_STORE.lock().await;

            let prefix1 = CachePrefix::new("prefix1".to_string()).unwrap();
            let prefix2 = CachePrefix::new("prefix2".to_string()).unwrap();
            let prefix3 = CachePrefix::new("prefix3".to_string()).unwrap();
            let key1 = CacheKey::new(key.to_string()).unwrap();
            let key2 = CacheKey::new(key.to_string()).unwrap();
            let key3 = CacheKey::new(key.to_string()).unwrap();

            // prefix1 and prefix3 should still exist
            assert!(cache.get(prefix1, key1).await.unwrap().is_some());
            assert!(cache.get(prefix3, key3).await.unwrap().is_some());

            // prefix2 should be removed
            assert!(cache.get(prefix2, key2).await.unwrap().is_none());
        }
    }

    /// Test for the TTL behavior in the in-memory cache store.
    /// This test checks that the in-memory store handles TTL values correctly,
    /// including that entries with non-zero TTL are retrievable before expiration
    /// and that TTL=0 means "no expiration".
    #[tokio::test]
    async fn test_cache_store_ttl_behavior() {
        // Initialize test environment
        init_test_environment().await;

        let prefix = "ttl_test";
        let key = "ttl_key";
        let value = CacheData {
            value: "ttl test value".to_string(),
        };

        // Test put_with_ttl with non-zero TTL
        {
            let mut cache = GENERIC_CACHE_STORE.lock().await;
            let cache_prefix = CachePrefix::new(prefix.to_string()).unwrap();
            let cache_key = CacheKey::new(key.to_string()).unwrap();
            let put_result = cache
                .put_with_ttl(cache_prefix, cache_key, value.clone(), 300)
                .await;
            assert!(put_result.is_ok(), "put_with_ttl should succeed");
        }

        // Verify the value is retrievable before expiration
        {
            let cache = GENERIC_CACHE_STORE.lock().await;
            let cache_prefix = CachePrefix::new(prefix.to_string()).unwrap();
            let cache_key = CacheKey::new(key.to_string()).unwrap();
            let get_result = cache.get(cache_prefix, cache_key).await.unwrap();
            assert!(get_result.is_some(), "Value should be stored despite TTL");
            assert_eq!(get_result.unwrap().value, "ttl test value");
        }

        // Test with zero TTL
        let zero_ttl_value = CacheData {
            value: "zero ttl value".to_string(),
        };

        {
            let mut cache = GENERIC_CACHE_STORE.lock().await;
            let cache_prefix = CachePrefix::new(prefix.to_string()).unwrap();
            let cache_key = CacheKey::new("zero_ttl_key".to_string()).unwrap();
            let put_result = cache
                .put_with_ttl(cache_prefix, cache_key, zero_ttl_value, 0)
                .await;
            assert!(
                put_result.is_ok(),
                "put_with_ttl with zero TTL should succeed"
            );
        }

        {
            let cache = GENERIC_CACHE_STORE.lock().await;
            let cache_prefix = CachePrefix::new(prefix.to_string()).unwrap();
            let cache_key = CacheKey::new("zero_ttl_key".to_string()).unwrap();
            let get_result = cache.get(cache_prefix, cache_key).await.unwrap();
            assert!(
                get_result.is_some(),
                "Value should be stored even with zero TTL in memory store"
            );
        }
    }

    /// Test for storing and retrieving large data in the in-memory cache store.
    /// This test checks that the in-memory store can handle large data sizes without issues.
    #[tokio::test]
    async fn test_cache_store_large_data() {
        // Initialize test environment
        init_test_environment().await;

        let prefix = "large_data_test";
        let key = "large_key";

        // Create a large value (1MB of data)
        let large_content = "x".repeat(1024 * 1024);
        let large_value = CacheData {
            value: large_content.clone(),
        };

        // Store large data
        {
            let mut cache = GENERIC_CACHE_STORE.lock().await;
            let cache_prefix = CachePrefix::new(prefix.to_string()).unwrap();
            let cache_key = CacheKey::new(key.to_string()).unwrap();
            let put_result = cache.put(cache_prefix, cache_key, large_value).await;
            assert!(put_result.is_ok(), "Should be able to store large data");
        }

        // Retrieve and verify large data
        {
            let cache = GENERIC_CACHE_STORE.lock().await;
            let cache_prefix = CachePrefix::new(prefix.to_string()).unwrap();
            let cache_key = CacheKey::new(key.to_string()).unwrap();
            let get_result = cache.get(cache_prefix, cache_key).await.unwrap();
            assert!(get_result.is_some(), "Large data should be retrievable");

            let retrieved = get_result.unwrap();
            assert_eq!(
                retrieved.value.len(),
                1024 * 1024,
                "Large data should maintain size"
            );
            assert_eq!(
                retrieved.value, large_content,
                "Large data should maintain content"
            );
        }
    }

    /// Test for storing and retrieving special characters in keys and values.
    /// This test checks that the in-memory cache store can handle special characters correctly.
    #[tokio::test]
    async fn test_cache_store_special_characters() {
        // Initialize test environment
        init_test_environment().await;

        let prefix = "special_chars_test";

        // Test with various special characters in keys and values
        let test_cases = vec![
            ("key_with_spaces", "value with spaces"),
            ("key-with-dashes", "value-with-dashes"),
            ("key_with_émojis", "value with émojis 🚀🔐"),
            ("key/with/slashes", "value/with/slashes"),
            ("key:with:colons", "value:with:colons"),
            ("", "empty_key_test"),  // Empty key
            ("empty_value_key", ""), // Empty value
        ];

        // Store all test cases
        {
            let mut cache = GENERIC_CACHE_STORE.lock().await;
            for (test_key, test_value) in &test_cases {
                let cache_data = CacheData {
                    value: test_value.to_string(),
                };
                let cache_prefix = CachePrefix::new(prefix.to_string()).unwrap();
                let cache_key = CacheKey::new(test_key.to_string()).unwrap();
                let put_result = cache.put(cache_prefix, cache_key, cache_data).await;
                assert!(
                    put_result.is_ok(),
                    "Should handle special characters in key: {test_key}"
                );
            }
        }

        // Retrieve and verify all test cases
        {
            let cache = GENERIC_CACHE_STORE.lock().await;
            for (test_key, expected_value) in &test_cases {
                let cache_prefix = CachePrefix::new(prefix.to_string()).unwrap();
                let cache_key = CacheKey::new(test_key.to_string()).unwrap();
                let get_result = cache.get(cache_prefix, cache_key).await.unwrap();
                assert!(
                    get_result.is_some(),
                    "Should retrieve value for key: {test_key}"
                );

                let retrieved = get_result.unwrap();
                assert_eq!(
                    &retrieved.value, expected_value,
                    "Value should match for key: {test_key}"
                );
            }
        }
    }
}