nibkeep-node 0.1.0

The NibKeep engine: everything an app needs to work with a library
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
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
//! Trees: folders whose files NibKeep tracks by their paths, with every version, as Git
//! tracks a working tree; commits of many files in one change, each with a revision; and
//! each replica's index of its copy ([technical design: trees]).
//!
//! [technical design: trees]: ../../../docs/Technical-design.md#15-trees

mod common;

use std::collections::BTreeSet;
use std::fs;
use std::path::{Path, PathBuf};
use std::time::{Duration, SystemTime};

use common::{library, now, this_day, values};
use nibkeep_base::EntityId;
use nibkeep_model::{Value, fields};
use nibkeep_node::{
	Change, Commit, Edit, Library, NodeError, TreeChange, TreePoint, resolved_path,
};
use nibkeep_std::{COMMITTED_IN, CONTENT, FILE, IN_TREE, MESSAGE, PARTS, REVISION, TREE_PATH};
use nibkeep_testkit::TestRng;

/// A folder of its own, as the file system resolves it.
fn folder(name: &str) -> PathBuf {
	let made = nibkeep_testkit::empty_directory(env!("CARGO_TARGET_TMPDIR"), name);
	resolved_path(&made).expect("resolves")
}

/// Writes a file, with its folders, and makes it look written an hour ago, so that what
/// reads it again only because it was written a moment ago does not.
fn write(root: &Path, path: &str, content: &str) -> PathBuf {
	let path = root.join(path);
	fs::create_dir_all(path.parent().expect("a folder")).expect("makes folders");
	fs::write(&path, content).expect("writes");
	age(&path);
	path
}

/// Makes a file look written an hour ago.
fn age(path: &Path) {
	let hour_ago = SystemTime::now() - Duration::from_secs(3_600);
	fs::File::options()
		.write(true)
		.open(path)
		.and_then(|file| file.set_modified(hour_ago))
		.expect("ages");
}

/// The types of an entity, as the library shows them.
fn types(library: &Library, entity: EntityId) -> Vec<nibkeep_model::TypeName> {
	library
		.entity(library.personal_space(), entity, this_day())
		.expect("reads")
		.expect("the entity")
		.types
}

fn text(text: &str) -> Value {
	Value::Text(text.to_owned())
}

/// The changes, as `(path, change)`, for comparing.
fn changes(changes: &[TreeChange]) -> Vec<(String, Change)> {
	changes
		.iter()
		.map(|change| (change.path.clone(), change.change.clone()))
		.collect()
}

/// A library with a tree started in a folder of its own, which holds `files`.
fn with_tree(
	name: &str,
	files: &[(&str, &str)],
	rng: &mut TestRng,
) -> (Library, EntityId, PathBuf) {
	let (_, mut library) = library(name, rng);
	let root = folder(&format!("{name}, tree"));
	for (path, content) in files {
		write(&root, path, content);
	}
	let personal = library.personal_space();
	let started = library
		.start_tree(personal, &root, Some("Start"), now() + 1_000, rng)
		.expect("starts");
	(library, started.tree, root)
}

/// The entity at a path of a tree, as the library has it now.
fn at_path(library: &Library, tree: EntityId, path: &str) -> EntityId {
	let personal = library.personal_space();
	library
		.tree_entries(personal, tree)
		.expect("reads")
		.into_iter()
		.find(|entry| entry.path == path)
		.unwrap_or_else(|| panic!("nothing at {path}"))
		.entity
}

#[test]
fn starting_a_tree_commits_its_files_in_parts_after_one_with_its_revision() {
	let mut rng = TestRng::new("tree started");
	let (_, mut library) = library("tree started", &mut rng);
	let root = folder("tree started, notes");
	write(&root, "notes.md", "# Notes\n");
	write(&root, "scripts/backup.sh", "#!/bin/sh\n");
	write(&root, ".github/workflows/ci.yml", "on: push\n");
	write(&root, ".env.example", "TOKEN=\n");
	#[cfg(unix)]
	{
		use std::os::unix::fs::{PermissionsExt, symlink};
		fs::set_permissions(
			root.join("scripts/backup.sh"),
			fs::Permissions::from_mode(0o755),
		)
		.expect("makes it executable");
		symlink("notes.md", root.join("latest.md")).expect("links");
	}
	let personal = library.personal_space();
	let preview = library
		.would_start_tree(personal, &root)
		.expect("would start");
	assert_eq!(preview.files, if cfg!(unix) { 5 } else { 4 });
	assert_eq!(preview.bytes, 8 + 10 + 9 + 7);
	let changes_before = library.status().expect("status").spaces[0].changes;
	let started = library
		.start_tree(personal, &root, Some("Start"), now() + 1_000, &mut rng)
		.expect("starts");
	assert_eq!(started.root, root);
	let mut expected = vec![
		(".env.example".to_owned(), Change::New),
		(".github/workflows/ci.yml".to_owned(), Change::New),
		("notes.md".to_owned(), Change::New),
		("scripts/backup.sh".to_owned(), Change::New),
	];
	if cfg!(unix) {
		expected.insert(2, ("latest.md".to_owned(), Change::New));
	}
	assert_eq!(changes(&started.committed.changes), expected);
	// The revision, the tree, then a part for each file.
	assert_eq!(started.committed.parts, 2 + expected.len());

	// The tree is named after its folder, and its root says where it is on this device.
	let trees = library.trees(personal).expect("lists");
	assert_eq!(trees.len(), 1);
	assert_eq!(trees[0].id, started.tree);
	assert_eq!(
		trees[0].name.as_deref(),
		root.file_name().and_then(|name| name.to_str())
	);
	assert_eq!(trees[0].root.as_deref(), Some(root.as_path()));
	let found = library
		.tree_at(personal, &root.join("scripts"))
		.expect("looks")
		.expect("finds the tree from a folder inside it");
	assert_eq!(found.id, started.tree);
	assert!(
		library
			.tree_at(personal, root.parent().expect("a parent"))
			.expect("looks")
			.is_none()
	);

	// Each file is a file of its own, with its path, and its revision names the tree and
	// holds the message.
	let notes = at_path(&library, started.tree, "notes.md");
	assert_eq!(types(&library, notes), [FILE]);
	assert_eq!(values(&library, notes, &TREE_PATH), [text("notes.md")]);
	assert_eq!(
		values(&library, notes, &IN_TREE),
		[Value::Reference(started.tree)]
	);
	assert_eq!(
		values(&library, notes, &fields::NAME),
		[Value::Name(nibkeep_model::Name::new("notes.md"))]
	);
	let revision = started.committed.revision.expect("a revision");
	assert_eq!(types(&library, revision), [REVISION]);
	assert_eq!(values(&library, revision, &MESSAGE), [text("Start")]);
	assert_eq!(
		values(&library, revision, &IN_TREE),
		[Value::Reference(started.tree)]
	);
	#[cfg(unix)]
	{
		use nibkeep_std::{EXECUTABLE, LINK_TARGET, SYMBOLIC_LINK};
		let script = at_path(&library, started.tree, "scripts/backup.sh");
		assert_eq!(
			values(&library, script, &EXECUTABLE),
			[Value::Boolean(true)]
		);
		assert_eq!(values(&library, notes, &EXECUTABLE), []);
		let link = at_path(&library, started.tree, "latest.md");
		assert_eq!(types(&library, link), [SYMBOLIC_LINK]);
		assert_eq!(values(&library, link, &LINK_TARGET), [text("notes.md")]);
		assert_eq!(values(&library, link, &CONTENT), []);
	}
	// A change for each part of the first commit, as the space has the types already, and
	// one that records the tree's folder on this device, which is no part of it.
	let changes_after = library.status().expect("status").spaces[0].changes;
	assert_eq!(
		changes_after - changes_before,
		started.committed.parts + 1,
		"{changes_before} → {changes_after}"
	);
	let status = library.tree_status(personal, started.tree).expect("status");
	nibkeep_testkit::assert_empty!(status.changes);
}

