Skip to content

Commit 8461a50

Browse files
Testcases
1 parent 4918ff0 commit 8461a50

File tree

3 files changed

+105
-26
lines changed

3 files changed

+105
-26
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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()

src/backend/tests/agents/test_planner.py

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import os
22
import sys
3-
import json
43
from unittest.mock import AsyncMock, MagicMock, patch
54
import pytest
65
from src.backend.agents.planner import PlannerAgent
@@ -198,28 +197,3 @@ async def test_create_structured_plan_with_error(planner_agent, mock_context):
198197
assert len(steps) == 0
199198
mock_context.add_plan.assert_not_called()
200199
mock_context.add_step.assert_not_called()
201-
202-
203-
@pytest.mark.asyncio
204-
async def test_create_structured_plan_with_multiple_steps(planner_agent, mock_context):
205-
"""Test _create_structured_plan with multiple steps."""
206-
planner_agent._model_client.create = AsyncMock(
207-
return_value=MagicMock(content=json.dumps({
208-
"initial_goal": "Task with multiple steps",
209-
"steps": [
210-
{"action": "Step 1", "agent": "HumanAgent"},
211-
{"action": "Step 2", "agent": "TechSupportAgent"},
212-
],
213-
"summary_plan_and_steps": "Generated summary with multiple steps",
214-
"human_clarification_request": None,
215-
}))
216-
)
217-
218-
messages = [{"content": "Test message", "source": "PlannerAgent"}]
219-
plan, steps = await planner_agent._create_structured_plan(messages)
220-
221-
# Assertions
222-
assert len(steps) == 2
223-
assert steps[0].action == "Step 1"
224-
assert steps[1].action == "Step 2"
225-
mock_context.add_step.assert_called()

src/backend/tests/test_app.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
os.environ["COSMOSDB_DATABASE"] = "mock-database"
1616
os.environ["COSMOSDB_CONTAINER"] = "mock-container"
1717
os.environ["APPLICATIONINSIGHTS_INSTRUMENTATION_KEY"] = "mock-instrumentation-key"
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"
1822

1923
# Mock telemetry initialization in the app
2024
with patch("src.backend.app.configure_azure_monitor", MagicMock()):

0 commit comments

Comments
 (0)