"""Tests for anima.shell.sandbox_check module.

Covers check_srt_available() and require_srt_available() with mocked
importlib.import_module for success/failure paths.

Source: vectl step sandbox-test-coverage — these functions had zero coverage.
"""

from __future__ import annotations

from unittest.mock import patch

import pytest
from returns.result import Failure, Success

from anima.shell.sandbox_check import check_srt_available, require_srt_available


class TestCheckSrtAvailable:
    """Unit tests for check_srt_available (Result-returning variant)."""

    def test_success_when_srt_importable(self) -> None:
        """Returns Success(None) when srt can be imported."""
        with patch("anima.shell.sandbox_check.importlib.import_module") as mock_import:
            mock_import.return_value = object()  # any truthy module stand-in
            result = check_srt_available()

        assert isinstance(result, Success)
        assert result.unwrap() is None
        mock_import.assert_called_once_with("srt")

    def test_failure_when_srt_missing(self) -> None:
        """Returns Failure(error_message) when srt import raises ImportError."""
        with patch(
            "anima.shell.sandbox_check.importlib.import_module",
            side_effect=ImportError("No module named 'srt'"),
        ):
            result = check_srt_available()

        assert isinstance(result, Failure)
        error_msg = result.failure()
        assert "sandbox-runtime" in error_msg
        assert "pip install" in error_msg

    def test_failure_message_includes_install_hint(self) -> None:
        """Error message should tell user how to install the package."""
        with patch(
            "anima.shell.sandbox_check.importlib.import_module",
            side_effect=ImportError("No module named 'srt'"),
        ):
            result = check_srt_available()

        error_msg = result.failure()
        assert "anima[sandbox]" in error_msg


class TestRequireSrtAvailable:
    """Unit tests for require_srt_available (exception-raising variant)."""

    def test_no_exception_when_srt_importable(self) -> None:
        """No exception raised when srt is available."""
        with patch("anima.shell.sandbox_check.importlib.import_module") as mock_import:
            mock_import.return_value = object()
            require_srt_available()  # should not raise

        mock_import.assert_called_once_with("srt")

    def test_raises_import_error_when_srt_missing(self) -> None:
        """Raises ImportError when srt is not installed."""
        with patch(
            "anima.shell.sandbox_check.importlib.import_module",
            side_effect=ImportError("No module named 'srt'"),
        ):
            with pytest.raises(ImportError, match="sandbox-runtime"):
                require_srt_available()

    def test_error_message_matches_check_failure(self) -> None:
        """ImportError message should match the Failure message from check_srt_available."""
        with patch(
            "anima.shell.sandbox_check.importlib.import_module",
            side_effect=ImportError("No module named 'srt'"),
        ):
            check_result = check_srt_available()

        with patch(
            "anima.shell.sandbox_check.importlib.import_module",
            side_effect=ImportError("No module named 'srt'"),
        ):
            with pytest.raises(ImportError) as exc_info:
                require_srt_available()

        assert str(exc_info.value) == check_result.failure()
