recall_entangler_storage 0.1.0

Distributed storage for uploading and downloading data.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
// Copyright 2024 Entanglement Contributors
// SPDX-License-Identifier: Apache-2.0, MIT

use anyhow::Result;
use async_trait::async_trait;
use bytes::Bytes;
use cid::Cid;
use futures::{future::ready, stream};
use multihash::{Code, MultihashDigest};
use sha2::{Digest, Sha256};
use std::collections::HashMap;
use std::sync::RwLock;
use std::sync::{Arc, Mutex};

use crate::storage::{
    ByteStream, ChunkIdMapper, ChunkStream, Error as StorageError, Storage, UploadResult,
};

const CHUNK_SIZE: usize = 1024;

/// `FakeStorage` is an in-memory implementation of the `Storage` trait for testing purposes.
/// It allows simulating various storage scenarios, including successful uploads/downloads,
/// chunk failures, and blob failures.
#[derive(Clone)]
pub struct FakeStorage {
    data: Arc<Mutex<HashMap<String, Vec<Bytes>>>>,
    fail_chunks: Arc<Mutex<HashMap<String, Vec<u64>>>>,
    fail_blobs: Arc<Mutex<HashMap<String, bool>>>,
    fail_streams: Arc<Mutex<HashMap<String, usize>>>, // Add this field
}

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

impl FakeStorage {
    pub fn new() -> Self {
        FakeStorage {
            data: Arc::new(Mutex::new(HashMap::new())),
            fail_chunks: Arc::new(Mutex::new(HashMap::new())),
            fail_blobs: Arc::new(Mutex::new(HashMap::new())),
            fail_streams: Arc::new(Mutex::new(HashMap::new())),
        }
    }

    /// Simulate a failure for a specific chunk of a blob.
    ///
    /// This state is being reset after a subsequent upload of the same blob.
    pub fn fake_failed_chunks(&self, hash: &str, chunks: Vec<u64>) {
        self.fail_chunks
            .lock()
            .unwrap()
            .insert(hash.to_string(), chunks);
    }

    /// Simulate a failure for a specific blob.
    ///
    /// This state is being reset after a subsequent upload of the same blob.
    pub fn fake_failed_download(&self, hash: &str) {
        self.fail_blobs
            .lock()
            .unwrap()
            .insert(hash.to_string(), true);
    }

    /// Simulate a failure in byte stream at specified position.
    /// The stream will provide bytes until the specified position and then fail.
    pub fn fake_failed_stream(&self, hash: &str, fail_at: usize) {
        self.fail_streams
            .lock()
            .unwrap()
            .insert(hash.to_string(), fail_at);
    }
}

#[derive(Clone)]
pub struct FakeChunkIdMapper {
    hash: String,
    num_chunks: u64,
}

impl ChunkIdMapper<u64> for FakeChunkIdMapper {
    fn index_to_id(&self, index: u64) -> Result<u64, StorageError> {
        if index >= self.num_chunks {
            return Err(StorageError::ChunkNotFound(
                index.to_string(),
                self.hash.clone(),
                "Chunk index out of bounds".to_string(),
            ));
        }
        Ok(index)
    }

    fn id_to_index(&self, chunk_id: &u64) -> Result<u64, StorageError> {
        if *chunk_id >= self.num_chunks {
            return Err(StorageError::ChunkNotFound(
                chunk_id.to_string(),
                self.hash.clone(),
                "Chunk id out of bounds".to_string(),
            ));
        }
        Ok(*chunk_id)
    }
}

#[async_trait]
impl Storage for FakeStorage {
    type ChunkId = u64;
    type ChunkIdMapper = FakeChunkIdMapper;

    async fn upload_bytes(
        &self,
        bytes: impl Into<Bytes> + Send,
    ) -> Result<UploadResult, StorageError> {
        let bytes = bytes.into();
        let size = bytes.len();

        let mut hasher = Sha256::new();
        hasher.update(&bytes);
        let hash = hasher.finalize();

        let multihash = Code::Sha2_256.wrap(&hash).unwrap();
        let cid = Cid::new_v1(0x55, multihash);
        let hash_str = cid.to_string();

        let chunks = bytes
            .chunks(CHUNK_SIZE)
            .map(Bytes::copy_from_slice)
            .collect();
        self.data.lock().unwrap().insert(hash_str.clone(), chunks);

        self.fail_blobs.lock().unwrap().remove(&hash_str);
        self.fail_chunks.lock().unwrap().remove(&hash_str);
        self.fail_streams.lock().unwrap().remove(&hash_str);

        let mut info = std::collections::HashMap::new();
        info.insert("tag".to_string(), format!("tag-{}", hash_str));

        Ok(UploadResult {
            hash: hash_str,
            size: size as u64,
            info,
        })
    }

