stillwater 1.0.1

Pragmatic effect composition and validation for Rust - pure core, imperative shell
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
//! Retry policy types and configuration.

use std::time::Duration;

/// A retry policy describing how to retry failed operations.
///
/// Policies are pure data - they describe retry behavior but don't execute it.
/// This makes them easy to test, clone, and inspect.
///
/// # Bounds Behavior
///
/// At least one bound MUST be set, or the policy will panic at construction:
/// - `max_retries`: Maximum number of retry attempts (not including initial attempt)
/// - `max_delay`: Maximum delay cap (also acts as implicit bound for exponential/fibonacci)
///
/// **Rationale**: Unbounded retries are almost always a bug. Requiring explicit bounds
/// forces intentional decisions about retry limits.
///
/// # Examples
///
/// ```rust
/// use stillwater::RetryPolicy;
/// use std::time::Duration;
///
/// // Exponential backoff with max retries
/// let policy = RetryPolicy::exponential(Duration::from_millis(100))
///     .with_max_retries(5);
///
/// assert_eq!(policy.max_retries(), Some(5));
///
/// // Constant delay with max delay cap
/// let policy = RetryPolicy::constant(Duration::from_millis(500))
///     .with_max_delay(Duration::from_secs(30));
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct RetryPolicy {
    strategy: RetryStrategy,
    max_retries: Option<u32>,
    max_delay: Option<Duration>,
    jitter: JitterStrategy,
}

/// The backoff strategy for retry delays.
#[derive(Debug, Clone, PartialEq)]
pub enum RetryStrategy {
    /// Fixed delay between attempts.
    Constant(Duration),
    /// Delay increases linearly: base * (attempt + 1).
    Linear {
        /// Base delay duration.
        base: Duration,
    },
    /// Delay doubles: base * 2^attempt.
    Exponential {
        /// Base delay duration.
        base: Duration,
    },
    /// Delay follows Fibonacci sequence: fib(attempt) * base.
    Fibonacci {
        /// Base delay duration.
        base: Duration,
    },
}

/// Strategy for adding randomness to delays.
#[derive(Debug, Clone, PartialEq, Default)]
pub enum JitterStrategy {
    /// No jitter applied.
    #[default]
    None,
    /// Add ±percentage randomness to delay.
    Proportional(f64),
    /// Random delay between 0 and calculated delay (AWS recommended).
    Full,
    /// Decorrelated jitter (AWS style).
    Decorrelated,
}

/// Information about a retry attempt, passed to hooks.
#[derive(Debug, Clone)]
pub struct RetryEvent<'a, E> {
    /// Which attempt just failed (1-indexed).
    pub attempt: u32,
    /// The error from the failed attempt.
    pub error: &'a E,
    /// Delay before next attempt (if retrying).
    pub next_delay: Option<Duration>,
    /// Total elapsed time since first attempt.
    pub elapsed: Duration,
}

impl RetryPolicy {
    /// Create a policy with constant delay between retries.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use stillwater::RetryPolicy;
    /// use std::time::Duration;
    ///
    /// let policy = RetryPolicy::constant(Duration::from_millis(500))
    ///     .with_max_retries(3);
    ///
    /// // Every retry waits 500ms
    /// assert_eq!(policy.delay_for_attempt(0), Some(Duration::from_millis(500)));
    /// assert_eq!(policy.delay_for_attempt(1), Some(Duration::from_millis(500)));
    /// assert_eq!(policy.delay_for_attempt(2), Some(Duration::from_millis(500)));
    /// assert_eq!(policy.delay_for_attempt(3), None); // max_retries exceeded
    /// ```
    pub fn constant(delay: Duration) -> Self {
        Self {
            strategy: RetryStrategy::Constant(delay),
            max_retries: None,
            max_delay: None,
            jitter: JitterStrategy::None,
        }
    }

    /// Create a policy with linearly increasing delay.
    ///
    /// Delay = base * (attempt + 1)
    ///
    /// # Examples
    ///
    /// ```rust
    /// use stillwater::RetryPolicy;
    /// use std::time::Duration;
    ///
    /// let policy = RetryPolicy::linear(Duration::from_millis(100))
    ///     .with_max_retries(5);
    ///
    /// // Delay increases: 100ms, 200ms, 300ms, 400ms, 500ms
    /// assert_eq!(policy.delay_for_attempt(0), Some(Duration::from_millis(100)));
    /// assert_eq!(policy.delay_for_attempt(1), Some(Duration::from_millis(200)));
    /// assert_eq!(policy.delay_for_attempt(2), Some(Duration::from_millis(300)));
    /// ```
    pub fn linear(base: Duration) -> Self {
        Self {
            strategy: RetryStrategy::Linear { base },
            max_retries: None,
            max_delay: None,
            jitter: JitterStrategy::None,
        }
    }

