Add contact normalization rather than loading the packed path bytes

This commit is contained in:
Jack Kingsman
2026-03-08 21:01:01 -07:00
parent 5a9489eff1
commit 9d806c608b
6 changed files with 186 additions and 21 deletions
+35
View File
@@ -5,6 +5,7 @@ import pytest
from app.path_utils import (
decode_path_byte,
first_hop_hex,
normalize_contact_route,
parse_packet_envelope,
path_wire_len,
split_path_hex,
@@ -153,6 +154,26 @@ class TestFirstHopHex:
assert first_hop_hex("", 0) is None
class TestNormalizeContactRoute:
def test_decodes_legacy_signed_packed_len(self):
path_hex, path_len, hash_mode = normalize_contact_route("3f3f69de1c7b7e7662", -125, 2)
assert path_hex == "3f3f69de1c7b7e7662"
assert path_len == 3
assert hash_mode == 2
def test_decodes_legacy_unsigned_packed_len(self):
path_hex, path_len, hash_mode = normalize_contact_route("7e7662ae9258", 130, None)
assert path_hex == "7e7662ae9258"
assert path_len == 2
assert hash_mode == 2
def test_normalizes_flood_to_empty_path(self):
path_hex, path_len, hash_mode = normalize_contact_route("abcd", -1, 2)
assert path_hex == ""
assert path_len == -1
assert hash_mode == -1
class TestContactToRadioDictHashMode:
"""Test that Contact.to_radio_dict() preserves the stored out_path_hash_mode."""
@@ -216,6 +237,20 @@ class TestContactToRadioDictHashMode:
d = c.to_radio_dict()
assert d["out_path_hash_mode"] == 1
def test_decodes_legacy_signed_packed_len_before_radio_sync(self):
from app.models import Contact
c = Contact(
public_key="ff" * 32,
last_path="3f3f69de1c7b7e7662",
last_path_len=-125,
out_path_hash_mode=2,
)
d = c.to_radio_dict()
assert d["out_path"] == "3f3f69de1c7b7e7662"
assert d["out_path_len"] == 3
assert d["out_path_hash_mode"] == 2
class TestContactFromRadioDictHashMode:
"""Test that Contact.from_radio_dict() preserves explicit path hash mode."""
+27
View File
@@ -377,6 +377,33 @@ class TestSyncRecentContactsToRadio:
assert payload["out_path_len"] == 2
assert payload["out_path_hash_mode"] == 1
@pytest.mark.asyncio
async def test_add_contact_decodes_legacy_packed_path_len(self, test_db):
"""Legacy signed packed path bytes are normalized before add_contact."""
await _insert_contact(
KEY_A,
"Alice",
last_contacted=2000,
last_path="3f3f69de1c7b7e7662",
last_path_len=-125,
out_path_hash_mode=2,
)
mock_mc = MagicMock()
mock_mc.get_contact_by_key_prefix = MagicMock(return_value=None)
mock_result = MagicMock()
mock_result.type = EventType.OK
mock_mc.commands.add_contact = AsyncMock(return_value=mock_result)
radio_manager._meshcore = mock_mc
result = await sync_recent_contacts_to_radio()
assert result["loaded"] == 1
payload = mock_mc.commands.add_contact.call_args.args[0]
assert payload["out_path"] == "3f3f69de1c7b7e7662"
assert payload["out_path_len"] == 3
assert payload["out_path_hash_mode"] == 2
@pytest.mark.asyncio
async def test_mc_param_bypasses_lock_acquisition(self, test_db):
"""When mc is passed, the function uses it directly without acquiring radio_operation.