-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathtest_opik_client.py
More file actions
388 lines (303 loc) · 13.7 KB
/
Copy pathtest_opik_client.py
File metadata and controls
388 lines (303 loc) · 13.7 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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
import httpx
import pytest
import respx
from opik_mcp.opik_client import (
FeedbackScore,
OpikAuthError,
OpikClient,
OpikNotFoundError,
OpikPermissionError,
OpikServerError,
OpikValidationError,
)
OPIK_BASE = "https://opik.test"
@pytest.fixture
def anyio_backend() -> str:
return "asyncio"
def _client() -> OpikClient:
return OpikClient(base_url=OPIK_BASE, api_key="key-abc", workspace="ws")
# --- happy paths: feedback scores ---------------------------------------- #
@pytest.mark.anyio
async def test_trace_feedback_score_sends_put_with_required_fields() -> None:
with respx.mock(base_url=OPIK_BASE) as mock:
route = mock.put("/v1/private/traces/tr-1/feedback-scores").mock(
return_value=httpx.Response(204)
)
await _client().add_trace_feedback_score(
"tr-1", FeedbackScore(name="helpfulness", value=0.8)
)
req = route.calls.last.request
assert req.headers["authorization"] == "key-abc"
assert req.headers["comet-workspace"] == "ws"
assert req.headers["content-type"].startswith("application/json")
assert req.read() == b'{"name":"helpfulness","value":0.8,"source":"sdk"}'
@pytest.mark.anyio
async def test_trace_feedback_score_omits_none_optional_fields() -> None:
"""Optional category_name / reason must not appear in the body when None."""
with respx.mock(base_url=OPIK_BASE) as mock:
route = mock.put("/v1/private/traces/tr-1/feedback-scores").mock(
return_value=httpx.Response(204)
)
await _client().add_trace_feedback_score("tr-1", FeedbackScore(name="x", value=1.0))
body = route.calls.last.request.read()
assert b"category_name" not in body
assert b"reason" not in body
@pytest.mark.anyio
async def test_trace_feedback_score_includes_optional_fields_when_set() -> None:
with respx.mock(base_url=OPIK_BASE) as mock:
route = mock.put("/v1/private/traces/tr-1/feedback-scores").mock(
return_value=httpx.Response(204)
)
await _client().add_trace_feedback_score(
"tr-1",
FeedbackScore(
name="quality",
value=0.5,
category_name="manual",
reason="user-confirmed",
),
)
body = route.calls.last.request.read()
assert b'"category_name":"manual"' in body
assert b'"reason":"user-confirmed"' in body
@pytest.mark.anyio
async def test_span_feedback_score_sends_put_with_required_fields() -> None:
"""Mirror of the trace-feedback shape test on the span endpoint.
Asserts the actual bytes — a `route.called`-only assertion silently passes
if a refactor swaps the trace/span URLs or drops a required field.
"""
with respx.mock(base_url=OPIK_BASE) as mock:
route = mock.put("/v1/private/spans/sp-1/feedback-scores").mock(
return_value=httpx.Response(204)
)
await _client().add_span_feedback_score(
"sp-1", FeedbackScore(name="helpfulness", value=0.8)
)
req = route.calls.last.request
assert req.headers["authorization"] == "key-abc"
assert req.headers["comet-workspace"] == "ws"
assert req.read() == b'{"name":"helpfulness","value":0.8,"source":"sdk"}'
@pytest.mark.anyio
async def test_thread_feedback_score_wraps_in_batch_envelope() -> None:
"""Thread scoring is batch-only. Single MCP score → 1-element scores array."""
with respx.mock(base_url=OPIK_BASE) as mock:
route = mock.put("/v1/private/traces/threads/feedback-scores").mock(
return_value=httpx.Response(204)
)
await _client().add_thread_feedback_score("th-1", FeedbackScore(name="x", value=0.9))
body = route.calls.last.request.read()
assert body == b'{"scores":[{"thread_id":"th-1","name":"x","value":0.9,"source":"sdk"}]}'
@pytest.mark.anyio
async def test_thread_feedback_score_with_project_name_passthrough() -> None:
with respx.mock(base_url=OPIK_BASE) as mock:
route = mock.put("/v1/private/traces/threads/feedback-scores").mock(
return_value=httpx.Response(204)
)
await _client().add_thread_feedback_score(
"th-1",
FeedbackScore(name="x", value=1.0),
project_name="demo",
)
body = route.calls.last.request.read()
assert b'"project_name":"demo"' in body
# --- happy paths: comments ----------------------------------------------- #
@pytest.mark.anyio
async def test_trace_comment_posts_text() -> None:
with respx.mock(base_url=OPIK_BASE) as mock:
route = mock.post("/v1/private/traces/tr-1/comments").mock(
return_value=httpx.Response(
201, headers={"Location": "/v1/private/traces/tr-1/comments/c-1"}
)
)
await _client().add_trace_comment("tr-1", "looks good")
req = route.calls.last.request
assert req.headers["comet-workspace"] == "ws"
assert req.read() == b'{"text":"looks good"}'
@pytest.mark.anyio
async def test_span_comment_posts_text() -> None:
with respx.mock(base_url=OPIK_BASE) as mock:
route = mock.post("/v1/private/spans/sp-1/comments").mock(return_value=httpx.Response(201))
await _client().add_span_comment("sp-1", "note")
req = route.calls.last.request
assert req.headers["comet-workspace"] == "ws"
assert req.read() == b'{"text":"note"}'
@pytest.mark.anyio
async def test_thread_comment_uses_thread_id_in_path() -> None:
with respx.mock(base_url=OPIK_BASE) as mock:
route = mock.post("/v1/private/traces/threads/th-1/comments").mock(
return_value=httpx.Response(201)
)
await _client().add_thread_comment("th-1", "see follow-up")
assert route.called
assert route.calls.last.request.read() == b'{"text":"see follow-up"}'
# --- error mapping (uniform across all 6 endpoints) ----------------------- #
@pytest.mark.parametrize(
("status", "expected_exc"),
[
(401, OpikAuthError),
(403, OpikPermissionError),
(404, OpikNotFoundError),
(400, OpikValidationError),
(422, OpikValidationError),
(500, OpikServerError),
(502, OpikServerError),
(503, OpikServerError),
],
)
@pytest.mark.anyio
async def test_trace_feedback_score_maps_status_to_typed_error(
status: int, expected_exc: type[Exception]
) -> None:
with respx.mock(base_url=OPIK_BASE) as mock:
mock.put("/v1/private/traces/tr-1/feedback-scores").mock(
return_value=httpx.Response(status, json={"message": "bad"})
)
with pytest.raises(expected_exc):
await _client().add_trace_feedback_score("tr-1", FeedbackScore(name="x", value=1.0))
@pytest.mark.anyio
async def test_not_found_error_includes_entity_hint() -> None:
"""404 message should make it clear which entity was missing."""
with respx.mock(base_url=OPIK_BASE) as mock:
mock.put("/v1/private/spans/sp-missing/feedback-scores").mock(
return_value=httpx.Response(404)
)
with pytest.raises(OpikNotFoundError, match=r"sp-missing"):
await _client().add_span_feedback_score(
"sp-missing", FeedbackScore(name="x", value=1.0)
)
@pytest.mark.anyio
async def test_write_path_propagates_read_timeout_unchanged() -> None:
"""The write path does NOT translate `httpx.ReadTimeout` to OpikServerError.
Pinning this behavior so a refactor that wraps transport errors (which
would be a real semantic change — callers currently use `try/except
httpx.TimeoutException` directly) can't happen silently.
A regression that started swallowing the timeout (e.g. inside
`_raise_for_status`) would surface as `expected_exc` no longer firing.
"""
with respx.mock(base_url=OPIK_BASE) as mock:
mock.put("/v1/private/traces/tr-1/feedback-scores").mock(
side_effect=httpx.ReadTimeout("read timed out")
)
with pytest.raises(httpx.ReadTimeout):
await _client().add_trace_feedback_score("tr-1", FeedbackScore(name="x", value=1.0))
@pytest.mark.anyio
async def test_write_path_propagates_connect_timeout_unchanged() -> None:
"""Same contract as ReadTimeout for the comment endpoint — the network
boundary error surfaces untranslated so callers can decide retry policy."""
with respx.mock(base_url=OPIK_BASE) as mock:
mock.post("/v1/private/traces/tr-1/comments").mock(
side_effect=httpx.ConnectTimeout("connect timed out")
)
with pytest.raises(httpx.ConnectTimeout):
await _client().add_trace_comment("tr-1", "hello")
@pytest.mark.anyio
async def test_validation_error_includes_server_body_excerpt() -> None:
"""A 400 with a JSON message should surface that message to the caller."""
with respx.mock(base_url=OPIK_BASE) as mock:
mock.post("/v1/private/traces/tr-1/comments").mock(
return_value=httpx.Response(400, json={"message": "text must be non-blank"})
)
with pytest.raises(OpikValidationError, match=r"text must be non-blank"):
await _client().add_trace_comment("tr-1", "")
# --- client injection (for tests + e.g. shared connection pool) ----------- #
@pytest.mark.anyio
async def test_uses_injected_httpx_client_when_provided() -> None:
"""The orchestrator may want to share one AsyncClient across calls."""
with respx.mock(base_url=OPIK_BASE) as mock:
route = mock.post("/v1/private/traces/tr-1/comments").mock(return_value=httpx.Response(201))
async with httpx.AsyncClient() as injected:
client = OpikClient(
base_url=OPIK_BASE,
api_key="k",
workspace="ws",
client=injected,
)
await client.add_trace_comment("tr-1", "x")
assert route.called
# --- base URL handling ---------------------------------------------------- #
@pytest.mark.anyio
async def test_base_url_trailing_slash_is_normalized() -> None:
with respx.mock(base_url="https://opik.test") as mock:
route = mock.post("/v1/private/traces/tr-1/comments").mock(return_value=httpx.Response(201))
client = OpikClient(base_url="https://opik.test/", api_key="k", workspace="ws")
await client.add_trace_comment("tr-1", "x")
assert route.called
# --- resolve_opik_config -------------------------------------------------- #
def test_resolve_opik_config_uses_opik_url_when_set() -> None:
from opik_mcp.config import Settings
from opik_mcp.opik_client import resolve_opik_config
s = Settings(opik_api_key="k", comet_workspace="ws", opik_url="https://opik.example.com/")
base, _api, _ws = resolve_opik_config(s)
assert base == "https://opik.example.com"
def test_resolve_opik_config_derives_from_comet_url_override() -> None:
from opik_mcp.config import Settings
from opik_mcp.opik_client import resolve_opik_config
s = Settings(
opik_api_key="k",
comet_workspace="ws",
comet_url_override="https://dev.comet.com/",
opik_url=None,
)
base, _, _ = resolve_opik_config(s)
assert base == "https://dev.comet.com/opik/api"
def test_resolve_opik_config_rejects_empty_url_pair() -> None:
"""``COMET_URL_OVERRIDE=""`` (explicit empty) is a misconfiguration.
Without this guard, the base URL would become ``/opik/api`` — a relative
URL — and httpx would target wherever the process happens to be running.
Better to fail loudly at construction.
"""
from opik_mcp.config import MissingConfigError, Settings
from opik_mcp.opik_client import resolve_opik_config
s = Settings(opik_api_key="k", comet_workspace="ws", comet_url_override="", opik_url=None)
with pytest.raises(MissingConfigError, match="OPIK_URL or COMET_URL_OVERRIDE"):
resolve_opik_config(s)
def test_resolve_opik_config_requires_api_key() -> None:
from opik_mcp.config import MissingConfigError, Settings
from opik_mcp.opik_client import resolve_opik_config
s = Settings(opik_api_key=None, comet_workspace="ws")
with pytest.raises(MissingConfigError, match="OPIK_API_KEY"):
resolve_opik_config(s)
def test_resolve_opik_config_requires_workspace() -> None:
from opik_mcp.config import MissingConfigError, Settings
from opik_mcp.opik_client import resolve_opik_config
s = Settings(opik_api_key="k", comet_workspace=None)
with pytest.raises(MissingConfigError, match="COMET_WORKSPACE"):
resolve_opik_config(s)
# --- execute_experiment ------------------------------------------------------- #
@pytest.mark.anyio
async def test_execute_experiment_posts_to_execute_endpoint() -> None:
body = {
"dataset_name": "suite-a",
"dataset_id": "0193a300-0000-7000-8000-000000000123",
"prompts": [
{
"model": "gpt-4o",
"messages": [{"role": "user", "content": "Hi"}],
"configs": {"temperature": 0.0},
}
],
}
with respx.mock(base_url=OPIK_BASE) as mock:
route = mock.post("/v1/private/experiments/execute").mock(
return_value=httpx.Response(
202,
json={
"experiments": [
{
"experiment_id": "0193a300-0000-7000-8000-0000000000e1",
"prompt_index": 0,
}
],
"total_items": 12,
},
)
)
resp = await _client().execute_experiment(body)
assert resp.status_code == 202
payload = resp.json()
assert payload["total_items"] == 12
assert payload["experiments"][0]["prompt_index"] == 0
sent = route.calls.last.request
assert sent.headers["comet-workspace"] == "ws"
# Body is forwarded verbatim:
assert sent.read().startswith(b'{"dataset_name":"suite-a"')