hashtree-core 0.2.41

Simple content-addressed merkle tree with KV storage
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
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
//! Extensive tests for HashTree - unified merkle tree operations
//!
//! Tests matching and exceeding hashtree-ts test coverage

use std::collections::HashMap;
use std::sync::Arc;

use futures::StreamExt;
use hashtree_core::{
    to_hex, Cid, DirEntry, HashTree, HashTreeConfig, HashTreeError, Link, LinkType, MemoryStore,
    Store,
};

fn make_tree() -> (Arc<MemoryStore>, HashTree<MemoryStore>) {
    let store = Arc::new(MemoryStore::new());
    // Use public (unencrypted) mode for these tests
    let tree = HashTree::new(HashTreeConfig::new(store.clone()).public());
    (store, tree)
}

fn make_tree_with_chunk_size(chunk_size: usize) -> (Arc<MemoryStore>, HashTree<MemoryStore>) {
    let store = Arc::new(MemoryStore::new());
    // Use public (unencrypted) mode for these tests
    let tree = HashTree::new(
        HashTreeConfig::new(store.clone())
            .public()
            .with_chunk_size(chunk_size),
    );
    (store, tree)
}

fn make_encrypted_tree() -> (Arc<MemoryStore>, HashTree<MemoryStore>) {
    let store = Arc::new(MemoryStore::new());
    // Default is encrypted: true
    let tree = HashTree::new(HashTreeConfig::new(store.clone()));
    (store, tree)
}

fn make_encrypted_tree_with_chunk_size(
    chunk_size: usize,
) -> (Arc<MemoryStore>, HashTree<MemoryStore>) {
    let store = Arc::new(MemoryStore::new());
    let tree = HashTree::new(HashTreeConfig::new(store.clone()).with_chunk_size(chunk_size));
    (store, tree)
}

// ============ CREATE TESTS ============

mod create {
    use super::*;

    #[tokio::test]
    async fn test_store_small_file_as_single_blob() {
        let (_store, tree) = make_tree();

        let data = b"hello world";
        let (cid, size) = tree.put_file(data).await.unwrap();

        assert_eq!(size, 11);
        assert_eq!(cid.hash.len(), 32);

        let retrieved = tree.read_file(&cid.hash).await.unwrap();
        assert_eq!(retrieved, Some(data.to_vec()));
    }

    #[tokio::test]
    async fn test_chunk_large_files() {
        let (_, tree) = make_tree_with_chunk_size(10);
        let data = b"this is a longer message that will be chunked";

        let (cid, size) = tree.put_file(data).await.unwrap();
        assert_eq!(size, data.len() as u64);

        let retrieved = tree.read_file(&cid.hash).await.unwrap();
        assert_eq!(retrieved, Some(data.to_vec()));
    }

    #[tokio::test]
    async fn test_create_empty_directory() {
        let (_store, tree) = make_tree();

        let dir_cid = tree.put_directory(vec![]).await.unwrap();

        let entries = tree.list_directory(&dir_cid).await.unwrap();
        assert_eq!(entries.len(), 0);
    }

    #[tokio::test]
    async fn test_create_directory_with_entries() {
        let (_store, tree) = make_tree();

        let (file1_cid, _) = tree.put_file(b"content1").await.unwrap();
        let (file2_cid, _) = tree.put_file(b"content2").await.unwrap();

        let dir_cid = tree
            .put_directory(vec![
                DirEntry::new("a.txt", file1_cid.hash).with_size(8),
                DirEntry::new("b.txt", file2_cid.hash).with_size(8),
            ])
            .await
            .unwrap();

        let entries = tree.list_directory(&dir_cid).await.unwrap();
        assert_eq!(entries.len(), 2);

        let mut names: Vec<_> = entries.iter().map(|e| e.name.as_str()).collect();
        names.sort();
        assert_eq!(names, vec!["a.txt", "b.txt"]);
    }

    #[tokio::test]
    async fn test_directory_entries_sorted() {
        let (_store, tree) = make_tree();

        let file_hash = tree.put_blob(b"data").await.unwrap();

        let dir_hash = tree
            .put_directory(vec![
                DirEntry::new("zebra", file_hash),
                DirEntry::new("apple", file_hash),
                DirEntry::new("mango", file_hash),
            ])
            .await
            .unwrap();

        let node = tree.get_tree_node(&dir_hash.hash).await.unwrap().unwrap();
        let names: Vec<_> = node.links.iter().filter_map(|l| l.name.clone()).collect();
        // Should be sorted alphabetically
        assert_eq!(names, vec!["apple", "mango", "zebra"]);
    }