#[test]
fn a_tree_is_never_inside_another_and_never_starts_in_a_git_repository() {
	let mut rng = TestRng::new("trees apart");
	let (mut library, _, root) = with_tree("trees apart", &[("a.txt", "a")], &mut rng);
	let personal = library.personal_space();
	let inside = root.join("inside");
	fs::create_dir(&inside).expect("makes a folder");
	assert!(matches!(
		library.start_tree(personal, &inside, None, now() + 2_000, &mut rng),
		Err(NodeError::TreeInTree(_))
	));
	assert!(matches!(
		library.would_start_tree(personal, root.parent().expect("a parent")),
		Err(NodeError::TreeInTree(_))
	));
	let repository = folder("trees apart, repository");
	fs::create_dir(repository.join(".git")).expect("makes a repository");
	assert!(matches!(
		library.would_start_tree(personal, &repository),
		Err(NodeError::GitRepositoryHere(_))
	));
}

#[test]
fn a_commit_records_new_files_versions_moves_and_deletions_in_a_part_each() {
	let mut rng = TestRng::new("tree commit");
	let files = [
		("a.txt", "Alpha\n"),
		("b.txt", "Beta\n"),
		("c.txt", "Gamma\n"),
		("docs/d.txt", "Delta\n"),
	];
	let (mut library, tree, root) = with_tree("tree commit", &files, &mut rng);
	let personal = library.personal_space();
	let a = at_path(&library, tree, "a.txt");
	let b = at_path(&library, tree, "b.txt");
	let d = at_path(&library, tree, "docs/d.txt");

	write(&root, "a.txt", "Alpha, again\n");
	fs::remove_file(root.join("c.txt")).expect("deletes");
	fs::rename(root.join("b.txt"), root.join("docs/b.txt")).expect("moves");
	write(&root, "e.txt", "Epsilon\n");
	let status = library.tree_status(personal, tree).expect("status");
	assert_eq!(
		changes(&status.changes),
		[
			("a.txt".to_owned(), Change::Edited),
			("c.txt".to_owned(), Change::Deleted),
			(
				"docs/b.txt".to_owned(),
				Change::Moved {
					from: "b.txt".to_owned()
				}
			),
			("e.txt".to_owned(), Change::New),
		]
	);
	let changes_before = library.status().expect("status").spaces[0].changes;
	let committed = library
		.commit_tree(
			personal,
			tree,
			&Commit::new(Some("Edit, move, delete")),
			now() + 2_000,
			&mut rng,
		)
		.expect("commits");
	assert_eq!(committed.changes, status.changes);
	assert_eq!(committed.parts, 5);
	assert_eq!(
		library.status().expect("status").spaces[0].changes,
		changes_before + 5,
		"a part for each, after one with the revision"
	);

	// A new version keeps the file; a move keeps it too, with its old path in history.
	assert_eq!(at_path(&library, tree, "a.txt"), a);
	assert_eq!(at_path(&library, tree, "docs/b.txt"), b);
	assert_eq!(at_path(&library, tree, "docs/d.txt"), d);
	let history = library
		.history(personal, b, Some(&TREE_PATH))
		.expect("reads")
		.expect("a history");
	let paths: Vec<&Value> = history.fields[0]
		.values
		.iter()
		.map(|past| &past.value)
		.collect();
	assert_eq!(paths, [&text("b.txt"), &text("docs/b.txt")]);
	let versions = library
		.history(personal, a, Some(&CONTENT))
		.expect("reads")
		.expect("a history");
	assert_eq!(versions.fields[0].values.len(), 2);
	let entries: Vec<String> = library
		.tree_entries(personal, tree)
		.expect("reads")
		.into_iter()
		.map(|entry| entry.path)
		.collect();
	assert_eq!(entries, ["a.txt", "docs/b.txt", "docs/d.txt", "e.txt"]);
	let deleted = library.deleted(personal).expect("lists");
	assert_eq!(deleted.len(), 1, "c.txt, which can be restored");
	nibkeep_testkit::assert_empty!(library.tree_status(personal, tree).expect("status").changes);
	// Nothing to commit commits nothing.
	let nothing = library
		.commit_tree(personal, tree, &Commit::new(None), now() + 3_000, &mut rng)
		.expect("commits nothing");
	assert_eq!(nothing.revision, None);
	assert_eq!(nothing.parts, 0);
}

#[test]
fn two_files_with_one_content_stay_two_files_and_adding_it_elsewhere_takes_neither() {
	let mut rng = TestRng::new("tree twins");
	let files = [("pkg/__init__.py", ""), ("pkg/sub/__init__.py", "")];
	let (mut library, tree, _) = with_tree("tree twins", &files, &mut rng);
	let personal = library.personal_space();
	let first = at_path(&library, tree, "pkg/__init__.py");
	let second = at_path(&library, tree, "pkg/sub/__init__.py");
	assert_ne!(first, second);
	assert_eq!(
		values(&library, first, &CONTENT),
		values(&library, second, &CONTENT)
	);
	let elsewhere = folder("tree twins, elsewhere");
	let empty = write(&elsewhere, "empty.txt", "");
	let added = library
		.add_file(personal, &empty, now() + 2_000, &mut rng)
		.expect("adds");
	assert!(!added.known, "a new file, not one of the tree's");
	assert_ne!(added.file, first);
	assert_ne!(added.file, second);
}

#[test]
fn files_outside_trees_never_reach_into_them() {
	let mut rng = TestRng::new("tree apart from files");
	let (mut library, tree, root) = with_tree(
		"tree apart from files",
		&[("notes.md", "# Notes\n")],
		&mut rng,
	);
	let personal = library.personal_space();
	// nib file add leaves the folders of trees to their commits.
	assert!(matches!(
		library.add_file(personal, &root.join("notes.md"), now() + 2_000, &mut rng),
		Err(NodeError::InTree(_))
	));
	// And no checkout writes a tree's file, which the tree's own commands write.
	let notes = at_path(&library, tree, "notes.md");
	let out = folder("tree apart from files, out");
	assert!(matches!(
		library.checkout(personal, notes, &out, now() + 3_000, &mut rng),
		Err(NodeError::TreeFile(_))
	));
}

#[test]
fn every_version_is_sealed_into_the_store() {
	let mut rng = TestRng::new("tree store");
	let (mut library, tree, root) = with_tree("tree store", &[("a.txt", "First\n")], &mut rng);
	let personal = library.personal_space();
	let a = at_path(&library, tree, "a.txt");
	let Value::Blob(first) = values(&library, a, &CONTENT)[0] else {
		panic!("a blob");
	};
	write(&root, "a.txt", "Second\n");
	library
		.commit_tree(personal, tree, &Commit::new(None), now() + 2_000, &mut rng)
		.expect("commits");
	fs::remove_file(root.join("a.txt")).expect("deletes the copy");
	assert_eq!(
		library
			.file_content(personal, &first, 1_000)
			.expect("reads"),
		Some(b"First\n".to_vec())
	);
	let Value::Blob(second) = values(&library, a, &CONTENT)[0] else {
		panic!("a blob");
	};
	assert_eq!(
		library
			.file_content(personal, &second, 1_000)
			.expect("reads"),
		Some(b"Second\n".to_vec())
	);
}

