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