"""
업데이터 단위 테스트.
"""
import os
import sys

import pytest

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

from utils.updater_client import (
    _parse_version, is_newer, RELEASES_API,
    UP_TO_DATE, NETWORK_ERROR, NO_RELEASE, NO_ASSET,
)


class TestVersionParsing:
    @pytest.mark.parametrize("input_str,expected", [
        ('1.0.0', (1, 0, 0)),
        ('v1.0.0', (1, 0, 0)),
        ('V2.1.3', (2, 1, 3)),
        ('1.0', (1, 0, 0)),  # 짧은 버전 → 0 패딩
        ('2', (2, 0, 0)),
        ('1.2.3.4', (1, 2, 3)),  # 초과 부분 무시
        ('2.0rc1', (2, 0, 0)),  # 접미사 제거
        ('', (0, 0, 0)),
    ])
    def test_parse_version(self, input_str, expected):
        assert _parse_version(input_str) == expected


class TestVersionComparison:
    @pytest.mark.parametrize("remote,local,expected", [
        ('2.0.0', '1.0.0', True),
        ('1.0.1', '1.0.0', True),
        ('v2.1.0', '2.0.5', True),
        ('1.0.0', '1.0.0', False),  # 동일 버전
        ('1.0.0', '2.0.0', False),  # 로컬이 더 최신
        ('v1.0.0', 'v1.0.0', False),
    ])
    def test_is_newer(self, remote, local, expected):
        assert is_newer(remote, local) == expected


class TestApiUrl:
    def test_default_points_to_gitea(self):
        """기본 URL이 자체 호스팅 Gitea를 가리키는지."""
        assert 'kindnick-git.duckdns.org' in RELEASES_API
        assert '/api/v1/repos/' in RELEASES_API
        assert '/releases/latest' in RELEASES_API

    def test_env_override(self, monkeypatch):
        """환경변수로 URL 오버라이드 가능."""
        monkeypatch.setenv('CLOCKOUT_RELEASES_API', 'https://example.com/api/test')
        # 모듈 재로드 필요
        import importlib
        from utils import updater_client
        importlib.reload(updater_client)
        assert updater_client.RELEASES_API == 'https://example.com/api/test'
        # 원복
        monkeypatch.delenv('CLOCKOUT_RELEASES_API', raising=False)
        importlib.reload(updater_client)


class TestCheckForUpdate:
    """check_for_update returns (info, reason) tuple."""

    def test_returns_tuple(self, monkeypatch):
        # 네트워크 호출이 실패하도록 잘못된 URL
        monkeypatch.setenv('CLOCKOUT_RELEASES_API', 'http://127.0.0.1:1/nope')
        import importlib
        from utils import updater_client
        importlib.reload(updater_client)
        try:
            result = updater_client.check_for_update('1.0.0', timeout=1)
            assert isinstance(result, tuple) and len(result) == 2
            info, reason = result
            assert info is None
            assert reason == updater_client.NETWORK_ERROR
        finally:
            monkeypatch.delenv('CLOCKOUT_RELEASES_API', raising=False)
            importlib.reload(updater_client)

    def test_constants_distinct(self):
        # 4개 상수가 모두 서로 다른 값
        values = {UP_TO_DATE, NETWORK_ERROR, NO_RELEASE, NO_ASSET}
        assert len(values) == 4


class TestUpdaterScript:
    """updater.py 자체 로직."""

    def test_is_pid_running_self(self):
        """현재 프로세스 PID는 running."""
        import updater
        assert updater.is_pid_running(os.getpid())

    def test_is_pid_running_dead(self):
        """존재하지 않는 PID는 not running."""
        import updater
        # 절대 사용되지 않을 PID (32비트 max + 1)
        assert not updater.is_pid_running(99999999)

    def test_replace_file_round_trip(self, tmp_path):
        """파일 교체 + 백업 생성 검증."""
        import updater
        target = tmp_path / "main.exe"
        new = tmp_path / "main_new.exe"
        target.write_bytes(b'OLD VERSION')
        new.write_bytes(b'NEW VERSION')

        backup = updater.replace_file(new, target)
        assert backup is not None
        assert backup.exists()
        assert backup.read_bytes() == b'OLD VERSION'
        assert target.exists()
        assert target.read_bytes() == b'NEW VERSION'
        assert not new.exists()  # 이동되었으므로 사라짐
