|
11 | 11 | from unittest.mock import mock_open, MagicMock, patch |
12 | 12 |
|
13 | 13 | import pytest |
| 14 | +import meshtastic.__main__ as mt_main |
14 | 15 |
|
15 | 16 | from meshtastic.__main__ import ( |
16 | 17 | export_config, |
|
27 | 28 | from ..protobuf.channel_pb2 import Channel # pylint: disable=E0611 |
28 | 29 |
|
29 | 30 | # from ..ble_interface import BLEInterface |
| 31 | +from ..mesh_interface import MeshInterface |
30 | 32 | from ..node import Node |
31 | 33 |
|
32 | 34 | # from ..radioconfig_pb2 import UserPreferences |
@@ -261,6 +263,113 @@ def test_main_info_with_permission_error(patched_getlogin, capsys, caplog): |
261 | 263 | assert err == "" |
262 | 264 |
|
263 | 265 |
|
| 266 | +@pytest.mark.unit |
| 267 | +@pytest.mark.usefixtures("reset_mt_config") |
| 268 | +def test_main_ble_device_not_found_message(capsys): |
| 269 | + """Test BLE device-not-found help text.""" |
| 270 | + sys.argv = ["", "--info", "--ble", "any"] |
| 271 | + mt_config.args = sys.argv |
| 272 | + |
| 273 | + with patch("meshtastic.__main__.BLEInterface.__init__") as mock_ble_init: |
| 274 | + mock_ble_init.side_effect = mt_main.BLEInterface.BLEError( |
| 275 | + "missing", |
| 276 | + mt_main.BLEInterface.BLEError.DEVICE_NOT_FOUND, |
| 277 | + ) |
| 278 | + with pytest.raises(SystemExit) as excinfo: |
| 279 | + main() |
| 280 | + |
| 281 | + out, err = capsys.readouterr() |
| 282 | + assert excinfo.value.code == 1 |
| 283 | + assert re.search(r"BLE device not found", out, re.MULTILINE) |
| 284 | + assert re.search(r"--ble-scan", out, re.MULTILINE) |
| 285 | + assert err == "" |
| 286 | + |
| 287 | + |
| 288 | +@pytest.mark.unit |
| 289 | +@pytest.mark.usefixtures("reset_mt_config") |
| 290 | +def test_main_ble_multiple_devices_message(capsys): |
| 291 | + """Test BLE multiple-devices help text.""" |
| 292 | + sys.argv = ["", "--info", "--ble", "any"] |
| 293 | + mt_config.args = sys.argv |
| 294 | + |
| 295 | + with patch("meshtastic.__main__.BLEInterface.__init__") as mock_ble_init: |
| 296 | + mock_ble_init.side_effect = mt_main.BLEInterface.BLEError( |
| 297 | + "multiple", |
| 298 | + mt_main.BLEInterface.BLEError.MULTIPLE_DEVICES, |
| 299 | + ) |
| 300 | + with pytest.raises(SystemExit) as excinfo: |
| 301 | + main() |
| 302 | + |
| 303 | + out, err = capsys.readouterr() |
| 304 | + assert excinfo.value.code == 1 |
| 305 | + assert re.search(r"Multiple Meshtastic BLE devices found", out, re.MULTILINE) |
| 306 | + assert re.search(r"meshtastic --ble <name_or_address>", out, re.MULTILINE) |
| 307 | + assert err == "" |
| 308 | + |
| 309 | + |
| 310 | +@pytest.mark.unit |
| 311 | +@pytest.mark.usefixtures("reset_mt_config") |
| 312 | +def test_main_ble_write_error_message(capsys): |
| 313 | + """Test BLE write-error help text.""" |
| 314 | + sys.argv = ["", "--info", "--ble", "any"] |
| 315 | + mt_config.args = sys.argv |
| 316 | + |
| 317 | + with patch("meshtastic.__main__.BLEInterface.__init__") as mock_ble_init: |
| 318 | + mock_ble_init.side_effect = mt_main.BLEInterface.BLEError( |
| 319 | + "write fail", |
| 320 | + mt_main.BLEInterface.BLEError.WRITE_ERROR, |
| 321 | + ) |
| 322 | + with pytest.raises(SystemExit) as excinfo: |
| 323 | + main() |
| 324 | + |
| 325 | + out, err = capsys.readouterr() |
| 326 | + assert excinfo.value.code == 1 |
| 327 | + assert re.search(r"Failed to write to BLE device", out, re.MULTILINE) |
| 328 | + assert re.search(r"user not in 'bluetooth' group", out, re.MULTILINE) |
| 329 | + assert err == "" |
| 330 | + |
| 331 | + |
| 332 | +@pytest.mark.unit |
| 333 | +@pytest.mark.usefixtures("reset_mt_config") |
| 334 | +def test_main_ble_read_error_message(capsys): |
| 335 | + """Test BLE read-error help text.""" |
| 336 | + sys.argv = ["", "--info", "--ble", "any"] |
| 337 | + mt_config.args = sys.argv |
| 338 | + |
| 339 | + with patch("meshtastic.__main__.BLEInterface.__init__") as mock_ble_init: |
| 340 | + mock_ble_init.side_effect = mt_main.BLEInterface.BLEError( |
| 341 | + "read fail", |
| 342 | + mt_main.BLEInterface.BLEError.READ_ERROR, |
| 343 | + ) |
| 344 | + with pytest.raises(SystemExit) as excinfo: |
| 345 | + main() |
| 346 | + |
| 347 | + out, err = capsys.readouterr() |
| 348 | + assert excinfo.value.code == 1 |
| 349 | + assert re.search(r"Failed to read from BLE device", out, re.MULTILINE) |
| 350 | + assert re.search(r"Move closer to the device", out, re.MULTILINE) |
| 351 | + assert err == "" |
| 352 | + |
| 353 | + |
| 354 | +@pytest.mark.unit |
| 355 | +@pytest.mark.usefixtures("reset_mt_config") |
| 356 | +def test_main_serial_timeout_message(capsys): |
| 357 | + """Test serial timeout help text.""" |
| 358 | + sys.argv = ["", "--info"] |
| 359 | + mt_config.args = sys.argv |
| 360 | + |
| 361 | + with patch("meshtastic.serial_interface.SerialInterface") as mock_serial: |
| 362 | + mock_serial.side_effect = MeshInterface.MeshInterfaceError("Timed out waiting") |
| 363 | + with pytest.raises(SystemExit) as excinfo: |
| 364 | + main() |
| 365 | + |
| 366 | + out, err = capsys.readouterr() |
| 367 | + assert excinfo.value.code == 1 |
| 368 | + assert re.search(r"Connection timed out", out, re.MULTILINE) |
| 369 | + assert re.search(r"Device is rebooting", out, re.MULTILINE) |
| 370 | + assert err == "" |
| 371 | + |
| 372 | + |
264 | 373 | @pytest.mark.unit |
265 | 374 | @pytest.mark.usefixtures("reset_mt_config") |
266 | 375 | def test_main_info_with_tcp_interface(capsys): |
|
0 commit comments