From 5975006cf76293a26c5d61459d877acca0381ce3 Mon Sep 17 00:00:00 2001 From: Jack Kingsman Date: Wed, 18 Mar 2026 19:22:09 -0700 Subject: [PATCH] Dupe code cleanup --- app/models.py | 45 ++----------------------------- app/repository/contacts.py | 17 ------------ tests/test_ack_tracking_wiring.py | 5 ++-- tests/test_api.py | 5 ++-- tests/test_contacts_router.py | 27 ++++++++++--------- tests/test_echo_dedup.py | 3 --- tests/test_event_handlers.py | 28 +++++++++---------- tests/test_packet_pipeline.py | 8 +++--- tests/test_radio_router.py | 12 ++++----- tests/test_repeater_routes.py | 5 ++-- tests/test_repository.py | 2 +- 11 files changed, 50 insertions(+), 107 deletions(-) diff --git a/app/models.py b/app/models.py index 65460a9..3d3790c 100644 --- a/app/models.py +++ b/app/models.py @@ -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( diff --git a/app/repository/contacts.py b/app/repository/contacts.py index ed72a98..c57830c 100644 --- a/app/repository/contacts.py +++ b/app/repository/contacts.py @@ -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, diff --git a/tests/test_ack_tracking_wiring.py b/tests/test_ack_tracking_wiring.py index 53be2fa..4f8ee49 100644 --- a/tests/test_ack_tracking_wiring.py +++ b/tests/test_ack_tracking_wiring.py @@ -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, diff --git a/tests/test_api.py b/tests/test_api.py index 2e84d78..1031540 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -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, diff --git a/tests/test_contacts_router.py b/tests/test_contacts_router.py index e5d9ac6..b2a0e5a 100644 --- a/tests/test_contacts_router.py +++ b/tests/test_contacts_router.py @@ -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): diff --git a/tests/test_echo_dedup.py b/tests/test_echo_dedup.py index 65aba2d..e65c7e3 100644 --- a/tests/test_echo_dedup.py +++ b/tests/test_echo_dedup.py @@ -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, } ) diff --git a/tests/test_event_handlers.py b/tests/test_event_handlers.py index 8d938a6..d1ab348 100644 --- a/tests/test_event_handlers.py +++ b/tests/test_event_handlers.py @@ -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: diff --git a/tests/test_packet_pipeline.py b/tests/test_packet_pipeline.py index d23ee2b..eaf3c31 100644 --- a/tests/test_packet_pipeline.py +++ b/tests/test_packet_pipeline.py @@ -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), diff --git a/tests/test_radio_router.py b/tests/test_radio_router.py index caa0aa5..d05171f 100644 --- a/tests/test_radio_router.py +++ b/tests/test_radio_router.py @@ -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, diff --git a/tests/test_repeater_routes.py b/tests/test_repeater_routes.py index 571baf6..7f113fb 100644 --- a/tests/test_repeater_routes.py +++ b/tests/test_repeater_routes.py @@ -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, diff --git a/tests/test_repository.py b/tests/test_repository.py index 178de93..05a656c 100644 --- a/tests/test_repository.py +++ b/tests/test_repository.py @@ -750,7 +750,7 @@ class TestContactRepositoryUpsertContracts: name="Bob", type=2, on_radio=True, - out_path_hash_mode=-1, + direct_path_hash_mode=-1, ) )