# tests/tasks/test_git_tasks.py
"""
Tests for git operation tasks.

These tests verify:
- Task signatures are correctly defined
- Tasks are bound (have access to self)
- Tasks return expected structure
- Tasks are routed to the 'git' queue

Note: These tests mock actual execution since they would require
Git operations and external APIs in production.
"""

import uuid
from unittest.mock import patch


class TestCloneRepositoryTask:
    """Tests for the clone_repository task."""

    def test_clone_repository_task_exists(self):
        """Test that clone_repository task is registered."""
        import app.tasks.git  # noqa: F401
        from app.celery_app import celery_app

        assert "app.tasks.git.clone_repository" in celery_app.tasks

    def test_clone_repository_is_bound_task(self):
        """Test that clone_repository is a bound task."""
        from app.tasks.git import clone_repository

        assert clone_repository.__bound__ is True

    def test_clone_repository_has_correct_name(self):
        """Test that clone_repository has the correct task name."""
        from app.tasks.git import clone_repository

        assert clone_repository.name == "app.tasks.git.clone_repository"

    def test_clone_repository_returns_expected_structure(self):
        """Test that clone_repository returns the expected result structure."""
        from app.tasks.git import clone_repository

        project_id = str(uuid.uuid4())
        repo_url = "https://gitea.example.com/org/repo.git"
        branch = "main"

        result = clone_repository(project_id, repo_url, branch)

        assert isinstance(result, dict)
        assert "status" in result
        assert "project_id" in result
        assert result["project_id"] == project_id

    def test_clone_repository_with_default_branch(self):
        """Test that clone_repository uses default branch when not specified."""
        from app.tasks.git import clone_repository

        project_id = str(uuid.uuid4())
        repo_url = "https://github.com/org/repo.git"

        # Call without specifying branch (should default to 'main')
        result = clone_repository(project_id, repo_url)

        assert result["status"] == "pending"


class TestCommitChangesTask:
    """Tests for the commit_changes task."""

    def test_commit_changes_task_exists(self):
        """Test that commit_changes task is registered."""
        import app.tasks.git  # noqa: F401
        from app.celery_app import celery_app

        assert "app.tasks.git.commit_changes" in celery_app.tasks

    def test_commit_changes_is_bound_task(self):
        """Test that commit_changes is a bound task."""
        from app.tasks.git import commit_changes

        assert commit_changes.__bound__ is True

    def test_commit_changes_returns_expected_structure(self):
        """Test that commit_changes returns the expected result structure."""
        from app.tasks.git import commit_changes

        project_id = str(uuid.uuid4())
        message = "feat: Add new feature"
        files = ["src/feature.py", "tests/test_feature.py"]

        result = commit_changes(project_id, message, files)

        assert isinstance(result, dict)
        assert "status" in result
        assert "project_id" in result

    def test_commit_changes_without_files(self):
        """Test that commit_changes handles None files (commit all staged)."""
        from app.tasks.git import commit_changes

        project_id = str(uuid.uuid4())
        message = "chore: Update dependencies"

        result = commit_changes(project_id, message, None)

        assert result["status"] == "pending"


class TestCreateBranchTask:
    """Tests for the create_branch task."""

    def test_create_branch_task_exists(self):
        """Test that create_branch task is registered."""
        import app.tasks.git  # noqa: F401
        from app.celery_app import celery_app

        assert "app.tasks.git.create_branch" in celery_app.tasks

    def test_create_branch_is_bound_task(self):
        """Test that create_branch is a bound task."""
        from app.tasks.git import create_branch

        assert create_branch.__bound__ is True

    def test_create_branch_returns_expected_structure(self):
        """Test that create_branch returns the expected result structure."""
        from app.tasks.git import create_branch

        project_id = str(uuid.uuid4())
        branch_name = "feature/new-feature"
        from_ref = "develop"

        result = create_branch(project_id, branch_name, from_ref)

        assert isinstance(result, dict)
        assert "status" in result
        assert "project_id" in result

    def test_create_branch_with_default_from_ref(self):
        """Test that create_branch uses default from_ref when not specified."""
        from app.tasks.git import create_branch

        project_id = str(uuid.uuid4())
        branch_name = "feature/123-add-login"

        result = create_branch(project_id, branch_name)

        assert result["status"] == "pending"


