from unittest.mock import patch

from GramAddict.core.llm_provider import query_telepathic_llm


def test_query_telepathic_llm_already_local():
    # If the provided URL is local, it should NOT switch to fallback model
    with patch("GramAddict.core.llm_provider.query_llm", return_value={"response": "test"}) as mock_query:
        query_telepathic_llm(
            model="llama3.2-vision",
            url="http://localhost:11434/api/generate",
            system_prompt="sys",
            user_prompt="user",
            use_local_edge=True,
        )
        mock_query.assert_called_once()
        args, kwargs = mock_query.call_args
        assert kwargs["model"] == "llama3.2-vision"
        assert kwargs["url"] == "http://localhost:11434/api/generate"


def test_query_telepathic_llm_remote_with_local_edge():
    # If the provided URL is remote, it SHOULD switch to fallback model when edge=True

    class MockArgs:
        ai_fallback_model = "llama3.2:1b"
        ai_fallback_url = "http://localhost:11434/api/generate"

    class MockConfig:
        args = MockArgs()

    with patch("GramAddict.core.llm_provider.query_llm", return_value={"response": "test"}) as mock_query:
        with patch("GramAddict.core.config.Config", return_value=MockConfig()):
            query_telepathic_llm(
                model="gpt-4o",
                url="https://api.openai.com/v1/chat/completions",
                system_prompt="sys",
                user_prompt="user",
                use_local_edge=True,
            )
        mock_query.assert_called_once()
        args, kwargs = mock_query.call_args
        assert kwargs["model"] == "llama3.2:1b"
        assert kwargs["url"] == "http://localhost:11434/api/generate"
