import re
from unittest.mock import patch

import pytest

from llm_agents_from_scratch.errors import MissingExtraError
from llm_agents_from_scratch.utils import check_extra_was_installed


def test_check_raises_error_missing_pandas() -> None:
    """Check raises error from utils."""

    modules = {"pandas": None}

    with patch.dict("sys.modules", modules):
        msg = (
            "The `notebook-utils` extra is required for this function. "
            "Install with `pip install "
            "llm-agents-from-scratch[notebook-utils]`."
        )
        with pytest.raises(
            MissingExtraError,
            match=re.escape(msg),
        ):
            check_extra_was_installed(
                extra="notebook-utils",
                packages=["pandas", "IPython"],
            )


def test_check_raises_error_missing_ipython() -> None:
    """Check raises error from utils."""

    modules = {"IPython": None}

    with patch.dict("sys.modules", modules):
        msg = (
            "The `notebook-utils` extra is required for this function. "
            "Install with `pip install "
            "llm-agents-from-scratch[notebook-utils]`."
        )
        with pytest.raises(
            MissingExtraError,
            match=re.escape(msg),
        ):
            check_extra_was_installed(
                extra="notebook-utils",
                packages=["pandas", "IPython"],
            )
