|
9 | 9 | Run: pytest tests/test_agent.py -v |
10 | 10 | """ |
11 | 11 |
|
| 12 | +import asyncio |
| 13 | + |
12 | 14 | import pytest |
| 15 | +import pytest_asyncio |
13 | 16 |
|
14 | | -from agentex.lib.testing import async_test_agent, assert_valid_agent_response |
| 17 | +from agentex.lib.testing import async_test_agent, stream_agent_response, assert_valid_agent_response |
| 18 | +from agentex.lib.testing.sessions import AsyncAgentTest |
15 | 19 |
|
16 | 20 | AGENT_NAME = "at020-state-machine" |
17 | 21 |
|
18 | 22 |
|
19 | | -@pytest.mark.asyncio |
20 | | -async def test_agent_basic(): |
21 | | - """Test basic agent functionality.""" |
22 | | - async with async_test_agent(agent_name=AGENT_NAME) as test: |
23 | | - response = await test.send_event("Test message", timeout_seconds=60.0) |
| 23 | + |
| 24 | +@pytest.fixture |
| 25 | +def agent_name(): |
| 26 | + """Return the agent name for testing.""" |
| 27 | + return AGENT_NAME |
| 28 | + |
| 29 | + |
| 30 | +@pytest_asyncio.fixture |
| 31 | +async def test_agent(agent_name: str): |
| 32 | + """Fixture to create a test async agent.""" |
| 33 | + async with async_test_agent(agent_name=agent_name) as test: |
| 34 | + yield test |
| 35 | + |
| 36 | +class TestNonStreamingEvents: |
| 37 | + """Test non-streaming event sending and polling with state machine workflow.""" |
| 38 | + |
| 39 | + @pytest.mark.asyncio |
| 40 | + async def test_send_event_and_poll_simple_query(self, test_agent: AsyncAgentTest): |
| 41 | + """Test basic agent functionality.""" |
| 42 | + # Wait for state initialization |
| 43 | + await asyncio.sleep(1) |
| 44 | + |
| 45 | + # Send a simple message that shouldn't require tool use |
| 46 | + response = await test_agent.send_event("Hello! Please tell me the latest news about AI and AI startups.", timeout_seconds=30.0) |
| 47 | + assert_valid_agent_response(response) |
| 48 | + |
| 49 | + # now we want to clarify that message |
| 50 | + await asyncio.sleep(2) |
| 51 | + next_user_message = "I want to know what viral news came up and which startups failed, got acquired, or became very successful or popular in the last 3 months" |
| 52 | + response = await test_agent.send_event(next_user_message, timeout_seconds=30.0) |
24 | 53 | assert_valid_agent_response(response) |
| 54 | + assert "starting deep research" in response.content.lower(), "Did not start deep research" |
| 55 | + |
| 56 | + |
25 | 57 |
|
| 58 | +class TestStreamingEvents: |
| 59 | + """Test streaming event sending with state machine workflow.""" |
| 60 | + @pytest.mark.asyncio |
| 61 | + async def test_send_event_and_stream(self, test_agent: AsyncAgentTest): |
| 62 | + """Test sending an event and streaming the response.""" |
| 63 | + # Wait for workflow to initialize |
| 64 | + await asyncio.sleep(1) |
26 | 65 |
|
27 | | -@pytest.mark.asyncio |
28 | | -async def test_agent_streaming(): |
29 | | - """Test streaming responses.""" |
30 | | - async with async_test_agent(agent_name=AGENT_NAME) as test: |
31 | | - events = [] |
32 | | - async for event in test.send_event_and_stream("Stream test", timeout_seconds=60.0): |
33 | | - events.append(event) |
34 | | - if event.get("type") == "done": |
| 66 | + # create the first message |
| 67 | + found_agent_message = False |
| 68 | + user_message = "Hello! Please tell me the latest news about AI and AI startups." |
| 69 | + async for event in test_agent.send_event_and_stream(user_message, timeout_seconds=30.0): |
| 70 | + content = event.get("content", {}) |
| 71 | + if content.get("type") == "text" and content.get("author") == "agent": |
| 72 | + found_agent_message = True |
35 | 73 | break |
36 | | - assert len(events) > 0 |
| 74 | + |
| 75 | + await asyncio.sleep(2) |
| 76 | + starting_deep_research_message = False |
| 77 | + uses_tool_requests = False |
| 78 | + |
| 79 | + |
| 80 | + next_user_message = "I want to know what viral news came up and which startups failed, got acquired, or became very successful or popular in the last 3 months" |
| 81 | + # Stream events |
| 82 | + async for event in stream_agent_response(test_agent.client, test_agent.task_id, timeout=60.0): |
| 83 | + event_type = event.get("type") |
| 84 | + content = event.get("content", {}) |
| 85 | + |
| 86 | + if event_type == "connected": |
| 87 | + await test_agent.send_event(next_user_message, timeout_seconds=30.0) |
| 88 | + |
| 89 | + if content.get("type") == "text" and content.get("author") == "agent": |
| 90 | + if "starting deep research" in content.get("content", "").lower(): |
| 91 | + starting_deep_research_message = True |
| 92 | + |
| 93 | + elif content.get("type") == "tool_request": |
| 94 | + # Check if we are using tool requests |
| 95 | + if content.get("author") == "agent": |
| 96 | + uses_tool_requests = True |
| 97 | + |
| 98 | + if starting_deep_research_message and uses_tool_requests: |
| 99 | + break |
| 100 | + |
| 101 | + assert starting_deep_research_message, "Did not start deep research" |
| 102 | + assert uses_tool_requests, "Did not use tool requests" |
37 | 103 |
|
38 | 104 |
|
39 | 105 | if __name__ == "__main__": |
|
0 commit comments