11# pylint: disable=import-error, wrong-import-position, missing-module-docstring
2- import json
32import os
43import sys
5- from unittest .mock import AsyncMock , MagicMock , patch
4+ from unittest .mock import MagicMock
65import pytest
76from pydantic import ValidationError
87
9-
108# Environment and module setup
119sys .modules ["azure.monitor.events.extension" ] = MagicMock ()
1210
1816os .environ ["AZURE_OPENAI_API_VERSION" ] = "2023-01-01"
1917os .environ ["AZURE_OPENAI_ENDPOINT" ] = "https://mock-openai-endpoint"
2018
21- # noqa: F401 is to ignore unused import warnings (if any)
2219from src .backend .agents .agentutils import extract_and_update_transition_states # noqa: F401, C0413
2320from src .backend .models .messages import Step # noqa: F401, C0413
2421
2522
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 (
46- "src.backend.context.cosmos_memory.CosmosBufferedChatCompletionContext" ,
47- cosmos_mock ,
48- ):
49- with pytest .raises (json .JSONDecodeError ):
50- await extract_and_update_transition_states (
51- step = step ,
52- session_id = session_id ,
53- user_id = user_id ,
54- planner_dynamic_or_workflow = "workflow" ,
55- model_client = model_client ,
56- )
57-
58- cosmos_mock .update_step .assert_not_called ()
59-
60-
61- @pytest .mark .asyncio
62- async def test_extract_and_update_transition_states_validation_error ():
63- """Test handling of a response missing required fields."""
64- session_id = "test_session"
65- user_id = "test_user"
66- step = Step (
67- data_type = "step" ,
68- plan_id = "test_plan" ,
69- action = "test_action" ,
70- agent = "HumanAgent" ,
71- session_id = session_id ,
72- user_id = user_id ,
73- agent_reply = "test_reply" ,
74- )
75- model_client = AsyncMock ()
76- cosmos_mock = MagicMock ()
77-
78- invalid_response = {
79- "identifiedTargetState" : "state1"
80- } # Missing 'identifiedTargetTransition'
81- model_client .create .return_value = MagicMock (content = json .dumps (invalid_response ))
82-
83- with patch (
84- "src.backend.context.cosmos_memory.CosmosBufferedChatCompletionContext" ,
85- cosmos_mock ,
86- ):
87- with pytest .raises (ValidationError ):
88- await extract_and_update_transition_states (
89- step = step ,
90- session_id = session_id ,
91- user_id = user_id ,
92- planner_dynamic_or_workflow = "workflow" ,
93- model_client = model_client ,
94- )
95-
96- cosmos_mock .update_step .assert_not_called ()
97-
98-
9923def test_step_initialization ():
10024 """Test Step initialization with valid data."""
10125 step = Step (
@@ -128,4 +52,3 @@ def test_step_missing_required_fields():
12852 agent = "test_agent" ,
12953 session_id = "test_session" ,
13054 )
131-
0 commit comments