mirror of
https://github.com/jkingsman/Remote-Terminal-for-MeshCore.git
synced 2026-03-28 17:43:05 +01:00
Dupe code cleanup
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
from collections.abc import Mapping
|
||||
from typing import Any, Literal
|
||||
from typing import Literal
|
||||
|
||||
from pydantic import BaseModel, Field, model_validator
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from app.path_utils import normalize_contact_route, normalize_route_override
|
||||
|
||||
@@ -38,20 +37,6 @@ class ContactUpsert(BaseModel):
|
||||
last_contacted: int | None = None
|
||||
first_seen: int | None = None
|
||||
|
||||
@model_validator(mode="before")
|
||||
@classmethod
|
||||
def _translate_legacy_route_fields(cls, data: Any) -> Any:
|
||||
if not isinstance(data, Mapping):
|
||||
return data
|
||||
translated = dict(data)
|
||||
if "direct_path" not in translated and "last_path" in translated:
|
||||
translated["direct_path"] = translated.get("last_path")
|
||||
if "direct_path_len" not in translated and "last_path_len" in translated:
|
||||
translated["direct_path_len"] = translated.get("last_path_len")
|
||||
if "direct_path_hash_mode" not in translated and "out_path_hash_mode" in translated:
|
||||
translated["direct_path_hash_mode"] = translated.get("out_path_hash_mode")
|
||||
return translated
|
||||
|
||||
@classmethod
|
||||
def from_contact(cls, contact: "Contact", **changes) -> "ContactUpsert":
|
||||
return cls.model_validate(
|
||||
@@ -114,20 +99,6 @@ class Contact(BaseModel):
|
||||
direct_route: ContactRoute | None = None
|
||||
route_override: ContactRoute | None = None
|
||||
|
||||
@model_validator(mode="before")
|
||||
@classmethod
|
||||
def _translate_legacy_route_fields(cls, data: Any) -> Any:
|
||||
if not isinstance(data, Mapping):
|
||||
return data
|
||||
translated = dict(data)
|
||||
if "direct_path" not in translated and "last_path" in translated:
|
||||
translated["direct_path"] = translated.get("last_path")
|
||||
if "direct_path_len" not in translated and "last_path_len" in translated:
|
||||
translated["direct_path_len"] = translated.get("last_path_len")
|
||||
if "direct_path_hash_mode" not in translated and "out_path_hash_mode" in translated:
|
||||
translated["direct_path_hash_mode"] = translated.get("out_path_hash_mode")
|
||||
return translated
|
||||
|
||||
def model_post_init(self, __context) -> None:
|
||||
direct_path, direct_path_len, direct_path_hash_mode = normalize_contact_route(
|
||||
self.direct_path,
|
||||
@@ -186,18 +157,6 @@ class Contact(BaseModel):
|
||||
def has_route_override(self) -> bool:
|
||||
return self.route_override_len is not None
|
||||
|
||||
@property
|
||||
def last_path(self) -> str | None:
|
||||
return self.direct_path
|
||||
|
||||
@property
|
||||
def last_path_len(self) -> int:
|
||||
return self.direct_path_len
|
||||
|
||||
@property
|
||||
def out_path_hash_mode(self) -> int:
|
||||
return self.direct_path_hash_mode
|
||||
|
||||
def effective_route_tuple(self) -> tuple[str, int, int]:
|
||||
if self.has_route_override():
|
||||
return normalize_contact_route(
|
||||
|
||||
@@ -335,23 +335,6 @@ class ContactRepository:
|
||||
)
|
||||
await db.conn.commit()
|
||||
|
||||
@staticmethod
|
||||
async def update_path(
|
||||
public_key: str,
|
||||
path: str,
|
||||
path_len: int,
|
||||
path_hash_mode: int | None = None,
|
||||
updated_at: int | None = None,
|
||||
) -> None:
|
||||
"""Compatibility shim for legacy callers/tests."""
|
||||
await ContactRepository.update_direct_path(
|
||||
public_key,
|
||||
path,
|
||||
path_len,
|
||||
path_hash_mode,
|
||||
updated_at=updated_at,
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
async def set_routing_override(
|
||||
public_key: str,
|
||||
|
||||
@@ -49,8 +49,9 @@ async def _insert_contact(public_key, name="Alice"):
|
||||
"name": name,
|
||||
"type": 0,
|
||||
"flags": 0,
|
||||
"last_path": None,
|
||||
"last_path_len": -1,
|
||||
"direct_path": None,
|
||||
"direct_path_len": -1,
|
||||
"direct_path_hash_mode": -1,
|
||||
"last_advert": None,
|
||||
"lat": None,
|
||||
"lon": None,
|
||||
|
||||
@@ -62,8 +62,9 @@ async def _insert_contact(public_key, name="Alice", **overrides):
|
||||
"name": name,
|
||||
"type": 0,
|
||||
"flags": 0,
|
||||
"last_path": None,
|
||||
"last_path_len": -1,
|
||||
"direct_path": None,
|
||||
"direct_path_len": -1,
|
||||
"direct_path_hash_mode": -1,
|
||||
"last_advert": None,
|
||||
"lat": None,
|
||||
"lon": None,
|
||||
|
||||
@@ -44,8 +44,9 @@ async def _insert_contact(public_key=KEY_A, name="Alice", on_radio=False, **over
|
||||
"name": name,
|
||||
"type": 0,
|
||||
"flags": 0,
|
||||
"last_path": None,
|
||||
"last_path_len": -1,
|
||||
"direct_path": None,
|
||||
"direct_path_len": -1,
|
||||
"direct_path_hash_mode": -1,
|
||||
"last_advert": None,
|
||||
"lat": None,
|
||||
"lon": None,
|
||||
@@ -307,9 +308,9 @@ class TestPathDiscovery:
|
||||
|
||||
updated = await ContactRepository.get_by_key(KEY_A)
|
||||
assert updated is not None
|
||||
assert updated.last_path == "11223344"
|
||||
assert updated.last_path_len == 2
|
||||
assert updated.out_path_hash_mode == 1
|
||||
assert updated.direct_path == "11223344"
|
||||
assert updated.direct_path_len == 2
|
||||
assert updated.direct_path_hash_mode == 1
|
||||
mc.commands.add_contact.assert_awaited()
|
||||
mock_broadcast.assert_called_once_with("contact", updated.model_dump())
|
||||
|
||||
@@ -527,7 +528,7 @@ class TestRoutingOverride:
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_set_explicit_routing_override(self, test_db, client):
|
||||
await _insert_contact(KEY_A, last_path="11", last_path_len=1, out_path_hash_mode=0)
|
||||
await _insert_contact(KEY_A, direct_path="11", direct_path_len=1, direct_path_hash_mode=0)
|
||||
|
||||
with (
|
||||
patch("app.routers.contacts.radio_manager") as mock_rm,
|
||||
@@ -542,8 +543,8 @@ class TestRoutingOverride:
|
||||
assert response.status_code == 200
|
||||
contact = await ContactRepository.get_by_key(KEY_A)
|
||||
assert contact is not None
|
||||
assert contact.last_path == "11"
|
||||
assert contact.last_path_len == 1
|
||||
assert contact.direct_path == "11"
|
||||
assert contact.direct_path_len == 1
|
||||
assert contact.route_override_path == "ae92f13e"
|
||||
assert contact.route_override_len == 2
|
||||
assert contact.route_override_hash_mode == 1
|
||||
@@ -554,9 +555,9 @@ class TestRoutingOverride:
|
||||
await _insert_contact(
|
||||
KEY_A,
|
||||
on_radio=True,
|
||||
last_path="11",
|
||||
last_path_len=1,
|
||||
out_path_hash_mode=0,
|
||||
direct_path="11",
|
||||
direct_path_len=1,
|
||||
direct_path_hash_mode=0,
|
||||
)
|
||||
|
||||
mock_mc = MagicMock()
|
||||
@@ -584,8 +585,8 @@ class TestRoutingOverride:
|
||||
contact = await ContactRepository.get_by_key(KEY_A)
|
||||
assert contact is not None
|
||||
assert contact.route_override_len == -1
|
||||
assert contact.last_path == "11"
|
||||
assert contact.last_path_len == 1
|
||||
assert contact.direct_path == "11"
|
||||
assert contact.direct_path_len == 1
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_blank_route_clears_override_and_preserves_learned_path(self, test_db, client):
|
||||
|
||||
@@ -559,7 +559,6 @@ class TestDualPathDedup:
|
||||
"last_contacted": SENDER_TIMESTAMP,
|
||||
"first_seen": SENDER_TIMESTAMP,
|
||||
"on_radio": False,
|
||||
"out_path_hash_mode": 0,
|
||||
}
|
||||
)
|
||||
|
||||
@@ -631,7 +630,6 @@ class TestDualPathDedup:
|
||||
"last_contacted": SENDER_TIMESTAMP,
|
||||
"first_seen": SENDER_TIMESTAMP,
|
||||
"on_radio": False,
|
||||
"out_path_hash_mode": 0,
|
||||
}
|
||||
)
|
||||
|
||||
@@ -706,7 +704,6 @@ class TestDualPathDedup:
|
||||
"last_contacted": SENDER_TIMESTAMP,
|
||||
"first_seen": SENDER_TIMESTAMP,
|
||||
"on_radio": False,
|
||||
"out_path_hash_mode": 0,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@@ -850,9 +850,9 @@ class TestOnPathUpdate:
|
||||
# Verify path was updated in DB
|
||||
contact = await ContactRepository.get_by_key("aa" * 32)
|
||||
assert contact is not None
|
||||
assert contact.last_path == "0102"
|
||||
assert contact.last_path_len == 2
|
||||
assert contact.out_path_hash_mode == 0
|
||||
assert contact.direct_path == "0102"
|
||||
assert contact.direct_path_len == 2
|
||||
assert contact.direct_path_hash_mode == 0
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_updates_path_hash_mode_when_present(self, test_db):
|
||||
@@ -880,9 +880,9 @@ class TestOnPathUpdate:
|
||||
|
||||
contact = await ContactRepository.get_by_key("ab" * 32)
|
||||
assert contact is not None
|
||||
assert contact.last_path == "aa00bb00"
|
||||
assert contact.last_path_len == 2
|
||||
assert contact.out_path_hash_mode == 1
|
||||
assert contact.direct_path == "aa00bb00"
|
||||
assert contact.direct_path_len == 2
|
||||
assert contact.direct_path_hash_mode == 1
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_does_nothing_when_contact_not_found(self, test_db):
|
||||
@@ -924,8 +924,8 @@ class TestOnPathUpdate:
|
||||
|
||||
contact = await ContactRepository.get_by_key("bb" * 32)
|
||||
assert contact is not None
|
||||
assert contact.last_path == "0a0b"
|
||||
assert contact.last_path_len == 2
|
||||
assert contact.direct_path == "0a0b"
|
||||
assert contact.direct_path_len == 2
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_missing_path_fields_does_not_modify_contact(self, test_db):
|
||||
@@ -940,7 +940,7 @@ class TestOnPathUpdate:
|
||||
"flags": 0,
|
||||
}
|
||||
)
|
||||
await ContactRepository.update_path("dd" * 32, "beef", 2)
|
||||
await ContactRepository.update_direct_path("dd" * 32, "beef", 2)
|
||||
|
||||
class MockEvent:
|
||||
payload = {"public_key": "dd" * 32}
|
||||
@@ -949,8 +949,8 @@ class TestOnPathUpdate:
|
||||
|
||||
contact = await ContactRepository.get_by_key("dd" * 32)
|
||||
assert contact is not None
|
||||
assert contact.last_path == "beef"
|
||||
assert contact.last_path_len == 2
|
||||
assert contact.direct_path == "beef"
|
||||
assert contact.direct_path_len == 2
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_missing_identity_fields_noop(self, test_db):
|
||||
@@ -965,7 +965,7 @@ class TestOnPathUpdate:
|
||||
"flags": 0,
|
||||
}
|
||||
)
|
||||
await ContactRepository.update_path("ee" * 32, "abcd", 2)
|
||||
await ContactRepository.update_direct_path("ee" * 32, "abcd", 2)
|
||||
|
||||
class MockEvent:
|
||||
payload = {}
|
||||
@@ -974,8 +974,8 @@ class TestOnPathUpdate:
|
||||
|
||||
contact = await ContactRepository.get_by_key("ee" * 32)
|
||||
assert contact is not None
|
||||
assert contact.last_path == "abcd"
|
||||
assert contact.last_path_len == 2
|
||||
assert contact.direct_path == "abcd"
|
||||
assert contact.direct_path_len == 2
|
||||
|
||||
|
||||
class TestOnNewContact:
|
||||
|
||||
@@ -216,8 +216,8 @@ class TestAdvertisementPipeline:
|
||||
assert contact.lon is not None
|
||||
assert abs(contact.lat - expected["lat"]) < 0.001
|
||||
assert abs(contact.lon - expected["lon"]) < 0.001
|
||||
assert contact.last_path_len == -1
|
||||
assert contact.last_path in (None, "")
|
||||
assert contact.direct_path_len == -1
|
||||
assert contact.direct_path in (None, "")
|
||||
|
||||
advert_paths = await ContactAdvertPathRepository.get_recent_for_contact(contact.public_key)
|
||||
assert len(advert_paths) == 1
|
||||
@@ -559,7 +559,7 @@ class TestAdvertisementPipeline:
|
||||
|
||||
contact = await ContactRepository.get_by_key(test_pubkey)
|
||||
assert contact is not None
|
||||
assert contact.last_path_len == -1
|
||||
assert contact.direct_path_len == -1
|
||||
advert_paths = await ContactAdvertPathRepository.get_recent_for_contact(test_pubkey)
|
||||
assert [(path.path, path.path_len) for path in advert_paths] == [
|
||||
("aabbccdd", 4),
|
||||
@@ -1878,7 +1878,7 @@ class TestProcessRawPacketIntegration:
|
||||
|
||||
contact = await ContactRepository.get_by_key(test_pubkey)
|
||||
assert contact is not None
|
||||
assert contact.last_path_len == -1
|
||||
assert contact.direct_path_len == -1
|
||||
advert_paths = await ContactAdvertPathRepository.get_recent_for_contact(test_pubkey)
|
||||
assert [(path.path, path.path_len) for path in advert_paths] == [
|
||||
("dd", 1),
|
||||
|
||||
@@ -356,9 +356,9 @@ class TestDiscoverMesh:
|
||||
name=None,
|
||||
type=2,
|
||||
flags=0,
|
||||
last_path=None,
|
||||
last_path_len=-1,
|
||||
out_path_hash_mode=0,
|
||||
direct_path=None,
|
||||
direct_path_len=-1,
|
||||
direct_path_hash_mode=-1,
|
||||
last_advert=None,
|
||||
lat=None,
|
||||
lon=None,
|
||||
@@ -418,9 +418,9 @@ class TestDiscoverMesh:
|
||||
name="Known",
|
||||
type=4,
|
||||
flags=0,
|
||||
last_path=None,
|
||||
last_path_len=-1,
|
||||
out_path_hash_mode=0,
|
||||
direct_path=None,
|
||||
direct_path_len=-1,
|
||||
direct_path_hash_mode=-1,
|
||||
last_advert=None,
|
||||
lat=None,
|
||||
lon=None,
|
||||
|
||||
@@ -58,8 +58,9 @@ async def _insert_contact(public_key: str, name: str = "Node", contact_type: int
|
||||
"name": name,
|
||||
"type": contact_type,
|
||||
"flags": 0,
|
||||
"last_path": None,
|
||||
"last_path_len": -1,
|
||||
"direct_path": None,
|
||||
"direct_path_len": -1,
|
||||
"direct_path_hash_mode": -1,
|
||||
"last_advert": None,
|
||||
"lat": None,
|
||||
"lon": None,
|
||||
|
||||
@@ -750,7 +750,7 @@ class TestContactRepositoryUpsertContracts:
|
||||
name="Bob",
|
||||
type=2,
|
||||
on_radio=True,
|
||||
out_path_hash_mode=-1,
|
||||
direct_path_hash_mode=-1,
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user