    #[tokio::test]
    async fn test_put_tree_node_with_link_meta() {
        let (_store, tree) = make_tree();

        let file_hash = tree.put_blob(b"data").await.unwrap();

        let mut link_meta = HashMap::new();
        link_meta.insert("version".to_string(), serde_json::json!(2));
        link_meta.insert("author".to_string(), serde_json::json!("test"));

        let node_hash = tree
            .put_tree_node(vec![Link::new(file_hash)
                .with_name("file.txt")
                .with_size(4)
                .with_meta(link_meta)])
            .await
            .unwrap();

        let node = tree.get_tree_node(&node_hash).await.unwrap().unwrap();
        assert_eq!(node.links.len(), 1);
        let m = node.links[0].meta.as_ref().unwrap();
        assert_eq!(m.get("version"), Some(&serde_json::json!(2)));
        assert_eq!(m.get("author"), Some(&serde_json::json!("test")));
    }

    #[tokio::test]
    async fn test_file_deduplication() {
        let (store, tree) = make_tree_with_chunk_size(100);

        let repeated_chunk = vec![42u8; 100];
        let data: Vec<u8> = repeated_chunk.iter().cycle().take(500).cloned().collect();

        let (cid, size) = tree.put_file(&data).await.unwrap();
        assert_eq!(size, 500);

        // Store should have fewer items due to deduplication
        // (1 unique chunk + tree nodes)
        let store_size = store.size();
        assert!(
            store_size < 5,
            "Expected deduplication, got {} items",
            store_size
        );

        // Verify can still read back
        let retrieved = tree.read_file(&cid.hash).await.unwrap().unwrap();
        assert_eq!(retrieved.len(), 500);
    }

    #[tokio::test]
    async fn test_nested_directory_structure() {
        let (_store, tree) = make_tree();

        let file_hash = tree.put_blob(b"deep content").await.unwrap();

        let deep_dir = tree
            .put_directory(vec![DirEntry::new("file.txt", file_hash).with_size(12)])
            .await
            .unwrap();

        let mid_dir = tree
            .put_directory(vec![DirEntry::new("deep", deep_dir.hash)])
            .await
            .unwrap();

        let root_dir = tree
            .put_directory(vec![DirEntry::new("mid", mid_dir.hash)])
            .await
            .unwrap();

        // Verify structure
        let resolved = tree
            .resolve_path(&root_dir, "mid/deep/file.txt")
            .await
            .unwrap();
        assert_eq!(resolved.map(|c| c.hash), Some(file_hash));
    }
}

// ============ READ TESTS ============

mod read {
    use super::*;

    #[tokio::test]
    async fn test_read_file() {
        let (_store, tree) = make_tree();

        let data = b"test content";
        let (cid, _) = tree.put_file(data).await.unwrap();

        let read_data = tree.read_file(&cid.hash).await.unwrap();
        assert_eq!(read_data, Some(data.to_vec()));
    }

    #[tokio::test]
    async fn test_read_missing_file() {
        let (_store, tree) = make_tree();

        let missing_hash = [0u8; 32];
        let result = tree.read_file(&missing_hash).await.unwrap();
        assert!(result.is_none());
    }

    #[tokio::test]
    async fn test_list_directory() {
        let (_store, tree) = make_tree();

        let (file_cid, _) = tree.put_file(b"data").await.unwrap();
        let dir_cid = tree
            .put_directory(vec![DirEntry::new("file.txt", file_cid.hash).with_size(4)])
            .await
            .unwrap();

        let entries = tree.list_directory(&dir_cid).await.unwrap();
        assert_eq!(entries.len(), 1);
        assert_eq!(entries[0].name, "file.txt");
    }

    #[tokio::test]
    async fn test_resolve_path() {
        let (_store, tree) = make_tree();

        let (file_cid, _) = tree.put_file(b"nested").await.unwrap();
        let sub_dir_cid = tree
            .put_directory(vec![DirEntry::new("file.txt", file_cid.hash).with_size(6)])
            .await
            .unwrap();
        let root_cid = tree
            .put_directory(vec![DirEntry::new("subdir", sub_dir_cid.hash).with_size(6)])
            .await
            .unwrap();

        let resolved = tree
            .resolve_path(&root_cid, "subdir/file.txt")
            .await
            .unwrap();
        assert!(resolved.is_some());
        assert_eq!(to_hex(&resolved.unwrap().hash), to_hex(&file_cid.hash));
    }

    #[tokio::test]
    async fn test_resolve_path_missing() {
        let (_store, tree) = make_tree();

        let dir_cid = tree.put_directory(vec![]).await.unwrap();

        let resolved = tree
            .resolve_path(&dir_cid, "nonexistent/path")
            .await
            .unwrap();
        assert!(resolved.is_none());
    }

    #[tokio::test]
    async fn test_check_if_hash_is_directory() {
        let (_store, tree) = make_tree();

        let (file_cid, _) = tree.put_file(b"data").await.unwrap();
        let dir_hash = tree.put_directory(vec![]).await.unwrap().hash;

        assert!(!tree.is_directory(&file_cid.hash).await.unwrap());
        assert!(tree.is_directory(&dir_hash).await.unwrap());
    }

