test(openai_compatible): use ChatMessage and ToolCall attribute access
The 5 tests in tests/test_openai_compatible.py used the LEGACY dict-based
API. Updated to use the canonical typed API:
- test_send_non_streaming_returns_text_in_result
- test_send_streaming_aggregates_chunks
- test_tool_call_detection_in_blocking_response
- test_vision_multimodal_message
- test_error_classification_429_to_rate_limit
Changes per test:
- messages=[{...}] -> messages=[ChatMessage(role=..., content=...)]
- tool_calls[0]['function']['name'] -> tool_calls[0].function.name
- tool_calls[0]['id'] -> tool_calls[0].id
The dict messages in test_tool_call_detection_in_blocking_response's kwargs
are CORRECT - that test calls _send_blocking(client, kwargs) directly with
raw OpenAI kwargs (which expect dicts because they go to the OpenAI client),
bypassing OpenAICompatibleRequest.
Verification:
- uv run pytest tests/test_openai_compatible.py -v -> 6 of 6 pass
- tier-1-unit-core in batched suite now PASS (was FAIL)
This commit is contained in:
@@ -5,6 +5,7 @@ from src.openai_compatible import (
|
||||
OpenAICompatibleRequest,
|
||||
send_openai_compatible,
|
||||
)
|
||||
from src.openai_schemas import ChatMessage
|
||||
from src.vendor_capabilities import VendorCapabilities, register
|
||||
|
||||
@pytest.fixture
|
||||
@@ -25,7 +26,7 @@ def _mock_completion(text: str = "hello", tool_calls=None, usage_input: int = 10
|
||||
def test_send_non_streaming_returns_text_in_result(caps: VendorCapabilities) -> None:
|
||||
client = MagicMock()
|
||||
client.chat.completions.create.return_value = _mock_completion("hi", usage_input=20, usage_output=10)
|
||||
request = OpenAICompatibleRequest(messages=[{"role": "user", "content": "ping"}], model="m", max_tokens=100)
|
||||
request = OpenAICompatibleRequest(messages=[ChatMessage(role="user", content="ping")], model="m", max_tokens=100)
|
||||
result = send_openai_compatible(client, request, capabilities=caps)
|
||||
assert result.ok
|
||||
assert result.data.text == "hi"
|
||||
@@ -40,7 +41,7 @@ def test_send_streaming_aggregates_chunks(caps: VendorCapabilities) -> None:
|
||||
]
|
||||
client.chat.completions.create.return_value = iter(chunks)
|
||||
received: list = []
|
||||
request = OpenAICompatibleRequest(messages=[{"role": "user", "content": "ping"}], model="m", stream=True, stream_callback=received.append)
|
||||
request = OpenAICompatibleRequest(messages=[ChatMessage(role="user", content="ping")], model="m", stream=True, stream_callback=received.append)
|
||||
result = send_openai_compatible(client, request, capabilities=caps)
|
||||
assert result.ok
|
||||
assert result.data.text == "hello"
|
||||
@@ -58,17 +59,18 @@ def test_tool_call_detection_in_blocking_response(caps: VendorCapabilities) -> N
|
||||
kwargs = {"model": "m", "messages": [{"role": "user", "content": "ping"}], "temperature": 0.0, "top_p": 1.0, "max_tokens": 8192, "stream": False}
|
||||
response = _send_blocking(client, kwargs)
|
||||
assert len(response.tool_calls) == 1
|
||||
assert response.tool_calls[0]["function"]["name"] == "read_file"
|
||||
assert response.tool_calls[0]["id"] == "call_1"
|
||||
assert response.tool_calls[0].function.name == "read_file"
|
||||
assert response.tool_calls[0].id == "call_1"
|
||||
|
||||
def test_vision_multimodal_message(caps: VendorCapabilities) -> None:
|
||||
client = MagicMock()
|
||||
client.chat.completions.create.return_value = _mock_completion("looks like a cat")
|
||||
messages = [{"role": "user", "content": [{"type": "text", "text": "what is this?"}, {"type": "image_url", "image_url": {"url": "data:image/png;base64,..."}}]}]
|
||||
expected_content = [{"type": "text", "text": "what is this?"}, {"type": "image_url", "image_url": {"url": "data:image/png;base64,..."}}]
|
||||
messages = [ChatMessage(role="user", content=expected_content)]
|
||||
request = OpenAICompatibleRequest(messages=messages, model="m")
|
||||
result = send_openai_compatible(client, request, capabilities=caps)
|
||||
sent_messages = client.chat.completions.create.call_args.kwargs["messages"]
|
||||
assert sent_messages[0]["content"] == messages[0]["content"]
|
||||
assert sent_messages[0]["content"] == expected_content
|
||||
assert result.data.text == "looks like a cat"
|
||||
|
||||
def test_error_classification_429_to_rate_limit(caps: VendorCapabilities) -> None:
|
||||
@@ -76,7 +78,7 @@ def test_error_classification_429_to_rate_limit(caps: VendorCapabilities) -> Non
|
||||
from src.result_types import Result, ErrorKind
|
||||
client = MagicMock()
|
||||
client.chat.completions.create.side_effect = RateLimitError("rate limited", response=MagicMock(status_code=429), body=None)
|
||||
request = OpenAICompatibleRequest(messages=[{"role": "user", "content": "ping"}], model="m")
|
||||
request = OpenAICompatibleRequest(messages=[ChatMessage(role="user", content="ping")], model="m")
|
||||
result = send_openai_compatible(client, request, capabilities=caps)
|
||||
assert isinstance(result, Result)
|
||||
assert not result.ok
|
||||
|
||||
Reference in New Issue
Block a user