#[test]
fn status_reads_only_what_changed_and_finds_the_same_once_its_index_is_lost() {
	let mut rng = TestRng::new("tree index");
	let files: Vec<(String, String)> = (0..20)
		.map(|n| (format!("file-{n:02}.txt"), format!("Content {n}\n")))
		.collect();
	let borrowed: Vec<(&str, &str)> = files
		.iter()
		.map(|(path, content)| (path.as_str(), content.as_str()))
		.collect();
	let (library, tree, root) = with_tree("tree index", &borrowed, &mut rng);
	let personal = library.personal_space();
	let status = library.tree_status(personal, tree).expect("status");
	assert_eq!(status.read, 0, "nothing changed since the commit");
	nibkeep_testkit::assert_empty!(status.changes);

	write(&root, "file-03.txt", "Content three, longer\n");
	write(&root, "file-07.txt", "Content 7?\n");
	let status = library.tree_status(personal, tree).expect("status");
	assert_eq!(status.read, 2, "only the files whose size or time changed");
	let expected = [
		("file-03.txt".to_owned(), Change::Edited),
		("file-07.txt".to_owned(), Change::Edited),
	];
	assert_eq!(changes(&status.changes), expected);

	// A file whose time alone changed is read, and found unchanged.
	write(&root, "file-11.txt", "Content 11\n");
	let status = library.tree_status(personal, tree).expect("status");
	assert_eq!(status.read, 3);
	assert_eq!(changes(&status.changes), expected);
	let status = library.tree_status(personal, tree).expect("status");
	assert_eq!(status.read, 2, "what was found unchanged is not read again");

	// The index is derived: without it, every file the tree holds is read. Unchanged ones
	// are found unchanged, a new one new, and an edited one edited from a version this
	// replica does not know.
	let mut library = library;
	library
		.commit_tree(personal, tree, &Commit::new(None), now() + 2_000, &mut rng)
		.expect("commits");
	write(&root, "file-05.txt", "Content 5, edited\n");
	write(&root, "new.txt", "New\n");
	let replica = library.close();
	let index = replica.directory().join("trees.sqlite");
	assert!(index.exists(), "the index is in the replica's folder");
	fs::remove_file(&index).expect("loses the index");
	let library = replica.open().expect("opens");
	let status = library.tree_status(personal, tree).expect("status");
	assert_eq!(status.read, 20, "every file the tree holds, and no new one");
	let found = [
		("file-05.txt".to_owned(), Change::Unknown),
		("new.txt".to_owned(), Change::New),
	];
	assert_eq!(changes(&status.changes), found);
	let status = library.tree_status(personal, tree).expect("status");
	assert_eq!(
		status.read, 1,
		"what was found unchanged is in the index again"
	);
	assert_eq!(changes(&status.changes), found);
}

#[test]
fn a_copy_that_matches_no_version_is_committed_only_with_force() {
	let mut rng = TestRng::new("tree unknown");
	let (library, tree, root) = with_tree("tree unknown", &[("a.txt", "First\n")], &mut rng);
	let personal = library.personal_space();
	let replica = library.close();
	fs::remove_file(replica.directory().join("trees.sqlite")).expect("loses the index");
	let mut library = replica.open().expect("opens");
	write(&root, "a.txt", "Edited from what?\n");
	let status = library.tree_status(personal, tree).expect("status");
	assert_eq!(
		changes(&status.changes),
		[("a.txt".to_owned(), Change::Unknown)]
	);
	assert!(matches!(
		library.commit_tree(personal, tree, &Commit::new(None), now() + 2_000, &mut rng),
		Err(NodeError::UnknownVersion(paths)) if paths == ["a.txt"]
	));
	library
		.commit_tree(
			personal,
			tree,
			&Commit::new(None).with_force(),
			now() + 3_000,
			&mut rng,
		)
		.expect("commits with force");
	nibkeep_testkit::assert_empty!(library.tree_status(personal, tree).expect("status").changes);
}

#[test]
fn edits_of_a_file_that_changed_elsewhere_are_committed_only_with_force() {
	let mut rng = TestRng::new("tree stale");
	let (mut library, tree, root) = with_tree("tree stale", &[("a.txt", "First\n")], &mut rng);
	let personal = library.personal_space();
	let a = at_path(&library, tree, "a.txt");
	// Elsewhere, as by another device, the file moves.
	library
		.edit_entity(
			personal,
			a,
			vec![Edit::Set(TREE_PATH, vec![text("b.txt")])],
			&common::today(),
			now() + 2_000,
			&mut rng,
		)
		.expect("moves it");
	let status = library.tree_status(personal, tree).expect("status");
	assert_eq!(status.behind, ["a.txt", "b.txt"]);
	write(&root, "a.txt", "Second\n");
	assert!(matches!(
		library.commit_tree(personal, tree, &Commit::new(None), now() + 3_000, &mut rng),
		Err(NodeError::TreeStale(paths)) if paths == ["a.txt"]
	));
	library
		.commit_tree(
			personal,
			tree,
			&Commit::new(None).with_force(),
			now() + 4_000,
			&mut rng,
		)
		.expect("commits on top");
}

#[test]
fn a_commit_of_some_paths_leaves_the_rest() {
	let mut rng = TestRng::new("tree some paths");
	let files = [("a.txt", "a"), ("docs/b.txt", "b"), ("docs/c.txt", "c")];
	let (mut library, tree, root) = with_tree("tree some paths", &files, &mut rng);
	let personal = library.personal_space();
	write(&root, "a.txt", "aa");
	write(&root, "docs/b.txt", "bb");
	write(&root, "docs/c.txt", "cc");
	let committed = library
		.commit_tree(
			personal,
			tree,
			&Commit::new(None).with_paths(vec!["docs".to_owned()]),
			now() + 2_000,
			&mut rng,
		)
		.expect("commits");
	assert_eq!(
		changes(&committed.changes),
		[
			("docs/b.txt".to_owned(), Change::Edited),
			("docs/c.txt".to_owned(), Change::Edited),
		]
	);
	let status = library.tree_status(personal, tree).expect("status");
	assert_eq!(
		changes(&status.changes),
		[("a.txt".to_owned(), Change::Edited)]
	);
}

#[test]
fn a_commit_takes_a_part_for_each_file_after_one_with_its_revision() {
	let mut rng = TestRng::new("tree parts");
	let (_, mut library) = library("tree parts", &mut rng);
	let root = folder("tree parts, files");
	for n in 0..60 {
		write(&root, &format!("file-{n:02}.txt"), &format!("{n}\n"));
	}
	let personal = library.personal_space();
	let started = library
		.start_tree(personal, &root, None, now() + 1_000, &mut rng)
		.expect("starts");
	for n in 0..60 {
		write(&root, &format!("file-{n:02}.txt"), &format!("{n} again\n"));
	}
	let committed = library
		.commit_tree(
			personal,
			started.tree,
			&Commit::new(Some("Everything")),
			now() + 2_000,
			&mut rng,
		)
		.expect("commits");
	assert_eq!(
		committed.parts, 61,
		"the revision, then a part for each file"
	);
	assert_eq!(committed.changes.len(), 60);
	// One revision for the commit, with its message and how many parts it has.
	let revision = committed.revision.expect("a revision");
	assert_eq!(values(&library, revision, &PARTS), [Value::Integer(61)]);
	assert_eq!(values(&library, revision, &MESSAGE), [text("Everything")]);
	// Each part names it on the file it records, and history keeps the commit before,
	// which named its own on the tree too.
	let started_as = started.committed.revision.expect("a revision");
	assert_eq!(
		values(&library, started_as, &PARTS),
		[Value::Integer(62)],
		"the revision, the tree, and a part for each file"
	);
	assert_eq!(
		values(&library, started.tree, &COMMITTED_IN),
		[Value::Reference(started_as)]
	);
	for n in 0..60 {
		let file = at_path(&library, started.tree, &format!("file-{n:02}.txt"));
		assert_eq!(
			values(&library, file, &COMMITTED_IN),
			[Value::Reference(revision)]
		);
		let history = library
			.history(personal, file, Some(&COMMITTED_IN))
			.expect("reads")
			.expect("a history");
		let commits: Vec<&Value> = history.fields[0]
			.values
			.iter()
			.map(|past| &past.value)
			.collect();
		assert_eq!(
			commits,
			[&Value::Reference(started_as), &Value::Reference(revision)]
		);
	}
	let status = library.tree_status(personal, started.tree).expect("status");
	nibkeep_testkit::assert_empty!(status.changes);
	nibkeep_testkit::assert_empty!(status.cut_short);
}