    async fn chunk_id_mapper(&self, hash: &str) -> Result<FakeChunkIdMapper, StorageError> {
        let num_chunks = match self.data.lock().unwrap().get(hash) {
            Some(chunks) => chunks.len() as u64,
            None => return Err(StorageError::BlobNotFound(hash.to_string())),
        };

        Ok(FakeChunkIdMapper {
            hash: hash.to_string(),
            num_chunks,
        })
    }

    async fn download_bytes(&self, hash: &str) -> Result<ByteStream, StorageError> {
        if self.fail_blobs.lock().unwrap().get(hash).is_some() {
            return Err(StorageError::BlobNotFound(hash.to_string()));
        }

        let data = self
            .data
            .lock()
            .unwrap()
            .get(hash)
            .map(|chunks| chunks.concat())
            .ok_or_else(|| StorageError::BlobNotFound(hash.to_string()))?;

        let fail_at = self.fail_streams.lock().unwrap().get(hash).cloned();

        // Return a stream that will fail at the specified position
        if let Some(fail_pos) = fail_at {
            let mut chunks = Vec::new();
            let mut remaining = fail_pos;

            // Only output complete chunks that fit within fail_pos
            while remaining >= CHUNK_SIZE {
                let position = fail_pos - remaining;
                chunks.push(Ok(Bytes::copy_from_slice(
                    &data[position..position + CHUNK_SIZE],
                )));
                remaining -= CHUNK_SIZE;
            }

            // Add any remaining partial chunk before failure
            if remaining > 0 {
                let position = fail_pos - remaining;
                chunks.push(Ok(Bytes::copy_from_slice(&data[position..fail_pos])));
            }

            // Add the failure
            chunks.push(Err(StorageError::Other(format!(
                "Stream failed at position {}",
                fail_pos
            ))));

            Ok(Box::pin(futures::stream::iter(chunks)))
        } else {
            // Normal stream without failures
            let chunks = data
                .chunks(CHUNK_SIZE)
                .map(|chunk| Ok(Bytes::copy_from_slice(chunk)))
                .collect::<Vec<_>>();
            Ok(Box::pin(futures::stream::iter(chunks)))
        }
    }

    async fn iter_chunks(&self, hash: &str) -> Result<ChunkStream<Self::ChunkId>, StorageError> {
        let chunks = self
            .data
            .lock()
            .unwrap()
            .get(hash)
            .cloned()
            .ok_or_else(|| StorageError::BlobNotFound(hash.to_string()))?;

        let fail_chunks = self
            .fail_chunks
            .lock()
            .unwrap()
            .get(hash)
            .cloned()
            .unwrap_or_default();

        let hash = hash.to_string();
        let stream = stream::iter(chunks.into_iter().enumerate().map(move |(index, chunk)| {
            let index = index as Self::ChunkId;
            if fail_chunks.contains(&index) {
                (
                    index,
                    Err(StorageError::ChunkNotFound(
                        index.to_string(),
                        hash.clone(),
                        "Simulated chunk failure".to_string(),
                    )),
                )
            } else {
                (index, Ok(chunk))
            }
        }));

        Ok(Box::pin(stream))
    }

    async fn download_chunk(&self, hash: &str, chunk_id: u64) -> Result<Bytes, StorageError> {
        let fail_chunks = self
            .fail_chunks
            .lock()
            .unwrap()
            .get(hash)
            .cloned()
            .unwrap_or_default();

        if fail_chunks.contains(&chunk_id) {
            return Err(StorageError::ChunkNotFound(
                chunk_id.to_string(),
                hash.to_string(),
                "Simulated chunk failure".to_string(),
            ));
        }

        let data = self.data.lock().unwrap();
        let chunks = data.get(hash);
        if let Some(chunks) = chunks {
            if chunk_id >= chunks.len() as u64 {
                return Err(StorageError::ChunkNotFound(
                    chunk_id.to_string(),
                    hash.to_string(),
                    "Chunk not found".to_string(),
                ));
            }
        }
        chunks
            .map(|chunks| chunks[chunk_id as usize].clone())
            .ok_or_else(|| StorageError::BlobNotFound(hash.to_string()))
    }
}

