1414import pytest
1515import pytest_asyncio
1616
17- from agentex import AsyncAgentex
1817from agentex .lib .testing import async_test_agent , stream_agent_response , assert_valid_agent_response
18+ from agentex .lib .testing .sessions import AsyncAgentTest
1919from agentex .types .text_content_param import TextContentParam
2020from agentex .types .task_message_content import TextContent
2121
@@ -35,53 +35,46 @@ async def test_agent(agent_name: str):
3535 yield test
3636
3737
38- @pytest .mark .asyncio
39- async def test_single_event_and_poll ():
40- """Test sending a single event and polling for response."""
41- async with async_test_agent (agent_name = AGENT_NAME ) as test :
42- response = await test .send_event ("Process this single event" , timeout_seconds = 30.0 )
38+ class TestNonStreamingEvents :
39+ """Test non-streaming event sending and polling."""
40+
41+ @pytest .mark .asyncio
42+ async def test_send_event_and_poll (self , test_agent : AsyncAgentTest ):
43+ """Test sending a single event and polling for response."""
44+ response = await test_agent .send_event ("Process this single event" , timeout_seconds = 30.0 )
4345 assert_valid_agent_response (response )
4446 assert "Processed event IDs" in response .content
4547
4648
47- @pytest .mark .asyncio
48- async def test_batch_events_and_poll ():
49- """Test sending events and polling for responses."""
50- # Need client access to send events directly
51- client = AsyncAgentex (api_key = "test" , base_url = "http://localhost:5003" )
52-
53- # Get agent ID
54- agents = await client .agents .list ()
55- agent = next ((a for a in agents if a .name == AGENT_NAME ), None )
56- assert agent is not None , f"Agent { AGENT_NAME } not found"
57-
58- num_events = 7
59- async with async_test_agent (agent_name = AGENT_NAME ) as test :
49+ @pytest .mark .asyncio
50+ async def test_batch_events_and_poll (self , test_agent : AsyncAgentTest ):
51+ """Test sending events and polling for responses."""
52+ num_events = 7
6053 for i in range (num_events ):
6154 event_content = TextContentParam (type = "text" , author = "user" , content = f"Batch event { i + 1 } " )
62- await client .agents .send_event (
63- agent_id = agent .id , params = {"task_id" : test .task_id , "content" : event_content }
55+ await test_agent . client .agents .send_event (
56+ agent_id = test_agent . agent .id , params = {"task_id" : test_agent .task_id , "content" : event_content }
6457 )
6558 await asyncio .sleep (0.1 ) # Small delay to ensure ordering
6659
6760 ## there should be at least 2 agent responses to ensure that not all of the events are processed
68- await test .send_event ("Process this single event" , timeout_seconds = 30.0 )
61+ await test_agent .send_event ("Process this single event" , timeout_seconds = 30.0 )
6962 # Wait for processing to complete (5 events * 5 seconds each = 25s + buffer)
70- messages = []
71- for i in range (8 ):
72- messages = await client .messages .list (task_id = test .task_id )
73- if len (messages ) >= 2 :
63+ agent_messages = []
64+ for _ in range (8 ):
65+ agent_messages = await test_agent . client .messages .list (task_id = test_agent .task_id )
66+ if len (agent_messages ) >= 2 :
7467 break
7568 await asyncio .sleep (5 )
76- assert len (messages ) > 0 , "Should have received at least one agent response"
69+ assert len (agent_messages ) > 0 , "Should have received at least one agent response"
7770 # PROOF OF BATCHING: Should have fewer responses than events sent
78- assert len (messages ) < num_events , (
79- f"Expected batching to result in fewer responses than { num_events } events, got { len (messages )} "
71+ assert len (agent_messages ) < num_events , (
72+ f"Expected batching to result in fewer responses than { num_events } events, got { len (agent_messages )} "
8073 )
8174
8275 # Analyze each batch response to count how many events were in each batch
8376 found_batch_with_multiple_events = False
84- for msg in messages :
77+ for msg in agent_messages :
8578 assert isinstance (msg .content , TextContent )
8679 response = msg .content .content
8780 # Count event IDs in this response (they're in a list like ['id1', 'id2', ...])
@@ -96,29 +89,23 @@ async def test_batch_events_and_poll():
9689 assert found_batch_with_multiple_events , "Should have found a batch with multiple events"
9790
9891
99- @pytest .mark .asyncio
100- async def test_batched_streaming ():
101- """Test streaming responses."""
102- # Need client access to send events directly
103- client = AsyncAgentex (api_key = "test" , base_url = "http://localhost:5003" )
104-
105- # Get agent ID
106- agents = await client .agents .list ()
107- agent = next ((a for a in agents if a .name == AGENT_NAME ), None )
108- assert agent is not None , f"Agent { AGENT_NAME } not found"
92+ class TestStreamingEvents :
93+ """Test streaming event sending."""
10994
110- num_events = 10
111- async with async_test_agent (agent_name = AGENT_NAME ) as test :
95+ @pytest .mark .asyncio
96+ async def test_batched_streaming (self , test_agent : AsyncAgentTest ):
97+ """Test streaming responses."""
98+ num_events = 10
11299 for i in range (num_events ):
113100 event_content = TextContentParam (type = "text" , author = "user" , content = f"Batch event { i + 1 } " )
114- await client .agents .send_event (
115- agent_id = agent .id , params = {"task_id" : test .task_id , "content" : event_content }
101+ await test_agent . client .agents .send_event (
102+ agent_id = test_agent . agent .id , params = {"task_id" : test_agent .task_id , "content" : event_content }
116103 )
117104 await asyncio .sleep (0.1 ) # Small delay to ensure ordering
118105
119106 # Stream events
120107 agent_messages = []
121- async for event in stream_agent_response (client , test .task_id , timeout = 30.0 ):
108+ async for event in stream_agent_response (test_agent . client , test_agent .task_id , timeout = 30.0 ):
122109 # Collect agent text messages
123110 if event .get ("type" ) == "full" :
124111 content = event .get ("content" , {})
0 commit comments