hitbox-fn 0.2.1

Function memoization for hitbox caching framework
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
//! Integration tests for #[cached] macro with skip(...) attribute and lifetime support.

use std::time::Duration;

use hitbox::CacheStatus;
use hitbox::policy::PolicyConfig;
use hitbox_derive::{CacheableResponse, cached};
use hitbox_fn::Cache;
use hitbox_moka::MokaBackend;
use serde::{Deserialize, Serialize};

// =============================================================================
// Types without KeyExtract (for testing skip)
// =============================================================================

/// A type that intentionally does NOT implement KeyExtract.
/// This simulates a database connection or request context.
#[derive(Debug)]
pub struct DbConnection {
    _id: u64,
}

impl DbConnection {
    pub fn new(id: u64) -> Self {
        Self { _id: id }
    }
}

// =============================================================================
// Zero-argument cached function
// =============================================================================

/// Function with no arguments at all.
#[cached]
pub async fn no_args_function() -> i64 {
    42
}

// =============================================================================
// Cached functions with skip(...) on macro
// =============================================================================

/// Function with one skipped parameter and one included.
#[cached(prefix = "compute", skip(_request_id))]
pub async fn compute_with_skip(_request_id: String, value: i64) -> i64 {
    value * 2
}

/// Function with multiple parameters, some skipped.
#[cached(prefix = "multi", skip(_trace_id, _span_id))]
pub async fn multi_params(a: i64, _trace_id: String, b: i64, _span_id: String) -> i64 {
    a + b
}

/// Function with all parameters skipped.
#[cached(prefix = "all_skip", skip(_id1, _id2))]
pub async fn all_params_skipped(_id1: String, _id2: String) -> i64 {
    42
}

/// Function with first parameter skipped.
#[cached(prefix = "first_skip", skip(_skip))]
pub async fn first_param_skipped(_skip: i64, keep: i64) -> i64 {
    keep
}

/// Function with last parameter skipped.
#[cached(prefix = "last_skip", skip(_skip))]
pub async fn last_param_skipped(keep: i64, _skip: i64) -> i64 {
    keep
}

/// Function with a skipped parameter that does NOT implement KeyExtract.
/// This proves that skipped parameters don't need KeyExtract bound.
#[cached(prefix = "with_db", skip(_db))]
pub async fn with_db_connection(_db: DbConnection, user_id: i64) -> String {
    format!("user_{}", user_id)
}

// =============================================================================
// Generic type parameter support
// =============================================================================

use hitbox_core::KeyPart;
use hitbox_fn::KeyExtract;

/// A type that implements KeyExtract for use in generic tests.
#[derive(Debug, Clone)]
pub struct TypedId {
    id: i64,
    label: &'static str,
}

impl TypedId {
    pub fn new(id: i64, label: &'static str) -> Self {
        Self { id, label }
    }
}

impl KeyExtract for TypedId {
    fn extract(&self) -> Vec<KeyPart> {
        vec![
            KeyPart::new("label", Some(self.label.to_string())),
            KeyPart::new("id", Some(self.id.to_string())),
        ]
    }
}

/// Function with a generic type parameter.
#[cached]
pub async fn generic_function<T: KeyExtract + Clone + std::fmt::Debug + Send + Sync + 'static>(
    value: T,
) -> String {
    format!("{:?}", value)
}

/// Function with generic type and skipped parameter.
#[cached(skip(_ctx))]
pub async fn generic_with_skip<T: KeyExtract + Clone + std::fmt::Debug + Send + Sync + 'static>(
    _ctx: String,
    value: T,
) -> String {
    format!("{:?}", value)
}

// =============================================================================
// Tests
// =============================================================================

fn create_cache()
-> Cache<MokaBackend, hitbox::concurrency::NoopConcurrencyManager, hitbox_core::DisabledOffload> {
    Cache::builder()
        .backend(MokaBackend::builder().max_entries(100).build())
        .policy(PolicyConfig::builder().ttl(Duration::from_secs(60)).build())
        .build()
}

