treeboot-spec 0.16.0

Executable language-agnostic conformance specification for Treeboot.
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
use std::path::{Path, PathBuf};

use predicates::prelude::*;
use tempfile::TempDir;

use crate::cases::support::{
    assert_json_object_keys, canonical_path, git, git_worktree, parse_json, treeboot, write_file,
};
#[cfg(unix)]
use crate::cases::support::{symlink_dir, symlink_file};

pub(crate) struct ExtraWorktree {
    _parent: TempDir,
    path: PathBuf,
}

pub(crate) fn add_worktree(root: &Path, name: &str) -> ExtraWorktree {
    let parent = TempDir::new().expect("worktree parent should be created");
    let path = parent.path().join(name);
    let path_text = path.to_str().expect("test path should be UTF-8");
    git(
        &[
            "worktree",
            "add",
            "-b",
            &format!("treeboot-{name}"),
            path_text,
        ],
        root,
    );
    ExtraWorktree {
        _parent: parent,
        path,
    }
}

pub(crate) fn worktree_id(cwd: &Path) -> String {
    let output = treeboot()
        .args(["worktree", "id"])
        .current_dir(cwd)
        .output()
        .expect("treeboot should run");
    assert!(output.status.success(), "ID command should succeed");
    String::from_utf8(output.stdout)
        .expect("ID should be UTF-8")
        .trim()
        .to_owned()
}

pub(crate) struct BarePrimaryWorktree {
    _temp: TempDir,
    bare_path: PathBuf,
    linked_path: PathBuf,
}

pub(crate) fn bare_primary_worktree() -> BarePrimaryWorktree {
    let temp = TempDir::new().expect("tempdir should be created");
    let bare_path = temp.path().join("bare.git");
    let seed_path = temp.path().join("seed");
    let linked_path = temp.path().join("linked");
    let bare = bare_path.to_str().expect("bare path should be UTF-8");
    let linked = linked_path.to_str().expect("linked path should be UTF-8");

    git(&["init", "--bare", bare], temp.path());
    git(&["init", "seed"], temp.path());
    git(&["config", "user.name", "treeboot"], &seed_path);
    git(
        &["config", "user.email", "treeboot@example.invalid"],
        &seed_path,
    );
    git(&["config", "commit.gpgsign", "false"], &seed_path);
    write_file(&seed_path.join("README.md"), "treeboot bare repo\n");
    git(&["add", "README.md"], &seed_path);
    git(&["commit", "-m", "Initial commit"], &seed_path);
    git(&["remote", "add", "origin", bare], &seed_path);
    git(&["push", "origin", "HEAD:main"], &seed_path);
    git(&["symbolic-ref", "HEAD", "refs/heads/main"], &bare_path);
    git(
        &["worktree", "add", "-b", "linked-worktree", linked, "main"],
        &bare_path,
    );

    BarePrimaryWorktree {
        _temp: temp,
        bare_path,
        linked_path,
    }
}