#[test]
fn moving_in_a_tree_moves_the_copy_and_the_file_at_once() {
	let mut rng = TestRng::new("tree move");
	let files = [("a.txt", "a"), ("docs/b.txt", "b"), ("docs/c.txt", "c")];
	let (mut library, tree, root) = with_tree("tree move", &files, &mut rng);
	let personal = library.personal_space();
	let a = at_path(&library, tree, "a.txt");
	let b = at_path(&library, tree, "docs/b.txt");
	let moved = library
		.move_in_tree(
			personal,
			tree,
			("a.txt", "notes/a.md"),
			now() + 2_000,
			&mut rng,
		)
		.expect("moves");
	assert_eq!(
		changes(&moved.changes),
		[(
			"notes/a.md".to_owned(),
			Change::Moved {
				from: "a.txt".to_owned()
			}
		)]
	);
	assert!(!root.join("a.txt").exists());
	assert_eq!(
		fs::read_to_string(root.join("notes/a.md")).expect("reads"),
		"a"
	);
	assert_eq!(at_path(&library, tree, "notes/a.md"), a);
	assert_eq!(
		values(&library, a, &fields::NAME),
		[Value::Name(nibkeep_model::Name::new("a.md"))]
	);
	// A folder moves with every file in it.
	library
		.move_in_tree(personal, tree, ("docs", "papers"), now() + 3_000, &mut rng)
		.expect("moves a folder");
	assert_eq!(at_path(&library, tree, "papers/b.txt"), b);
	assert!(root.join("papers/c.txt").exists());
	assert!(!root.join("docs").exists());
	nibkeep_testkit::assert_empty!(library.tree_status(personal, tree).expect("status").changes);
	// Nothing is moved onto what is there, nor out of the tree, nor into .nib or a .git.
	assert!(matches!(
		library.move_in_tree(
			personal,
			tree,
			("papers/b.txt", "papers/c.txt"),
			now() + 4_000,
			&mut rng
		),
		Err(NodeError::PlaceTaken(_))
	));
	for unsafe_path in [
		"../b.txt",
		".nib/b.txt",
		".GIT/b.txt",
		"x/.git/b.txt",
		"/b.txt",
	] {
		assert!(
			matches!(
				library.move_in_tree(
					personal,
					tree,
					("papers/b.txt", unsafe_path),
					now() + 5_000,
					&mut rng
				),
				Err(NodeError::UnsafePath(_))
			),
			"{unsafe_path}"
		);
	}
	assert!(matches!(
		library.move_in_tree(
			personal,
			tree,
			("missing.txt", "here.txt"),
			now() + 6_000,
			&mut rng
		),
		Err(NodeError::NotInTree(_))
	));
}

#[test]
fn removing_deletes_unedited_copies_for_the_next_commit_to_record() {
	let mut rng = TestRng::new("tree remove");
	let files = [
		("a.txt", "a"),
		("docs/b.txt", "b"),
		("docs/deep/c.txt", "c"),
		("keep.txt", "k"),
		("stays.txt", "s"),
	];
	let (mut library, tree, root) = with_tree("tree remove", &files, &mut rng);
	let personal = library.personal_space();
	let changes_before = library.status().expect("status").spaces[0].changes;
	let removed = library
		.remove_in_tree(personal, tree, &["a.txt".to_owned()], false)
		.expect("removes");
	assert_eq!(removed.removed, ["a.txt"]);
	assert!(!root.join("a.txt").exists());
	// A folder goes with the tree's files in it, and the folders they leave empty; a file
	// the tree does not hold stays, and so does its folder.
	write(&root, "docs/new.txt", "not the tree's");
	let removed = library
		.remove_in_tree(personal, tree, &["docs".to_owned()], false)
		.expect("removes");
	assert_eq!(removed.removed, ["docs/b.txt", "docs/deep/c.txt"]);
	assert!(!root.join("docs/deep").exists());
	assert!(root.join("docs/new.txt").exists());
	assert_eq!(
		library.status().expect("status").spaces[0].changes,
		changes_before,
		"nothing recorded"
	);
	let status = library.tree_status(personal, tree).expect("status");
	assert_eq!(
		changes(&status.changes),
		[
			("a.txt".to_owned(), Change::Deleted),
			("docs/b.txt".to_owned(), Change::Deleted),
			("docs/deep/c.txt".to_owned(), Change::Deleted),
			("docs/new.txt".to_owned(), Change::New),
		]
	);
	// An edited copy stays, unless forced, as Git's rm keeps one, and so does every other
	// copy named with it.
	write(&root, "keep.txt", "edited");
	assert!(matches!(
		library.remove_in_tree(
			personal,
			tree,
			&["stays.txt".to_owned(), "keep.txt".to_owned()],
			false
		),
		Err(NodeError::EditedCopy(_))
	));
	assert!(root.join("keep.txt").exists());
	assert!(root.join("stays.txt").exists());
	let removed = library
		.remove_in_tree(personal, tree, &["keep.txt".to_owned()], true)
		.expect("removes");
	assert_eq!(removed.removed, ["keep.txt"]);
	assert!(!root.join("keep.txt").exists());
	// What the tree does not hold is not removed.
	assert!(matches!(
		library.remove_in_tree(personal, tree, &["docs/new.txt".to_owned()], false),
		Err(NodeError::NotInTree(_))
	));
	assert!(matches!(
		library.remove_in_tree(personal, tree, &["../a.txt".to_owned()], false),
		Err(NodeError::UnsafePath(_))
	));
	// The next commit records the deletions.
	let committed = library
		.commit_tree(
			personal,
			tree,
			&Commit::new(Some("Removed")).with_paths(vec![
				"a.txt".to_owned(),
				"docs/b.txt".to_owned(),
				"docs/deep/c.txt".to_owned(),
				"keep.txt".to_owned(),
			]),
			now() + 2_000,
			&mut rng,
		)
		.expect("commits");
	assert_eq!(committed.changes.len(), 4);
	assert!(
		committed
			.changes
			.iter()
			.all(|change| change.change == Change::Deleted)
	);
}

#[cfg(unix)]
#[test]
fn a_move_never_goes_through_a_link() {
	let mut rng = TestRng::new("tree move through link");
	let (mut library, tree, root) =
		with_tree("tree move through link", &[("a.txt", "a")], &mut rng);
	let personal = library.personal_space();
	let outside = folder("tree move through link, outside");
	std::os::unix::fs::symlink(&outside, root.join("out")).expect("links");
	assert!(matches!(
		library.move_in_tree(
			personal,
			tree,
			("a.txt", "out/a.txt"),
			now() + 2_000,
			&mut rng
		),
		Err(NodeError::UnsafePath(_))
	));
	assert!(root.join("a.txt").exists());
	assert!(fs::read_dir(&outside).expect("reads").next().is_none());
}

#[test]
fn commits_leave_out_the_library_git_repositories_and_what_nibignore_names() {
	let mut rng = TestRng::new("tree left out");
	let (_, mut library) = library("tree left out", &mut rng);
	let root = folder("tree left out, files");
	write(&root, "notes.md", "# Notes\n");
	write(&root, ".nib/keys.cbor", "keys");
	write(&root, "project/.git/HEAD", "ref: refs/heads/main\n");
	write(&root, "project/src/main.rs", "fn main() {}\n");
	write(&root, "work/.git", "gitdir: ../somewhere\n");
	write(&root, "work/file.txt", "a worktree's\n");
	write(&root, ".nibignore", "target/\n*.log\n");
	write(&root, "target/debug/app", "binary");
	write(&root, "run.log", "log");
	write(&root, "docs/.nibignore", "!keep.log\n");
	write(&root, "docs/keep.log", "kept");
	let personal = library.personal_space();
	let started = library
		.start_tree(personal, &root, None, now() + 1_000, &mut rng)
		.expect("starts");
	let paths: Vec<String> = started
		.committed
		.changes
		.iter()
		.map(|change| change.path.clone())
		.collect();
	assert_eq!(
		paths,
		[".nibignore", "docs/.nibignore", "docs/keep.log", "notes.md"]
	);
}

