oxirs-samm 0.2.2

Semantic Aspect Meta Model (SAMM) implementation for OxiRS
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
//! Cloud Storage Client Abstraction Layer
//!
//! This module provides a sync, trait-based cloud storage abstraction with:
//!
//! - [`CloudStorageClient`] — core CRUD trait
//! - [`CloudStorageError`] — structured error enum
//! - [`MockCloudStorage`] — in-memory implementation for testing with error injection
//! - [`RetryableCloudClient`] — wraps any client with configurable retry logic
//!
//! # Example
//!
//! ```rust
//! use oxirs_samm::cloud_client::{CloudStorageClient, MockCloudStorage, CloudStorageError};
//!
//! let store = MockCloudStorage::new();
//! store.upload("models/test.ttl", b"@prefix ...").expect("should succeed");
//! assert!(store.exists("models/test.ttl").expect("should succeed"));
//! let data = store.download("models/test.ttl").expect("should succeed");
//! assert_eq!(data, b"@prefix ...");
//! ```

use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use std::time::Duration;

// ─────────────────────────────────────────────────────────────────────────────
// Error types
// ─────────────────────────────────────────────────────────────────────────────

/// Errors that can occur during cloud storage operations.
#[derive(Debug, thiserror::Error)]
pub enum CloudStorageError {
    /// The requested key was not found.
    #[error("not found: {0}")]
    NotFound(String),

    /// The operation was denied due to insufficient permissions.
    #[error("permission denied: {0}")]
    PermissionDenied(String),

    /// The storage quota has been exceeded.
    #[error("quota exceeded: {0}")]
    QuotaExceeded(String),

    /// A transient network error occurred (retryable).
    #[error("network error: {0}")]
    NetworkError(String),

    /// An internal server error occurred (retryable).
    #[error("internal error: {0}")]
    InternalError(String),
}

impl CloudStorageError {
    /// Returns `true` if this error is retryable.
    pub fn is_retryable(&self) -> bool {
        matches!(
            self,
            CloudStorageError::NetworkError(_) | CloudStorageError::InternalError(_)
        )
    }
}

/// Clonable kind mirror of [`CloudStorageError`] used internally in [`MockCloudStorage`].
#[derive(Debug, Clone)]
enum CloudStorageErrorKind {
    NotFound(String),
    PermissionDenied(String),
    QuotaExceeded(String),
    NetworkError(String),
    InternalError(String),
}

impl CloudStorageErrorKind {
    /// Convert into a [`CloudStorageError`].
    fn into_error(self) -> CloudStorageError {
        match self {
            CloudStorageErrorKind::NotFound(m) => CloudStorageError::NotFound(m),
            CloudStorageErrorKind::PermissionDenied(m) => CloudStorageError::PermissionDenied(m),
            CloudStorageErrorKind::QuotaExceeded(m) => CloudStorageError::QuotaExceeded(m),
            CloudStorageErrorKind::NetworkError(m) => CloudStorageError::NetworkError(m),
            CloudStorageErrorKind::InternalError(m) => CloudStorageError::InternalError(m),
        }
    }
}

