from unittest.mock import Mock, patch

import pytest
from fastapi import HTTPException

from src.api.git_repositories import _build_provider_clone_url, _preflight_remote_repository


def test_build_provider_clone_url_uses_fixed_host() -> None:
    assert _build_provider_clone_url("alice", "demo") == "git@git.commumedia.org:alice/demo.git"


def test_preflight_remote_repository_allows_accessible_repo() -> None:
    completed = Mock(returncode=0)
    with patch("src.api.git_repositories.subprocess.run", return_value=completed) as run_mock:
        _preflight_remote_repository("git@git.commumedia.org:alice/demo.git")

    run_mock.assert_called_once()


def test_preflight_remote_repository_rejects_missing_repo() -> None:
    completed = Mock(returncode=128)
    with patch("src.api.git_repositories.subprocess.run", return_value=completed):
        with pytest.raises(HTTPException) as exc_info:
            _preflight_remote_repository("git@git.commumedia.org:alice/missing.git")

    assert exc_info.value.status_code == 400
    assert exc_info.value.detail == "repository not found or inaccessible"
