nibkeep-cli 0.1.0

nib, the generic command-line tool for NibKeep
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
//! Trees: folders whose files NibKeep keeps with every version, as Git keeps a working tree,
//! committed in one change each, with a revision, through commands named as Git, Mercurial,
//! Subversion, Fossil and Jujutsu name theirs ([technical design: trees]).
//!
//! [technical design: trees]: ../../../docs/Technical-design.md#15-trees

mod common;

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

use clap::Parser;
use common::{Test, library};
use nibkeep_cli::{Cli, CliError};
use nibkeep_node::NodeError;

/// 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) {
	let path = root.join(path);
	fs::create_dir_all(path.parent().expect("a folder")).expect("makes folders");
	fs::write(&path, content).expect("writes");
	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 output with every short ID, eight hexadecimal digits, replaced by `<id>`.
fn ids_hidden(output: &str) -> String {
	output
		.split_inclusive(|c: char| c.is_whitespace() || c == ',')
		.map(|token| {
			let word = token.trim_end_matches(|c: char| c.is_whitespace() || c == ',');
			if word.len() == 8 && word.chars().all(|c| c.is_ascii_hexdigit()) {
				format!("<id>{}", &token[word.len()..])
			} else {
				token.to_owned()
			}
		})
		.collect()
}

/// A library whose folder holds a few files, and is a tree.
fn with_tree(name: &str) -> Test {
	let mut test = library(name);
	let here = test.here.clone();
	write(&here, "notes.md", "# Notes\n");
	write(&here, "docs/plan.md", "Plan\n");
	write(&here, ".nibignore", "*.log\n");
	write(&here, "build.log", "noise\n");
	test.nib(&["init", "--yes", "--message", "Start"])
		.expect("starts");
	test
}

#[test]
fn init_in_a_library_makes_its_folder_a_tree_once_the_person_agrees() {
	let mut test = library("tree init asks");
	let here = test.here.clone();
	write(&here, "notes.md", "# Notes\n");
	write(&here, "docs/plan.md", "Plan\n");

	// Without a terminal, nothing starts.
	assert!(matches!(
		test.nib(&["init"]),
		Err(CliError::UnconfirmedStart)
	));
	// Asked, the person says no.
	test.answers = Some(["n"].into());
	assert!(matches!(
		test.nib(&["init"]),
		Err(CliError::UnconfirmedStart)
	));
	assert_eq!(test.questions.len(), 1);
	let question = &test.questions[0];
	assert!(question.contains("2 files"), "{question}");
	assert!(question.contains("purge"), "{question}");
	// And then yes.
	test.answers = Some(["y"].into());
	let started = test.nib(&["init"]).expect("starts");
	let name = here
		.file_name()
		.and_then(|name| name.to_str())
		.expect("a name");
	assert_eq!(
		ids_hidden(&started),
		format!(
			"Started the tree {name} in {}: 2 files, committed as revision <id>\n",
			here.display()
		)
	);
	assert!(matches!(
		test.nib(&["init", "--yes"]),
		Err(CliError::Node(NodeError::TreeInTree(_)))
	));
}

#[test]
fn init_makes_a_library_only_where_none_is_found() {
	// Outside every library, init given a folder or a message, which only a tree takes, says
	// how to make a library or join one.
	let mut test = Test::new("tree init outside");
	for arguments in [
		&["init", "."][..],
		&["init", "--message", "Start"],
		&["init", "--yes"],
	] {
		let refused = test.nib(arguments);
		assert!(
			matches!(refused, Err(CliError::NoLibraryForTree(_))),
			"{arguments:?}: {refused:?}"
		);
		let said = refused.expect_err("refused").to_string();
		assert!(said.contains("in your library's folder"), "{said}");
		assert!(said.contains("nib init alone"), "{said}");
		assert!(said.contains("nib init --join"), "{said}");
	}
	assert!(nibkeep_node::Library::replica_folder(&test.here.join(".nib")).is_err());
	// Init alone makes a library there, as git init makes a repository; again, in the
	// library's folder, it makes the folder a tree.
	test.answer(&["correct horse", "correct horse"]);
	let started = test.nib(&["init"]).expect("makes a library");
	assert!(
		started.starts_with("Ada Lovelace, on laptop, in "),
		"{started}"
	);
	write(&test.here, "notes.md", "# Notes\n");
	test.answer(&["correct horse"]);
	let tree = test.nib(&["init", "--yes"]).expect("makes a tree");
	assert!(tree.starts_with("Started the tree "), "{tree}");
	// The library is looked for from the folder to make a tree, as Git looks for a
	// repository.
	let mut test = library("tree init folder");
	let notes = test.here.join("notes");
	write(
		&notes, "notes.md", "# Notes
",
	);
	test.here =
		nibkeep_testkit::empty_directory(env!("CARGO_TARGET_TMPDIR"), "tree init elsewhere");
	let started = test
		.nib(&["init", notes.to_str().expect("UTF-8"), "--yes"])
		.expect("starts");
	assert!(started.contains("1 file"), "{started}");
	// What makes a library is never taken for a tree, nor the other way round.
	let parses = |arguments: &[&str]| {
		Cli::try_parse_from(std::iter::once("nib").chain(arguments.iter().copied())).is_ok()
	};
	assert!(parses(&["init", "--name", "Ada", "--passphrase"]));
	assert!(parses(&["init", "--passphrase", "--device", "laptop"]));
	assert!(parses(&["init", "notes", "--yes", "-m", "Start"]));
	assert!(!parses(&["init", "notes", "--passphrase"]));
	assert!(!parses(&["init", "--device", "laptop", "--yes"]));
	assert!(!parses(&["init", "--name", "Ada", "--message", "Start"]));
	assert!(!parses(&["init", "--join", "37-482-177", "notes"]));
}

/// A folder of its own beside the test's library, with a file in it.
fn beside(name: &str) -> std::path::PathBuf {
	let folder = nibkeep_testkit::empty_directory(env!("CARGO_TARGET_TMPDIR"), name);
	write(&folder, "notes.md", "# Notes\n");
	folder
}

/// The library that the `.nib` file in a tree's folder names, as the file system resolves it.
fn named_in(folder: &Path) -> std::path::PathBuf {
	let text = fs::read_to_string(folder.join(".nib")).expect("reads the file");
	let named = text
		.strip_prefix("library: ")
		.and_then(|rest| rest.strip_suffix('\n'))
		.expect("one line naming the library");
	fs::canonicalize(named).expect("a folder")
}