    /// Create a policy with exponentially increasing delay.
    ///
    /// Delay = base * 2^attempt
    ///
    /// # Examples
    ///
    /// ```rust
    /// use stillwater::RetryPolicy;
    /// use std::time::Duration;
    ///
    /// let policy = RetryPolicy::exponential(Duration::from_millis(100))
    ///     .with_max_retries(5);
    ///
    /// // Delay doubles: 100ms, 200ms, 400ms, 800ms, 1600ms
    /// assert_eq!(policy.delay_for_attempt(0), Some(Duration::from_millis(100)));
    /// assert_eq!(policy.delay_for_attempt(1), Some(Duration::from_millis(200)));
    /// assert_eq!(policy.delay_for_attempt(2), Some(Duration::from_millis(400)));
    /// ```
    pub fn exponential(base: Duration) -> Self {
        Self {
            strategy: RetryStrategy::Exponential { base },
            max_retries: None,
            max_delay: None,
            jitter: JitterStrategy::None,
        }
    }

    /// Create a policy with Fibonacci-based delay.
    ///
    /// Delay = base * fib(attempt + 1)
    ///
    /// # Examples
    ///
    /// ```rust
    /// use stillwater::RetryPolicy;
    /// use std::time::Duration;
    ///
    /// let policy = RetryPolicy::fibonacci(Duration::from_millis(100))
    ///     .with_max_retries(5);
    ///
    /// // Delay follows Fibonacci: 100ms, 100ms, 200ms, 300ms, 500ms
    /// assert_eq!(policy.delay_for_attempt(0), Some(Duration::from_millis(100)));
    /// assert_eq!(policy.delay_for_attempt(1), Some(Duration::from_millis(100)));
    /// assert_eq!(policy.delay_for_attempt(2), Some(Duration::from_millis(200)));
    /// assert_eq!(policy.delay_for_attempt(3), Some(Duration::from_millis(300)));
    /// assert_eq!(policy.delay_for_attempt(4), Some(Duration::from_millis(500)));
    /// ```
    pub fn fibonacci(base: Duration) -> Self {
        Self {
            strategy: RetryStrategy::Fibonacci { base },
            max_retries: None,
            max_delay: None,
            jitter: JitterStrategy::None,
        }
    }

    /// Set the maximum number of retry attempts.
    ///
    /// This does not include the initial attempt. For example, `max_retries(3)`
    /// means up to 4 total attempts (1 initial + 3 retries).
    ///
    /// # Examples
    ///
    /// ```rust
    /// use stillwater::RetryPolicy;
    /// use std::time::Duration;
    ///
    /// let policy = RetryPolicy::exponential(Duration::from_millis(100))
    ///     .with_max_retries(3);
    ///
    /// assert_eq!(policy.max_retries(), Some(3));
    /// ```
    pub fn with_max_retries(mut self, n: u32) -> Self {
        self.max_retries = Some(n);
        self
    }

    /// Set the maximum delay cap.
    ///
    /// Delays will never exceed this value, regardless of the backoff strategy.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use stillwater::RetryPolicy;
    /// use std::time::Duration;
    ///
    /// let policy = RetryPolicy::exponential(Duration::from_millis(100))
    ///     .with_max_retries(10)
    ///     .with_max_delay(Duration::from_secs(5));
    ///
    /// // Without cap: 100ms, 200ms, 400ms, 800ms, 1600ms, 3200ms, 6400ms...
    /// // With 5s cap: 100ms, 200ms, 400ms, 800ms, 1600ms, 3200ms, 5000ms...
    /// ```
    pub fn with_max_delay(mut self, d: Duration) -> Self {
        self.max_delay = Some(d);
        self
    }

    /// Add proportional jitter to delays.
    ///
    /// The factor determines the range of randomness. For example, `0.25` means
    /// the actual delay will be ±25% of the calculated delay.
    ///
    /// **Note**: Requires the `jitter` feature. Without it, this method does nothing.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use stillwater::RetryPolicy;
    /// use std::time::Duration;
    ///
    /// let policy = RetryPolicy::exponential(Duration::from_millis(100))
    ///     .with_jitter(0.25)
    ///     .with_max_retries(5);
    ///
    /// // Delays will be between 75ms-125ms, 150ms-250ms, 300ms-500ms, etc.
    /// ```
    pub fn with_jitter(mut self, factor: f64) -> Self {
        self.jitter = JitterStrategy::Proportional(factor.clamp(0.0, 1.0));
        self
    }