#[test]
fn nibignore_leaves_out_what_git_would_and_nothing_else() {
	let Ok(version) = std::process::Command::new("git").arg("--version").output() else {
		eprintln!("Git is not installed: skipped");
		return;
	};
	assert!(version.status.success());
	let rules = [
		(
			"",
			"*.log\n!keep.log\nbuild/\n/root-only.txt\ndocs/**/*.tmp\n**/cache\na?c.txt\n[ab]*.bak\n\\#hash.txt\nspaced\\ \n",
		),
		("sub/", "*.md\n!README.md\n/local.txt\n"),
	];
	let files = [
		"keep.log",
		"other.log",
		"build/out.o",
		"src/build/out.o",
		"root-only.txt",
		"src/root-only.txt",
		"docs/a/b/c.tmp",
		"docs/c.tmp",
		"other/c.tmp",
		"x/cache/file",
		"cache/file",
		"abc.txt",
		"axc.txt",
		"abbc.txt",
		"a1.bak",
		"b2.bak",
		"c3.bak",
		"#hash.txt",
		"spaced ",
		"spaced",
		"sub/notes.md",
		"sub/README.md",
		"sub/local.txt",
		"sub/deeper/local.txt",
		"sub/deeper/other.md",
		".hidden/file",
		"plain.txt",
	];
	let tree_root = folder("nibignore like git, tree");
	let git_root = folder("nibignore like git, git");
	for (folder, rule) in rules {
		write(&tree_root, &format!("{folder}.nibignore"), rule);
		write(&git_root, &format!("{folder}.gitignore"), rule);
	}
	for path in files {
		write(&tree_root, path, "x");
		write(&git_root, path, "x");
	}
	let empty = git_root
		.parent()
		.expect("a parent")
		.join("nibignore like git, empty config");
	fs::write(&empty, "").expect("writes");
	let git = |arguments: &[&str]| {
		let output = std::process::Command::new("git")
			.args(arguments)
			.current_dir(&git_root)
			.env("GIT_CONFIG_GLOBAL", &empty)
			.env("GIT_CONFIG_NOSYSTEM", "1")
			.output()
			.expect("runs Git");
		assert!(output.status.success(), "{output:?}");
		String::from_utf8(output.stdout).expect("UTF-8")
	};
	git(&["init", "--quiet"]);
	let listed = git(&["ls-files", "--others", "--exclude-standard", "-z"]);
	let by_git: BTreeSet<String> = listed
		.split('\0')
		.filter(|path| !path.is_empty())
		.map(|path| path.replace(".gitignore", ".nibignore"))
		.collect();

	let mut rng = TestRng::new("nibignore like git");
	let (_, mut library) = library("nibignore like git", &mut rng);
	let personal = library.personal_space();
	let started = library
		.start_tree(personal, &tree_root, None, now() + 1_000, &mut rng)
		.expect("starts");
	let by_tree: BTreeSet<String> = started
		.committed
		.changes
		.iter()
		.map(|change| change.path.clone())
		.collect();
	assert_eq!(by_tree, by_git);
}

#[test]
fn tree_paths_never_leave_the_tree_nor_name_the_library_or_git() {
	use nibkeep_node::is_safe_tree_path;
	for safe in [
		"a.txt",
		"docs/a.txt",
		".github/workflows/ci.yml",
		".gitignore",
		".nibignore",
		"git/a",
		"nib",
		"a..b",
	] {
		assert!(is_safe_tree_path(safe), "{safe}");
	}
	for unsafe_path in [
		"",
		"/a",
		"a/",
		"a//b",
		"./a",
		"a/./b",
		"..",
		"../a",
		"a/../../b",
		"a\\b",
		"a\0b",
		".git",
		".git/config",
		"x/.git/hooks/post-checkout",
		".GIT/config",
		".Git/config",
		".git./config",
		".git /config",
		".git::$INDEX_ALLOCATION/config",
		"git~1/config",
		".g\u{200c}it/config",
		".nib",
		".nib/keys.cbor",
		".NIB/keys.cbor",
		"nib~1/keys.cbor",
	] {
		assert!(!is_safe_tree_path(unsafe_path), "{unsafe_path:?}");
	}
	// Only the composed form: what a Mac writes decomposed is recorded composed.
	assert!(is_safe_tree_path("caf\u{e9}.txt"));
	assert!(!is_safe_tree_path("cafe\u{301}.txt"));
}

#[test]
fn purging_one_file_of_a_commit_leaves_the_others_as_they_were_written() {
	let mut rng = TestRng::new("tree purge one");
	let files = [
		("a.txt", "Alpha\n"),
		("b.txt", "Leaked\n"),
		("c.txt", "Gamma\n"),
	];
	let (mut library, tree, _root) = with_tree("tree purge one", &files, &mut rng);
	let personal = library.personal_space();
	let (a, b, c) = (
		at_path(&library, tree, "a.txt"),
		at_path(&library, tree, "b.txt"),
		at_path(&library, tree, "c.txt"),
	);
	let history = |library: &Library, entity| {
		library
			.history(personal, entity, Some(&CONTENT))
			.expect("reads history")
	};
	let (a_before, c_before) = (history(&library, a), history(&library, c));
	let leaked = values(&library, b, &CONTENT)[0].clone();
	library
		.purge(
			personal,
			b,
			&CONTENT,
			Some(&leaked),
			now() + 2_000,
			&mut rng,
		)
		.expect("purges");
	// Each file was a part of its own, so the purge stated none of the others again: their
	// history is as it was, and so is a view of a moment before the purge.
	assert_eq!(history(&library, a), a_before);
	assert_eq!(history(&library, c), c_before);
	for entity in [a, c] {
		let past = history(&library, entity).expect("a history");
		let restated = past
			.fields
			.iter()
			.flat_map(|field| &field.values)
			.any(|value| value.restated.is_some());
		assert!(!restated, "{past:?}");
	}
	let then = folder("tree purge one, then");
	fs::remove_dir(&then).expect("removes");
	library
		.write_tree(
			personal,
			tree,
			&then,
			TreePoint::AsOf(now() + 1_500),
			now() + 3_000,
			&mut rng,
		)
		.expect("writes");
	assert_eq!(
		fs::read_to_string(then.join("a.txt")).expect("reads"),
		"Alpha\n"
	);
	assert_eq!(
		fs::read_to_string(then.join("c.txt")).expect("reads"),
		"Gamma\n"
	);
	// The commit is still one, with its message.
	let log = library
		.tree_log(personal, tree, None)
		.expect("reads the log");
	let start: Vec<_> = log
		.iter()
		.filter(|entry| entry.message.as_deref() == Some("Start"))
		.collect();
	assert_eq!(start.len(), 1, "{log:?}");
	assert_eq!(start[0].part, None);
	// The purge stated again the statement that counts b.txt's part, so the commit is whole
	// still, and a view of its revision has the other files.
	nibkeep_testkit::assert_empty!(
		library
			.tree_status(personal, tree)
			.expect("status")
			.cut_short
	);
	let at_revision = folder("tree purge one, at its revision");
	fs::remove_dir(&at_revision).expect("removes");
	let revision = start[0].revision.expect("a revision");
	library
		.write_tree(
			personal,
			tree,
			&at_revision,
			TreePoint::Revision(revision),
			now() + 3_000,
			&mut rng,
		)
		.expect("writes");
	for (path, content) in [("a.txt", "Alpha\n"), ("c.txt", "Gamma\n")] {
		assert_eq!(
			fs::read_to_string(at_revision.join(path)).expect("reads"),
			content
		);
	}
}