#[test]
fn init_in_the_librarys_folder_makes_any_folder_a_tree_that_names_the_library() {
	let mut test = library("tree init beside");
	let library = test.here.clone();
	let project = beside("tree init beside, project");
	let name = project
		.file_name()
		.and_then(|name| name.to_str())
		.expect("a name")
		.to_owned();

	// From the library's folder, as git worktree add makes a worktree of the repository there.
	let started = test
		.nib(&[
			"init",
			project.to_str().expect("UTF-8"),
			"--yes",
			"-m",
			"Start",
		])
		.expect("starts");
	assert_eq!(
		ids_hidden(&started),
		format!(
			"Started the tree {name} in {}: 1 file, committed as revision <id>\nWrote .nib into the tree's folder, which tells nib there which library to use\n",
			project.display()
		)
	);
	assert_eq!(
		named_in(&project),
		fs::canonicalize(&library).expect("a folder")
	);

	// In the tree's folder, and in the folders in it, nib finds the library by it, and never
	// takes the file into the tree.
	test.here.clone_from(&project);
	assert_eq!(
		test.nib(&["status"]).expect("status"),
		"Nothing changed in the tree's copy here\n"
	);
	write(&project, "docs/plan.md", "Plan\n");
	test.here = project.join("docs");
	assert_eq!(
		test.nib(&["status"]).expect("status"),
		"new       docs/plan.md\n"
	);
	let committed = test.nib(&["commit", "-m", "Plan"]).expect("commits");
	assert_eq!(
		ids_hidden(&committed),
		"Committed 1 change as revision <id>\nnew       docs/plan.md\n"
	);
	let whole = test.nib(&["status", "--library-wide"]).expect("status");
	assert!(whole.starts_with("Ada, on laptop, in "), "{whole}");

	// For programs, the file it wrote; and none for a tree in the library's folder, where nib
	// finds the library anyway.
	test.here.clone_from(&library);
	let other = beside("tree init beside, other");
	let shown = test.json(&["init", other.to_str().expect("UTF-8"), "--yes"]);
	assert_eq!(
		shown["pointer"],
		other.join(".nib").display().to_string(),
		"{shown}"
	);
	write(&library, "inside/notes.md", "# Notes\n");
	let shown = test.json(&["init", "inside", "--yes"]);
	assert_eq!(shown["pointer"], serde_json::Value::Null, "{shown}");
	assert!(!library.join("inside/.nib").exists());
}

#[test]
fn a_nib_file_counts_only_for_a_tree_of_its_library_in_that_folder() {
	let mut test = library("tree pointer checked");
	let library = test.here.clone();
	let project = beside("tree pointer checked, project");
	test.nib(&["init", project.to_str().expect("UTF-8"), "--yes"])
		.expect("starts");

	// A copy of the tree's folder, as in an archive from elsewhere, names the library, which
	// keeps no tree there: nib does not take it.
	let copy =
		nibkeep_testkit::empty_directory(env!("CARGO_TARGET_TMPDIR"), "tree pointer checked, copy");
	fs::copy(project.join(".nib"), copy.join(".nib")).expect("copies");
	test.here.clone_from(&copy);
	for arguments in [
		&["status"][..],
		&["status", "--library-wide"],
		&["device", "list"],
	] {
		let refused = test.nib(arguments);
		assert!(
			matches!(refused, Err(CliError::StrayPointer { .. })),
			"{arguments:?}: {refused:?}"
		);
	}

	// A file that names no library, or is not a line that names one, stops nib, which looks
	// no further.
	let bad = [
		format!("library: {}\n", copy.display()),
		"library: relative\n".to_owned(),
		"something else\n".to_owned(),
		String::new(),
	];
	for text in bad {
		fs::write(copy.join(".nib"), &text).expect("writes");
		let refused = test.nib(&["status"]);
		assert!(
			matches!(refused, Err(CliError::BadPointer { .. })),
			"{text:?}: {refused:?}"
		);
	}

	// Nor does nib leave NIBKEEP_CEILING for a library above it, as a build under development
	// must not.
	let inner = copy.join("inner");
	fs::create_dir_all(&inner).expect("makes a folder");
	fs::write(
		inner.join(".nib"),
		format!("library: {}\n", library.display()),
	)
	.expect("writes");
	test.here.clone_from(&inner);
	test.ceiling = Some(copy.clone());
	let refused = test.nib(&["status"]);
	assert!(
		matches!(refused, Err(CliError::BadPointer { .. })),
		"{refused:?}"
	);
	let said = refused.expect_err("refused").to_string();
	assert!(said.contains("NIBKEEP_CEILING"), "{said}");
}

#[test]
fn init_writes_a_lost_nib_file_again() {
	let mut test = library("tree pointer lost");
	let library = test.here.clone();
	let project = beside("tree pointer lost, project");
	let folder = project.to_str().expect("UTF-8").to_owned();
	test.nib(&["init", &folder, "--yes"]).expect("starts");
	let name = project
		.file_name()
		.and_then(|name| name.to_str())
		.expect("a name")
		.to_owned();

	// Without the file, nib finds no library in the tree's folder.
	fs::remove_file(project.join(".nib")).expect("deletes");
	test.here.clone_from(&project);
	assert!(matches!(test.nib(&["status"]), Err(CliError::NotInLibrary)));

	// nib init in the library's folder writes it again, without asking, as the tree is there.
	test.here.clone_from(&library);
	assert_eq!(
		test.nib(&["init", &folder]).expect("writes it again"),
		format!(
			"Wrote .nib again into {}, the tree {name}'s folder, which tells nib there which library to use\n",
			project.display()
		)
	);
	assert_eq!(
		named_in(&project),
		fs::canonicalize(&library).expect("a folder")
	);
	// So it does for a file that names no library, as after the library's folder moved.
	fs::write(project.join(".nib"), "library: /nowhere\n").expect("writes");
	test.nib(&["init", &folder]).expect("writes it again");
	assert_eq!(
		named_in(&project),
		fs::canonicalize(&library).expect("a folder")
	);
	test.here.clone_from(&project);
	assert_eq!(
		test.nib(&["status"]).expect("status"),
		"Nothing changed in the tree's copy here\n"
	);

	// For programs, as a tree that starts, with nothing committed.
	fs::remove_file(project.join(".nib")).expect("deletes");
	test.here.clone_from(&library);
	let shown = test.json(&["init", &folder]);
	assert_eq!(shown["revision"], serde_json::Value::Null, "{shown}");
	assert_eq!(shown["changes"], serde_json::json!([]), "{shown}");
	assert_eq!(
		shown["pointer"],
		project.join(".nib").display().to_string(),
		"{shown}"
	);

	// With the file in place, the folder is a tree already.
	assert!(matches!(
		test.nib(&["init", &folder, "--yes"]),
		Err(CliError::Node(NodeError::TreeInTree(_)))
	));
}

