Skip to content

Commit 2702c77

Browse files
Add tests for tool call result event translation
(cherry picked from commit 5921ef0)
1 parent 14d051e commit 2702c77

File tree

1 file changed

+58
-4
lines changed

1 file changed

+58
-4
lines changed

typescript-sdk/integrations/adk-middleware/python/tests/test_event_translator_comprehensive.py

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#!/usr/bin/env python
22
"""Comprehensive tests for EventTranslator, focusing on untested paths."""
33

4+
import json
5+
from dataclasses import asdict, dataclass
6+
from types import SimpleNamespace
7+
48
import pytest
59
import uuid
610
from unittest.mock import MagicMock, patch, AsyncMock
@@ -109,15 +113,65 @@ async def test_translate_function_calls_detection(self, translator, mock_adk_eve
109113
async def test_translate_function_responses_handling(self, translator, mock_adk_event):
110114
"""Test function responses handling."""
111115
# Mock event with function responses
112-
mock_function_response = MagicMock()
113-
mock_adk_event.get_function_responses = MagicMock(return_value=[mock_function_response])
116+
function_response = SimpleNamespace(id="tool-1", response={"ok": True})
117+
mock_adk_event.get_function_calls = MagicMock(return_value=[])
118+
mock_adk_event.get_function_responses = MagicMock(return_value=[function_response])
114119

115120
events = []
116121
async for event in translator.translate(mock_adk_event, "thread_1", "run_1"):
117122
events.append(event)
118123

119-
# Function responses should be handled but not emit events
120-
assert len(events) == 0
124+
assert len(events) == 1
125+
event = events[0]
126+
assert isinstance(event, ToolCallResultEvent)
127+
assert json.loads(event.content) == {"ok": True}
128+
129+
@pytest.mark.asyncio
130+
async def test_translate_function_response_with_call_tool_result_payload(self, translator):
131+
"""Ensure CallToolResult payloads are serialized without errors."""
132+
133+
@dataclass
134+
class TextContent:
135+
text: str
136+
137+
@dataclass
138+
class CallToolResult:
139+
meta: dict | None
140+
structuredContent: dict | None
141+
isError: bool
142+
content: list[TextContent]
143+
144+
repeated_text_entries = [
145+
"Primary Task: Provide a detailed walkthrough for the requested topic.",
146+
"Primary Task: Provide a detailed walkthrough for the requested topic.",
147+
"Constraints: Ensure clarity and maintain a concise explanation.",
148+
"Constraints: Ensure clarity and maintain a concise explanation.",
149+
]
150+
151+
payload = CallToolResult(
152+
meta={"source": "test"},
153+
structuredContent=None,
154+
isError=False,
155+
content=[TextContent(text=text) for text in repeated_text_entries],
156+
)
157+
158+
function_response = SimpleNamespace(
159+
id="tool-structured-1",
160+
response=asdict(payload),
161+
)
162+
163+
events = []
164+
async for event in translator._translate_function_response([function_response]):
165+
events.append(event)
166+
167+
assert len(events) == 1
168+
event = events[0]
169+
assert isinstance(event, ToolCallResultEvent)
170+
171+
content = json.loads(event.content)
172+
assert content["isError"] is False
173+
assert content["structuredContent"] is None
174+
assert [item["text"] for item in content["content"]] == repeated_text_entries
121175

122176
@pytest.mark.asyncio
123177
async def test_translate_state_delta_event(self, translator, mock_adk_event):

0 commit comments

Comments
 (0)