diff --git a/src-tauri/src/services/worktree_manager.rs b/src-tauri/src/services/worktree_manager.rs index 084a73b..d5cbdaa 100644 --- a/src-tauri/src/services/worktree_manager.rs +++ b/src-tauri/src/services/worktree_manager.rs @@ -142,12 +142,23 @@ mod tests { dir } + fn current_branch(path: &str) -> String { + let output = Command::new("git") + .args(["rev-parse", "--abbrev-ref", "HEAD"]) + .current_dir(path) + .output() + .unwrap(); + assert!(output.status.success()); + String::from_utf8_lossy(&output.stdout).trim().to_string() + } + #[test] fn test_create_worktree() { let dir = setup_test_repo(); let path = dir.path().to_str().unwrap(); + let base_branch = current_branch(path); - let (wt_path, branch) = create_worktree(path, "main", 42).unwrap(); + let (wt_path, branch) = create_worktree(path, &base_branch, 42).unwrap(); assert!(wt_path.contains("orchai-42")); assert_eq!(branch, "orchai/42"); assert!(Path::new(&wt_path).exists()); @@ -157,9 +168,10 @@ mod tests { fn test_get_diff_empty() { let dir = setup_test_repo(); let path = dir.path().to_str().unwrap(); + let base_branch = current_branch(path); - let (_, branch) = create_worktree(path, "main", 1).unwrap(); - let diff = get_diff(path, "main", &branch).unwrap(); + let (_, branch) = create_worktree(path, &base_branch, 1).unwrap(); + let diff = get_diff(path, &base_branch, &branch).unwrap(); assert!(diff.is_empty(), "No changes yet, diff should be empty"); } @@ -167,8 +179,9 @@ mod tests { fn test_get_diff_with_changes() { let dir = setup_test_repo(); let path = dir.path().to_str().unwrap(); + let base_branch = current_branch(path); - let (wt_path, branch) = create_worktree(path, "main", 2).unwrap(); + let (wt_path, branch) = create_worktree(path, &base_branch, 2).unwrap(); std::fs::write(Path::new(&wt_path).join("fix.txt"), "fixed").unwrap(); Command::new("git").args(["add", "."]).current_dir(&wt_path).output().unwrap(); @@ -178,7 +191,7 @@ mod tests { .output() .unwrap(); - let diff = get_diff(path, "main", &branch).unwrap(); + let diff = get_diff(path, &base_branch, &branch).unwrap(); assert!(diff.contains("fix.txt")); assert!(diff.contains("+fixed")); } @@ -187,8 +200,9 @@ mod tests { fn test_list_commits() { let dir = setup_test_repo(); let path = dir.path().to_str().unwrap(); + let base_branch = current_branch(path); - let (wt_path, branch) = create_worktree(path, "main", 3).unwrap(); + let (wt_path, branch) = create_worktree(path, &base_branch, 3).unwrap(); std::fs::write(Path::new(&wt_path).join("a.txt"), "a").unwrap(); Command::new("git").args(["add", "."]).current_dir(&wt_path).output().unwrap(); @@ -206,7 +220,7 @@ mod tests { .output() .unwrap(); - let commits = list_commits(path, "main", &branch).unwrap(); + let commits = list_commits(path, &base_branch, &branch).unwrap(); assert_eq!(commits.len(), 2); } @@ -214,10 +228,11 @@ mod tests { fn test_list_local_branches() { let dir = setup_test_repo(); let path = dir.path().to_str().unwrap(); + let base_branch = current_branch(path); - create_worktree(path, "main", 10).unwrap(); + create_worktree(path, &base_branch, 10).unwrap(); let branches = list_local_branches(path).unwrap(); - assert!(branches.contains(&"main".to_string())); + assert!(branches.contains(&base_branch)); assert!(branches.contains(&"orchai/10".to_string())); } @@ -225,8 +240,9 @@ mod tests { fn test_delete_worktree() { let dir = setup_test_repo(); let path = dir.path().to_str().unwrap(); + let base_branch = current_branch(path); - let (wt_path, branch) = create_worktree(path, "main", 99).unwrap(); + let (wt_path, branch) = create_worktree(path, &base_branch, 99).unwrap(); assert!(Path::new(&wt_path).exists()); delete_worktree(path, &wt_path, &branch).unwrap(); @@ -240,6 +256,7 @@ mod tests { fn test_apply_fix() { let dir = setup_test_repo(); let path = dir.path().to_str().unwrap(); + let base_branch = current_branch(path); Command::new("git") .args(["branch", "feature/test"]) @@ -247,7 +264,7 @@ mod tests { .output() .unwrap(); - let (wt_path, branch) = create_worktree(path, "main", 7).unwrap(); + let (wt_path, branch) = create_worktree(path, &base_branch, 7).unwrap(); std::fs::write(Path::new(&wt_path).join("fix.txt"), "the fix").unwrap(); Command::new("git").args(["add", "."]).current_dir(&wt_path).output().unwrap(); Command::new("git") @@ -256,7 +273,7 @@ mod tests { .output() .unwrap(); - apply_fix(path, "main", &branch, "feature/test").unwrap(); + apply_fix(path, &base_branch, &branch, "feature/test").unwrap(); Command::new("git") .args(["checkout", "feature/test"]) @@ -266,7 +283,7 @@ mod tests { assert!(Path::new(path).join("fix.txt").exists()); Command::new("git") - .args(["checkout", "main"]) + .args(["checkout", &base_branch]) .current_dir(path) .output() .unwrap();