    #[tokio::test]
    async fn test_is_tree() {
        let (_store, tree) = make_tree();

        let blob_hash = tree.put_blob(b"raw data").await.unwrap();
        let dir_hash = tree.put_directory(vec![]).await.unwrap().hash;

        assert!(!tree.is_tree(&blob_hash).await.unwrap());
        assert!(tree.is_tree(&dir_hash).await.unwrap());
    }

    #[tokio::test]
    async fn test_get_size_blob() {
        let (_store, tree) = make_tree();

        let data = b"test data for size";
        let hash = tree.put_blob(data).await.unwrap();

        let size = tree.get_size(&hash).await.unwrap();
        assert_eq!(size, data.len() as u64);
    }

    #[tokio::test]
    async fn test_get_size_chunked_file() {
        let (_store, tree) = make_tree_with_chunk_size(100);

        let data = vec![0u8; 500];
        let (cid, _) = tree.put_file(&data).await.unwrap();

        let size = tree.get_size(&cid.hash).await.unwrap();
        assert_eq!(size, 500);
    }

    #[tokio::test]
    async fn test_read_file_chunks() {
        let (_store, tree) = make_tree_with_chunk_size(10);

        let data = b"hello world!!!";
        let (cid, _) = tree.put_file(data).await.unwrap();

        let chunks = tree.read_file_chunks(&cid.hash).await.unwrap();
        assert!(chunks.len() > 1); // Should be multiple chunks

        // Reconstruct
        let combined: Vec<u8> = chunks.into_iter().flatten().collect();
        assert_eq!(combined, data.to_vec());
    }

    #[tokio::test]
    async fn test_walk() {
        let (_store, tree) = make_tree();

        let f1 = tree.put_blob(b"1").await.unwrap();
        let f2 = tree.put_blob(b"23").await.unwrap();

        let sub_dir = tree
            .put_directory(vec![DirEntry::new("nested.txt", f2).with_size(2)])
            .await
            .unwrap();

        let root_dir = tree
            .put_directory(vec![
                DirEntry::new("root.txt", f1).with_size(1),
                DirEntry::new("sub", sub_dir.hash),
            ])
            .await
            .unwrap();

        let entries = tree.walk(&root_dir, "").await.unwrap();
        let paths: Vec<_> = entries.iter().map(|e| e.path.as_str()).collect();

        assert!(paths.contains(&""));
        assert!(paths.contains(&"root.txt"));
        assert!(paths.contains(&"sub"));
        assert!(paths.contains(&"sub/nested.txt"));
    }

    #[tokio::test]
    async fn test_get_tree_node() {
        let (_store, tree) = make_tree();

        let blob_hash = tree.put_blob(b"data").await.unwrap();
        let dir_cid = tree
            .put_directory(vec![DirEntry::new("file.txt", blob_hash)])
            .await
            .unwrap();

        // Blob should return None
        let blob_node = tree.get_tree_node(&blob_hash).await.unwrap();
        assert!(blob_node.is_none());

        // Directory should return TreeNode
        let dir_node = tree.get_tree_node(&dir_cid.hash).await.unwrap();
        assert!(dir_node.is_some());
        assert_eq!(dir_node.unwrap().links.len(), 1);
    }

    #[tokio::test]
    async fn test_encrypted_directory_keeps_real_underscore_file_entries() {
        let (_store, tree) = make_encrypted_tree();

        let headers = b"cache-control: no-store\n";
        let (headers_cid, _) = tree.put_file(headers).await.unwrap();
        let (index_cid, _) = tree.put_file(b"<html></html>").await.unwrap();
        let dir_cid = tree
            .put_directory(vec![
                DirEntry::from_cid("_headers", &headers_cid)
                    .with_size(headers.len() as u64)
                    .with_link_type(LinkType::Blob),
                DirEntry::from_cid("index.html", &index_cid)
                    .with_size(13)
                    .with_link_type(LinkType::Blob),
            ])
            .await
            .unwrap();

        let entries = tree.list_directory(&dir_cid).await.unwrap();
        let mut names: Vec<_> = entries.iter().map(|entry| entry.name.as_str()).collect();
        names.sort();
        assert_eq!(names, vec!["_headers", "index.html"]);

        let resolved = tree
            .resolve_path(&dir_cid, "_headers")
            .await
            .unwrap()
            .expect("resolve _headers");
        assert_eq!(resolved, headers_cid);

        let data = tree
            .get(&resolved, None)
            .await
            .unwrap()
            .expect("read _headers");
        assert_eq!(data, headers);
    }

