From be5e75e67c0dc8dd87ebf1194d38a34e3556dc01 Mon Sep 17 00:00:00 2001 From: Woody Date: Thu, 23 Apr 2026 14:52:41 +0800 Subject: [PATCH] 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 --- backend/app/test/test_phase1_llm_client.py | 31 +++++++++---------- .../app/test/test_phase1_query_decomposer.py | 4 ++- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/backend/app/test/test_phase1_llm_client.py b/backend/app/test/test_phase1_llm_client.py index de74e7f..0f66d93 100644 --- a/backend/app/test/test_phase1_llm_client.py +++ b/backend/app/test/test_phase1_llm_client.py @@ -20,34 +20,31 @@ class TestLLMClient: @pytest.mark.asyncio async def test_llm_call_success(self, monkeypatch): """Should return content from mocked LLM API.""" + from unittest.mock import MagicMock, AsyncMock settings = get_settings() client = LLMClient(settings) - # Mock the underlying HTTP response - class _Resp: - status_code = 200 - def json(self): - return { - "choices": [{"message": {"content": "mock response"}}] - } - def raise_for_status(self): - pass + mock_message = MagicMock() + mock_message.content = "mock response" + mock_choice = MagicMock() + mock_choice.message = mock_message + mock_usage = MagicMock() + mock_usage.prompt_tokens = 10 + mock_usage.completion_tokens = 5 + mock_response = MagicMock() + mock_response.choices = [mock_choice] + mock_response.usage = mock_usage - async def _mock_post(*args, **kwargs): # type: ignore - return _Resp() + client._client.chat.completions.create = AsyncMock(return_value=mock_response) - # Patch AsyncClient.post - 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) + result = await client.complete(prompt="test prompt", temperature=0.7, step_name="TestStep") assert isinstance(result, str) assert "mock" in result def test_llm_provider_switching(self): settings = get_settings() - # Ensure base URL comes from settings via client; the client stores base_url 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 async def test_llm_api_error_handling(self, monkeypatch): diff --git a/backend/app/test/test_phase1_query_decomposer.py b/backend/app/test/test_phase1_query_decomposer.py index 82cd1fa..8d5e793 100644 --- a/backend/app/test/test_phase1_query_decomposer.py +++ b/backend/app/test/test_phase1_query_decomposer.py @@ -12,9 +12,11 @@ class MockLLMClient: def __init__(self, response: str): self._response = response 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_step_name = step_name return self._response