pub(crate) fn worktree_id_and_slug_should_print_bare_text_and_exact_structured_shapes() {
    let repo = git_worktree();
    write_file(
        &repo.worktree_path().join(".treeboot.toml"),
        "worktree_id = { length = 8 }\n\
         worktree_slug = { max_length = 20, separator = \"_\" }\n",
    );

    let text = treeboot()
        .args(["worktree", "id"])
        .current_dir(repo.worktree_path())
        .assert()
        .success()
        .stderr(predicate::str::is_empty())
        .get_output()
        .stdout
        .clone();
    let id = String::from_utf8(text).expect("text ID should be UTF-8");
    assert!(id.ends_with('\n'));
    assert!(!id.trim().contains(char::is_whitespace));
    assert_eq!(id.trim().len(), 8);

    let json = treeboot()
        .args(["worktree", "id", "--json"])
        .current_dir(repo.worktree_path())
        .assert()
        .success()
        .stderr(predicate::str::is_empty())
        .get_output()
        .stdout
        .clone();
    let json = parse_json(json, "worktree id");
    assert_json_object_keys(&json, &["id"]);
    assert_eq!(json["id"], id.trim());

    let env_json = treeboot()
        .args(["env", "--json"])
        .current_dir(repo.worktree_path())
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();
    let env_json = parse_json(env_json, "configured env");
    assert_eq!(json["id"], env_json["TREEBOOT_WORKTREE_ID"]);

    let yaml = treeboot()
        .args(["worktree", "id", "--yaml"])
        .current_dir(repo.worktree_path())
        .assert()
        .success()
        .stderr(predicate::str::is_empty())
        .get_output()
        .stdout
        .clone();
    let yaml: serde_json::Value = yaml_serde::from_slice(&yaml)
        .unwrap_or_else(|error| panic!("worktree ID YAML should parse: {error}"));
    assert_json_object_keys(&yaml, &["id"]);
    assert_eq!(yaml["id"], id.trim());

    let slug = treeboot()
        .args(["worktree", "slug"])
        .current_dir(repo.worktree_path())
        .assert()
        .success()
        .stderr(predicate::str::is_empty())
        .get_output()
        .stdout
        .clone();
    let slug = String::from_utf8(slug).expect("text slug should be UTF-8");
    assert!(slug.trim().starts_with("linked_"));
    assert!(slug.trim().ends_with(id.trim()));
    assert!(slug.trim().len() <= 20);

    let slug_json = treeboot()
        .args(["worktree", "slug", "--json"])
        .current_dir(repo.worktree_path())
        .assert()
        .success()
        .stderr(predicate::str::is_empty())
        .get_output()
        .stdout
        .clone();
    let slug_json = parse_json(slug_json, "worktree slug");
    assert_json_object_keys(&slug_json, &["slug"]);
    assert_eq!(slug_json["slug"], slug.trim());
    assert_eq!(slug_json["slug"], env_json["TREEBOOT_WORKTREE_SLUG"]);

    treeboot()
        .args(["worktree", "slug", "--yaml"])
        .current_dir(repo.worktree_path())
        .assert()
        .success()
        .stderr(predicate::str::is_empty())
        .stdout(predicate::str::contains(format!("slug: {}", slug.trim())));
}

pub(crate) fn worktree_path_and_list_should_use_candidate_local_config_and_contract_order() {
    let repo = git_worktree();
    let second = add_worktree(repo.root_path(), "zeta");
    write_file(
        &repo.root_path().join(".treeboot.toml"),
        "worktree_id = { length = 5 }\nworktree_slug = { separator = \"_\" }\n",
    );
    write_file(
        &repo.worktree_path().join(".treeboot.toml"),
        "worktree_id = { length = 7 }\nworktree_slug = { separator = \"_\" }\n",
    );
    write_file(
        &second.path.join(".treeboot.toml"),
        "worktree_id = { length = 9 }\nworktree_slug = { separator = \"_\" }\n",
    );

    let root_id = worktree_id(repo.root_path());
    let linked_id = worktree_id(repo.worktree_path());
    let second_id = worktree_id(&second.path);
    assert_eq!(root_id.len(), 5);
    assert_eq!(linked_id.len(), 7);
    assert_eq!(second_id.len(), 9);

    for (id, path) in [
        (&root_id, repo.root_path()),
        (&linked_id, repo.worktree_path()),
        (&second_id, second.path.as_path()),
    ] {
        treeboot()
            .args(["worktree", "path", id])
            .current_dir(repo.worktree_path())
            .assert()
            .success()
            .stderr(predicate::str::is_empty())
            .stdout(format!("{}\n", canonical_path(path).display()));
    }

    let json = treeboot()
        .args(["worktree", "list", "--json"])
        .current_dir(repo.worktree_path())
        .assert()
        .success()
        .stderr(predicate::str::is_empty())
        .get_output()
        .stdout
        .clone();
    let json = parse_json(json, "worktree list");
    assert_json_object_keys(&json, &["worktrees"]);
    let entries = json["worktrees"]
        .as_array()
        .expect("worktrees should be an array");
    assert_eq!(entries.len(), 3);
    for entry in entries {
        assert_json_object_keys(entry, &["id", "path", "slug"]);
        assert!(
            entry["slug"]
                .as_str()
                .expect("slug should be a string")
                .ends_with(entry["id"].as_str().expect("ID should be a string"))
        );
    }
    assert_eq!(
        entries[0]["path"],
        canonical_path(repo.root_path()).display().to_string()
    );
    let remaining = entries[1..]
        .iter()
        .map(|entry| entry["path"].as_str().expect("path should be a string"))
        .collect::<Vec<_>>();
    assert!(remaining.windows(2).all(|paths| paths[0] <= paths[1]));

    treeboot()
        .args(["worktree", "list", "--format", "text"])
        .current_dir(repo.worktree_path())
        .assert()
        .success()
        .stderr(predicate::str::is_empty())
        .stdout(predicate::str::starts_with("ID"))
        .stdout(predicate::str::contains("SLUG"))
        .stdout(predicate::str::contains("PATH"))
        .stdout(predicate::str::contains(root_id))
        .stdout(predicate::str::contains(linked_id))
        .stdout(predicate::str::contains(second_id));

    treeboot()
        .args(["worktree", "list", "--yaml"])
        .current_dir(repo.worktree_path())
        .assert()
        .success()
        .stderr(predicate::str::is_empty())
        .stdout(predicate::str::starts_with("worktrees:"))
        .stdout(predicate::str::contains("id:"))
        .stdout(predicate::str::contains("slug:"))
        .stdout(predicate::str::contains("path:"));
}

