1+ """
2+ Combined Test cases for GroupChatManager class in the backend agents module.
3+ """
4+
15import os
26import sys
3- from unittest .mock import AsyncMock , patch , MagicMock
7+ from unittest .mock import AsyncMock , patch , MagicMock , call
48import pytest
5-
9+
610# Set mock environment variables for Azure and CosmosDB before importing anything else
711os .environ ["COSMOSDB_ENDPOINT" ] = "https://mock-endpoint"
812os .environ ["COSMOSDB_KEY" ] = "mock-key"
1115os .environ ["AZURE_OPENAI_DEPLOYMENT_NAME" ] = "mock-deployment-name"
1216os .environ ["AZURE_OPENAI_API_VERSION" ] = "2023-01-01"
1317os .environ ["AZURE_OPENAI_ENDPOINT" ] = "https://mock-openai-endpoint"
14-
18+
1519# Mock Azure dependencies
1620sys .modules ["azure.monitor.events.extension" ] = MagicMock ()
17-
21+
1822# Import after setting environment variables
1923from src .backend .agents .group_chat_manager import GroupChatManager
2024from src .backend .models .messages import (
21- HumanFeedback ,
2225 Step ,
2326 StepStatus ,
2427 BAgentType ,
2528 Plan ,
29+ ActionRequest ,
2630)
27- from autogen_core .base import MessageContext , AgentInstantiationContext , AgentRuntime
31+ from autogen_core .base import AgentInstantiationContext , AgentRuntime
2832from autogen_core .components .models import AzureOpenAIChatCompletionClient
2933from src .backend .context .cosmos_memory import CosmosBufferedChatCompletionContext
3034from autogen_core .base import AgentId
31-
32-
35+
36+
3337@pytest .fixture
3438def setup_group_chat_manager ():
3539 """
@@ -41,11 +45,11 @@ def setup_group_chat_manager():
4145 user_id = "test_user_id"
4246 mock_memory = AsyncMock (spec = CosmosBufferedChatCompletionContext )
4347 mock_agent_ids = {BAgentType .planner_agent : AgentId ("planner_agent" , session_id )}
44-
48+
4549 # Mock AgentInstantiationContext
4650 mock_runtime = MagicMock (spec = AgentRuntime )
4751 mock_agent_id = "test_agent_id"
48-
52+
4953 with patch .object (AgentInstantiationContext , "current_runtime" , return_value = mock_runtime ):
5054 with patch .object (AgentInstantiationContext , "current_agent_id" , return_value = mock_agent_id ):
5155 # Instantiate GroupChatManager
@@ -56,18 +60,18 @@ def setup_group_chat_manager():
5660 memory = mock_memory ,
5761 agent_ids = mock_agent_ids ,
5862 )
59-
63+
6064 return group_chat_manager , mock_memory , session_id , user_id , mock_agent_ids
61-
62-
65+
66+
6367@pytest .mark .asyncio
6468@patch ("src.backend.agents.group_chat_manager.track_event_if_configured" )
6569async def test_update_step_status (mock_track_event , setup_group_chat_manager ):
6670 """
6771 Test the `_update_step_status` method.
6872 """
6973 group_chat_manager , mock_memory , session_id , user_id , mock_agent_ids = setup_group_chat_manager
70-
74+
7175 # Create a mock Step
7276 step = Step (
7377 id = "test_step_id" ,
@@ -78,10 +82,10 @@ async def test_update_step_status(mock_track_event, setup_group_chat_manager):
7882 agent = BAgentType .human_agent ,
7983 status = StepStatus .planned ,
8084 )
81-
85+
8286 # Call the method
8387 await group_chat_manager ._update_step_status (step , True , "Feedback message" )
84-
88+
8589 # Assertions
8690 step .status = StepStatus .completed
8791 step .human_feedback = "Feedback message"
@@ -95,4 +99,117 @@ async def test_update_step_status(mock_track_event, setup_group_chat_manager):
9599 "human_feedback" : "Feedback message" ,
96100 "source" : step .agent ,
97101 },
98- )
102+ )
103+
104+
105+ @pytest .mark .asyncio
106+ @patch ("src.backend.agents.group_chat_manager.track_event_if_configured" )
107+ async def test_execute_step (mock_track_event , setup_group_chat_manager ):
108+ """
109+ Test the `_execute_step` method.
110+ """
111+ group_chat_manager , mock_memory , session_id , user_id , mock_agent_ids = setup_group_chat_manager
112+
113+ # Create a mock Step
114+ step = Step (
115+ id = "test_step_id" ,
116+ session_id = session_id ,
117+ plan_id = "test_plan_id" ,
118+ user_id = user_id ,
119+ action = "Test Action" ,
120+ agent = BAgentType .planner_agent ,
121+ status = StepStatus .planned ,
122+ )
123+
124+ # Mock memory responses
125+ mock_plan = Plan (
126+ id = "test_plan_id" ,
127+ session_id = session_id ,
128+ user_id = user_id ,
129+ summary = "Test Plan Summary" ,
130+ initial_goal = "Test Initial Goal" ,
131+ )
132+ mock_memory .get_plan_by_session .return_value = mock_plan
133+ mock_memory .get_steps_by_plan .return_value = [step ]
134+
135+ # Mock helper methods
136+ group_chat_manager .send_message = AsyncMock ()
137+
138+ # Call the method
139+ await group_chat_manager ._execute_step (session_id , step )
140+
141+ # Assertions
142+ step .status = StepStatus .action_requested
143+ mock_memory .update_step .assert_called_once_with (step )
144+
145+ mock_track_event .assert_has_calls ([
146+ call (
147+ "Group Chat Manager - Update step to action_requested and updated into the cosmos" ,
148+ {
149+ "status" : step .status ,
150+ "session_id" : step .session_id ,
151+ "user_id" : user_id ,
152+ "source" : step .agent ,
153+ },
154+ ),
155+ call (
156+ "Group Chat Manager - Requesting Planneragent to perform the action and added into the cosmos" ,
157+ {
158+ "session_id" : session_id ,
159+ "user_id" : user_id ,
160+ "plan_id" : "test_plan_id" ,
161+ "content" : f"Requesting Planneragent to perform action: { step .action } " ,
162+ "source" : "GroupChatManager" ,
163+ "step_id" : step .id ,
164+ },
165+ ),
166+ ])
167+
168+ # Adjusted expected ActionRequest
169+ expected_action_request = ActionRequest (
170+ step_id = "test_step_id" ,
171+ plan_id = "test_plan_id" ,
172+ session_id = "test_session_id" ,
173+ action = (
174+ "<conversation_history>Here is the conversation history so far for the current plan. "
175+ "This information may or may not be relevant to the step you have been asked to execute."
176+ "The user's task was:\n Test Plan Summary\n \n "
177+ "The conversation between the previous agents so far is below:\n <conversation_history \\ >. "
178+ "Here is the step to action: Test Action. ONLY perform the steps and actions required to complete this specific step, "
179+ "the other steps have already been completed. Only use the conversational history for additional information, "
180+ "if it's required to complete the step you have been assigned."
181+ ),
182+ agent = BAgentType .planner_agent ,
183+ )
184+
185+ group_chat_manager .send_message .assert_called_once_with (
186+ expected_action_request , mock_agent_ids [BAgentType .planner_agent ]
187+ )
188+
189+
190+ @pytest .mark .asyncio
191+ async def test_update_step_invalid_feedback_status (setup_group_chat_manager ):
192+ """
193+ Test `_update_step_status` with invalid feedback status.
194+ Covers lines 210-211.
195+ """
196+ group_chat_manager , mock_memory , session_id , user_id , mock_agent_ids = setup_group_chat_manager
197+
198+ # Create a mock Step
199+ step = Step (
200+ id = "test_step_id" ,
201+ session_id = session_id ,
202+ plan_id = "test_plan_id" ,
203+ user_id = user_id ,
204+ action = "Test Action" ,
205+ agent = BAgentType .human_agent ,
206+ status = StepStatus .planned ,
207+ )
208+
209+ # Call the method with invalid feedback status
210+ await group_chat_manager ._update_step_status (step , None , "Feedback message" )
211+
212+ # Assertions
213+ step .status = StepStatus .planned # Status should remain unchanged
214+ step .human_feedback = "Feedback message"
215+ mock_memory .update_step .assert_called_once_with (step )
0 commit comments