#[test]
fn init_makes_a_copy_of_a_trees_folder_a_tree_of_its_own() {
	let mut test = library("tree pointer copied");
	let library = test.here.clone();
	let project = beside("tree pointer copied, project");
	test.nib(&["init", project.to_str().expect("UTF-8"), "--yes"])
		.expect("starts");

	// A copy holds the tree's .nib file, which names the library that keeps no tree there:
	// init takes it all the same, and writes it anew.
	let copy = beside("tree pointer copied, copy");
	fs::copy(project.join(".nib"), copy.join(".nib")).expect("copies");
	let started = test
		.nib(&["init", copy.to_str().expect("UTF-8"), "--yes"])
		.expect("starts");
	assert!(started.starts_with("Started the tree "), "{started}");
	assert_eq!(
		named_in(&copy),
		fs::canonicalize(&library).expect("a folder")
	);
	test.here.clone_from(&copy);
	assert_eq!(
		test.nib(&["status"]).expect("status"),
		"Nothing changed in the tree's copy here\n"
	);
	// So in the copy itself, outside every library's folder.
	let again = beside("tree pointer copied, again");
	fs::copy(project.join(".nib"), again.join(".nib")).expect("copies");
	test.here.clone_from(&again);
	let started = test.nib(&["init", "--yes"]).expect("starts");
	assert!(started.starts_with("Started the tree "), "{started}");
}

#[test]
fn a_tree_in_the_librarys_folder_keeps_no_nib_file() {
	let mut test = library("tree pointer inside");
	let library = test.here.clone();
	let copied = library.join("copied");
	let file = copied.join(".nib");

	// A folder copied in with the .nib file of a tree elsewhere, which names no library here
	// and would take over the commands there: nib finds the library without it, so init takes
	// it away.
	write(&library, "copied/notes.md", "# Notes\n");
	fs::write(&file, "library: /nowhere\n").expect("writes");
	let started = test.nib(&["init", "copied", "--yes"]).expect("starts");
	assert!(
		started.starts_with("Started the tree copied in "),
		"{started}"
	);
	assert!(
		started.ends_with(
			"Took away .nib from the tree's folder, where nib finds the library without it\n"
		),
		"{started}"
	);
	assert!(!file.exists());
	test.here.clone_from(&copied);
	assert_eq!(
		test.nib(&["status"]).expect("status"),
		"Nothing changed in the tree's copy here\n"
	);

	// One left there later, as by an older program, stops nib there; nib init in the library's
	// folder takes it away, without asking, as the tree is there.
	fs::write(&file, "library: /nowhere\n").expect("writes");
	assert!(matches!(
		test.nib(&["status"]),
		Err(CliError::BadPointer { .. })
	));
	test.here.clone_from(&library);
	assert_eq!(
		test.nib(&["init", "copied"]).expect("takes it away"),
		format!(
			"Took away .nib from {}, the tree copied's folder, where nib finds the library without it\n",
			copied.display()
		)
	);
	assert!(!file.exists());
	test.here.clone_from(&copied);
	assert_eq!(
		test.nib(&["status"]).expect("status"),
		"Nothing changed in the tree's copy here\n"
	);

	// For programs, as a tree that starts, with nothing committed.
	fs::write(&file, format!("library: {}\n", library.display())).expect("writes");
	test.here.clone_from(&library);
	let shown = test.json(&["init", "copied"]);
	assert_eq!(shown["revision"], serde_json::Value::Null, "{shown}");
	assert_eq!(shown["pointer"], serde_json::Value::Null, "{shown}");
	assert_eq!(
		shown["pointer_taken_away"],
		file.display().to_string(),
		"{shown}"
	);
	// A link to a file, which nib would read as one, goes too; what it links to stays.
	#[cfg(unix)]
	{
		let target = beside("tree pointer inside, elsewhere").join("old.nib");
		fs::write(&target, "library: /nowhere\n").expect("writes");
		std::os::unix::fs::symlink(&target, &file).expect("links");
		test.nib(&["init", "copied"]).expect("takes it away");
		assert!(fs::symlink_metadata(&file).is_err());
		assert!(target.is_file());
	}
	// With nothing there to take away, the folder is a tree already.
	assert!(matches!(
		test.nib(&["init", "copied", "--yes"]),
		Err(CliError::Node(NodeError::TreeInTree(_)))
	));

	// A folder in its place, which holds no library, stays, as nib passes it by.
	write(&library, "kept/notes.md", "# Notes\n");
	fs::create_dir(library.join("kept/.nib")).expect("makes a folder");
	let shown = test.json(&["init", "kept", "--yes"]);
	assert_eq!(
		shown["pointer_taken_away"],
		serde_json::Value::Null,
		"{shown}"
	);
	assert!(library.join("kept/.nib").is_dir());
	test.here = library.join("kept");
	assert_eq!(
		test.nib(&["status"]).expect("status"),
		"Nothing changed in the tree's copy here\n"
	);
}

#[test]
fn what_concerns_the_librarys_place_does_not_follow_a_nib_file() {
	let mut test = library("tree pointer place");
	let project = beside("tree pointer place, project");
	test.nib(&["init", project.to_str().expect("UTF-8"), "--yes"])
		.expect("starts");
	test.here.clone_from(&project);
	for arguments in [
		&["verify", "--drive-checked"][..],
		&["sync", "--at-login", "on"],
	] {
		let refused = test.nib(arguments);
		assert!(
			matches!(refused, Err(CliError::ThroughPointer(_))),
			"{arguments:?}: {refused:?}"
		);
	}
}

