|
11 | 11 |
|
12 | 12 | Run: pytest tests/test_agent.py -v |
13 | 13 | """ |
| 14 | +import asyncio |
14 | 15 |
|
15 | 16 | import pytest |
16 | 17 |
|
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 | +) |
18 | 23 |
|
19 | 24 | AGENT_NAME = "ab020-streaming" |
20 | 25 |
|
21 | 26 |
|
22 | 27 | @pytest.mark.asyncio |
23 | 28 | async def test_send_event_and_poll(): |
24 | 29 | """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 | + |
25 | 38 | 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 |
31 | 70 |
|
32 | 71 |
|
33 | 72 | @pytest.mark.asyncio |
34 | 73 | async def test_streaming_events(): |
35 | 74 | """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 | + |
36 | 83 | 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": |
41 | 114 | 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 |
43 | 137 |
|
44 | 138 |
|
45 | 139 | if __name__ == "__main__": |
|
0 commit comments