mod support;
use assert_cmd::cargo::cargo_bin_cmd;
use predicates::prelude::*;
use std::fs;
use tempfile::TempDir;
#[test]
fn test_worktree_initialization() -> Result<(), Box<dyn std::error::Error>> {
let temp = TempDir::new()?;
let main_repo = temp.path().join("main");
let worktree = temp.path().join("worktree");
fs::create_dir_all(&main_repo)?;
support::git_ok(&main_repo, &["init"]);
support::git_ok(&main_repo, &["config", "user.email", "test@example.com"]);
support::git_ok(&main_repo, &["config", "user.name", "Test User"]);
cargo_bin_cmd!("thoughts")
.current_dir(&main_repo)
.arg("init")
.assert()
.success();
support::git_ok(
&main_repo,
&["commit", "--allow-empty", "-m", "Initial commit"],
);
support::git_ok(
&main_repo,
&["worktree", "add", worktree.to_str().unwrap(), "HEAD"],
);
cargo_bin_cmd!("thoughts")
.current_dir(&worktree)
.arg("init")
.assert()
.success()
.stderr(predicate::str::contains("Detected git worktree"));
let worktree_thoughts_data = worktree.join(".thoughts-data");
assert!(worktree_thoughts_data.is_symlink());
let target = fs::read_link(&worktree_thoughts_data)?;
assert!(target.to_string_lossy().contains("main"));
Ok(())
}
#[test]
fn test_worktree_requires_main_init() -> Result<(), Box<dyn std::error::Error>> {
let temp = TempDir::new()?;
let main_repo = temp.path().join("main");
let worktree = temp.path().join("worktree");
fs::create_dir_all(&main_repo)?;
support::git_ok(&main_repo, &["init"]);
support::git_ok(&main_repo, &["config", "user.email", "test@example.com"]);
support::git_ok(&main_repo, &["config", "user.name", "Test User"]);
support::git_ok(
&main_repo,
&["commit", "--allow-empty", "-m", "Initial commit"],
);
support::git_ok(
&main_repo,
&["worktree", "add", worktree.to_str().unwrap(), "HEAD"],
);
cargo_bin_cmd!("thoughts")
.current_dir(&worktree)
.arg("init")
.assert()
.failure()
.stderr(predicate::str::contains(
"Main repository must be initialized first",
));
Ok(())
}
#[test]
fn test_worktree_config_routing() -> Result<(), Box<dyn std::error::Error>> {
let temp = TempDir::new()?;
let main_repo = temp.path().join("main");
let worktree = temp.path().join("worktree");
fs::create_dir_all(&main_repo)?;
support::git_ok(&main_repo, &["init"]);
support::git_ok(&main_repo, &["config", "user.email", "test@example.com"]);
support::git_ok(&main_repo, &["config", "user.name", "Test User"]);
cargo_bin_cmd!("thoughts")
.current_dir(&main_repo)
.arg("init")
.assert()
.success();
support::git_ok(
&main_repo,
&["commit", "--allow-empty", "-m", "Initial commit"],
);
support::git_ok(
&main_repo,
&["worktree", "add", worktree.to_str().unwrap(), "HEAD"],
);
cargo_bin_cmd!("thoughts")
.current_dir(&worktree)
.arg("init")
.assert()
.success();
let main_config = main_repo.join(".thoughts").join("config.json");
assert!(
main_config.exists(),
"Config should exist in main repo at {main_config:?}"
);
let worktree_config = worktree.join(".thoughts").join("config.json");
assert!(
!worktree_config.exists(),
"Config should NOT exist in worktree at {worktree_config:?}"
);
cargo_bin_cmd!("thoughts")
.current_dir(&worktree)
.args(["config", "show"])
.assert()
.success()
.stdout(predicate::str::contains("Repository Configuration"));
Ok(())
}