File size: 7,473 Bytes
52a4f3c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
"""
Tests for API clients (Gemini, Claude).
"""

import json
import pytest
import asyncio
from unittest.mock import Mock, patch, AsyncMock

from models.gemini_api import GeminiClient
from models.claude_api import ClaudeClient
from models.base import get_api_client


class TestGeminiClient:
    """Test Gemini API client."""

    @pytest.fixture
    def client(self):
        """Create a Gemini client for testing."""
        return GeminiClient(
            api_key="test_key",
            model="gemini-3.1-flash-lite-preview",
        )

    def test_initialization(self, client):
        """Test client initialization."""
        assert client.api_key == "test_key"
        assert client.model == "gemini-3.1-flash-lite-preview"
        assert client.temperature == 0.8
        assert client.max_tokens == 1024

    def test_parse_response_valid_json(self, client):
        """Test parsing valid JSON response."""
        response = json.dumps({
            "action": "send_message",
            "content": "Hello, opponent.",
            "tone": "sharp"
        })

        parsed = client._parse_response(response)
        assert parsed["action"] == "send_message"
        assert parsed["content"] == "Hello, opponent."
        assert parsed["tone"] == "sharp"

    def test_parse_response_with_markdown(self, client):
        """Test parsing JSON from markdown code block."""
        response = '''```json
{
    "action": "send_message",
    "content": "Nice move.",
    "tone": "respectful"
}
```'''

        parsed = client._parse_response(response)
        assert parsed["action"] == "send_message"
        assert parsed["content"] == "Nice move."

    def test_parse_response_missing_action(self, client):
        """Test that missing action field raises error."""
        response = json.dumps({
            "content": "Hello",
            "tone": "sharp"
        })

        with pytest.raises(ValueError, match="Missing 'action' field"):
            client._parse_response(response)

    def test_parse_response_missing_content(self, client):
        """Test that missing content field raises error (except for stop)."""
        response = json.dumps({
            "action": "send_message"
        })

        with pytest.raises(ValueError, match="Missing 'content' field"):
            client._parse_response(response)

    def test_parse_response_stop_no_content(self, client):
        """Test that stop action doesn't require content."""
        response = json.dumps({
            "action": "stop"
        })

        parsed = client._parse_response(response)
        assert parsed["action"] == "stop"

    def test_parse_response_invalid_action(self, client):
        """Test that invalid action raises error."""
        response = json.dumps({
            "action": "invalid_action",
            "content": "test"
        })

        with pytest.raises(ValueError, match="Invalid action"):
            client._parse_response(response)

    def test_parse_response_save_memory(self, client):
        """Test parsing save_memory action."""
        response = json.dumps({
            "action": "save_memory",
            "content": "Alice plays aggressively",
            "memory_type": "player_behavior"
        })

        parsed = client._parse_response(response)
        assert parsed["action"] == "save_memory"
        assert parsed["content"] == "Alice plays aggressively"

    def test_parse_response_set_emotion(self, client):
        """Test parsing set_emotion action."""
        response = json.dumps({
            "action": "set_emotion",
            "content": "smirk"
        })

        parsed = client._parse_response(response)
        assert parsed["action"] == "set_emotion"
        assert parsed["content"] == "smirk"

    def test_parse_response_defaults_metadata(self, client):
        """Test that missing metadata defaults to empty dict."""
        response = json.dumps({
            "action": "send_message",
            "content": "test"
        })

        parsed = client._parse_response(response)
        assert parsed["metadata"] == {}

    def test_build_full_prompt(self, client):
        """Test prompt building."""
        system = "You are a chess master."
        user = "What do you think of my move?"

        prompt = client._build_full_prompt(system, user)
        assert system in prompt
        assert user in prompt
        assert "JSON" in prompt
        assert "action" in prompt

    def test_get_stats(self, client):
        """Test statistics tracking."""
        stats = client.get_stats()
        assert stats["total_calls"] == 0
        assert stats["total_errors"] == 0
        assert stats["error_rate"] == 0

        client.call_count = 10
        client.error_count = 2

        stats = client.get_stats()
        assert stats["total_calls"] == 10
        assert stats["total_errors"] == 2
        assert stats["error_rate"] == 0.2

    def test_reset_stats(self, client):
        """Test statistics reset."""
        client.call_count = 10
        client.error_count = 2

        client.reset_stats()
        assert client.call_count == 0
        assert client.error_count == 0


class TestClaudeClient:
    """Test Claude API client."""

    @pytest.fixture
    def client(self):
        """Create a Claude client for testing."""
        return ClaudeClient(
            api_key="test_key",
            model="claude-opus-4-20250514",
        )

    def test_initialization(self, client):
        """Test client initialization."""
        assert client.api_key == "test_key"
        assert client.model == "claude-opus-4-20250514"

    def test_parse_response_valid_json(self, client):
        """Test parsing valid JSON response."""
        response = json.dumps({
            "action": "send_message",
            "content": "Hello, opponent.",
            "tone": "respectful"
        })

        parsed = client._parse_response(response)
        assert parsed["action"] == "send_message"
        assert parsed["content"] == "Hello, opponent."


class TestAPIFactory:
    """Test API client factory."""

    def test_get_gemini_client(self):
        """Test getting Gemini client."""
        client = get_api_client(
            provider="gemini",
            api_key="test_key"
        )
        assert isinstance(client, GeminiClient)

    def test_get_claude_client(self):
        """Test getting Claude client."""
        client = get_api_client(
            provider="claude",
            api_key="test_key"
        )
        assert isinstance(client, ClaudeClient)

    def test_get_invalid_provider(self):
        """Test that invalid provider raises error."""
        with pytest.raises(ValueError, match="Unknown LLM provider"):
            get_api_client(provider="invalid")


# Integration test (requires actual API keys)
@pytest.mark.skipif(
    True,  # Skip by default, enable with API keys
    reason="Requires actual API credentials"
)
class TestGeminiIntegration:
    """Integration tests with real Gemini API."""

    @pytest.mark.asyncio
    async def test_respond_real_api(self):
        """Test actual API call to Gemini."""
        client = GeminiClient()
        response = await client.respond(
            system_prompt="You are a helpful chess player.",
            user_prompt="How are you?"
        )

        assert "action" in response
        assert "content" in response
        assert response["action"] in ["send_message", "stop", "save_memory", "set_emotion"]