use super::*;
use tempfile::TempDir;
const PARENT_DIRS_STDERR: &str =
"fatal: not a git repository (or any of the parent directories): .git";
fn init_repo(dir: &std::path::Path) {
let out = std::process::Command::new("git")
.arg("-C")
.arg(dir)
.args(["init", "-q"])
.output()
.expect("git must be available to run the VCS-claim tests");
assert!(
out.status.success(),
"git init failed: {}",
String::from_utf8_lossy(&out.stderr)
);
}
fn git_add(repo: &std::path::Path, rel: &str) {
let out = std::process::Command::new("git")
.arg("-C")
.arg(repo)
.args(["add", "--force", rel])
.output()
.expect("spawn git add");
assert!(
out.status.success(),
"git add {rel} failed: {}",
String::from_utf8_lossy(&out.stderr)
);
}
fn repo_with_mixed_tier() -> (TempDir, std::path::PathBuf) {
let tmp = TempDir::new().expect("tempdir");
init_repo(tmp.path());
let tier = tmp.path().join(".claude").join("agents");
std::fs::create_dir_all(&tier).expect("create tier");
std::fs::write(tier.join("tracked.md"), "---\nname: tracked\n---\n").expect("write tracked");
std::fs::write(tier.join("loose.md"), "---\nname: loose\n---\n").expect("write loose");
git_add(tmp.path(), ".claude/agents/tracked.md");
(tmp, tier)
}
#[test]
fn probe_finds_tracked_files() {
let (_tmp, tier) = repo_with_mixed_tier();
let index = VcsIndex::probe(&tier);
assert_eq!(index.claim("tracked.md"), VcsClaim::Claimed);
}
#[test]
fn claim_of_an_untracked_file() {
let (_tmp, tier) = repo_with_mixed_tier();
let index = VcsIndex::probe(&tier);
assert_eq!(index.claim("loose.md"), VcsClaim::Unclaimed);
}
#[test]
fn claim_of_an_absent_file() {
let (_tmp, tier) = repo_with_mixed_tier();
let index = VcsIndex::probe(&tier);
assert_eq!(index.claim("never-existed.md"), VcsClaim::Unclaimed);
}
#[test]
fn claim_outside_a_repo_is_unclaimed() {
let tmp = TempDir::new().expect("tempdir");
let tier = tmp.path().join(".claude").join("agents");
std::fs::create_dir_all(&tier).expect("create tier");
std::fs::write(tier.join("qa.md"), "---\nname: qa\n---\n").expect("write");
let index = VcsIndex::probe(&tier);
assert_eq!(index.claim("qa.md"), VcsClaim::Unclaimed);
}
#[test]
fn probe_drops_nested_entries() {
let tmp = TempDir::new().expect("tempdir");
init_repo(tmp.path());
let tier = tmp.path().join(".claude").join("agents");
std::fs::create_dir_all(tier.join("sub")).expect("create sub");
std::fs::write(tier.join("sub").join("qa.md"), "---\nname: qa\n---\n").expect("write");
git_add(tmp.path(), ".claude/agents/sub/qa.md");
let index = VcsIndex::probe(&tier);
assert_eq!(
index.claim("qa.md"),
VcsClaim::Unclaimed,
"`sub/qa.md` must not claim the flat name `qa.md`"
);
}
#[test]
fn claim_is_unknown_when_git_is_unavailable() {
let index = VcsIndex {
state: IndexState::Unavailable,
};
assert_eq!(index.claim("qa.md"), VcsClaim::Unknown);
assert_eq!(index.claim("anything.md"), VcsClaim::Unknown);
}
#[test]
fn probe_of_a_missing_directory_does_not_panic() {
let tmp = TempDir::new().expect("tempdir");
let index = VcsIndex::probe(&tmp.path().join("nope").join("agents"));
assert_ne!(
index.claim("qa.md"),
VcsClaim::Claimed,
"a directory that does not exist can hold no tracked file"
);
}
#[test]
fn an_unreadable_repo_is_unknown_not_no_repo() {
let (tmp, tier) = repo_with_mixed_tier();
assert_eq!(
VcsIndex::probe(&tier).claim("tracked.md"),
VcsClaim::Claimed,
"the fixture must start healthy, or this test proves nothing"
);
let out = std::process::Command::new("git")
.arg("-C")
.arg(tmp.path())
.args(["config", "core.repositoryformatversion", "99"])
.output()
.expect("spawn git config");
assert!(out.status.success(), "could not break the repo");
assert_eq!(
VcsIndex::probe(&tier).claim("tracked.md"),
VcsClaim::Unknown,
"a repository git cannot read must be UNKNOWN — `NoRepo` would make its \
committed files sweepable"
);
}
#[test]
fn a_stale_worktree_pointer_is_unknown_not_no_repo() {
let tmp = TempDir::new().expect("tempdir");
let tier = tmp.path().join(".claude").join("agents");
std::fs::create_dir_all(&tier).expect("create tier");
std::fs::write(
tmp.path().join(".git"),
"gitdir: /nonexistent/parent/.git/worktrees/gone\n",
)
.expect("write stale pointer");
assert_eq!(
VcsIndex::probe(&tier).claim("qa.md"),
VcsClaim::Unknown,
"a stale worktree pointer must be UNKNOWN, not NoRepo"
);
}
#[test]
fn a_bare_repo_is_unknown() {
let tmp = TempDir::new().expect("tempdir");
let bare = tmp.path().join("bare.git");
let out = std::process::Command::new("git")
.args(["init", "-q", "--bare"])
.arg(&bare)
.output()
.expect("spawn git init --bare");
assert!(out.status.success(), "could not create a bare repo");
assert_eq!(VcsIndex::probe(&bare).claim("qa.md"), VcsClaim::Unknown);
}
#[test]
fn a_healthy_repo_still_answers_both_ways() {
let (_tmp, tier) = repo_with_mixed_tier();
let index = VcsIndex::probe(&tier);
assert_eq!(index.claim("tracked.md"), VcsClaim::Claimed);
assert_eq!(index.claim("loose.md"), VcsClaim::Unclaimed);
}
#[cfg(unix)]
struct ModeGuard(std::path::PathBuf);
#[cfg(unix)]
impl Drop for ModeGuard {
fn drop(&mut self) {
use std::os::unix::fs::PermissionsExt;
let _ = std::fs::set_permissions(&self.0, std::fs::Permissions::from_mode(0o755));
}
}
#[test]
#[cfg(unix)]
fn an_unreadable_git_dir_is_unknown_not_no_repo() {
use std::os::unix::fs::PermissionsExt;
let (tmp, tier) = repo_with_mixed_tier();
assert_eq!(
VcsIndex::probe(&tier).claim("tracked.md"),
VcsClaim::Claimed,
"the fixture must start healthy, or this test proves nothing"
);
let git_dir = tmp.path().join(".git");
let _guard = ModeGuard(git_dir.clone());
std::fs::set_permissions(&git_dir, std::fs::Permissions::from_mode(0o000)).expect("chmod .git");
assert_eq!(
VcsIndex::probe(&tier).claim("tracked.md"),
VcsClaim::Unknown,
"an unreadable .git must be UNKNOWN — git reports the SAME text it uses \
for a genuinely absent repository, so the message alone cannot decide"
);
}
#[test]
#[cfg(unix)]
fn an_unreadable_git_head_is_unknown_not_no_repo() {
use std::os::unix::fs::PermissionsExt;
let (tmp, tier) = repo_with_mixed_tier();
let head = tmp.path().join(".git").join("HEAD");
let _guard = ModeGuard(head.clone());
std::fs::set_permissions(&head, std::fs::Permissions::from_mode(0o000)).expect("chmod HEAD");
assert_eq!(
VcsIndex::probe(&tier).claim("tracked.md"),
VcsClaim::Unknown
);
}
#[test]
#[serial_test::serial]
fn a_ceiling_directory_is_unknown_not_no_repo() {
let (tmp, tier) = repo_with_mixed_tier();
assert_eq!(
VcsIndex::probe(&tier).claim("tracked.md"),
VcsClaim::Claimed,
"the fixture must start healthy, or this test proves nothing"
);
struct EnvGuard(Option<std::ffi::OsString>);
impl Drop for EnvGuard {
fn drop(&mut self) {
unsafe {
match self.0.take() {
Some(prev) => std::env::set_var("GIT_CEILING_DIRECTORIES", prev),
None => std::env::remove_var("GIT_CEILING_DIRECTORIES"),
}
}
}
}
let _guard = EnvGuard(std::env::var_os("GIT_CEILING_DIRECTORIES"));
unsafe {
std::env::set_var("GIT_CEILING_DIRECTORIES", tmp.path());
}
assert_eq!(
VcsIndex::probe(&tier).claim("tracked.md"),
VcsClaim::Unknown,
"a ceiling-blocked discovery must be UNKNOWN — the repository and its \
committed files are still there"
);
}
#[test]
fn a_stray_empty_git_dir_is_unknown() {
let tmp = TempDir::new().expect("tempdir");
let tier = tmp.path().join(".claude").join("agents");
std::fs::create_dir_all(&tier).expect("create tier");
std::fs::create_dir_all(tmp.path().join(".git")).expect("create stray .git");
assert_eq!(VcsIndex::probe(&tier).claim("qa.md"), VcsClaim::Unknown);
}
#[test]
fn an_unresolvable_relative_path_is_unknown() {
let relative = std::path::Path::new("no/such/relative/.claude/agents");
assert_eq!(
classify_failure(relative, PARENT_DIRS_STDERR),
IndexState::Unavailable
);
}
#[test]
fn a_truly_empty_directory_is_still_no_repo() {
let tmp = TempDir::new().expect("tempdir");
let tier = tmp.path().join(".claude").join("agents");
std::fs::create_dir_all(&tier).expect("create tier");
assert_eq!(
classify_failure(&tier, PARENT_DIRS_STDERR),
IndexState::NoRepo
);
assert_eq!(VcsIndex::probe(&tier).claim("qa.md"), VcsClaim::Unclaimed);
}
#[test]
fn a_non_matching_stderr_is_unavailable_without_stat() {
assert_eq!(
classify_failure(
std::path::Path::new("/"),
"fatal: detected dubious ownership in repository at '/x'"
),
IndexState::Unavailable
);
}
#[test]
#[cfg(unix)]
fn a_dangling_git_symlink_is_unknown_not_no_repo() {
let tmp = TempDir::new().expect("tempdir");
let tier = tmp.path().join(".claude").join("agents");
std::fs::create_dir_all(&tier).expect("create tier");
std::os::unix::fs::symlink("/nonexistent/gone", tmp.path().join(".git"))
.expect("dangling .git symlink");
assert!(tmp.path().join(".git").symlink_metadata().is_ok());
assert!(tmp.path().join(".git").metadata().is_err());
assert_eq!(
VcsIndex::probe(&tier).claim("qa.md"),
VcsClaim::Unknown,
"a broken .git symlink must refuse, not read as an absent repository"
);
}
#[test]
fn a_bogus_git_dir_message_is_unavailable_even_with_no_ancestor_git() {
let tmp = TempDir::new().expect("tempdir");
let tier = tmp.path().join(".claude").join("agents");
std::fs::create_dir_all(&tier).expect("create tier");
assert!(
!tier.ancestors().any(|p| p.join(".git").exists()),
"the fixture must have NO ancestor .git, or the witness masks the phrase"
);
assert_eq!(
classify_failure(&tier, "fatal: not a git repository: '/nonexistent/x'"),
IndexState::Unavailable,
"only the parenthesised form means genuine absence"
);
}
const MOUNT_BOUNDARY_STDERR: &str = "fatal: not a git repository (or any parent up to mount point /)\n\
Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).\n";
#[test]
fn classify_failure_accepts_the_mount_boundary_wording() {
let tmp = TempDir::new().expect("tempdir");
let tier = tmp.path().join(".claude").join("agents");
std::fs::create_dir_all(&tier).expect("create tier");
assert_eq!(
classify_failure(&tier, MOUNT_BOUNDARY_STDERR),
IndexState::NoRepo,
"the mount-boundary wording with no .git witness is a genuine absence"
);
for stderr in [
"fatal: not a git repository: (null)",
"fatal: not a git repository: '/nonexistent/x'",
"fatal: detected dubious ownership in repository at '/x'",
"fatal: cannot change to '/x': Permission denied",
] {
assert_eq!(
classify_failure(&tier, stderr),
IndexState::Unavailable,
"unrecognised failure must refuse, not read as no repository: {stderr:?}"
);
}
}
#[test]
fn the_mount_boundary_wording_under_a_real_repo_is_unavailable() {
let tmp = TempDir::new().expect("tempdir");
let tier = tmp.path().join(".claude").join("agents");
std::fs::create_dir_all(&tier).expect("create tier");
std::fs::write(tmp.path().join(".git"), "gitdir: /somewhere\n").expect("gitlink");
assert_eq!(
classify_failure(&tier, MOUNT_BOUNDARY_STDERR),
IndexState::Unavailable,
"an ancestor .git contradicts the boundary wording — never sweep a real repo"
);
}