1+ import os
2+ import sys
3+ from unittest .mock import AsyncMock , patch , MagicMock
4+ import pytest
5+
6+
7+ # Set mock environment variables for Azure and CosmosDB before importing anything else
8+ os .environ ["COSMOSDB_ENDPOINT" ] = "https://mock-endpoint"
9+ os .environ ["COSMOSDB_KEY" ] = "mock-key"
10+ os .environ ["COSMOSDB_DATABASE" ] = "mock-database"
11+ os .environ ["COSMOSDB_CONTAINER" ] = "mock-container"
12+ os .environ ["AZURE_OPENAI_DEPLOYMENT_NAME" ] = "mock-deployment-name"
13+ os .environ ["AZURE_OPENAI_API_VERSION" ] = "2023-01-01"
14+ os .environ ["AZURE_OPENAI_ENDPOINT" ] = "https://mock-openai-endpoint"
15+
16+
17+ # Mock Azure dependencies
18+ sys .modules ["azure.monitor.events.extension" ] = MagicMock ()
19+
20+
21+ # Import after setting environment variables
22+ from src .backend .agents .group_chat_manager import GroupChatManager
23+ from src .backend .models .messages import (
24+ HumanFeedback ,
25+ Step ,
26+ StepStatus ,
27+ BAgentType ,
28+ Plan ,
29+ )
30+ from autogen_core .base import MessageContext , AgentInstantiationContext , AgentRuntime
31+ from autogen_core .components .models import AzureOpenAIChatCompletionClient
32+ from src .backend .context .cosmos_memory import CosmosBufferedChatCompletionContext
33+ from autogen_core .base import AgentId
34+
35+
36+ @pytest .fixture
37+ def setup_group_chat_manager ():
38+ """
39+ Fixture to set up a GroupChatManager and its dependencies.
40+ """
41+ # Mock dependencies
42+ mock_model_client = MagicMock (spec = AzureOpenAIChatCompletionClient )
43+ session_id = "test_session_id"
44+ user_id = "test_user_id"
45+ mock_memory = AsyncMock (spec = CosmosBufferedChatCompletionContext )
46+ mock_agent_ids = {BAgentType .planner_agent : AgentId ("planner_agent" , session_id )}
47+
48+ # Mock AgentInstantiationContext
49+ mock_runtime = MagicMock (spec = AgentRuntime )
50+ mock_agent_id = "test_agent_id"
51+
52+ with patch .object (AgentInstantiationContext , "current_runtime" , return_value = mock_runtime ):
53+ with patch .object (AgentInstantiationContext , "current_agent_id" , return_value = mock_agent_id ):
54+ # Instantiate GroupChatManager
55+ group_chat_manager = GroupChatManager (
56+ model_client = mock_model_client ,
57+ session_id = session_id ,
58+ user_id = user_id ,
59+ memory = mock_memory ,
60+ agent_ids = mock_agent_ids ,
61+ )
62+
63+ return group_chat_manager , mock_memory , session_id , user_id , mock_agent_ids
64+
65+
66+ @pytest .mark .asyncio
67+ @patch ("src.backend.agents.group_chat_manager.track_event_if_configured" )
68+ async def test_update_step_status (mock_track_event , setup_group_chat_manager ):
69+ """
70+ Test the `_update_step_status` method.
71+ """
72+ group_chat_manager , mock_memory , session_id , user_id , mock_agent_ids = setup_group_chat_manager
73+
74+ # Create a mock Step
75+ step = Step (
76+ id = "test_step_id" ,
77+ session_id = session_id ,
78+ plan_id = "test_plan_id" ,
79+ user_id = user_id ,
80+ action = "Test Action" ,
81+ agent = BAgentType .human_agent ,
82+ status = StepStatus .planned ,
83+ )
84+
85+ # Call the method
86+ await group_chat_manager ._update_step_status (step , True , "Feedback message" )
87+
88+ # Assertions
89+ step .status = StepStatus .completed
90+ step .human_feedback = "Feedback message"
91+ mock_memory .update_step .assert_called_once_with (step )
92+ mock_track_event .assert_called_once_with (
93+ "Group Chat Manager - Received human feedback, Updating step and updated into the cosmos" ,
94+ {
95+ "status" : StepStatus .completed ,
96+ "session_id" : step .session_id ,
97+ "user_id" : step .user_id ,
98+ "human_feedback" : "Feedback message" ,
99+ "source" : step .agent ,
100+ },
101+ )
0 commit comments