#[test]
fn init_starts_no_tree_where_its_nib_file_cannot_go() {
	let mut test = library("tree pointer in the way");
	let library = test.here.clone();
	let project = beside("tree pointer in the way, project");
	fs::create_dir(project.join(".nib")).expect("makes a folder");
	let folder = project.to_str().expect("UTF-8").to_owned();
	let refused = test.nib(&["init", &folder, "--yes"]);
	assert!(
		matches!(refused, Err(CliError::PointerInTheWay(_))),
		"{refused:?}"
	);
	// Nothing started, so once the way is clear, it starts.
	fs::remove_dir(project.join(".nib")).expect("deletes");
	test.nib(&["init", &folder, "--yes"]).expect("starts");

	// With --library, from a folder in no library, the same.
	let elsewhere = nibkeep_testkit::empty_directory(
		env!("CARGO_TARGET_TMPDIR"),
		"tree pointer in the way, elsewhere",
	);
	let other = beside("tree pointer in the way, other");
	test.here.clone_from(&elsewhere);
	let named = library.to_str().expect("UTF-8");
	test.nib(&[
		"--library",
		named,
		"init",
		other.to_str().expect("UTF-8"),
		"--yes",
	])
	.expect("starts");
	assert_eq!(
		named_in(&other),
		fs::canonicalize(&library).expect("a folder")
	);
	test.here.clone_from(&other);
	assert_eq!(
		test.nib(&["status"]).expect("status"),
		"Nothing changed in the tree's copy here\n"
	);
}

#[test]
fn status_and_commit_show_and_record_what_changed() {
	let mut test = with_tree("tree status and commit");
	let here = test.here.clone();
	assert_eq!(
		test.nib(&["status"]).expect("status"),
		"Nothing changed in the tree's copy here\n"
	);
	write(&here, "notes.md", "# Notes, edited\n");
	write(&here, "docs/new.md", "New\n");
	fs::remove_file(here.join("docs/plan.md")).expect("deletes");
	write(&here, "more.log", "left out\n");
	let status = test.nib(&["status"]).expect("status");
	assert_eq!(
		status,
		"deleted   docs/plan.md\nnew       docs/new.md\nedited    notes.md\n"
	);
	assert_eq!(test.nib(&["st"]).expect("status"), status);
	let shown = test.json(&["status"]);
	assert_eq!(shown["changes"][0]["path"], "docs/new.md");
	assert_eq!(shown["changes"][0]["change"], "new");
	assert_eq!(shown["changes"][1]["change"], "deleted");
	assert_eq!(shown["root"], here.display().to_string());
	// The library's status, from inside a tree too.
	let whole = test.nib(&["status", "--library-wide"]).expect("status");
	assert!(whole.starts_with("Ada, on laptop, in "), "{whole}");
	let whole = test.json(&["status", "--library-wide"]);
	assert_eq!(whole["user"]["name"], "Ada");
	assert!(whole.get("tree").is_none(), "{whole}");

	// From a folder inside the tree, a path is relative to it.
	test.here = here.join("docs");
	let committed = test
		.nib(&["commit", "new.md", "plan.md", "--message", "Docs"])
		.expect("commits");
	assert_eq!(
		ids_hidden(&committed),
		"Committed 2 changes as revision <id>\ndeleted   docs/plan.md\nnew       docs/new.md\n"
	);
	test.here = here.clone();
	assert_eq!(
		test.nib(&["status"]).expect("status"),
		"edited    notes.md\n"
	);
	let committed = test.json(&["commit"]);
	// The commit's revision, then the part that records the file.
	assert_eq!(committed["parts"], 2);
	assert!(committed["revision"].is_string());
	assert_eq!(committed["changes"][0]["path"], "notes.md");
	assert_eq!(
		test.nib(&["commit"]).expect("commits"),
		"Nothing to commit\n"
	);
	// As Mercurial and Subversion name it, and with Git's -a, which is what commit does.
	write(&here, "notes.md", "# Notes, again\n");
	let committed = test.nib(&["ci", "-a", "-m", "Again"]).expect("commits");
	assert!(committed.contains("edited    notes.md"), "{committed}");
	assert!(
		Cli::try_parse_from(["nib", "commit", "-a", "notes.md"]).is_err(),
		"-a means every change, so takes no paths"
	);
	// A revision is an entity, found by the words of its message.
	let found = test.nib(&["find", "Docs"]).expect("finds");
	assert!(found.contains("std.revision"), "{found}");
}

#[test]
fn a_move_moves_the_copy_and_the_file_at_once() {
	let mut test = with_tree("tree move");
	let here = test.here.clone();
	let moved = test
		.nib(&["mv", "notes.md", "docs/notes.md"])
		.expect("moves");
	assert_eq!(
		ids_hidden(&moved),
		"Moved notes.md to docs/notes.md, as revision <id>\n"
	);
	assert!(here.join("docs/notes.md").exists());
	assert!(!here.join("notes.md").exists());
	assert_eq!(
		test.nib(&["status"]).expect("status"),
		"Nothing changed in the tree's copy here\n"
	);
	// As Mercurial names it.
	test.nib(&["rename", "docs/notes.md", "notes.md"])
		.expect("moves");
	assert!(here.join("notes.md").exists());
	assert!(matches!(
		test.nib(&["mv", "notes.md", ".nib/notes.md"]),
		Err(CliError::Node(NodeError::UnsafePath(_)))
	));
}

#[test]
fn rm_deletes_copies_and_the_next_commit_records_it() {
	let mut test = with_tree("tree rm");
	let here = test.here.clone();
	let removed = test.nib(&["rm", "notes.md"]).expect("removes");
	assert_eq!(
		removed,
		"removed   notes.md\nNothing is recorded: nib commit records it.\n"
	);
	assert!(!here.join("notes.md").exists());
	assert_eq!(
		test.nib(&["status"]).expect("status"),
		"deleted   notes.md\n"
	);
	// A folder, with Git's -r or without, as Mercurial's remove takes one.
	let removed = test.json(&["remove", "-r", "docs"]);
	assert_eq!(removed["removed"], serde_json::json!(["docs/plan.md"]));
	assert!(!here.join("docs").exists());
	// An edited copy stays, unless forced.
	test.nib(&["restore", "notes.md"]).expect("restores");
	write(&here, "notes.md", "# Notes, edited\n");
	assert!(matches!(
		test.nib(&["rm", "notes.md"]),
		Err(CliError::Node(NodeError::EditedCopy(_)))
	));
	test.nib(&["rm", "--force", "notes.md"]).expect("removes");
	assert!(!here.join("notes.md").exists());
	let committed = test.nib(&["commit", "-m", "Gone"]).expect("commits");
	assert!(committed.contains("deleted   notes.md"), "{committed}");
	assert!(committed.contains("deleted   docs/plan.md"), "{committed}");
}

