|
1 | 1 | import pytest |
| 2 | +import asyncio |
2 | 3 | import importlib |
3 | 4 | from types import ModuleType |
4 | 5 | from unittest.mock import MagicMock, patch |
5 | | -from threading import Event |
| 6 | +from threading import Event, Thread |
6 | 7 |
|
7 | 8 | # --------------------------------------------------------------------------- |
8 | 9 | # 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_ |
292 | 293 |
|
293 | 294 | # Ensure the raised error contains our message to confirm correct propagation |
294 | 295 | 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