    /// Use full jitter (AWS recommended).
    ///
    /// The delay will be a random value between 0 and the calculated delay.
    /// This provides maximum spread to prevent thundering herd.
    ///
    /// **Note**: Requires the `jitter` feature. Without it, this method does nothing.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use stillwater::RetryPolicy;
    /// use std::time::Duration;
    ///
    /// let policy = RetryPolicy::exponential(Duration::from_millis(100))
    ///     .with_full_jitter()
    ///     .with_max_retries(5);
    /// ```
    pub fn with_full_jitter(mut self) -> Self {
        self.jitter = JitterStrategy::Full;
        self
    }

    /// Use decorrelated jitter (AWS style).
    ///
    /// Each delay is random between base and 3x the previous delay.
    /// This provides good spread while maintaining progression.
    ///
    /// **Note**: Requires the `jitter` feature. Without it, this method does nothing.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use stillwater::RetryPolicy;
    /// use std::time::Duration;
    ///
    /// let policy = RetryPolicy::exponential(Duration::from_millis(100))
    ///     .with_decorrelated_jitter()
    ///     .with_max_retries(5);
    /// ```
    pub fn with_decorrelated_jitter(mut self) -> Self {
        self.jitter = JitterStrategy::Decorrelated;
        self
    }

    /// Get the maximum number of retries.
    pub fn max_retries(&self) -> Option<u32> {
        self.max_retries
    }

    /// Get the maximum delay cap.
    pub fn max_delay(&self) -> Option<Duration> {
        self.max_delay
    }

    /// Get the jitter strategy.
    pub fn jitter(&self) -> &JitterStrategy {
        &self.jitter
    }

    /// Get the retry strategy.
    pub fn strategy(&self) -> &RetryStrategy {
        &self.strategy
    }

    /// Calculate the delay before attempt N (0-indexed).
    ///
    /// Returns None if no more retries should be attempted.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use stillwater::RetryPolicy;
    /// use std::time::Duration;
    ///
    /// let policy = RetryPolicy::exponential(Duration::from_millis(100))
    ///     .with_max_retries(3);
    ///
    /// assert_eq!(policy.delay_for_attempt(0), Some(Duration::from_millis(100)));
    /// assert_eq!(policy.delay_for_attempt(1), Some(Duration::from_millis(200)));
    /// assert_eq!(policy.delay_for_attempt(2), Some(Duration::from_millis(400)));
    /// assert_eq!(policy.delay_for_attempt(3), None); // exceeded max_retries
    /// ```
    pub fn delay_for_attempt(&self, attempt: u32) -> Option<Duration> {
        // Check max_retries
        if let Some(max) = self.max_retries {
            if attempt >= max {
                return None;
            }
        }

        // Calculate base delay from strategy
        let base_delay = match &self.strategy {
            RetryStrategy::Constant(d) => *d,
            RetryStrategy::Linear { base } => base.saturating_mul(attempt + 1),
            RetryStrategy::Exponential { base } => {
                base.saturating_mul(2u32.saturating_pow(attempt))
            }
            RetryStrategy::Fibonacci { base } => base.saturating_mul(fibonacci(attempt + 1)),
        };

        // Apply max_delay cap
        let capped = match self.max_delay {
            Some(max) => base_delay.min(max),
            None => base_delay,
        };

        Some(capped)
    }

    /// Calculate the delay with jitter applied.
    ///
    /// This is used internally by the retry executor.
    #[doc(hidden)]
    pub fn delay_with_jitter(
        &self,
        attempt: u32,
        prev_delay: Option<Duration>,
    ) -> Option<Duration> {
        let base_delay = self.delay_for_attempt(attempt)?;
        Some(self.jitter.apply(base_delay, prev_delay, self.max_delay))
    }

    /// Validate that the policy has at least one bound.
    ///
    /// Returns an error message if the policy is invalid.
    pub fn validate(&self) -> Result<(), &'static str> {
        if self.max_retries.is_none() && self.max_delay.is_none() {
            Err("RetryPolicy must have at least one bound (max_retries or max_delay)")
        } else {
            Ok(())
        }
    }
}

