shard-cli 2.4.1

CLI for shard distributed VCS — content-addressed version control for large ML artifacts
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
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
use std::fs;
use std::io::{BufRead, BufReader};
use std::ops::Deref;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use std::sync::mpsc;
use std::time::Duration;

fn shard_bin() -> PathBuf {
    PathBuf::from(env!("CARGO_BIN_EXE_shard"))
}

struct TempRepo {
    _dir: tempfile::TempDir,
    path: PathBuf,
}

impl Deref for TempRepo {
    type Target = Path;
    fn deref(&self) -> &Self::Target {
        &self.path
    }
}

impl AsRef<Path> for TempRepo {
    fn as_ref(&self) -> &Path {
        &self.path
    }
}

fn repo_dir(_name: &str) -> TempRepo {
    let dir = tempfile::tempdir().expect("failed to create temp repo dir");
    let path = dir.path().to_path_buf();
    TempRepo { _dir: dir, path }
}

fn shard(args: &[&str], cwd: &Path) -> Command {
    let mut cmd = Command::new(shard_bin());
    cmd.args(args).current_dir(cwd);
    cmd
}

fn shard_ok(args: &[&str], cwd: &Path) -> std::process::Output {
    let out = shard(args, cwd).output().unwrap();
    assert!(
        out.status.success(),
        "shard {:?} failed:\nstdout: {}\nstderr: {}",
        args,
        String::from_utf8_lossy(&out.stdout),
        String::from_utf8_lossy(&out.stderr)
    );
    out
}

#[test]
fn test_init_creates_dot_shard() {
    let dir = repo_dir("init-test");
    let output = shard(&["init"], &dir).output().unwrap();
    assert!(
        output.status.success(),
        "init failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    assert!(dir.join(".shard").is_dir());
    assert!(dir.join(".shard/objects").is_dir());
    assert!(dir.join(".shard/keys").is_dir());
    assert!(dir.join(".shard/keys/secret.key").exists());
    assert!(dir.join(".shard/keys/public.key").exists());
}

#[test]
fn test_parallel_init_creates_one_repo() {
    let dir = repo_dir("parallel-init-test");
    let barrier = std::sync::Arc::new(std::sync::Barrier::new(3));
    let mut handles = Vec::new();

    for _ in 0..2 {
        let barrier = barrier.clone();
        let path = dir.path.clone();
        handles.push(std::thread::spawn(move || {
            let mut command = Command::new(shard_bin());
            command.args(["init"]).current_dir(path);
            barrier.wait();
            command.output().unwrap()
        }));
    }
    barrier.wait();

    let outputs: Vec<_> = handles
        .into_iter()
        .map(|handle| handle.join().unwrap())
        .collect();
    assert_eq!(
        outputs
            .iter()
            .filter(|output| output.status.success())
            .count(),
        1
    );
    assert!(dir.path.join(".shard").is_dir());
    assert!(dir.path.join(".shard.lock").is_file());
}

#[test]
fn test_init_twice_fails() {
    let dir = repo_dir("init-twice");
    shard(&["init"], &dir).output().unwrap();
    let output = shard(&["init"], &dir).output().unwrap();
    assert!(!output.status.success());
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("already initialized"),
        "wrong error: {stderr}"
    );
}

#[test]
fn test_add_commit_verify_roundtrip() {
    let dir = repo_dir("add-commit-verify");
    shard(&["init"], &dir).output().unwrap();

    fs::write(dir.join("hello.txt"), b"Hello, Shard!").unwrap();
    let output = shard(&["add", "hello.txt"], &dir).output().unwrap();
    assert!(
        output.status.success(),
        "add failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    let output = shard(&["commit", "-m", "first", "--author", "Test"], &dir)
        .output()
        .unwrap();
    assert!(
        output.status.success(),
        "commit failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let stdout = String::from_utf8(output.stdout).unwrap();
    let commit_id = stdout
        .split_whitespace()
        .nth(1)
        .expect("no commit id in output");

    let output = shard(&["verify", commit_id], &dir).output().unwrap();
    assert!(
        output.status.success(),
        "verify failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let stdout = String::from_utf8(output.stdout).unwrap();
    assert!(
        stdout.contains("Signature verified"),
        "no signature verification: {stdout}"
    );
    assert!(
        stdout.contains("Verification successful"),
        "verify not successful: {stdout}"
    );
}

#[test]
fn test_verify_fails_on_tampered_chunk() {
    let dir = repo_dir("tamper-test");
    shard(&["init"], &dir).output().unwrap();

    fs::write(dir.join("data.bin"), b"important data").unwrap();
    shard(&["add", "data.bin"], &dir).output().unwrap();
    let output = shard(&["commit", "-m", "tamper-me", "--author", "Test"], &dir)
        .output()
        .unwrap();
    let stdout = String::from_utf8(output.stdout).unwrap();
    let commit_id = stdout.split_whitespace().nth(1).unwrap().to_string();

    // Tamper with the chunk
    let objects_dir = dir.join(".shard/objects");
    for entry in walkdir::WalkDir::new(&objects_dir) {
        let entry = entry.unwrap();
        if entry.file_type().is_file() && entry.path().extension().is_none() {
            fs::write(entry.path(), b"TAMPERED").unwrap();
            break;
        }
    }

    let output = shard(&["verify", &commit_id], &dir).output().unwrap();
    assert!(
        !output.status.success(),
        "verify should have failed after tampering"
    );
}

#[test]
fn test_empty_commit_fails() {
    let dir = repo_dir("empty-commit");
    shard(&["init"], &dir).output().unwrap();
    let output = shard(&["commit", "-m", "empty", "--author", "Test"], &dir)
        .output()
        .unwrap();
    assert!(!output.status.success());
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("nothing to commit"),
        "wrong error: {stderr}"
    );
}

#[test]
fn test_verify_nonexistent_commit_fails() {
    let dir = repo_dir("bad-verify");
    shard(&["init"], &dir).output().unwrap();
    let output = shard(
        &[
            "verify",
            "0000000000000000000000000000000000000000000000000000000000000000",
        ],
        &dir,
    )
    .output()
    .unwrap();
    assert!(!output.status.success());
}

#[test]
fn test_multiple_adds_and_commits() {
    let dir = repo_dir("multi-commit");
    shard(&["init"], &dir).output().unwrap();

    fs::write(dir.join("a.txt"), b"file a").unwrap();
    shard(&["add", "a.txt"], &dir).output().unwrap();
    let out1 = shard(&["commit", "-m", "commit a", "--author", "T"], &dir)
        .output()
        .unwrap();
    let id1 = String::from_utf8(out1.stdout)
        .unwrap()
        .split_whitespace()
        .nth(1)
        .unwrap()
        .to_string();

    fs::write(dir.join("b.txt"), b"file b").unwrap();
    shard(&["add", "b.txt"], &dir).output().unwrap();
    let out2 = shard(&["commit", "-m", "commit b", "--author", "T"], &dir)
        .output()
        .unwrap();
    let id2 = String::from_utf8(out2.stdout)
        .unwrap()
        .split_whitespace()
        .nth(1)
        .unwrap()
        .to_string();

    assert_ne!(id1, id2, "commit ids should differ");

    shard(&["verify", &id1], &dir).output().unwrap();
    shard(&["verify", &id2], &dir).output().unwrap();
}

#[test]
fn test_large_file_chunking() {
    let dir = repo_dir("large-file");
    shard(&["init"], &dir).output().unwrap();

    let data = vec![0xABu8; 5 * 1024 * 1024]; // 5 MiB (crosses 4 MiB chunk boundary)
    fs::write(dir.join("large.bin"), &data).unwrap();
    shard(&["add", "large.bin"], &dir).output().unwrap();
    let output = shard(&["commit", "-m", "large", "--author", "T"], &dir)
        .output()
        .unwrap();
    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout).unwrap();
    let commit_id = stdout.split_whitespace().nth(1).unwrap();

    shard(&["verify", commit_id], &dir).output().unwrap();
}

#[test]
fn test_log_shows_commits() {
    let dir = repo_dir("log-test");
    shard(&["init"], &dir).output().unwrap();

    fs::write(dir.join("a.txt"), b"alpha").unwrap();
    assert!(shard(&["add", "a.txt"], &dir)
        .output()
        .unwrap()
        .status
        .success());
    assert!(shard(&["commit", "-m", "first", "--author", "A"], &dir)
        .output()
        .unwrap()
        .status
        .success());

    fs::write(dir.join("b.txt"), b"beta").unwrap();
    assert!(shard(&["add", "b.txt"], &dir)
        .output()
        .unwrap()
        .status
        .success());
    assert!(shard(&["commit", "-m", "second", "--author", "B"], &dir)
        .output()
        .unwrap()
        .status
        .success());

    let output = shard(&["log"], &dir).output().unwrap();
    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout).unwrap();
    assert!(stdout.contains("second"), "missing second commit: {stdout}");
    assert!(stdout.contains("first"), "missing first commit: {stdout}");
    assert!(stdout.contains("author: B"), "missing author B: {stdout}");
    assert!(stdout.contains("author: A"), "missing author A: {stdout}");
}