#[tokio::test]
async fn test_skipped_param_not_in_cache_key() {
    let cache = create_cache();

    // Call with different request_id but same value
    let (r1, c1) = compute_with_skip("req-1".into(), 10)
        .cache(&cache)
        .with_context()
        .await;
    let (r2, c2) = compute_with_skip("req-2".into(), 10)
        .cache(&cache)
        .with_context()
        .await;

    // Both should return same result
    assert_eq!(r1, r2);
    // First should be miss, second should be hit (same cache key)
    assert_eq!(c1.status, CacheStatus::Miss);
    assert_eq!(c2.status, CacheStatus::Hit);
}

#[tokio::test]
async fn test_included_param_affects_cache_key() {
    let cache = create_cache();

    // Call with same request_id but different value
    let (_, c1) = compute_with_skip("req-1".into(), 10)
        .cache(&cache)
        .with_context()
        .await;
    let (_, c2) = compute_with_skip("req-1".into(), 20)
        .cache(&cache)
        .with_context()
        .await;

    // Both should be misses (different cache keys due to different value)
    assert_eq!(c1.status, CacheStatus::Miss);
    assert_eq!(c2.status, CacheStatus::Miss);
}

#[tokio::test]
async fn test_multiple_skipped_params() {
    let cache = create_cache();

    // Call with different trace_id and span_id but same a and b
    let (r1, c1) = multi_params(1, "trace-1".into(), 2, "span-1".into())
        .cache(&cache)
        .with_context()
        .await;

    // Clone cache and use the clone — exercises Clone for Cache
    let cache2 = cache.clone();
    let (r2, c2) = multi_params(1, "trace-2".into(), 2, "span-2".into())
        .cache(&cache2)
        .with_context()
        .await;

    assert_eq!(r1, r2);
    assert_eq!(c1.status, CacheStatus::Miss);
    assert_eq!(c2.status, CacheStatus::Hit);
}

#[tokio::test]
async fn test_multiple_params_different_values() {
    let cache = create_cache();

    // Same trace/span, different a value
    let (_, c1) = multi_params(1, "trace".into(), 2, "span".into())
        .cache(&cache)
        .with_context()
        .await;
    let (_, c2) = multi_params(100, "trace".into(), 2, "span".into())
        .cache(&cache)
        .with_context()
        .await;

    // Different cache keys
    assert_eq!(c1.status, CacheStatus::Miss);
    assert_eq!(c2.status, CacheStatus::Miss);
}

#[tokio::test]
async fn test_all_params_skipped_same_key() {
    let cache = create_cache();

    // All params skipped - any call should hit same key
    let (r1, c1) = all_params_skipped("a".into(), "b".into())
        .cache(&cache)
        .with_context()
        .await;
    let (r2, c2) = all_params_skipped("x".into(), "y".into())
        .cache(&cache)
        .with_context()
        .await;

    assert_eq!(r1, r2);
    assert_eq!(c1.status, CacheStatus::Miss);
    assert_eq!(c2.status, CacheStatus::Hit);
}

#[tokio::test]
async fn test_first_param_skipped() {
    let cache = create_cache();

    let (_, c1) = first_param_skipped(999, 42)
        .cache(&cache)
        .with_context()
        .await;
    let (_, c2) = first_param_skipped(111, 42)
        .cache(&cache)
        .with_context()
        .await;

    // Different first param (skipped) - should hit
    assert_eq!(c1.status, CacheStatus::Miss);
    assert_eq!(c2.status, CacheStatus::Hit);
}

#[tokio::test]
async fn test_last_param_skipped() {
    let cache = create_cache();

    let (_, c1) = last_param_skipped(42, 999)
        .cache(&cache)
        .with_context()
        .await;
    let (_, c2) = last_param_skipped(42, 111)
        .cache(&cache)
        .with_context()
        .await;

    // Different last param (skipped) - should hit
    assert_eq!(c1.status, CacheStatus::Miss);
    assert_eq!(c2.status, CacheStatus::Hit);
}