class TestCreatePullRequestTask:
    """Tests for the create_pull_request task."""

    def test_create_pull_request_task_exists(self):
        """Test that create_pull_request task is registered."""
        import app.tasks.git  # noqa: F401
        from app.celery_app import celery_app

        assert "app.tasks.git.create_pull_request" in celery_app.tasks

    def test_create_pull_request_is_bound_task(self):
        """Test that create_pull_request is a bound task."""
        from app.tasks.git import create_pull_request

        assert create_pull_request.__bound__ is True

    def test_create_pull_request_returns_expected_structure(self):
        """Test that create_pull_request returns expected result structure."""
        from app.tasks.git import create_pull_request

        project_id = str(uuid.uuid4())
        title = "feat: Add authentication"
        body = "## Summary\n- Added JWT auth\n- Added login endpoint"
        head_branch = "feature/auth"
        base_branch = "main"

        result = create_pull_request(project_id, title, body, head_branch, base_branch)

        assert isinstance(result, dict)
        assert "status" in result
        assert "project_id" in result

    def test_create_pull_request_with_default_base(self):
        """Test that create_pull_request uses default base branch."""
        from app.tasks.git import create_pull_request

        project_id = str(uuid.uuid4())

        result = create_pull_request(
            project_id, "Fix bug", "Bug fix description", "fix/bug-123"
        )

        assert result["status"] == "pending"


class TestPushChangesTask:
    """Tests for the push_changes task."""

    def test_push_changes_task_exists(self):
        """Test that push_changes task is registered."""
        import app.tasks.git  # noqa: F401
        from app.celery_app import celery_app

        assert "app.tasks.git.push_changes" in celery_app.tasks

    def test_push_changes_is_bound_task(self):
        """Test that push_changes is a bound task."""
        from app.tasks.git import push_changes

        assert push_changes.__bound__ is True

    def test_push_changes_returns_expected_structure(self):
        """Test that push_changes returns the expected result structure."""
        from app.tasks.git import push_changes

        project_id = str(uuid.uuid4())
        branch = "feature/new-feature"
        force = False

        result = push_changes(project_id, branch, force)

        assert isinstance(result, dict)
        assert "status" in result
        assert "project_id" in result

    def test_push_changes_with_force_option(self):
        """Test that push_changes handles force push option."""
        from app.tasks.git import push_changes

        project_id = str(uuid.uuid4())
        branch = "feature/rebased-branch"
        force = True

        result = push_changes(project_id, branch, force)

        assert result["status"] == "pending"


class TestGitTaskRouting:
    """Tests for git task queue routing."""

    def test_git_tasks_should_route_to_git_queue(self):
        """Test that git tasks are configured to route to 'git' queue."""
        from app.celery_app import celery_app

        routes = celery_app.conf.task_routes
        git_route = routes.get("app.tasks.git.*")

        assert git_route is not None
        assert git_route["queue"] == "git"

    def test_all_git_tasks_match_routing_pattern(self):
        """Test that all git task names match the routing pattern."""

        task_names = [
            "app.tasks.git.clone_repository",
            "app.tasks.git.commit_changes",
            "app.tasks.git.create_branch",
            "app.tasks.git.create_pull_request",
            "app.tasks.git.push_changes",
        ]

        for name in task_names:
            assert name.startswith("app.tasks.git.")


class TestGitTaskLogging:
    """Tests for git task logging behavior."""

    def test_clone_repository_logs_execution(self):
        """Test that clone_repository logs when executed."""
        from app.tasks.git import clone_repository

        project_id = str(uuid.uuid4())
        repo_url = "https://github.com/org/repo.git"

        with patch("app.tasks.git.logger") as mock_logger:
            clone_repository(project_id, repo_url)

            mock_logger.info.assert_called_once()
            call_args = mock_logger.info.call_args[0][0]
            assert repo_url in call_args
            assert project_id in call_args

    def test_commit_changes_logs_execution(self):
        """Test that commit_changes logs when executed."""
        from app.tasks.git import commit_changes

        project_id = str(uuid.uuid4())
        message = "test commit"

        with patch("app.tasks.git.logger") as mock_logger:
            commit_changes(project_id, message)

            mock_logger.info.assert_called_once()
            call_args = mock_logger.info.call_args[0][0]
            assert message in call_args
