11# pylint: disable=import-error, wrong-import-position, missing-module-docstring
2+ import asyncio
23import json
34import os
45import sys
56from unittest .mock import AsyncMock , MagicMock , patch
67import pytest
78from pydantic import ValidationError
89
9-
1010# Environment and module setup
1111sys .modules ["azure.monitor.events.extension" ] = MagicMock ()
1212
2222from src .backend .agents .agentutils import extract_and_update_transition_states # noqa: F401, C0413
2323from src .backend .models .messages import Step # noqa: F401, C0413
2424
25+ @pytest .fixture (scope = "function" )
26+ async def reset_event_loop ():
27+ """Ensure a fresh event loop for each test."""
28+ yield
29+ loop = asyncio .get_event_loop ()
30+ if not loop .is_closed ():
31+ loop .close ()
2532
2633@pytest .mark .asyncio
27- async def test_extract_and_update_transition_states_invalid_response ():
34+ async def test_extract_and_update_transition_states_invalid_response (reset_event_loop ):
2835 """Test handling of invalid JSON response from model client."""
2936 session_id = "test_session"
3037 user_id = "test_user"
@@ -57,9 +64,8 @@ async def test_extract_and_update_transition_states_invalid_response():
5764
5865 cosmos_mock .update_step .assert_not_called ()
5966
60-
6167@pytest .mark .asyncio
62- async def test_extract_and_update_transition_states_validation_error ():
68+ async def test_extract_and_update_transition_states_validation_error (reset_event_loop ):
6369 """Test handling of a response missing required fields."""
6470 session_id = "test_session"
6571 user_id = "test_user"
@@ -95,7 +101,6 @@ async def test_extract_and_update_transition_states_validation_error():
95101
96102 cosmos_mock .update_step .assert_not_called ()
97103
98-
99104def test_step_initialization ():
100105 """Test Step initialization with valid data."""
101106 step = Step (
@@ -118,7 +123,6 @@ def test_step_initialization():
118123 assert step .status == "planned"
119124 assert step .human_approval_status == "requested"
120125
121-
122126def test_step_missing_required_fields ():
123127 """Test Step initialization with missing required fields."""
124128 with pytest .raises (ValidationError ):
@@ -128,4 +132,3 @@ def test_step_missing_required_fields():
128132 agent = "test_agent" ,
129133 session_id = "test_session" ,
130134 )
131-
0 commit comments