From b56440a4e8cd9e3860abfe78b4110c87002bf34d Mon Sep 17 00:00:00 2001 From: Mike Kinney Date: Thu, 6 Jan 2022 11:55:36 -0800 Subject: [PATCH] should not need to talk with remote node if just doing sendtext --- meshtastic/__main__.py | 5 +++ meshtastic/tests/test_main.py | 80 +++++++++++++++++++---------------- 2 files changed, 49 insertions(+), 36 deletions(-) diff --git a/meshtastic/__main__.py b/meshtastic/__main__.py index 32bc746..341f4f4 100644 --- a/meshtastic/__main__.py +++ b/meshtastic/__main__.py @@ -243,7 +243,12 @@ def onConnected(interface): channelIndex = 0 if args.ch_index is not None: channelIndex = int(args.ch_index) + logging.debug(f'channelIndex:{channelIndex}') + logging.debug(f'interface.localNode:{interface.localNode}') + logging.debug(f'interface.localNode.channels:{interface.localNode.channels}') + our_globals.set_target_node(interface.localNode) ch = getNode().getChannelByChannelIndex(channelIndex) + logging.debug(f'ch:{ch}') if ch and ch.role != channel_pb2.Channel.Role.DISABLED: print(f"Sending text message {args.sendtext} to {args.destOrAll} on channelIndex:{channelIndex}") interface.sendText(args.sendtext, args.destOrAll, wantAck=True, channelIndex=channelIndex) diff --git a/meshtastic/tests/test_main.py b/meshtastic/tests/test_main.py index e437e10..d408e5c 100644 --- a/meshtastic/tests/test_main.py +++ b/meshtastic/tests/test_main.py @@ -456,63 +456,71 @@ def test_main_sendtext_with_channel(capsys, reset_globals): @pytest.mark.unit -def test_main_sendtext_with_invalid_channel(capsys, reset_globals): +def test_main_sendtext_with_invalid_channel(caplog, capsys, reset_globals): """Test --sendtext""" sys.argv = ['', '--sendtext', 'hello', '--ch-index', '-1'] Globals.getInstance().set_args(sys.argv) iface = MagicMock(autospec=SerialInterface) - iface.getChannelByChannelIndex.return_value = None - with patch('meshtastic.serial_interface.SerialInterface', return_value=iface) as mo: - iface.getNode.return_value.getChannelByChannelIndex.return_value = None - with pytest.raises(SystemExit) as pytest_wrapped_e: - main() - assert pytest_wrapped_e.type == SystemExit - assert pytest_wrapped_e.value.code == 1 - out, err = capsys.readouterr() - assert re.search(r'is not a valid channel', out, re.MULTILINE) - assert err == '' - mo.assert_called() + iface.localNode.getChannelByChannelIndex.return_value = None + + with caplog.at_level(logging.DEBUG): + with patch('meshtastic.serial_interface.SerialInterface', return_value=iface) as mo: + with pytest.raises(SystemExit) as pytest_wrapped_e: + main() + assert pytest_wrapped_e.type == SystemExit + assert pytest_wrapped_e.value.code == 1 + out, err = capsys.readouterr() + assert re.search(r'is not a valid channel', out, re.MULTILINE) + assert err == '' + mo.assert_called() @pytest.mark.unit -def test_main_sendtext_with_invalid_channel_nine(capsys, reset_globals): +def test_main_sendtext_with_invalid_channel_nine(caplog, capsys, reset_globals): """Test --sendtext""" sys.argv = ['', '--sendtext', 'hello', '--ch-index', '9'] Globals.getInstance().set_args(sys.argv) iface = MagicMock(autospec=SerialInterface) - with patch('meshtastic.serial_interface.SerialInterface', return_value=iface) as mo: - iface.getNode.return_value.getChannelByChannelIndex.return_value = None - with pytest.raises(SystemExit) as pytest_wrapped_e: - main() - assert pytest_wrapped_e.type == SystemExit - assert pytest_wrapped_e.value.code == 1 - out, err = capsys.readouterr() - assert re.search(r'is not a valid channel', out, re.MULTILINE) - assert err == '' - mo.assert_called() + iface.localNode.getChannelByChannelIndex.return_value = None + + with caplog.at_level(logging.DEBUG): + with patch('meshtastic.serial_interface.SerialInterface', return_value=iface) as mo: + with pytest.raises(SystemExit) as pytest_wrapped_e: + main() + assert pytest_wrapped_e.type == SystemExit + assert pytest_wrapped_e.value.code == 1 + out, err = capsys.readouterr() + assert re.search(r'is not a valid channel', out, re.MULTILINE) + assert err == '' + mo.assert_called() @pytest.mark.unit -def test_main_sendtext_with_dest(capsys, reset_globals): +def test_main_sendtext_with_dest(capsys, caplog, reset_globals, iface_with_nodes): """Test --sendtext with --dest""" sys.argv = ['', '--sendtext', 'hello', '--dest', 'foo'] Globals.getInstance().set_args(sys.argv) - iface = MagicMock(autospec=SerialInterface) - def mock_sendText(text, dest, wantAck, channelIndex): - print('inside mocked sendText') - iface.sendText.side_effect = mock_sendText + iface = iface_with_nodes + iface.myInfo.my_node_num = 2475227164 + mocked_channel = MagicMock(autospec=Channel) + iface.localNode.getChannelByChannelIndex = mocked_channel - with patch('meshtastic.serial_interface.SerialInterface', return_value=iface) as mo: - main() - out, err = capsys.readouterr() - assert re.search(r'Connected to radio', out, re.MULTILINE) - assert re.search(r'Sending text message', out, re.MULTILINE) - assert re.search(r'inside mocked sendText', out, re.MULTILINE) - assert err == '' - mo.assert_called() + with patch('meshtastic.serial_interface.SerialInterface', return_value=iface): + with caplog.at_level(logging.DEBUG): + + with pytest.raises(SystemExit) as pytest_wrapped_e: + main() + assert pytest_wrapped_e.type == SystemExit + assert pytest_wrapped_e.value.code == 1 + out, err = capsys.readouterr() + assert re.search(r'Connected to radio', out, re.MULTILINE) + assert not re.search(r"Warning: 0 is not a valid channel", out, re.MULTILINE) + assert not re.search(r"There is a SECONDARY channel named 'admin'", out, re.MULTILINE) + assert re.search(r'Warning: NodeId foo not found in DB', out, re.MULTILINE) + assert err == '' @pytest.mark.unit