@@ -289,3 +289,138 @@ async def test_list_messages_pagination(
289289 assert {(d ["id" ], d ["content" ]["content" ]) for d in paginated_messages } == {
290290 (d .id , d .content .content ) for d in test_pagination_messages
291291 }
292+
293+ async def test_list_messages_with_order_by (
294+ self , isolated_client , isolated_repositories
295+ ):
296+ """Test that list messages endpoint supports order_by parameter"""
297+ # Given - Create an agent and task
298+ agent_repo = isolated_repositories ["agent_repository" ]
299+ agent = AgentEntity (
300+ id = orm_id (),
301+ name = "order-by-message-agent" ,
302+ description = "Agent for order_by message testing" ,
303+ acp_url = "http://test-acp:8000" ,
304+ acp_type = ACPType .SYNC ,
305+ )
306+ await agent_repo .create (agent )
307+
308+ task_repo = isolated_repositories ["task_repository" ]
309+ task = TaskEntity (
310+ id = orm_id (),
311+ name = "order-by-message-task" ,
312+ status = TaskStatus .RUNNING ,
313+ status_reason = "Task for order_by message testing" ,
314+ )
315+ await task_repo .create (agent_id = agent .id , task = task )
316+
317+ # Create multiple messages
318+ message_repo = isolated_repositories ["task_message_repository" ]
319+ messages = []
320+ for i in range (3 ):
321+ message = TaskMessageEntity (
322+ id = orm_id (),
323+ task_id = task .id ,
324+ content = TextContentEntity (
325+ type = "text" , author = "user" , content = f"Order test message { i } "
326+ ),
327+ streaming_status = "DONE" ,
328+ )
329+ messages .append (await message_repo .create (message ))
330+
331+ # When - Request messages with order_by=created_at and order_direction=asc
332+ response_asc = await isolated_client .get (
333+ "/messages" ,
334+ params = {
335+ "task_id" : task .id ,
336+ "order_by" : "created_at" ,
337+ "order_direction" : "asc" ,
338+ },
339+ )
340+
341+ # Then - Should return messages in ascending order
342+ assert response_asc .status_code == 200
343+ messages_asc = response_asc .json ()
344+ assert len (messages_asc ) == 3
345+
346+ # Verify ascending order
347+ for i in range (len (messages_asc ) - 1 ):
348+ assert messages_asc [i ]["created_at" ] <= messages_asc [i + 1 ]["created_at" ]
349+
350+ # When - Request messages with order_by=created_at and order_direction=desc
351+ response_desc = await isolated_client .get (
352+ "/messages" ,
353+ params = {
354+ "task_id" : task .id ,
355+ "order_by" : "created_at" ,
356+ "order_direction" : "desc" ,
357+ },
358+ )
359+
360+ # Then - Should return messages in descending order
361+ assert response_desc .status_code == 200
362+ messages_desc = response_desc .json ()
363+ assert len (messages_desc ) == 3
364+
365+ # Verify descending order
366+ for i in range (len (messages_desc ) - 1 ):
367+ assert messages_desc [i ]["created_at" ] >= messages_desc [i + 1 ]["created_at" ]
368+
369+ # Verify asc and desc return different orderings (first element of asc should be in last position of desc)
370+ # Note: We only check the first element reversal since items with identical timestamps
371+ # may have unpredictable relative ordering
372+ assert messages_asc [0 ]["id" ] == messages_desc [- 1 ]["id" ]
373+
374+ async def test_list_messages_order_by_defaults_to_desc (
375+ self , isolated_client , isolated_repositories
376+ ):
377+ """Test that order_direction defaults to desc for messages (newest first)"""
378+ # Given - Create an agent and task
379+ agent_repo = isolated_repositories ["agent_repository" ]
380+ agent = AgentEntity (
381+ id = orm_id (),
382+ name = "order-default-message-agent" ,
383+ description = "Agent for order default message testing" ,
384+ acp_url = "http://test-acp:8000" ,
385+ acp_type = ACPType .SYNC ,
386+ )
387+ await agent_repo .create (agent )
388+
389+ task_repo = isolated_repositories ["task_repository" ]
390+ task = TaskEntity (
391+ id = orm_id (),
392+ name = "order-default-message-task" ,
393+ status = TaskStatus .RUNNING ,
394+ status_reason = "Task for order default message testing" ,
395+ )
396+ await task_repo .create (agent_id = agent .id , task = task )
397+
398+ # Create multiple messages
399+ message_repo = isolated_repositories ["task_message_repository" ]
400+ for i in range (3 ):
401+ message = TaskMessageEntity (
402+ id = orm_id (),
403+ task_id = task .id ,
404+ content = TextContentEntity (
405+ type = "text" , author = "user" , content = f"Default order message { i } "
406+ ),
407+ streaming_status = "DONE" ,
408+ )
409+ await message_repo .create (message )
410+
411+ # When - Request messages without specifying order_direction
412+ response = await isolated_client .get (
413+ "/messages" ,
414+ params = {"task_id" : task .id },
415+ )
416+
417+ # Then - Should return messages successfully
418+ assert response .status_code == 200
419+ messages = response .json ()
420+ assert len (messages ) == 3
421+
422+ # Verify descending order - items with same timestamp may have any relative order,
423+ # so we only check that timestamps are non-increasing (allowing equal timestamps)
424+ timestamps = [m ["created_at" ] for m in messages ]
425+ # Sort timestamps descending and verify the returned order matches a valid descending order
426+ assert timestamps == sorted (timestamps , reverse = True )
0 commit comments