22
33from __future__ import annotations
44
5- from unittest .mock import AsyncMock , MagicMock , patch
5+ from unittest .mock import AsyncMock , MagicMock , PropertyMock , patch
66
77import pytest
88
@@ -40,7 +40,7 @@ def mock_hass():
4040@pytest .fixture
4141def flow (mock_hass ):
4242 """Create a config flow instance."""
43- flow = AIHubConfigFlow (mock_hass )
43+ flow = AIHubConfigFlow ()
4444 flow .hass = mock_hass
4545 return flow
4646
@@ -94,9 +94,10 @@ async def test_empty_api_key_skips_validation(self, mock_hass):
9494class TestAIHubConfigFlow :
9595 """Tests for AIHubConfigFlow."""
9696
97- def test_form_show (self , flow ):
97+ @pytest .mark .asyncio
98+ async def test_form_show (self , flow ):
9899 """Test the initial form is shown."""
99- result = flow .async_step_user (None )
100+ result = await flow .async_step_user (None )
100101
101102 assert result ["type" ] == "form"
102103 assert result ["step_id" ] == "user"
@@ -183,22 +184,29 @@ def subentry_flow(self, mock_hass):
183184 """Create a subentry flow instance."""
184185 config_entry = MagicMock (spec = config_entries .ConfigEntry )
185186 config_entry .subentries = {}
187+ subentry = {"data" : {}}
186188
187- flow = AIHubSubentryFlowHandler (mock_hass , config_entry , None )
189+ flow = AIHubSubentryFlowHandler ()
188190 flow .hass = mock_hass
189- flow .source = "user"
190- flow ._subentry_type = "conversation"
191+ flow .__dict__ ["_entry" ] = config_entry
192+ flow .__dict__ ["handler" ] = (config_entry , "conversation" , None )
193+ flow .__dict__ ["_reconfigure_subentry" ] = subentry
194+ flow .__dict__ ["context" ] = {"source" : "user" }
191195 return flow
192196
193- def test_init_form_show (self , subentry_flow ):
197+ @pytest .mark .asyncio
198+ async def test_init_form_show (self , subentry_flow ):
194199 """Test the initial form is shown."""
195- result = subentry_flow .async_step_init (None )
200+ with patch .object (AIHubSubentryFlowHandler , "_is_new" , new_callable = PropertyMock , return_value = True ):
201+ with patch .object (AIHubSubentryFlowHandler , "_subentry_type" , new_callable = PropertyMock , return_value = "conversation" ):
202+ result = await subentry_flow .async_step_init (None )
196203
197204 assert result ["type" ] == "form"
198205 assert result ["step_id" ] == "init"
199206 assert "data_schema" in result
200207
201- def test_recommended_mode_toggle (self , subentry_flow ):
208+ @pytest .mark .asyncio
209+ async def test_recommended_mode_toggle (self , subentry_flow ):
202210 """Test recommended mode toggling."""
203211 user_input = {
204212 CONF_RECOMMENDED : False ,
@@ -211,35 +219,43 @@ def test_recommended_mode_toggle(self, subentry_flow):
211219 subentry_flow .last_rendered_recommended = True
212220 subentry_flow .options = RECOMMENDED_CONVERSATION_OPTIONS .copy ()
213221
214- result = subentry_flow .async_step_init (user_input )
222+ with patch .object (AIHubSubentryFlowHandler , "_is_new" , new_callable = PropertyMock , return_value = True ):
223+ with patch .object (AIHubSubentryFlowHandler , "_subentry_type" , new_callable = PropertyMock , return_value = "conversation" ):
224+ result = await subentry_flow .async_step_init (user_input )
215225
216226 # Should re-render form with advanced options
217227 assert result ["type" ] == "form"
218228
219- def test_conversation_subentry_with_recommended_mode (self , subentry_flow ):
229+ @pytest .mark .asyncio
230+ async def test_conversation_subentry_with_recommended_mode (self , subentry_flow ):
220231 """Test conversation subentry in recommended mode."""
221- subentry_flow ._subentry_type = "conversation"
222232 subentry_flow .options = RECOMMENDED_CONVERSATION_OPTIONS .copy ()
223233 subentry_flow .last_rendered_recommended = True
224234
225- result = subentry_flow .async_step_init ({CONF_RECOMMENDED : True })
235+ with patch .object (AIHubSubentryFlowHandler , "_is_new" , new_callable = PropertyMock , return_value = True ):
236+ with patch .object (AIHubSubentryFlowHandler , "_subentry_type" , new_callable = PropertyMock , return_value = "conversation" ):
237+ result = await subentry_flow .async_step_init ({CONF_RECOMMENDED : True })
226238
227- assert result ["type" ] == "form "
239+ assert result ["type" ] == "create_entry "
228240
229- def test_tts_subentry (self , subentry_flow ):
241+ @pytest .mark .asyncio
242+ async def test_tts_subentry (self , subentry_flow ):
230243 """Test TTS subentry."""
231- subentry_flow ._subentry_type = "tts"
232244 subentry_flow .options = {"recommended" : True }
233245
234- result = subentry_flow .async_step_init ({CONF_RECOMMENDED : True })
246+ with patch .object (AIHubSubentryFlowHandler , "_is_new" , new_callable = PropertyMock , return_value = True ):
247+ with patch .object (AIHubSubentryFlowHandler , "_subentry_type" , new_callable = PropertyMock , return_value = "tts" ):
248+ result = await subentry_flow .async_step_init ({CONF_RECOMMENDED : True })
235249
236250 assert result ["type" ] == "form"
237251
238- def test_translation_subentry (self , subentry_flow ):
252+ @pytest .mark .asyncio
253+ async def test_translation_subentry (self , subentry_flow ):
239254 """Test translation subentry uses the generic flow."""
240- subentry_flow ._subentry_type = "translation"
241255 subentry_flow .options = {"recommended" : True }
242256
243- result = subentry_flow .async_step_init ({CONF_RECOMMENDED : True })
257+ with patch .object (AIHubSubentryFlowHandler , "_is_new" , new_callable = PropertyMock , return_value = True ):
258+ with patch .object (AIHubSubentryFlowHandler , "_subentry_type" , new_callable = PropertyMock , return_value = "translation" ):
259+ result = await subentry_flow .async_step_init ({CONF_RECOMMENDED : True })
244260
245261 assert result ["type" ] == "form"
0 commit comments