Skip to content

Commit fabf559

Browse files
committed
test updates
1 parent 4f4dbec commit fabf559

File tree

7 files changed

+542
-36
lines changed

7 files changed

+542
-36
lines changed

examples/tutorials/00_sync/010_multiturn/tests/test_agent.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
test_sync_agent,
2121
collect_streaming_deltas,
2222
assert_valid_agent_response,
23+
assert_conversation_maintains_context
2324
)
2425

2526
AGENT_NAME = "s010-multiturn"
@@ -29,9 +30,9 @@ def test_multiturn_conversation():
2930
"""Test multi-turn conversation with non-streaming messages."""
3031
with test_sync_agent(agent_name=AGENT_NAME) as test:
3132
messages = [
32-
"Hello",
33-
"How are you?",
34-
"Thank you",
33+
"Hello, can you tell me a litle bit about tennis? I want to you make sure you use the word 'tennis' in each response.",
34+
"Pick one of the things you just mentioned, and dive deeper into it.",
35+
"Can you now output a summary of this conversation",
3536
]
3637

3738
for msg in messages:
@@ -40,6 +41,9 @@ def test_multiturn_conversation():
4041
# Validate response (agent may require OpenAI key)
4142
assert_valid_agent_response(response)
4243

44+
# Validate that "tennis" appears in the response because that is what our model does
45+
assert "tennis" in response.content.lower()
46+
4347
# Verify conversation history
4448
history = test.get_conversation_history()
4549
assert len(history) >= 6, f"Expected >= 6 messages (3 user + 3 agent), got {len(history)}"
@@ -49,9 +53,9 @@ def test_multiturn_streaming():
4953
"""Test multi-turn conversation with streaming messages."""
5054
with test_sync_agent(agent_name=AGENT_NAME) as test:
5155
messages = [
52-
"Hello",
53-
"How are you?",
54-
"Thank you",
56+
"Hello, can you tell me a litle bit about tennis? I want to you make sure you use the word 'tennis' in each response.",
57+
"Pick one of the things you just mentioned, and dive deeper into it.",
58+
"Can you now output a summary of this conversation",
5559
]
5660

5761
for msg in messages:
@@ -65,6 +69,9 @@ def test_multiturn_streaming():
6569
assert len(chunks) > 0, "Should receive chunks"
6670
assert len(aggregated_content) > 0, "Should receive content"
6771

72+
# Validate that "tennis" appears in the response because that is what our model does
73+
assert "tennis" in aggregated_content.lower()
74+
6875
# Verify conversation history (only user messages tracked with streaming)
6976
history = test.get_conversation_history()
7077
assert len(history) >= 3, f"Expected >= 3 user messages, got {len(history)}"

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def test_multiturn_conversation_with_state():
3636

3737
with test_sync_agent(agent_name=AGENT_NAME) as test:
3838
messages = [
39-
"Hello, can you tell me a little bit about tennis? I want to you make sure you use the word 'tennis' in each response.",
39+
"Hello, can you tell me a little bit about tennis? I want you to make sure you use the word 'tennis' in each response.",
4040
"Pick one of the things you just mentioned, and dive deeper into it.",
4141
"Can you now output a summary of this conversation",
4242
]

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,37 @@ async def test_multiturn_with_state_management():
7878
@pytest.mark.asyncio
7979
async def test_streaming_events():
8080
"""Test streaming events from agentic agent."""
81+
# Need client access to check state
82+
client = AsyncAgentex(api_key="test", base_url="http://localhost:5003")
83+
84+
# Get agent ID
85+
agents = await client.agents.list()
86+
agent = next((a for a in agents if a.name == AGENT_NAME), None)
87+
assert agent is not None, f"Agent {AGENT_NAME} not found"
88+
8189
async with test_agentic_agent(agent_name=AGENT_NAME) as test:
90+
# Wait for state initialization
91+
await asyncio.sleep(1)
92+
93+
# Check initial state
94+
states = await client.states.list(agent_id=agent.id, task_id=test.task_id)
95+
assert len(states) == 1
96+
97+
state = states[0].state
98+
assert state is not None
99+
messages = state.get("messages", [])
100+
assert isinstance(messages, list)
101+
assert len(messages) == 1 # Initial system message
102+
assert messages[0] == {
103+
"role": "system",
104+
"content": "You are a helpful assistant that can answer questions.",
105+
}
106+
107+
# Send message and stream response
82108
user_message = "Hello! Stream this response"
83109

84110
events_received = []
111+
agent_response_found = False
85112

86113
# Stream events
87114
async for event in test.send_event_and_stream(user_message, timeout_seconds=30.0):
@@ -90,9 +117,25 @@ async def test_streaming_events():
90117

91118
if event_type == "done":
92119
break
120+
elif event_type == "full":
121+
content = event.get("content", {})
122+
if content.get("author") == "agent":
123+
agent_response_found = True
93124