#[test]
fn outside_a_tree_the_tree_commands_say_so() {
	let mut test = library("tree outside");
	assert!(matches!(test.nib(&["commit"]), Err(CliError::NotInTree(_))));
	// The library's status, as there is no tree's to show.
	let status = test.nib(&["status"]).expect("status");
	assert!(status.starts_with("Ada, on laptop, in "), "{status}");
	// Reading history names the other ways in.
	let refused = test.nib(&["log"]);
	assert!(
		matches!(refused, Err(CliError::NoTreeHere(_))),
		"{refused:?}"
	);
	let said = refused.expect_err("refused").to_string();
	assert!(said.contains("--tree"), "{said}");
	assert!(said.contains("nib history"), "{said}");
}

#[test]
fn what_nib_lacks_says_what_to_do_instead() {
	let mut test = with_tree("tree lacks");
	for (command, instead) in [
		(&["add", "."][..], "nib commit takes in new files"),
		(&["add", "-A"][..], "nib commit takes in new files"),
		(&["checkout", "main"][..], "nib clone"),
		(&["branch"][..], "no branches"),
		(&["tag", "v1"][..], "no branches"),
		(&["stash"][..], "no stash"),
		(&["reset", "--hard"][..], "nib restore"),
		(&["rebase", "main"][..], "no branches"),
		(&["fetch"][..], "nib sync"),
	] {
		let refused = test.nib(command);
		let Err(error @ CliError::NoSuchCommand { .. }) = refused else {
			panic!("{command:?}: {refused:?}");
		};
		let said = error.to_string();
		assert!(said.contains(instead), "{command:?}: {said}");
		assert!(
			said.starts_with(&format!("there is no {} command", command[0])),
			"{said}"
		);
	}
}

#[test]
fn files_outside_trees_leave_trees_alone() {
	let mut test = with_tree("tree and files");
	let here = test.here.clone();
	// In a folder, a tree's files are left out, as checkouts are; named, they fail.
	let docs = here.join("docs");
	let added = test.json(&["file", "add", docs.to_str().expect("UTF-8")]);
	assert_eq!(added["files"].as_array().map(Vec::len), Some(0));
	assert_eq!(added["left_out"], 1);
	let notes = here.join("notes.md");
	let named = test.nib_printing(&["file", "add", notes.to_str().expect("UTF-8")]);
	assert!(
		named.0.contains("in a tree; nib commit records it"),
		"{}",
		named.0
	);
	assert!(matches!(named.1, Err(CliError::FilesFailed(1))));
	assert!(matches!(
		test.nib(&[
			"file",
			"checkout",
			"plan.md",
			here.join("out.md").to_str().expect("UTF-8")
		]),
		Err(CliError::Node(NodeError::TreeFile(_)))
	));
}

/// A library whose folder is a tree, with a second commit that edits notes.md; the
/// revisions' IDs are returned, from the JSON of the commits.
fn with_two_commits(name: &str) -> (Test, Vec<String>) {
	let mut test = library(name);
	let here = test.here.clone();
	write(&here, "notes.md", "one\ntwo\n");
	write(&here, "old.txt", "Old\n");
	let started = test.json(&["init", "--yes", "--message", "Start"]);
	write(&here, "notes.md", "one\nTWO\n");
	fs::remove_file(here.join("old.txt")).expect("deletes");
	let second = test.json(&["commit", "--message", "Second"]);
	let revision =
		|shown: &serde_json::Value| shown["revision"].as_str().expect("a revision").to_owned();
	let revisions = vec![revision(&started), revision(&second)];
	(test, revisions)
}

#[test]
fn log_diff_and_blame_read_the_tree_s_history() {
	let (mut test, revisions) = with_two_commits("tree history commands");
	let log = test.nib(&["log"]).expect("logs");
	let lines: Vec<&str> = log.lines().collect();
	assert!(
		lines[0].starts_with("revision ") && lines[0].ends_with("Ada"),
		"{log}"
	);
	assert_eq!(lines[1], "    Second");
	assert_eq!(
		&lines[2..4],
		["  deleted   old.txt", "  edited    notes.md"]
	);
	assert!(log.contains("    Start"), "{log}");
	let notes_log = test.json(&["log", "notes.md"]);
	assert_eq!(notes_log.as_array().map(Vec::len), Some(2));

	let diff = test
		.nib(&["diff", &revisions[0], &revisions[1]])
		.expect("diffs");
	assert!(diff.contains("deleted   old.txt"), "{diff}");
	assert!(diff.contains("-two\n+TWO\n"), "{diff}");
	// As Mercurial, Fossil and Jujutsu name the two points.
	let named = test
		.nib(&["diff", "--from", &revisions[0], "--to", &revisions[1]])
		.expect("diffs");
	assert_eq!(named, diff);
	write(&test.here.clone(), "notes.md", "one\nTWO\nthree\n");
	let diff = test.nib(&["diff"]).expect("diffs the copy");
	assert!(diff.starts_with("edited    notes.md\n"), "{diff}");
	assert!(diff.contains("+three"), "{diff}");

	let blame = test.json(&["blame", "notes.md"]);
	assert_eq!(blame[0]["text"], "one");
	assert_eq!(blame[0]["revision"], revisions[0]);
	assert_eq!(blame[1]["revision"], revisions[1]);
	assert_eq!(test.json(&["annotate", "notes.md"]), blame);
}

