From 0dd67bb35ff2d2beb17a4ee3eba50f1a6e7ea189 Mon Sep 17 00:00:00 2001 From: Adithya Date: Wed, 11 Nov 2026 15:02:50 +0530 Subject: [PATCH] test: complete chat SDK doubles and verify wire requests --- tests/core/test_agentic_chat_wire_messages.py | 81 +++++++++++++++++++ .../llm/test_openai_compat_wire_api.py | 5 +- tests/services/llm/test_openai_http_client.py | 2 + 3 files changed, 86 insertions(+), 2 deletions(-) diff --git a/tests/core/test_agentic_chat_wire_messages.py b/tests/core/test_agentic_chat_wire_messages.py index 450d5aa2f..5f75c922d 100644 --- a/tests/core/test_agentic_chat_wire_messages.py +++ b/tests/core/test_agentic_chat_wire_messages.py @@ -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 = {} diff --git a/tests/services/llm/test_openai_compat_wire_api.py b/tests/services/llm/test_openai_compat_wire_api.py index a425cafd5..4383a4430 100644 --- a/tests/services/llm/test_openai_compat_wire_api.py +++ b/tests/services/llm/test_openai_compat_wire_api.py @@ -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) diff --git a/tests/services/llm/test_openai_http_client.py b/tests/services/llm/test_openai_http_client.py index bbf25a297..ecbd9c9f3 100644 --- a/tests/services/llm/test_openai_http_client.py +++ b/tests/services/llm/test_openai_http_client.py @@ -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