|
| 1 | +"""Tests for async callbacks support in akickoff.""" |
| 2 | + |
| 3 | +import asyncio |
| 4 | +import pytest |
| 5 | +from typing import Any |
| 6 | +from unittest.mock import AsyncMock, MagicMock, patch |
| 7 | + |
| 8 | +from crewai.agent import Agent |
| 9 | +from crewai.crew import Crew |
| 10 | +from crewai.task import Task |
| 11 | +from crewai.crews.crew_output import CrewOutput |
| 12 | +from crewai.tasks.task_output import TaskOutput |
| 13 | + |
| 14 | + |
| 15 | +@pytest.fixture |
| 16 | +def test_agent() -> Agent: |
| 17 | + """Create a test agent.""" |
| 18 | + return Agent( |
| 19 | + role="Test Agent", |
| 20 | + goal="Test goal", |
| 21 | + backstory="Test backstory", |
| 22 | + llm="gpt-4o-mini", |
| 23 | + verbose=False, |
| 24 | + ) |
| 25 | + |
| 26 | + |
| 27 | +@pytest.fixture |
| 28 | +def test_task(test_agent: Agent) -> Task: |
| 29 | + """Create a test task.""" |
| 30 | + return Task( |
| 31 | + description="Test task description", |
| 32 | + expected_output="Test expected output", |
| 33 | + agent=test_agent, |
| 34 | + ) |
| 35 | + |
| 36 | + |
| 37 | +class TestAsyncCallbacksSupport: |
| 38 | + """Tests for async callback support in akickoff.""" |
| 39 | + |
| 40 | + @pytest.mark.asyncio |
| 41 | + @patch("crewai.task.Task.aexecute_sync", new_callable=AsyncMock) |
| 42 | + async def test_akickoff_calls_async_before_callback( |
| 43 | + self, mock_execute: AsyncMock, test_agent: Agent |
| 44 | + ) -> None: |
| 45 | + """Test that async before_callback is awaited in aprepare_kickoff.""" |
| 46 | + callback_result = {"called": False} |
| 47 | + |
| 48 | + async def async_before_callback(inputs: dict | None) -> dict[str, Any]: |
| 49 | + callback_result["called"] = True |
| 50 | + await asyncio.sleep(0.01) |
| 51 | + return inputs or {} |
| 52 | + |
| 53 | + task = Task( |
| 54 | + description="Test task for {topic}", |
| 55 | + expected_output="Expected output for {topic}", |
| 56 | + agent=test_agent, |
| 57 | + ) |
| 58 | + crew = Crew( |
| 59 | + agents=[test_agent], |
| 60 | + tasks=[task], |
| 61 | + verbose=False, |
| 62 | + before_kickoff_callbacks=[async_before_callback], |
| 63 | + ) |
| 64 | + |
| 65 | + mock_output = TaskOutput( |
| 66 | + description="Test task for AI", |
| 67 | + raw="Task result about AI", |
| 68 | + agent="Test Agent", |
| 69 | + ) |
| 70 | + mock_execute.return_value = mock_output |
| 71 | + |
| 72 | + result = await crew.akickoff(inputs={"topic": "AI"}) |
| 73 | + |
| 74 | + assert callback_result["called"], "Async before callback was not called" |
| 75 | + assert result is not None |
| 76 | + assert isinstance(result, CrewOutput) |
| 77 | + |
| 78 | + @pytest.mark.asyncio |
| 79 | + @patch("crewai.task.Task.aexecute_sync", new_callable=AsyncMock) |
| 80 | + async def test_akickoff_calls_async_after_callback( |
| 81 | + self, mock_execute: AsyncMock, test_agent: Agent |
| 82 | + ) -> None: |
| 83 | + """Test that async after_callback is awaited in akickoff.""" |
| 84 | + callback_result = {"called": False, "received": None} |
| 85 | + |
| 86 | + async def async_after_callback(result: CrewOutput) -> CrewOutput: |
| 87 | + nonlocal callback_result |
| 88 | + callback_result["called"] = True |
| 89 | + callback_result["received"] = result |
| 90 | + await asyncio.sleep(0.01) |
| 91 | + return result |
| 92 | + |
| 93 | + task = Task( |
| 94 | + description="Test task", |
| 95 | + expected_output="Test output", |
| 96 | + agent=test_agent, |
| 97 | + ) |
| 98 | + crew = Crew( |
| 99 | + agents=[test_agent], |
| 100 | + tasks=[task], |
| 101 | + verbose=False, |
| 102 | + after_kickoff_callbacks=[async_after_callback], |
| 103 | + ) |
| 104 | + |
| 105 | + mock_output = TaskOutput( |
| 106 | + description="Test task", |
| 107 | + raw="Task result", |
| 108 | + agent="Test Agent", |
| 109 | + ) |
| 110 | + mock_execute.return_value = mock_output |
| 111 | + |
| 112 | + result = await crew.akickoff() |
| 113 | + |
| 114 | + assert callback_result["called"], "Async after callback was not called" |
| 115 | + assert callback_result["received"] is not None |
| 116 | + assert isinstance(callback_result["received"], CrewOutput) |
| 117 | + |
| 118 | + @pytest.mark.asyncio |
| 119 | + @patch("crewai.task.Task.aexecute_sync", new_callable=AsyncMock) |
| 120 | + async def test_akickoff_mixed_sync_and_async_callbacks( |
| 121 | + self, mock_execute: AsyncMock, test_agent: Agent |
| 122 | + ) -> None: |
| 123 | + """Test that mixed sync and async callbacks work together.""" |
| 124 | + sync_result = {"called": False} |
| 125 | + async_result = {"called": False, "received": None} |
| 126 | + |
| 127 | + def sync_before_callback(inputs: dict | None) -> dict: |
| 128 | + sync_result["called"] = True |
| 129 | + return inputs or {} |
| 130 | + |
| 131 | + async def async_after_callback(result: CrewOutput) -> CrewOutput: |
| 132 | + nonlocal async_result |
| 133 | + async_result["called"] = True |
| 134 | + async_result["received"] = result |
| 135 | + await asyncio.sleep(0.01) |
| 136 | + return result |
| 137 | + |
| 138 | + task = Task( |
| 139 | + description="Test task", |
| 140 | + expected_output="Test output", |
| 141 | + agent=test_agent, |
| 142 | + ) |
| 143 | + crew = Crew( |
| 144 | + agents=[test_agent], |
| 145 | + tasks=[task], |
| 146 | + verbose=False, |
| 147 | + before_kickoff_callbacks=[sync_before_callback], |
| 148 | + after_kickoff_callbacks=[async_after_callback], |
| 149 | + ) |
| 150 | + |
| 151 | + mock_output = TaskOutput( |
| 152 | + description="Test task", |
| 153 | + raw="Task result", |
| 154 | + agent="Test Agent", |
| 155 | + ) |
| 156 | + mock_execute.return_value = mock_output |
| 157 | + |
| 158 | + result = await crew.akickoff() |
| 159 | + |
| 160 | + assert sync_result["called"], "Sync before callback was not called" |
| 161 | + assert async_result["called"], "Async after callback was not called" |
| 162 | + assert async_result["received"] is not None |
| 163 | + |
| 164 | + @pytest.mark.asyncio |
| 165 | + @patch("crewai.task.Task.aexecute_sync", new_callable=AsyncMock) |
| 166 | + async def test_akickoff_empty_callbacks( |
| 167 | + self, mock_execute: AsyncMock, test_agent: Agent |
| 168 | + ) -> None: |
| 169 | + """Test that empty callbacks list still works normally.""" |
| 170 | + task = Task( |
| 171 | + description="Test task", |
| 172 | + expected_output="Test output", |
| 173 | + agent=test_agent, |
| 174 | + ) |
| 175 | + crew = Crew( |
| 176 | + agents=[test_agent], |
| 177 | + tasks=[task], |
| 178 | + verbose=False, |
| 179 | + before_kickoff_callbacks=[], |
| 180 | + after_kickoff_callbacks=[], |
| 181 | + ) |
| 182 | + |
| 183 | + mock_output = TaskOutput( |
| 184 | + description="Test task", |
| 185 | + raw="Task result", |
| 186 | + agent="Test Agent", |
| 187 | + ) |
| 188 | + mock_execute.return_value = mock_output |
| 189 | + |
| 190 | + result = await crew.akickoff() |
| 191 | + |
| 192 | + assert result is not None |
| 193 | + assert isinstance(result, CrewOutput) |
| 194 | + assert result.raw == "Task result" |
0 commit comments