#[tokio::test]
async fn test_skipped_type_without_key_extract() {
    let cache = create_cache();

    // DbConnection does NOT implement KeyExtract, but can be skipped
    let db1 = DbConnection::new(1);
    let db2 = DbConnection::new(2);

    let (r1, c1) = with_db_connection(db1, 42)
        .cache(&cache)
        .with_context()
        .await;
    let (r2, c2) = with_db_connection(db2, 42)
        .cache(&cache)
        .with_context()
        .await;

    // Same user_id = cache hit, despite different DbConnection
    assert_eq!(r1, r2);
    assert_eq!(c1.status, CacheStatus::Miss);
    assert_eq!(c2.status, CacheStatus::Hit);
}

// =============================================================================
// Generic type parameter tests
// =============================================================================

#[tokio::test]
async fn test_generic_function_same_value() {
    let cache = create_cache();

    let id1 = TypedId::new(42, "user");
    let id2 = TypedId::new(42, "user");

    let (r1, c1) = generic_function(id1).cache(&cache).with_context().await;
    let (r2, c2) = generic_function(id2).cache(&cache).with_context().await;

    // Same value = cache hit
    assert_eq!(r1, r2);
    assert_eq!(c1.status, CacheStatus::Miss);
    assert_eq!(c2.status, CacheStatus::Hit);
}

#[tokio::test]
async fn test_generic_function_different_value() {
    let cache = create_cache();

    let id1 = TypedId::new(1, "user");
    let id2 = TypedId::new(2, "user");

    let (_, c1) = generic_function(id1).cache(&cache).with_context().await;
    let (_, c2) = generic_function(id2).cache(&cache).with_context().await;

    // Different value = cache miss
    assert_eq!(c1.status, CacheStatus::Miss);
    assert_eq!(c2.status, CacheStatus::Miss);
}

#[tokio::test]
async fn test_generic_function_different_label() {
    let cache = create_cache();

    // Same id but different label = different cache key
    let id1 = TypedId::new(42, "user");
    let id2 = TypedId::new(42, "product");

    let (_, c1) = generic_function(id1).cache(&cache).with_context().await;
    let (_, c2) = generic_function(id2).cache(&cache).with_context().await;

    // Different label = cache miss
    assert_eq!(c1.status, CacheStatus::Miss);
    assert_eq!(c2.status, CacheStatus::Miss);
}

#[tokio::test]
async fn test_generic_with_skip() {
    let cache = create_cache();

    let id1 = TypedId::new(42, "user");
    let id2 = TypedId::new(42, "user");

    // Different ctx values should not affect cache key
    let (r1, c1) = generic_with_skip("ctx-1".to_string(), id1)
        .cache(&cache)
        .with_context()
        .await;
    let (r2, c2) = generic_with_skip("ctx-2".to_string(), id2)
        .cache(&cache)
        .with_context()
        .await;

    // Same value, different ctx (skipped) = cache hit
    assert_eq!(r1, r2);
    assert_eq!(c1.status, CacheStatus::Miss);
    assert_eq!(c2.status, CacheStatus::Hit);
}

