|
| 1 | +import pytest |
| 2 | +import sys |
| 3 | +import os |
| 4 | +import json # Fix for missing import |
| 5 | +from unittest.mock import AsyncMock, MagicMock, patch |
| 6 | +from pydantic import ValidationError |
| 7 | +sys.modules['azure.monitor.events.extension'] = MagicMock() |
| 8 | + |
| 9 | +# Set environment variables to mock Config dependencies before any import |
| 10 | +os.environ["COSMOSDB_ENDPOINT"] = "https://mock-endpoint" |
| 11 | +os.environ["COSMOSDB_KEY"] = "mock-key" |
| 12 | +os.environ["COSMOSDB_DATABASE"] = "mock-database" |
| 13 | +os.environ["COSMOSDB_CONTAINER"] = "mock-container" |
| 14 | +os.environ["AZURE_OPENAI_DEPLOYMENT_NAME"] = "mock-deployment-name" |
| 15 | +os.environ["AZURE_OPENAI_API_VERSION"] = "2023-01-01" |
| 16 | +os.environ["AZURE_OPENAI_ENDPOINT"] = "https://mock-openai-endpoint" |
| 17 | + |
| 18 | +from autogen_core.components.models import AssistantMessage, AzureOpenAIChatCompletionClient |
| 19 | +from src.backend.models.messages import Step |
| 20 | +from src.backend.context.cosmos_memory import CosmosBufferedChatCompletionContext |
| 21 | +from src.backend.agents.agentutils import extract_and_update_transition_states |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | + |
| 26 | +@pytest.mark.asyncio |
| 27 | +async def test_extract_and_update_transition_states_invalid_response(): |
| 28 | + """Test handling of invalid JSON response from model client.""" |
| 29 | + session_id = "test_session" |
| 30 | + user_id = "test_user" |
| 31 | + step = Step( |
| 32 | + data_type="step", |
| 33 | + plan_id="test_plan", |
| 34 | + action="test_action", |
| 35 | + agent="HumanAgent", |
| 36 | + session_id=session_id, |
| 37 | + user_id=user_id, |
| 38 | + agent_reply="test_reply", |
| 39 | + ) |
| 40 | + model_client = AsyncMock() |
| 41 | + cosmos_mock = MagicMock() |
| 42 | + |
| 43 | + model_client.create.return_value = MagicMock(content="invalid_json") |
| 44 | + |
| 45 | + with patch("src.backend.context.cosmos_memory.CosmosBufferedChatCompletionContext", cosmos_mock): |
| 46 | + with pytest.raises(json.JSONDecodeError): |
| 47 | + await extract_and_update_transition_states( |
| 48 | + step=step, |
| 49 | + session_id=session_id, |
| 50 | + user_id=user_id, |
| 51 | + planner_dynamic_or_workflow="workflow", |
| 52 | + model_client=model_client, |
| 53 | + ) |
| 54 | + |
| 55 | + cosmos_mock.update_step.assert_not_called() |
| 56 | + |
| 57 | +@pytest.mark.asyncio |
| 58 | +async def test_extract_and_update_transition_states_validation_error(): |
| 59 | + """Test handling of a response missing required fields.""" |
| 60 | + session_id = "test_session" |
| 61 | + user_id = "test_user" |
| 62 | + step = Step( |
| 63 | + data_type="step", |
| 64 | + plan_id="test_plan", |
| 65 | + action="test_action", |
| 66 | + agent="HumanAgent", |
| 67 | + session_id=session_id, |
| 68 | + user_id=user_id, |
| 69 | + agent_reply="test_reply", |
| 70 | + ) |
| 71 | + model_client = AsyncMock() |
| 72 | + cosmos_mock = MagicMock() |
| 73 | + |
| 74 | + invalid_response = {"identifiedTargetState": "state1"} # Missing 'identifiedTargetTransition' |
| 75 | + model_client.create.return_value = MagicMock( |
| 76 | + content=json.dumps(invalid_response) |
| 77 | + ) |
| 78 | + |
| 79 | + with patch("src.backend.context.cosmos_memory.CosmosBufferedChatCompletionContext", cosmos_mock): |
| 80 | + with pytest.raises(ValidationError): |
| 81 | + await extract_and_update_transition_states( |
| 82 | + step=step, |
| 83 | + session_id=session_id, |
| 84 | + user_id=user_id, |
| 85 | + planner_dynamic_or_workflow="workflow", |
| 86 | + model_client=model_client, |
| 87 | + ) |
| 88 | + |
| 89 | + cosmos_mock.update_step.assert_not_called() |
| 90 | + |
| 91 | +def test_step_initialization(): |
| 92 | + """Test Step initialization with valid data.""" |
| 93 | + step = Step( |
| 94 | + data_type="step", |
| 95 | + plan_id="test_plan", |
| 96 | + action="test_action", |
| 97 | + agent="HumanAgent", |
| 98 | + session_id="test_session", |
| 99 | + user_id="test_user", |
| 100 | + agent_reply="test_reply", |
| 101 | + ) |
| 102 | + |
| 103 | + assert step.data_type == "step" |
| 104 | + assert step.plan_id == "test_plan" |
| 105 | + assert step.action == "test_action" |
| 106 | + assert step.agent == "HumanAgent" |
| 107 | + assert step.session_id == "test_session" |
| 108 | + assert step.user_id == "test_user" |
| 109 | + assert step.agent_reply == "test_reply" |
| 110 | + assert step.status == "planned" |
| 111 | + assert step.human_approval_status == "requested" |
| 112 | + |
| 113 | +def test_step_missing_required_fields(): |
| 114 | + """Test Step initialization with missing required fields.""" |
| 115 | + with pytest.raises(ValidationError): |
| 116 | + Step( |
| 117 | + data_type="step", |
| 118 | + action="test_action", |
| 119 | + agent="test_agent", |
| 120 | + session_id="test_session", |
| 121 | + ) |
0 commit comments