import pytest from git import Repo from afk_developer.scheduler.git_manager import GitManager def test_git_manager_auto_initializes_a_missing_repository(tmp_path) -> None: manager = GitManager(tmp_path) assert (tmp_path / ".git").is_dir() assert manager.current_branch == "main" assert manager.is_clean() # An initial commit must exist so branch isolation works from task 1 onward. branch = manager.create_task_branch("first-task") assert branch == "afk/first-task" assert manager.current_branch == branch def test_git_manager_auto_init_commits_pre_existing_brownfield_files(tmp_path) -> None: (tmp_path / "existing.py").write_text("value = 1\n", encoding="utf-8") manager = GitManager(tmp_path) assert manager.is_clean() committed_files = manager.repo.git.show("--name-only", "--format=").splitlines() assert "existing.py" in committed_files def test_git_manager_rejects_a_bare_repository_with_a_specific_message(tmp_path) -> None: Repo.init(tmp_path, bare=True) with pytest.raises(ValueError, match="Bare repository not supported"): GitManager(tmp_path) def test_create_task_branch_raises_a_catchable_error_for_a_stale_branch(tmp_path) -> None: """A prior failed attempt can leave `afk/` behind uncleaned up (discard_branch_on_failure defaults to False). If the current branch has since moved on, GitPython's create_head raises a plain OSError (not GitCommandError) -- this must surface as a catchable RuntimeError, not crash the whole process.""" manager = GitManager(tmp_path) manager.create_task_branch("stale-task") manager.repo.heads["main"].checkout() (tmp_path / "tracked.txt").write_text("moved on\n", encoding="utf-8") manager.repo.git.add("--all") manager.repo.git.commit("-m", "main moved on") with pytest.raises(RuntimeError, match="Failed to create branch"): manager.create_task_branch("stale-task") def test_checkout_or_create_task_branch_uses_specified_start_point(tmp_path) -> None: repo = Repo.init(tmp_path, initial_branch="main") with repo.config_writer() as config_writer: config_writer.set_value("user", "name", "AFK Test") config_writer.set_value("user", "email", "afk-test@example.invalid") (tmp_path / "base.txt").write_text("base\n", encoding="utf-8") repo.index.add(["base.txt"]) repo.index.commit("base") manager = GitManager(tmp_path) red_branch = manager.create_task_branch("red-witness") witness = tmp_path / "tests" / "test_red.py" witness.parent.mkdir() witness.write_text("def test_red(): assert False\n", encoding="utf-8") assert manager.commit_changes("Retain red witness") manager.repo.heads["main"].checkout() implementation_branch = manager.checkout_or_create_task_branch( "implementation", start_point=red_branch ) assert implementation_branch == "afk/implementation" assert witness.exists() assert ( manager.repo.commit(implementation_branch).hexsha == manager.repo.commit(red_branch).hexsha ) def test_checkout_or_create_task_branch_recreates_stale_branch_without_start_point_ancestor( tmp_path, ) -> None: repo = Repo.init(tmp_path, initial_branch="main") with repo.config_writer() as config_writer: config_writer.set_value("user", "name", "AFK Test") config_writer.set_value("user", "email", "afk-test@example.invalid") (tmp_path / "base.txt").write_text("base\n", encoding="utf-8") repo.index.add(["base.txt"]) repo.index.commit("base") old_main = repo.head.commit.hexsha manager = GitManager(tmp_path) red_branch = manager.create_task_branch("red-witness") witness = tmp_path / "tests" / "test_red.py" witness.parent.mkdir() witness.write_text("def test_red(): assert False\n", encoding="utf-8") assert manager.commit_changes("Retain red witness") manager.repo.heads["main"].checkout() manager.repo.create_head(manager.task_branch_name("implementation"), old_main) implementation_branch = manager.checkout_or_create_task_branch( "implementation", start_point=red_branch ) assert implementation_branch == "afk/implementation" assert manager._is_ancestor(red_branch, implementation_branch) assert witness.exists() def test_primary_contains_files_ignores_stale_branch_worktree_files(tmp_path) -> None: manager = GitManager(tmp_path) branch = manager.create_task_branch("stale-task") artifact = tmp_path / "artifact.txt" artifact.write_text("only on task branch\n", encoding="utf-8") assert manager.commit_changes("Task artifact") assert not manager.primary_contains_files(["artifact.txt"]) manager.repo.heads["main"].checkout() manager.repo.git.merge(branch, "--no-ff", "-m", "merge task") assert manager.primary_contains_files(["artifact.txt"]) def test_task_branches_and_worktrees_are_scoped_by_plan_namespace(tmp_path) -> None: first = GitManager(tmp_path, branch_namespace="first-plan") second = GitManager(tmp_path, branch_namespace="second-plan") worktree_root = tmp_path.parent / "worktrees" assert first.task_branch_name("same-task") == "afk/first-plan/same-task" assert second.task_branch_name("same-task") == "afk/second-plan/same-task" assert first.worktree_path_for("same-task", worktree_root) != second.worktree_path_for( "same-task", worktree_root ) def test_create_task_worktree_recreates_stale_branch_without_start_point_ancestor(tmp_path) -> None: repo = Repo.init(tmp_path, initial_branch="main") with repo.config_writer() as config_writer: config_writer.set_value("user", "name", "AFK Test") config_writer.set_value("user", "email", "afk-test@example.invalid") (tmp_path / "base.txt").write_text("base\n", encoding="utf-8") repo.index.add(["base.txt"]) repo.index.commit("base") old_main = repo.head.commit.hexsha manager = GitManager(tmp_path) worktree_root = tmp_path.parent / "worktrees" red_branch = manager.create_task_branch("red-witness") witness = tmp_path / "tests" / "test_red.py" witness.parent.mkdir() witness.write_text("def test_red(): assert False\n", encoding="utf-8") assert manager.commit_changes("Retain red witness") manager.repo.heads["main"].checkout() stale_branch = manager.task_branch_name("implementation") manager.repo.create_head(stale_branch, old_main) manager.repo.git.worktree("add", str(worktree_root / "implementation"), stale_branch) branch_name, path = manager.create_task_worktree( "implementation", worktree_root, start_point=red_branch ) assert branch_name == stale_branch assert path == worktree_root / "implementation" assert manager._is_ancestor(red_branch, branch_name) assert (path / "tests" / "test_red.py").exists() def test_discard_task_branch_removes_unaccepted_changes_and_restores_main(tmp_path) -> None: repo = Repo.init(tmp_path, initial_branch="main") tracked = tmp_path / "tracked.txt" tracked.write_text("base\n", encoding="utf-8") repo.index.add(["tracked.txt"]) repo.index.commit("base") manager = GitManager(tmp_path) branch = manager.create_task_branch("failed-task") tracked.write_text("unaccepted\n", encoding="utf-8") (tmp_path / "generated.txt").write_text("discard me\n", encoding="utf-8") assert manager.discard_task_branch(branch) assert manager.current_branch == "main" assert manager.is_clean() assert tracked.read_text(encoding="utf-8") == "base\n" assert not (tmp_path / "generated.txt").exists() assert branch not in manager.repo.heads def test_discard_task_branch_removes_ignored_files_from_rejected_branch(tmp_path) -> None: repo = Repo.init(tmp_path, initial_branch="main") repo.git.commit("--allow-empty", "-m", "base") manager = GitManager(tmp_path) branch = manager.create_task_branch("failed-scaffold") (tmp_path / ".gitignore").write_text("__pycache__/\n", encoding="utf-8") cache = tmp_path / "package" / "__pycache__" / "module.pyc" cache.parent.mkdir(parents=True) cache.write_bytes(b"generated") assert manager.discard_task_branch(branch) assert manager.current_branch == "main" assert manager.is_clean() assert not cache.exists() assert branch not in manager.repo.heads def test_discard_task_branch_preserves_baseline_excluded_environment(tmp_path) -> None: repo = Repo.init(tmp_path, initial_branch="main") repo.git.commit("--allow-empty", "-m", "base") with (tmp_path / ".git" / "info" / "exclude").open("a", encoding="utf-8") as exclude: exclude.write(".venv/\n") environment_marker = tmp_path / ".venv" / "Scripts" / "python.exe" environment_marker.parent.mkdir(parents=True) environment_marker.write_bytes(b"target interpreter") manager = GitManager(tmp_path) branch = manager.create_task_branch("failed-task") (tmp_path / "generated.txt").write_text("discard me\n", encoding="utf-8") assert manager.discard_task_branch(branch) assert manager.current_branch == "main" assert manager.is_clean() assert environment_marker.read_bytes() == b"target interpreter" assert not (tmp_path / "generated.txt").exists() assert branch not in manager.repo.heads def test_create_task_worktree_creates_isolated_checkout(tmp_path) -> None: repo = Repo.init(tmp_path, initial_branch="main") (tmp_path / "tracked.txt").write_text("base\n", encoding="utf-8") repo.index.add(["tracked.txt"]) repo.index.commit("base") manager = GitManager(tmp_path) worktree_root = tmp_path.parent / "worktrees" branch_name, path = manager.create_task_worktree("new-feature", worktree_root) assert branch_name == "afk/new-feature" assert path == worktree_root / "new-feature" assert path.is_dir() assert (path / "tracked.txt").exists() # The primary checkout is never switched to the task's branch. assert manager.current_branch == "main" assert Repo(path).active_branch.name == branch_name # Calling again resumes the same worktree rather than erroring or recreating it. resumed_branch, resumed_path = manager.create_task_worktree("new-feature", worktree_root) assert (resumed_branch, resumed_path) == (branch_name, path) def test_merge_branch_into_merges_a_worktree_only_branch_into_primary(tmp_path) -> None: repo = Repo.init(tmp_path, initial_branch="main") with repo.config_writer() as config_writer: config_writer.set_value("user", "name", "AFK Test") config_writer.set_value("user", "email", "afk-test@example.invalid") (tmp_path / "tracked.txt").write_text("base\n", encoding="utf-8") repo.index.add(["tracked.txt"]) repo.index.commit("base") manager = GitManager(tmp_path) worktree_root = tmp_path.parent / "worktrees" branch_name, path = manager.create_task_worktree("feature", worktree_root) worktree_manager = GitManager(path) (path / "new_file.txt").write_text("new content\n", encoding="utf-8") assert worktree_manager.commit_changes("Add new file") worktree_manager.repo.close() # The primary checkout was never switched to the task branch, yet the merge succeeds. assert manager.current_branch == "main" assert manager.merge_branch_into(branch_name) assert manager.current_branch == "main" assert (tmp_path / "new_file.txt").exists() # Matches production usage in _finalize_task_branch: force is safe once merged. assert manager.remove_task_worktree(path, force=True) assert not path.exists() def test_discard_task_worktree_removes_worktree_and_branch(tmp_path) -> None: repo = Repo.init(tmp_path, initial_branch="main") (tmp_path / "tracked.txt").write_text("base\n", encoding="utf-8") repo.index.add(["tracked.txt"]) repo.index.commit("base") manager = GitManager(tmp_path) worktree_root = tmp_path.parent / "worktrees" branch_name, path = manager.create_task_worktree("bad-attempt", worktree_root) (path / "scratch.txt").write_text("discard me\n", encoding="utf-8") assert manager.discard_task_worktree(path, branch_name) assert not path.exists() assert branch_name not in manager.repo.heads assert manager.current_branch == "main"