pub(crate) fn worktree_list_should_keep_main_first_when_a_linked_path_sorts_before_it() {
    let temp = TempDir::new().expect("tempdir should be created");
    let root = temp.path().join("z-root");
    let linked = temp.path().join("a-linked");
    std::fs::create_dir(&root).expect("root should be created");
    git(&["init"], &root);
    git(&["config", "user.name", "treeboot"], &root);
    git(&["config", "user.email", "treeboot@example.invalid"], &root);
    git(&["config", "commit.gpgsign", "false"], &root);
    write_file(&root.join("README.md"), "treeboot ordering repo\n");
    git(&["add", "README.md"], &root);
    git(&["commit", "-m", "Initial commit"], &root);
    git(
        &[
            "worktree",
            "add",
            "-b",
            "treeboot-ordering",
            linked.to_str().expect("linked path should be UTF-8"),
        ],
        &root,
    );
    let root = canonical_path(&root);
    let linked = canonical_path(&linked);
    assert!(linked < root, "fixture should sort linked path before root");

    let list = treeboot()
        .args(["worktree", "list", "--format", "json"])
        .current_dir(&linked)
        .assert()
        .success()
        .stderr(predicate::str::is_empty())
        .get_output()
        .stdout
        .clone();
    let list = parse_json(list, "main-first list");
    let entries = list["worktrees"]
        .as_array()
        .expect("worktrees should be an array");

    assert_eq!(entries[0]["path"], root.display().to_string());
    assert_eq!(entries[1]["path"], linked.display().to_string());
}

pub(crate) fn worktree_commands_should_support_a_bare_primary_repository() {
    let repo = bare_primary_worktree();
    let linked = canonical_path(&repo.linked_path);
    let bare = canonical_path(&repo.bare_path);
    let id = worktree_id(&repo.linked_path);

    let env = treeboot()
        .args(["env", "--json"])
        .current_dir(&repo.linked_path)
        .assert()
        .success()
        .stderr(predicate::str::is_empty())
        .get_output()
        .stdout
        .clone();
    let env = parse_json(env, "bare primary env");
    assert_eq!(env["TREEBOOT_ROOT_PATH"], bare.display().to_string());

    let list = treeboot()
        .args(["worktree", "list", "--json"])
        .current_dir(&repo.linked_path)
        .assert()
        .success()
        .stderr(predicate::str::is_empty())
        .get_output()
        .stdout
        .clone();
    let list = parse_json(list, "bare primary list");
    let entries = list["worktrees"]
        .as_array()
        .expect("worktrees should be an array");
    assert_eq!(entries.len(), 1);
    assert_eq!(entries[0]["id"], id);
    assert_eq!(entries[0]["path"], linked.display().to_string());
    assert_ne!(entries[0]["path"], bare.display().to_string());

    treeboot()
        .args(["worktree", "path", &id])
        .current_dir(&repo.linked_path)
        .assert()
        .success()
        .stderr(predicate::str::is_empty())
        .stdout(format!("{}\n", linked.display()));
}

