Files
wehub-resource-sync 7a0da7932b
OSV-Scanner (Scheduled) / scan-scheduled (push) Failing after 0s
Create Release / test-gate (push) Has been cancelled
Create Release / release-gate (push) Has been cancelled
Create Release / ci-gate (push) Has been cancelled
Create Release / version-check (push) Has been cancelled
Create Release / e2e-test-gate (push) Has been cancelled
Create Release / responsive-test-gate (push) Has been cancelled
Create Release / compat-test-gate (push) Has been cancelled
Create Release / compose-integration-gate (push) Has been cancelled
Create Release / vulture-gate (push) Has been cancelled
Create Release / build (push) Has been cancelled
Create Release / provenance (push) Has been cancelled
Create Release / prerelease-docker (push) Has been cancelled
Create Release / publish-docker (push) Has been cancelled
Create Release / create-release (push) Has been cancelled
Create Release / cleanup-changelog (push) Has been cancelled
Create Release / trigger-pypi (push) Has been cancelled
Create Release / monitor-pypi (push) Has been cancelled
Create Release / Clean up orphan prerelease tags and signatures (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [research-form] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [research-metrics] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [research-workflow] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [settings-core] (push) Has been cancelled
CodeQL Advanced / Analyze (javascript-typescript) (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [history-news] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [library] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [link-analytics] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [chat-core] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [chat-lifecycle] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [error-benchmark] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [settings-pages] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) (push) Has been cancelled
Docker Tests (Consolidated) / Accessibility Tests (push) Has been cancelled
Docker Tests (Consolidated) / LLM Unit Tests (push) Has been cancelled
Docker Tests (Consolidated) / LLM Example Tests (push) Has been cancelled
Docker Tests (Consolidated) / Production Image Smoke Test (push) Has been cancelled
Docker Tests (Consolidated) / Infrastructure Tests (push) Has been cancelled
OSSF Scorecard / OSSF Security Scorecard Analysis (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [mobile] (push) Has been cancelled
Backwards Compatibility / Verify Encryption Constants (push) Has been cancelled
Backwards Compatibility / PyPI Version Compatibility (push) Has been cancelled
Backwards Compatibility / Database Migration Tests (push) Has been cancelled
CodeQL Advanced / Analyze (python) (push) Has been cancelled
Docker Tests (Consolidated) / detect-changes (push) Has been cancelled
Docker Tests (Consolidated) / Build Test Image (push) Has been cancelled
Docker Tests (Consolidated) / All Pytest Tests + Coverage (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [accessibility] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [api-crud] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [auth-login] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [auth-pages] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [auth-register] (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:08:55 +08:00

151 lines
5.8 KiB
Python

"""
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"