#[test]
fn a_purge_deletes_the_copies_not_edited_and_takes_the_content_out_of_the_index() {
	let mut rng = TestRng::new("tree purge");
	let files = [
		("a.txt", "Leaked token\n"),
		("b.txt", "Leaked token\n"),
		("c.txt", "Kept\n"),
	];
	let (mut library, tree, root) = with_tree("tree purge", &files, &mut rng);
	let personal = library.personal_space();
	let a = at_path(&library, tree, "a.txt");
	write(&root, "b.txt", "Leaked token, and an edit\n");
	let leaked = values(&library, a, &CONTENT)[0].clone();
	let purge = library
		.purgeable(personal, a, &CONTENT, Some(&leaked))
		.expect("can purge");
	let copies: Vec<(PathBuf, bool)> = purge
		.tree_copies
		.iter()
		.map(|copy| (copy.path.clone(), copy.edited))
		.collect();
	assert_eq!(
		copies,
		[(root.join("a.txt"), false), (root.join("b.txt"), true)]
	);
	let purge = library
		.purge(
			personal,
			a,
			&CONTENT,
			Some(&leaked),
			now() + 2_000,
			&mut rng,
		)
		.expect("purges");
	let deleted: Vec<bool> = purge.tree_copies.iter().map(|copy| copy.deleted).collect();
	assert_eq!(deleted, [true, false]);
	assert!(!root.join("a.txt").exists(), "the copy as it was committed");
	assert_eq!(
		fs::read_to_string(root.join("b.txt")).expect("reads"),
		"Leaked token, and an edit\n",
		"an edited copy stays"
	);
	// The index no longer says what a.txt held, so its copy is not taken as deleted. The
	// edited copy was edited from content that is gone, so the person commits it knowingly.
	let status = library.tree_status(personal, tree).expect("status");
	assert_eq!(
		changes(&status.changes),
		[("b.txt".to_owned(), Change::Unknown)]
	);
	nibkeep_testkit::assert_empty!(status.behind);
	assert!(matches!(
		library.commit_tree(personal, tree, &Commit::new(None), now() + 3_000, &mut rng),
		Err(NodeError::UnknownVersion(_))
	));
	library
		.commit_tree(
			personal,
			tree,
			&Commit::new(None).with_force(),
			now() + 4_000,
			&mut rng,
		)
		.expect("commits the edit");
	nibkeep_testkit::assert_empty!(library.tree_status(personal, tree).expect("status").changes);
	// A copy of the purged content, made anywhere in the tree, is never committed.
	write(&root, "again.txt", "Leaked token\n");
	let status = library.tree_status(personal, tree).expect("status");
	nibkeep_testkit::assert_empty!(status.changes);
	let committed = library
		.commit_tree(personal, tree, &Commit::new(None), now() + 5_000, &mut rng)
		.expect("commits nothing");
	nibkeep_testkit::assert_empty!(committed.changes);
}

#[test]
fn a_copy_with_none_of_its_files_is_not_taken_for_files_all_deleted() {
	let mut rng = TestRng::new("tree emptied");
	let files = [("a.txt", "a"), ("docs/b.txt", "b")];
	let (mut library, tree, root) = with_tree("tree emptied", &files, &mut rng);
	let personal = library.personal_space();
	// A folder that is gone, as when it moved, is no folder whose files were deleted.
	let moved = root.with_file_name("tree emptied, moved");
	let _ = fs::remove_dir_all(&moved);
	fs::rename(&root, &moved).expect("moves the folder");
	assert!(matches!(
		library.tree_status(personal, tree),
		Err(NodeError::TreeFolderGone(_))
	));
	assert!(matches!(
		library.commit_tree(personal, tree, &Commit::new(None), now() + 2_000, &mut rng),
		Err(NodeError::TreeFolderGone(_))
	));
	// An empty folder in its place, as a drive that is not there leaves, is not emptied for
	// good unless the person says so.
	fs::create_dir(&root).expect("makes an empty folder");
	assert!(matches!(
		library.commit_tree(personal, tree, &Commit::new(None), now() + 3_000, &mut rng),
		Err(NodeError::TreeEmptied(_))
	));
	let committed = library
		.commit_tree(
			personal,
			tree,
			&Commit::new(None).with_force(),
			now() + 4_000,
			&mut rng,
		)
		.expect("deletes them all");
	assert_eq!(committed.changes.len(), 2);
	nibkeep_testkit::assert_empty!(library.tree_entries(personal, tree).expect("reads"));
}

#[test]
fn a_file_that_nibignore_names_after_it_was_committed_stays_in_the_tree() {
	let mut rng = TestRng::new("tree ignored later");
	let files = [("a.log", "first"), ("b.txt", "b")];
	let (library, tree, root) = with_tree("tree ignored later", &files, &mut rng);
	let personal = library.personal_space();
	write(&root, ".nibignore", "*.log\n");
	write(&root, "a.log", "second, longer");
	write(&root, "c.log", "never taken");
	let status = library.tree_status(personal, tree).expect("status");
	assert_eq!(
		changes(&status.changes),
		[
			(".nibignore".to_owned(), Change::New),
			("a.log".to_owned(), Change::Edited),
		]
	);
	fs::remove_file(root.join("a.log")).expect("deletes");
	let status = library.tree_status(personal, tree).expect("status");
	assert_eq!(
		changes(&status.changes),
		[
			(".nibignore".to_owned(), Change::New),
			("a.log".to_owned(), Change::Deleted),
		]
	);
}

/// Sets a folder's permissions, on Unix.
#[cfg(unix)]
fn set_mode(path: &Path, mode: u32) {
	use std::os::unix::fs::PermissionsExt;
	fs::set_permissions(path, fs::Permissions::from_mode(mode)).expect("sets permissions");
}

#[cfg(unix)]
#[test]
fn a_purged_copy_that_cannot_be_deleted_yet_is_never_committed_again() {
	let mut rng = TestRng::new("tree purge held");
	let files = [("locked/token.txt", "Leaked token\n"), ("b.txt", "b")];
	let (mut library, tree, root) = with_tree("tree purge held", &files, &mut rng);
	let personal = library.personal_space();
	let token = at_path(&library, tree, "locked/token.txt");
	let leaked = values(&library, token, &CONTENT)[0].clone();
	// Its folder lets nothing be deleted from it.
	set_mode(&root.join("locked"), 0o555);
	let purge = library
		.purge(
			personal,
			token,
			&CONTENT,
			Some(&leaked),
			now() + 2_000,
			&mut rng,
		)
		.expect("purges");
	assert_eq!(purge.tree_copies.len(), 1);
	assert!(!purge.tree_copies[0].deleted);
	assert!(root.join("locked/token.txt").exists());
	let status = library.tree_status(personal, tree).expect("status");
	nibkeep_testkit::assert_empty!(status.changes);
	assert_eq!(status.skipped.len(), 1, "{:?}", status.skipped);
	let nothing = library
		.commit_tree(personal, tree, &Commit::new(None), now() + 3_000, &mut rng)
		.expect("commits nothing");
	nibkeep_testkit::assert_empty!(nothing.changes);
	// Once it can be, the next status deletes it.
	set_mode(&root.join("locked"), 0o755);
	let status = library.tree_status(personal, tree).expect("status");
	assert!(!root.join("locked/token.txt").exists());
	nibkeep_testkit::assert_empty!(status.changes);
	nibkeep_testkit::assert_empty!(status.skipped);
}

#[cfg(target_os = "linux")]
#[test]
fn a_decomposed_name_on_the_disk_is_its_composed_path_in_the_tree() {
	let mut rng = TestRng::new("tree decomposed");
	let files = [("cafe\u{301}/menu.txt", "Coffee\n"), ("b.txt", "b")];
	let (mut library, tree, root) = with_tree("tree decomposed", &files, &mut rng);
	let personal = library.personal_space();
	let menu = at_path(&library, tree, "caf\u{e9}/menu.txt");
	// A `.nibignore` that names it later leaves it in the tree, found by its composed path.
	write(&root, ".nibignore", "caf\u{e9}/\n");
	let status = library.tree_status(personal, tree).expect("status");
	assert_eq!(
		changes(&status.changes),
		[(".nibignore".to_owned(), Change::New)]
	);
	library
		.move_in_tree(
			personal,
			tree,
			("caf\u{e9}/menu.txt", "menu.txt"),
			now() + 2_000,
			&mut rng,
		)
		.expect("moves it");
	assert_eq!(at_path(&library, tree, "menu.txt"), menu);
	assert!(root.join("menu.txt").exists());
}