    #[tokio::test]
    async fn test_list_directory_preserves_legacy_internal_fanout_nodes() {
        let (_store, tree) = make_tree();

        let (file_cid, _) = tree.put_file(b"fanout").await.unwrap();
        let sub_hash = tree
            .put_tree_node(vec![Link {
                hash: file_cid.hash,
                name: Some("apple.txt".to_string()),
                size: 6,
                key: None,
                link_type: LinkType::Blob,
                meta: None,
            }])
            .await
            .unwrap();
        let root_hash = tree
            .put_tree_node(vec![Link {
                hash: sub_hash,
                name: Some("_a".to_string()),
                size: 6,
                key: None,
                link_type: LinkType::Dir,
                meta: None,
            }])
            .await
            .unwrap();

        let root_cid = Cid::public(root_hash);
        let entries = tree.list_directory(&root_cid).await.unwrap();
        assert_eq!(entries.len(), 1);
        assert_eq!(entries[0].name, "apple.txt");

        let resolved = tree
            .resolve_path(&root_cid, "apple.txt")
            .await
            .unwrap()
            .expect("resolve apple.txt");
        assert_eq!(resolved, file_cid);
    }
}

// ============ STREAMING TESTS ============

mod streaming {
    use super::*;

    #[tokio::test]
    async fn test_read_file_stream_small() {
        let (_store, tree) = make_tree();

        let data = b"small data";
        let (cid, _) = tree.put_file(data).await.unwrap();

        let mut stream = tree.read_file_stream(cid.hash);
        let mut collected = Vec::new();

        while let Some(chunk_result) = stream.next().await {
            collected.extend(chunk_result.unwrap());
        }

        assert_eq!(collected, data.to_vec());
    }

    #[tokio::test]
    async fn test_read_file_stream_chunked() {
        let (_store, tree) = make_tree_with_chunk_size(5);

        let data = b"hello world!";
        let (cid, _) = tree.put_file(data).await.unwrap();

        let mut stream = tree.read_file_stream(cid.hash);
        let mut chunks = Vec::new();

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

        assert!(chunks.len() > 1);

        let combined: Vec<u8> = chunks.into_iter().flatten().collect();
        assert_eq!(combined, data.to_vec());
    }

    #[tokio::test]
    async fn test_read_file_stream_missing() {
        let (_store, tree) = make_tree();

        let missing_hash = [0u8; 32];
        let mut stream = tree.read_file_stream(missing_hash);

        let result = stream.next().await;
        assert!(result.is_none());
    }

    #[tokio::test]
    async fn test_read_file_stream_large() {
        let (_store, tree) = make_tree_with_chunk_size(100);

        let data: Vec<u8> = (0..1000).map(|i| (i % 256) as u8).collect();
        let (cid, _) = tree.put_file(&data).await.unwrap();

        let mut stream = tree.read_file_stream(cid.hash);
        let mut total_size = 0;

        while let Some(chunk_result) = stream.next().await {
            let chunk = chunk_result.unwrap();
            total_size += chunk.len();
        }

        assert_eq!(total_size, 1000);
    }

    #[tokio::test]
    async fn test_walk_stream() {
        let (_store, tree) = make_tree();

        let f1 = tree.put_blob(b"1").await.unwrap();
        let f2 = tree.put_blob(b"23").await.unwrap();

        let sub_dir = tree
            .put_directory(vec![DirEntry::new("nested.txt", f2).with_size(2)])
            .await
            .unwrap();

        let root_dir = tree
            .put_directory(vec![
                DirEntry::new("root.txt", f1).with_size(1),
                DirEntry::new("sub", sub_dir.hash),
            ])
            .await
            .unwrap();

        let mut stream = tree.walk_stream(root_dir, "".to_string());
        let mut paths = Vec::new();

        while let Some(entry_result) = stream.next().await {
            let entry = entry_result.unwrap();
            paths.push(entry.path);
        }

        assert!(paths.contains(&"".to_string()));
        assert!(paths.contains(&"root.txt".to_string()));
        assert!(paths.contains(&"sub".to_string()));
        assert!(paths.contains(&"sub/nested.txt".to_string()));
    }

    #[tokio::test]
    async fn test_walk_stream_single_file() {
        let (_store, tree) = make_tree();

        let file_hash = tree.put_blob(b"content").await.unwrap();

        let mut stream = tree.walk_stream(Cid::public(file_hash), "file.txt".to_string());
        let mut entries = Vec::new();

        while let Some(entry_result) = stream.next().await {
            entries.push(entry_result.unwrap());
        }

        assert_eq!(entries.len(), 1);
        assert_eq!(entries[0].path, "file.txt");
        assert!(!entries[0].link_type.is_tree());
        assert_eq!(entries[0].size, 7);
    }
}

// ============ EDIT TESTS ============

mod edit {
    use super::*;