94125
# Validate we received events
95126
assert len(events_received) > 0, "Should receive streaming events"
127+
assert agent_response_found, "Should receive agent response event"
128+
129+
# Verify state has been updated
130+
await asyncio.sleep(1) # Wait for state update
131+
132+
states = await client.states.list(agent_id=agent.id, task_id=test.task_id)
133+
assert len(states) == 1
134+
state = states[0].state
135+
messages = state.get("messages", [])
136+
137+
assert isinstance(messages, list)
138+
assert len(messages) == 3
96139

97140

98141
if __name__ == "__main__":

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

Lines changed: 105 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,129 @@
1111
1212
Run: pytest tests/test_agent.py -v
1313
"""
14+
import asyncio
1415

1516
import pytest
1617

17-
from agentex.lib.testing import test_agentic_agent
18+
from agentex import AsyncAgentex
19+
from agentex.lib.testing import (
20+
assert_valid_agent_response,
21+
test_agentic_agent,
22+
)
1823

1924
AGENT_NAME = "ab020-streaming"
2025

2126

2227
@pytest.mark.asyncio
2328
async def test_send_event_and_poll():
2429
"""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")
32+
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"
37+
2538
async with test_agentic_agent(agent_name=AGENT_NAME) as test:
26-
response = await test.send_event("Test message", timeout_seconds=30.0)
27-
# Validate we got a response (agent may need OpenAI key)
28-
assert response is not None
29-
assert response.content is not None # May be error message
30-
print(f"Response: {response.content[:150]}")
39+
# Wait for state initialization
40+
await asyncio.sleep(1)
41+
42+
# Check initial state
43+
states = await client.states.list(agent_id=agent.id, task_id=test.task_id)
44+
assert len(states) == 1
45+
46+
state = states[0].state
47+
assert state is not None
48+
messages = state.get("messages", [])
49+
assert isinstance(messages, list)
50+
assert len(messages) == 1 # Initial system message
51+
assert messages[0] == {
52+
"role": "system",
53+
"content": "You are a helpful assistant that can answer questions.",
54+
}
55+
56+
# Send first message
57+
user_message = "Hello! Here is my test message"
58+
response = await test.send_event(user_message, timeout_seconds=30.0)
59+
assert_valid_agent_response(response)
60+
61+
# Wait for state update (agent may or may not update state with messages)
62+
await asyncio.sleep(2)
63+
64+
# Check if state was updated
65+
states = await client.states.list(agent_id=agent.id, task_id=test.task_id)
66+
state = states[0].state
67+
messages = state.get("messages", [])
68+
assert isinstance(messages, list)
69+
assert len(messages) == 3
3170

3271

3372
@pytest.mark.asyncio
3473
async def test_streaming_events():
3574
"""Test streaming event responses."""
75+
# Need client access to check state
76+
client = AsyncAgentex(api_key="test", base_url="http://localhost:5003")
77+
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"
82+
3683
async with test_agentic_agent(agent_name=AGENT_NAME) as test:
37-
events = []
38-
async for event in test.send_event_and_stream("Stream test", timeout_seconds=30.0):
39-
events.append(event)
40-
if event.get("type") == "done":
84+
# Wait for state initialization
85+
await asyncio.sleep(1)
86+
87+
# Check initial state
88+
states = await client.states.list(agent_id=agent.id, task_id=test.task_id)
89+
assert len(states) == 1
90+
91+
state = states[0].state
92+
assert state is not None
93+
messages = state.get("messages", [])
94+
assert isinstance(messages, list)
95+
assert len(messages) == 1 # Initial system message
96+
assert messages[0] == {
97+
"role": "system",
98+
"content": "You are a helpful assistant that can answer questions.",
99+
}
100+
101+
# Send message and stream response
102+
user_message = "Hello! Stream this response"
103+
104+
events_received = []
105+
agent_response_found = False
106+
delta_messages_found = False
107+
108+
# Stream events
109+
async for event in test.send_event_and_stream(user_message, timeout_seconds=30.0):
110+
events_received.append(event)
111+
event_type = event.get("type")
112+
113+
if event_type == "done":
41114
break
42-
assert len(events) > 0
115+
elif event_type == "full":
116+
content = event.get("content", {})
117+
if content.get("author") == "agent":
118+
agent_response_found = True
119+
elif event_type == "delta":
120+
delta_messages_found = True
121+
122+
# Validate we received events
123+
assert len(events_received) > 0, "Should receive streaming events"
124+
assert agent_response_found, "Should receive agent response event"
125+
assert delta_messages_found, "Should receive delta agent message events"
126+
127+
# Verify state has been updated
128+
await asyncio.sleep(1) # Wait for state update
129+
130+
states = await client.states.list(agent_id=agent.id, task_id=test.task_id)
131+
assert len(states) == 1
132+
state = states[0].state
133+
messages = state.get("messages", [])
134+
135+
assert isinstance(messages, list)
136+
assert len(messages) == 3
43137

44138

45139
if __name__ == "__main__":

0 commit comments

Comments
 (0)