pub(crate) fn worktree_path_should_emit_exact_structured_shapes() {
    let repo = git_worktree();
    let id = worktree_id(repo.worktree_path());
    let path = canonical_path(repo.worktree_path());
    let nested = repo.worktree_path().join("nested");
    std::fs::create_dir(&nested).expect("nested directory should be created");

    let json = treeboot()
        .args(["worktree", "path", &id, "--format", "json"])
        .current_dir(&nested)
        .assert()
        .success()
        .stderr(predicate::str::is_empty())
        .get_output()
        .stdout
        .clone();
    let json = parse_json(json, "worktree path");
    assert_json_object_keys(&json, &["id", "path"]);
    assert_eq!(json["id"], id);
    assert_eq!(json["path"], path.display().to_string());

    let yaml = treeboot()
        .args(["worktree", "path", &id, "--format", "yaml"])
        .current_dir(repo.root_path())
        .assert()
        .success()
        .stderr(predicate::str::is_empty())
        .get_output()
        .stdout
        .clone();
    let yaml: serde_json::Value = yaml_serde::from_slice(&yaml)
        .unwrap_or_else(|error| panic!("worktree path YAML should parse: {error}"));
    assert_json_object_keys(&yaml, &["id", "path"]);
    assert_eq!(yaml["id"], id);
    assert_eq!(yaml["path"], path.display().to_string());
}

pub(crate) fn worktree_inspection_should_use_defaults_without_config() {
    let repo = git_worktree();
    let id = worktree_id(repo.worktree_path());

    assert_eq!(id.len(), 6);
    assert!(id.bytes().all(|byte| {
        byte.is_ascii_digit()
            || matches!(
                byte,
                b'a'..=b'h' | b'j'..=b'k' | b'm'..=b'n' | b'p'..=b't' | b'v'..=b'z'
            )
    }));
}

pub(crate) fn explicit_actual_worktree_identity_should_match_implicit_and_local_config() {
    let repo = git_worktree();
    write_file(
        &repo.worktree_path().join(".treeboot.toml"),
        "worktree_id = { length = 10 }\n\
         worktree_slug = { max_length = 24, separator = \"_\" }\n",
    );

    for command in ["id", "slug"] {
        let implicit = treeboot()
            .args(["worktree", command])
            .current_dir(repo.worktree_path())
            .output()
            .expect("implicit identity command should run");
        assert!(
            implicit.status.success(),
            "implicit {command} should succeed"
        );

        let explicit = treeboot()
            .args(["worktree", command])
            .arg(repo.worktree_path())
            .current_dir(repo.root_path())
            .output()
            .expect("explicit identity command should run");
        assert!(
            explicit.status.success(),
            "explicit {command} should succeed"
        );
        assert_eq!(explicit.stdout, implicit.stdout);
    }
}

pub(crate) fn explicit_ordinary_directory_identity_should_work_outside_git_and_use_local_config() {
    let parent = TempDir::new().expect("tempdir should be created");
    let target = parent.path().join("Feature Login");
    std::fs::create_dir(&target).expect("target should be created");
    write_file(
        &target.join(".treeboot.toml"),
        "worktree_id = { length = 9 }\n\
         worktree_slug = { max_length = 22, separator = \"_\" }\n",
    );

    let id = treeboot()
        .args(["worktree", "id"])
        .arg(&target)
        .current_dir(parent.path())
        .assert()
        .success()
        .stderr(predicate::str::is_empty())
        .get_output()
        .stdout
        .clone();
    let id = String::from_utf8(id).expect("ID should be UTF-8");
    assert_eq!(id.trim().len(), 9);

    let slug = treeboot()
        .args(["worktree", "slug"])
        .arg(&target)
        .current_dir(parent.path())
        .assert()
        .success()
        .stderr(predicate::str::is_empty())
        .get_output()
        .stdout
        .clone();
    let slug = String::from_utf8(slug).expect("slug should be UTF-8");
    assert!(slug.trim().starts_with("feature_logi_"));
    assert!(slug.trim().ends_with(id.trim()));
    assert_eq!(slug.trim().len(), 22);
}

pub(crate) fn explicit_identity_relative_absolute_dot_and_dotdot_should_normalize_to_one_target() {
    let parent = TempDir::new().expect("tempdir should be created");
    let real_parent = parent.path().join("real");
    let target = real_parent.join("target");
    std::fs::create_dir_all(&target).expect("target should be created");

    for command in ["id", "slug"] {
        let absolute = worktree_identity_for_path(command, parent.path(), &target);
        let relative = worktree_identity_for_path(command, parent.path(), Path::new("real/target"));
        let dotted =
            worktree_identity_for_path(command, parent.path(), Path::new("./real/./target"));
        let parented =
            worktree_identity_for_path(command, parent.path(), Path::new("real/other/../target"));

        assert_eq!(absolute, relative);
        assert_eq!(absolute, dotted);
        assert_eq!(absolute, parented);
    }
}