// =============================================================================
// CacheableResponse skip field tests
// =============================================================================

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, CacheableResponse)]
pub struct AuthResult {
    pub user_id: u64,
    pub permissions: Vec<String>,
    #[cacheable_response(skip)]
    pub access_token: Option<String>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct AuthError;

#[cached]
pub async fn authenticate(user_id: i64) -> Result<AuthResult, AuthError> {
    Ok(AuthResult {
        user_id: user_id as u64,
        permissions: vec!["read".into(), "write".into()],
        access_token: Some("secret-token".into()),
    })
}

#[tokio::test]
async fn test_skipped_response_field_preserved_on_miss() {
    let cache = create_cache();

    let (result, ctx) = authenticate(1).cache(&cache).with_context().await;

    assert_eq!(ctx.status, CacheStatus::Miss);
    let auth = result.unwrap();
    assert_eq!(auth.access_token, Some("secret-token".into()));
    assert_eq!(auth.permissions, vec!["read", "write"]);
}

#[tokio::test]
async fn test_skipped_response_field_default_on_hit() {
    let cache = create_cache();

    // First call — miss, populates cache
    let (r1, c1) = authenticate(2).cache(&cache).with_context().await;
    // Second call — hit, from cache
    let (r2, c2) = authenticate(2).cache(&cache).with_context().await;

    assert_eq!(c1.status, CacheStatus::Miss);
    assert_eq!(c2.status, CacheStatus::Hit);

    // On miss: skipped field preserved
    assert_eq!(
        r1.as_ref().unwrap().access_token,
        Some("secret-token".into())
    );

    // On hit: skipped field is Default (None for Option<String>)
    assert_eq!(r2.as_ref().unwrap().access_token, None);

    // Non-skipped fields are identical
    assert_eq!(r1.as_ref().unwrap().user_id, r2.as_ref().unwrap().user_id);
    assert_eq!(
        r1.as_ref().unwrap().permissions,
        r2.as_ref().unwrap().permissions
    );
}

// =============================================================================
// Skipped field does NOT require Clone
// =============================================================================

/// A type that implements Default but NOT Clone.
#[derive(Debug, Default, PartialEq, Serialize, Deserialize)]
#[cfg_attr(
    feature = "rkyv_format",
    derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
)]
pub struct NonCloneable {
    pub value: String,
}

#[derive(Debug, PartialEq, Serialize, Deserialize, CacheableResponse)]
pub struct ResponseWithNonCloneable {
    pub id: u64,
    #[cacheable_response(skip)]
    pub ctx: NonCloneable,
}

#[cached(prefix = "non_clone")]
pub async fn get_with_non_cloneable(id: i64) -> Result<ResponseWithNonCloneable, AuthError> {
    Ok(ResponseWithNonCloneable {
        id: id as u64,
        ctx: NonCloneable {
            value: "original".into(),
        },
    })
}

#[tokio::test]
async fn test_skipped_field_no_clone_bound() {
    let cache = create_cache();

    // Miss: NonCloneable field preserved despite not implementing Clone
    let (r1, c1) = get_with_non_cloneable(1).cache(&cache).with_context().await;
    assert_eq!(c1.status, CacheStatus::Miss);
    assert_eq!(r1.as_ref().unwrap().ctx.value, "original");

    // Hit: NonCloneable field is Default
    let (r2, c2) = get_with_non_cloneable(1).cache(&cache).with_context().await;
    assert_eq!(c2.status, CacheStatus::Hit);
    assert_eq!(r2.as_ref().unwrap().ctx.value, "");

    // Non-skipped field identical
    assert_eq!(r1.as_ref().unwrap().id, r2.as_ref().unwrap().id);
}

// =============================================================================
// Zero-argument function tests
// =============================================================================

#[tokio::test]
async fn test_zero_args_always_same_key() {
    use std::sync::Arc;

    let cache = Arc::new(create_cache());

    // Using Arc<Cache> exercises CacheAccess for Arc<T>
    let (r1, c1) = no_args_function().cache(&cache).with_context().await;
    let (r2, c2) = no_args_function().cache(&cache).with_context().await;

    assert_eq!(r1, r2);
    assert_eq!(c1.status, CacheStatus::Miss);
    assert_eq!(c2.status, CacheStatus::Hit);
}

