|
| 1 | +"""Tests for the Anthropic client.""" |
| 2 | + |
| 3 | +import os |
| 4 | +from unittest.mock import MagicMock, patch |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from rlm.clients.anthropic import AnthropicClient |
| 9 | +from rlm.core.types import ModelUsageSummary, UsageSummary |
| 10 | + |
| 11 | + |
| 12 | +class TestAnthropicClientUnit: |
| 13 | + """Unit tests that don't require API calls.""" |
| 14 | + |
| 15 | + def test_init_with_api_key(self): |
| 16 | + """Test client initialization with explicit API key.""" |
| 17 | + with patch("rlm.clients.anthropic.anthropic.Anthropic"): |
| 18 | + with patch("rlm.clients.anthropic.anthropic.AsyncAnthropic"): |
| 19 | + client = AnthropicClient(api_key="test-key", model_name="claude-3-5-sonnet") |
| 20 | + assert client.model_name == "claude-3-5-sonnet" |
| 21 | + |
| 22 | + def test_completion_empty_content(self): |
| 23 | + """Test completion returns empty string when response.content is empty.""" |
| 24 | + mock_response = MagicMock() |
| 25 | + mock_response.content = [] |
| 26 | + mock_response.usage.input_tokens = 10 |
| 27 | + mock_response.usage.output_tokens = 0 |
| 28 | + |
| 29 | + with patch("rlm.clients.anthropic.anthropic.Anthropic") as mock_client_class: |
| 30 | + mock_client = MagicMock() |
| 31 | + mock_client.messages.create.return_value = mock_response |
| 32 | + mock_client_class.return_value = mock_client |
| 33 | + |
| 34 | + with patch("rlm.clients.anthropic.anthropic.AsyncAnthropic"): |
| 35 | + client = AnthropicClient(api_key="test-key", model_name="claude-3-5-sonnet") |
| 36 | + result = client.completion("Hello") |
| 37 | + |
| 38 | + assert result == "" |
| 39 | + |
| 40 | + def test_completion_with_text_content(self): |
| 41 | + """Test completion returns text when response.content has text blocks.""" |
| 42 | + mock_block = MagicMock() |
| 43 | + mock_block.type = "text" |
| 44 | + mock_block.text = "Hello from Claude!" |
| 45 | + |
| 46 | + mock_response = MagicMock() |
| 47 | + mock_response.content = [mock_block] |
| 48 | + mock_response.usage.input_tokens = 10 |
| 49 | + mock_response.usage.output_tokens = 5 |
| 50 | + |
| 51 | + with patch("rlm.clients.anthropic.anthropic.Anthropic") as mock_client_class: |
| 52 | + mock_client = MagicMock() |
| 53 | + mock_client.messages.create.return_value = mock_response |
| 54 | + mock_client_class.return_value = mock_client |
| 55 | + |
| 56 | + with patch("rlm.clients.anthropic.anthropic.AsyncAnthropic"): |
| 57 | + client = AnthropicClient(api_key="test-key", model_name="claude-3-5-sonnet") |
| 58 | + result = client.completion("Hello") |
| 59 | + |
| 60 | + assert result == "Hello from Claude!" |
0 commit comments