|
| 1 | +"""Tests for the Gemini client.""" |
| 2 | + |
| 3 | +import os |
| 4 | +from unittest.mock import MagicMock, patch |
| 5 | + |
| 6 | +import pytest |
| 7 | +from dotenv import load_dotenv |
| 8 | + |
| 9 | +from rlm.clients.gemini import GeminiClient |
| 10 | +from rlm.core.types import ModelUsageSummary, UsageSummary |
| 11 | + |
| 12 | +load_dotenv() |
| 13 | + |
| 14 | + |
| 15 | +class TestGeminiClientUnit: |
| 16 | + """Unit tests that don't require API calls.""" |
| 17 | + |
| 18 | + def test_init_with_api_key(self): |
| 19 | + """Test client initialization with explicit API key.""" |
| 20 | + with patch("rlm.clients.gemini.genai.Client"): |
| 21 | + client = GeminiClient(api_key="test-key", model_name="gemini-2.5-flash") |
| 22 | + assert client.model_name == "gemini-2.5-flash" |
| 23 | + |
| 24 | + def test_init_default_model(self): |
| 25 | + """Test client uses default model name.""" |
| 26 | + with patch("rlm.clients.gemini.genai.Client"): |
| 27 | + client = GeminiClient(api_key="test-key") |
| 28 | + assert client.model_name == "gemini-2.5-flash" |
| 29 | + |
| 30 | + def test_init_requires_api_key(self): |
| 31 | + """Test client raises error when no API key provided.""" |
| 32 | + with patch.dict(os.environ, {}, clear=True): |
| 33 | + with patch("rlm.clients.gemini.DEFAULT_GEMINI_API_KEY", None): |
| 34 | + with pytest.raises(ValueError, match="Gemini API key is required"): |
| 35 | + GeminiClient(api_key=None) |
| 36 | + |
| 37 | + def test_usage_tracking_initialization(self): |
| 38 | + """Test that usage tracking is properly initialized.""" |
| 39 | + with patch("rlm.clients.gemini.genai.Client"): |
| 40 | + client = GeminiClient(api_key="test-key") |
| 41 | + assert client.model_call_counts == {} |
| 42 | + assert client.model_input_tokens == {} |
| 43 | + assert client.model_output_tokens == {} |
| 44 | + assert client.last_prompt_tokens == 0 |
| 45 | + assert client.last_completion_tokens == 0 |
| 46 | + |
| 47 | + def test_get_usage_summary_empty(self): |
| 48 | + """Test usage summary when no calls have been made.""" |
| 49 | + with patch("rlm.clients.gemini.genai.Client"): |
| 50 | + client = GeminiClient(api_key="test-key") |
| 51 | + summary = client.get_usage_summary() |
| 52 | + assert isinstance(summary, UsageSummary) |
| 53 | + assert summary.model_usage_summaries == {} |
| 54 | + |
| 55 | + def test_get_last_usage(self): |
| 56 | + """Test last usage returns correct format.""" |
| 57 | + with patch("rlm.clients.gemini.genai.Client"): |
| 58 | + client = GeminiClient(api_key="test-key") |
| 59 | + client.last_prompt_tokens = 100 |
| 60 | + client.last_completion_tokens = 50 |
| 61 | + usage = client.get_last_usage() |
| 62 | + assert isinstance(usage, ModelUsageSummary) |
| 63 | + assert usage.total_calls == 1 |
| 64 | + assert usage.total_input_tokens == 100 |
| 65 | + assert usage.total_output_tokens == 50 |
| 66 | + |
| 67 | + def test_prepare_contents_string(self): |
| 68 | + """Test _prepare_contents with string input.""" |
| 69 | + with patch("rlm.clients.gemini.genai.Client"): |
| 70 | + client = GeminiClient(api_key="test-key") |
| 71 | + contents, system = client._prepare_contents("Hello world") |
| 72 | + assert contents == "Hello world" |
| 73 | + assert system is None |
| 74 | + |
| 75 | + def test_prepare_contents_messages_with_system(self): |
| 76 | + """Test _prepare_contents extracts system message.""" |
| 77 | + with patch("rlm.clients.gemini.genai.Client"): |
| 78 | + client = GeminiClient(api_key="test-key") |
| 79 | + messages = [ |
| 80 | + {"role": "system", "content": "You are helpful"}, |
| 81 | + {"role": "user", "content": "Hello"}, |
| 82 | + ] |
| 83 | + contents, system = client._prepare_contents(messages) |
| 84 | + assert system == "You are helpful" |
| 85 | + assert len(contents) == 1 |
| 86 | + assert contents[0].role == "user" |
| 87 | + |
| 88 | + def test_prepare_contents_role_mapping(self): |
| 89 | + """Test _prepare_contents maps assistant to model.""" |
| 90 | + with patch("rlm.clients.gemini.genai.Client"): |
| 91 | + client = GeminiClient(api_key="test-key") |
| 92 | + messages = [ |
| 93 | + {"role": "user", "content": "Hello"}, |
| 94 | + {"role": "assistant", "content": "Hi there"}, |
| 95 | + {"role": "user", "content": "How are you?"}, |
| 96 | + ] |
| 97 | + contents, system = client._prepare_contents(messages) |
| 98 | + assert system is None |
| 99 | + assert len(contents) == 3 |
| 100 | + assert contents[0].role == "user" |
| 101 | + assert contents[1].role == "model" # assistant -> model |
| 102 | + assert contents[2].role == "user" |
| 103 | + |
| 104 | + def test_prepare_contents_invalid_type(self): |
| 105 | + """Test _prepare_contents raises on invalid input.""" |
| 106 | + with patch("rlm.clients.gemini.genai.Client"): |
| 107 | + client = GeminiClient(api_key="test-key") |
| 108 | + with pytest.raises(ValueError, match="Invalid prompt type"): |
| 109 | + client._prepare_contents(12345) |
| 110 | + |
| 111 | + def test_completion_requires_model(self): |
| 112 | + """Test completion raises when no model specified.""" |
| 113 | + with patch("rlm.clients.gemini.genai.Client"): |
| 114 | + client = GeminiClient(api_key="test-key", model_name=None) |
| 115 | + with pytest.raises(ValueError, match="Model name is required"): |
| 116 | + client.completion("Hello") |
| 117 | + |
| 118 | + def test_completion_with_mocked_response(self): |
| 119 | + """Test completion with mocked API response.""" |
| 120 | + mock_response = MagicMock() |
| 121 | + mock_response.text = "Hello from Gemini!" |
| 122 | + mock_response.usage_metadata.prompt_token_count = 10 |
| 123 | + mock_response.usage_metadata.candidates_token_count = 5 |
| 124 | + |
| 125 | + with patch("rlm.clients.gemini.genai.Client") as mock_client_class: |
| 126 | + mock_client = MagicMock() |
| 127 | + mock_client.models.generate_content.return_value = mock_response |
| 128 | + mock_client_class.return_value = mock_client |
| 129 | + |
| 130 | + client = GeminiClient(api_key="test-key", model_name="gemini-2.5-flash") |
| 131 | + result = client.completion("Hello") |
| 132 | + |
| 133 | + assert result == "Hello from Gemini!" |
| 134 | + assert client.model_call_counts["gemini-2.5-flash"] == 1 |
| 135 | + assert client.model_input_tokens["gemini-2.5-flash"] == 10 |
| 136 | + assert client.model_output_tokens["gemini-2.5-flash"] == 5 |
| 137 | + |
| 138 | + |
| 139 | +class TestGeminiClientIntegration: |
| 140 | + """Integration tests that require a real API key.""" |
| 141 | + |
| 142 | + @pytest.mark.skipif( |
| 143 | + not os.environ.get("GEMINI_API_KEY"), |
| 144 | + reason="GEMINI_API_KEY not set", |
| 145 | + ) |
| 146 | + def test_simple_completion(self): |
| 147 | + """Test a simple completion with real API.""" |
| 148 | + client = GeminiClient(model_name="gemini-2.5-flash") |
| 149 | + result = client.completion("What is 2+2? Reply with just the number.") |
| 150 | + assert "4" in result |
| 151 | + |
| 152 | + # Verify usage was tracked |
| 153 | + usage = client.get_usage_summary() |
| 154 | + assert "gemini-2.5-flash" in usage.model_usage_summaries |
| 155 | + assert usage.model_usage_summaries["gemini-2.5-flash"].total_calls == 1 |
| 156 | + |
| 157 | + @pytest.mark.skipif( |
| 158 | + not os.environ.get("GEMINI_API_KEY"), |
| 159 | + reason="GEMINI_API_KEY not set", |
| 160 | + ) |
| 161 | + def test_message_list_completion(self): |
| 162 | + """Test completion with message list format.""" |
| 163 | + client = GeminiClient(model_name="gemini-2.5-flash") |
| 164 | + messages = [ |
| 165 | + {"role": "system", "content": "You are a helpful math tutor."}, |
| 166 | + {"role": "user", "content": "What is 5 * 5? Reply with just the number."}, |
| 167 | + ] |
| 168 | + result = client.completion(messages) |
| 169 | + assert "25" in result |
| 170 | + |
| 171 | + @pytest.mark.skipif( |
| 172 | + not os.environ.get("GEMINI_API_KEY"), |
| 173 | + reason="GEMINI_API_KEY not set", |
| 174 | + ) |
| 175 | + async def test_async_completion(self): |
| 176 | + """Test async completion.""" |
| 177 | + client = GeminiClient(model_name="gemini-2.5-flash") |
| 178 | + result = await client.acompletion("What is 3+3? Reply with just the number.") |
| 179 | + assert "6" in result |
| 180 | + |
| 181 | + |
| 182 | +if __name__ == "__main__": |
| 183 | + # Run integration tests directly |
| 184 | + test = TestGeminiClientIntegration() |
| 185 | + print("Testing simple completion...") |
| 186 | + test.test_simple_completion() |
| 187 | + print("Testing message list completion...") |
| 188 | + test.test_message_list_completion() |
| 189 | + print("All integration tests passed!") |
0 commit comments