1919import os
2020
2121import pytest
22- from test_utils .sync import collect_streaming_response
22+ from test_utils .sync import validate_text_in_string , collect_streaming_response
2323
2424from agentex import Agentex
2525from agentex .types import TextContent , TextContentParam
@@ -57,19 +57,27 @@ class TestNonStreamingMessages:
5757 """Test non-streaming message sending."""
5858
5959 def test_send_message (self , client : Agentex , agent_name : str , agent_id : str ):
60- """Test sending a message and receiving a response."""
60+ """
61+ Test message ordering by sending messages about distinct topics.
62+
63+ This validates that the agent receives messages in chronological order.
64+ If messages are reversed (newest first), the agent would respond about
65+ the wrong topic.
66+ """
6167 task_response = client .agents .create_task (agent_id , params = ParamsCreateTaskRequest (name = uuid .uuid1 ().hex ))
6268 task = task_response .result
6369
6470 assert task is not None
6571
66- messages = [
67- "Hello, can you tell me a little bit about tennis? I want you to make sure you use the word 'tennis' in each response." ,
68- "Pick one of the things you just mentioned, and dive deeper into it." ,
69- "Can you now output a summary of this conversation" ,
72+ # Each message asks about a distinct topic with a required keyword in response
73+ # This validates message ordering: if order is wrong, agent responds about wrong topic
74+ messages_and_expected_keywords = [
75+ ("Tell me about tennis. You must include the word 'tennis' in your response." , "tennis" ),
76+ ("Now tell me about basketball. You must include the word 'basketball' in your response. Do not mention tennis." , "basketball" ),
77+ ("Now tell me about soccer. You must include the word 'soccer' in your response. Do not mention tennis or basketball." , "soccer" ),
7078 ]
7179
72- for i , msg in enumerate (messages ):
80+ for i , ( msg , expected_keyword ) in enumerate (messages_and_expected_keywords ):
7381 response = client .agents .send_message (
7482 agent_name = agent_name ,
7583 params = ParamsSendMessageRequest (
@@ -88,6 +96,8 @@ def test_send_message(self, client: Agentex, agent_name: str, agent_id: str):
8896 content = message .content
8997 assert content is not None
9098 assert isinstance (content , TextContent ) and isinstance (content .content , str )
99+ # Validate response contains the expected keyword for THIS message's topic
100+ validate_text_in_string (expected_keyword , content .content .lower ())
91101
92102 states = client .states .list (agent_id = agent_id , task_id = task .id )
93103 assert len (states ) == 1
@@ -105,19 +115,28 @@ class TestStreamingMessages:
105115 """Test streaming message sending."""
106116
107117 def test_send_stream_message (self , client : Agentex , agent_name : str , agent_id : str ):
108- """Test streaming messages in a multi-turn conversation."""
118+ """
119+ Test message ordering with streaming by sending messages about distinct topics.
120+
121+ This validates that the agent receives messages in chronological order.
122+ If messages are reversed (newest first), the agent would respond about
123+ the wrong topic.
124+ """
109125 # create a task for this specific conversation
110126 task_response = client .agents .create_task (agent_id , params = ParamsCreateTaskRequest (name = uuid .uuid1 ().hex ))
111127 task = task_response .result
112128
113129 assert task is not None
114- messages = [
115- "Hello, can you tell me a little bit about tennis? I want you to make sure you use the word 'tennis' in each response." ,
116- "Pick one of the things you just mentioned, and dive deeper into it." ,
117- "Can you now output a summary of this conversation" ,
130+
131+ # Each message asks about a distinct topic with a required keyword in response
132+ # This validates message ordering: if order is wrong, agent responds about wrong topic
133+ messages_and_expected_keywords = [
134+ ("Tell me about tennis. You must include the word 'tennis' in your response." , "tennis" ),
135+ ("Now tell me about basketball. You must include the word 'basketball' in your response. Do not mention tennis." , "basketball" ),
136+ ("Now tell me about soccer. You must include the word 'soccer' in your response. Do not mention tennis or basketball." , "soccer" ),
118137 ]
119138
120- for i , msg in enumerate (messages ):
139+ for i , ( msg , expected_keyword ) in enumerate (messages_and_expected_keywords ):
121140 stream = client .agents .send_message_stream (
122141 agent_name = agent_name ,
123142 params = ParamsSendMessageRequest (
@@ -137,6 +156,9 @@ def test_send_stream_message(self, client: Agentex, agent_name: str, agent_id: s
137156 # this is using the chat_completion_stream, so we will be getting chunks of data
138157 assert len (chunks ) > 1 , "No chunks received in streaming response."
139158
159+ # Validate response contains the expected keyword for THIS message's topic
160+ validate_text_in_string (expected_keyword , aggregated_content .lower ())
161+
140162 states = client .states .list (agent_id = agent_id , task_id = task .id )
141163 assert len (states ) == 1
142164
0 commit comments