1212Run: pytest tests/test_agent.py -v
1313"""
1414import asyncio
15-
1615import pytest
16+ import pytest_asyncio
17+
18+ from agentex .lib .testing .sessions import AsyncAgentTest
19+ from agentex .lib .testing import stream_agent_response
1720
18- from agentex import AsyncAgentex
1921from agentex .lib .testing import (
20- assert_valid_agent_response ,
2122 async_test_agent ,
23+ assert_valid_agent_response ,
24+ assert_agent_response_contains ,
2225)
2326
2427AGENT_NAME = "ab020-streaming"
2528
29+ @pytest .fixture
30+ def agent_name ():
31+ """Return the agent name for testing."""
32+ return AGENT_NAME
2633
27- @pytest .mark .asyncio
28- async def test_send_event_and_poll ():
29- """Test sending events and polling for responses."""
30- # Need client access to check state
31- client = AsyncAgentex (api_key = "test" , base_url = "http://localhost:5003" )
3234
33- # Get agent ID
34- agents = await client .agents .list ()
35- agent = next ((a for a in agents if a .name == AGENT_NAME ), None )
36- assert agent is not None , f"Agent { AGENT_NAME } not found"
35+ @pytest_asyncio .fixture
36+ async def test_agent (agent_name : str ):
37+ """Fixture to create a test async agent."""
38+ async with async_test_agent (agent_name = agent_name ) as test :
39+ yield test
3740
38- async with async_test_agent (agent_name = AGENT_NAME ) as test :
39- # Wait for state initialization
40- await asyncio .sleep (1 )
4141
42- # Check initial state
43- states = await client .states .list (agent_id = agent .id , task_id = test .task_id )
44- assert len (states ) == 1
42+ class TestNonStreamingEvents :
43+ """Test non-streaming event sending and polling."""
4544
45+ @pytest .mark .asyncio
46+ async def test_send_event_and_poll (self , test_agent : AsyncAgentTest ):
47+ """Test sending an event and polling for the response."""
48+ await asyncio .sleep (1 ) # Wait for state initialization
49+ states = await test_agent .client .states .list (agent_id = test_agent .agent .id , task_id = test_agent .task_id )
50+ assert len (states ) == 1
51+ # Check initial state
4652 state = states [0 ].state
4753 assert state is not None
4854 messages = state .get ("messages" , [])
@@ -53,39 +59,33 @@ async def test_send_event_and_poll():
5359 "content" : "You are a helpful assistant that can answer questions." ,
5460 }
5561
56- # Send first message
5762 user_message = "Hello! Here is my test message"
58- response = await test .send_event (user_message , timeout_seconds = 30.0 )
63+ response = await test_agent .send_event (user_message , timeout_seconds = 30.0 )
5964 assert_valid_agent_response (response )
6065
61- # Wait for state update (agent may or may not update state with messages)
66+ # Wait for state update
6267 await asyncio .sleep (2 )
6368
64- # Check if state was updated
65- states = await client .states .list (agent_id = agent .id , task_id = test .task_id )
69+ # Check if state was updated (optional - depends on agent implementation)
70+ states = await test_agent .client .states .list (agent_id = test_agent .agent .id , task_id = test_agent .task_id )
71+ assert len (states ) == 1
6672 state = states [0 ].state
6773 messages = state .get ("messages" , [])
6874 assert isinstance (messages , list )
69- assert len (messages ) == 3
70-
75+ assert len (messages ) == 3
7176
72- @pytest .mark .asyncio
73- async def test_streaming_events ():
74- """Test streaming event responses."""
75- # Need client access to check state
76- client = AsyncAgentex (api_key = "test" , base_url = "http://localhost:5003" )
7777
78- # Get agent ID
79- agents = await client .agents .list ()
80- agent = next ((a for a in agents if a .name == AGENT_NAME ), None )
81- assert agent is not None , f"Agent { AGENT_NAME } not found"
78+ class TestStreamingEvents :
79+ """Test streaming event sending."""
8280
83- async with async_test_agent (agent_name = AGENT_NAME ) as test :
81+ @pytest .mark .asyncio
82+ async def test_streaming_events (self , test_agent : AsyncAgentTest ):
83+ """Test streaming events from async agent."""
8484 # Wait for state initialization
8585 await asyncio .sleep (1 )
8686
8787 # Check initial state
88- states = await client .states .list (agent_id = agent .id , task_id = test .task_id )
88+ states = await test_agent . client .states .list (agent_id = test_agent . agent .id , task_id = test_agent .task_id )
8989 assert len (states ) == 1
9090
9191 state = states [0 ].state
@@ -102,32 +102,52 @@ async def test_streaming_events():
102102 user_message = "Hello! Stream this response"
103103
104104 events_received = []
105+ user_echo_found = False
105106 agent_response_found = False
106107 delta_messages_found = False
107108
108109 # Stream events
109- async for event in test . send_event_and_stream ( user_message , timeout_seconds = 30.0 ):
110+ async for event in stream_agent_response ( test_agent . client , test_agent . task_id , timeout = 30.0 ):
110111 events_received .append (event )
111112 event_type = event .get ("type" )
112113
113- if event_type == "done" :
114+ if event_type == 'connected' :
115+ await test_agent .send_event (user_message , timeout_seconds = 30.0 )
116+
117+ elif event_type == "done" :
114118 break
119+
115120 elif event_type == "full" :
116121 content = event .get ("content" , {})
117- if content .get ("author" ) == "agent" :
122+ if content .get ("content" ) is None :
123+ continue # Skip empty content
124+
125+ if content .get ("type" ) == "text" and content .get ("author" ) == "agent" :
126+ # Check for agent response to user message
118127 agent_response_found = True
128+ assert user_echo_found , "User echo should be found before agent response"
129+
130+ elif content .get ("type" ) == "text" and content .get ("author" ) == "user" :
131+ # Check for user message echo
132+ if content .get ("content" ) == user_message :
133+ user_echo_found = True
134+
119135 elif event_type == "delta" :
120136 delta_messages_found = True
121137
138+ if agent_response_found and user_echo_found :
139+ break
140+
122141 # Validate we received events
123142 assert len (events_received ) > 0 , "Should receive streaming events"
124143 assert agent_response_found , "Should receive agent response event"
125- assert delta_messages_found , "Should receive delta agent message events"
144+ assert user_echo_found , "Should receive user message event"
145+ assert delta_messages_found , "Should receive delta streaming events"
126146
127147 # Verify state has been updated
128148 await asyncio .sleep (1 ) # Wait for state update
129149
130- states = await client .states .list (agent_id = agent .id , task_id = test .task_id )
150+ states = await test_agent . client .states .list (agent_id = test_agent . agent .id , task_id = test_agent .task_id )
131151 assert len (states ) == 1
132152 state = states [0 ].state
133153 messages = state .get ("messages" , [])
0 commit comments