#[test]
fn show_of_a_revision_shows_what_it_changed() {
	let (mut test, revisions) = with_two_commits("tree show revision");
	let shown = test.nib(&["show", &revisions[1]]).expect("shows");
	assert!(shown.contains("Second"), "{shown}");
	assert!(shown.contains("\ndeleted   old.txt\n"), "{shown}");
	assert!(shown.contains("\nedited    notes.md\n"), "{shown}");
	assert!(shown.contains("-two\n+TWO\n"), "{shown}");
	let shown = test.json(&["show", &revisions[1]]);
	assert_eq!(shown["diff"][0]["path"], "notes.md");
	assert_eq!(shown["diff"][0]["change"], "edited");
	assert_eq!(shown["diff"][1]["path"], "old.txt");
	// Another entity shows no diff.
	let notes = test.json(&["show", "notes.md"]);
	assert!(notes.get("diff").is_none(), "{notes}");
}

#[test]
fn restore_and_revert_write_the_copy_and_record_nothing() {
	let (mut test, revisions) = with_two_commits("tree going back");
	let here = test.here.clone();
	let restored = test.nib(&["restore", "old.txt"]).expect("restores");
	assert_eq!(
		restored,
		"restored  old.txt\nNothing is recorded: nib commit records what you keep.\n"
	);
	assert_eq!(
		fs::read_to_string(here.join("old.txt")).expect("reads"),
		"Old\n"
	);
	fs::remove_file(here.join("old.txt")).expect("deletes it again");
	let reverted = test.nib(&["revert", &revisions[1]]).expect("reverts");
	assert!(reverted.contains("written   notes.md"), "{reverted}");
	assert!(reverted.contains("written   old.txt"), "{reverted}");
	assert_eq!(
		fs::read_to_string(here.join("notes.md")).expect("reads"),
		"one\ntwo\n"
	);
	let status = test.nib(&["status"]).expect("status");
	assert_eq!(status, "new       old.txt\nedited    notes.md\n");

	// Restoring over edits discards them from the copy, and keeps them beside it.
	let restored = test
		.nib(&["restore", "notes.md", "-r", &revisions[1]])
		.expect("restores");
	assert_eq!(
		restored,
		"restored  notes.md, your edits kept as notes.md.orig\nNothing is recorded: nib commit records what you keep.\n"
	);
	assert_eq!(
		fs::read_to_string(here.join("notes.md")).expect("reads"),
		"one\nTWO\n"
	);
	assert_eq!(
		fs::read_to_string(here.join("notes.md.orig")).expect("reads"),
		"one\ntwo\n"
	);
	let restored = test.json(&["restore", "old.txt", "--at", &revisions[0]]);
	assert_eq!(restored["written"], serde_json::json!([]));
	fs::remove_file(here.join("notes.md.orig")).expect("deletes");
	fs::remove_file(here.join("old.txt")).expect("deletes");

	// As Mercurial and Fossil name it; a path is for restore.
	test.nib(&["backout", &revisions[1]]).expect("reverts");
	let refused = test.nib(&["revert", "notes.md"]);
	assert!(
		matches!(refused, Err(CliError::RevertTakesRevision(_))),
		"{refused:?}"
	);
	let said = refused.expect_err("refused").to_string();
	assert!(said.contains("nib restore notes.md"), "{said}");
}

#[test]
fn a_tree_is_cloned_into_a_new_folder_at_a_revision() {
	let (mut test, revisions) = with_two_commits("tree cloned");
	let here = test.here.clone();
	let name = here
		.file_name()
		.and_then(|name| name.to_str())
		.expect("a name")
		.to_owned();
	let picture = here
		.parent()
		.expect("a parent")
		.join("tree cloned, picture");
	let _ = fs::remove_dir_all(&picture);
	let written = test
		.nib(&[
			"clone",
			&name,
			picture.to_str().expect("UTF-8"),
			"-r",
			&revisions[0],
		])
		.expect("writes");
	assert_eq!(
		written,
		format!(
			"Wrote 2 files into {}, a picture of the past, which is no copy\n",
			picture.display()
		)
	);
	assert_eq!(
		fs::read_to_string(picture.join("notes.md")).expect("reads"),
		"one\ntwo\n"
	);
	assert!(matches!(
		test.nib(&["clone", &name, picture.to_str().expect("UTF-8")]),
		Err(CliError::Node(NodeError::TreeHereAlready(_)))
	));
	// Without a folder, a folder named after the tree, in the current one.
	let elsewhere =
		nibkeep_testkit::empty_directory(env!("CARGO_TARGET_TMPDIR"), "tree cloned, elsewhere");
	test.here.clone_from(&elsewhere);
	let library = here.to_str().expect("UTF-8");
	test.nib(&["--library", library, "clone", &name, "--at", &revisions[1]])
		.expect("writes");
	assert_eq!(
		fs::read_to_string(elsewhere.join(&name).join("notes.md")).expect("reads"),
		"one\nTWO\n"
	);
}

#[test]
fn update_brings_the_copy_to_the_tree_as_it_is() {
	let mut test = with_tree("tree update command");
	let here = test.here.clone();
	assert_eq!(
		test.nib(&["update"]).expect("updates"),
		"The copy here has the tree as it is\n"
	);
	// Elsewhere, as with nib edit on another device, a file moves.
	test.nib(&["edit", "plan.md", "std.tree-path=docs/later.md"])
		.expect("moves it in the tree");
	let updated = test.nib(&["up"]).expect("updates");
	assert_eq!(updated, "moved     docs/later.md, from docs/plan.md\n");
	assert!(here.join("docs/later.md").exists());
	assert_eq!(
		test.nib(&["status"]).expect("status"),
		"Nothing changed in the tree's copy here\n"
	);
}

#[test]
fn pull_syncs_then_updates_and_push_syncs() {
	let mut test = with_tree("tree pull");
	let here = test.here.clone();
	test.nib(&["edit", "plan.md", "std.tree-path=docs/later.md"])
		.expect("moves it in the tree");
	// A library on one device has nothing to sync with, which pull says once it updated.
	let (pulled, result) = test.nib_printing(&["pull", "--wait", "0"]);
	assert_eq!(pulled, "moved     docs/later.md, from docs/plan.md\n");
	assert!(
		matches!(result, Err(CliError::Node(NodeError::NoOtherReplicas))),
		"{result:?}"
	);
	assert!(here.join("docs/later.md").exists());
	let (pulled, result) = test.nib_printing(&["--json", "pull", "--wait", "0"]);
	assert!(result.is_err());
	let pulled: serde_json::Value = serde_json::from_str(&pulled).expect("JSON");
	assert_eq!(pulled["sync"], serde_json::Value::Null);
	assert_eq!(pulled["update"]["written"], serde_json::json!([]));
	assert!(matches!(
		test.nib(&["push", "--wait", "0"]),
		Err(CliError::Node(NodeError::NoOtherReplicas))
	));
}