#[cfg(unix)]
pub(crate) fn explicit_identity_symlink_alias_should_normalize_to_target() {
    let parent = TempDir::new().expect("tempdir should be created");
    let real_parent = parent.path().join("real");
    let target = real_parent.join("target");
    let alias = parent.path().join("alias");
    std::fs::create_dir_all(&target).expect("target should be created");
    symlink_dir(&real_parent, &alias);

    for command in ["id", "slug"] {
        let expected = worktree_identity_for_path(command, parent.path(), &target);
        let aliased = worktree_identity_for_path(command, parent.path(), Path::new("alias/target"));

        assert_eq!(expected, aliased);
    }
}

pub(crate) fn explicit_nonexistent_identity_should_survive_ordinary_directory_creation() {
    let parent = TempDir::new().expect("tempdir should be created");
    let target = parent.path().join("future").join("worktree");

    let before_id = worktree_identity_for_path("id", parent.path(), &target);
    let before_slug = worktree_identity_for_path("slug", parent.path(), &target);
    std::fs::create_dir_all(&target).expect("target should be created");
    let after_id = worktree_identity_for_path("id", parent.path(), &target);
    let after_slug = worktree_identity_for_path("slug", parent.path(), &target);

    assert_eq!(before_id, after_id);
    assert_eq!(before_slug, after_slug);
}

pub(crate) fn explicit_nonexistent_git_targets_should_keep_fallback_slug_after_creation() {
    let repo = git_worktree();
    let targets = [
        repo.worktree_path().join("future").join("deadbeef"),
        repo.worktree_path().join("future").join("💥"),
    ];

    for target in targets {
        let before_id = worktree_identity_for_path("id", repo.worktree_path(), &target);
        let before_slug = worktree_identity_for_path("slug", repo.worktree_path(), &target);
        std::fs::create_dir_all(&target).expect("target should be created");
        let after_id = worktree_identity_for_path("id", repo.worktree_path(), &target);
        let after_slug = worktree_identity_for_path("slug", repo.worktree_path(), &target);

        assert_eq!(before_id, after_id);
        assert_eq!(before_slug, after_slug, "target: {}", target.display());
    }
}

pub(crate) fn explicit_git_subdirectory_should_keep_exact_identity_and_config_scope() {
    let repo = git_worktree();
    write_file(
        &repo.root_path().join(".treeboot.toml"),
        "worktree_id = { length = 12 }\n",
    );
    write_file(
        &repo.worktree_path().join(".treeboot.toml"),
        "worktree_id = { length = 10 }\n",
    );
    let target = repo.worktree_path().join("nested").join("target");
    std::fs::create_dir_all(&target).expect("target should be created");

    let worktree = worktree_identity_for_path("id", repo.worktree_path(), repo.worktree_path());
    let subdirectory = worktree_identity_for_path("id", repo.worktree_path(), &target);

    assert_eq!(worktree.len(), 10);
    assert_eq!(subdirectory.len(), 6);
    assert_ne!(subdirectory, worktree);
}

pub(crate) fn explicit_regular_file_and_invalid_config_should_fail_before_stdout() {
    let parent = TempDir::new().expect("tempdir should be created");
    let file = parent.path().join("file");
    write_file(&file, "not a directory\n");

    for command in ["id", "slug"] {
        treeboot()
            .args(["worktree", command])
            .arg(&file)
            .current_dir(parent.path())
            .assert()
            .failure()
            .stdout(predicate::str::is_empty())
            .stderr(predicate::str::contains("is not a directory"));
    }

    let invalid = parent.path().join("invalid");
    std::fs::create_dir(&invalid).expect("invalid target should be created");
    write_file(
        &invalid.join(".treeboot.toml"),
        "worktree_id = { length = 0 }\n",
    );
    for command in ["id", "slug"] {
        treeboot()
            .args(["worktree", command])
            .arg(&invalid)
            .current_dir(parent.path())
            .assert()
            .failure()
            .stdout(predicate::str::is_empty())
            .stderr(predicate::str::contains("must be between 1 and 52"));
    }

    let unrelated = parent.path().join("unrelated-invalid");
    std::fs::create_dir(&unrelated).expect("invalid target should be created");
    write_file(
        &unrelated.join(".treeboot.toml"),
        "commands = [{ args = [\"orphan\"] }]\n",
    );
    for command in ["id", "slug"] {
        treeboot()
            .args(["worktree", command])
            .arg(&unrelated)
            .current_dir(parent.path())
            .assert()
            .failure()
            .stdout(predicate::str::is_empty())
            .stderr(predicate::str::contains(
                "command is missing required `run` or `program`",
            ));
    }
}

