|
| 1 | +#!/usr/bin/env python |
| 2 | +"""Integration test: mixed partials with non-LRO calls before final LRO. |
| 3 | +
|
| 4 | +Scenario: |
| 5 | +- Stream text in partial chunks |
| 6 | +- Mid-stream, a non-LRO function call appears (should close text and emit tool events) |
| 7 | +- Finally, an LRO function call arrives (should close any open text and emit LRO tool events) |
| 8 | +
|
| 9 | +Asserts order, deduplication, and correct tool ids. |
| 10 | +""" |
| 11 | + |
| 12 | +import pytest |
| 13 | +from unittest.mock import MagicMock, AsyncMock, Mock, patch |
| 14 | + |
| 15 | +from ag_ui.core import ( |
| 16 | + RunAgentInput, UserMessage |
| 17 | +) |
| 18 | +from ag_ui_adk import ADKAgent |
| 19 | + |
| 20 | + |
| 21 | +@pytest.fixture |
| 22 | +def adk_agent_instance(): |
| 23 | + from google.adk.agents import Agent |
| 24 | + mock_agent = Mock(spec=Agent) |
| 25 | + mock_agent.name = "test_agent" |
| 26 | + return ADKAgent(adk_agent=mock_agent, app_name="test_app", user_id="test_user") |
| 27 | + |
| 28 | + |
| 29 | +@pytest.mark.asyncio |
| 30 | +async def test_mixed_partials_non_lro_then_lro(adk_agent_instance): |
| 31 | + # Helper to create partial text events |
| 32 | + def mk_partial(text): |
| 33 | + e = MagicMock() |
| 34 | + e.author = "assistant" |
| 35 | + e.content = MagicMock(); e.content.parts = [MagicMock(text=text)] |
| 36 | + e.partial = True |
| 37 | + e.turn_complete = False |
| 38 | + e.is_final_response = lambda: False |
| 39 | + # No function responses in these partials |
| 40 | + e.get_function_responses = lambda: [] |
| 41 | + e.get_function_calls = lambda: [] |
| 42 | + return e |
| 43 | + |
| 44 | + # First partial text only |
| 45 | + evt1 = mk_partial("Hello") |
| 46 | + |
| 47 | + # Second partial: text + non-LRO function call |
| 48 | + normal_id = "normal-999" |
| 49 | + normal_func = MagicMock(); normal_func.id = normal_id; normal_func.name = "regular_tool"; normal_func.args = {"b": 2} |
| 50 | + evt2 = mk_partial(" world") |
| 51 | + evt2.get_function_calls = lambda: [normal_func] |
| 52 | + evt2.long_running_tool_ids = [] |
| 53 | + |
| 54 | + # Final: LRO function call |
| 55 | + lro_id = "lro-777" |
| 56 | + lro_func = MagicMock(); lro_func.id = lro_id; lro_func.name = "long_running_tool"; lro_func.args = {"v": 1} |
| 57 | + lro_part = MagicMock(); lro_part.function_call = lro_func |
| 58 | + |
| 59 | + evt3 = MagicMock() |
| 60 | + evt3.author = "assistant" |
| 61 | + evt3.content = MagicMock(); evt3.content.parts = [lro_part] |
| 62 | + evt3.partial = False |
| 63 | + evt3.turn_complete = True |
| 64 | + evt3.is_final_response = lambda: True |
| 65 | + evt3.get_function_calls = lambda: [] |
| 66 | + evt3.get_function_responses = lambda: [] |
| 67 | + evt3.long_running_tool_ids = [lro_id] |
| 68 | + |
| 69 | + async def mock_run_async(*args, **kwargs): |
| 70 | + yield evt1 |
| 71 | + yield evt2 |
| 72 | + yield evt3 |
| 73 | + |
| 74 | + mock_runner = AsyncMock(); mock_runner.run_async = mock_run_async |
| 75 | + |
| 76 | + sample_input = RunAgentInput( |
| 77 | + thread_id="thread_mixed", |
| 78 | + run_id="run_mixed", |
| 79 | + messages=[UserMessage(id="u1", role="user", content="go")], |
| 80 | + tools=[], context=[], state={}, forwarded_props={}, |
| 81 | + ) |
| 82 | + |
| 83 | + with patch.object(adk_agent_instance, "_create_runner", return_value=mock_runner): |
| 84 | + events = [] |
| 85 | + async for e in adk_agent_instance.run(sample_input): |
| 86 | + events.append(e) |
| 87 | + |
| 88 | + types = [str(ev.type).split(".")[-1] for ev in events] |
| 89 | + |
| 90 | + # Expect at least one START and 2 CONTENTs from streaming |
| 91 | + assert types.count("TEXT_MESSAGE_START") == 1 |
| 92 | + assert types.count("TEXT_MESSAGE_CONTENT") >= 2 |
| 93 | + |
| 94 | + # Non-LRO tool call should appear exactly once |
| 95 | + normal_starts = [i for i, ev in enumerate(events) if str(ev.type).endswith("TOOL_CALL_START") and getattr(ev, "tool_call_id", None) == normal_id] |
| 96 | + normal_args = [i for i, ev in enumerate(events) if str(ev.type).endswith("TOOL_CALL_ARGS") and getattr(ev, "tool_call_id", None) == normal_id] |
| 97 | + normal_ends = [i for i, ev in enumerate(events) if str(ev.type).endswith("TOOL_CALL_END") and getattr(ev, "tool_call_id", None) == normal_id] |
| 98 | + assert len(normal_starts) == len(normal_args) == len(normal_ends) == 1 |
| 99 | + |
| 100 | + # Ensure a TEXT_MESSAGE_END precedes the normal tool start |
| 101 | + text_ends = [i for i, t in enumerate(types) if t == "TEXT_MESSAGE_END"] |
| 102 | + assert len(text_ends) >= 1 |
| 103 | + assert text_ends[-1] < normal_starts[0], "TEXT_MESSAGE_END must precede first non-LRO TOOL_CALL_START" |
| 104 | + |
| 105 | + # LRO tool call should appear exactly once and after the non-LRO |
| 106 | + lro_starts = [i for i, ev in enumerate(events) if str(ev.type).endswith("TOOL_CALL_START") and getattr(ev, "tool_call_id", None) == lro_id] |
| 107 | + lro_args = [i for i, ev in enumerate(events) if str(ev.type).endswith("TOOL_CALL_ARGS") and getattr(ev, "tool_call_id", None) == lro_id] |
| 108 | + lro_ends = [i for i, ev in enumerate(events) if str(ev.type).endswith("TOOL_CALL_END") and getattr(ev, "tool_call_id", None) == lro_id] |
| 109 | + assert len(lro_starts) == len(lro_args) == len(lro_ends) == 1 |
| 110 | + assert lro_starts[0] > normal_starts[0] |
| 111 | + |
0 commit comments