/// Runs the test's Git in `folder`, and returns what it wrote.
fn git(test: &Test, folder: &Path, arguments: &[&str]) -> String {
	let output = test
		.git_command()
		.arg("-C")
		.arg(folder)
		.args(arguments)
		.output()
		.expect("runs Git");
	assert!(
		output.status.success(),
		"git {arguments:?}: {}",
		String::from_utf8_lossy(&output.stderr)
	);
	String::from_utf8_lossy(&output.stdout).trim().to_owned()
}

/// A library whose folder holds notes and a Git repository at `proj`, on `main`, whose
/// `origin` is an address with a token in it, which the test's Git takes to a bare
/// repository beside it that holds the first commit; then a tree.
fn with_repository(name: &str) -> Test {
	let mut test = with_repository_unstarted(name);
	test.nib(&["init", "--yes", "--message", "Start"])
		.expect("starts");
	test
}

/// The library of [`with_repository`], before its folder is a tree.
fn with_repository_unstarted(name: &str) -> Test {
	let test = library(name);
	let here = test.here.clone();
	write(&here, "notes.md", "# Notes\n");
	let remote =
		nibkeep_testkit::empty_directory(env!("CARGO_TARGET_TMPDIR"), &format!("{name}, remote"));
	git(&test, &remote, &["init", "--quiet", "--bare"]);
	// What the person's Git would reach over the network, it reaches on the disk.
	let settings = format!(
		"{}[url \"{}\"]\n\tinsteadOf = https://example.invalid/proj.git\n\tinsteadOf = https://ada:secret-token@example.invalid/proj.git\n",
		fs::read_to_string(test.git_home.join("config")).expect("reads"),
		remote.display().to_string().replace('\\', "/")
	);
	fs::write(test.git_home.join("config"), settings).expect("writes");
	let proj = here.join("proj");
	fs::create_dir(&proj).expect("makes a folder");
	git(&test, &proj, &["init", "--quiet"]);
	write(&here, "proj/README.md", "Proj\n");
	git(&test, &proj, &["add", "README.md"]);
	git(&test, &proj, &["commit", "--quiet", "-m", "First"]);
	git(
		&test,
		&proj,
		&[
			"remote",
			"add",
			"origin",
			"https://ada:secret-token@example.invalid/proj.git",
		],
	);
	git(
		&test,
		&proj,
		&["push", "--quiet", "--set-upstream", "origin", "main"],
	);
	test
}

#[test]
fn a_tree_records_its_git_repositories_when_it_starts() {
	let mut test = with_repository_unstarted("tree git start");
	let started = test
		.nib(&["init", "--yes", "--message", "Start"])
		.expect("starts");
	assert!(
		started.contains("new       proj, a Git repository"),
		"{started}"
	);
	assert!(!started.contains("README"), "{started}");
	let status = test.nib(&["status"]).expect("status");
	assert_eq!(status, "Nothing changed in the tree's copy here\n");
	let listed = test.json(&["status", "--json"]);
	assert_eq!(listed["repositories"], serde_json::json!([]));
	assert_eq!(listed["only_here"], serde_json::json!([]));
}

#[test]
fn status_names_the_work_that_only_this_computer_holds() {
	let mut test = with_repository("tree git work here");
	let proj = test.here.join("proj");
	write(&test.here, "proj/more.md", "More\n");
	git(&test, &proj, &["add", "more.md"]);
	git(&test, &proj, &["commit", "--quiet", "-m", "Not pushed"]);
	write(&test.here, "proj/draft.md", "Draft\n");
	let status = test.nib(&["status"]).expect("status");
	assert!(
		status.contains("only here proj: changes not committed, 1 commit not pushed"),
		"{status}"
	);
}

#[test]
fn a_commit_records_what_changed_of_a_repository_and_pin_pins_it() {
	let mut test = with_repository("tree git commit");
	let proj = test.here.join("proj");
	git(
		&test,
		&proj,
		&["remote", "add", "backup", "git@example.invalid:me/proj.git"],
	);
	let committed = test.nib(&["commit", "-m", "Backup"]).expect("commits");
	assert!(
		committed.contains("edited    proj, a Git repository's remotes, branch or pinned commit"),
		"{committed}"
	);
	// The repository's change counts, as the files' do.
	assert!(
		committed.starts_with("Committed 1 change as revision "),
		"{committed}"
	);
	let head = git(&test, &proj, &["rev-parse", "HEAD"]);
	let pinned = test.nib(&["commit", "--pin", "proj"]).expect("pins");
	assert_eq!(
		ids_hidden(&pinned),
		format!("Pinned proj at {}, as revision <id>\n", &head[..12])
	);
	let unpinned = test.nib(&["commit", "--unpin", "proj"]).expect("unpins");
	assert_eq!(ids_hidden(&unpinned), "Unpinned proj, as revision <id>\n");
	assert!(matches!(
		test.nib(&["commit", "--pin", "notes.md"]),
		Err(CliError::NoGitCommit(_) | CliError::Node(NodeError::NoRepository(_)))
	));
}

/// The repository `proj` of a test's tree gone, as on a computer that never had it: its
/// folder, and the index of copies with it.
fn as_never_cloned(test: &Test) {
	fs::remove_dir_all(test.here.join("proj")).expect("deletes");
	for replica in fs::read_dir(test.here.join(".nib/replicas")).expect("lists") {
		let index = replica.expect("reads").path().join("trees.sqlite");
		if index.exists() {
			fs::remove_file(index).expect("loses the index");
		}
	}
}