#[cfg(unix)]
pub(crate) fn explicit_dangling_symlink_should_fail_before_stdout() {
    let parent = TempDir::new().expect("tempdir should be created");
    let dangling = parent.path().join("dangling");
    symlink_file(parent.path().join("missing"), &dangling);

    for command in ["id", "slug"] {
        treeboot()
            .args(["worktree", command])
            .arg(&dangling)
            .current_dir(parent.path())
            .assert()
            .failure()
            .stdout(predicate::str::is_empty())
            .stderr(predicate::str::contains("is not a directory"));
    }
}

pub(crate) fn explicit_empty_path_should_be_a_usage_error_without_stdout() {
    for command in ["id", "slug"] {
        treeboot()
            .args(["worktree", command, ""])
            .assert()
            .code(2)
            .stdout(predicate::str::is_empty())
            .stderr(predicate::str::contains("PATH must not be empty"));
    }
}

#[cfg(windows)]
pub(crate) fn explicit_windows_special_relative_paths_should_fail_before_stdout() {
    let parent = TempDir::new().expect("tempdir should be created");
    for (path, reason) in [
        (r"C:relative", "drive-relative paths are not supported"),
        (
            r"\relative",
            "root-relative paths without a drive or share are not supported",
        ),
    ] {
        for command in ["id", "slug"] {
            treeboot()
                .args(["worktree", command, path])
                .current_dir(parent.path())
                .assert()
                .failure()
                .stdout(predicate::str::is_empty())
                .stderr(predicate::str::contains(reason));
        }
    }
}

#[cfg(target_os = "linux")]
pub(crate) fn explicit_identity_path_parser_should_preserve_native_non_utf8_input() {
    use std::ffi::OsString;
    use std::os::unix::ffi::OsStringExt;

    let parent = TempDir::new().expect("tempdir should be created");
    let target = parent
        .path()
        .join(OsString::from_vec(b"target-\xff".to_vec()));
    std::fs::create_dir(&target).expect("target should be created");

    for command in ["id", "slug"] {
        treeboot()
            .args(["worktree", command])
            .arg(&target)
            .current_dir(parent.path())
            .assert()
            .success()
            .stderr(predicate::str::is_empty())
            .stdout(predicate::str::is_empty().not());
    }
}

pub(crate) fn explicit_identity_should_ignore_ambient_discovery_overrides() {
    let parent = TempDir::new().expect("tempdir should be created");
    let target = parent.path().join("target");
    std::fs::create_dir(&target).expect("target should be created");
    for command in ["id", "slug"] {
        let expected = worktree_identity_for_path(command, parent.path(), &target);

        treeboot()
            .args(["worktree", command])
            .arg(&target)
            .env("TREEBOOT_ROOT_PATH", parent.path().join("missing-root"))
            .env("CONDUCTOR_DEFAULT_BRANCH", "ambient-branch")
            .current_dir(parent.path())
            .assert()
            .success()
            .stderr(predicate::str::is_empty())
            .stdout(format!("{expected}\n"));
    }
}

