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