|
1 | 1 | #!/usr/bin/env python
|
2 | 2 | """Comprehensive tests for EventTranslator, focusing on untested paths."""
|
3 | 3 |
|
| 4 | +import json |
| 5 | +from dataclasses import asdict, dataclass |
| 6 | +from types import SimpleNamespace |
| 7 | + |
4 | 8 | import pytest
|
5 | 9 | import uuid
|
6 | 10 | from unittest.mock import MagicMock, patch, AsyncMock
|
@@ -109,15 +113,65 @@ async def test_translate_function_calls_detection(self, translator, mock_adk_eve
|
109 | 113 | async def test_translate_function_responses_handling(self, translator, mock_adk_event):
|
110 | 114 | """Test function responses handling."""
|
111 | 115 | # 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]) |
114 | 119 |
|
115 | 120 | events = []
|
116 | 121 | async for event in translator.translate(mock_adk_event, "thread_1", "run_1"):
|
117 | 122 | events.append(event)
|
118 | 123 |
|
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 |
121 | 175 |
|
122 | 176 | @pytest.mark.asyncio
|
123 | 177 | async def test_translate_state_delta_event(self, translator, mock_adk_event):
|
|
0 commit comments