#!/usr/bin/python3
"""Tests for slpkg.self_check.get_repo_latest_version() with mocked HTTP."""

from unittest.mock import MagicMock, patch

import pytest

from slpkg.self_check import get_repo_latest_version

FAKE_URL = "https://example.com/pyproject.toml"


def make_response(text: str, status_code: int = 200) -> MagicMock:
    """Build a minimal mock requests.Response."""
    resp = MagicMock()
    resp.text = text
    resp.status_code = status_code
    resp.raise_for_status = MagicMock()
    return resp


# ---------------------------------------------------------------------------
# Successful extraction
# ---------------------------------------------------------------------------


def test_extracts_version_from_pyproject(monkeypatch: pytest.MonkeyPatch) -> None:
    content = '[project]\nname = "slpkg"\nversion = "5.5.4"\n'
    with patch("slpkg.self_check.requests.get", return_value=make_response(content)):
        result = get_repo_latest_version(FAKE_URL)
    assert result == "5.5.4"


def test_extracts_version_different_value(monkeypatch: pytest.MonkeyPatch) -> None:
    content = 'version = "1.2.3"\n'
    with patch("slpkg.self_check.requests.get", return_value=make_response(content)):
        result = get_repo_latest_version(FAKE_URL)
    assert result == "1.2.3"


def test_returns_first_match(monkeypatch: pytest.MonkeyPatch) -> None:
    content = 'version = "3.0.0"\nversion = "4.0.0"\n'
    with patch("slpkg.self_check.requests.get", return_value=make_response(content)):
        result = get_repo_latest_version(FAKE_URL)
    assert result == "3.0.0"


# ---------------------------------------------------------------------------
# No version found → None
# ---------------------------------------------------------------------------


def test_returns_none_when_no_version_line(monkeypatch: pytest.MonkeyPatch) -> None:
    content = '[project]\nname = "slpkg"\n'
    with patch("slpkg.self_check.requests.get", return_value=make_response(content)):
        result = get_repo_latest_version(FAKE_URL)
    assert result is None


def test_returns_none_for_empty_content(monkeypatch: pytest.MonkeyPatch) -> None:
    with patch("slpkg.self_check.requests.get", return_value=make_response("")):
        result = get_repo_latest_version(FAKE_URL)
    assert result is None


# ---------------------------------------------------------------------------
# Network errors → None (no exception propagated)
# ---------------------------------------------------------------------------


def test_returns_none_on_timeout(monkeypatch: pytest.MonkeyPatch) -> None:
    import requests

    with patch("slpkg.self_check.requests.get", side_effect=requests.exceptions.Timeout()):
        result = get_repo_latest_version(FAKE_URL)
    assert result is None


def test_returns_none_on_connection_error(monkeypatch: pytest.MonkeyPatch) -> None:
    import requests

    with patch("slpkg.self_check.requests.get", side_effect=requests.exceptions.ConnectionError()):
        result = get_repo_latest_version(FAKE_URL)
    assert result is None


def test_returns_none_on_http_error(monkeypatch: pytest.MonkeyPatch) -> None:
    import requests

    resp = make_response("", 404)
    http_err = requests.exceptions.HTTPError(response=resp)
    with patch("slpkg.self_check.requests.get", side_effect=http_err):
        result = get_repo_latest_version(FAKE_URL)
    assert result is None


def test_returns_none_on_request_exception(monkeypatch: pytest.MonkeyPatch) -> None:
    import requests

    with patch("slpkg.self_check.requests.get", side_effect=requests.exceptions.RequestException()):
        result = get_repo_latest_version(FAKE_URL)
    assert result is None