#[test]
fn test_log_json_output() {
    let dir = repo_dir("log-json");
    shard(&["init"], &dir).output().unwrap();

    fs::write(dir.join("x.txt"), b"data").unwrap();
    assert!(shard(&["add", "x.txt"], &dir)
        .output()
        .unwrap()
        .status
        .success());
    assert!(shard(&["commit", "-m", "json-test", "--author", "J"], &dir)
        .output()
        .unwrap()
        .status
        .success());

    let output = shard(&["log", "--json"], &dir).output().unwrap();
    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout).unwrap();
    // Should be parseable JSON array
    let entries: serde_json::Value = serde_json::from_str(&stdout).unwrap();
    let arr = entries.as_array().unwrap();
    assert!(!arr.is_empty(), "expected non-empty log");
    assert!(arr[0].get("commit_id").and_then(|v| v.as_str()).is_some());
    assert!(arr[0].get("message").and_then(|v| v.as_str()) == Some("json-test"));
}

#[test]
fn test_log_empty_repo_fails() {
    let dir = repo_dir("log-empty");
    shard(&["init"], &dir).output().unwrap();
    let output = shard(&["log"], &dir).output().unwrap();
    assert!(!output.status.success());
}

#[test]
fn test_checkout_restores_files() {
    let dir = repo_dir("checkout-test");
    shard(&["init"], &dir).output().unwrap();

    fs::write(dir.join("hello.txt"), b"Hello, checkout!").unwrap();
    shard(&["add", "hello.txt"], &dir).output().unwrap();
    let output = shard(&["commit", "-m", "checkout-me", "--author", "T"], &dir)
        .output()
        .unwrap();
    let stdout = String::from_utf8(output.stdout).unwrap();
    let commit_id = stdout.split_whitespace().nth(1).unwrap().to_string();

    // Remove the file
    fs::remove_file(dir.join("hello.txt")).unwrap();

    // Checkout restores it
    let output = shard(&["checkout", &commit_id], &dir).output().unwrap();
    assert!(
        output.status.success(),
        "checkout failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    assert!(dir.join("hello.txt").exists());
    assert_eq!(
        fs::read_to_string(dir.join("hello.txt")).unwrap(),
        "Hello, checkout!"
    );
}

#[test]
fn test_checkout_multiple_files() {
    let dir = repo_dir("checkout-multi");
    shard(&["init"], &dir).output().unwrap();

    fs::write(dir.join("a.txt"), b"AAA").unwrap();
    fs::write(dir.join("b.txt"), b"BBB").unwrap();
    shard(&["add", "a.txt"], &dir).output().unwrap();
    shard(&["add", "b.txt"], &dir).output().unwrap();
    let output = shard(&["commit", "-m", "multi", "--author", "T"], &dir)
        .output()
        .unwrap();
    let stdout = String::from_utf8(output.stdout).unwrap();
    let commit_id = stdout.split_whitespace().nth(1).unwrap().to_string();

    fs::remove_file(dir.join("a.txt")).unwrap();
    fs::remove_file(dir.join("b.txt")).unwrap();

    let output = shard(&["checkout", &commit_id], &dir).output().unwrap();
    assert!(output.status.success());
    assert_eq!(fs::read_to_string(dir.join("a.txt")).unwrap(), "AAA");
    assert_eq!(fs::read_to_string(dir.join("b.txt")).unwrap(), "BBB");
}

#[test]
fn test_checkout_wrong_commit_fails() {
    let dir = repo_dir("checkout-bad");
    shard(&["init"], &dir).output().unwrap();
    let output = shard(
        &[
            "checkout",
            "0000000000000000000000000000000000000000000000000000000000000000",
        ],
        &dir,
    )
    .output()
    .unwrap();
    assert!(!output.status.success());
}

#[test]
fn test_status_after_init() {
    let dir = repo_dir("status-init");
    shard(&["init"], &dir).output().unwrap();
    let output = shard(&["status"], &dir).output().unwrap();
    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout).unwrap();
    assert!(stdout.contains("No commits"));
}

#[test]
fn test_status_after_commit() {
    let dir = repo_dir("status-commit");
    shard(&["init"], &dir).output().unwrap();
    fs::write(dir.join("f.txt"), b"data").unwrap();
    shard(&["add", "f.txt"], &dir).output().unwrap();
    shard(&["commit", "-m", "first", "--author", "T"], &dir)
        .output()
        .unwrap();
    let output = shard(&["status"], &dir).output().unwrap();
    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout).unwrap();
    assert!(stdout.contains("On branch:"));
    assert!(stdout.contains("Nothing staged"));
}

#[test]
fn test_status_shows_untracked() {
    let dir = repo_dir("status-untracked");
    shard(&["init"], &dir).output().unwrap();
    fs::write(dir.join("tracked.txt"), b"tracked").unwrap();
    shard(&["add", "tracked.txt"], &dir).output().unwrap();
    shard(&["commit", "-m", "first", "--author", "T"], &dir)
        .output()
        .unwrap();
    fs::write(dir.join("untracked.txt"), b"untracked").unwrap();
    let output = shard(&["status"], &dir).output().unwrap();
    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout).unwrap();
    assert!(stdout.contains("untracked.txt"));
}

#[test]
fn test_config_set_and_get() {
    let dir = repo_dir("config-test");
    shard(&["init"], &dir).output().unwrap();

    let out = shard(&["config", "set", "user.name", "Alice"], &dir)
        .output()
        .unwrap();
    assert!(out.status.success());

    let out = shard(&["config", "get", "user.name"], &dir)
        .output()
        .unwrap();
    assert!(out.status.success());
    assert!(String::from_utf8(out.stdout).unwrap().contains("Alice"));
}

#[test]
fn test_config_get_all() {
    let dir = repo_dir("config-all");
    shard(&["init"], &dir).output().unwrap();

    shard(&["config", "set", "a", "1"], &dir).output().unwrap();
    shard(&["config", "set", "b", "2"], &dir).output().unwrap();

    let out = shard(&["config", "get"], &dir).output().unwrap();
    assert!(out.status.success());
    let stdout = String::from_utf8(out.stdout).unwrap();
    assert!(stdout.contains("a = 1"));
    assert!(stdout.contains("b = 2"));
}

#[test]
fn test_config_get_missing_fails() {
    let dir = repo_dir("config-missing");
    shard(&["init"], &dir).output().unwrap();

    let out = shard(&["config", "get", "nonexistent"], &dir)
        .output()
        .unwrap();
    assert!(!out.status.success());
}

#[test]
fn test_tag_add_and_list() {
    let dir = repo_dir("tag-test");
    shard(&["init"], &dir).output().unwrap();
    fs::write(dir.join("f.txt"), b"data").unwrap();
    shard(&["add", "f.txt"], &dir).output().unwrap();
    let out = shard(&["commit", "-m", "first", "--author", "T"], &dir)
        .output()
        .unwrap();
    let stdout = String::from_utf8(out.stdout).unwrap();
    let cid = stdout.split_whitespace().nth(1).unwrap().to_string();

    let out = shard(&["tag", "add", "v1", &cid], &dir).output().unwrap();
    assert!(out.status.success());
    assert!(String::from_utf8(out.stdout).unwrap().contains("Tagged"));

    let out = shard(&["tag", "list"], &dir).output().unwrap();
    assert!(out.status.success());
    let stdout = String::from_utf8(out.stdout).unwrap();
    assert!(stdout.contains("v1"));
    assert!(stdout.contains(&cid));
}

#[test]
fn test_tag_bad_commit_fails() {
    let dir = repo_dir("tag-bad");
    shard(&["init"], &dir).output().unwrap();
    let out = shard(
        &[
            "tag",
            "add",
            "bad",
            "0000000000000000000000000000000000000000000000000000000000000000",
        ],
        &dir,
    )
    .output()
    .unwrap();
    assert!(!out.status.success());
}

