test: complete chat SDK doubles and verify wire requests

This commit is contained in:
Adithya
2026-11-11 15:02:50 +05:30
parent 9d8742e994
commit 0dd67bb35f
3 changed files with 86 additions and 2 deletions

View File

@@ -1,8 +1,10 @@
"""Raw agent SDK requests must not leak private replay envelopes."""
from copy import deepcopy
import json
from types import SimpleNamespace
import httpx
import pytest
from deeptutor.runtime.agentic import client as agentic_client
@@ -61,6 +63,85 @@ async def test_raw_chat_replays_reasoning_without_private_state(monkeypatch, bin
assert sent[0]["stream"] is True
@pytest.mark.asyncio
@pytest.mark.parametrize("binding", ["custom", "azure_openai"])
@pytest.mark.parametrize("stream", [False, True])
async def test_raw_sdk_projects_replay_state_on_http_wire(monkeypatch, binding, stream):
sent = []
def handle(request):
sent.append(json.loads(request.content))
payload = {
"id": "chatcmpl-test",
"object": "chat.completion.chunk" if stream else "chat.completion",
"created": 0,
"model": "thinking-model",
"choices": [
{
"index": 0,
"delta" if stream else "message": {"role": "assistant", "content": "answer"},
"finish_reason": "stop",
}
],
}
if stream:
return httpx.Response(
200,
headers={"content-type": "text/event-stream"},
text=f"data: {json.dumps(payload)}\n\ndata: [DONE]\n\n",
)
return httpx.Response(200, json=payload)
client_kwargs = agentic_client.openai_sdk_client_kwargs
async with httpx.AsyncClient(transport=httpx.MockTransport(handle)) as http_client:
monkeypatch.setattr(
agentic_client,
"openai_sdk_client_kwargs",
lambda **kwargs: {**client_kwargs(**kwargs), "http_client": http_client},
)
client = agentic_client._build_openai_client(
agentic_client.LLMClientConfig(
binding=binding,
model="thinking-model",
api_key="test-key",
base_url="https://example.test/v1",
api_version="2025-01-01" if binding == "azure_openai" else None,
api_format="openai_chat",
),
disable_ssl_verify=False,
)
messages = [
{
"role": "assistant",
"content": "previous",
"_provider_response_state": {"reasoning_content": "retained reasoning"},
"_responses_output_items": [{"type": "reasoning", "id": "rs_private"}],
},
{"role": "user", "content": "continue"},
]
original = deepcopy(messages)
try:
result = await client.chat.completions.create(
model="thinking-model", messages=messages, stream=stream
)
if stream:
chunks = [chunk async for chunk in result]
assert chunks[0].choices[0].delta.content == "answer"
else:
assert result.choices[0].message.content == "answer"
finally:
await client.close()
assert len(sent) == 1
assert sent[0]["stream"] is stream
assert sent[0]["messages"][0] == {
"role": "assistant",
"content": "previous",
"reasoning_content": "retained reasoning",
}
assert messages == original
@pytest.mark.asyncio
async def test_responses_adapter_keeps_native_replay_state(monkeypatch):
captured = {}

View File

@@ -1,5 +1,6 @@
import asyncio
from types import SimpleNamespace
from unittest.mock import AsyncMock
import pytest
@@ -100,7 +101,7 @@ def test_agentic_client_routes_forced_responses_through_adapter(monkeypatch) ->
def test_agentic_client_does_not_force_responses_for_azure(monkeypatch) -> None:
class FakeAzureClient:
def __init__(self, **_kwargs):
pass
self.chat = SimpleNamespace(completions=SimpleNamespace(create=AsyncMock()))
class UnexpectedCompatProvider:
def __init__(self, **_kwargs):
@@ -130,7 +131,7 @@ def test_agentic_client_does_not_force_responses_for_azure(monkeypatch) -> None:
def test_agentic_client_forced_chat_completions_beats_auto_responses(monkeypatch) -> None:
class FakeChatCompletionsClient:
def __init__(self, **_kwargs):
self.chat = SimpleNamespace(completions=SimpleNamespace())
self.chat = SimpleNamespace(completions=SimpleNamespace(create=AsyncMock()))
monkeypatch.setattr(agentic_client, "AsyncOpenAI", FakeChatCompletionsClient)

View File

@@ -4,6 +4,7 @@ from __future__ import annotations
from types import SimpleNamespace
from typing import Any
from unittest.mock import AsyncMock
import pytest
@@ -37,6 +38,7 @@ def _capture_async_openai(monkeypatch: pytest.MonkeyPatch, module: Any) -> list[
class AsyncOpenAIStub:
def __init__(self, **kwargs: Any) -> None:
captured.append(kwargs)
self.chat = SimpleNamespace(completions=SimpleNamespace(create=AsyncMock()))
monkeypatch.setattr(module, "AsyncOpenAI", AsyncOpenAIStub)
return captured