impl From<CloudStorageError> for CloudStorageErrorKind {
    fn from(e: CloudStorageError) -> Self {
        match e {
            CloudStorageError::NotFound(m) => CloudStorageErrorKind::NotFound(m),
            CloudStorageError::PermissionDenied(m) => CloudStorageErrorKind::PermissionDenied(m),
            CloudStorageError::QuotaExceeded(m) => CloudStorageErrorKind::QuotaExceeded(m),
            CloudStorageError::NetworkError(m) => CloudStorageErrorKind::NetworkError(m),
            CloudStorageError::InternalError(m) => CloudStorageErrorKind::InternalError(m),
        }
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Trait
// ─────────────────────────────────────────────────────────────────────────────

/// Sync cloud storage CRUD trait.
///
/// Implement this trait to provide different storage backends.
/// The trait is object-safe and can be used as `Box<dyn CloudStorageClient>`.
pub trait CloudStorageClient: Send + Sync {
    /// Upload `data` bytes under the given `key`.
    fn upload(&self, key: &str, data: &[u8]) -> Result<(), CloudStorageError>;

    /// Download and return the bytes stored under `key`.
    fn download(&self, key: &str) -> Result<Vec<u8>, CloudStorageError>;

    /// Delete the object stored under `key`.
    fn delete(&self, key: &str) -> Result<(), CloudStorageError>;

    /// List all keys that start with `prefix`.
    fn list(&self, prefix: &str) -> Result<Vec<String>, CloudStorageError>;

    /// Check whether an object exists under `key`.
    fn exists(&self, key: &str) -> Result<bool, CloudStorageError>;
}

// ─────────────────────────────────────────────────────────────────────────────
// MockCloudStorage
// ─────────────────────────────────────────────────────────────────────────────

/// In-memory cloud storage mock for testing.
///
/// Supports error injection via [`MockCloudStorage::set_error_on_key`] so that
/// individual test cases can simulate failure modes without hitting a real endpoint.
#[derive(Debug, Clone)]
pub struct MockCloudStorage {
    storage: Arc<Mutex<HashMap<String, Vec<u8>>>>,
    error_keys: Arc<Mutex<HashMap<String, CloudStorageErrorKind>>>,
}

impl MockCloudStorage {
    /// Create a new, empty in-memory store.
    pub fn new() -> Self {
        Self {
            storage: Arc::new(Mutex::new(HashMap::new())),
            error_keys: Arc::new(Mutex::new(HashMap::new())),
        }
    }

    /// Return a sorted list of all keys currently stored.
    pub fn uploaded_keys(&self) -> Vec<String> {
        let guard = match self.storage.lock() {
            Ok(g) => g,
            Err(poisoned) => poisoned.into_inner(),
        };
        let mut keys: Vec<String> = guard.keys().cloned().collect();
        keys.sort();
        keys
    }

    /// Inject an error that will be returned for any operation on `key`.
    ///
    /// Call [`MockCloudStorage::clear_errors`] to remove injected errors.
    pub fn set_error_on_key(&self, key: &str, error: CloudStorageError) {
        let mut guard = match self.error_keys.lock() {
            Ok(g) => g,
            Err(poisoned) => poisoned.into_inner(),
        };
        guard.insert(key.to_string(), CloudStorageErrorKind::from(error));
    }

    /// Remove all injected errors.
    pub fn clear_errors(&self) {
        let mut guard = match self.error_keys.lock() {
            Ok(g) => g,
            Err(poisoned) => poisoned.into_inner(),
        };
        guard.clear();
    }

    /// Check for an injected error on `key`, returning it if present.
    fn check_error(&self, key: &str) -> Option<CloudStorageError> {
        let guard = match self.error_keys.lock() {
            Ok(g) => g,
            Err(poisoned) => poisoned.into_inner(),
        };
        guard.get(key).cloned().map(|k| k.into_error())
    }
}

impl Default for MockCloudStorage {
    fn default() -> Self {
        Self::new()
    }
}

impl CloudStorageClient for MockCloudStorage {
    fn upload(&self, key: &str, data: &[u8]) -> Result<(), CloudStorageError> {
        if let Some(err) = self.check_error(key) {
            return Err(err);
        }
        let mut guard = match self.storage.lock() {
            Ok(g) => g,
            Err(poisoned) => poisoned.into_inner(),
        };
        guard.insert(key.to_string(), data.to_vec());
        Ok(())
    }

    fn download(&self, key: &str) -> Result<Vec<u8>, CloudStorageError> {
        if let Some(err) = self.check_error(key) {
            return Err(err);
        }
        let guard = match self.storage.lock() {
            Ok(g) => g,
            Err(poisoned) => poisoned.into_inner(),
        };
        guard
            .get(key)
            .cloned()
            .ok_or_else(|| CloudStorageError::NotFound(key.to_string()))
    }

    fn delete(&self, key: &str) -> Result<(), CloudStorageError> {
        if let Some(err) = self.check_error(key) {
            return Err(err);
        }
        let mut guard = match self.storage.lock() {
            Ok(g) => g,
            Err(poisoned) => poisoned.into_inner(),
        };
        guard.remove(key);
        Ok(())
    }

    fn list(&self, prefix: &str) -> Result<Vec<String>, CloudStorageError> {
        let guard = match self.storage.lock() {
            Ok(g) => g,
            Err(poisoned) => poisoned.into_inner(),
        };
        let mut keys: Vec<String> = guard
            .keys()
            .filter(|k| k.starts_with(prefix))
            .cloned()
            .collect();
        keys.sort();
        Ok(keys)
    }

    fn exists(&self, key: &str) -> Result<bool, CloudStorageError> {
        if let Some(err) = self.check_error(key) {
            return Err(err);
        }
        let guard = match self.storage.lock() {
            Ok(g) => g,
            Err(poisoned) => poisoned.into_inner(),
        };
        Ok(guard.contains_key(key))
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// RetryableCloudClient
// ─────────────────────────────────────────────────────────────────────────────

/// Wraps any [`CloudStorageClient`] with configurable retry logic.
///
/// Only [`CloudStorageError::NetworkError`] and [`CloudStorageError::InternalError`]
/// are retried. `NotFound`, `PermissionDenied`, and `QuotaExceeded` are returned
/// immediately without retry.
///
/// # Example
///
/// ```rust
/// use oxirs_samm::cloud_client::{MockCloudStorage, RetryableCloudClient, CloudStorageClient};
///
/// let inner = MockCloudStorage::new();
/// let client = RetryableCloudClient::new(inner, 3, 0, true);
/// client.upload("key", b"data").expect("should succeed");
/// ```
#[derive(Debug)]
pub struct RetryableCloudClient<C: CloudStorageClient> {
    inner: C,
    /// Maximum number of retry attempts after the first failure.
    pub max_retries: u32,
    /// Base delay in milliseconds between retries.
    pub base_delay_ms: u64,
    /// Whether to use exponential backoff (`delay = base_delay_ms * 2^attempt`).
    pub exponential_backoff: bool,
}

impl<C: CloudStorageClient> RetryableCloudClient<C> {
    /// Create a new retryable client.
    ///
    /// # Arguments
    ///
    /// * `inner` — the underlying client to wrap
    /// * `max_retries` — how many additional attempts to make after the first failure
    /// * `base_delay_ms` — delay in milliseconds before retrying (use 0 in tests)
    /// * `exponential_backoff` — if `true`, doubles the delay on each attempt
    pub fn new(inner: C, max_retries: u32, base_delay_ms: u64, exponential_backoff: bool) -> Self {
        Self {
            inner,
            max_retries,
            base_delay_ms,
            exponential_backoff,
        }
    }

    /// Compute the sleep duration for a given attempt index (0-based).
    fn sleep_duration(&self, attempt: u32) -> Duration {
        if self.base_delay_ms == 0 {
            return Duration::from_millis(0);
        }
        let multiplier = if self.exponential_backoff {
            // 2^attempt, capped to avoid overflow
            1u64.checked_shl(attempt).unwrap_or(u64::MAX)
        } else {
            1u64
        };
        Duration::from_millis(self.base_delay_ms.saturating_mul(multiplier))
    }

    /// Execute `op` with retry logic for transient errors.
    fn with_retry<F, T>(&self, op: F) -> Result<T, CloudStorageError>
    where
        F: Fn() -> Result<T, CloudStorageError>,
    {
        let mut last_err: Option<CloudStorageError> = None;
        for attempt in 0..=self.max_retries {
            match op() {
                Ok(val) => return Ok(val),
                Err(e) => {
                    if e.is_retryable() {
                        if attempt < self.max_retries {
                            let delay = self.sleep_duration(attempt);
                            if !delay.is_zero() {
                                std::thread::sleep(delay);
                            }
                        }
                        last_err = Some(e);
                    } else {
                        // Non-retryable — return immediately
                        return Err(e);
                    }
                }
            }
        }
        Err(last_err
            .unwrap_or_else(|| CloudStorageError::InternalError("retry exhausted".to_string())))
    }
}

impl<C: CloudStorageClient> CloudStorageClient for RetryableCloudClient<C> {
    fn upload(&self, key: &str, data: &[u8]) -> Result<(), CloudStorageError> {
        // Capture data as owned to allow repeated calls inside closure
        let data_owned = data.to_vec();
        self.with_retry(|| self.inner.upload(key, &data_owned))
    }

    fn download(&self, key: &str) -> Result<Vec<u8>, CloudStorageError> {
        self.with_retry(|| self.inner.download(key))
    }

    fn delete(&self, key: &str) -> Result<(), CloudStorageError> {
        self.with_retry(|| self.inner.delete(key))
    }

    fn list(&self, prefix: &str) -> Result<Vec<String>, CloudStorageError> {
        self.with_retry(|| self.inner.list(prefix))
    }

    fn exists(&self, key: &str) -> Result<bool, CloudStorageError> {
        self.with_retry(|| self.inner.exists(key))
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Tests
// ─────────────────────────────────────────────────────────────────────────────

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

    // ── Error display ──────────────────────────────────────────────────────

    #[test]
    fn test_cloud_storage_error_not_found_display() {
        let e = CloudStorageError::NotFound("key.ttl".to_string());
        assert!(e.to_string().contains("not found"));
        assert!(e.to_string().contains("key.ttl"));
    }

    #[test]
    fn test_cloud_storage_error_network_error_display() {
        let e = CloudStorageError::NetworkError("timeout".to_string());
        assert!(e.to_string().contains("network error"));
        assert!(e.to_string().contains("timeout"));
    }

    #[test]
    fn test_cloud_storage_error_internal_error_display() {
        let e = CloudStorageError::InternalError("503".to_string());
        assert!(e.to_string().contains("internal error"));
        assert!(e.to_string().contains("503"));
    }

    #[test]
    fn test_cloud_storage_error_permission_denied_display() {
        let e = CloudStorageError::PermissionDenied("read".to_string());
        assert!(e.to_string().contains("permission denied"));
    }

    #[test]
    fn test_cloud_storage_error_quota_exceeded_display() {
        let e = CloudStorageError::QuotaExceeded("5 GB".to_string());
        assert!(e.to_string().contains("quota exceeded"));
    }

    #[test]
    fn test_cloud_storage_error_retryable_network() {
        assert!(CloudStorageError::NetworkError("x".to_string()).is_retryable());
    }

    #[test]
    fn test_cloud_storage_error_retryable_internal() {
        assert!(CloudStorageError::InternalError("x".to_string()).is_retryable());
    }

    #[test]
    fn test_cloud_storage_error_not_retryable_not_found() {
        assert!(!CloudStorageError::NotFound("x".to_string()).is_retryable());
    }

    #[test]
    fn test_cloud_storage_error_not_retryable_permission_denied() {
        assert!(!CloudStorageError::PermissionDenied("x".to_string()).is_retryable());
    }

    #[test]
    fn test_cloud_storage_error_not_retryable_quota_exceeded() {
        assert!(!CloudStorageError::QuotaExceeded("x".to_string()).is_retryable());
    }

    // ── MockCloudStorage basic operations ─────────────────────────────────

    #[test]
    fn test_mock_upload_and_download() {
        let store = MockCloudStorage::new();
        store
            .upload("a.ttl", b"hello")
            .expect("upload should succeed");
        let data = store.download("a.ttl").expect("download should succeed");
        assert_eq!(data, b"hello");
    }

    #[test]
    fn test_mock_download_not_found() {
        let store = MockCloudStorage::new();
        let result = store.download("missing.ttl");
        assert!(matches!(result, Err(CloudStorageError::NotFound(_))));
    }

    #[test]
    fn test_mock_exists_true() {
        let store = MockCloudStorage::new();
        store.upload("x.ttl", b"").expect("upload should succeed");
        assert!(store.exists("x.ttl").expect("exists should succeed"));
    }

    #[test]
    fn test_mock_exists_false() {
        let store = MockCloudStorage::new();
        assert!(!store.exists("absent.ttl").expect("exists should succeed"));
    }

    #[test]
    fn test_mock_delete() {
        let store = MockCloudStorage::new();
        store
            .upload("del.ttl", b"data")
            .expect("upload should succeed");
        store.delete("del.ttl").expect("delete should succeed");
        assert!(!store.exists("del.ttl").expect("exists should succeed"));
    }

    #[test]
    fn test_mock_list_prefix_filter() {
        let store = MockCloudStorage::new();
        store
            .upload("models/a.ttl", b"")
            .expect("upload should succeed");
        store
            .upload("models/b.ttl", b"")
            .expect("upload should succeed");
        store
            .upload("other/c.ttl", b"")
            .expect("upload should succeed");
        let keys = store.list("models/").expect("list should succeed");
        assert_eq!(keys.len(), 2);
        assert!(keys.contains(&"models/a.ttl".to_string()));
        assert!(keys.contains(&"models/b.ttl".to_string()));
    }

    #[test]
    fn test_mock_list_empty_prefix() {
        let store = MockCloudStorage::new();
        store.upload("x.ttl", b"").expect("upload should succeed");
        store.upload("y.ttl", b"").expect("upload should succeed");
        let keys = store.list("").expect("list should succeed");
        assert_eq!(keys.len(), 2);
    }

    #[test]
    fn test_mock_uploaded_keys_sorted() {
        let store = MockCloudStorage::new();
        store.upload("c.ttl", b"").expect("upload should succeed");
        store.upload("a.ttl", b"").expect("upload should succeed");
        store.upload("b.ttl", b"").expect("upload should succeed");
        let keys = store.uploaded_keys();
        assert_eq!(keys, vec!["a.ttl", "b.ttl", "c.ttl"]);
    }

    // ── Error injection ────────────────────────────────────────────────────

    #[test]
    fn test_mock_set_error_on_key_upload() {
        let store = MockCloudStorage::new();
        store.set_error_on_key(
            "bad.ttl",
            CloudStorageError::PermissionDenied("write".to_string()),
        );
        let result = store.upload("bad.ttl", b"data");
        assert!(matches!(
            result,
            Err(CloudStorageError::PermissionDenied(_))
        ));
    }

    #[test]
    fn test_mock_set_error_on_key_download() {
        let store = MockCloudStorage::new();
        store
            .upload("e.ttl", b"data")
            .expect("upload should succeed");
        store.set_error_on_key(
            "e.ttl",
            CloudStorageError::NetworkError("flaky".to_string()),
        );
        let result = store.download("e.ttl");
        assert!(matches!(result, Err(CloudStorageError::NetworkError(_))));
    }

    #[test]
    fn test_mock_set_error_on_key_delete() {
        let store = MockCloudStorage::new();
        store.set_error_on_key(
            "d.ttl",
            CloudStorageError::InternalError("disk".to_string()),
        );
        let result = store.delete("d.ttl");
        assert!(matches!(result, Err(CloudStorageError::InternalError(_))));
    }

    #[test]
    fn test_mock_set_error_on_key_exists() {
        let store = MockCloudStorage::new();
        store.set_error_on_key("e2.ttl", CloudStorageError::NetworkError("net".to_string()));
        let result = store.exists("e2.ttl");
        assert!(matches!(result, Err(CloudStorageError::NetworkError(_))));
    }

    #[test]
    fn test_mock_clear_errors() {
        let store = MockCloudStorage::new();
        store.set_error_on_key(
            "f.ttl",
            CloudStorageError::PermissionDenied("x".to_string()),
        );
        store.clear_errors();
        // After clearing, upload should work normally
        store
            .upload("f.ttl", b"ok")
            .expect("upload should succeed after clearing errors");
        assert!(store.exists("f.ttl").expect("exists should succeed"));
    }

    #[test]
    fn test_mock_error_not_found_on_key() {
        let store = MockCloudStorage::new();
        store.set_error_on_key("nf.ttl", CloudStorageError::NotFound("nf.ttl".to_string()));
        let result = store.download("nf.ttl");
        assert!(matches!(result, Err(CloudStorageError::NotFound(_))));
    }

    #[test]
    fn test_mock_error_permission_denied_on_key() {
        let store = MockCloudStorage::new();
        store.set_error_on_key(
            "pd.ttl",
            CloudStorageError::PermissionDenied("denied".to_string()),
        );
        let result = store.upload("pd.ttl", b"x");
        assert!(matches!(
            result,
            Err(CloudStorageError::PermissionDenied(_))
        ));
    }

    #[test]
    fn test_mock_error_quota_exceeded_on_key() {
        let store = MockCloudStorage::new();
        store.set_error_on_key(
            "qe.ttl",
            CloudStorageError::QuotaExceeded("limit".to_string()),
        );
        let result = store.upload("qe.ttl", b"x");
        assert!(matches!(result, Err(CloudStorageError::QuotaExceeded(_))));
    }

    // ── RetryableCloudClient ───────────────────────────────────────────────

    #[test]
    fn test_retryable_client_succeeds_no_retries() {
        let inner = MockCloudStorage::new();
        let client = RetryableCloudClient::new(inner, 3, 0, false);
        client
            .upload("ok.ttl", b"data")
            .expect("upload should succeed");
        assert!(client.exists("ok.ttl").expect("exists should succeed"));
    }

    #[test]
    fn test_retryable_client_retries_network_error() {
        // Upload to a key, then inject network error AFTER it's stored so retries pass.
        // We verify that a network error is returned after exhausting retries when it persists.
        let inner = MockCloudStorage::new();
        inner.set_error_on_key(
            "net.ttl",
            CloudStorageError::NetworkError("timeout".to_string()),
        );
        let client = RetryableCloudClient::new(inner, 2, 0, false);
        let result = client.upload("net.ttl", b"x");
        // All 3 attempts fail (initial + 2 retries) — should return NetworkError
        assert!(matches!(result, Err(CloudStorageError::NetworkError(_))));
    }

    #[test]
    fn test_retryable_client_does_not_retry_not_found() {
        let inner = MockCloudStorage::new();
        // No data uploaded — download should return NotFound immediately
        let client = RetryableCloudClient::new(inner, 5, 0, false);
        let result = client.download("absent.ttl");
        assert!(matches!(result, Err(CloudStorageError::NotFound(_))));
    }

    #[test]
    fn test_retryable_client_does_not_retry_permission_denied() {
        let inner = MockCloudStorage::new();
        inner.set_error_on_key(
            "pd.ttl",
            CloudStorageError::PermissionDenied("x".to_string()),
        );
        let client = RetryableCloudClient::new(inner, 5, 0, false);
        let result = client.upload("pd.ttl", b"x");
        assert!(matches!(
            result,
            Err(CloudStorageError::PermissionDenied(_))
        ));
    }

    #[test]
    fn test_retryable_client_does_not_retry_quota_exceeded() {
        let inner = MockCloudStorage::new();
        inner.set_error_on_key("qe.ttl", CloudStorageError::QuotaExceeded("x".to_string()));
        let client = RetryableCloudClient::new(inner, 5, 0, false);
        let result = client.upload("qe.ttl", b"x");
        assert!(matches!(result, Err(CloudStorageError::QuotaExceeded(_))));
    }

    #[test]
    fn test_retryable_client_exponential_backoff_no_panic() {
        let inner = MockCloudStorage::new();
        // Zero base_delay_ms so test is fast; just checks no panic
        let client = RetryableCloudClient::new(inner, 3, 0, true);
        client
            .upload("exp.ttl", b"data")
            .expect("upload should succeed");
        let data = client.download("exp.ttl").expect("download should succeed");
        assert_eq!(data, b"data");
    }

    #[test]
    fn test_retryable_client_max_retries_exhausted() {
        let inner = MockCloudStorage::new();
        inner.set_error_on_key(
            "err.ttl",
            CloudStorageError::InternalError("500".to_string()),
        );
        // max_retries = 1 means 2 total attempts
        let client = RetryableCloudClient::new(inner, 1, 0, false);
        let result = client.download("err.ttl");
        assert!(matches!(result, Err(CloudStorageError::InternalError(_))));
    }

    #[test]
    fn test_retryable_client_list_works() {
        let inner = MockCloudStorage::new();
        inner
            .upload("ns/a.ttl", b"")
            .expect("upload should succeed");
        inner
            .upload("ns/b.ttl", b"")
            .expect("upload should succeed");
        let client = RetryableCloudClient::new(inner, 2, 0, false);
        let keys = client.list("ns/").expect("list should succeed");
        assert_eq!(keys.len(), 2);
    }

    #[test]
    fn test_retryable_client_delete_works() {
        let inner = MockCloudStorage::new();
        inner.upload("rm.ttl", b"x").expect("upload should succeed");
        let client = RetryableCloudClient::new(inner, 2, 0, false);
        client.delete("rm.ttl").expect("delete should succeed");
        assert!(!client.exists("rm.ttl").expect("exists should succeed"));
    }
}