Skip to content

Commit a7cd369

Browse files
committed
making testing better
1 parent b1d05a6 commit a7cd369

File tree

16 files changed

+732
-446
lines changed

16 files changed

+732
-446
lines changed

.github/workflows/agentex-tutorials-test.yml

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,17 @@ jobs:
2020
id: get-tutorials
2121
run: |
2222
cd examples/tutorials
23-
# Find all tutorials and exclude specific temporal ones
23+
# Find all tutorials with a manifest.yaml
2424
all_tutorials=$(find . -name "manifest.yaml" -exec dirname {} \; | sort | sed 's|^\./||')
2525
26-
# Filter out the specified temporal tutorials that are being updated
27-
filtered_tutorials=$(echo "$all_tutorials" | grep -v -E "(temporal)")
26+
# Include all tutorials (temporal tutorials are now included)
27+
filtered_tutorials="$all_tutorials"
2828
2929
# Convert to JSON array
3030
tutorials=$(echo "$filtered_tutorials" | jq -R -s -c 'split("\n") | map(select(length > 0))')
3131
3232
echo "tutorials=$tutorials" >> $GITHUB_OUTPUT
3333
echo "All tutorials found: $(echo "$all_tutorials" | wc -l)"
34-
echo "Filtered tutorials: $(echo "$filtered_tutorials" | wc -l)"
35-
echo "Excluded tutorials:"
36-
echo "$all_tutorials" | grep -E "(10_temporal/050_|10_temporal/070_|10_temporal/080_)" || echo " (none matched exclusion pattern)"
3734
echo "Final tutorial list: $tutorials"
3835
3936
test-tutorial:

