"""Tests para parse_git_url, is_git_repo_url, validate_git_ssh_uri."""

import sys
import os

sys.path.insert(0, os.path.dirname(__file__))

from core import parse_git_url, is_git_repo_url, validate_git_ssh_uri


# ── parse_git_url ─────────────────────────────────────────────────────────────


def test_parse_git_url_https_github():
    result = parse_git_url("https://github.com/psf/requests")
    assert result == "psf/requests"


def test_parse_git_url_ssh_git_at():
    result = parse_git_url("git@github.com:psf/requests.git")
    assert result == "psf/requests"


def test_parse_git_url_git_suffix_stripped():
    result = parse_git_url("https://github.com/psf/requests.git")
    assert result == "psf/requests"


def test_parse_git_url_path_extra_ignored():
    result = parse_git_url("https://github.com/psf/requests/issues/123")
    assert result == "psf/requests"


def test_parse_git_url_unknown_host_returns_none():
    result = parse_git_url("https://bitbucket.org/org/repo")
    assert result is None


# ── is_git_repo_url ───────────────────────────────────────────────────────────


def test_is_git_repo_url_valid_repo():
    assert is_git_repo_url("https://github.com/psf/requests") is True


def test_is_git_repo_url_issue_rejected():
    assert is_git_repo_url("https://github.com/psf/requests/issues/123") is False


def test_is_git_repo_url_blob_rejected():
    assert is_git_repo_url("https://github.com/psf/requests/blob/main/README.md") is False


def test_is_git_repo_url_tree_accepted():
    assert is_git_repo_url("https://github.com/psf/requests/tree/main") is True


# ── validate_git_ssh_uri ──────────────────────────────────────────────────────


def test_validate_git_ssh_uri_valid():
    # Should not raise
    validate_git_ssh_uri("git@github.com:org/repo.git")


def test_validate_git_ssh_uri_no_git_at():
    try:
        validate_git_ssh_uri("https://github.com/org/repo")
        assert False, "Expected ValueError"
    except ValueError as e:
        assert "git@" in str(e)


def test_validate_git_ssh_uri_no_colon():
    try:
        validate_git_ssh_uri("git@github.com/org/repo")
        assert False, "Expected ValueError"
    except ValueError as e:
        assert ":" in str(e)


def test_validate_git_ssh_uri_empty_path():
    try:
        validate_git_ssh_uri("git@github.com:")
        assert False, "Expected ValueError"
    except ValueError as e:
        assert "non-empty path" in str(e)


if __name__ == "__main__":
    test_parse_git_url_https_github()
    test_parse_git_url_ssh_git_at()
    test_parse_git_url_git_suffix_stripped()
    test_parse_git_url_path_extra_ignored()
    test_parse_git_url_unknown_host_returns_none()
    test_is_git_repo_url_valid_repo()
    test_is_git_repo_url_issue_rejected()
    test_is_git_repo_url_blob_rejected()
    test_is_git_repo_url_tree_accepted()
    test_validate_git_ssh_uri_valid()
    test_validate_git_ssh_uri_no_git_at()
    test_validate_git_ssh_uri_no_colon()
    test_validate_git_ssh_uri_empty_path()
    print("All tests passed.")