    #[tokio::test]
    async fn test_add_entry_to_directory() {
        let (_store, tree) = make_tree();

        let root_cid = tree.put_directory(vec![]).await.unwrap();
        let (file_cid, file_size) = tree.put_file(b"hello").await.unwrap();

        let new_root = tree
            .set_entry(
                &root_cid,
                &[],
                "test.txt",
                &file_cid,
                file_size,
                LinkType::File,
            )
            .await
            .unwrap();

        let entries = tree.list_directory(&new_root).await.unwrap();
        assert_eq!(entries.len(), 1);
        assert_eq!(entries[0].name, "test.txt");
    }

    #[tokio::test]
    async fn test_update_existing_entry() {
        let (_store, tree) = make_tree();

        let (file1_cid, _) = tree.put_file(b"v1").await.unwrap();
        let root_cid = tree
            .put_directory(vec![DirEntry::new("file.txt", file1_cid.hash).with_size(2)])
            .await
            .unwrap();

        let (file2_cid, file2_size) = tree.put_file(b"v2 updated").await.unwrap();
        let new_root = tree
            .set_entry(
                &root_cid,
                &[],
                "file.txt",
                &file2_cid,
                file2_size,
                LinkType::File,
            )
            .await
            .unwrap();

        let entries = tree.list_directory(&new_root).await.unwrap();
        assert_eq!(entries.len(), 1);
        assert_eq!(to_hex(&entries[0].hash), to_hex(&file2_cid.hash));
    }

    #[tokio::test]
    async fn test_remove_entry() {
        let (_store, tree) = make_tree();

        let (file1_cid, _) = tree.put_file(b"a").await.unwrap();
        let (file2_cid, _) = tree.put_file(b"b").await.unwrap();
        let root_cid = tree
            .put_directory(vec![
                DirEntry::new("a.txt", file1_cid.hash).with_size(1),
                DirEntry::new("b.txt", file2_cid.hash).with_size(1),
            ])
            .await
            .unwrap();

        let new_root = tree.remove_entry(&root_cid, &[], "a.txt").await.unwrap();

        let entries = tree.list_directory(&new_root).await.unwrap();
        assert_eq!(entries.len(), 1);
        assert_eq!(entries[0].name, "b.txt");
    }

    #[tokio::test]
    async fn test_rename_entry() {
        let (_store, tree) = make_tree();

        let (file_cid, _) = tree.put_file(b"content").await.unwrap();
        let root_cid = tree
            .put_directory(vec![DirEntry::new("old.txt", file_cid.hash).with_size(7)])
            .await
            .unwrap();

        let new_root = tree
            .rename_entry(&root_cid, &[], "old.txt", "new.txt")
            .await
            .unwrap();

        let entries = tree.list_directory(&new_root).await.unwrap();
        assert_eq!(entries.len(), 1);
        assert_eq!(entries[0].name, "new.txt");
        assert_eq!(to_hex(&entries[0].hash), to_hex(&file_cid.hash));
    }

    #[tokio::test]
    async fn test_rename_same_name_no_change() {
        let (_store, tree) = make_tree();

        let (file_cid, _) = tree.put_file(b"content").await.unwrap();
        let root_cid = tree
            .put_directory(vec![DirEntry::new("file.txt", file_cid.hash)])
            .await
            .unwrap();

        let new_root = tree
            .rename_entry(&root_cid, &[], "file.txt", "file.txt")
            .await
            .unwrap();

        assert_eq!(to_hex(&new_root.hash), to_hex(&root_cid.hash));
    }

    #[tokio::test]
    async fn test_move_entry_between_directories() {
        let (_store, tree) = make_tree();

        let (file_cid, _) = tree.put_file(b"content").await.unwrap();
        let dir1_cid = tree
            .put_directory(vec![DirEntry::new("file.txt", file_cid.hash).with_size(7)])
            .await
            .unwrap();
        let dir2_cid = tree.put_directory(vec![]).await.unwrap();
        let root_cid = tree
            .put_directory(vec![
                DirEntry::new("dir1", dir1_cid.hash).with_size(7),
                DirEntry::new("dir2", dir2_cid.hash).with_size(0),
            ])
            .await
            .unwrap();

        let new_root = tree
            .move_entry(&root_cid, &["dir1"], "file.txt", &["dir2"])
            .await
            .unwrap();

        let entries = tree.list_directory(&new_root).await.unwrap();
        assert_eq!(entries.len(), 2);

        let dir1_entries = tree
            .list_directory(&tree.resolve_path(&new_root, "dir1").await.unwrap().unwrap())
            .await
            .unwrap();
        assert_eq!(dir1_entries.len(), 0);

        let dir2_entries = tree
            .list_directory(&tree.resolve_path(&new_root, "dir2").await.unwrap().unwrap())
            .await
            .unwrap();
        assert_eq!(dir2_entries.len(), 1);
        assert_eq!(dir2_entries[0].name, "file.txt");
    }