pub(crate) fn worktree_identity_for_path(command: &str, cwd: &Path, path: &Path) -> String {
    let output = treeboot()
        .args(["worktree", command])
        .arg(path)
        .current_dir(cwd)
        .output()
        .expect("explicit identity command should run");
    assert!(
        output.status.success(),
        "explicit {command} should succeed: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    String::from_utf8(output.stdout)
        .expect("identity should be UTF-8")
        .trim()
        .to_owned()
}

pub(crate) fn worktree_list_should_skip_stale_registered_paths() {
    let repo = git_worktree();
    let stale_parent = TempDir::new().expect("stale parent should be created");
    let stale_path = stale_parent.path().join("stale");
    let stale_text = stale_path.to_str().expect("stale path should be UTF-8");
    git(
        &["worktree", "add", "-b", "treeboot-stale", stale_text],
        repo.root_path(),
    );
    std::fs::remove_dir_all(&stale_path).expect("stale worktree should be removed from disk");

    let root = canonical_path(repo.root_path()).display().to_string();
    treeboot()
        .args(["worktree", "list"])
        .current_dir(repo.root_path())
        .assert()
        .success()
        .stderr(predicate::str::is_empty())
        .stdout(predicate::str::contains("stale").not())
        .stdout(predicate::str::contains(root));
}

// Git on macOS rejects non-UTF-8 worktree administrative directory names.
#[cfg(target_os = "linux")]
pub(crate) fn worktree_text_paths_should_preserve_native_bytes_and_structured_fail_atomically() {
    use std::ffi::OsString;
    use std::os::unix::ffi::{OsStrExt, OsStringExt};

    let repo = git_worktree();
    let parent = TempDir::new().expect("non-UTF-8 parent should be created");
    let path = parent
        .path()
        .join(OsString::from_vec(b"non-utf8-\xff".to_vec()));
    let output = crate::cases::support::host_process("git")
        .args(["worktree", "add", "-b", "treeboot-non-utf8"])
        .arg(&path)
        .current_dir(repo.root_path())
        .output()
        .expect("git should run");
    crate::cases::support::assert_fixture_process_success("git worktree add", &output);
    let path = canonical_path(&path);
    let id = worktree_id(&path);
    let mut expected_path = path.as_os_str().as_bytes().to_vec();
    expected_path.push(b'\n');

    let path_output = treeboot()
        .args(["worktree", "path", &id])
        .current_dir(repo.root_path())
        .assert()
        .success()
        .stderr(predicate::str::is_empty())
        .get_output()
        .stdout
        .clone();
    assert_eq!(path_output, expected_path);

    let list_output = treeboot()
        .args(["worktree", "list"])
        .current_dir(repo.root_path())
        .assert()
        .success()
        .stderr(predicate::str::is_empty())
        .get_output()
        .stdout
        .clone();
    assert!(
        list_output
            .windows(expected_path.len())
            .any(|window| window == expected_path),
        "list should contain the exact native path bytes"
    );

    for args in [
        vec!["worktree", "path", &id, "--json"],
        vec!["worktree", "path", &id, "--yaml"],
        vec!["worktree", "list", "--format", "json"],
        vec!["worktree", "list", "--format", "yaml"],
    ] {
        treeboot()
            .args(args)
            .current_dir(repo.root_path())
            .assert()
            .failure()
            .stdout(predicate::str::is_empty())
            .stderr(predicate::str::is_empty().not());
    }
}

pub(crate) fn worktree_path_should_report_missing_and_ambiguous_ids_without_stdout() {
    let repo = git_worktree();

    treeboot()
        .args(["worktree", "path", "missing-id"])
        .current_dir(repo.root_path())
        .assert()
        .failure()
        .stdout(predicate::str::is_empty())
        .stderr(predicate::str::contains("no worktree found"));

    let second = add_worktree(repo.root_path(), "collision");
    for path in [repo.worktree_path(), second.path.as_path()] {
        write_file(
            &path.join(".treeboot.toml"),
            "worktree_id = { length = 1 }\nworktree_slug = { max_length = 3 }\n",
        );
    }

    // A one-character hash has only 32 values. Add linked worktrees until two
    // complete identifiers collide, then assert lookup reports every path.
    let mut candidates = vec![repo.worktree_path().to_path_buf(), second.path];
    let mut parents = Vec::new();
    let (collision_id, collision_paths) = loop {
        let mut by_id = std::collections::BTreeMap::<String, Vec<PathBuf>>::new();
        for path in &candidates {
            by_id
                .entry(worktree_id(path))
                .or_default()
                .push(path.clone());
        }
        if let Some((id, paths)) = by_id.into_iter().find(|(_, paths)| paths.len() > 1) {
            break (id, paths);
        }
        let name = format!("collision-{}", candidates.len());
        let extra = add_worktree(repo.root_path(), &name);
        write_file(
            &extra.path.join(".treeboot.toml"),
            "worktree_id = { length = 1 }\nworktree_slug = { max_length = 3 }\n",
        );
        candidates.push(extra.path.clone());
        parents.push(extra);
        assert!(candidates.len() <= 34, "pigeonhole collision should exist");
    };

    let output = treeboot()
        .args(["worktree", "path", &collision_id])
        .current_dir(repo.root_path())
        .assert()
        .failure()
        .stdout(predicate::str::is_empty())
        .stderr(predicate::str::contains("ambiguous worktree ID"))
        .get_output()
        .stderr
        .clone();
    let stderr = String::from_utf8(output).expect("stderr should be UTF-8");
    for path in &collision_paths {
        assert!(
            stderr.contains(&format!("{:?}", canonical_path(path))),
            "stderr should report colliding path"
        );
    }

    let list = treeboot()
        .args(["worktree", "list", "--json"])
        .current_dir(repo.root_path())
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();
    let list = parse_json(list, "colliding worktree list");
    let duplicate_count = list["worktrees"]
        .as_array()
        .expect("worktrees should be an array")
        .iter()
        .filter(|entry| entry["id"] == collision_id)
        .count();
    assert_eq!(duplicate_count, collision_paths.len());
}

pub(crate) fn malformed_sibling_config_should_fail_atomically_with_candidate_path() {
    let repo = git_worktree();
    let sibling = add_worktree(repo.root_path(), "malformed");
    write_file(
        &sibling.path.join(".treeboot.toml"),
        "worktree_id = { length = 0 }\n",
    );

    for args in [
        vec!["worktree", "list"],
        vec!["worktree", "path", "missing"],
    ] {
        treeboot()
            .args(args)
            .current_dir(repo.root_path())
            .assert()
            .failure()
            .stdout(predicate::str::is_empty())
            .stderr(predicate::str::contains(format!(
                "{:?}",
                canonical_path(&sibling.path)
            )))
            .stderr(predicate::str::contains("length"));
    }
}

pub(crate) fn worktree_commands_should_fail_outside_git() {
    let dir = TempDir::new().expect("tempdir should be created");

    for args in [
        vec!["worktree", "id"],
        vec!["worktree", "slug"],
        vec!["worktree", "list"],
        vec!["worktree", "path", "id"],
    ] {
        treeboot()
            .args(args)
            .current_dir(dir.path())
            .assert()
            .failure()
            .stdout(predicate::str::is_empty())
            .stderr(predicate::str::contains("not inside a Git worktree"));
    }
}

pub(crate) fn worktree_commands_should_honor_recognized_ambient_environment() {
    let repo = git_worktree();
    let missing_root = repo.root_path().join("missing-root");

    for args in [
        vec!["worktree", "id"],
        vec!["worktree", "slug"],
        vec!["worktree", "list"],
        vec!["worktree", "path", "id"],
    ] {
        treeboot()
            .args(args)
            .env("TREEBOOT_ROOT_PATH", &missing_root)
            .current_dir(repo.worktree_path())
            .assert()
            .failure()
            .stdout(predicate::str::is_empty())
            .stderr(predicate::str::contains("missing-root"));
    }
}

pub(crate) fn worktree_nested_help_and_version_should_be_exposed() {
    treeboot()
        .arg("worktree")
        .assert()
        .code(2)
        .stdout(predicate::str::is_empty())
        .stderr(predicate::str::is_empty().not());

    treeboot()
        .args(["worktree", "--help"])
        .assert()
        .success()
        .stdout(predicate::str::contains("id"))
        .stdout(predicate::str::contains("slug"))
        .stdout(predicate::str::contains("path"))
        .stdout(predicate::str::contains("list"));

    treeboot()
        .args(["worktree", "id", "--help"])
        .assert()
        .success()
        .stdout(predicate::str::contains("[PATH]"))
        .stdout(predicate::str::contains("--format"))
        .stdout(predicate::str::contains("--json"))
        .stdout(predicate::str::contains("--yaml"));

    treeboot()
        .args(["worktree", "slug", "--help"])
        .assert()
        .success()
        .stdout(predicate::str::contains("[PATH]"))
        .stdout(predicate::str::contains("--format"));

    for flag in ["--version", "-V"] {
        let output = treeboot()
            .args(["worktree", flag])
            .assert()
            .success()
            .get_output()
            .stdout
            .clone();
        let text = std::str::from_utf8(&output).expect("worktree version should be valid UTF-8");
        assert!(
            text.starts_with(&format!(
                "treeboot-worktree {} (spec ",
                crate::cases::support::candidate_package_version()
            )),
            "unexpected worktree version output: {text:?}"
        );
        assert!(
            text.ends_with(")\n") && !text.ends_with("(spec )\n"),
            "unexpected worktree version output: {text:?}"
        );
    }
}

pub(crate) fn worktree_version_flags_should_declare_suite_spec() {
    for flag in ["--version", "-V"] {
        treeboot()
            .args(["worktree", flag])
            .assert()
            .success()
            .stdout(predicate::str::contains(format!(
                "(spec {})",
                crate::SPEC_VERSION
            )));
    }
}