#[test]
fn update_clones_a_repository_this_copy_lacks_once_the_person_agrees() {
	let mut test = with_repository("tree git clone");
	as_never_cloned(&test);
	let listed = test.nib(&["update", "--no-clone"]).expect("updates");
	assert!(
		listed.contains(
			"to clone  proj, from https://example.invalid/proj.git (origin), recorded by"
		),
		"{listed}"
	);
	assert!(listed.contains("nib update --clone clones it"), "{listed}");
	assert!(!test.here.join("proj").exists());
	// Asked, the person says no, then yes.
	test.answers = Some(["n"].into_iter().collect());
	test.nib(&["update"]).expect("updates");
	assert!(!test.here.join("proj").exists());
	test.answers = Some(["y"].into_iter().collect());
	let cloned = test.nib(&["update"]).expect("updates");
	assert!(cloned.contains("cloned    proj"), "{cloned}");
	assert!(
		test.questions
			.iter()
			.any(|question| question.contains("proj from https://example.invalid/proj.git"))
	);
	let proj = test.here.join("proj");
	assert_eq!(
		fs::read_to_string(proj.join("README.md")).expect("reads"),
		"Proj\n"
	);
	assert_eq!(
		git(&test, &proj, &["config", "--get", "remote.origin.url"]),
		"https://example.invalid/proj.git"
	);
	assert_eq!(git(&test, &proj, &["branch", "--show-current"]), "main");
	// Not listed again.
	let again = test.nib(&["update"]).expect("updates");
	assert!(!again.contains("proj"), "{again}");
}

#[test]
fn a_branch_that_no_remote_has_is_cloned_as_the_remote_s_own() {
	let mut test = with_repository("tree git branch not pushed");
	let proj = test.here.join("proj");
	git(&test, &proj, &["switch", "--quiet", "--create", "feature"]);
	test.nib(&["commit", "-m", "Feature"]).expect("commits");
	as_never_cloned(&test);
	let cloned = test.nib(&["update", "--clone"]).expect("updates");
	assert!(cloned.contains("cloned    proj"), "{cloned}");
	assert_eq!(git(&test, &proj, &["branch", "--show-current"]), "main");
}

#[test]
fn a_clone_that_fails_leaves_nothing_behind() {
	let mut test = with_repository("tree git clone fails");
	let proj = test.here.join("proj");
	write(&test.here, "proj/local.md", "Only here\n");
	git(&test, &proj, &["add", "local.md"]);
	git(&test, &proj, &["commit", "--quiet", "-m", "Not pushed"]);
	test.nib(&["commit", "--pin", "proj"]).expect("pins");
	as_never_cloned(&test);
	let failed = test.nib(&["update", "--clone"]).expect("updates");
	assert!(failed.contains("could not clone proj"), "{failed}");
	assert!(!proj.exists());
}

#[test]
fn a_tree_keeps_its_env_as_a_secret_file_read_with_the_secrets_alone() {
	let mut test = library("tree secrets");
	let here = test.here.clone();
	write(&here, "notes.md", "# Notes\n");
	write(&here, ".env", "TOKEN=s3ntinel\n");
	// The library's first secret sets up its secrets, with a passphrase of their own.
	test.passphrases = ["secrets passphrase", "secrets passphrase"].into();
	let started = test.nib(&["init", "--yes"]).expect("starts");
	assert!(started.contains("secret    .env"), "{started}");
	assert!(started.contains("2 files"), "{started}");

	// A later edit is a secret file's, which needs the passphrase to commit.
	write(&here, ".env", "TOKEN=again\n");
	let status = test.nib(&["status"]).expect("status");
	assert!(status.contains("edited    .env"), "{status}");
	assert!(status.contains("secret    .env"), "{status}");
	assert!(matches!(
		test.nib(&["commit"]),
		Err(CliError::Node(NodeError::SecretsNeeded(_)))
	));
	let left = test
		.nib(&["commit", "--leave-secrets"])
		.expect("commits the rest");
	assert!(left.contains("left      .env"), "{left}");
	test.passphrases = ["secrets passphrase"].into();
	let committed = test.nib(&["commit"]).expect("commits");
	assert!(committed.contains("secret    .env"), "{committed}");

	// Its lines show only when asked for.
	write(&here, ".env", "TOKEN=third\n");
	let diff = test.nib(&["diff"]).expect("diffs");
	assert!(diff.contains("edited    .env"), "{diff}");
	assert!(!diff.contains("TOKEN"), "{diff}");
	test.passphrases = ["secrets passphrase"].into();
	let shown = test.nib(&["diff", "--secrets"]).expect("diffs");
	assert!(shown.contains("+TOKEN=third"), "{shown}");
	assert!(matches!(
		test.nib(&["blame", ".env"]),
		Err(CliError::Node(NodeError::SecretLines(_)))
	));

	// The status names the secret files, and the ordinary ones that look secret, when asked:
	// a file taken in as ordinary on purpose, such as a public certificate, is not named in
	// every status.
	write(&here, "public.pem", "public\n");
	test.nib(&["commit", "--ordinary", "public.pem", "--leave-secrets"])
		.expect("commits");
	let status = test.nib(&["status"]).expect("status");
	assert!(!status.contains("public.pem"), "{status}");
	let listed = test.nib(&["status", "--secrets"]).expect("lists");
	assert!(listed.contains("secret    .env"), "{listed}");
	assert!(
		listed.contains("ordinary  public.pem, which looks secret: nib commit --secret public.pem"),
		"{listed}"
	);
	let listed = test.json(&["status", "--secrets"]);
	assert_eq!(listed["secret"][0], ".env");
	assert_eq!(listed["looking_secret"][0], "public.pem");
}

#[test]
fn update_asks_before_it_moves_a_secret_file_where_another_device_moved_it() {
	let mut test = library("tree secret moved");
	let here = test.here.clone();
	write(&here, "notes.md", "# Notes\n");
	write(&here, ".env", "TOKEN=s3ntinel\n");
	test.passphrases = ["secrets passphrase", "secrets passphrase"].into();
	test.nib(&["init", "--yes"]).expect("starts");
	test.nib(&["edit", ".env", "std.tree-path=public/env.txt"])
		.expect("moves it, as another device may");
	let held = test.nib(&["update"]).expect("updates");
	assert!(held.contains("held      .env -> public/env.txt"), "{held}");
	assert!(here.join(".env").exists());
	// At a terminal, it asks for the secrets, and whether to write the move.
	test.answers = Some(["y"].into());
	test.passphrases = ["secrets passphrase"].into();
	let moved = test.nib(&["update"]).expect("updates");
	assert_eq!(test.questions.len(), 1, "{:?}", test.questions);
	assert!(
		moved.contains("moved     public/env.txt, from .env"),
		"{moved}"
	);
	assert!(here.join("public/env.txt").exists());
}