    #[tokio::test]
    async fn test_nested_path_edits() {
        let (_store, tree) = make_tree();

        let c_cid = tree.put_directory(vec![]).await.unwrap();
        let b_cid = tree
            .put_directory(vec![DirEntry::new("c", c_cid.hash).with_size(0)])
            .await
            .unwrap();
        let a_cid = tree
            .put_directory(vec![DirEntry::new("b", b_cid.hash).with_size(0)])
            .await
            .unwrap();
        let root_cid = tree
            .put_directory(vec![DirEntry::new("a", a_cid.hash).with_size(0)])
            .await
            .unwrap();

        let (file_cid, file_size) = tree.put_file(b"deep").await.unwrap();
        let new_root = tree
            .set_entry(
                &root_cid,
                &["a", "b", "c"],
                "file.txt",
                &file_cid,
                file_size,
                LinkType::File,
            )
            .await
            .unwrap();

        // Verify nested file
        let entries = tree
            .list_directory(
                &tree
                    .resolve_path(&new_root, "a/b/c")
                    .await
                    .unwrap()
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(entries.len(), 1);
        assert_eq!(entries[0].name, "file.txt");

        // Verify parent structure intact
        let a_entries = tree
            .list_directory(&tree.resolve_path(&new_root, "a").await.unwrap().unwrap())
            .await
            .unwrap();
        assert_eq!(a_entries.len(), 1);
        assert_eq!(a_entries[0].name, "b");
    }

    #[tokio::test]
    async fn test_set_entry_path_not_found() {
        let (_store, tree) = make_tree();

        let root_cid = tree.put_directory(vec![]).await.unwrap();
        let (file_cid, file_size) = tree.put_file(b"data").await.unwrap();

        let result = tree
            .set_entry(
                &root_cid,
                &["nonexistent"],
                "file.txt",
                &file_cid,
                file_size,
                LinkType::File,
            )
            .await;

        assert!(matches!(result, Err(HashTreeError::PathNotFound(_))));
    }

    #[tokio::test]
    async fn test_rename_entry_not_found() {
        let (_store, tree) = make_tree();

        let root_cid = tree.put_directory(vec![]).await.unwrap();

        let result = tree
            .rename_entry(&root_cid, &[], "nonexistent.txt", "new.txt")
            .await;

        assert!(matches!(result, Err(HashTreeError::EntryNotFound(_))));
    }

    #[tokio::test]
    async fn test_immutable_edit_operations() {
        let (_store, tree) = make_tree();

        let (file_cid, _) = tree.put_file(b"original").await.unwrap();
        let original_root = tree
            .put_directory(vec![DirEntry::new("file.txt", file_cid.hash).with_size(8)])
            .await
            .unwrap();

        let (file2_cid, file2_size) = tree.put_file(b"modified").await.unwrap();
        let new_root = tree
            .set_entry(
                &original_root,
                &[],
                "file.txt",
                &file2_cid,
                file2_size,
                LinkType::File,
            )
            .await
            .unwrap();

        // Original unchanged
        let original_entries = tree.list_directory(&original_root).await.unwrap();
        assert_eq!(to_hex(&original_entries[0].hash), to_hex(&file_cid.hash));

        // New root has changes
        let new_entries = tree.list_directory(&new_root).await.unwrap();
        assert_eq!(to_hex(&new_entries[0].hash), to_hex(&file2_cid.hash));
    }
}

// ============ VERIFY TESTS ============

mod verify {
    use super::*;
    use hashtree_core::hashtree_verify_tree;

    #[tokio::test]
    async fn test_verify_valid_tree() {
        let (store, tree) = make_tree_with_chunk_size(100);

        let data = vec![0u8; 350];
        let (cid, _) = tree.put_file(&data).await.unwrap();

        let verify_result = hashtree_verify_tree(store, &cid.hash).await.unwrap();
        assert!(verify_result.valid);
        assert!(verify_result.missing.is_empty());
    }

    #[tokio::test]
    async fn test_verify_missing_chunk() {
        let (store, tree) = make_tree_with_chunk_size(100);

        let data = vec![0u8; 350];
        let (cid, _) = tree.put_file(&data).await.unwrap();

        // Delete one chunk
        let keys = store.keys();
        if let Some(chunk_to_delete) = keys.iter().find(|k| **k != cid.hash) {
            store.delete(chunk_to_delete).await.unwrap();
        }

        let verify_result = hashtree_verify_tree(store, &cid.hash).await.unwrap();
        assert!(!verify_result.valid);
        assert!(!verify_result.missing.is_empty());
    }
}

// ============ EDGE CASES ============

mod edge_cases {
    use super::*;

