55import socket # Add this import
66from unittest .mock import AsyncMock , MagicMock , patch
77
8- from airos .discovery import DISCOVERY_PORT , AirOSDiscoveryProtocol , async_discover_devices
8+ from airos .discovery import (
9+ DISCOVERY_PORT ,
10+ AirOSDiscoveryProtocol ,
11+ async_discover_devices ,
12+ )
913from airos .exceptions import AirOSDiscoveryError , AirOSEndpointError , AirOSListenerError
1014import pytest
1115
@@ -206,10 +210,14 @@ async def test_error_received():
206210 f"UDP error received in AirOSDiscoveryProtocol: { test_exception } "
207211 )
208212
213+
209214# Front-end discovery tests
210215
216+
211217@pytest .mark .asyncio
212- async def test_async_discover_devices_success (mock_airos_packet , mock_datagram_endpoint ):
218+ async def test_async_discover_devices_success (
219+ mock_airos_packet , mock_datagram_endpoint
220+ ):
213221 """Test the high-level discovery function on a successful run."""
214222 mock_transport , mock_protocol_instance = mock_datagram_endpoint
215223
@@ -224,13 +232,16 @@ def inner_callback(device_info):
224232 return MagicMock (callback = inner_callback )
225233
226234 with patch (
227- "airos.discovery.AirOSDiscoveryProtocol" , new = MagicMock (side_effect = mock_protocol_factory )
235+ "airos.discovery.AirOSDiscoveryProtocol" ,
236+ new = MagicMock (side_effect = mock_protocol_factory ),
228237 ):
229238
230239 async def _simulate_discovery ():
231240 await asyncio .sleep (0.1 )
232241
233- protocol = AirOSDiscoveryProtocol (MagicMock ()) # Create a real protocol instance just for parsing
242+ protocol = AirOSDiscoveryProtocol (
243+ MagicMock ()
244+ ) # Create a real protocol instance just for parsing
234245 parsed_data = protocol .parse_airos_packet (mock_airos_packet , "192.168.1.3" )
235246
236247 mock_protocol_factory (MagicMock ()).callback (parsed_data )
@@ -251,10 +262,10 @@ async def _simulate_discovery():
251262async def test_async_discover_devices_no_devices (mock_datagram_endpoint ):
252263 """Test discovery returns an empty dict if no devices are found."""
253264 mock_transport , _ = mock_datagram_endpoint
254-
265+
255266 with patch ("asyncio.sleep" , new = AsyncMock ()):
256267 result = await async_discover_devices (timeout = 1 )
257-
268+
258269 assert result == {}
259270 mock_transport .close .assert_called_once ()
260271
@@ -263,13 +274,16 @@ async def test_async_discover_devices_no_devices(mock_datagram_endpoint):
263274async def test_async_discover_devices_oserror (mock_datagram_endpoint ):
264275 """Test discovery handles OSError during endpoint creation."""
265276 mock_transport , _ = mock_datagram_endpoint
266-
267- with patch (
268- "asyncio.get_running_loop"
269- ) as mock_get_loop , pytest .raises (AirOSEndpointError ) as excinfo :
277+
278+ with (
279+ patch ("asyncio.get_running_loop" ) as mock_get_loop ,
280+ pytest .raises (AirOSEndpointError ) as excinfo ,
281+ ):
270282 mock_loop = mock_get_loop .return_value
271- mock_loop .create_datagram_endpoint = AsyncMock (side_effect = OSError (98 , "Address in use" ))
272-
283+ mock_loop .create_datagram_endpoint = AsyncMock (
284+ side_effect = OSError (98 , "Address in use" )
285+ )
286+
273287 await async_discover_devices (timeout = 1 )
274288
275289 assert "address_in_use" in str (excinfo .value )
@@ -280,12 +294,13 @@ async def test_async_discover_devices_oserror(mock_datagram_endpoint):
280294async def test_async_discover_devices_cancelled (mock_datagram_endpoint ):
281295 """Test discovery handles CancelledError during the timeout."""
282296 mock_transport , _ = mock_datagram_endpoint
283-
297+
284298 # Patch asyncio.sleep to immediately raise CancelledError
285- with patch (
286- "asyncio.sleep" , new = AsyncMock (side_effect = asyncio .CancelledError )
287- ), pytest .raises (AirOSListenerError ) as excinfo :
299+ with (
300+ patch ("asyncio.sleep" , new = AsyncMock (side_effect = asyncio .CancelledError )),
301+ pytest .raises (AirOSListenerError ) as excinfo ,
302+ ):
288303 await async_discover_devices (timeout = 1 )
289-
304+
290305 assert "cannot_connect" in str (excinfo .value )
291306 mock_transport .close .assert_called_once ()
0 commit comments