impl JitterStrategy {
    /// Apply jitter to a base delay.
    ///
    /// # Arguments
    ///
    /// * `base_delay` - The calculated delay before jitter
    /// * `prev_delay` - The previous delay (for decorrelated jitter)
    /// * `max_delay` - Optional cap on the final delay
    pub fn apply(
        &self,
        base_delay: Duration,
        #[cfg_attr(not(feature = "jitter"), allow(unused_variables))] prev_delay: Option<Duration>,
        max_delay: Option<Duration>,
    ) -> Duration {
        let jittered = match self {
            JitterStrategy::None => base_delay,
            #[cfg(feature = "jitter")]
            JitterStrategy::Proportional(factor) => {
                use rand::Rng;
                let mut rng = rand::rng();
                let base_millis = base_delay.as_millis() as f64;
                let jitter_range = base_millis * factor;
                let min = (base_millis - jitter_range).max(0.0);
                let max = base_millis + jitter_range;
                let jittered_millis = rng.random_range(min..=max);
                Duration::from_millis(jittered_millis as u64)
            }
            #[cfg(not(feature = "jitter"))]
            JitterStrategy::Proportional(_) => base_delay,
            #[cfg(feature = "jitter")]
            JitterStrategy::Full => {
                use rand::Rng;
                let mut rng = rand::rng();
                let max_millis = base_delay.as_millis() as u64;
                if max_millis == 0 {
                    Duration::ZERO
                } else {
                    Duration::from_millis(rng.random_range(0..=max_millis))
                }
            }
            #[cfg(not(feature = "jitter"))]
            JitterStrategy::Full => base_delay,
            #[cfg(feature = "jitter")]
            JitterStrategy::Decorrelated => {
                use rand::Rng;
                let mut rng = rand::rng();
                let prev = prev_delay.unwrap_or(base_delay);
                let base_millis = base_delay.as_millis() as u64;
                let max_millis = prev.as_millis().saturating_mul(3) as u64;
                if max_millis <= base_millis {
                    base_delay
                } else {
                    Duration::from_millis(rng.random_range(base_millis..=max_millis))
                }
            }
            #[cfg(not(feature = "jitter"))]
            JitterStrategy::Decorrelated => base_delay,
        };

        // Apply max_delay cap
        match max_delay {
            Some(max) => jittered.min(max),
            None => jittered,
        }
    }
}