#[tokio::test]
async fn test_zero_args_generated_key() {
    use hitbox::Extractor;
    use hitbox_fn::{Arg, Args, FnExtractor, Skipped};

    // Exercise Args::new() and Args::into_inner()
    let args = Args::new(());
    assert_eq!(args.into_inner(), ());

    // Exercise Arg::value()
    let arg = Arg::new("x", 42i64);
    assert_eq!(*arg.value(), 42);

    // Exercise Skipped::value()
    let skipped = Skipped::new("ctx");
    assert_eq!(*skipped.value(), "ctx");

    let extractor = FnExtractor::<Args<()>>::new("no_args_function");
    let (_, key) = extractor.get(Args(())).await.into_cache_key();

    // Zero-arg function should produce key with only the function name
    assert_eq!(key.to_string(), "fn=no_args_function");
}

// =============================================================================
// Passthrough tests (no backend, no policy — direct function call)
// =============================================================================

#[tokio::test]
async fn test_passthrough_no_backend() {
    let result = compute_with_skip("req-1".into(), 10).await;
    assert_eq!(result, 20);
}

#[tokio::test]
async fn test_passthrough_zero_args() {
    let result = no_args_function().await;
    assert_eq!(result, 42);
}

#[tokio::test]
async fn test_passthrough_generic() {
    let id = TypedId::new(42, "user");
    let result = generic_function(id).await;
    assert!(result.contains("42"));
}

// =============================================================================
// Inline .backend().policy() path tests
// =============================================================================

#[tokio::test]
async fn test_inline_backend_policy() {
    let backend = MokaBackend::builder().max_entries(100).build();
    let policy = PolicyConfig::builder().ttl(Duration::from_secs(60)).build();

    let result = compute_with_skip("req-1".into(), 10)
        .backend(backend)
        .policy(policy)
        .await;

    assert_eq!(result, 20);
}

#[tokio::test]
async fn test_inline_backend_policy_with_context() {
    let backend = MokaBackend::builder().max_entries(100).build();
    let policy = PolicyConfig::builder().ttl(Duration::from_secs(60)).build();

    let (result, ctx) = compute_with_skip("req-1".into(), 10)
        .backend(backend)
        .policy(policy)
        .with_context()
        .await;

    assert_eq!(result, 20);
    assert_eq!(ctx.status, CacheStatus::Miss);
}

// =============================================================================
// Spy backend for key inspection
// =============================================================================

mod spy_backend {
    use async_trait::async_trait;
    use dashmap::DashMap;
    use hitbox::backend::{Backend, BackendError, CacheBackend, DeleteStatus};
    use hitbox_backend::format::RonFormat;
    use hitbox_core::{CacheKey, CacheValue, Raw};

    /// A backend that always misses but records keys and values.
    /// Uses RON format for human-readable value inspection.
    pub struct SpyBackend {
        store: DashMap<String, CacheValue<Raw>>,
    }

    impl SpyBackend {
        pub fn new() -> Self {
            Self {
                store: DashMap::new(),
            }
        }

        /// Returns all stored keys as Display strings.
        pub fn keys(&self) -> Vec<String> {
            self.store.iter().map(|e| e.key().clone()).collect()
        }

        /// Returns the raw value bytes for a key, deserialized as RON string.
        pub fn value_as_ron(&self, key: &str) -> Option<String> {
            self.store.get(key).map(|entry| {
                let (_, raw) = entry.value().clone().into_parts();
                String::from_utf8(raw.to_vec()).unwrap()
            })
        }
    }

    #[async_trait]
    impl Backend for SpyBackend {
        async fn read(&self, _key: &CacheKey) -> Result<Option<CacheValue<Raw>>, BackendError> {
            Ok(None)
        }

        async fn write(&self, key: &CacheKey, value: CacheValue<Raw>) -> Result<(), BackendError> {
            self.store.insert(key.to_string(), value);
            Ok(())
        }

        async fn remove(&self, _key: &CacheKey) -> Result<DeleteStatus, BackendError> {
            Ok(DeleteStatus::Missing)
        }

        fn value_format(&self) -> &dyn hitbox_backend::format::Format {
            &RonFormat
        }
    }

    impl CacheBackend for SpyBackend {}
}

// =============================================================================
// Key verification tests (using SpyBackend)
// =============================================================================

use spy_backend::SpyBackend;

