|
1 | | -"""Meshtastic unit tests for showNodes favorite column feature""" |
| 1 | +"""Meshtastic unit tests for showNodes favorite column feature""" |
2 | 2 |
|
3 | 3 | from unittest.mock import MagicMock |
4 | 4 |
|
@@ -37,6 +37,19 @@ def _iface_with_favorite_nodes(): |
37 | 37 | "lastHeard": 1640204999, |
38 | 38 | "isFavorite": False, |
39 | 39 | }, |
| 40 | + "!abcdef00": { |
| 41 | + "num": 2882400000, |
| 42 | + "user": { |
| 43 | + "id": "!abcdef00", |
| 44 | + "longName": "Legacy Node", |
| 45 | + "shortName": "LEG1", |
| 46 | + "macaddr": "XYZABC00", |
| 47 | + "hwModel": "HELTEC_V3", |
| 48 | + }, |
| 49 | + "position": {}, |
| 50 | + "lastHeard": 1640205000, |
| 51 | + # Note: No isFavorite field - testing backward compatibility |
| 52 | + }, |
40 | 53 | } |
41 | 54 |
|
42 | 55 | nodesByNum = { |
@@ -66,6 +79,19 @@ def _iface_with_favorite_nodes(): |
66 | 79 | "lastHeard": 1640206200, |
67 | 80 | "isFavorite": False, |
68 | 81 | }, |
| 82 | + 2882400000: { |
| 83 | + "num": 2882400000, |
| 84 | + "user": { |
| 85 | + "id": "!abcdef00", |
| 86 | + "longName": "Legacy Node", |
| 87 | + "shortName": "LEG1", |
| 88 | + "macaddr": "XYZABC00", |
| 89 | + "hwModel": "HELTEC_V3", |
| 90 | + }, |
| 91 | + "position": {"time": 1640206100}, |
| 92 | + "lastHeard": 1640206100, |
| 93 | + # Note: No isFavorite field - testing backward compatibility |
| 94 | + }, |
69 | 95 | } |
70 | 96 |
|
71 | 97 | iface = MeshInterface(noProto=True) |
@@ -97,22 +123,33 @@ def test_showNodes_favorite_asterisk_display(capsys, _iface_with_favorite_nodes) |
97 | 123 | # Check that the output contains the "Fav" column |
98 | 124 | assert "Fav" in out |
99 | 125 |
|
100 | | - # The favorite node should have an asterisk in the output |
101 | | - # We can't easily check the exact table cell, but we can verify |
102 | | - # the asterisk appears somewhere in the output |
103 | | - lines = out.split('\n') |
104 | | - |
105 | 126 | # Find lines containing our nodes |
| 127 | + lines = out.split('\n') |
106 | 128 | favorite_line = None |
107 | 129 | regular_line = None |
| 130 | + legacy_line = None |
108 | 131 | for line in lines: |
109 | 132 | if "Favorite Node" in line or "FAV1" in line: |
110 | 133 | favorite_line = line |
111 | 134 | if "Regular Node" in line or "REG1" in line: |
112 | 135 | regular_line = line |
| 136 | + if "Legacy Node" in line or "LEG1" in line: |
| 137 | + legacy_line = line |
| 138 | + |
| 139 | + # Verify all nodes are present in the output |
| 140 | + assert favorite_line is not None, "Favorite node should be in output" |
| 141 | + assert regular_line is not None, "Regular node should be in output" |
| 142 | + assert legacy_line is not None, "Legacy node should be in output" |
| 143 | + |
| 144 | + # Verify the favorite node has an asterisk in its row |
| 145 | + assert "*" in favorite_line, "Favorite node should have an asterisk" |
| 146 | + |
| 147 | + # Verify the regular (non-favorite) node does NOT have an asterisk |
| 148 | + assert regular_line.count("*") == 0, "Non-favorite node should not have an asterisk" |
| 149 | + |
| 150 | + # Verify the legacy node (without isFavorite field) does NOT have an asterisk |
| 151 | + assert legacy_line.count("*") == 0, "Legacy node without isFavorite field should not have an asterisk" |
113 | 152 |
|
114 | | - # Basic sanity check - if we found the lines, they should be present |
115 | | - assert favorite_line is not None or regular_line is not None |
116 | 153 | assert err == "" |
117 | 154 |
|
118 | 155 |
|
@@ -158,3 +195,27 @@ def test_showNodes_default_fields_includes_favorite(_iface_with_favorite_nodes): |
158 | 195 |
|
159 | 196 | # The result should contain the formatted table as a string |
160 | 197 | assert "Fav" in result |
| 198 | + |
| 199 | + |
| 200 | +@pytest.mark.unit |
| 201 | +def test_showNodes_backward_compatibility_missing_field(capsys, _iface_with_favorite_nodes): |
| 202 | + """Test that nodes without isFavorite field are handled gracefully""" |
| 203 | + iface = _iface_with_favorite_nodes |
| 204 | + iface.showNodes() |
| 205 | + out, err = capsys.readouterr() |
| 206 | + |
| 207 | + # Find the legacy node line |
| 208 | + lines = out.split('\n') |
| 209 | + legacy_line = None |
| 210 | + for line in lines: |
| 211 | + if "Legacy Node" in line or "LEG1" in line: |
| 212 | + legacy_line = line |
| 213 | + break |
| 214 | + |
| 215 | + # Verify the legacy node appears in output |
| 216 | + assert legacy_line is not None, "Legacy node without isFavorite field should appear in output" |
| 217 | + |
| 218 | + # Verify it doesn't have an asterisk (should be treated as non-favorite) |
| 219 | + assert legacy_line.count("*") == 0, "Legacy node should not have asterisk (treated as non-favorite)" |
| 220 | + |
| 221 | + assert err == "" |
0 commit comments