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
229 lines
8.2 KiB
Python
229 lines
8.2 KiB
Python
"""Tests for OpenRouter LLM provider."""
|
|
|
|
import pytest
|
|
from unittest.mock import Mock, patch
|
|
|
|
from local_deep_research.llm.providers.implementations.openrouter import (
|
|
OpenRouterProvider,
|
|
)
|
|
|
|
|
|
class TestOpenRouterProviderMetadata:
|
|
"""Tests for OpenRouterProvider class metadata."""
|
|
|
|
def test_provider_name(self):
|
|
"""Provider name is correct."""
|
|
assert OpenRouterProvider.provider_name == "OpenRouter"
|
|
|
|
def test_provider_key(self):
|
|
"""Provider key is correct."""
|
|
assert OpenRouterProvider.provider_key == "OPENROUTER"
|
|
|
|
def test_is_cloud(self):
|
|
"""OpenRouter is a cloud provider."""
|
|
assert OpenRouterProvider.is_cloud is True
|
|
|
|
def test_company_name(self):
|
|
"""Company name is OpenRouter."""
|
|
assert OpenRouterProvider.company_name == "OpenRouter"
|
|
|
|
def test_api_key_setting(self):
|
|
"""API key setting is correct."""
|
|
assert OpenRouterProvider.api_key_setting == "llm.openrouter.api_key"
|
|
|
|
def test_default_model(self):
|
|
"""Default model is empty by design — users must explicitly pick one."""
|
|
assert OpenRouterProvider.default_model == ""
|
|
|
|
def test_default_base_url(self):
|
|
"""Default base URL is correct."""
|
|
assert "openrouter.ai" in OpenRouterProvider.default_base_url
|
|
|
|
|
|
class TestOpenRouterCreateLLM:
|
|
"""Tests for create_llm method."""
|
|
|
|
def test_create_llm_raises_without_api_key(self):
|
|
"""Raises ValueError when API key not configured."""
|
|
with patch(
|
|
"local_deep_research.config.thread_settings.get_setting_from_snapshot"
|
|
) as mock_get_setting:
|
|
mock_get_setting.return_value = None
|
|
|
|
with pytest.raises(ValueError) as exc_info:
|
|
OpenRouterProvider.create_llm()
|
|
|
|
assert "api key" in str(exc_info.value).lower()
|
|
|
|
def test_create_llm_with_valid_api_key(self):
|
|
"""Successfully creates ChatOpenAI instance with valid API key."""
|
|
|
|
def mock_get_setting_side_effect(key, default=None, *args, **kwargs):
|
|
settings_map = {
|
|
"llm.openrouter.api_key": "test-openrouter-key",
|
|
"llm.max_tokens": None,
|
|
"llm.streaming": None,
|
|
"llm.max_retries": None,
|
|
"llm.request_timeout": None,
|
|
}
|
|
return settings_map.get(key, default)
|
|
|
|
with patch(
|
|
"local_deep_research.config.thread_settings.get_setting_from_snapshot"
|
|
) as mock_get_setting:
|
|
mock_get_setting.side_effect = mock_get_setting_side_effect
|
|
|
|
with patch(
|
|
"local_deep_research.llm.providers.openai_base.ChatOpenAI"
|
|
) as mock_chat:
|
|
mock_llm = Mock()
|
|
mock_chat.return_value = mock_llm
|
|
|
|
result = OpenRouterProvider.create_llm(model_name="test-model")
|
|
|
|
assert result is mock_llm
|
|
mock_chat.assert_called_once()
|
|
|
|
def test_create_llm_uses_default_model_when_none(self):
|
|
"""Raises ValueError when no model name is provided (no silent default)."""
|
|
|
|
def mock_get_setting_side_effect(key, default=None, *args, **kwargs):
|
|
settings_map = {
|
|
"llm.openrouter.api_key": "test-key",
|
|
"llm.max_tokens": None,
|
|
"llm.streaming": None,
|
|
"llm.max_retries": None,
|
|
"llm.request_timeout": None,
|
|
}
|
|
return settings_map.get(key, default)
|
|
|
|
with patch(
|
|
"local_deep_research.config.thread_settings.get_setting_from_snapshot"
|
|
) as mock_get_setting:
|
|
mock_get_setting.side_effect = mock_get_setting_side_effect
|
|
|
|
with pytest.raises(ValueError, match="model not configured"):
|
|
OpenRouterProvider.create_llm()
|
|
|
|
def test_create_llm_with_custom_model(self):
|
|
"""Uses custom model when specified."""
|
|
|
|
def mock_get_setting_side_effect(key, default=None, *args, **kwargs):
|
|
settings_map = {
|
|
"llm.openrouter.api_key": "test-key",
|
|
"llm.max_tokens": None,
|
|
"llm.streaming": None,
|
|
"llm.max_retries": None,
|
|
"llm.request_timeout": None,
|
|
}
|
|
return settings_map.get(key, default)
|
|
|
|
with patch(
|
|
"local_deep_research.config.thread_settings.get_setting_from_snapshot"
|
|
) as mock_get_setting:
|
|
mock_get_setting.side_effect = mock_get_setting_side_effect
|
|
|
|
with patch(
|
|
"local_deep_research.llm.providers.openai_base.ChatOpenAI"
|
|
) as mock_chat:
|
|
OpenRouterProvider.create_llm(model_name="openai/gpt-4")
|
|
|
|
call_kwargs = mock_chat.call_args[1]
|
|
assert call_kwargs["model"] == "openai/gpt-4"
|
|
|
|
def test_create_llm_passes_temperature(self):
|
|
"""Passes temperature parameter."""
|
|
|
|
def mock_get_setting_side_effect(key, default=None, *args, **kwargs):
|
|
settings_map = {
|
|
"llm.openrouter.api_key": "test-key",
|
|
"llm.max_tokens": None,
|
|
"llm.streaming": None,
|
|
"llm.max_retries": None,
|
|
"llm.request_timeout": None,
|
|
}
|
|
return settings_map.get(key, default)
|
|
|
|
with patch(
|
|
"local_deep_research.config.thread_settings.get_setting_from_snapshot"
|
|
) as mock_get_setting:
|
|
mock_get_setting.side_effect = mock_get_setting_side_effect
|
|
|
|
with patch(
|
|
"local_deep_research.llm.providers.openai_base.ChatOpenAI"
|
|
) as mock_chat:
|
|
OpenRouterProvider.create_llm(
|
|
model_name="test-model", temperature=0.5
|
|
)
|
|
|
|
call_kwargs = mock_chat.call_args[1]
|
|
assert call_kwargs["temperature"] == 0.5
|
|
|
|
def test_create_llm_uses_openrouter_base_url(self):
|
|
"""Uses OpenRouter's base URL."""
|
|
|
|
def mock_get_setting_side_effect(key, default=None, *args, **kwargs):
|
|
settings_map = {
|
|
"llm.openrouter.api_key": "test-key",
|
|
"llm.max_tokens": None,
|
|
"llm.streaming": None,
|
|
"llm.max_retries": None,
|
|
"llm.request_timeout": None,
|
|
}
|
|
return settings_map.get(key, default)
|
|
|
|
with patch(
|
|
"local_deep_research.config.thread_settings.get_setting_from_snapshot"
|
|
) as mock_get_setting:
|
|
mock_get_setting.side_effect = mock_get_setting_side_effect
|
|
|
|
with patch(
|
|
"local_deep_research.llm.providers.openai_base.ChatOpenAI"
|
|
) as mock_chat:
|
|
OpenRouterProvider.create_llm(model_name="test-model")
|
|
|
|
call_kwargs = mock_chat.call_args[1]
|
|
assert "openrouter.ai" in call_kwargs["base_url"]
|
|
|
|
|
|
class TestOpenRouterIsAvailable:
|
|
"""Tests for is_available method."""
|
|
|
|
def test_is_available_true_when_key_exists(self):
|
|
"""Returns True when API key is configured."""
|
|
with patch(
|
|
"local_deep_research.config.thread_settings.get_setting_from_snapshot"
|
|
) as mock_get_setting:
|
|
mock_get_setting.return_value = "test-key"
|
|
|
|
result = OpenRouterProvider.is_available()
|
|
assert result is True
|
|
|
|
def test_is_available_false_when_no_key(self):
|
|
"""Returns False when API key is not configured."""
|
|
with patch(
|
|
"local_deep_research.config.thread_settings.get_setting_from_snapshot"
|
|
) as mock_get_setting:
|
|
mock_get_setting.return_value = None
|
|
|
|
result = OpenRouterProvider.is_available()
|
|
assert result is False
|
|
|
|
def test_is_available_false_when_empty_key(self):
|
|
"""Returns False when API key is empty string."""
|
|
with patch(
|
|
"local_deep_research.config.thread_settings.get_setting_from_snapshot"
|
|
) as mock_get_setting:
|
|
mock_get_setting.return_value = ""
|
|
|
|
result = OpenRouterProvider.is_available()
|
|
assert result is False
|
|
|
|
|
|
class TestOpenRouterRequiresAuth:
|
|
"""Tests for requires_auth_for_models method."""
|
|
|
|
def test_does_not_require_auth_for_models(self):
|
|
"""OpenRouter doesn't require authentication for listing models."""
|
|
assert OpenRouterProvider.requires_auth_for_models() is False
|