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
190 lines
6.4 KiB
Python
190 lines
6.4 KiB
Python
"""
|
|
Tests for uncovered code paths in OpenAICompatibleProvider.
|
|
|
|
Targets:
|
|
- create_llm: no api_key_setting (dummy key), max_tokens/streaming/retries/timeout from settings
|
|
- create_llm: NoSettingsContextError catch paths for optional params
|
|
- create_llm: base_url normalization, default model fallback
|
|
"""
|
|
|
|
from unittest.mock import Mock, patch
|
|
|
|
import pytest
|
|
|
|
from local_deep_research.llm.providers.openai_base import (
|
|
OpenAICompatibleProvider,
|
|
)
|
|
|
|
MODULE = "local_deep_research.llm.providers.openai_base"
|
|
SETTINGS_MODULE = "local_deep_research.config.thread_settings"
|
|
|
|
|
|
class TestCreateLlmApiKeyHandling:
|
|
@patch(f"{MODULE}.ChatOpenAI")
|
|
@patch(f"{SETTINGS_MODULE}.get_setting_from_snapshot")
|
|
def test_no_api_key_setting_uses_dummy(self, mock_get_setting, mock_chat):
|
|
"""Provider without api_key_setting uses dummy key."""
|
|
mock_get_setting.return_value = None
|
|
|
|
class NoKeyProvider(OpenAICompatibleProvider):
|
|
provider_name = "local_test"
|
|
api_key_setting = None # No API key needed
|
|
default_base_url = "http://localhost:8080/v1"
|
|
default_model = "local-model"
|
|
|
|
mock_chat.return_value = Mock()
|
|
|
|
NoKeyProvider.create_llm(model_name="test-model")
|
|
|
|
call_kwargs = mock_chat.call_args[1]
|
|
assert call_kwargs["api_key"] == "not-required"
|
|
|
|
@patch(f"{MODULE}.ChatOpenAI")
|
|
@patch(f"{SETTINGS_MODULE}.get_setting_from_snapshot")
|
|
def test_missing_api_key_raises(self, mock_get_setting, mock_chat):
|
|
"""Raises ValueError when required API key is missing."""
|
|
mock_get_setting.return_value = None
|
|
|
|
with pytest.raises(ValueError, match="API key not configured"):
|
|
OpenAICompatibleProvider.create_llm(
|
|
model_name="test-model",
|
|
settings_snapshot={"some": "settings"},
|
|
)
|
|
|
|
@patch(f"{MODULE}.ChatOpenAI")
|
|
@patch(f"{SETTINGS_MODULE}.get_setting_from_snapshot")
|
|
def test_api_key_from_settings(self, mock_get_setting, mock_chat):
|
|
"""API key is read from settings snapshot."""
|
|
|
|
def side_effect(key, default=None, settings_snapshot=None):
|
|
if "api_key" in key:
|
|
return "sk-test-key"
|
|
return default
|
|
|
|
mock_get_setting.side_effect = side_effect
|
|
mock_chat.return_value = Mock()
|
|
|
|
OpenAICompatibleProvider.create_llm(
|
|
model_name="test-model",
|
|
settings_snapshot={"llm.openai_endpoint.api_key": "sk-test-key"},
|
|
)
|
|
|
|
call_kwargs = mock_chat.call_args[1]
|
|
assert call_kwargs["api_key"] == "sk-test-key"
|
|
|
|
|
|
def _api_key_side_effect(key, default=None, settings_snapshot=None):
|
|
"""Return API key for api_key settings, None for everything else."""
|
|
if "api_key" in key:
|
|
return "sk-key"
|
|
return default
|
|
|
|
|
|
class TestCreateLlmModelAndUrl:
|
|
@patch(f"{MODULE}.ChatOpenAI")
|
|
@patch(
|
|
f"{SETTINGS_MODULE}.get_setting_from_snapshot",
|
|
side_effect=_api_key_side_effect,
|
|
)
|
|
def test_default_model_used(self, mock_get_setting, mock_chat):
|
|
"""Raises ValueError when no model name is provided (no silent default)."""
|
|
mock_chat.return_value = Mock()
|
|
|
|
with pytest.raises(ValueError, match="model not configured"):
|
|
OpenAICompatibleProvider.create_llm(
|
|
model_name=None,
|
|
settings_snapshot={},
|
|
)
|
|
|
|
@patch(f"{MODULE}.ChatOpenAI")
|
|
@patch(
|
|
f"{SETTINGS_MODULE}.get_setting_from_snapshot",
|
|
side_effect=_api_key_side_effect,
|
|
)
|
|
def test_custom_base_url(self, mock_get_setting, mock_chat):
|
|
"""Custom base_url is normalized and used."""
|
|
mock_chat.return_value = Mock()
|
|
|
|
OpenAICompatibleProvider.create_llm(
|
|
model_name="gpt-4",
|
|
base_url="http://localhost:1234/v1",
|
|
settings_snapshot={},
|
|
)
|
|
|
|
call_kwargs = mock_chat.call_args[1]
|
|
assert "localhost:1234" in call_kwargs["base_url"]
|
|
|
|
|
|
class TestCreateLlmOptionalParams:
|
|
@patch(f"{MODULE}.ChatOpenAI")
|
|
@patch(f"{SETTINGS_MODULE}.get_setting_from_snapshot")
|
|
def test_max_tokens_from_settings(self, mock_get_setting, mock_chat):
|
|
"""max_tokens is read from settings and passed to ChatOpenAI."""
|
|
|
|
def side_effect(key, default=None, settings_snapshot=None):
|
|
if key == "llm.openai_endpoint.api_key":
|
|
return "sk-key"
|
|
if key == "llm.max_tokens":
|
|
return 2048
|
|
return default
|
|
|
|
mock_get_setting.side_effect = side_effect
|
|
mock_chat.return_value = Mock()
|
|
|
|
OpenAICompatibleProvider.create_llm(
|
|
model_name="test-model", settings_snapshot={}
|
|
)
|
|
|
|
call_kwargs = mock_chat.call_args[1]
|
|
assert call_kwargs["max_tokens"] == 2048
|
|
|
|
@patch(f"{MODULE}.ChatOpenAI")
|
|
@patch(f"{SETTINGS_MODULE}.get_setting_from_snapshot")
|
|
def test_streaming_from_settings(self, mock_get_setting, mock_chat):
|
|
"""streaming is read from settings."""
|
|
|
|
def side_effect(key, default=None, settings_snapshot=None):
|
|
if key == "llm.openai_endpoint.api_key":
|
|
return "sk-key"
|
|
if key == "llm.streaming":
|
|
return True
|
|
return default
|
|
|
|
mock_get_setting.side_effect = side_effect
|
|
mock_chat.return_value = Mock()
|
|
|
|
OpenAICompatibleProvider.create_llm(
|
|
model_name="test-model", settings_snapshot={}
|
|
)
|
|
|
|
call_kwargs = mock_chat.call_args[1]
|
|
assert call_kwargs["streaming"] is True
|
|
|
|
@patch(f"{MODULE}.ChatOpenAI")
|
|
def test_no_settings_context_error_handled(self, mock_chat):
|
|
"""NoSettingsContextError is caught for optional params."""
|
|
from local_deep_research.config.thread_settings import (
|
|
NoSettingsContextError,
|
|
)
|
|
|
|
call_count = [0]
|
|
|
|
def side_effect(key, default=None, settings_snapshot=None):
|
|
call_count[0] += 1
|
|
if key == "llm.openai_endpoint.api_key":
|
|
return "sk-key"
|
|
raise NoSettingsContextError(f"No context for {key}")
|
|
|
|
mock_chat.return_value = Mock()
|
|
|
|
with patch(
|
|
f"{SETTINGS_MODULE}.get_setting_from_snapshot",
|
|
side_effect=side_effect,
|
|
):
|
|
# Should not raise despite NoSettingsContextError for optional params
|
|
OpenAICompatibleProvider.create_llm(
|
|
model_name="test-model", settings_snapshot={}
|
|
)
|
|
|
|
mock_chat.assert_called_once()
|