#[cfg(unix)]
#[test]
fn a_folder_that_cannot_be_read_is_not_taken_for_its_files_deleted() {
	let mut rng = TestRng::new("tree unreadable");
	let files = [("docs/a.txt", "a"), ("docs/b.txt", "b"), ("c.txt", "c")];
	let (mut library, tree, root) = with_tree("tree unreadable", &files, &mut rng);
	let personal = library.personal_space();
	set_mode(&root.join("docs"), 0o000);
	let status = library.tree_status(personal, tree);
	let committed =
		library.commit_tree(personal, tree, &Commit::new(None), now() + 2_000, &mut rng);
	set_mode(&root.join("docs"), 0o755);
	let status = status.expect("status");
	nibkeep_testkit::assert_empty!(status.changes);
	let skipped: Vec<&str> = status
		.skipped
		.iter()
		.map(|(path, _)| path.as_str())
		.collect();
	assert_eq!(skipped, ["docs", "docs/a.txt", "docs/b.txt"]);
	nibkeep_testkit::assert_empty!(committed.expect("commits").changes);
	assert_eq!(
		library.tree_entries(personal, tree).expect("reads").len(),
		3
	);
}

#[test]
fn a_copy_emptied_with_a_purged_file_in_the_tree_is_still_not_taken_for_files_all_deleted() {
	let mut rng = TestRng::new("tree emptied after purge");
	let files = [("a.txt", "Leaked\n"), ("b.txt", "b"), ("c.txt", "c")];
	let (mut library, tree, root) = with_tree("tree emptied after purge", &files, &mut rng);
	let personal = library.personal_space();
	let a = at_path(&library, tree, "a.txt");
	let leaked = values(&library, a, &CONTENT)[0].clone();
	library
		.purge(
			personal,
			a,
			&CONTENT,
			Some(&leaked),
			now() + 2_000,
			&mut rng,
		)
		.expect("purges");
	fs::remove_dir_all(&root).expect("empties it");
	fs::create_dir(&root).expect("leaves an empty folder");
	assert!(matches!(
		library.commit_tree(personal, tree, &Commit::new(None), now() + 3_000, &mut rng),
		Err(NodeError::TreeEmptied(_))
	));
}

#[test]
fn no_tree_starts_in_the_library_s_own_folder_or_in_git_s() {
	let mut rng = TestRng::new("tree reserved folders");
	let (directory, library) = library("tree reserved folders", &mut rng);
	let personal = library.personal_space();
	let inside = directory.join("inside");
	fs::create_dir_all(&inside).expect("makes a folder");
	assert!(matches!(
		library.would_start_tree(personal, &inside),
		Err(NodeError::NotATreeFolder(_))
	));
	let hooks = folder("tree reserved folders, repository").join(".git/hooks");
	fs::create_dir_all(&hooks).expect("makes a repository");
	assert!(matches!(
		library.would_start_tree(personal, &hooks),
		Err(NodeError::NotATreeFolder(_))
	));
	// A tree whose folder holds the library leaves the library out, whatever its name.
	let holder = directory
		.parent()
		.expect("a parent")
		.join("tree reserved folders, holder");
	let _ = fs::remove_dir_all(&holder);
	fs::create_dir_all(&holder).expect("makes a folder");
	write(&holder, "notes.md", "# Notes\n");
	let replica = library.close();
	let moved = holder.join("data");
	fs::rename(&directory, &moved).expect("moves the library");
	drop(replica);
	let mut library =
		Library::open(&moved, &nibkeep_node::Unlocker::Passphrase("correct horse")).expect("opens");
	let started = library
		.start_tree(personal, &holder, None, now() + 1_000, &mut rng)
		.expect("starts");
	let paths: Vec<&str> = started
		.committed
		.changes
		.iter()
		.map(|change| change.path.as_str())
		.collect();
	assert_eq!(paths, ["notes.md"]);
}

#[cfg(unix)]
#[test]
fn a_link_named_on_the_command_line_is_the_link() {
	let mut rng = TestRng::new("tree link named");
	let files = [("releases/v2/app", "app")];
	let (mut library, tree, root) = with_tree("tree link named", &files, &mut rng);
	let personal = library.personal_space();
	std::os::unix::fs::symlink("releases/v2", root.join("current")).expect("links");
	library
		.commit_tree(personal, tree, &Commit::new(None), now() + 2_000, &mut rng)
		.expect("commits the link");
	assert_eq!(
		nibkeep_node::tree_path_at(&root, &root.join("current")).expect("a path"),
		"current"
	);
	library
		.move_in_tree(
			personal,
			tree,
			("current", "latest"),
			now() + 3_000,
			&mut rng,
		)
		.expect("moves the link");
	assert!(root.join("releases/v2/app").exists(), "its target stays");
	assert!(
		fs::symlink_metadata(root.join("latest"))
			.expect("the link")
			.file_type()
			.is_symlink()
	);
}

#[test]
fn a_move_never_lands_on_a_path_that_the_tree_holds() {
	let mut rng = TestRng::new("tree move onto held");
	let files = [("a.txt", "a"), ("b.txt", "b"), ("docs/c.txt", "c")];
	let (mut library, tree, root) = with_tree("tree move onto held", &files, &mut rng);
	let personal = library.personal_space();
	fs::remove_file(root.join("b.txt")).expect("deletes the copy, not yet committed");
	for onto in ["b.txt", "docs", "b.txt/x"] {
		assert!(
			matches!(
				library.move_in_tree(personal, tree, ("a.txt", onto), now() + 2_000, &mut rng),
				Err(NodeError::PlaceTaken(_))
			),
			"{onto}"
		);
	}
	assert!(root.join("a.txt").exists());
}

#[test]
fn a_folder_replaced_by_a_file_deletes_what_was_in_it() {
	let mut rng = TestRng::new("tree folder to file");
	let files = [("docs/a.txt", "a"), ("b.txt", "b")];
	let (library, tree, root) = with_tree("tree folder to file", &files, &mut rng);
	let personal = library.personal_space();
	fs::remove_dir_all(root.join("docs")).expect("deletes the folder");
	write(&root, "docs", "now a file");
	let status = library.tree_status(personal, tree).expect("status");
	assert_eq!(
		changes(&status.changes),
		[
			("docs".to_owned(), Change::New),
			("docs/a.txt".to_owned(), Change::Deleted),
		]
	);
}

#[test]
fn a_commit_cut_short_is_listed_until_the_next_commit() {
	let mut rng = TestRng::new("tree cut short");
	let (mut library, tree, root) = with_tree("tree cut short", &[("a.txt", "a")], &mut rng);
	let personal = library.personal_space();
	// The first part of three, as a crash after it leaves it.
	let first = library
		.create_entity(
			personal,
			&[REVISION],
			vec![
				(IN_TREE, Value::Reference(tree)),
				(PARTS, Value::Integer(3)),
			],
			now() + 2_000,
			&mut rng,
		)
		.expect("records a first part");
	let status = library.tree_status(personal, tree).expect("status");
	assert_eq!(status.cut_short.len(), 1);
	assert_eq!(status.cut_short[0].revision, first);
	assert_eq!(
		(status.cut_short[0].made, status.cut_short[0].parts),
		(1, 3)
	);
	write(&root, "a.txt", "aa");
	library
		.commit_tree(personal, tree, &Commit::new(None), now() + 3_000, &mut rng)
		.expect("commits the rest");
	let status = library.tree_status(personal, tree).expect("status");
	nibkeep_testkit::assert_empty!(status.cut_short);
}