#[derive(Clone)]
pub struct DummyStorage;

#[derive(Clone)]
pub struct DummyChunkIdMapper {}

impl ChunkIdMapper<u64> for DummyChunkIdMapper {
    fn index_to_id(&self, index: u64) -> Result<u64, StorageError> {
        Ok(index)
    }

    fn id_to_index(&self, chunk_id: &u64) -> Result<u64, StorageError> {
        Ok(*chunk_id)
    }
}

#[async_trait]
impl Storage for DummyStorage {
    type ChunkId = u64;
    type ChunkIdMapper = DummyChunkIdMapper;

    async fn upload_bytes(
        &self,
        bytes: impl Into<Bytes> + Send,
    ) -> Result<UploadResult, StorageError> {
        let bytes = bytes.into();
        let mut info = std::collections::HashMap::new();
        info.insert("mock_info".to_string(), "dummy_storage".to_string());

        Ok(UploadResult {
            hash: "dummy_hash".to_string(),
            info,
            size: bytes.len() as u64,
        })
    }

    async fn chunk_id_mapper(&self, _: &str) -> Result<Self::ChunkIdMapper, StorageError> {
        Ok(DummyChunkIdMapper {})
    }

    async fn download_bytes(&self, _: &str) -> Result<ByteStream, StorageError> {
        Ok(Box::pin(futures::stream::once(ready(Ok(Bytes::from(
            "dummy data",
        ))))))
    }

    async fn iter_chunks(&self, _: &str) -> Result<ChunkStream<Self::ChunkId>, StorageError> {
        let chunks = vec![Bytes::from("dummy data")];

        let stream = futures::stream::iter(
            chunks
                .into_iter()
                .enumerate()
                .map(move |(index, chunk)| (index as u64, Ok(chunk))),
        );

        Ok(Box::pin(stream))
    }

    async fn download_chunk(&self, _: &str, _: Self::ChunkId) -> Result<Bytes, StorageError> {
        Ok(Bytes::from("dummy data"))
    }
}

#[derive(Clone)]
pub struct StubStorage {
    upload_bytes_result: Result<UploadResult, StorageError>,
    download_bytes_result: HashMap<Option<String>, Result<Bytes, StorageError>>,
    iter_chunks_result: Vec<(u64, Result<Bytes, StorageError>)>,
    download_chunk_result: HashMap<Option<(String, u64)>, Result<Bytes, StorageError>>,
    chunk_id_mapper_result: Result<DummyChunkIdMapper, StorageError>,
}

impl Default for StubStorage {
    fn default() -> Self {
        let mut info = std::collections::HashMap::new();
        info.insert("mock_info".to_string(), "stub_storage".to_string());

        Self {
            upload_bytes_result: Ok(UploadResult {
                hash: "dummy_hash".to_string(),
                info,
                size: 0,
            }),
            download_bytes_result: HashMap::new(),
            iter_chunks_result: vec![(0, Ok(Bytes::from("dummy data")))],
            download_chunk_result: HashMap::new(),
            chunk_id_mapper_result: Ok(DummyChunkIdMapper {}),
        }
    }
}

impl StubStorage {
    pub fn stub_upload_bytes(&mut self, result: Result<UploadResult, StorageError>) {
        self.upload_bytes_result = result;
    }

    pub fn stub_download_bytes(
        &mut self,
        hash: Option<String>,
        result: Result<Bytes, StorageError>,
    ) {
        self.download_bytes_result.insert(hash, result);
    }

    pub fn stub_iter_chunks(&mut self, chunks: Vec<(u64, Result<Bytes, StorageError>)>) {
        self.iter_chunks_result = chunks;
    }

    pub fn stub_download_chunk(
        &mut self,
        params: Option<(String, u64)>,
        result: Result<Bytes, StorageError>,
    ) {
        self.download_chunk_result.insert(params, result);
    }

    pub fn stub_chunk_id_mapper(&mut self, result: Result<DummyChunkIdMapper, StorageError>) {
        self.chunk_id_mapper_result = result;
    }
}

#[async_trait]
impl Storage for StubStorage {
    type ChunkId = u64;
    type ChunkIdMapper = DummyChunkIdMapper;

