""" Tests for llm_utils module - Extended Edge Cases Tests cover edge cases not covered by the main test_llm_utils.py: - fetch_ollama_models with JSON decode errors (actual safe_get mocking) - Handling of malformed responses """ from unittest.mock import Mock, patch from local_deep_research.utilities.llm_utils import ( fetch_ollama_models, ) class TestFetchOllamaModelsWithSafeGet: """Tests for fetch_ollama_models using the actual safe_get function.""" def test_json_decode_error_returns_empty_list(self): """Should return empty list when JSON parsing fails.""" with patch("local_deep_research.security.safe_get") as mock_safe_get: mock_response = Mock() mock_response.status_code = 200 mock_response.json.side_effect = ValueError("Invalid JSON") mock_safe_get.return_value = mock_response result = fetch_ollama_models("http://localhost:11434") assert result == [] def test_safe_get_called_with_correct_params(self): """Should call safe_get with localhost and private IP flags enabled.""" with patch("local_deep_research.security.safe_get") as mock_safe_get: mock_response = Mock() mock_response.status_code = 200 mock_response.json.return_value = {"models": []} mock_safe_get.return_value = mock_response fetch_ollama_models("http://localhost:11434", timeout=5.0) mock_safe_get.assert_called_once() call_kwargs = mock_safe_get.call_args.kwargs assert call_kwargs["allow_localhost"] is True assert call_kwargs["allow_private_ips"] is True assert call_kwargs["timeout"] == 5.0 def test_handles_response_content_attribute(self): """Should handle responses with content attribute (like AIMessage).""" with patch("local_deep_research.security.safe_get") as mock_safe_get: mock_response = Mock() mock_response.status_code = 200 mock_response.json.return_value = {"models": [{"name": "llama2"}]} mock_safe_get.return_value = mock_response result = fetch_ollama_models("http://localhost:11434") assert len(result) == 1 assert result[0]["value"] == "llama2" def test_network_timeout_returns_empty_list(self): """Should return empty list on network timeout.""" import requests with patch("local_deep_research.security.safe_get") as mock_safe_get: mock_safe_get.side_effect = requests.exceptions.Timeout( "Connection timed out" ) result = fetch_ollama_models("http://localhost:11434") assert result == [] def test_connection_refused_returns_empty_list(self): """Should return empty list when connection is refused.""" import requests with patch("local_deep_research.security.safe_get") as mock_safe_get: mock_safe_get.side_effect = requests.exceptions.ConnectionError( "Connection refused" ) result = fetch_ollama_models("http://localhost:11434") assert result == [] def test_handles_list_response_format(self): """Should handle older API format where response is a list directly.""" with patch("local_deep_research.security.safe_get") as mock_safe_get: mock_response = Mock() mock_response.status_code = 200 # Older API format returns list directly mock_response.json.return_value = [ {"name": "model1"}, {"name": "model2"}, ] mock_safe_get.return_value = mock_response result = fetch_ollama_models("http://localhost:11434") assert len(result) == 2 assert result[0]["value"] == "model1" assert result[1]["value"] == "model2" def test_auth_headers_passed_to_safe_get(self): """Should pass auth headers to safe_get.""" with patch("local_deep_research.security.safe_get") as mock_safe_get: mock_response = Mock() mock_response.status_code = 200 mock_response.json.return_value = {"models": []} mock_safe_get.return_value = mock_response headers = {"Authorization": "Bearer test-token"} fetch_ollama_models("http://localhost:11434", auth_headers=headers) call_kwargs = mock_safe_get.call_args.kwargs assert call_kwargs["headers"] == headers def test_none_auth_headers_sends_empty_dict(self): """Should send empty dict when auth_headers is None.""" with patch("local_deep_research.security.safe_get") as mock_safe_get: mock_response = Mock() mock_response.status_code = 200 mock_response.json.return_value = {"models": []} mock_safe_get.return_value = mock_response fetch_ollama_models("http://localhost:11434", auth_headers=None) call_kwargs = mock_safe_get.call_args.kwargs assert call_kwargs["headers"] == {} def test_model_without_name_field_skipped(self): """Should skip models that don't have a name field.""" with patch("local_deep_research.security.safe_get") as mock_safe_get: mock_response = Mock() mock_response.status_code = 200 mock_response.json.return_value = { "models": [ {"name": "valid-model"}, {"size": "7B"}, # No name field {"name": ""}, # Empty name {"model": "wrong-field"}, # Wrong field name ] } mock_safe_get.return_value = mock_response result = fetch_ollama_models("http://localhost:11434") assert len(result) == 1 assert result[0]["value"] == "valid-model"