77import re
88import sys
99import tempfile
10+ from types import SimpleNamespace
1011from unittest .mock import mock_open , MagicMock , patch
1112
1213import pytest
@@ -1722,11 +1723,9 @@ def test_main_onReceive_with_sendtext(caplog, capsys):
17221723
17231724@pytest .mark .unit
17241725@pytest .mark .usefixtures ("reset_mt_config" )
1725- def test_main_onReceive_with_text (caplog , capsys ):
1726- """Test onReceive with text"""
1727- args = MagicMock ()
1728- args .sendtext .return_value = "foo"
1729- mt_config .args = args
1726+ def test_main_onReceive_with_text_replies_on_target_channel (caplog , capsys ):
1727+ """Test onReceive replies when channel matches --ch-index (default 0)."""
1728+ mt_config .args = SimpleNamespace (reply = True , ch_index = 0 , sendtext = None )
17301729
17311730 # Note: 'TEXT_MESSAGE_APP' value is 1
17321731 # Note: Some of this is faked below.
@@ -1752,6 +1751,83 @@ def test_main_onReceive_with_text(caplog, capsys):
17521751 assert re .search (r"in onReceive" , caplog .text , re .MULTILINE )
17531752 out , err = capsys .readouterr ()
17541753 assert re .search (r"Sending reply" , out , re .MULTILINE )
1754+ iface .sendText .assert_called_once_with (
1755+ "got msg 'faked' with rxSnr: 6.0 and hopLimit: 3" , channelIndex = 0
1756+ )
1757+ assert err == ""
1758+
1759+
1760+ @pytest .mark .unit
1761+ @pytest .mark .usefixtures ("reset_mt_config" )
1762+ def test_main_onReceive_with_text_ignores_non_target_channel (caplog , capsys ):
1763+ """Test onReceive does not reply when packet channel differs from --ch-index."""
1764+ mt_config .args = SimpleNamespace (reply = True , ch_index = 1 , sendtext = None )
1765+
1766+ packet = {
1767+ "to" : 4294967295 ,
1768+ "decoded" : {"portnum" : 1 , "payload" : "hello" , "text" : "faked" },
1769+ "id" : 334776977 ,
1770+ "hop_limit" : 3 ,
1771+ "want_ack" : True ,
1772+ "rxSnr" : 6.0 ,
1773+ "hopLimit" : 3 ,
1774+ "raw" : "faked" ,
1775+ "fromId" : "!28b5465c" ,
1776+ "toId" : "^all" ,
1777+ "channel" : 0 ,
1778+ }
1779+
1780+ iface = MagicMock (autospec = SerialInterface )
1781+ iface .myInfo .my_node_num = 4294967295
1782+
1783+ with patch ("meshtastic.serial_interface.SerialInterface" , return_value = iface ):
1784+ with caplog .at_level (logging .DEBUG ):
1785+ onReceive (packet , iface )
1786+ assert re .search (r"in onReceive" , caplog .text , re .MULTILINE )
1787+ out , err = capsys .readouterr ()
1788+ assert re .search (
1789+ r"Ignored message on channel 0 \(waiting for channel 1\)" , out , re .MULTILINE
1790+ )
1791+ iface .sendText .assert_not_called ()
1792+ assert err == ""
1793+
1794+
1795+ @pytest .mark .unit
1796+ @pytest .mark .usefixtures ("reset_mt_config" )
1797+ def test_main_onReceive_with_text_replies_on_explicit_matching_channel (caplog , capsys ):
1798+ """Test onReceive replies when explicit packet channel matches --ch-index."""
1799+ mt_config .args = SimpleNamespace (reply = True , ch_index = 2 , sendtext = None )
1800+
1801+ packet = {
1802+ "to" : 4294967295 ,
1803+ "decoded" : {"portnum" : 1 , "payload" : "hello" , "text" : "faked" },
1804+ "id" : 334776977 ,
1805+ "hop_limit" : 3 ,
1806+ "want_ack" : True ,
1807+ "rxSnr" : 6.0 ,
1808+ "hopLimit" : 3 ,
1809+ "raw" : "faked" ,
1810+ "fromId" : "!28b5465c" ,
1811+ "toId" : "^all" ,
1812+ "channel" : 2 ,
1813+ }
1814+
1815+ iface = MagicMock (autospec = SerialInterface )
1816+ iface .myInfo .my_node_num = 4294967295
1817+
1818+ with patch ("meshtastic.serial_interface.SerialInterface" , return_value = iface ):
1819+ with caplog .at_level (logging .DEBUG ):
1820+ onReceive (packet , iface )
1821+ assert re .search (r"in onReceive" , caplog .text , re .MULTILINE )
1822+ out , err = capsys .readouterr ()
1823+ assert re .search (
1824+ r"Received channel 2\. Sending reply: got msg 'faked' with rxSnr: 6.0 and hopLimit: 3" ,
1825+ out ,
1826+ re .MULTILINE ,
1827+ )
1828+ iface .sendText .assert_called_once_with (
1829+ "got msg 'faked' with rxSnr: 6.0 and hopLimit: 3" , channelIndex = 2
1830+ )
17551831 assert err == ""
17561832
17571833
0 commit comments