#[test]
fn a_root_counts_only_on_the_replica_that_recorded_it() {
	let mut rng = TestRng::new("tree root elsewhere");
	let (_, mut laptop) = library("tree root elsewhere, laptop", &mut rng);
	let desk_directory =
		nibkeep_testkit::empty_directory(env!("CARGO_TARGET_TMPDIR"), "tree root elsewhere, desk");
	fs::remove_dir(&desk_directory).expect("removes");
	let code = nibkeep_node::PairingCode::generate(&mut rng);
	let (paired, joined) = common::pair_with(&mut laptop, &desk_directory, &code, &code, &mut rng);
	paired.expect("the laptop paired");
	let mut desk = joined.expect("the desk joined").library;
	let personal = laptop.personal_space();
	let laptop_device = laptop.status().expect("status").device.id;
	// The desk, turned against its user, names a folder of the laptop's as a tree's.
	let home = folder("tree root elsewhere, home");
	write(&home, "secret.txt", "keys");
	desk.install_standard(
		personal,
		&[nibkeep_std::TREE, nibkeep_std::TREE_ROOT],
		now() + 1_000,
		&mut rng,
	)
	.expect("installs the types");
	let tree = desk
		.create_entity(
			personal,
			&[nibkeep_std::TREE],
			vec![],
			now() + 2_000,
			&mut rng,
		)
		.expect("makes a tree");
	desk.create_entity(
		personal,
		&[nibkeep_std::TREE_ROOT],
		vec![
			(nibkeep_std::ROOT_OF, Value::Reference(tree)),
			(nibkeep_std::ON_DEVICE, Value::Reference(laptop_device)),
			(nibkeep_std::PATH, Value::Text(home.display().to_string())),
		],
		now() + 3_000,
		&mut rng,
	)
	.expect("records the root");
	let (first, second) = common::sync_in_memory(&mut desk, &mut laptop, now() + 4_000, (1, 2));
	first.expect("the desk synced");
	second.expect("the laptop synced");
	assert!(laptop.tree_at(personal, &home).expect("looks").is_none());
	laptop
		.add_file(personal, &home.join("secret.txt"), now() + 5_000, &mut rng)
		.expect("adds a file there, which is in no tree");
}

#[test]
fn a_root_outlives_a_purge_made_on_another_device_and_the_purge_reaches_its_copy() {
	let mut rng = TestRng::new("tree purged elsewhere");
	let files = [("token.txt", "Leaked token\n"), ("b.txt", "b")];
	let (mut laptop, tree, root) = with_tree("tree purged elsewhere, laptop", &files, &mut rng);
	let desk_directory = nibkeep_testkit::empty_directory(
		env!("CARGO_TARGET_TMPDIR"),
		"tree purged elsewhere, desk",
	);
	fs::remove_dir(&desk_directory).expect("removes");
	let code = nibkeep_node::PairingCode::generate(&mut rng);
	let (paired, joined) = common::pair_with(&mut laptop, &desk_directory, &code, &code, &mut rng);
	paired.expect("the laptop paired");
	let mut desk = joined.expect("the desk joined").library;
	let personal = laptop.personal_space();
	let token = at_path(&laptop, tree, "token.txt");
	let leaked = values(&laptop, token, &CONTENT)[0].clone();
	desk.purge(
		personal,
		token,
		&CONTENT,
		Some(&leaked),
		now() + 2_000,
		&mut rng,
	)
	.expect("the desk purges");
	let (first, second) = common::sync_in_memory(&mut desk, &mut laptop, now() + 3_000, (1, 2));
	first.expect("the desk synced");
	let synced = second.expect("the laptop synced");
	// The laptop's copy goes as the sync settles, and the tree is still its tree.
	assert!(!root.join("token.txt").exists(), "{synced:?}");
	let status = laptop
		.tree_status(personal, tree)
		.expect("still the laptop's tree");
	nibkeep_testkit::assert_empty!(status.changes);
	assert!(laptop.tree_at(personal, &root).expect("looks").is_some());
}

#[test]
fn a_purge_that_comes_while_the_drive_is_away_reaches_the_copy_when_it_is_back() {
	let mut rng = TestRng::new("tree purge drive away");
	let files = [("token.txt", "Leaked token\n"), ("b.txt", "b")];
	let (mut library, tree, root) = with_tree("tree purge drive away", &files, &mut rng);
	let personal = library.personal_space();
	let token = at_path(&library, tree, "token.txt");
	let leaked = values(&library, token, &CONTENT)[0].clone();
	let away = root.with_file_name("tree purge drive away, away");
	let _ = fs::remove_dir_all(&away);
	fs::rename(&root, &away).expect("the drive goes");
	fs::create_dir(&root).expect("an empty mount point");
	library
		.purge(
			personal,
			token,
			&CONTENT,
			Some(&leaked),
			now() + 2_000,
			&mut rng,
		)
		.expect("purges");
	assert!(matches!(
		library.commit_tree(personal, tree, &Commit::new(None), now() + 3_000, &mut rng),
		Err(NodeError::TreeEmptied(_))
	));
	fs::remove_dir(&root).expect("the mount point");
	fs::rename(&away, &root).expect("the drive is back");
	let status = library.tree_status(personal, tree).expect("status");
	assert!(!root.join("token.txt").exists());
	nibkeep_testkit::assert_empty!(status.changes);
}

#[test]
fn a_folder_that_holds_a_checkout_does_not_move() {
	let mut rng = TestRng::new("tree move checkout");
	let files = [("docs/a.txt", "a")];
	let (mut library, tree, root) = with_tree("tree move checkout", &files, &mut rng);
	let personal = library.personal_space();
	let outside = folder("tree move checkout, outside");
	let report = write(&outside, "report.txt", "Report\n");
	let added = library
		.add_file(personal, &report, now() + 2_000, &mut rng)
		.expect("adds a file outside the tree");
	library
		.checkout(
			personal,
			added.file,
			&root.join("docs"),
			now() + 3_000,
			&mut rng,
		)
		.expect("checks it out into the tree's folder");
	nibkeep_testkit::assert_empty!(library.tree_status(personal, tree).expect("status").changes);
	assert!(matches!(
		library.move_in_tree(personal, tree, ("docs", "notes"), now() + 4_000, &mut rng),
		Err(NodeError::HoldsOtherCopies(_))
	));
	assert!(root.join("docs/report.txt").exists());
}

#[test]
fn a_copy_deleted_here_of_what_the_tree_deleted_too_needs_no_commit() {
	let mut rng = TestRng::new("tree deleted both");
	let files = [("a.txt", "a"), ("b.txt", "b")];
	let (mut library, tree, root) = with_tree("tree deleted both", &files, &mut rng);
	let personal = library.personal_space();
	let a = at_path(&library, tree, "a.txt");
	library
		.edit_entity(
			personal,
			a,
			vec![Edit::Delete],
			&common::today(),
			now() + 2_000,
			&mut rng,
		)
		.expect("deletes it, as another device would");
	fs::remove_file(root.join("a.txt")).expect("deletes the copy");
	let status = library.tree_status(personal, tree).expect("status");
	nibkeep_testkit::assert_empty!(status.changes);
	nibkeep_testkit::assert_empty!(status.behind);
	write(&root, "b.txt", "bb");
	library
		.commit_tree(personal, tree, &Commit::new(None), now() + 3_000, &mut rng)
		.expect("commits without force");
}

#[test]
fn a_folder_moves_with_every_file_that_competes_for_a_path_in_it() {
	let mut rng = TestRng::new("tree move competing");
	let files = [("docs/a.txt", "a")];
	let (mut library, tree, root) = with_tree("tree move competing", &files, &mut rng);
	let personal = library.personal_space();
	// Another device added a file at the same path, offline.
	let other = library
		.create_entity(
			personal,
			&[FILE],
			vec![
				(IN_TREE, Value::Reference(tree)),
				(TREE_PATH, text("docs/a.txt")),
			],
			now() + 2_000,
			&mut rng,
		)
		.expect("adds a competing file");
	let status = library.tree_status(personal, tree).expect("status");
	assert_eq!(status.competing, ["docs/a.txt"]);
	library
		.move_in_tree(personal, tree, ("docs", "notes"), now() + 3_000, &mut rng)
		.expect("moves");
	assert_eq!(values(&library, other, &TREE_PATH), [text("notes/a.txt")]);
	assert!(root.join("notes/a.txt").exists());
}