72 lines
2.5 KiB
Python
72 lines
2.5 KiB
Python
"""Tests for API request compatibility and structured-response validation."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
|
|
import httpx
|
|
|
|
from swe_data_processing.client import GLMClient
|
|
from swe_data_processing.config import Settings
|
|
|
|
|
|
def test_client_parses_fenced_json_and_never_sends_key_in_body() -> None:
|
|
captured_body = {}
|
|
|
|
def handler(request: httpx.Request) -> httpx.Response:
|
|
captured_body.update(json.loads(request.content))
|
|
assert request.headers["authorization"] == "Bearer test-secret"
|
|
return httpx.Response(
|
|
200,
|
|
headers={"x-request-id": "request-123"},
|
|
json={
|
|
"choices": [
|
|
{"message": {"role": "assistant", "content": "```json\n{\"status\":\"ok\"}\n```"}}
|
|
],
|
|
"usage": {"total_tokens": 10},
|
|
},
|
|
)
|
|
|
|
schema = {
|
|
"type": "object",
|
|
"additionalProperties": False,
|
|
"required": ["status"],
|
|
"properties": {"status": {"const": "ok"}},
|
|
}
|
|
settings = Settings(api_key="test-secret", max_retries=1)
|
|
with GLMClient(settings, transport=httpx.MockTransport(handler)) as client:
|
|
response = client.invoke_json(system_prompt="Return JSON.", payload={"ping": True}, schema=schema)
|
|
|
|
assert response.data == {"status": "ok"}
|
|
assert response.request_id == "request-123"
|
|
assert response.usage == {"total_tokens": 10}
|
|
assert "test-secret" not in json.dumps(captured_body)
|
|
assert captured_body["model"] == "glm-5.2"
|
|
|
|
|
|
def test_client_falls_back_when_vendor_extensions_are_rejected() -> None:
|
|
calls = []
|
|
|
|
def handler(request: httpx.Request) -> httpx.Response:
|
|
body = json.loads(request.content)
|
|
calls.append(body)
|
|
if len(calls) == 1:
|
|
return httpx.Response(400, json={"error": {"message": "unknown parameter: thinking"}})
|
|
return httpx.Response(
|
|
200,
|
|
json={"choices": [{"message": {"content": "{\"status\":\"ok\"}"}}]},
|
|
)
|
|
|
|
schema = {
|
|
"type": "object",
|
|
"required": ["status"],
|
|
"properties": {"status": {"const": "ok"}},
|
|
}
|
|
settings = Settings(api_key="test-secret", max_retries=2)
|
|
with GLMClient(settings, transport=httpx.MockTransport(handler)) as client:
|
|
response = client.invoke_json(system_prompt="Return JSON.", payload={}, schema=schema)
|
|
|
|
assert "thinking" in calls[0]
|
|
assert "thinking" not in calls[1]
|
|
assert response.compatibility_fallback_used is True
|