|
| 1 | +import unittest |
| 2 | +from unittest.mock import patch |
| 3 | + |
| 4 | +from memu.app.settings import LLMConfig |
| 5 | +from memu.llm.backends.grok import GrokBackend |
| 6 | +from memu.llm.openai_sdk import OpenAISDKClient |
| 7 | + |
| 8 | + |
| 9 | +class TestGrokProvider(unittest.IsolatedAsyncioTestCase): |
| 10 | + def test_settings_defaults(self): |
| 11 | + """Test that setting provider='grok' sets the correct defaults.""" |
| 12 | + config = LLMConfig(provider="grok") |
| 13 | + self.assertEqual(config.base_url, "https://api.x.ai/v1") |
| 14 | + self.assertEqual(config.api_key, "XAI_API_KEY") |
| 15 | + self.assertEqual(config.chat_model, "grok-2-latest") |
| 16 | + |
| 17 | + @patch("memu.llm.openai_sdk.AsyncOpenAI") |
| 18 | + async def test_client_initialization_with_grok_config(self, mock_async_openai): |
| 19 | + """Test that OpenAISDKClient initializes with Grok base URL when configured.""" |
| 20 | + # Setup config |
| 21 | + config = LLMConfig(provider="grok") |
| 22 | + |
| 23 | + # Instantiate client with Grok config |
| 24 | + # We simulate what the application factory would do: pass the config values |
| 25 | + client = OpenAISDKClient( |
| 26 | + base_url=config.base_url, |
| 27 | + api_key="fake-key", # In real app, this would be os.getenv(config.api_key) |
| 28 | + chat_model=config.chat_model, |
| 29 | + embed_model=config.embed_model, |
| 30 | + ) |
| 31 | + |
| 32 | + # Assert AsyncOpenAI was called with the correct base_url |
| 33 | + mock_async_openai.assert_called_with(api_key="fake-key", base_url="https://api.x.ai/v1") |
| 34 | + |
| 35 | + # Verify client attributes |
| 36 | + self.assertEqual(client.chat_model, "grok-2-latest") |
| 37 | + |
| 38 | + def test_grok_backend_payload_parsing(self): |
| 39 | + """Test that GrokBackend parses responses correctly (inherited from OpenAI).""" |
| 40 | + backend = GrokBackend() |
| 41 | + |
| 42 | + # Simulate a typical OpenAI-compatible response |
| 43 | + dummy_response = {"choices": [{"message": {"content": "Grok response content", "role": "assistant"}}]} |
| 44 | + |
| 45 | + result = backend.parse_summary_response(dummy_response) |
| 46 | + self.assertEqual(result, "Grok response content") |
0 commit comments