use super::*;
use std::process::Command;
fn init_git_repo(dir: &Path) {
let ok = Command::new("git")
.arg("-C")
.arg(dir)
.arg("init")
.output()
.expect("git init must run")
.status
.success();
assert!(ok, "git init must succeed");
}
#[test]
fn resolve_none_is_projectless() {
let binding = ProjectBinding::resolve(None).expect("projectless must resolve, never error");
assert_eq!(binding, ProjectBinding::None);
assert!(!binding.is_bound(), "projectless must not be bound");
assert!(!binding.is_git(), "projectless has no git affordances");
assert_eq!(binding.root(), None, "projectless has no root");
}
#[test]
fn resolve_plain_dir_binds_as_directory() {
let tmp = tempfile::tempdir().expect("tempdir");
let binding =
ProjectBinding::resolve(Some(tmp.path().to_path_buf())).expect("a plain dir must bind");
assert!(
matches!(binding, ProjectBinding::Directory(_)),
"a non-git dir must bind as Directory, got {binding:?}"
);
}
#[test]
fn resolve_git_repo_binds_as_git_repo() {
let tmp = tempfile::tempdir().expect("tempdir");
init_git_repo(tmp.path());
let binding =
ProjectBinding::resolve(Some(tmp.path().to_path_buf())).expect("a git repo must bind");
assert!(
matches!(binding, ProjectBinding::GitRepo(_)),
"a git worktree must bind as GitRepo, got {binding:?}"
);
assert!(binding.is_git(), "a git repo must offer git affordances");
assert!(binding.is_bound());
assert!(binding.should_index());
}
#[test]
fn non_git_dir_is_bound_and_indexes() {
let tmp = tempfile::tempdir().expect("tempdir");
let binding = ProjectBinding::resolve(Some(tmp.path().to_path_buf())).expect("must bind");
assert!(
binding.is_bound(),
"a non-git dir MUST bind — binding is not gated on .git (#2728/#2747)"
);
assert!(
binding.should_index(),
"a non-git dir MUST index — indexing is not gated on .git (AC-2.3)"
);
assert!(
!binding.is_git(),
"a non-git dir must NOT claim git affordances — they are hidden, not faked"
);
assert_ne!(
binding,
ProjectBinding::None,
"a non-git dir is NOT projectless"
);
}
#[test]
fn projectless_does_not_index() {
assert!(
!ProjectBinding::None.should_index(),
"projectless has no project to index"
);
}
#[test]
fn resolve_rejects_missing_path() {
let missing = PathBuf::from("/definitely/not/a/real/path/xyzzy-42");
let err = ProjectBinding::resolve(Some(missing.clone()))
.expect_err("a nonexistent path must not resolve");
assert_eq!(err, BindingError::NotFound(missing));
}
#[test]
fn resolve_rejects_file_path() {
let tmp = tempfile::tempdir().expect("tempdir");
let file = tmp.path().join("not-a-dir.txt");
std::fs::write(&file, "x").expect("write");
let err = ProjectBinding::resolve(Some(file)).expect_err("a file path must not resolve");
assert!(
matches!(err, BindingError::NotADirectory(_)),
"expected NotADirectory, got {err:?}"
);
}
#[test]
fn root_is_none_only_when_projectless() {
let tmp = tempfile::tempdir().expect("tempdir");
let dir = ProjectBinding::Directory(tmp.path().to_path_buf());
let git = ProjectBinding::GitRepo(tmp.path().to_path_buf());
assert_eq!(ProjectBinding::None.root(), None);
assert_eq!(dir.root(), Some(tmp.path()));
assert_eq!(git.root(), Some(tmp.path()));
}
#[test]
fn label_is_derived_from_root() {
let tmp = tempfile::tempdir().expect("tempdir");
let binding = ProjectBinding::Directory(tmp.path().to_path_buf());
assert_eq!(binding.label(), Some(tmp.path().display().to_string()));
assert_eq!(
ProjectBinding::None.label(),
None,
"projectless must have no label"
);
}
#[test]
fn agents_dir_uses_project_root_when_bound() {
let tmp = tempfile::tempdir().expect("tempdir");
let agents = tmp.path().join(".claude").join("agents");
std::fs::create_dir_all(&agents).expect("mkdir");
let binding = ProjectBinding::Directory(tmp.path().to_path_buf());
assert_eq!(binding.agents_dir(), agents);
}
#[test]
fn projectless_agents_dir_is_user_level() {
let Some(home) = dirs::home_dir() else {
assert_eq!(
ProjectBinding::None.agents_dir(),
PathBuf::from(".claude").join("agents")
);
return;
};
let dir = ProjectBinding::None.agents_dir();
assert!(
dir.starts_with(&home),
"projectless agents_dir must be user-level ({}), got {}",
home.display(),
dir.display()
);
assert!(
!dir.starts_with(std::env::current_dir().unwrap_or_else(|_| PathBuf::from("/nonexistent"))),
"projectless must NOT implicitly bind agents to the process CWD"
);
}
#[test]
fn wire_shape_round_trips_each_state() {
let tmp = tempfile::tempdir().expect("tempdir");
let root = tmp.path().display().to_string();
let none = ProjectBinding::None.to_json();
assert_eq!(none["state"], STATE_PROJECTLESS);
assert!(none["root"].is_null(), "projectless must have a null root");
let dir = ProjectBinding::Directory(tmp.path().to_path_buf()).to_json();
assert_eq!(dir["state"], STATE_DIRECTORY);
assert_eq!(dir["root"], root);
let git = ProjectBinding::GitRepo(tmp.path().to_path_buf()).to_json();
assert_eq!(git["state"], STATE_GIT_REPO);
assert_eq!(git["root"], root);
}
#[test]
fn serialize_matches_to_json() {
let tmp = tempfile::tempdir().expect("tempdir");
let binding = ProjectBinding::GitRepo(tmp.path().to_path_buf());
let via_serde = serde_json::to_value(&binding).expect("serialize");
assert_eq!(via_serde, binding.to_json());
}
#[test]
fn is_git_worktree_fails_open_for_missing_path() {
assert!(!is_git_worktree(Path::new("/definitely/not/here/xyzzy-42")));
}
#[test]
fn deserialize_round_trips_without_touching_disk() {
let phantom = PathBuf::from("/definitely/not/here/xyzzy-42");
for original in [
ProjectBinding::None,
ProjectBinding::Directory(phantom.clone()),
ProjectBinding::GitRepo(phantom.clone()),
] {
let json = serde_json::to_value(&original).expect("serialize");
let back: ProjectBinding = serde_json::from_value(json).expect("deserialize");
assert_eq!(
back, original,
"round-trip must preserve the state even for a path that does not exist on this host"
);
}
}
#[test]
fn deserialize_rejects_bound_state_without_root() {
let err = serde_json::from_value::<ProjectBinding>(serde_json::json!({"state": "git_repo"}))
.expect_err("a bound state with no root must not deserialize");
assert!(
err.to_string().contains("requires a root"),
"unexpected error: {err}"
);
let err = serde_json::from_value::<ProjectBinding>(
serde_json::json!({"state": "bogus", "root": "/x"}),
)
.expect_err("an unknown state must not deserialize");
assert!(
err.to_string().contains("unknown project binding state"),
"unexpected error: {err}"
);
}
#[test]
fn default_is_projectless() {
assert_eq!(ProjectBinding::default(), ProjectBinding::None);
}