mirror of
https://github.com/jkingsman/Remote-Terminal-for-MeshCore.git
synced 2026-03-28 17:43:05 +01:00
Fix last-advert selection logic for path recency
This commit is contained in:
@@ -442,8 +442,8 @@ async def _process_advertisement(
|
||||
PATH_FRESHNESS_SECONDS = 60
|
||||
use_existing_path = False
|
||||
|
||||
if existing and existing.last_seen:
|
||||
path_age = timestamp - existing.last_seen
|
||||
if existing and existing.last_advert:
|
||||
path_age = timestamp - existing.last_advert
|
||||
existing_path_len = existing.last_path_len if existing.last_path_len >= 0 else float("inf")
|
||||
|
||||
# Keep existing path if it's fresh and shorter (or equal)
|
||||
|
||||
@@ -291,6 +291,7 @@ class TestAdvertisementPipeline:
|
||||
"public_key": test_pubkey,
|
||||
"name": "TestNode",
|
||||
"type": 1,
|
||||
"last_advert": 1000,
|
||||
"last_seen": 1000,
|
||||
"last_path_len": 3,
|
||||
"last_path": "aabbcc", # 3 bytes = 3 hops
|
||||
@@ -353,6 +354,54 @@ class TestAdvertisementPipeline:
|
||||
contact = await ContactRepository.get_by_key(test_pubkey)
|
||||
assert contact.last_path_len == 1 # Still the shorter path
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_advertisement_path_freshness_uses_last_advert_not_last_seen(
|
||||
self, test_db, captured_broadcasts
|
||||
):
|
||||
"""Non-advert contact activity should not keep an old advert path artificially fresh."""
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
from app.decoder import ParsedAdvertisement
|
||||
from app.packet_processor import _process_advertisement
|
||||
|
||||
test_pubkey = "abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890"
|
||||
await ContactRepository.upsert(
|
||||
{
|
||||
"public_key": test_pubkey,
|
||||
"name": "TestNode",
|
||||
"type": 1,
|
||||
"last_advert": 1000,
|
||||
"last_seen": 1055, # Simulates later non-advert activity
|
||||
"last_path_len": 1,
|
||||
"last_path": "aa",
|
||||
}
|
||||
)
|
||||
|
||||
broadcasts, mock_broadcast = captured_broadcasts
|
||||
|
||||
longer_packet_info = MagicMock()
|
||||
longer_packet_info.path_length = 3
|
||||
longer_packet_info.path = bytes.fromhex("aabbcc")
|
||||
longer_packet_info.path_hash_size = 1
|
||||
longer_packet_info.payload = b""
|
||||
|
||||
with patch("app.packet_processor.broadcast_event", mock_broadcast):
|
||||
with patch("app.packet_processor.parse_advertisement") as mock_parse:
|
||||
mock_parse.return_value = ParsedAdvertisement(
|
||||
public_key=test_pubkey,
|
||||
name="TestNode",
|
||||
timestamp=1070,
|
||||
lat=None,
|
||||
lon=None,
|
||||
device_role=1,
|
||||
)
|
||||
await _process_advertisement(b"", timestamp=1070, packet_info=longer_packet_info)
|
||||
|
||||
contact = await ContactRepository.get_by_key(test_pubkey)
|
||||
assert contact is not None
|
||||
assert contact.last_path_len == 3
|
||||
assert contact.last_path == "aabbcc"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_advertisement_default_path_len_treated_as_infinity(
|
||||
self, test_db, captured_broadcasts
|
||||
|
||||
Reference in New Issue
Block a user