/// Calculate the nth Fibonacci number.
fn fibonacci(n: u32) -> u32 {
    if n == 0 {
        return 0;
    }
    let mut a = 0u32;
    let mut b = 1u32;
    for _ in 1..n {
        let temp = a.saturating_add(b);
        a = b;
        b = temp;
    }
    b
}

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

    #[test]
    fn test_constant_delay() {
        let policy = RetryPolicy::constant(Duration::from_millis(100)).with_max_retries(3);

        assert_eq!(
            policy.delay_for_attempt(0),
            Some(Duration::from_millis(100))
        );
        assert_eq!(
            policy.delay_for_attempt(1),
            Some(Duration::from_millis(100))
        );
        assert_eq!(
            policy.delay_for_attempt(2),
            Some(Duration::from_millis(100))
        );
        assert_eq!(policy.delay_for_attempt(3), None);
    }

    #[test]
    fn test_linear_delay() {
        let policy = RetryPolicy::linear(Duration::from_millis(100)).with_max_retries(5);

        assert_eq!(
            policy.delay_for_attempt(0),
            Some(Duration::from_millis(100))
        );
        assert_eq!(
            policy.delay_for_attempt(1),
            Some(Duration::from_millis(200))
        );
        assert_eq!(
            policy.delay_for_attempt(2),
            Some(Duration::from_millis(300))
        );
        assert_eq!(
            policy.delay_for_attempt(3),
            Some(Duration::from_millis(400))
        );
    }

    #[test]
    fn test_exponential_delay() {
        let policy = RetryPolicy::exponential(Duration::from_millis(100)).with_max_retries(5);

        assert_eq!(
            policy.delay_for_attempt(0),
            Some(Duration::from_millis(100))
        );
        assert_eq!(
            policy.delay_for_attempt(1),
            Some(Duration::from_millis(200))
        );
        assert_eq!(
            policy.delay_for_attempt(2),
            Some(Duration::from_millis(400))
        );
        assert_eq!(
            policy.delay_for_attempt(3),
            Some(Duration::from_millis(800))
        );
    }

    #[test]
    fn test_fibonacci_delay() {
        let policy = RetryPolicy::fibonacci(Duration::from_millis(100)).with_max_retries(6);

        // fib sequence: 1, 1, 2, 3, 5, 8, 13...
        assert_eq!(
            policy.delay_for_attempt(0),
            Some(Duration::from_millis(100))
        );
        assert_eq!(
            policy.delay_for_attempt(1),
            Some(Duration::from_millis(100))
        );
        assert_eq!(
            policy.delay_for_attempt(2),
            Some(Duration::from_millis(200))
        );
        assert_eq!(
            policy.delay_for_attempt(3),
            Some(Duration::from_millis(300))
        );
        assert_eq!(
            policy.delay_for_attempt(4),
            Some(Duration::from_millis(500))
        );
        assert_eq!(
            policy.delay_for_attempt(5),
            Some(Duration::from_millis(800))
        );
    }

    #[test]
    fn test_max_delay_cap() {
        let policy = RetryPolicy::exponential(Duration::from_millis(100))
            .with_max_retries(10)
            .with_max_delay(Duration::from_millis(500));

        assert_eq!(
            policy.delay_for_attempt(0),
            Some(Duration::from_millis(100))
        );
        assert_eq!(
            policy.delay_for_attempt(1),
            Some(Duration::from_millis(200))
        );
        assert_eq!(
            policy.delay_for_attempt(2),
            Some(Duration::from_millis(400))
        );
        assert_eq!(
            policy.delay_for_attempt(3),
            Some(Duration::from_millis(500))
        ); // capped
        assert_eq!(
            policy.delay_for_attempt(4),
            Some(Duration::from_millis(500))
        ); // capped
    }

    #[test]
    fn test_max_retries_limit() {
        let policy = RetryPolicy::constant(Duration::from_millis(100)).with_max_retries(2);

        assert!(policy.delay_for_attempt(0).is_some());
        assert!(policy.delay_for_attempt(1).is_some());
        assert!(policy.delay_for_attempt(2).is_none());
    }

    #[test]
    fn test_policy_is_clone() {
        let policy = RetryPolicy::exponential(Duration::from_millis(100)).with_max_retries(3);
        let cloned = policy.clone();
        assert_eq!(policy, cloned);
    }

    #[test]
    fn test_policy_is_debug() {
        let policy = RetryPolicy::exponential(Duration::from_millis(100)).with_max_retries(3);
        let debug = format!("{:?}", policy);
        assert!(debug.contains("RetryPolicy"));
    }

    #[test]
    fn test_fibonacci_function() {
        assert_eq!(fibonacci(0), 0);
        assert_eq!(fibonacci(1), 1);
        assert_eq!(fibonacci(2), 1);
        assert_eq!(fibonacci(3), 2);
        assert_eq!(fibonacci(4), 3);
        assert_eq!(fibonacci(5), 5);
        assert_eq!(fibonacci(6), 8);
        assert_eq!(fibonacci(7), 13);
    }

    #[test]
    fn test_validate_with_max_retries() {
        let policy = RetryPolicy::constant(Duration::from_millis(100)).with_max_retries(3);
        assert!(policy.validate().is_ok());
    }

    #[test]
    fn test_validate_with_max_delay() {
        let policy = RetryPolicy::constant(Duration::from_millis(100))
            .with_max_delay(Duration::from_secs(5));
        assert!(policy.validate().is_ok());
    }

    #[test]
    fn test_validate_with_both_bounds() {
        let policy = RetryPolicy::constant(Duration::from_millis(100))
            .with_max_retries(3)
            .with_max_delay(Duration::from_secs(5));
        assert!(policy.validate().is_ok());
    }

    #[test]
    fn test_validate_no_bounds() {
        let policy = RetryPolicy::constant(Duration::from_millis(100));
        assert!(policy.validate().is_err());
    }

    #[test]
    fn test_jitter_strategy_default() {
        let jitter = JitterStrategy::default();
        assert_eq!(jitter, JitterStrategy::None);
    }

    #[test]
    fn test_jitter_none_returns_base_delay() {
        let jitter = JitterStrategy::None;
        let base = Duration::from_millis(100);
        let result = jitter.apply(base, None, None);
        assert_eq!(result, base);
    }

    #[test]
    fn test_policy_getters() {
        let policy = RetryPolicy::exponential(Duration::from_millis(100))
            .with_max_retries(3)
            .with_max_delay(Duration::from_secs(5))
            .with_jitter(0.25);

        assert_eq!(policy.max_retries(), Some(3));
        assert_eq!(policy.max_delay(), Some(Duration::from_secs(5)));
        assert!(matches!(policy.jitter(), JitterStrategy::Proportional(_)));
        assert!(matches!(
            policy.strategy(),
            RetryStrategy::Exponential { .. }
        ));
    }
}