44import json # Fix for missing import
55from unittest .mock import AsyncMock , MagicMock , patch
66from pydantic import ValidationError
7- sys .modules ['azure.monitor.events.extension' ] = MagicMock ()
7+
8+ sys .modules ["azure.monitor.events.extension" ] = MagicMock ()
89
910# Set environment variables to mock Config dependencies before any import
1011os .environ ["COSMOSDB_ENDPOINT" ] = "https://mock-endpoint"
1516os .environ ["AZURE_OPENAI_API_VERSION" ] = "2023-01-01"
1617os .environ ["AZURE_OPENAI_ENDPOINT" ] = "https://mock-openai-endpoint"
1718
18- from autogen_core .components .models import AssistantMessage , AzureOpenAIChatCompletionClient
19+ from autogen_core .components .models import (
20+ AssistantMessage ,
21+ AzureOpenAIChatCompletionClient ,
22+ )
1923from src .backend .models .messages import Step
2024from src .backend .context .cosmos_memory import CosmosBufferedChatCompletionContext
2125from src .backend .agents .agentutils import extract_and_update_transition_states
2226
2327
24-
25-
2628@pytest .mark .asyncio
2729async def test_extract_and_update_transition_states_invalid_response ():
2830 """Test handling of invalid JSON response from model client."""
@@ -41,8 +43,11 @@ async def test_extract_and_update_transition_states_invalid_response():
4143 cosmos_mock = MagicMock ()
4244
4345 model_client .create .return_value = MagicMock (content = "invalid_json" )
44-
45- with patch ("src.backend.context.cosmos_memory.CosmosBufferedChatCompletionContext" , cosmos_mock ):
46+
47+ with patch (
48+ "src.backend.context.cosmos_memory.CosmosBufferedChatCompletionContext" ,
49+ cosmos_mock ,
50+ ):
4651 with pytest .raises (json .JSONDecodeError ):
4752 await extract_and_update_transition_states (
4853 step = step ,
@@ -54,6 +59,7 @@ async def test_extract_and_update_transition_states_invalid_response():
5459
5560 cosmos_mock .update_step .assert_not_called ()
5661
62+
5763@pytest .mark .asyncio
5864async def test_extract_and_update_transition_states_validation_error ():
5965 """Test handling of a response missing required fields."""
@@ -71,12 +77,15 @@ async def test_extract_and_update_transition_states_validation_error():
7177 model_client = AsyncMock ()
7278 cosmos_mock = MagicMock ()
7379
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+ invalid_response = {
81+ "identifiedTargetState" : "state1"
82+ } # Missing 'identifiedTargetTransition'
83+ model_client .create .return_value = MagicMock (content = json .dumps (invalid_response ))
84+
85+ with patch (
86+ "src.backend.context.cosmos_memory.CosmosBufferedChatCompletionContext" ,
87+ cosmos_mock ,
88+ ):
8089 with pytest .raises (ValidationError ):
8190 await extract_and_update_transition_states (
8291 step = step ,
@@ -88,6 +97,7 @@ async def test_extract_and_update_transition_states_validation_error():
8897
8998 cosmos_mock .update_step .assert_not_called ()
9099
100+
91101def test_step_initialization ():
92102 """Test Step initialization with valid data."""
93103 step = Step (
@@ -110,6 +120,7 @@ def test_step_initialization():
110120 assert step .status == "planned"
111121 assert step .human_approval_status == "requested"
112122
123+
113124def test_step_missing_required_fields ():
114125 """Test Step initialization with missing required fields."""
115126 with pytest .raises (ValidationError ):
0 commit comments