diff --git a/internal_filesystem/lib/mpos/net/wifi_service.py b/internal_filesystem/lib/mpos/net/wifi_service.py index 0303c00b..07cb9cec 100644 --- a/internal_filesystem/lib/mpos/net/wifi_service.py +++ b/internal_filesystem/lib/mpos/net/wifi_service.py @@ -47,7 +47,7 @@ class WifiService: _desktop_connected_ssid = None @staticmethod - def connect(network_module=None): + def connect(network_module=None, time_module=None): """ Scan for available networks and connect to the first saved network found. Networks are tried in order of signal strength (strongest first). @@ -55,6 +55,7 @@ class WifiService: Args: network_module: Network module for dependency injection (testing) + time_module: Time module for dependency injection (testing) Returns: bool: True if successfully connected, False otherwise @@ -79,7 +80,12 @@ class WifiService: password = WifiService.access_points.get(ssid).get("password") print(f"WifiService: Attempting to connect to saved network '{ssid}'") - if WifiService.attempt_connecting(ssid, password, network_module=network_module): + if WifiService.attempt_connecting( + ssid, + password, + network_module=network_module, + time_module=time_module, + ): print(f"WifiService: Connected to '{ssid}'") return True else: @@ -93,7 +99,12 @@ class WifiService: password = config.get("password") print(f"WifiService: Attempting hidden network '{ssid}'") - if WifiService.attempt_connecting(ssid, password, network_module=network_module): + if WifiService.attempt_connecting( + ssid, + password, + network_module=network_module, + time_module=time_module, + ): print(f"WifiService: Connected to hidden network '{ssid}'") return True else: @@ -201,7 +212,10 @@ class WifiService: print("WifiService: Simulated connection complete") else: # Attempt to connect to saved networks - if WifiService.connect(network_module=network_module): + if WifiService.connect( + network_module=network_module, + time_module=time_module, + ): print("WifiService: Auto-connect successful") else: print("WifiService: Auto-connect failed") diff --git a/internal_filesystem/lib/mpos/testing/mocks.py b/internal_filesystem/lib/mpos/testing/mocks.py index cd3a5a42..94863e62 100644 --- a/internal_filesystem/lib/mpos/testing/mocks.py +++ b/internal_filesystem/lib/mpos/testing/mocks.py @@ -412,6 +412,19 @@ class MockNetwork: if self._connected: return ('192.168.1.100', '255.255.255.0', '192.168.1.1', '8.8.8.8') return ('0.0.0.0', '0.0.0.0', '0.0.0.0', '0.0.0.0') + + def ipconfig(self, key=None): + """Return IP configuration details, mirroring network.WLAN.ipconfig.""" + config = self.ifconfig() + mapping = { + 'addr4': config[0], + 'netmask4': config[1], + 'gateway4': config[2], + 'dns4': config[3], + } + if key is None: + return mapping + return mapping.get(key) def scan(self): """Scan for available networks.""" diff --git a/tests/test_wifi_service.py b/tests/test_wifi_service.py index 705b85d4..be4cf493 100644 --- a/tests/test_wifi_service.py +++ b/tests/test_wifi_service.py @@ -102,7 +102,7 @@ class TestWifiServiceConnect(unittest.TestCase): mock_wlan.connect = mock_connect - result = WifiService.connect(network_module=mock_network) + result = WifiService.connect(network_module=mock_network, time_module=MockTime()) self.assertTrue(result) @@ -114,7 +114,7 @@ class TestWifiServiceConnect(unittest.TestCase): mock_wlan = mock_network.WLAN(mock_network.STA_IF) mock_wlan._scan_results = [(b"UnsavedNetwork", -50, 1, 3, b"", 0)] - result = WifiService.connect(network_module=mock_network) + result = WifiService.connect(network_module=mock_network, time_module=MockTime()) self.assertFalse(result) @@ -128,7 +128,7 @@ class TestWifiServiceConnect(unittest.TestCase): mock_wlan = mock_network.WLAN(mock_network.STA_IF) mock_wlan._scan_results = [(b"DifferentNetwork", -50, 1, 3, b"", 0)] - result = WifiService.connect(network_module=mock_network) + result = WifiService.connect(network_module=mock_network, time_module=MockTime()) self.assertFalse(result) @@ -166,6 +166,8 @@ class TestWifiServiceAttemptConnecting(unittest.TestCase): ) self.assertTrue(result) + # Should not sleep once connected immediately + self.assertEqual(len(mock_time.get_sleep_calls()), 0) def test_connection_timeout(self): """Test connection timeout after 10 attempts.""" @@ -227,6 +229,8 @@ class TestWifiServiceAttemptConnecting(unittest.TestCase): self.assertFalse(result) # Should have checked less than 10 times (aborted early) self.assertTrue(check_count[0] < 10) + # Should have slept only until abort + self.assertEqual(len(mock_time.get_sleep_calls()), 2) def test_connection_error_handling(self): """Test handling of connection errors.""" @@ -501,7 +505,7 @@ class TestWifiServiceRSSISorting(unittest.TestCase): mock_wlan.connect = mock_connect - result = WifiService.connect(network_module=mock_network) + result = WifiService.connect(network_module=mock_network, time_module=MockTime()) self.assertTrue(result) # Should try strongest first (-45 dBm) @@ -538,7 +542,7 @@ class TestWifiServiceRSSISorting(unittest.TestCase): mock_wlan.connect = mock_connect - result = WifiService.connect(network_module=mock_network) + result = WifiService.connect(network_module=mock_network, time_module=MockTime()) self.assertTrue(result) # Verify order: strongest to weakest @@ -572,7 +576,7 @@ class TestWifiServiceRSSISorting(unittest.TestCase): mock_wlan.connect = mock_connect - result = WifiService.connect(network_module=mock_network) + result = WifiService.connect(network_module=mock_network, time_module=MockTime()) self.assertTrue(result) # Should only try once (first is strongest and succeeds) @@ -618,7 +622,7 @@ class TestWifiServiceRSSISorting(unittest.TestCase): mock_wlan.connect = mock_connect - result = WifiService.connect(network_module=mock_network) + result = WifiService.connect(network_module=mock_network, time_module=MockTime()) self.assertTrue(result) # Expected order: Channel 8 (-47), Baptistus (-48), telenet (-70), Galaxy (-83) @@ -654,7 +658,7 @@ class TestWifiServiceRSSISorting(unittest.TestCase): mock_wlan.connect = mock_connect - result = WifiService.connect(network_module=mock_network) + result = WifiService.connect(network_module=mock_network, time_module=MockTime()) self.assertFalse(result) # No connection succeeded # Verify all 3 were attempted in RSSI order @@ -684,7 +688,7 @@ class TestWifiServiceRSSISorting(unittest.TestCase): mock_wlan.connect = mock_connect - result = WifiService.connect(network_module=mock_network) + result = WifiService.connect(network_module=mock_network, time_module=MockTime()) self.assertFalse(result) # No attempts should be made @@ -706,7 +710,7 @@ class TestWifiServiceRSSISorting(unittest.TestCase): # The connect method now logs "Found network 'TestNet' (RSSI: -55 dBm)" # This test just verifies it doesn't crash - result = WifiService.connect(network_module=mock_network) + result = WifiService.connect(network_module=mock_network, time_module=MockTime()) # Since mock doesn't actually connect, this will likely be False # but the important part is the code runs without error