test(backend): update unit tests for LLM monitoring changes

- Fixed MockLLMClient to accept step_name parameter

- Updated test mocks for OpenAI SDK structure

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
Woody 2026-04-23 14:52:41 +08:00
parent f5cfe44183
commit be5e75e67c
2 changed files with 17 additions and 18 deletions

View File

@ -20,34 +20,31 @@ class TestLLMClient:
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_llm_call_success(self, monkeypatch): async def test_llm_call_success(self, monkeypatch):
"""Should return content from mocked LLM API.""" """Should return content from mocked LLM API."""
from unittest.mock import MagicMock, AsyncMock
settings = get_settings() settings = get_settings()
client = LLMClient(settings) client = LLMClient(settings)
# Mock the underlying HTTP response mock_message = MagicMock()
class _Resp: mock_message.content = "mock response"
status_code = 200 mock_choice = MagicMock()
def json(self): mock_choice.message = mock_message
return { mock_usage = MagicMock()
"choices": [{"message": {"content": "mock response"}}] mock_usage.prompt_tokens = 10
} mock_usage.completion_tokens = 5
def raise_for_status(self): mock_response = MagicMock()
pass mock_response.choices = [mock_choice]
mock_response.usage = mock_usage
async def _mock_post(*args, **kwargs): # type: ignore client._client.chat.completions.create = AsyncMock(return_value=mock_response)
return _Resp()
# Patch AsyncClient.post result = await client.complete(prompt="test prompt", temperature=0.7, step_name="TestStep")
if hasattr(client, "_client") and client._client is not None:
client._client.post = _mock_post # type: ignore
result = await client.complete(prompt="test prompt", temperature=0.7)
assert isinstance(result, str) assert isinstance(result, str)
assert "mock" in result assert "mock" in result
def test_llm_provider_switching(self): def test_llm_provider_switching(self):
settings = get_settings() settings = get_settings()
# Ensure base URL comes from settings via client; the client stores base_url
client = LLMClient(settings) client = LLMClient(settings)
assert settings.llm_base_url.rstrip("/") in client.base_url assert str(client._client.base_url).rstrip("/") == settings.llm_base_url.rstrip("/")
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_llm_api_error_handling(self, monkeypatch): async def test_llm_api_error_handling(self, monkeypatch):

View File

@ -12,9 +12,11 @@ class MockLLMClient:
def __init__(self, response: str): def __init__(self, response: str):
self._response = response self._response = response
self.last_prompt = None self.last_prompt = None
self.last_step_name = None
async def complete(self, prompt: str, temperature: float = 0.7) -> str: async def complete(self, prompt: str, temperature: float = 0.7, step_name: str = "LLM") -> str:
self.last_prompt = prompt self.last_prompt = prompt
self.last_step_name = step_name
return self._response return self._response