from unittest.mock import MagicMock, patch

from GramAddict.core.llm_provider import query_llm


def test_query_llm_passes_timeout_to_requests():
    """
    Verifies that the query_llm wrapper passes the configured
    timeout down to the requests.post call, enabling long
    generation tasks like comments to complete without crashing.
    """
    mock_response = MagicMock()
    mock_response.json.return_value = {"response": "test"}
    mock_response.raise_for_status.return_value = None

    with patch("GramAddict.core.llm_provider.requests.post", return_value=mock_response) as mock_post:
        # Act with custom 180s timeout
        # Using format_json=False because Ollama branch accesses resp_json["response"]
        query_llm("http://localhost:11434", "test_model", "test_prompt", timeout=180, format_json=False)

        # Assert
        mock_post.assert_called_once()
        _, kwargs = mock_post.call_args
        assert kwargs.get("timeout") == 180, "query_llm failed to pass the custom timeout to requests.post"


def test_query_llm_default_timeout_is_configurable():
    """
    Verifies that if no timeout is passed, by default we still use a configured or sensible value (60s).
    """
    mock_response = MagicMock()
    mock_response.json.return_value = {"response": "test"}

    with patch("GramAddict.core.llm_provider.requests.post", return_value=mock_response) as mock_post:
        query_llm("http://localhost:11434", "test_model", "test_prompt", format_json=False)

        _, kwargs = mock_post.call_args
        assert kwargs.get("timeout") == 180, "query_llm default timeout should act as fallback"