    async fn upload_bytes(
        &self,
        _bytes: impl Into<Bytes> + Send,
    ) -> Result<UploadResult, StorageError> {
        self.upload_bytes_result.clone()
    }

    async fn download_bytes(&self, hash: &str) -> Result<ByteStream, StorageError> {
        let result = self
            .download_bytes_result
            .get(&Some(hash.to_string()))
            .or_else(|| self.download_bytes_result.get(&None))
            .cloned()
            .unwrap_or_else(|| Ok(Bytes::from("dummy data")))?;

        Ok(Box::pin(futures::stream::once(ready(Ok(result)))))
    }

    async fn iter_chunks(&self, _: &str) -> Result<ChunkStream<Self::ChunkId>, StorageError> {
        Ok(Box::pin(futures::stream::iter(
            self.iter_chunks_result.clone().into_iter(),
        )))
    }

    async fn download_chunk(
        &self,
        hash: &str,
        chunk_id: Self::ChunkId,
    ) -> Result<Bytes, StorageError> {
        self.download_chunk_result
            .get(&Some((hash.to_string(), chunk_id)))
            .or_else(|| self.download_chunk_result.get(&None))
            .cloned()
            .unwrap_or_else(|| Ok(Bytes::from("dummy data")))
    }

    async fn chunk_id_mapper(&self, _: &str) -> Result<Self::ChunkIdMapper, StorageError> {
        self.chunk_id_mapper_result.clone()
    }
}

#[derive(Clone)]
pub struct SpyStorage {
    inner: StubStorage,
    upload_bytes_calls: Arc<RwLock<Vec<Bytes>>>,
    download_bytes_calls: Arc<RwLock<Vec<String>>>,
    iter_chunks_calls: Arc<RwLock<Vec<String>>>,
    download_chunk_calls: Arc<RwLock<Vec<(String, u64)>>>,
    chunk_id_mapper_calls: Arc<RwLock<Vec<String>>>,
}

impl std::ops::Deref for SpyStorage {
    type Target = StubStorage;

    fn deref(&self) -> &Self::Target {
        &self.inner
    }
}

impl std::ops::DerefMut for SpyStorage {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.inner
    }
}

impl Default for SpyStorage {
    fn default() -> Self {
        Self {
            inner: StubStorage::default(),
            upload_bytes_calls: Arc::new(RwLock::new(Vec::new())),
            download_bytes_calls: Arc::new(RwLock::new(Vec::new())),
            iter_chunks_calls: Arc::new(RwLock::new(Vec::new())),
            download_chunk_calls: Arc::new(RwLock::new(Vec::new())),
            chunk_id_mapper_calls: Arc::new(RwLock::new(Vec::new())),
        }
    }
}

impl SpyStorage {
    pub fn upload_bytes_calls(&self) -> Vec<Bytes> {
        self.upload_bytes_calls.read().unwrap().clone()
    }

    pub fn download_bytes_calls(&self) -> Vec<String> {
        self.download_bytes_calls.read().unwrap().clone()
    }

    pub fn iter_chunks_calls(&self) -> Vec<String> {
        self.iter_chunks_calls.read().unwrap().clone()
    }

    pub fn download_chunk_calls(&self) -> Vec<(String, u64)> {
        self.download_chunk_calls.read().unwrap().clone()
    }

    pub fn chunk_id_mapper_calls(&self) -> Vec<String> {
        self.chunk_id_mapper_calls.read().unwrap().clone()
    }
}

#[async_trait]
impl Storage for SpyStorage {
    type ChunkId = u64;
    type ChunkIdMapper = DummyChunkIdMapper;

    async fn upload_bytes(
        &self,
        bytes: impl Into<Bytes> + Send,
    ) -> Result<UploadResult, StorageError> {
        let bytes = bytes.into();
        self.upload_bytes_calls.write().unwrap().push(bytes.clone());
        self.inner.upload_bytes(bytes).await
    }

    async fn download_bytes(&self, hash: &str) -> Result<ByteStream, StorageError> {
        self.download_bytes_calls
            .write()
            .unwrap()
            .push(hash.to_string());
        self.inner.download_bytes(hash).await
    }

    async fn iter_chunks(&self, hash: &str) -> Result<ChunkStream<Self::ChunkId>, StorageError> {
        self.iter_chunks_calls
            .write()
            .unwrap()
            .push(hash.to_string());
        self.inner.iter_chunks(hash).await
    }

