Skip to content

Commit 21c9488

Browse files
committed
🧪 modify test_run_agent.py
1 parent af86f4b commit 21c9488

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

‎test/sdk/core/agents/test_run_agent.py‎

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import pytest
2+
import asyncio
23
import importlib
34
from types import ModuleType
45
from unittest.mock import MagicMock, patch
5-
from threading import Event
6+
from threading import Event, Thread
67

78
# ---------------------------------------------------------------------------
89
# Prepare mocks for external dependencies that are not required for this test
@@ -292,3 +293,37 @@ def test_agent_run_thread_handles_internal_exception(basic_agent_run_info, mock_
292293

293294
# Ensure the raised error contains our message to confirm correct propagation
294295
assert "Error in agent_run_thread: Boom" in str(exc_info.value)
296+
297+
298+
@pytest.mark.asyncio
299+
async def test_agent_run_thread_creation_and_start(basic_agent_run_info, monkeypatch):
300+
"""Test that agent_run creates and starts a thread correctly (lines 54-56)."""
301+
# Mock the agent_run_thread function to avoid actual execution
302+
mock_agent_run_thread = MagicMock()
303+
monkeypatch.setattr(run_agent, "agent_run_thread", mock_agent_run_thread)
304+
305+
# Mock Thread class to track instantiation and start calls
306+
mock_thread_instance = MagicMock()
307+
mock_thread_instance.is_alive.return_value = False # Thread finishes immediately
308+
mock_thread_class = MagicMock(return_value=mock_thread_instance)
309+
monkeypatch.setattr(run_agent, "Thread", mock_thread_class)
310+
311+
# Mock observer.get_cached_message to return empty list
312+
basic_agent_run_info.observer.get_cached_message.return_value = []
313+
314+
# Execute the async function
315+
messages = []
316+
async for message in run_agent.agent_run(basic_agent_run_info):
317+
messages.append(message)
318+
319+
# Verify Thread was created with correct target and args (line 55)
320+
mock_thread_class.assert_called_once_with(
321+
target=mock_agent_run_thread,
322+
args=(basic_agent_run_info,)
323+
)
324+
325+
# Verify thread was started (line 56)
326+
mock_thread_instance.start.assert_called_once()
327+
328+
# Verify observer was accessed (line 54)
329+
assert basic_agent_run_info.observer is not None

0 commit comments

Comments
 (0)