    #[tokio::test]
    async fn test_empty_file() {
        let (_store, tree) = make_tree();

        let (cid, size) = tree.put_file(&[]).await.unwrap();
        assert_eq!(size, 0);

        let data = tree.read_file(&cid.hash).await.unwrap().unwrap();
        assert!(data.is_empty());
    }

    #[tokio::test]
    async fn test_single_byte_file() {
        let (_store, tree) = make_tree();

        let (cid, size) = tree.put_file(&[42]).await.unwrap();
        assert_eq!(size, 1);

        let data = tree.read_file(&cid.hash).await.unwrap().unwrap();
        assert_eq!(data, vec![42]);
    }

    #[tokio::test]
    async fn test_exact_chunk_size() {
        let (_store, tree) = make_tree_with_chunk_size(100);

        let data = vec![0u8; 100];
        let (cid, _) = tree.put_file(&data).await.unwrap();

        let read_data = tree.read_file(&cid.hash).await.unwrap().unwrap();
        assert_eq!(read_data.len(), 100);
    }

    #[tokio::test]
    async fn test_chunk_size_plus_one() {
        let (_store, tree) = make_tree_with_chunk_size(100);

        let data = vec![0u8; 101];
        let (cid, _) = tree.put_file(&data).await.unwrap();

        let read_data = tree.read_file(&cid.hash).await.unwrap().unwrap();
        assert_eq!(read_data.len(), 101);
    }

    #[tokio::test]
    async fn test_binary_data() {
        let (_store, tree) = make_tree();

        let data: Vec<u8> = (0..=255).cycle().take(512).collect();
        let (cid, _) = tree.put_file(&data).await.unwrap();

        let read_data = tree.read_file(&cid.hash).await.unwrap().unwrap();
        assert_eq!(read_data, data);
    }

    #[tokio::test]
    async fn test_special_characters_in_names() {
        let (_store, tree) = make_tree();

        let file_hash = tree.put_blob(b"data").await.unwrap();

        let dir_cid = tree
            .put_directory(vec![
                DirEntry::new("file with spaces.txt", file_hash),
                DirEntry::new("file-with-dashes.txt", file_hash),
                DirEntry::new("file_with_underscores.txt", file_hash),
                DirEntry::new("file.multiple.dots.txt", file_hash),
            ])
            .await
            .unwrap();

        let entries = tree.list_directory(&dir_cid).await.unwrap();
        assert_eq!(entries.len(), 4);
    }

    #[tokio::test]
    async fn test_unicode_names() {
        let (_store, tree) = make_tree();

        let file_hash = tree.put_blob(b"data").await.unwrap();

        let dir_cid = tree
            .put_directory(vec![
                DirEntry::new("日本語.txt", file_hash),
                DirEntry::new("émoji🎉.txt", file_hash),
                DirEntry::new("中文文件.txt", file_hash),
            ])
            .await
            .unwrap();

        let entries = tree.list_directory(&dir_cid).await.unwrap();
        assert_eq!(entries.len(), 3);

        let names: Vec<_> = entries.iter().map(|e| e.name.as_str()).collect();
        assert!(names.contains(&"日本語.txt"));
        assert!(names.contains(&"émoji🎉.txt"));
        assert!(names.contains(&"中文文件.txt"));
    }

    #[tokio::test]
    async fn test_deeply_nested_path() {
        let (_store, tree) = make_tree();

        // Create 10 levels deep
        let file_hash = tree.put_blob(b"deep content").await.unwrap();
        let mut current_cid = tree
            .put_directory(vec![DirEntry::new("file.txt", file_hash)])
            .await
            .unwrap();

        for i in (1..=10).rev() {
            current_cid = tree
                .put_directory(vec![DirEntry::new(format!("level{}", i), current_cid.hash)])
                .await
                .unwrap();
        }

        let path =
            "level1/level2/level3/level4/level5/level6/level7/level8/level9/level10/file.txt";
        let resolved = tree.resolve_path(&current_cid, path).await.unwrap();
        assert_eq!(resolved.map(|c| c.hash), Some(file_hash));
    }

    #[tokio::test]
    async fn test_concurrent_operations() {
        let (_store, tree) = make_tree();

        // Create multiple files concurrently
        let futures: Vec<_> = (0..10)
            .map(|i| {
                let t = &tree;
                async move {
                    let data = vec![i as u8; 100];
                    t.put_file(&data).await
                }
            })
            .collect();

        let results = futures::future::join_all(futures).await;

        for result in results {
            assert!(result.is_ok());
        }
    }
}

// ============ ENCRYPTION TESTS ============

mod encryption {
    use super::*;

    /// Count unique byte values in first 256 bytes (blossom compatibility check)
    fn count_unique_bytes(data: &[u8]) -> usize {
        let sample_size = data.len().min(256);
        let mut seen = [false; 256];
        let mut count = 0;
        for &b in &data[..sample_size] {
            if !seen[b as usize] {
                seen[b as usize] = true;
                count += 1;
            }
        }
        count
    }