#[test]
fn test_prune_removes_unreachable_objects() {
    let dir = repo_dir("prune-test");
    shard(&["init"], &dir).output().unwrap();

    // Create and commit a file
    fs::write(dir.join("keep.txt"), b"keep me").unwrap();
    shard(&["add", "keep.txt"], &dir).output().unwrap();
    let out = shard(&["commit", "-m", "first", "--author", "T"], &dir)
        .output()
        .unwrap();
    let stdout = String::from_utf8(out.stdout).unwrap();
    let commit_id = stdout.split_whitespace().nth(1).unwrap().to_string();

    // Create an orphan object (unreachable)
    let orphan_hash = "aa00000000000000000000000000000000000000000000000000000000000000";
    let orphan_dir = dir.join(".shard/objects/aa");
    fs::create_dir_all(&orphan_dir).unwrap();
    fs::write(orphan_dir.join(orphan_hash), b"orphan").unwrap();

    // Prune
    let out = shard(&["prune"], &dir).output().unwrap();
    assert!(
        out.status.success(),
        "prune failed: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    let stdout = String::from_utf8(out.stdout).unwrap();
    assert!(stdout.contains("Pruned 1"), "expected 1 pruned: {stdout}");

    // Verify orphan is gone
    assert!(
        !orphan_dir.join(orphan_hash).exists(),
        "orphan should be removed"
    );

    // Verify reachable commit still exists
    let prefix = &commit_id[..2];
    assert!(
        dir.join(".shard/objects")
            .join(prefix)
            .join(&commit_id)
            .exists(),
        "commit should still exist"
    );
}

#[test]
fn test_prune_keeps_staged_objects() {
    let dir = repo_dir("prune-staged");
    shard(&["init"], &dir).output().unwrap();

    // Stage a file (chunks stored but not committed)
    fs::write(dir.join("staged.txt"), b"staged data").unwrap();
    shard(&["add", "staged.txt"], &dir).output().unwrap();

    // Create an orphan object
    let orphan_hash = "bb00000000000000000000000000000000000000000000000000000000000000";
    let orphan_dir = dir.join(".shard/objects/bb");
    fs::create_dir_all(&orphan_dir).unwrap();
    fs::write(orphan_dir.join(orphan_hash), b"orphan").unwrap();

    // Prune
    let out = shard(&["prune"], &dir).output().unwrap();
    assert!(out.status.success());
    let stdout = String::from_utf8(out.stdout).unwrap();
    assert!(stdout.contains("Pruned 1"), "expected 1 pruned: {stdout}");
    assert!(
        !orphan_dir.join(orphan_hash).exists(),
        "orphan should be removed"
    );
}

#[test]
fn test_verify_json_output() {
    let dir = repo_dir("verify-json");
    shard(&["init"], &dir).output().unwrap();

    fs::write(dir.join("f.txt"), b"data").unwrap();
    shard(&["add", "f.txt"], &dir).output().unwrap();
    let out = shard(&["commit", "-m", "json-verify", "--author", "T"], &dir)
        .output()
        .unwrap();
    let stdout = String::from_utf8(out.stdout).unwrap();
    let commit_id = stdout.split_whitespace().nth(1).unwrap();

    let out = shard(&["verify", "--json", commit_id], &dir)
        .output()
        .unwrap();
    assert!(out.status.success());
    let stdout = String::from_utf8(out.stdout).unwrap();
    let v: serde_json::Value = serde_json::from_str(&stdout).unwrap();
    assert_eq!(v["verified"], true);
    assert_eq!(v["signature_verified"], true);
    assert!(v["files_checked"].as_u64().unwrap_or(0) >= 1);
}

#[test]
fn test_status_json_output() {
    let dir = repo_dir("status-json");
    shard(&["init"], &dir).output().unwrap();

    fs::write(dir.join("a.txt"), b"alpha").unwrap();
    shard(&["add", "a.txt"], &dir).output().unwrap();
    shard(&["commit", "-m", "first", "--author", "T"], &dir)
        .output()
        .unwrap();

    let out = shard(&["status", "--json"], &dir).output().unwrap();
    assert!(out.status.success());
    let stdout = String::from_utf8(out.stdout).unwrap();
    let v: serde_json::Value = serde_json::from_str(&stdout).unwrap();
    assert!(v["commit"].is_string());
    assert!(v["staged"].is_array());
    assert!(v["deleted"].is_array());
    assert!(v["untracked"].is_array());
}

#[test]
fn test_checkout_json_output() {
    let dir = repo_dir("checkout-json");
    shard(&["init"], &dir).output().unwrap();

    fs::write(dir.join("f.txt"), b"checkout json").unwrap();
    shard(&["add", "f.txt"], &dir).output().unwrap();
    let out = shard(&["commit", "-m", "json-co", "--author", "T"], &dir)
        .output()
        .unwrap();
    let stdout = String::from_utf8(out.stdout).unwrap();
    let commit_id = stdout.split_whitespace().nth(1).unwrap().to_string();

    fs::remove_file(dir.join("f.txt")).unwrap();

    let out = shard(&["checkout", "--json", &commit_id], &dir)
        .output()
        .unwrap();
    assert!(out.status.success());
    let stdout = String::from_utf8(out.stdout).unwrap();
    let v: serde_json::Value = serde_json::from_str(&stdout).unwrap();
    assert_eq!(v["commit_id"], commit_id);
    assert!(v["files"]
        .as_array()
        .unwrap()
        .contains(&serde_json::json!("f.txt")));
}

#[test]
fn test_init_private_sets_config() {
    let dir = repo_dir("init-private");
    let out = shard(&["init", "--private"], &dir).output().unwrap();
    assert!(out.status.success());

    let out = shard(&["config", "get", "private"], &dir).output().unwrap();
    assert!(out.status.success());
    let stdout = String::from_utf8(out.stdout).unwrap();
    assert!(stdout.contains("private = true"));
}

#[test]
fn test_three_node_pull() {
    let dir_a = repo_dir("3node-a");
    let dir_b = repo_dir("3node-b");
    let dir_c = repo_dir("3node-c");

    // Setup node A: init, add, commit
    shard_ok(&["init"], &dir_a);
    fs::write(dir_a.join("data.txt"), b"shared data").unwrap();
    shard_ok(&["add", "data.txt"], &dir_a);
    let out = shard_ok(&["commit", "-m", "shared", "--author", "T"], &dir_a);
    let stdout = String::from_utf8(out.stdout).unwrap();
    let commit_id = stdout.split_whitespace().nth(1).unwrap().to_string();

    // Start share on node A bound to loopback
    let mut child = Command::new(shard_bin())
        .args(["share", "--listen", "/ip4/127.0.0.1/tcp/0"])
        .current_dir(&dir_a)
        .stdout(Stdio::piped())
        .stderr(Stdio::null())
        .spawn()
        .expect("failed to start share");

    let reader = BufReader::new(child.stdout.take().unwrap());
    let (tx, rx) = mpsc::channel();
    std::thread::spawn(move || {
        let mut peer_id = String::new();
        let mut listen_addr = String::new();
        let mut sent = false;
        for line in reader.lines().map_while(Result::ok) {
            if !sent {
                if let Some(id) = line.strip_prefix("Local peer id: ") {
                    peer_id = id.trim().to_string();
                }
                if let Some(addr) = line.strip_prefix("Listening on ") {
                    let candidate = addr.trim().trim_matches('"').to_string();
                    if candidate.contains("/ip4/127.0.0.1/") {
                        listen_addr = candidate;
                    }
                }
                if !peer_id.is_empty() && !listen_addr.is_empty() {
                    let _ = tx.send((peer_id.clone(), listen_addr.clone()));
                    sent = true;
                }
            }
        }
    });

    let (peer_id, listen_addr) = rx
        .recv_timeout(Duration::from_secs(30))
        .expect("timed out waiting for share output");
    let multiaddr = format!("{}/p2p/{}", listen_addr, peer_id);

    // Node B pulls from A
    shard_ok(&["pull", &multiaddr, &commit_id], &dir_b);
    assert!(dir_b.join("data.txt").exists());
    assert_eq!(
        fs::read_to_string(dir_b.join("data.txt")).unwrap(),
        "shared data"
    );

    // Node C pulls from A
    shard_ok(&["pull", &multiaddr, &commit_id], &dir_c);
    assert!(dir_c.join("data.txt").exists());
    assert_eq!(
        fs::read_to_string(dir_c.join("data.txt")).unwrap(),
        "shared data"
    );

    // Cleanup
    let _ = child.kill();
    let _ = child.wait();
}

#[test]
fn test_sync_auto_pull() {
    let dir_a = repo_dir("sync-a");
    let dir_b = repo_dir("sync-b");

    // Setup node A: init, add, commit
    shard_ok(&["init"], &dir_a);
    fs::write(dir_a.join("shared.txt"), b"sync test data").unwrap();
    shard_ok(&["add", "shared.txt"], &dir_a);
    let out = shard_ok(&["commit", "-m", "sync-test", "--author", "T"], &dir_a);
    let stdout = String::from_utf8(out.stdout).unwrap();
    let commit_id = stdout.split_whitespace().nth(1).unwrap().to_string();

    // Start sync on A in background
    let mut child_a = Command::new(shard_bin())
        .arg("sync")
        .current_dir(&dir_a)
        .stdout(Stdio::piped())
        .stderr(Stdio::null())
        .spawn()
        .expect("failed to start sync A");

    let reader_a = BufReader::new(child_a.stdout.take().unwrap());
    let (tx_a, rx_a) = mpsc::channel();
    std::thread::spawn(move || {
        let mut peer_id = String::new();
        let mut listen_addr = String::new();
        let mut sent = false;
        // Keep draining stdout until EOF so child_a never gets SIGPIPE on subsequent NewListenAddr prints!
        for line in reader_a.lines().map_while(Result::ok) {
            if !sent {
                if let Some(id) = line.strip_prefix("Local peer id: ") {
                    peer_id = id.trim().to_string();
                }
                if let Some(addr) = line.strip_prefix("Listening on ") {
                    let candidate = addr.trim().trim_matches('"').to_string();
                    if candidate.contains("/ip4/127.0.0.1/") {
                        listen_addr = candidate;
                    }
                }
                if !peer_id.is_empty() && !listen_addr.is_empty() {
                    let _ = tx_a.send((peer_id.clone(), listen_addr.clone()));
                    sent = true;
                }
            }
        }
    });

    let (peer_id_a, listen_addr_a) = rx_a
        .recv_timeout(Duration::from_secs(30))
        .expect("timed out waiting for sync A output");
    let multiaddr_a = format!("{}/p2p/{}", listen_addr_a, peer_id_a);

    // Configure B with A's repo_id and peer address
    shard_ok(&["init"], &dir_b);
    let a_config: std::collections::BTreeMap<String, String> =
        serde_json::from_slice(&fs::read(dir_a.join(".shard/config.json")).unwrap()).unwrap();
    let repo_id = a_config.get("repo_id").expect("A has repo_id");
    shard_ok(&["config", "set", "repo_id", repo_id], &dir_b);
    shard_ok(&["peer", "add", &multiaddr_a], &dir_b);

    // Start sync on B in background
    let mut child_b = Command::new(shard_bin())
        .arg("sync")
        .current_dir(&dir_b)
        .stdout(Stdio::piped())
        .stderr(Stdio::null())
        .spawn()
        .expect("failed to start sync B");

    let reader_b = BufReader::new(child_b.stdout.take().unwrap());
    let (tx_b, rx_b) = mpsc::channel();
    std::thread::spawn(move || {
        let mut sent = false;
        for line in reader_b.lines().map_while(Result::ok) {
            if !sent && line.starts_with("Auto-pulled commit") {
                let _ = tx_b.send(line);
                sent = true;
            }
        }
    });

    // Wait for B to auto-pull
    let msg = rx_b
        .recv_timeout(Duration::from_secs(30))
        .expect("B did not auto-pull commit within timeout");
    assert!(msg.contains(&commit_id), "auto-pulled wrong commit: {msg}");

    // Verify file was pulled
    assert!(dir_b.join("shared.txt").exists());
    assert_eq!(
        fs::read_to_string(dir_b.join("shared.txt")).unwrap(),
        "sync test data"
    );

    // Cleanup
    let _ = child_a.kill();
    let _ = child_a.wait();
    let _ = child_b.kill();
    let _ = child_b.wait();
}

#[test]
fn test_init_sqlite_creates_dot_shard() {
    let dir = repo_dir("sqlite-init");
    let output = shard(&["init", "--db", "sqlite"], &dir).output().unwrap();
    assert!(
        output.status.success(),
        "sqlite init failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    assert!(dir.join(".shard").is_dir());
    // Config must store the sqlite backend
    let config: std::collections::BTreeMap<String, String> =
        serde_json::from_slice(&fs::read(dir.join(".shard/config.json")).unwrap()).unwrap();
    assert_eq!(
        config.get("storage_backend").map(|s| s.as_str()),
        Some("sqlite")
    );
}

#[test]
fn test_sqlite_add_commit_verify_roundtrip() {
    let dir = repo_dir("sqlite-roundtrip");
    shard(&["init", "--db", "sqlite"], &dir).output().unwrap();

    fs::write(dir.join("hello.txt"), b"Hello, SQLite!").unwrap();
    let output = shard(&["add", "hello.txt"], &dir).output().unwrap();
    assert!(
        output.status.success(),
        "sqlite add failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    // DB file should now exist after first chunk stored
    assert!(dir.join(".shard/objects.db").exists());

    let output = shard(&["commit", "-m", "sqlite-test", "--author", "Test"], &dir)
        .output()
        .unwrap();
    assert!(
        output.status.success(),
        "sqlite commit failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let stdout = String::from_utf8(output.stdout).unwrap();
    let commit_id = stdout.split_whitespace().nth(1).expect("no commit id");

    let output = shard(&["verify", commit_id], &dir).output().unwrap();
    assert!(
        output.status.success(),
        "sqlite verify failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let stdout = String::from_utf8(output.stdout).unwrap();
    assert!(stdout.contains("Verification successful"));
}

#[test]
fn test_sqlite_checkout_restores_files() {
    let dir = repo_dir("sqlite-checkout");
    shard(&["init", "--db", "sqlite"], &dir).output().unwrap();

    fs::write(dir.join("restore.txt"), b"SQLite checkout").unwrap();
    shard(&["add", "restore.txt"], &dir).output().unwrap();
    let output = shard(&["commit", "-m", "co-test", "--author", "T"], &dir)
        .output()
        .unwrap();
    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout).unwrap();
    let commit_id = stdout.split_whitespace().nth(1).unwrap().to_string();

    fs::remove_file(dir.join("restore.txt")).unwrap();
    let output = shard(&["checkout", &commit_id], &dir).output().unwrap();
    assert!(output.status.success());
    assert_eq!(
        fs::read_to_string(dir.join("restore.txt")).unwrap(),
        "SQLite checkout"
    );
}

#[test]
fn test_private_init_creates_repo_key_and_config() {
    let dir = repo_dir("private-init-key");
    let out = shard(&["init", "--private"], &dir).output().unwrap();
    assert!(out.status.success(), "private init failed");

    // repo.key must exist and be a 64-char hex string (32 bytes)
    let key_path = dir.join(".shard/keys/repo.key");
    assert!(key_path.exists(), "repo.key not created for private repo");
    let key_hex = fs::read_to_string(&key_path).unwrap();
    let key_hex = key_hex.trim();
    assert_eq!(
        key_hex.len(),
        64,
        "repo.key should be 64 hex chars (32 bytes)"
    );

    // Config must have private=true
    let out = shard(&["config", "get", "private"], &dir).output().unwrap();
    assert!(out.status.success());
    let stdout = String::from_utf8(out.stdout).unwrap();
    assert!(stdout.contains("private = true"));
}

#[test]
fn test_private_add_commit_verify_checkout_roundtrip() {
    let dir = repo_dir("private-roundtrip");
    let out = shard(&["init", "--private"], &dir).output().unwrap();
    assert!(out.status.success());

    fs::write(dir.join("secret.txt"), b"this is private data").unwrap();
    let out = shard(&["add", "secret.txt"], &dir).output().unwrap();
    assert!(
        out.status.success(),
        "private add failed: {}",
        String::from_utf8_lossy(&out.stderr)
    );

    let out = shard(
        &["commit", "-m", "private-commit", "--author", "Test"],
        &dir,
    )
    .output()
    .unwrap();
    assert!(
        out.status.success(),
        "private commit failed: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    let stdout = String::from_utf8(out.stdout).unwrap();
    let commit_id = stdout.split_whitespace().nth(1).expect("no commit id");

    // Verify must succeed on encrypted data
    let out = shard(&["verify", commit_id], &dir).output().unwrap();
    assert!(
        out.status.success(),
        "private verify failed: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    let stdout = String::from_utf8(out.stdout).unwrap();
    assert!(stdout.contains("Verification successful"));

    // Checkout must produce original content
    fs::remove_file(dir.join("secret.txt")).unwrap();
    let out = shard(&["checkout", commit_id], &dir).output().unwrap();
    assert!(
        out.status.success(),
        "private checkout failed: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    assert_eq!(
        fs::read_to_string(dir.join("secret.txt")).unwrap(),
        "this is private data"
    );

    // Verify stored chunk is NOT the plaintext (encryption active)
    let objects_dir = dir.join(".shard/objects");
    if objects_dir.is_dir() {
        let plaintext = b"this is private data";
        for entry in walkdir::WalkDir::new(&objects_dir) {
            let entry = entry.unwrap();
            if entry.file_type().is_file() {
                let content = fs::read(entry.path()).unwrap();
                assert!(
                    !content.windows(plaintext.len()).any(|w| w == plaintext),
                    "stored chunk contains plaintext — encryption not active"
                );
            }
        }
    }
}

#[test]
fn test_key_rotate_verify_keychain() {
    let dir = repo_dir("key-rotate");
    let out = shard(&["init"], &dir).output().unwrap();
    assert!(out.status.success(), "init failed");

    // Initial key list should show one key (the genesis key)
    let out = shard(&["key", "list"], &dir).output().unwrap();
    assert!(out.status.success());
    let stdout = String::from_utf8(out.stdout).unwrap();
    assert!(stdout.contains("Current key:"));
    assert!(stdout.contains("Key history:"));
    assert!(stdout.contains("(active)"));

    // Verify keychain (should pass with genesis key only)
    let out = shard(&["key", "verify"], &dir).output().unwrap();
    assert!(out.status.success());
    let stdout = String::from_utf8(out.stdout).unwrap();
    assert!(stdout.contains("Keychain verification successful"));

    // Rotate the key
    let out = shard(&["key", "rotate"], &dir).output().unwrap();
    assert!(
        out.status.success(),
        "key rotate failed: {}",
        String::from_utf8_lossy(&out.stderr)
    );

    // Verify keychain (should pass with rotation)
    let out = shard(&["key", "verify"], &dir).output().unwrap();
    assert!(out.status.success());
    let stdout = String::from_utf8(out.stdout).unwrap();
    assert!(stdout.contains("Keychain verification successful"));

    // Key list should now show 2 keys
    let out = shard(&["key", "list"], &dir).output().unwrap();
    assert!(out.status.success());
    let stdout = String::from_utf8(out.stdout).unwrap();
    let lines: Vec<&str> = stdout.lines().filter(|l| l.contains("previous:")).collect();
    assert!(!lines.is_empty(), "should show previous key reference");
}

#[test]
fn test_key_rotate_add_commit_verify_roundtrip() {
    let dir = repo_dir("key-roundtrip");
    let out = shard(&["init"], &dir).output().unwrap();
    assert!(out.status.success());

    // Add a file and commit with genesis key
    fs::write(dir.join("model.bin"), b"some model data").unwrap();
    let out = shard(&["add", "model.bin"], &dir).output().unwrap();
    assert!(out.status.success());
    let out = shard(
        &["commit", "-m", "genesis-commit", "--author", "Test"],
        &dir,
    )
    .output()
    .unwrap();
    assert!(out.status.success());
    let genesis_cid = String::from_utf8(out.stdout)
        .unwrap()
        .split_whitespace()
        .nth(1)
        .expect("no commit id")
        .to_string();

    // Rotate the signing key
    let out = shard(&["key", "rotate"], &dir).output().unwrap();
    assert!(out.status.success());

    // Add another file and commit with rotated key
    fs::write(dir.join("model-v2.bin"), b"more model data").unwrap();
    let out = shard(&["add", "model-v2.bin"], &dir).output().unwrap();
    assert!(out.status.success());
    let out = shard(
        &["commit", "-m", "rotated-commit", "--author", "Test"],
        &dir,
    )
    .output()
    .unwrap();
    assert!(out.status.success());

    // Both commits should verify (keychain validates both)
    let out = shard(&["verify", &genesis_cid], &dir).output().unwrap();
    assert!(
        out.status.success(),
        "genesis commit verify failed after rotation: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    let stdout = String::from_utf8(out.stdout).unwrap();
    assert!(stdout.contains("Verification successful"));

    let out = shard(&["verify", &genesis_cid, "--json"], &dir)
        .output()
        .unwrap();
    assert!(
        out.status.success(),
        "genesis verify --json failed: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    let stdout = String::from_utf8(out.stdout).unwrap();
    assert!(stdout.contains("verified"));
}

#[test]
fn test_key_verify_tampered_rotation_fails() {
    let dir = repo_dir("key-tamper");
    let out = shard(&["init"], &dir).output().unwrap();
    assert!(out.status.success());

    let out = shard(&["key", "rotate"], &dir).output().unwrap();
    assert!(out.status.success());

    // Tamper with rotation signature by modifying the file
    let rot_dir = dir.join(".shard/keys/rotations");
    for entry in fs::read_dir(&rot_dir).unwrap() {
        let entry = entry.unwrap();
        if entry.file_type().unwrap().is_file() {
            let data = fs::read(entry.path()).unwrap();
            if let Ok(mut obj) = serde_json::from_slice::<serde_json::Value>(&data) {
                if obj.get("signature_hex").is_some() {
                    let tampered = "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
                    obj["signature_hex"] = serde_json::Value::String(tampered.to_string());
                    fs::write(entry.path(), serde_json::to_string_pretty(&obj).unwrap()).unwrap();
                }
            }
        }
    }

    // Verify should fail
    let out = shard(&["key", "verify"], &dir).output().unwrap();
    assert!(
        !out.status.success(),
        "tampered keychain should fail verify"
    );
    let stderr = String::from_utf8_lossy(&out.stderr);
    assert!(stderr.contains("Keychain verification failed"));
}

#[test]
fn test_transfer_list_remove() {
    let dir = repo_dir("transfer-list");
    let out = shard(&["init"], &dir).output().unwrap();
    assert!(out.status.success());

    // No transfers initially
    let out = shard(&["transfer", "list"], &dir).output().unwrap();
    assert!(out.status.success());
    let stdout = String::from_utf8(out.stdout).unwrap();
    assert!(stdout.contains("No incomplete transfers"));

    // Create a fake partial transfer directory
    let partial = dir.join(".shard/partial/fake-commit-id");
    fs::create_dir_all(&partial).unwrap();
    fs::write(partial.join("chunk1"), b"data1").unwrap();
    fs::write(partial.join("chunk2"), b"data2").unwrap();

    // List should show it
    let out = shard(&["transfer", "list"], &dir).output().unwrap();
    assert!(out.status.success());
    let stdout = String::from_utf8(out.stdout).unwrap();
    assert!(stdout.contains("fake-commit-id"));

    // JSON output
    let out = shard(&["transfer", "list", "--json"], &dir)
        .output()
        .unwrap();
    assert!(out.status.success());
    let stdout = String::from_utf8(out.stdout).unwrap();
    assert!(stdout.contains("fake-commit-id"));

    // Remove should clean it up
    let out = shard(&["transfer", "remove", "fake-commit-id"], &dir)
        .output()
        .unwrap();
    assert!(out.status.success());

    // List should be empty again
    let out = shard(&["transfer", "list"], &dir).output().unwrap();
    assert!(out.status.success());
    let stdout = String::from_utf8(out.stdout).unwrap();
    assert!(stdout.contains("No incomplete transfers"));
}

#[test]
fn test_diff_no_changes() {
    let dir = repo_dir("diff-none");
    let out = shard(&["init"], &dir).output().unwrap();
    assert!(out.status.success());

    fs::write(dir.join("f.txt"), b"content").unwrap();
    shard(&["add", "f.txt"], &dir).output().unwrap();
    let out = shard(&["commit", "-m", "c1", "--author", "T"], &dir)
        .output()
        .unwrap();
    assert!(out.status.success());
    let c1 = String::from_utf8(out.stdout)
        .unwrap()
        .split_whitespace()
        .nth(1)
        .unwrap()
        .to_string();

    // Diff same commit against itself
    let out = shard(&["diff", &c1, &c1], &dir).output().unwrap();
    assert!(out.status.success());
    let stdout = String::from_utf8(out.stdout).unwrap();
    assert!(stdout.contains("No differences"));
}

#[test]
fn test_diff_single_file_added() {
    let dir = repo_dir("diff-added");
    shard(&["init"], &dir).output().unwrap();
    fs::write(dir.join("f.txt"), b"line1\nline2\nline3").unwrap();
    shard(&["add", "f.txt"], &dir).output().unwrap();
    let out = shard(&["commit", "-m", "c1", "--author", "T"], &dir)
        .output()
        .unwrap();
    assert!(out.status.success());
    let c1 = String::from_utf8(out.stdout)
        .unwrap()
        .split_whitespace()
        .nth(1)
        .unwrap()
        .to_string();

    // Compare c1 with initial (empty) HEAD — need a parent commit reference
    // Instead, compare two commits: the genesis empty init has no commit.
    // Use the first commit's parent which may not exist.
    // Better: create two different commits and compare them.
    fs::write(dir.join("g.txt"), b"other").unwrap();
    shard(&["add", "g.txt"], &dir).output().unwrap();
    let out = shard(&["commit", "-m", "c2", "--author", "T"], &dir)
        .output()
        .unwrap();
    assert!(out.status.success());
    let c2 = String::from_utf8(out.stdout)
        .unwrap()
        .split_whitespace()
        .nth(1)
        .unwrap()
        .to_string();

    let out = shard(&["diff", &c1, &c2], &dir).output().unwrap();
    assert!(out.status.success());
    let stdout = String::from_utf8(out.stdout).unwrap();
    assert!(stdout.contains("f.txt") || stdout.contains("g.txt"));
    assert!(stdout.contains("No differences") || stdout.contains("---") || stdout.contains("+++"));
}

#[test]
fn test_diff_modified_file() {
    let dir = repo_dir("diff-mod");
    shard(&["init"], &dir).output().unwrap();
    fs::write(dir.join("f.txt"), b"hello\nworld\n").unwrap();
    shard(&["add", "f.txt"], &dir).output().unwrap();
    let out = shard(&["commit", "-m", "c1", "--author", "T"], &dir)
        .output()
        .unwrap();
    assert!(out.status.success());
    let c1 = String::from_utf8(out.stdout)
        .unwrap()
        .split_whitespace()
        .nth(1)
        .unwrap()
        .to_string();

    fs::write(dir.join("f.txt"), b"hello\nuniverse\n").unwrap();
    shard(&["add", "f.txt"], &dir).output().unwrap();
    let out = shard(&["commit", "-m", "c2", "--author", "T"], &dir)
        .output()
        .unwrap();
    assert!(out.status.success());
    let c2 = String::from_utf8(out.stdout)
        .unwrap()
        .split_whitespace()
        .nth(1)
        .unwrap()
        .to_string();

    let out = shard(&["diff", &c1, &c2], &dir).output().unwrap();
    assert!(out.status.success());
    let stdout = String::from_utf8(out.stdout).unwrap();
    assert!(stdout.contains("f.txt"));
    assert!(stdout.contains("-world"));
    assert!(stdout.contains("+universe"));
}

#[test]
fn test_diff_json_output() {
    let dir = repo_dir("diff-json");
    shard(&["init"], &dir).output().unwrap();
    fs::write(dir.join("a.txt"), b"aaa").unwrap();
    shard(&["add", "a.txt"], &dir).output().unwrap();
    let out = shard(&["commit", "-m", "c1", "--author", "T"], &dir)
        .output()
        .unwrap();
    assert!(out.status.success());
    let c1 = String::from_utf8(out.stdout)
        .unwrap()
        .split_whitespace()
        .nth(1)
        .unwrap()
        .to_string();

    fs::write(dir.join("b.txt"), b"bbb").unwrap();
    shard(&["add", "b.txt"], &dir).output().unwrap();
    let out = shard(&["commit", "-m", "c2", "--author", "T"], &dir)
        .output()
        .unwrap();
    assert!(out.status.success());
    let c2 = String::from_utf8(out.stdout)
        .unwrap()
        .split_whitespace()
        .nth(1)
        .unwrap()
        .to_string();

    let out = shard(&["diff", &c1, &c2, "--json"], &dir).output().unwrap();
    assert!(out.status.success());
    let stdout = String::from_utf8(out.stdout).unwrap();
    assert!(stdout.contains("added"));
    assert!(stdout.contains("b.txt"));
}

#[test]
fn test_diff_between_commits_with_removed_file() {
    let dir = repo_dir("diff-removed");
    shard(&["init"], &dir).output().unwrap();
    fs::write(dir.join("keep.txt"), b"keep").unwrap();
    fs::write(dir.join("remove.txt"), b"remove").unwrap();
    shard(&["add", "keep.txt"], &dir).output().unwrap();
    shard(&["add", "remove.txt"], &dir).output().unwrap();
    let out = shard(&["commit", "-m", "c1", "--author", "T"], &dir)
        .output()
        .unwrap();
    assert!(out.status.success());
    let c1 = String::from_utf8(out.stdout)
        .unwrap()
        .split_whitespace()
        .nth(1)
        .unwrap()
        .to_string();

    // Remove remove.txt and commit
    fs::remove_file(dir.join("remove.txt")).unwrap();
    // shard add with a non-existent file would fail. Instead, just re-add keep.txt.
    // But we can't remove from index directly. Let's use prune or a new file.
    // Better approach: compare c1 (has both) with c2 (only keep.txt)
    // Since we can't remove from index, let's modify keep.txt and add a new file:
    fs::write(dir.join("keep.txt"), b"keep-modified").unwrap();
    shard(&["add", "keep.txt"], &dir).output().unwrap();
    let out = shard(&["commit", "-m", "c2", "--author", "T"], &dir)
        .output()
        .unwrap();
    assert!(out.status.success());
    let c2 = String::from_utf8(out.stdout)
        .unwrap()
        .split_whitespace()
        .nth(1)
        .unwrap()
        .to_string();

    // c1 has {keep.txt, remove.txt}, c2 has {keep.txt}
    // diff should note remove.txt is gone from c2
    let out = shard(&["diff", &c1, &c2], &dir).output().unwrap();
    assert!(out.status.success());
    let stdout = String::from_utf8(out.stdout).unwrap();
    // c2 doesn't have remove.txt
    // Since remove.txt was in c1 manifest but not in c2 index/commit, it should show as removed
    assert!(stdout.contains("keep") || stdout.contains("remove"));
}

#[test]
fn test_partial_recover_after_interrupted_pull() {
    let dir = repo_dir("partial-recover");
    let out = shard(&["init"], &dir).output().unwrap();
    assert!(out.status.success());

    // Create a multi-chunk file (~5 MB = 2 chunks with 4 MiB default)
    let data = b"x".repeat(6_000_000);
    fs::write(dir.join("large.bin"), &data).unwrap();
    let out = shard(&["add", "large.bin"], &dir).output().unwrap();
    assert!(out.status.success());
    let out = shard(&["commit", "-m", "big-file", "--author", "Test"], &dir)
        .output()
        .unwrap();
    assert!(out.status.success());
    let stdout = String::from_utf8(out.stdout).unwrap();
    let commit_id = stdout
        .split_whitespace()
        .nth(1)
        .expect("no commit id")
        .to_string();

    // Simulate partial transfer: copy chunk files from objects into .shard/partial/<commit_id>/
    let partial_dir = dir.join(".shard/partial").join(&commit_id);
    fs::create_dir_all(&partial_dir).unwrap();

    // Copy a chunk file from the objects directory to simulate a previous partial download
    let objects_dir = dir.join(".shard/objects");
    let mut copied = 0usize;
    if objects_dir.is_dir() {
        for entry in walkdir::WalkDir::new(&objects_dir) {
            if copied >= 1 {
                break;
            }
            let entry = entry.unwrap();
            if entry.file_type().is_file() {
                let name = entry.file_name().to_string_lossy().to_string();
                let content = fs::read(entry.path()).unwrap();
                fs::write(partial_dir.join(&name), &content).unwrap();
                copied += 1;
            }
        }
    }

    // Verify — should still succeed (chunks in store, partial just has copies)
    let out = shard(&["verify", &commit_id], &dir).output().unwrap();
    assert!(
        out.status.success(),
        "verify after partial recovery failed: {}",
        String::from_utf8_lossy(&out.stderr)
    );

    // Clean up via transfer remove
    let out = shard(&["transfer", "remove", &commit_id], &dir)
        .output()
        .unwrap();
    assert!(out.status.success());

    // Partial dir should be gone
    assert!(
        !partial_dir.exists(),
        "partial dir should be removed after transfer remove"
    );

    // Verify still works after partial cleanup
    let out = shard(&["verify", &commit_id], &dir).output().unwrap();
    assert!(
        out.status.success(),
        "verify after partial cleanup failed: {}",
        String::from_utf8_lossy(&out.stderr)
    );
}

#[test]
fn test_cbor_commit_verify_roundtrip() {
    let dir = repo_dir("cbor-commit");
    let out = shard(&["init"], &dir).output().unwrap();
    assert!(out.status.success(), "init failed");

    // Enable CBOR serialization
    let out = shard(&["config", "set", "serialization_format", "cbor"], &dir)
        .output()
        .unwrap();
    assert!(
        out.status.success(),
        "config set cbor failed: {}",
        String::from_utf8_lossy(&out.stderr)
    );

    fs::write(dir.join("data.txt"), b"cbor test data").unwrap();
    let out = shard(&["add", "data.txt"], &dir).output().unwrap();
    assert!(out.status.success(), "cbor add failed");
    let out = shard(&["commit", "-m", "cbor-commit", "--author", "Test"], &dir)
        .output()
        .unwrap();
    assert!(
        out.status.success(),
        "cbor commit failed: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    let stdout = String::from_utf8(out.stdout).unwrap();
    let commit_id = stdout.split_whitespace().nth(1).expect("no commit id");

    // Verify
    let out = shard(&["verify", commit_id], &dir).output().unwrap();
    assert!(
        out.status.success(),
        "cbor verify failed: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    let stdout = String::from_utf8(out.stdout).unwrap();
    assert!(stdout.contains("Verification successful"));

    // Checkout
    fs::remove_file(dir.join("data.txt")).unwrap();
    let out = shard(&["checkout", commit_id], &dir).output().unwrap();
    assert!(
        out.status.success(),
        "cbor checkout failed: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    assert_eq!(
        fs::read_to_string(dir.join("data.txt")).unwrap(),
        "cbor test data"
    );

    // Verify works after cleanup
    let out = shard(&["verify", commit_id], &dir).output().unwrap();
    assert!(out.status.success(), "cbor verify after checkout failed");
}

#[test]
fn test_cbor_mixed_format_compat() {
    let dir = repo_dir("cbor-compat");
    let out = shard(&["init"], &dir).output().unwrap();
    assert!(out.status.success(), "init failed");

    // Commit 1: JSON (default)
    fs::write(dir.join("f1.txt"), b"json version").unwrap();
    let out = shard(&["add", "f1.txt"], &dir).output().unwrap();
    assert!(out.status.success());
    let out = shard(&["commit", "-m", "json-commit", "--author", "Test"], &dir)
        .output()
        .unwrap();
    assert!(out.status.success());
    let stdout = String::from_utf8(out.stdout).unwrap();
    let c1 = stdout.split_whitespace().nth(1).unwrap().to_string();

    // Switch to CBOR and commit again
    let out = shard(&["config", "set", "serialization_format", "cbor"], &dir)
        .output()
        .unwrap();
    assert!(out.status.success());

    fs::write(dir.join("f2.txt"), b"cbor version").unwrap();
    let out = shard(&["add", "f2.txt"], &dir).output().unwrap();
    assert!(out.status.success());
    let out = shard(&["commit", "-m", "cbor-commit", "--author", "Test"], &dir)
        .output()
        .unwrap();
    assert!(out.status.success());
    let stdout = String::from_utf8(out.stdout).unwrap();
    let c2 = stdout.split_whitespace().nth(1).unwrap().to_string();

    // Both commits should verify
    let out = shard(&["verify", &c1], &dir).output().unwrap();
    assert!(
        out.status.success(),
        "JSON commit verify failed after CBOR commit"
    );
    let out = shard(&["verify", &c2], &dir).output().unwrap();
    assert!(out.status.success(), "CBOR commit verify failed");

    // Log should show both commits
    let out = shard(&["log"], &dir).output().unwrap();
    assert!(out.status.success());
    let stdout = String::from_utf8(out.stdout).unwrap();
    assert!(stdout.contains(&c1[..8]));
    assert!(stdout.contains(&c2[..8]));

    // Diff should work between the two
    let out = shard_ok(&["diff", &c1, &c2], &dir);
    let stdout = String::from_utf8(out.stdout).unwrap();
    assert!(stdout.contains("f1.txt") || stdout.contains("f2.txt"));
}

fn spawn_share(repo: &Path) -> (std::process::Child, String) {
    let mut child = Command::new(shard_bin())
        .args(["share", "--listen", "/ip4/127.0.0.1/tcp/0"])
        .current_dir(repo)
        .stdout(Stdio::piped())
        .stderr(Stdio::null())
        .spawn()
        .expect("failed to start share");

    let reader = BufReader::new(child.stdout.take().unwrap());
    let (tx, rx) = mpsc::channel();
    std::thread::spawn(move || {
        let mut peer_id = String::new();
        let mut listen_addr = String::new();
        let mut sent = false;
        for line in reader.lines().map_while(Result::ok) {
            if !sent {
                if let Some(id) = line.strip_prefix("Local peer id: ") {
                    peer_id = id.trim().to_string();
                }
                if let Some(addr) = line.strip_prefix("Listening on ") {
                    let candidate = addr.trim().trim_matches('"').to_string();
                    if candidate.contains("/ip4/127.0.0.1/") {
                        listen_addr = candidate;
                    }
                }
                if !peer_id.is_empty() && !listen_addr.is_empty() {
                    let _ = tx.send(format!("{}/p2p/{}", listen_addr, peer_id));
                    sent = true;
                }
            }
        }
    });

    let multiaddr = rx
        .recv_timeout(Duration::from_secs(30))
        .expect("timed out waiting for share multiaddr");
    (child, multiaddr)
}

#[test]
fn test_p2p_auth_authorized_succeeds_and_unauthorized_denied() {
    let dir_a = repo_dir("auth-a");
    let dir_b = repo_dir("auth-b");
    let dir_c = repo_dir("auth-c");

    shard_ok(&["init"], &dir_a);
    shard_ok(&["init"], &dir_b);
    shard_ok(&["init"], &dir_c);

    // Authorize B's public key on A (C is NOT authorized)
    let b_pub = fs::read(dir_b.join(".shard/keys/public.key")).unwrap();
    let b_pub_hex: String = b_pub.iter().map(|b| format!("{:02x}", b)).collect();
    shard_ok(&["key", "add-authorized", &b_pub_hex], &dir_a);

    fs::write(dir_a.join("restricted.txt"), b"authorized payload").unwrap();
    shard_ok(&["add", "restricted.txt"], &dir_a);
    let out = shard_ok(&["commit", "-m", "restricted", "--author", "A"], &dir_a);
    let commit_id = String::from_utf8(out.stdout)
        .unwrap()
        .split_whitespace()
        .nth(1)
        .unwrap()
        .to_string();

    let (mut child, multiaddr) = spawn_share(&dir_a);

    // Authorized peer B succeeds
    shard_ok(&["pull", &multiaddr, &commit_id], &dir_b);
    assert_eq!(
        fs::read_to_string(dir_b.join("restricted.txt")).unwrap(),
        "authorized payload"
    );

    // Unauthorized peer C must be denied
    let c_out = shard(&["pull", &multiaddr, &commit_id], &dir_c)
        .output()
        .unwrap();
    assert!(
        !c_out.status.success(),
        "unauthorized peer C should have been denied"
    );

    let _ = child.kill();
    let _ = child.wait();
}

#[test]
fn test_p2p_push_and_pull_zstd_and_private_with_resume() {
    // 1. Default zstd push & pull
    let dir_src = repo_dir("push-zstd-src");
    let dir_dst = repo_dir("push-zstd-dst");
    shard_ok(&["init", "--compression", "zstd"], &dir_src);
    shard_ok(&["init", "--compression", "zstd"], &dir_dst);

    let big_payload = b"zstd push pull payload data! ".repeat(2048);
    fs::write(dir_src.join("weights.bin"), &big_payload).unwrap();
    shard_ok(&["add", "weights.bin"], &dir_src);
    let out = shard_ok(&["commit", "-m", "zstd-push", "--author", "S"], &dir_src);
    let cid = String::from_utf8(out.stdout)
        .unwrap()
        .split_whitespace()
        .nth(1)
        .unwrap()
        .to_string();

    let src_pub = fs::read(dir_src.join(".shard/keys/public.key")).unwrap();
    let src_pub_hex: String = src_pub.iter().map(|b| format!("{:02x}", b)).collect();
    shard_ok(&["key", "add-authorized", &src_pub_hex], &dir_dst);
    let (mut dst_server, dst_addr) = spawn_share(&dir_dst);
    let push_out = shard_ok(&["push", &dst_addr], &dir_src);
    assert!(String::from_utf8_lossy(&push_out.stdout).contains(&cid));
    let _ = dst_server.kill();
    let _ = dst_server.wait();

    // Checkout on destination proves pushed zstd chunks are intact!
    shard_ok(&["checkout", &cid], &dir_dst);
    assert_eq!(fs::read(dir_dst.join("weights.bin")).unwrap(), big_payload);

    // 2. Private (--private) encrypted push & pull + partial resume!
    let priv_a = repo_dir("priv-a");
    let priv_b = repo_dir("priv-b");
    shard_ok(&["init", "--private", "--compression", "zstd"], &priv_a);
    shard_ok(&["init", "--private", "--compression", "zstd"], &priv_b);
    // Share repo.key between private cluster peers
    fs::copy(
        priv_a.join(".shard/keys/repo.key"),
        priv_b.join(".shard/keys/repo.key"),
    )
    .unwrap();

    let secret_payload = b"top secret encrypted tensor data! ".repeat(1024);
    fs::write(priv_a.join("model.safetensors"), &secret_payload).unwrap();
    shard_ok(&["add", "model.safetensors"], &priv_a);
    let out = shard_ok(&["commit", "-m", "priv-commit", "--author", "A"], &priv_a);
    let priv_cid = String::from_utf8(out.stdout)
        .unwrap()
        .split_whitespace()
        .nth(1)
        .unwrap()
        .to_string();

    // Pre-populate partial/ on priv_b with the encrypted chunk to test resume recovery!
    let chunk_hash = format!("{}", blake3::hash(&secret_payload).to_hex());
    let src_chunk_file = priv_a
        .join(".shard/objects")
        .join(&chunk_hash[..2])
        .join(&chunk_hash);
    let partial_dir = priv_b.join(".shard/partial").join(&priv_cid);
    fs::create_dir_all(&partial_dir).unwrap();
    fs::copy(&src_chunk_file, partial_dir.join(&chunk_hash)).unwrap();

    let (mut priv_server, priv_addr) = spawn_share(&priv_a);
    let pull_out = shard_ok(&["pull", &priv_addr, &priv_cid], &priv_b);
    let pull_stdout = String::from_utf8(pull_out.stdout).unwrap();
    assert!(
        pull_stdout.contains("Recovered 1 chunks from partial transfer"),
        "expected partial resume recovery, got: {}",
        pull_stdout
    );
    assert_eq!(
        fs::read(priv_b.join("model.safetensors")).unwrap(),
        secret_payload
    );
    let _ = priv_server.kill();
    let _ = priv_server.wait();

    // Push private commit from priv_a to a fresh priv_c sharing repo.key
    let priv_c = repo_dir("priv-c");
    shard_ok(&["init", "--private", "--compression", "zstd"], &priv_c);
    fs::copy(
        priv_a.join(".shard/keys/repo.key"),
        priv_c.join(".shard/keys/repo.key"),
    )
    .unwrap();
    let priv_a_pub = fs::read(priv_a.join(".shard/keys/public.key")).unwrap();
    let priv_a_pub_hex: String = priv_a_pub.iter().map(|b| format!("{:02x}", b)).collect();
    shard_ok(&["key", "add-authorized", &priv_a_pub_hex], &priv_c);
    let (mut priv_c_server, priv_c_addr) = spawn_share(&priv_c);
    shard_ok(&["push", &priv_c_addr], &priv_a);
    let _ = priv_c_server.kill();
    let _ = priv_c_server.wait();
    shard_ok(&["checkout", &priv_cid], &priv_c);
    assert_eq!(
        fs::read(priv_c.join("model.safetensors")).unwrap(),
        secret_payload
    );
}

#[test]
fn test_merge_zstd_preserves_compression_and_detects_conflicts() {
    let dir = repo_dir("merge-zstd");
    shard_ok(&["init", "--compression", "zstd"], &dir);

    fs::write(dir.join("base.txt"), b"base content").unwrap();
    shard_ok(&["add", "base.txt"], &dir);
    shard_ok(&["commit", "-m", "base", "--author", "T"], &dir);

    // Clean 3-way merge branch
    shard_ok(&["branch", "create", "feature/clean"], &dir);
    shard_ok(&["checkout", "feature/clean"], &dir);
    fs::write(dir.join("feat.txt"), b"feature content").unwrap();
    shard_ok(&["add", "feat.txt"], &dir);
    shard_ok(&["commit", "-m", "add feat", "--author", "T"], &dir);

    shard_ok(&["checkout", "main"], &dir);
    fs::write(dir.join("main2.txt"), b"main2 content").unwrap();
    shard_ok(&["add", "main2.txt"], &dir);
    shard_ok(&["commit", "-m", "add main2", "--author", "T"], &dir);

    let out = shard_ok(
        &[
            "merge",
            "feature/clean",
            "-m",
            "merge clean",
            "--author",
            "T",
        ],
        &dir,
    );
    let merge_cid = String::from_utf8(out.stdout)
        .unwrap()
        .split_whitespace()
        .nth(2)
        .unwrap()
        .to_string();

    // Verify AND checkout merge commit — must reconstruct exact plaintext (NOT raw zstd bytes!)
    shard_ok(&["verify", &merge_cid], &dir);
    let _ = fs::remove_file(dir.join("base.txt"));
    let _ = fs::remove_file(dir.join("feat.txt"));
    let _ = fs::remove_file(dir.join("main2.txt"));
    shard_ok(&["checkout", &merge_cid], &dir);
    assert_eq!(
        fs::read_to_string(dir.join("base.txt")).unwrap(),
        "base content"
    );
    assert_eq!(
        fs::read_to_string(dir.join("feat.txt")).unwrap(),
        "feature content"
    );
    assert_eq!(
        fs::read_to_string(dir.join("main2.txt")).unwrap(),
        "main2 content"
    );

    // Conflicting 3-way merge must fail with clear conflict error!
    shard_ok(&["checkout", "main"], &dir);
    shard_ok(&["branch", "create", "feature/conflict"], &dir);
    shard_ok(&["checkout", "feature/conflict"], &dir);
    fs::write(dir.join("base.txt"), b"theirs version").unwrap();
    shard_ok(&["add", "base.txt"], &dir);
    shard_ok(&["commit", "-m", "theirs edit", "--author", "T"], &dir);

    shard_ok(&["checkout", "main"], &dir);
    fs::write(dir.join("base.txt"), b"ours version").unwrap();
    shard_ok(&["add", "base.txt"], &dir);
    shard_ok(&["commit", "-m", "ours edit", "--author", "T"], &dir);

    let conflict = shard(
        &[
            "merge",
            "feature/conflict",
            "-m",
            "should conflict",
            "--author",
            "T",
        ],
        &dir,
    )
    .output()
    .unwrap();
    assert!(!conflict.status.success(), "conflicting merge must fail");
    let stderr = String::from_utf8_lossy(&conflict.stderr);
    assert!(
        stderr.contains("conflict") && stderr.contains("base.txt"),
        "expected merge conflict listing base.txt, got: {}",
        stderr
    );
}

#[test]
fn test_branch_traversal_rejected_and_gc_preserves_slashed_branch() {
    let dir = repo_dir("slashed-branch-gc");
    shard_ok(&["init"], &dir);
    fs::write(dir.join("a.txt"), b"alpha").unwrap();
    shard_ok(&["add", "a.txt"], &dir);
    shard_ok(&["commit", "-m", "c1", "--author", "T"], &dir);

    // Branch traversal must be rejected
    let bad = shard(&["branch", "create", "../pwned"], &dir)
        .output()
        .unwrap();
    assert!(!bad.status.success(), "branch traversal must fail");

    // Slashed branch works and survives GC!
    shard_ok(&["branch", "create", "feature/models/v1"], &dir);
    shard_ok(&["checkout", "feature/models/v1"], &dir);
    fs::write(dir.join("nested/weights.bin"), b"branch-only weights").unwrap_or_else(|_| {
        fs::create_dir_all(dir.join("nested")).unwrap();
        fs::write(dir.join("nested/weights.bin"), b"branch-only weights").unwrap();
    });
    shard_ok(&["add", "nested/weights.bin"], &dir);
    let out = shard_ok(&["commit", "-m", "feat commit", "--author", "T"], &dir);
    let feat_cid = String::from_utf8(out.stdout)
        .unwrap()
        .split_whitespace()
        .nth(1)
        .unwrap()
        .to_string();

    // Switch back to main and run GC — feature/models/v1 commit must NOT be deleted!
    shard_ok(&["checkout", "main"], &dir);
    shard_ok(&["prune"], &dir);
    shard_ok(&["verify", &feat_cid], &dir);
}

#[test]
fn test_key_verify_json_exits_nonzero_on_tamper() {
    let dir = repo_dir("key-verify-json-exit");
    shard_ok(&["init"], &dir);
    shard_ok(&["key", "rotate"], &dir);

    let rot_dir = dir.join(".shard/keys/rotations");
    for entry in fs::read_dir(&rot_dir).unwrap() {
        let entry = entry.unwrap();
        let mut obj: serde_json::Value =
            serde_json::from_slice(&fs::read(entry.path()).unwrap()).unwrap();
        obj["signature_hex"] = serde_json::Value::String("00".repeat(64));
        fs::write(entry.path(), serde_json::to_string_pretty(&obj).unwrap()).unwrap();
    }

    let out = shard(&["key", "verify", "--json"], &dir).output().unwrap();
    assert!(
        !out.status.success(),
        "key verify --json must exit non-zero on tampered rotation"
    );
    let v: serde_json::Value =
        serde_json::from_slice(&out.stdout).expect("stdout must still be valid JSON");
    assert_eq!(v["verified"], false);
}

#[test]
fn test_commit_snapshot_and_sled_import_preserve_subdirs() {
    let dir = repo_dir("sled-import-snapshot");
    shard_ok(&["init", "--db", "sled"], &dir);

    let import_src = tempfile::tempdir().unwrap();
    fs::create_dir_all(import_src.path().join("sub/dir")).unwrap();
    fs::write(import_src.path().join("root.txt"), b"root").unwrap();
    fs::write(import_src.path().join("sub/dir/leaf.txt"), b"leaf").unwrap();

    // Sled import must NOT fail with exclusive lock double-open, and must preserve subdirs!
    shard_ok(
        &[
            "import",
            import_src.path().to_str().unwrap(),
            "-m",
            "imported",
            "--author",
            "T",
        ],
        &dir,
    );

    // Add a second file and commit — unmodified imported files must be carried forward!
    fs::write(dir.join("extra.txt"), b"extra").unwrap();
    shard_ok(&["add", "extra.txt"], &dir);
    let out = shard_ok(&["commit", "-m", "second", "--author", "T"], &dir);
    let cid2 = String::from_utf8(out.stdout)
        .unwrap()
        .split_whitespace()
        .nth(1)
        .unwrap()
        .to_string();

    let exported = tempfile::tempdir().unwrap();
    shard_ok(&["export", &cid2, exported.path().to_str().unwrap()], &dir);
    assert_eq!(
        fs::read_to_string(exported.path().join("root.txt")).unwrap(),
        "root"
    );
    assert_eq!(
        fs::read_to_string(exported.path().join("sub/dir/leaf.txt")).unwrap(),
        "leaf"
    );
    assert_eq!(
        fs::read_to_string(exported.path().join("extra.txt")).unwrap(),
        "extra"
    );
}