|
| 1 | +""" |
| 2 | +Test cases for HumanAgent class in the backend agents module. |
| 3 | +""" |
| 4 | + |
| 5 | +# Standard library imports |
| 6 | +import os |
| 7 | +import sys |
| 8 | +from unittest.mock import AsyncMock, MagicMock, patch |
| 9 | +import pytest |
| 10 | + |
| 11 | +sys.modules["azure.monitor.events.extension"] = MagicMock() |
| 12 | + |
| 13 | +# Environment and Mock setup (must be before imports) |
| 14 | +os.environ["COSMOSDB_ENDPOINT"] = "https://mock-endpoint" |
| 15 | +os.environ["COSMOSDB_KEY"] = "mock-key" |
| 16 | +os.environ["COSMOSDB_DATABASE"] = "mock-database" |
| 17 | +os.environ["COSMOSDB_CONTAINER"] = "mock-container" |
| 18 | +os.environ["APPLICATIONINSIGHTS_INSTRUMENTATION_KEY"] = "mock-instrumentation-key" |
| 19 | +os.environ["AZURE_OPENAI_DEPLOYMENT_NAME"] = "mock-deployment-name" |
| 20 | +os.environ["AZURE_OPENAI_API_VERSION"] = "2023-01-01" |
| 21 | +os.environ["AZURE_OPENAI_ENDPOINT"] = "https://mock-openai-endpoint" |
| 22 | + |
| 23 | + |
| 24 | +from autogen_core.base import AgentInstantiationContext, AgentRuntime |
| 25 | +from src.backend.agents.human import HumanAgent |
| 26 | +from src.backend.models.messages import ( |
| 27 | + HumanFeedback, |
| 28 | + Step, |
| 29 | + StepStatus, |
| 30 | + BAgentType, |
| 31 | +) |
| 32 | + |
| 33 | + |
| 34 | +@pytest.fixture |
| 35 | +def setup_agent(): |
| 36 | + """ |
| 37 | + Fixture to set up a HumanAgent and its dependencies. |
| 38 | + """ |
| 39 | + memory = AsyncMock() |
| 40 | + user_id = "test_user" |
| 41 | + group_chat_manager_id = "group_chat_manager" |
| 42 | + |
| 43 | + # Mock runtime and agent ID |
| 44 | + mock_runtime = MagicMock(spec=AgentRuntime) |
| 45 | + mock_agent_id = "test_agent_id" |
| 46 | + |
| 47 | + # Set up the context |
| 48 | + with patch.object(AgentInstantiationContext, "current_runtime", return_value=mock_runtime): |
| 49 | + with patch.object(AgentInstantiationContext, "current_agent_id", return_value=mock_agent_id): |
| 50 | + agent = HumanAgent(memory, user_id, group_chat_manager_id) |
| 51 | + |
| 52 | + session_id = "session123" |
| 53 | + step_id = "step123" |
| 54 | + plan_id = "plan123" |
| 55 | + |
| 56 | + # Mock HumanFeedback message |
| 57 | + feedback_message = HumanFeedback( |
| 58 | + session_id=session_id, |
| 59 | + step_id=step_id, |
| 60 | + plan_id=plan_id, |
| 61 | + approved=True, |
| 62 | + human_feedback="Great job!", |
| 63 | + ) |
| 64 | + |
| 65 | + # Mock Step with all required fields |
| 66 | + step = Step( |
| 67 | + plan_id=plan_id, |
| 68 | + action="Test Action", |
| 69 | + agent=BAgentType.human_agent, |
| 70 | + status=StepStatus.planned, |
| 71 | + session_id=session_id, |
| 72 | + user_id=user_id, |
| 73 | + human_feedback=None, |
| 74 | + ) |
| 75 | + |
| 76 | + return agent, memory, feedback_message, step, session_id, step_id, plan_id |
| 77 | + |
| 78 | + |
| 79 | +@patch("src.backend.agents.human.logging.info") |
| 80 | +@patch("src.backend.agents.human.track_event") |
| 81 | +@pytest.mark.asyncio |
| 82 | +async def test_handle_step_feedback_step_not_found(mock_track_event, mock_logging, setup_agent): |
| 83 | + """ |
| 84 | + Test scenario where the step is not found in memory. |
| 85 | + """ |
| 86 | + agent, memory, feedback_message, _, _, step_id, _ = setup_agent |
| 87 | + |
| 88 | + # Mock no step found |
| 89 | + memory.get_step.return_value = None |
| 90 | + |
| 91 | + # Run the method |
| 92 | + await agent.handle_step_feedback(feedback_message, MagicMock()) |
| 93 | + |
| 94 | + # Check if log and return were called correctly |
| 95 | + mock_logging.assert_called_with(f"No step found with id: {step_id}") |
| 96 | + memory.update_step.assert_not_called() |
| 97 | + mock_track_event.assert_not_called() |
| 98 | + |
| 99 | + |
| 100 | +if __name__ == "__main__": |
| 101 | + pytest.main() |
0 commit comments