Files
Dorod-Sky/tests/unit_tests/test_openrouter_integration.py

123 lines
4.1 KiB
Python
Raw Permalink Normal View History

2025-07-01 14:02:22 -04:00
import importlib
import json
2025-07-01 14:02:22 -04:00
import types
2025-10-06 18:55:52 -07:00
from unittest.mock import AsyncMock, MagicMock
2025-07-01 14:02:22 -04:00
import pytest
2025-10-06 18:55:52 -07:00
from skyvern import config
2025-07-01 14:02:22 -04:00
from skyvern.config import Settings
from skyvern.forge import app
from skyvern.forge.forge_app_initializer import start_forge_app
2025-07-01 14:02:22 -04:00
from skyvern.forge.sdk.api.llm import api_handler_factory, config_registry
from skyvern.forge.sdk.settings_manager import SettingsManager
@pytest.fixture(scope="module", autouse=True)
def setup_forge_app():
start_forge_app()
yield
2025-07-01 14:02:22 -04:00
class DummyResponse(dict):
def __init__(self, content: str):
super().__init__({"choices": [{"message": {"content": content}}], "usage": {}})
self.choices = [types.SimpleNamespace(message=types.SimpleNamespace(content=content))]
def model_dump_json(self, indent: int = 2):
return json.dumps(self, indent=indent)
2025-10-06 18:55:52 -07:00
def model_dump(self):
return self
2025-07-01 14:02:22 -04:00
class DummyArtifactManager:
async def create_llm_artifact(self, *args, **kwargs):
return None
@pytest.mark.asyncio
async def test_openrouter_basic_completion(monkeypatch):
settings = Settings(
ENABLE_OPENROUTER=True,
OPENROUTER_API_KEY="key",
OPENROUTER_MODEL="test-model",
LLM_KEY="OPENROUTER",
)
SettingsManager.set_settings(settings)
importlib.reload(config_registry)
monkeypatch.setattr(app, "ARTIFACT_MANAGER", DummyArtifactManager())
async_mock = AsyncMock(return_value=DummyResponse('{"result": "ok"}'))
monkeypatch.setattr(api_handler_factory.litellm, "acompletion", async_mock)
handler = api_handler_factory.LLMAPIHandlerFactory.get_llm_api_handler("OPENROUTER")
result = await handler("hi", "test")
assert result == {"result": "ok"}
async_mock.assert_called_once()
@pytest.mark.asyncio
async def test_openrouter_dynamic_model(monkeypatch):
2025-10-06 18:55:52 -07:00
# Update settings via monkeypatch to ensure config_registry sees them
monkeypatch.setattr(config.settings, "ENABLE_OPENROUTER", True)
monkeypatch.setattr(config.settings, "OPENROUTER_API_KEY", "key")
monkeypatch.setattr(config.settings, "OPENROUTER_MODEL", "base-model")
monkeypatch.setattr(config.settings, "OPENROUTER_API_BASE", "https://openrouter.ai/api/v1")
# Clear existing configs before reload
config_registry.LLMConfigRegistry._configs.clear()
2025-07-01 14:02:22 -04:00
importlib.reload(config_registry)
monkeypatch.setattr(app, "ARTIFACT_MANAGER", DummyArtifactManager())
2025-10-06 18:55:52 -07:00
# Mock the AsyncOpenAI client
2025-07-01 14:02:22 -04:00
async_mock = AsyncMock(return_value=DummyResponse('{"status": "ok"}'))
2025-10-06 18:55:52 -07:00
mock_client = MagicMock()
mock_client.chat.completions.create = async_mock
# Patch AsyncOpenAI to return our mock client
monkeypatch.setattr(api_handler_factory, "AsyncOpenAI", lambda **kwargs: mock_client)
2025-07-01 14:02:22 -04:00
base_handler = api_handler_factory.LLMAPIHandlerFactory.get_llm_api_handler("OPENROUTER")
override_handler = api_handler_factory.LLMAPIHandlerFactory.get_override_llm_api_handler(
"openrouter/other-model", default=base_handler
)
2025-10-06 18:55:52 -07:00
2025-07-01 14:02:22 -04:00
result = await override_handler("hi", "test")
assert result == {"status": "ok"}
called_model = async_mock.call_args.kwargs.get("model")
2025-10-06 18:55:52 -07:00
assert called_model == "other-model"
2025-07-01 14:02:22 -04:00
@pytest.mark.asyncio
async def test_openrouter_error_propagation(monkeypatch):
class DummyAPIError(Exception):
pass
settings = Settings(
ENABLE_OPENROUTER=True,
OPENROUTER_API_KEY="key",
OPENROUTER_MODEL="test-model",
LLM_KEY="OPENROUTER",
)
SettingsManager.set_settings(settings)
importlib.reload(config_registry)
monkeypatch.setattr(app, "ARTIFACT_MANAGER", DummyArtifactManager())
async def _raise(*args, **kwargs):
raise DummyAPIError()
fake_litellm = types.SimpleNamespace(
acompletion=_raise,
exceptions=types.SimpleNamespace(APIError=DummyAPIError),
)
monkeypatch.setattr(api_handler_factory, "litellm", fake_litellm)
handler = api_handler_factory.LLMAPIHandlerFactory.get_llm_api_handler("OPENROUTER")
with pytest.raises(api_handler_factory.LLMProviderErrorRetryableTask):
await handler("hi", "test")