#!/usr/bin/python3
"""Tests for slpkg.package_validator.PackageValidator."""

from unittest.mock import MagicMock

import pytest

from slpkg.package_validator import PackageValidator


def make_validator() -> PackageValidator:
    """Construct a PackageValidator bypassing __init__ (avoids config_load)."""
    validator: PackageValidator = object.__new__(PackageValidator)
    validator.red = ""
    validator.endc = ""
    validator.utils = MagicMock()
    return validator


# ---------------------------------------------------------------------------
# is_package_exists
# ---------------------------------------------------------------------------


def test_is_package_exists_found(sample_data: dict) -> None:
    v = make_validator()
    v.is_package_exists(["ffmpeg"], sample_data)  # no exception


def test_is_package_exists_wildcard_no_exception(sample_data: dict) -> None:
    v = make_validator()
    v.is_package_exists(["*"], sample_data)  # no exception


def test_is_package_exists_multiple_found(sample_data: dict) -> None:
    v = make_validator()
    v.is_package_exists(["ffmpeg", "bash", "yasm"], sample_data)  # no exception


def test_is_package_exists_not_found_raises(sample_data: dict) -> None:
    v = make_validator()
    with pytest.raises(SystemExit) as exc:
        v.is_package_exists(["nonexistent"], sample_data)
    assert exc.value.code == 1


def test_is_package_exists_one_missing_among_many_raises(sample_data: dict) -> None:
    v = make_validator()
    with pytest.raises(SystemExit) as exc:
        v.is_package_exists(["ffmpeg", "nonexistent", "bash"], sample_data)
    assert exc.value.code == 1


def test_is_package_exists_empty_data_raises() -> None:
    v = make_validator()
    with pytest.raises(SystemExit) as exc:
        v.is_package_exists(["ffmpeg"], {})
    assert exc.value.code == 1


def test_is_package_exists_wildcard_in_empty_data_no_exception() -> None:
    v = make_validator()
    v.is_package_exists(["*"], {})  # '*' is always valid


# ---------------------------------------------------------------------------
# is_package_installed
# ---------------------------------------------------------------------------


def test_is_package_installed_found_no_exception() -> None:
    v = make_validator()
    v.utils.is_package_installed.return_value = "ffmpeg-7.1-x86_64-1alien"
    v.is_package_installed(["ffmpeg"])  # no exception


def test_is_package_installed_multiple_found_no_exception() -> None:
    v = make_validator()
    v.utils.is_package_installed.return_value = "bash-5.2.026-x86_64-1"
    v.is_package_installed(["bash", "yasm"])  # no exception


def test_is_package_installed_not_found_raises() -> None:
    v = make_validator()
    v.utils.is_package_installed.return_value = ""  # empty → not installed
    with pytest.raises(SystemExit) as exc:
        v.is_package_installed(["nonexistent"])
    assert exc.value.code == 1


def test_is_package_installed_one_missing_raises() -> None:
    v = make_validator()

    def side_effect(name: str) -> str:
        return "ffmpeg-7.1-x86_64-1alien" if name == "ffmpeg" else ""

    v.utils.is_package_installed.side_effect = side_effect
    with pytest.raises(SystemExit) as exc:
        v.is_package_installed(["ffmpeg", "nonexistent"])
    assert exc.value.code == 1
