Skip to content

Commit ceca7b3

Browse files
committed
Add a few extra tests of the revamped channel deletion behavior
1 parent 15438c8 commit ceca7b3

1 file changed

Lines changed: 72 additions & 0 deletions

File tree

meshtastic/tests/test_node.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,78 @@ def test_getChannelByChannelIndex():
669669
assert anode.getChannelByChannelIndex(9) is None
670670

671671

672+
def _build_channels(highest_secondary_index: int):
673+
"""Build an 8-slot channel table with contiguous active channels.
674+
675+
Slot 0 is PRIMARY. Slots 1..highest_secondary_index are SECONDARY.
676+
Remaining slots are DISABLED.
677+
"""
678+
channels = []
679+
for idx in range(8):
680+
ch = Channel()
681+
ch.index = idx
682+
if idx == 0:
683+
ch.role = Channel.Role.PRIMARY
684+
ch.settings.name = "primary"
685+
elif idx <= highest_secondary_index:
686+
ch.role = Channel.Role.SECONDARY
687+
ch.settings.name = f"ch{idx}"
688+
else:
689+
ch.role = Channel.Role.DISABLED
690+
channels.append(ch)
691+
return channels
692+
693+
694+
@pytest.mark.unit
695+
@pytest.mark.parametrize(
696+
"highest_secondary_index,delete_index,expected_writes",
697+
[
698+
pytest.param(1, 1, [1], id="active-0-1-del-1"),
699+
pytest.param(2, 1, [1, 2], id="active-0-2-del-1"),
700+
pytest.param(3, 1, [1, 2, 3], id="active-0-3-del-1"),
701+
pytest.param(3, 2, [2, 3], id="active-0-3-del-2"),
702+
],
703+
)
704+
def test_delete_channel_writes_only_changed_suffix(
705+
highest_secondary_index, delete_index, expected_writes
706+
):
707+
"""deleteChannel should only write slots whose payload changed."""
708+
iface = MagicMock()
709+
anode = Node(iface, "bar", noProto=True)
710+
iface.localNode = anode
711+
anode.channels = _build_channels(highest_secondary_index)
712+
713+
writes = []
714+
715+
def fake_write(channel_index, adminIndex=0):
716+
writes.append((channel_index, adminIndex))
717+
718+
anode.writeChannel = fake_write
719+
720+
anode.deleteChannel(delete_index)
721+
722+
written_indices = [idx for idx, _ in writes]
723+
assert written_indices == expected_writes
724+
assert all(admin_idx == 0 for _, admin_idx in writes)
725+
assert 0 not in written_indices
726+
assert all(idx < 4 for idx in written_indices)
727+
728+
729+
@pytest.mark.unit
730+
def test_delete_channel_rejects_primary():
731+
"""deleteChannel should refuse deleting PRIMARY slot 0."""
732+
iface = MagicMock()
733+
anode = Node(iface, "bar", noProto=True)
734+
iface.localNode = anode
735+
anode.channels = _build_channels(3)
736+
737+
with pytest.raises(SystemExit) as pytest_wrapped_e:
738+
anode.deleteChannel(0)
739+
740+
assert pytest_wrapped_e.type == SystemExit
741+
assert pytest_wrapped_e.value.code == 1
742+
743+
672744
# TODO
673745
# @pytest.mark.unit
674746
# def test_deleteChannel_try_to_delete_primary_channel(capsys):

0 commit comments

Comments
 (0)