    #[tokio::test]
    async fn test_put_file_produces_encrypted_blobs() {
        let (store, tree) = make_encrypted_tree();

        // Store a file with plaintext data
        let plaintext = b"This is plaintext content that should be encrypted";
        let (cid, _) = tree.put_file(plaintext).await.unwrap();

        // The stored blob should look random (encrypted), not like plaintext
        let stored = store.get(&cid.hash).await.unwrap().unwrap();
        let unique_bytes = count_unique_bytes(&stored);

        // Encrypted data should have high unique byte count (55%+ for small blobs)
        // Plaintext would have ~20-30 unique bytes
        let threshold = (stored.len().min(256) as f64 * 0.55) as usize;
        assert!(
            unique_bytes >= threshold,
            "put_file blob should be encrypted! Got {} unique bytes, expected >= {} (threshold 55%)",
            unique_bytes,
            threshold
        );
    }

    #[tokio::test]
    async fn test_put_file_chunked_produces_encrypted_chunks() {
        let (store, tree) = make_encrypted_tree_with_chunk_size(32);

        // Create data that will be chunked
        let plaintext: Vec<u8> = (0..100).map(|i| (i % 26 + 65) as u8).collect(); // "ABC..."
        let (cid, _) = tree.put_file(&plaintext).await.unwrap();

        // Check all stored blobs look encrypted
        for key in store.keys() {
            let blob = store.get(&key).await.unwrap().unwrap();
            if blob.len() >= 28 {
                // Min CHK size
                let unique_bytes = count_unique_bytes(&blob);
                let threshold = (blob.len().min(256) as f64 * 0.55) as usize;
                assert!(
                    unique_bytes >= threshold,
                    "Chunk should be encrypted! Got {} unique bytes in {} byte blob",
                    unique_bytes,
                    blob.len()
                );
            }
        }

        // Verify we can still read the file back
        // Need to use the encryption-aware read method
        assert!(cid.hash.len() == 32);
    }

    #[tokio::test]
    async fn test_put_file_returns_cid_with_key() {
        let (_, tree) = make_encrypted_tree();

        let plaintext = b"secret content";
        let (_, size) = tree.put_file(plaintext).await.unwrap();

        // put_file should return a result that can be used to decrypt
        // Currently it only returns hash, but encrypted mode should return key too
        // This test documents expected behavior for encrypted put_file
        assert_eq!(size, plaintext.len() as u64);
    }

    #[tokio::test]
    async fn test_public_mode_stores_plaintext() {
        let (store, tree) = make_tree(); // Uses .public()

        let plaintext = b"This content should NOT be encrypted in public mode";
        let (cid, _) = tree.put_file(plaintext).await.unwrap();

        // In public mode, the stored data should be the original plaintext
        let stored = store.get(&cid.hash).await.unwrap().unwrap();
        assert_eq!(stored, plaintext.to_vec());
    }
}

// ============ INTEROPERABILITY TESTS ============

mod interop {
    use super::*;

    #[tokio::test]
    async fn test_hash_consistency() {
        let (_store1, tree1) = make_tree();
        let (_store2, tree2) = make_tree();

        let data = b"test data for hash consistency";

        let (cid1, _) = tree1.put_file(data).await.unwrap();
        let (cid2, _) = tree2.put_file(data).await.unwrap();

        // Same data should produce same hash
        assert_eq!(to_hex(&cid1.hash), to_hex(&cid2.hash));
    }

    #[tokio::test]
    async fn test_directory_hash_consistency() {
        let (_store1, tree1) = make_tree();
        let (_store2, tree2) = make_tree();

        // Create same structure in both
        let (file1_1_cid, _) = tree1.put_file(b"content1").await.unwrap();
        let (file1_2_cid, _) = tree1.put_file(b"content2").await.unwrap();
        let dir1 = tree1
            .put_directory(vec![
                DirEntry::new("a.txt", file1_1_cid.hash).with_size(8),
                DirEntry::new("b.txt", file1_2_cid.hash).with_size(8),
            ])
            .await
            .unwrap();

        let (file2_1_cid, _) = tree2.put_file(b"content1").await.unwrap();
        let (file2_2_cid, _) = tree2.put_file(b"content2").await.unwrap();
        let dir2 = tree2
            .put_directory(vec![
                DirEntry::new("b.txt", file2_2_cid.hash).with_size(8), // Different order
                DirEntry::new("a.txt", file2_1_cid.hash).with_size(8),
            ])
            .await
            .unwrap();

        // Should produce same hash due to sorting
        assert_eq!(to_hex(&dir1.hash), to_hex(&dir2.hash));
    }
}