-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest_client.py
More file actions
196 lines (163 loc) · 6.71 KB
/
test_client.py
File metadata and controls
196 lines (163 loc) · 6.71 KB
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
"""Tests for the Jules client."""
import pytest
from unittest.mock import Mock, patch, MagicMock
from jules_agent_sdk import JulesClient
from jules_agent_sdk.exceptions import JulesAuthenticationError, JulesValidationError
class TestJulesClient:
"""Test cases for JulesClient."""
def test_client_initialization(self):
"""Test client initializes correctly."""
client = JulesClient(api_key="test-api-key")
assert client is not None
assert client.sessions is not None
assert client.activities is not None
assert client.sources is not None
def test_client_requires_api_key(self):
"""Test client raises error without API key."""
with pytest.raises(ValueError, match="API key is required"):
JulesClient(api_key="")
def test_client_context_manager(self):
"""Test client works as context manager."""
with JulesClient(api_key="test-api-key") as client:
assert client is not None
@patch("jules_agent_sdk.base.BaseClient._request")
def test_sessions_create(self, mock_request):
"""Test session creation."""
mock_request.return_value = {
"name": "sessions/test123",
"id": "test123",
"prompt": "Fix bug",
"sourceContext": {"source": "sources/repo1"},
"state": "QUEUED",
}
client = JulesClient(api_key="test-api-key")
session = client.sessions.create(
prompt="Fix bug", source="sources/repo1", starting_branch="main"
)
assert session.id == "test123"
assert session.prompt == "Fix bug"
mock_request.assert_called_once()
@patch("jules_agent_sdk.base.BaseClient._request")
def test_sessions_create_with_automation_mode(self, mock_request):
"""Test session creation with automation mode."""
mock_request.return_value = {
"name": "sessions/test456",
"id": "test456",
"prompt": "Auto PR feature",
"sourceContext": {"source": "sources/repo2"},
"state": "QUEUED",
"automationMode": "AUTO_CREATE_PR",
}
client = JulesClient(api_key="test-api-key")
session = client.sessions.create(
prompt="Auto PR feature",
source="sources/repo2",
automation_mode="AUTO_CREATE_PR",
)
assert session.id == "test456"
assert session.automation_mode == "AUTO_CREATE_PR"
# Verify that automationMode was included in the request data
mock_request.assert_called_once()
_name, _args, kwargs = mock_request.mock_calls[0]
assert "json" in kwargs
assert kwargs["json"]["automationMode"] == "AUTO_CREATE_PR"
@patch("jules_agent_sdk.base.BaseClient._request")
def test_sessions_get(self, mock_request):
"""Test getting a session."""
mock_request.return_value = {
"name": "sessions/test123",
"id": "test123",
"prompt": "Fix bug",
"sourceContext": {"source": "sources/repo1"},
"state": "IN_PROGRESS",
}
client = JulesClient(api_key="test-api-key")
session = client.sessions.get("test123")
assert session.id == "test123"
assert session.state.value == "IN_PROGRESS"
@patch("jules_agent_sdk.base.BaseClient._request")
def test_sessions_list(self, mock_request):
"""Test listing sessions."""
mock_request.return_value = {
"sessions": [
{
"name": "sessions/test1",
"id": "test1",
"prompt": "Task 1",
"sourceContext": {"source": "sources/repo1"},
},
{
"name": "sessions/test2",
"id": "test2",
"prompt": "Task 2",
"sourceContext": {"source": "sources/repo2"},
},
],
"nextPageToken": "next-page",
}
client = JulesClient(api_key="test-api-key")
result = client.sessions.list(page_size=10)
assert len(result["sessions"]) == 2
assert result["nextPageToken"] == "next-page"
@patch("jules_agent_sdk.base.BaseClient._request")
def test_activities_list(self, mock_request):
"""Test listing activities."""
mock_request.return_value = {
"activities": [
{
"name": "sessions/s1/activities/a1",
"id": "a1",
"description": "Activity 1",
},
{
"name": "sessions/s1/activities/a2",
"id": "a2",
"description": "Activity 2",
},
]
}
client = JulesClient(api_key="test-api-key")
result = client.activities.list("s1")
assert len(result["activities"]) == 2
assert result["activities"][0].id == "a1"
@patch("jules_agent_sdk.base.BaseClient._request")
def test_sources_list(self, mock_request):
"""Test listing sources."""
mock_request.return_value = {
"sources": [
{
"name": "sources/src1",
"id": "src1",
"githubRepo": {"owner": "test", "repo": "repo1"},
}
]
}
client = JulesClient(api_key="test-api-key")
result = client.sources.list()
assert len(result["sources"]) == 1
assert result["sources"][0].id == "src1"
assert result["sources"][0].github_repo.owner == "test"
class TestErrorHandling:
"""Test error handling."""
@patch("jules_agent_sdk.base.requests.Session.request")
def test_authentication_error(self, mock_request):
"""Test authentication error handling."""
mock_response = Mock()
mock_response.ok = False
mock_response.status_code = 401
mock_response.json.return_value = {"error": {"message": "Invalid API key"}}
mock_request.return_value = mock_response
client = JulesClient(api_key="invalid-key")
with pytest.raises(JulesAuthenticationError):
client.sessions.list()
@patch("jules_agent_sdk.base.requests.Session.request")
def test_validation_error(self, mock_request):
"""Test validation error handling."""
mock_response = Mock()
mock_response.ok = False
mock_response.status_code = 400
mock_response.json.return_value = {"error": {"message": "Invalid request"}}
mock_request.return_value = mock_response
client = JulesClient(api_key="test-key")
with pytest.raises(JulesValidationError):
client.sessions.create(prompt="", source="")