    async fn download_chunk(
        &self,
        hash: &str,
        chunk_id: Self::ChunkId,
    ) -> Result<Bytes, StorageError> {
        self.download_chunk_calls
            .write()
            .unwrap()
            .push((hash.to_string(), chunk_id));
        self.inner.download_chunk(hash, chunk_id).await
    }

    async fn chunk_id_mapper(&self, hash: &str) -> Result<Self::ChunkIdMapper, StorageError> {
        self.chunk_id_mapper_calls
            .write()
            .unwrap()
            .push(hash.to_string());
        self.inner.chunk_id_mapper(hash).await
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use futures::{Stream, StreamExt};
    use std::task::{Context, Poll};

    #[tokio::test]
    async fn test_upload_and_download_bytes() -> Result<()> {
        let storage = FakeStorage::new();
        let data = b"Hello, world!".to_vec();
        let result = storage.upload_bytes(data.clone()).await?;

        // Verify the UploadResult contains the expected fields
        assert!(!result.hash.is_empty());
        assert!(result.info.contains_key("tag"));
        assert_eq!(
            *result.info.get("tag").unwrap(),
            format!("tag-{}", result.hash)
        );

        let mut stream = storage.download_bytes(&result.hash).await?;
        let mut downloaded = Vec::new();
        while let Some(chunk) = stream.next().await {
            downloaded.extend_from_slice(&chunk?);
        }
        assert_eq!(data, downloaded);
        Ok(())
    }

    #[tokio::test]
    async fn test_download_non_existent_blob() {
        let storage = FakeStorage::new();
        let result = storage.download_bytes("non_existent_hash").await;
        assert!(matches!(result, Err(StorageError::BlobNotFound(_))));
    }

    #[tokio::test]
    async fn test_fake_failed_download() -> Result<()> {
        let storage = FakeStorage::new();
        let data = b"Test data".to_vec();
        let result = storage.upload_bytes(data).await?;

        storage.fake_failed_download(&result.hash);
        let result = storage.download_bytes(&result.hash).await;
        assert!(matches!(result, Err(StorageError::BlobNotFound(_))));
        Ok(())
    }

    #[tokio::test]
    async fn faked_failed_blob_download_after_upload_should_be_available() -> Result<()> {
        let storage = FakeStorage::new();
        let data = b"Test data".to_vec();
        let result = storage.upload_bytes(data.clone()).await?;

        let dl_result = storage.download_bytes(&result.hash).await;
        assert!(dl_result.is_ok());

        storage.fake_failed_chunks(&result.hash, vec![0]);
        let new_result = storage.upload_bytes(data).await?;

        let chunk_result = storage.download_chunk(&new_result.hash, 0).await;
        assert!(
            chunk_result.is_ok(),
            "Expected download to succeed after upload"
        );
        Ok(())
    }

    #[tokio::test]
    async fn faked_failed_chunk_download_after_upload_should_be_available() -> Result<()> {
        let storage = FakeStorage::new();
        let data = b"Test data".to_vec();
        let result = storage.upload_bytes(data.clone()).await?;

        let dl_result = storage.download_bytes(&result.hash).await;
        assert!(dl_result.is_ok());

        storage.fake_failed_download(&result.hash);
        let new_result = storage.upload_bytes(data).await?;

        let dl_result = storage.download_bytes(&new_result.hash).await;
        assert!(
            dl_result.is_ok(),
            "Expected download to succeed after upload"
        );
        Ok(())
    }

    #[tokio::test]
    async fn test_iter_chunks() -> Result<()> {
        let storage = FakeStorage::new();
        let data = (0..3000).map(|i| (i % 256) as u8).collect::<Vec<u8>>(); // 3 chunks with predictable content
        let result = storage.upload_bytes(data.clone()).await?;

        let mut stream = storage.iter_chunks(&result.hash).await?;
        let mut chunk_count = 0;
        let mut total_bytes = 0;

        while let Some((_, chunk_result)) = stream.next().await {
            let chunk = chunk_result?;
            chunk_count += 1;

            // Check the content of each chunk
            let start = total_bytes;
            let end = total_bytes + chunk.len();
            assert_eq!(
                &data[start..end],
                &chunk[..],
                "Chunk {} content mismatch",
                chunk_count
            );

            total_bytes += chunk.len();
        }

        assert_eq!(chunk_count, 3, "Expected 3 chunks");
        assert_eq!(total_bytes, data.len(), "Total bytes mismatch");

        let last_chunk_size = data.len() % CHUNK_SIZE;
        assert_eq!(
            total_bytes % CHUNK_SIZE,
            last_chunk_size,
            "Last chunk size mismatch"
        );

        Ok(())
    }

    #[tokio::test]
    async fn test_iter_chunks_non_existent_blob() {
        let storage = FakeStorage::new();
        let result = storage.iter_chunks("non_existent_hash").await;
        assert!(matches!(result, Err(StorageError::BlobNotFound(_))));
    }

    #[tokio::test]
    async fn test_fake_failed_chunks() -> Result<()> {
        let storage = FakeStorage::new();
        let data = vec![0u8; 3000]; // 3 chunks
        let result = storage.upload_bytes(data).await?;

        let fail_chunk_index = 1;
        storage.fake_failed_chunks(&result.hash, vec![fail_chunk_index]);

        let mut stream = storage.iter_chunks(&result.hash).await?;
        let mut chunk_results = Vec::new();

        while let Some(chunk_result) = stream.next().await {
            chunk_results.push(chunk_result);
        }

        assert_eq!(chunk_results.len(), 3, "Expected 3 chunk results");

        for (index, chunk_result) in chunk_results {
            if index == fail_chunk_index {
                assert!(chunk_result.is_err(), "Expected chunk {} to fail", index);
                let err = chunk_result.unwrap_err();
                if let StorageError::ChunkNotFound(index_str, hash, _) = err {
                    assert_eq!(index_str, "1");
                    assert_eq!(hash, result.hash);
                } else {
                    panic!("Expected ChunkNotFound error");
                }
            } else {
                assert!(chunk_result.is_ok(), "Expected chunk {} to succeed", index);
            }
        }

        Ok(())
    }

    #[tokio::test]
    async fn test_large_upload() -> Result<()> {
        let storage = FakeStorage::new();
        let data = vec![0u8; 10 * CHUNK_SIZE * CHUNK_SIZE]; // 10 MB
        let result = storage.upload_bytes(data.clone()).await?;

        let mut stream = storage.download_bytes(&result.hash).await?;
        let mut downloaded = Vec::new();
        while let Some(chunk) = stream.next().await {
            downloaded.extend_from_slice(&chunk?);
        }
        assert_eq!(data.len(), downloaded.len());
        assert_eq!(data, downloaded);
        Ok(())
    }

    #[tokio::test]
    async fn test_empty_upload() -> Result<()> {
        let storage = FakeStorage::new();
        let data = vec![];
        let result = storage.upload_bytes(data.clone()).await?;

        let mut stream = storage.download_bytes(&result.hash).await?;
        let mut downloaded = Vec::new();
        while let Some(chunk) = stream.next().await {
            downloaded.extend_from_slice(&chunk?);
        }
        assert_eq!(data, downloaded);
        Ok(())
    }

    #[tokio::test]
    async fn test_concurrent_uploads() -> Result<()> {
        let storage = FakeStorage::new();
        let data1 = b"Data 1".to_vec();
        let data2 = b"Data 2".to_vec();

        let (result1, result2) = tokio::join!(
            storage.upload_bytes(data1.clone()),
            storage.upload_bytes(data2.clone())
        );

        let result1 = result1?;
        let result2 = result2?;
        assert_ne!(result1.hash, result2.hash);

        let (stream1, stream2) = tokio::join!(
            storage.download_bytes(&result1.hash),
            storage.download_bytes(&result2.hash)
        );

        // Collect chunks from both streams concurrently
        let (chunks1, chunks2) = futures::future::join(
            async {
                let mut result = Vec::new();
                let mut stream = stream1?;
                while let Some(chunk) = stream.next().await {
                    result.extend_from_slice(&chunk?);
                }
                Result::<Vec<u8>, StorageError>::Ok(result)
            },
            async {
                let mut result = Vec::new();
                let mut stream = stream2?;
                while let Some(chunk) = stream.next().await {
                    result.extend_from_slice(&chunk?);
                }
                Result::<Vec<u8>, StorageError>::Ok(result)
            },
        )
        .await;

        assert_eq!(data1, chunks1?);
        assert_eq!(data2, chunks2?);
        Ok(())
    }

    #[tokio::test]
    async fn test_download_chunk() -> Result<()> {
        let storage = FakeStorage::new();
        let data = vec![0u8; 3000]; // 3 chunks
        let result = storage.upload_bytes(data.clone()).await?;

        for chunk_id in 0..3 {
            let chunk = storage.download_chunk(&result.hash, chunk_id).await?;
            let b = chunk_id as usize * CHUNK_SIZE;
            let e = (b + CHUNK_SIZE).min(data.len());
            let expected_chunk = &data[b..e];
            assert_eq!(chunk, expected_chunk);
        }

        Ok(())
    }

    #[tokio::test]
    async fn test_download_chunk_non_existent_blob() {
        let storage = FakeStorage::new();
        let result = storage.download_chunk("non_existent_hash", 0).await;
        assert!(matches!(result, Err(StorageError::BlobNotFound(_))));
    }

    #[tokio::test]
    async fn test_download_chunk_out_of_bounds() -> Result<()> {
        let storage = FakeStorage::new();
        let data = vec![0u8; 3000]; // 3 chunks
        let result = storage.upload_bytes(data).await?;

        let err = storage.download_chunk(&result.hash, 3).await.unwrap_err();
        if let StorageError::ChunkNotFound(chunk_id, hash, _) = err {
            assert_eq!(chunk_id, "3");
            assert_eq!(hash, result.hash);
        } else {
            panic!("Expected ChunkNotFound error");
        }
        Ok(())
    }

    #[tokio::test]
    async fn test_fake_failed_chunk_download() -> Result<()> {
        let storage = FakeStorage::new();
        let data = vec![0u8; 3000]; // 3 chunks
        let result = storage.upload_bytes(data).await?;

        let fail_chunk_index = 1;
        storage.fake_failed_chunks(&result.hash, vec![fail_chunk_index]);

        for chunk_id in 0..3 {
            let chunk_result = storage.download_chunk(&result.hash, chunk_id).await;
            if chunk_id == fail_chunk_index {
                let err = chunk_result.unwrap_err();
                if let StorageError::ChunkNotFound(chunk_id_str, hash, _) = err {
                    assert_eq!(chunk_id_str, "1");
                    assert_eq!(hash, result.hash);
                } else {
                    panic!("Expected ChunkNotFound error");
                }
            } else {
                assert!(
                    chunk_result.is_ok(),
                    "Expected chunk {} to download successfully",
                    chunk_id
                );
            }
        }

        Ok(())
    }

    #[tokio::test]
    async fn test_chunk_id_mapper() -> Result<()> {
        let storage = FakeStorage::new();
        let data = vec![0u8; 3000]; // 3 chunks
        let result = storage.upload_bytes(data).await?;

        let mapper = storage.chunk_id_mapper(&result.hash).await?;
        assert_eq!(mapper.index_to_id(0)?, 0);
        assert_eq!(mapper.index_to_id(1)?, 1);
        assert_eq!(mapper.index_to_id(2)?, 2);
        assert!(
            matches!(mapper.id_to_index(&3), Err(StorageError::ChunkNotFound(chunk_id, hash, _)) if hash == result.hash && chunk_id == "3"),
        );

        let res = storage.chunk_id_mapper("invalid").await;
        assert!(res.is_err(), "Expected error");
        assert!(matches!(res.err().unwrap(), StorageError::BlobNotFound(h) if h == "invalid"));
        Ok(())
    }

    #[tokio::test]
    async fn test_fake_failed_stream() -> Result<()> {
        struct TestCase {
            total_size: usize,
            fail_at: usize,
        }

        let test_cases = [
            TestCase {
                total_size: 3000, // ~3 chunks
                fail_at: 1500,    // Middle of second chunk
            },
            TestCase {
                total_size: 1000, // <1 chunk
                fail_at: 500,     // Middle of first chunk
            },
            TestCase {
                total_size: 5000, // ~5 chunks
                fail_at: 3072,    // End of third chunk
            },
            TestCase {
                total_size: 2048, // 2 chunks exactly
                fail_at: 1024,    // At first chunk boundary
            },
        ];

        for (i, test_case) in test_cases.iter().enumerate() {
            let storage = FakeStorage::new();
            let data = vec![i as u8; test_case.total_size];
            let result = storage.upload_bytes(data.clone()).await?;

            storage.fake_failed_stream(&result.hash, test_case.fail_at);

            let mut stream = storage.download_bytes(&result.hash).await?;
            let mut downloaded = Vec::new();
            let mut error_occurred = false;

            while let Some(chunk_result) = stream.next().await {
                match chunk_result {
                    Ok(chunk) => downloaded.extend_from_slice(&chunk),
                    Err(_) => {
                        error_occurred = true;
                        break;
                    }
                }
            }

            assert!(
                error_occurred,
                "Case {}: Expected stream to fail at {} bytes",
                i, test_case.fail_at
            );
            assert_eq!(
                downloaded.len(),
                test_case.fail_at,
                "Case {}: Expected exactly {} bytes before failure",
                i,
                test_case.fail_at
            );
            assert_eq!(
                &data[..test_case.fail_at],
                &downloaded[..],
                "Case {}: Data mismatch before failure point",
                i
            );
        }

        Ok(())
    }

    #[tokio::test]
    async fn faked_failed_stream_after_upload_should_be_available() -> Result<()> {
        let storage = FakeStorage::new();
        let data = vec![0u8; 3000]; // 3 chunks
        let result = storage.upload_bytes(data.clone()).await?;

        // First verify stream fails at 1500 bytes
        storage.fake_failed_stream(&result.hash, 1500);
        let mut stream = storage.download_bytes(&result.hash).await?;
        let mut downloaded = Vec::new();
        while let Some(chunk_result) = stream.next().await {
            match chunk_result {
                Ok(chunk) => downloaded.extend_from_slice(&chunk),
                Err(_) => break,
            }
        }
        assert_eq!(
            downloaded.len(),
            1500,
            "Stream should have failed at 1500 bytes"
        );

        // Now upload the same data again and verify stream works completely
        let new_result = storage.upload_bytes(data.clone()).await?;
        let mut stream = storage.download_bytes(&new_result.hash).await?;
        let mut downloaded = Vec::with_capacity(3000);
        while let Some(chunk_result) = stream.next().await {
            downloaded.extend_from_slice(&chunk_result?);
        }
        assert_eq!(
            downloaded.len(),
            3000,
            "Stream should complete after re-upload"
        );
        assert_eq!(data, downloaded, "Downloaded data should match original");

        Ok(())
    }

    #[tokio::test]
    async fn test_upload_and_download_bytes_with_poll() -> Result<()> {
        let storage = FakeStorage::new();
        let data = vec![1u8; 3000];
        let result = storage.upload_bytes(data.clone()).await?;

        let stream = storage.download_bytes(&result.hash).await?;
        let mut pinned = Box::pin(stream);
        let mut downloaded = Vec::new();
        let mut error_received = false;

        let waker = futures::task::noop_waker();
        let mut cx = Context::from_waker(&waker);

        loop {
            match pinned.as_mut().poll_next(&mut cx) {
                Poll::Ready(Some(Ok(chunk))) => downloaded.extend_from_slice(&chunk),
                Poll::Ready(Some(Err(_))) => {
                    error_received = true;
                    break;
                }
                Poll::Ready(None) => break,
                Poll::Pending => panic!("Stream should not be pending in this test"),
            }
        }

        assert!(!error_received, "No error should be received");
        assert_eq!(data, downloaded);
        Ok(())
    }

    #[tokio::test]
    async fn test_upload_and_download_bytes_with_poll_failed_stream() -> Result<()> {
        let storage = FakeStorage::new();
        let data = vec![1u8; 3000];
        let result = storage.upload_bytes(data.clone()).await?;

        storage.fake_failed_stream(&result.hash, 1500);

        let stream = storage.download_bytes(&result.hash).await?;
        let mut pinned = Box::pin(stream);
        let mut downloaded = Vec::new();
        let mut error_received = false;

        let waker = futures::task::noop_waker();
        let mut cx = Context::from_waker(&waker);

        loop {
            match pinned.as_mut().poll_next(&mut cx) {
                Poll::Ready(Some(Ok(chunk))) => downloaded.extend_from_slice(&chunk),
                Poll::Ready(Some(Err(_))) => {
                    error_received = true;
                    break;
                }
                Poll::Ready(None) => break,
                Poll::Pending => panic!("Stream should not be pending in this test"),
            }
        }

        assert!(error_received, "Error should be received");
        assert_eq!(downloaded.len(), 1500, "Should receive exactly 1500 bytes");
        assert_eq!(
            &data[..1500],
            &downloaded[..],
            "Data mismatch before failure"
        );
        Ok(())
    }
}