examples/tutorials/00_sync/020_streaming/tests/test_agent.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,4 +151,3 @@ def test_send_stream_message(self, client: Agentex, agent_name: str, agent_id: s
151151

152152
if __name__ == "__main__":
153153
pytest.main([__file__, "-v"])
154-

examples/tutorials/10_async/00_base/000_hello_acp/tests/test_agent.py

Lines changed: 65 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -73,31 +73,55 @@ async def test_send_event_and_poll(self, client: AsyncAgentex, agent_id: str):
7373
assert task is not None
7474

7575
# Poll for the initial task creation message
76-
async for message in poll_messages(
77-
client=client,
78-
task_id=task.id,
79-
timeout=30,
80-
sleep_interval=1.0,
81-
):
82-
assert isinstance(message, TaskMessage)
83-
if message.content and message.content.type == "text" and message.content.author == "agent":
84-
assert "Hello! I've received your task" in message.content.content
85-
break
76+
task_creation_message_found = False
77+
78+
async def poll_for_task_creation() -> None:
79+
nonlocal task_creation_message_found
80+
async for message in poll_messages(
81+
client=client,
82+
task_id=task.id,
83+
timeout=30,
84+
sleep_interval=1.0,
85+
):
86+
assert isinstance(message, TaskMessage)
87+
if message.content and message.content.type == "text" and message.content.author == "agent":
88+
assert "Hello! I've received your task" in message.content.content
89+
task_creation_message_found = True
90+
break
91+
92+
try:
93+
await asyncio.wait_for(poll_for_task_creation(), timeout=30)
94+
except asyncio.TimeoutError:
95+
pytest.fail("Polling timed out waiting for task creation message")
96+
97+
assert task_creation_message_found, "Task creation message not found"
8698

8799
# Send an event and poll for response
88100
user_message = "Hello, this is a test message!"
89-
async for message in send_event_and_poll_yielding(
90-
client=client,
91-
agent_id=agent_id,
92-
task_id=task.id,
93-
user_message=user_message,
94-
timeout=30,
95-
sleep_interval=1.0,
96-
):
97-
assert isinstance(message, TaskMessage)
98-
if message.content and message.content.type == "text" and message.content.author == "agent":
99-
assert "Hello! I've received your task" in message.content.content
100-
break
101+
agent_response_found = False
102+
103+
async def poll_for_response() -> None:
104+
nonlocal agent_response_found
105+
async for message in send_event_and_poll_yielding(
106+
client=client,
107+
agent_id=agent_id,
108+
task_id=task.id,
109+
user_message=user_message,
110+
timeout=30,
111+
sleep_interval=1.0,
112+
):
113+
assert isinstance(message, TaskMessage)
114+
if message.content and message.content.type == "text" and message.content.author == "agent":
115+
assert "Hello! I've received your message" in message.content.content
116+
agent_response_found = True
117+
break
118+
119+
try:
120+
await asyncio.wait_for(poll_for_response(), timeout=30)
121+
except asyncio.TimeoutError:
122+
pytest.fail("Polling timed out waiting for agent response")
123+
124+
assert agent_response_found, "Agent response not found"
101125

102126

103127
class TestStreamingEvents:
@@ -111,18 +135,26 @@ async def test_send_event_and_stream(self, client: AsyncAgentex, agent_id: str):
111135

112136
assert task is not None
113137
task_creation_found = False
138+
114139
# Poll for the initial task creation message
115-
async for message in poll_messages(
116-
client=client,
117-
task_id=task.id,
118-
timeout=30,
119-
sleep_interval=1.0,
120-
):
121-
assert isinstance(message, TaskMessage)
122-
if message.content and message.content.type == "text" and message.content.author == "agent":
123-
assert "Hello! I've received your task" in message.content.content
124-
task_creation_found = True
125-
break
140+
async def poll_for_task_creation() -> None:
141+
nonlocal task_creation_found
142+
async for message in poll_messages(
143+
client=client,
144+
task_id=task.id,
145+
timeout=30,
146+
sleep_interval=1.0,
147+
):
148+
assert isinstance(message, TaskMessage)
149+
if message.content and message.content.type == "text" and message.content.author == "agent":
150+
assert "Hello! I've received your task" in message.content.content
151+
task_creation_found = True
152+
break
153+
154+
try:
155+
await asyncio.wait_for(poll_for_task_creation(), timeout=30)
156+
except asyncio.TimeoutError:
157+
pytest.fail("Polling timed out waiting for task creation message")
126158

127159
assert task_creation_found, "Task creation message not found in poll"
128160

examples/tutorials/10_async/00_base/010_multiturn/tests/test_agent.py

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -89,25 +89,48 @@ async def test_send_event_and_poll(self, client: AsyncAgentex, agent_id: str):
8989

9090
user_message = "Hello! Here is my test message"
9191
messages = []
92-
async for message in send_event_and_poll_yielding(
93-
client=client,
94-
agent_id=agent_id,
95-
task_id=task.id,
96-
user_message=user_message,
97-
timeout=30,
98-
sleep_interval=1.0,
99-
):
100-
messages.append(message)
101-
if len(messages) == 1:
102-
assert message.content == TextContent(
103-
author="user",
104-
content=user_message,
105-
type="text",
106-
)
107-
else:
108-
assert message.content is not None
109-
assert message.content.author == "agent"
110-
break
92+
93+
# Flags to track what we've received
94+
user_message_found = False
95+
agent_response_found = False
96+
97+
async def poll_for_messages() -> None:
98+
nonlocal user_message_found, agent_response_found
99+
100+
async for message in send_event_and_poll_yielding(
101+
client=client,
102+
agent_id=agent_id,
103+
task_id=task.id,
104+
user_message=user_message,
105+
timeout=30,
106+
sleep_interval=1.0,
107+
):
108+
messages.append(message)
109+
110+
# Validate messages as they arrive
111+
if message.content and hasattr(message.content, 'author'):
112+
if message.content.author == "user" and message.content.content == user_message:
113+
assert message.content == TextContent(
114+
author="user",
115+
content=user_message,
116+
type="text",
117+
)
118+
user_message_found = True
119+
elif message.content.author == "agent":
120+
assert user_message_found, "Agent response arrived before user message"
121+
agent_response_found = True
122+
123+
# Exit early if we've found all expected messages
124+
if user_message_found and agent_response_found:
125+
break
126+
127+
try:
128+
await asyncio.wait_for(poll_for_messages(), timeout=30)
129+
except asyncio.TimeoutError:
130+
pytest.fail("Polling timed out after 30s waiting for expected messages")
131+
132+
assert user_message_found, "User message not found"
133+
assert agent_response_found, "Agent response not found"
111134

112135
await asyncio.sleep(1) # wait for state to be updated
113136
states = await client.states.list(agent_id=agent_id, task_id=task.id)

examples/tutorials/10_async/00_base/020_streaming/tests/test_agent.py

Lines changed: 69 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -89,32 +89,48 @@ async def test_send_event_and_poll(self, client: AsyncAgentex, agent_id: str):
8989

9090
user_message = "Hello! Here is my test message"
9191
messages = []
92-
async for message in send_event_and_poll_yielding(
93-
client=client,
94-
agent_id=agent_id,
95-
task_id=task.id,
96-
user_message=user_message,
97-
timeout=30,
98-
sleep_interval=1.0,
99-
yield_updates=False,
100-
):
101-
102-
messages.append(message)
103-
104-
assert len(messages) > 0
105-
# the first message should be the agent re-iterating what the user sent
106-
assert isinstance(messages, List)
107-
assert len(messages) == 2
108-
first_message: TaskMessage = messages[0]
109-
assert first_message.content == TextContent(
110-
author="user",
111-
content=user_message,
112-
type="text",
113-
)
114-
115-
second_message: TaskMessage = messages[1]
116-
assert second_message.content is not None
117-
assert second_message.content.author == "agent"
92+
93+
# Flags to track what we've received
94+
user_message_found = False
95+
agent_response_found = False
96+
97+
async def poll_for_messages() -> None:
98+
nonlocal user_message_found, agent_response_found
99+
100+
async for message in send_event_and_poll_yielding(
101+
client=client,
102+
agent_id=agent_id,
103+
task_id=task.id,
104+
user_message=user_message,
105+
timeout=30,
106+
sleep_interval=1.0,
107+
yield_updates=False,
108+
):
109+
messages.append(message)
110+
111+
# Validate messages as they come in
112+
if message.content and hasattr(message.content, 'author'):
113+
if message.content.author == "user" and message.content.content == user_message:
114+
user_message_found = True
115+
elif message.content.author == "agent":
116+
# Agent response should come after user message
117+
assert user_message_found, "Agent response arrived before user message"
118+
agent_response_found = True
119+
120+
# Exit early if we've found all expected messages
121+
if user_message_found and agent_response_found:
122+
break
123+
124+
# Run polling with timeout
125+
try:
126+
await asyncio.wait_for(poll_for_messages(), timeout=30)
127+
except asyncio.TimeoutError:
128+
pytest.fail("Polling timed out after 30s waiting for expected messages")
129+
130+
# Validate we received expected messages
131+
assert len(messages) >= 2, "Expected at least 2 messages (user + agent)"
132+
assert user_message_found, "User message not found"
133+
assert agent_response_found, "Agent response not found"
118134

119135
# assert the state has been updated
120136
await asyncio.sleep(1) # wait for state to be updated
@@ -158,41 +174,49 @@ async def test_send_event_and_stream(self, client: AsyncAgentex, agent_id: str):
158174
# Collect events from stream
159175
all_events = []
160176

177+
# Flags to track what we've received
178+
user_message_found = False
179+
full_agent_message_found = False
180+
delta_messages_found = False
181+
161182
async def stream_messages() -> None:
183+
nonlocal user_message_found, full_agent_message_found, delta_messages_found
184+
162185
async for event in stream_agent_response(
163186
client=client,
164187
task_id=task.id,
165188
timeout=15,
166189
):
167190
all_events.append(event)
168191

192+
# Check events as they arrive
193+
event_type = event.get("type")
194+
if event_type == "full":
195+
content = event.get("content", {})
196+
if content.get("content") == user_message and content.get("author") == "user":
197+
user_message_found = True
198+
elif content.get("author") == "agent":
199+
full_agent_message_found = True
200+
elif event_type == "delta":
201+
delta_messages_found = True
202+
203+
# Exit early if we've found all expected messages
204+
if user_message_found and full_agent_message_found and delta_messages_found:
205+
break
206+
169207
stream_task = asyncio.create_task(stream_messages())
170208

171209
event_content = TextContentParam(type="text", author="user", content=user_message)
172210
await client.agents.send_event(agent_id=agent_id, params={"task_id": task.id, "content": event_content})
173211

174-
# Wait for streaming to complete
175-
await stream_task
212+
# Wait for streaming to complete (with timeout)
213+
try:
214+
await asyncio.wait_for(stream_task, timeout=15)
215+
except asyncio.TimeoutError:
216+
pytest.fail("Stream timed out after 15s waiting for expected messages")
176217

177218
# Validate we received events
178219
assert len(all_events) > 0, "No events received in streaming response"
179-
180-
# Check for user message, full agent response, and delta messages
181-
user_message_found = False
182-
full_agent_message_found = False
183-
delta_messages_found = False
184-
185-
for event in all_events:
186-
event_type = event.get("type")
187-
if event_type == "full":
188-
content = event.get("content", {})
189-
if content.get("content") == user_message and content.get("author") == "user":
190-
user_message_found = True
191-
elif content.get("author") == "agent":
192-
full_agent_message_found = True
193-
elif event_type == "delta":
194-
delta_messages_found = True
195-
196220
assert user_message_found, "User message not found in stream"
197221
assert full_agent_message_found, "Full agent message not found in stream"
198222
assert delta_messages_found, "Delta messages not found in stream (streaming response expected)"

0 commit comments

Comments
 (0)