#[tokio::test]
async fn test_key_zero_args() {
    let cache = Cache::builder()
        .backend(SpyBackend::new())
        .policy(PolicyConfig::builder().ttl(Duration::from_secs(60)).build())
        .build();

    no_args_function().cache(&cache).await;

    let keys = cache.backend().keys();
    assert_eq!(keys.len(), 1);
    assert_eq!(keys[0], "fn=no_args_function");

    // Reconstruct a second cache from accessors of the first one
    let cache2 = Cache::builder()
        .backend(SpyBackend::new())
        .policy(cache.policy().as_ref().clone())
        .concurrency_manager(cache.concurrency_manager().clone())
        .offload(*cache.offload())
        .build();

    no_args_function().cache(&cache2).await;

    let keys2 = cache2.backend().keys();
    assert_eq!(keys2.len(), 1);
    assert_eq!(keys2[0], "fn=no_args_function");
}

#[tokio::test]
async fn test_key_with_skip() {
    let cache = Cache::builder()
        .backend(SpyBackend::new())
        .policy(PolicyConfig::builder().ttl(Duration::from_secs(60)).build())
        .build();

    compute_with_skip("req-1".into(), 10).cache(&cache).await;

    let keys = cache.backend().keys();
    assert_eq!(keys.len(), 1);
    assert_eq!(keys[0], "fn=compute&value=10");
}

#[tokio::test]
async fn test_key_multiple_params_with_skip() {
    let cache = Cache::builder()
        .backend(SpyBackend::new())
        .policy(PolicyConfig::builder().ttl(Duration::from_secs(60)).build())
        .build();

    multi_params(1, "trace-1".into(), 2, "span-1".into())
        .cache(&cache)
        .await;

    let keys = cache.backend().keys();
    assert_eq!(keys.len(), 1);
    assert_eq!(keys[0], "fn=multi&a=1&b=2");
}

// =============================================================================
// Value verification tests (using SpyBackend with RON format)
// =============================================================================

#[tokio::test]
async fn test_value_i64() {
    let cache = Cache::builder()
        .backend(SpyBackend::new())
        .policy(PolicyConfig::builder().ttl(Duration::from_secs(60)).build())
        .build();

    no_args_function().cache(&cache).await;

    let ron = cache.backend().value_as_ron("fn=no_args_function").unwrap();
    assert_eq!(ron, "42");
}

#[tokio::test]
async fn test_value_computed() {
    let cache = Cache::builder()
        .backend(SpyBackend::new())
        .policy(PolicyConfig::builder().ttl(Duration::from_secs(60)).build())
        .build();

    compute_with_skip("req-1".into(), 10).cache(&cache).await;

    let ron = cache.backend().value_as_ron("fn=compute&value=10").unwrap();
    assert_eq!(ron, "20");
}

#[tokio::test]
async fn test_value_string() {
    let cache = Cache::builder()
        .backend(SpyBackend::new())
        .policy(PolicyConfig::builder().ttl(Duration::from_secs(60)).build())
        .build();

    with_db_connection(DbConnection::new(1), 42)
        .cache(&cache)
        .await;

    let ron = cache
        .backend()
        .value_as_ron("fn=with_db&user_id=42")
        .unwrap();
    assert_eq!(ron, "\"user_42\"");
}

#[tokio::test]
async fn test_value_skipped_response_field() {
    let cache = Cache::builder()
        .backend(SpyBackend::new())
        .policy(PolicyConfig::builder().ttl(Duration::from_secs(60)).build())
        .build();

    let _ = authenticate(1).cache(&cache).await;

    let ron = cache
        .backend()
        .value_as_ron("fn=authenticate&user_id=1")
        .unwrap();

    // The cached value should contain user_id and permissions
    assert!(ron.contains("user_id:1"));
    assert!(ron.contains("permissions:[\"read\",\"write\"]"));

    // The skipped field (access_token) is excluded from serialization entirely
    assert!(!ron.contains("access_token"));
}