mirror of
https://github.com/pyMC-dev/pyMC_Repeater.git
synced 2026-10-30 19:51:59 +01:00
"Import repeater contacts" wrote nothing whenever a contact-type filter was
supplied, and reported success having imported zero rows.
`adverts.contact_type` holds the *display* name written through
`handler_helpers.discovery.NODE_TYPE_NAMES` — "Chat Node", "Repeater",
"Room Server", "Sensor". The import API accepts MeshCore's names — "companion",
"repeater", "room_server", "sensor", validated in
`companion_endpoints.import_repeater_contacts`. `companion_import_repeater_contacts`
compared them directly:
query += f" AND contact_type IN ({placeholders})"
so "repeater" never matched "Repeater", "room_server" never matched
"Room Server", and "companion" never matched "Chat Node" at all. On a live
repeater with 285 adverts, every one of the four permitted values selected 0
rows, as did all four together; only an unfiltered import worked.
A second defect had the same cause. The adv_type lookup normalised the stored
name (`lower()`, spaces to underscores) but mapped it with the API-name table,
where "chat_node" is absent — so chat contacts imported as adv_type 0 rather
than 1, even on an unfiltered import. That is silently wrong rather than empty,
and needs a manual `UPDATE companion_contacts SET adv_type=1 WHERE adv_type=0`
to repair.
Both sites derived the mapping independently, which is how they drifted. They
now share one `_ADVERT_TYPE_BY_STORED` table keyed on the normalised stored
form: the filter reverse-maps the requested API names onto the stored keys and
compares `LOWER(REPLACE(TRIM(contact_type), ' ', '_'))`, and the adv_type comes
from the same table. Filtering and limiting stay in SQL. An unrecognised type
still selects nothing rather than silently widening the query.
Tested against the real stored forms: 6 of the 8 new tests fail on current dev
(each of the four type filters, all four combined, and the adv_type mapping)
and all 8 pass with this change.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
130 lines
4.2 KiB
Python
130 lines
4.2 KiB
Python
"""Regression tests for bulk 'import repeater contacts'.
|
|
|
|
``adverts.contact_type`` stores the *display* name written through
|
|
``handler_helpers.discovery.NODE_TYPE_NAMES`` -- "Chat Node", "Repeater",
|
|
"Room Server", "Sensor". The import API accepts MeshCore's names -- "companion",
|
|
"repeater", "room_server", "sensor" (validated in
|
|
``companion_endpoints.import_repeater_contacts``).
|
|
|
|
``companion_import_repeater_contacts`` compared the two directly:
|
|
|
|
query += f" AND contact_type IN ({placeholders})" # API names
|
|
# vs stored display names
|
|
|
|
so every filtered import matched zero rows and reported success having written
|
|
nothing. Separately the adv_type lookup normalised "Chat Node" to "chat_node",
|
|
which was absent from its map, so chat contacts imported as adv_type 0 rather
|
|
than 1 even on an unfiltered import.
|
|
|
|
Both derived the mapping independently; they now share one table.
|
|
"""
|
|
import os
|
|
import shutil
|
|
import sqlite3
|
|
import sys
|
|
import tempfile
|
|
import time
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
|
|
from repeater.data_acquisition.sqlite_handler import SQLiteHandler # noqa: E402
|
|
|
|
# Exactly the forms the repeater writes.
|
|
SEEDS = [
|
|
("aa" * 32, "RepeaterOne", "Repeater", 2),
|
|
("bb" * 32, "ChatOne", "Chat Node", 1),
|
|
("cc" * 32, "RoomOne", "Room Server", 3),
|
|
("dd" * 32, "SensorOne", "Sensor", 4),
|
|
]
|
|
|
|
|
|
@pytest.fixture
|
|
def handler():
|
|
d = tempfile.mkdtemp()
|
|
h = SQLiteHandler(Path(d))
|
|
con = sqlite3.connect(os.path.join(d, "repeater.db"))
|
|
now = time.time()
|
|
for pk, name, ctype, _ in SEEDS:
|
|
con.execute(
|
|
"INSERT INTO adverts"
|
|
" (timestamp, pubkey, node_name, is_repeater, contact_type, latitude,"
|
|
" longitude, first_seen, last_seen, advert_count, is_new_neighbor, zero_hop)"
|
|
" VALUES (?, ?, ?, ?, ?, 0, 0, ?, ?, 1, 0, 0)",
|
|
(now, pk, name, 1 if ctype == "Repeater" else 0, ctype, now, now),
|
|
)
|
|
con.commit()
|
|
con.close()
|
|
yield h, d
|
|
shutil.rmtree(d, ignore_errors=True)
|
|
|
|
|
|
def _imported(d, companion="0x01"):
|
|
con = sqlite3.connect(os.path.join(d, "repeater.db"))
|
|
try:
|
|
return {
|
|
(r[0].hex() if isinstance(r[0], (bytes, bytearray)) else r[0]): r[1]
|
|
for r in con.execute(
|
|
"select pubkey, adv_type from companion_contacts where companion_hash=?",
|
|
(companion,),
|
|
)
|
|
}
|
|
finally:
|
|
con.close()
|
|
|
|
|
|
def test_unfiltered_import_takes_everything(handler):
|
|
h, d = handler
|
|
n = h.companion_import_repeater_contacts("0x01")
|
|
assert n == len(SEEDS)
|
|
assert len(_imported(d)) == len(SEEDS)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"api_name,expected_pubkey",
|
|
[
|
|
("repeater", "aa" * 32),
|
|
("companion", "bb" * 32),
|
|
("room_server", "cc" * 32),
|
|
("sensor", "dd" * 32),
|
|
],
|
|
)
|
|
def test_each_contact_type_filter_matches_its_stored_display_name(
|
|
handler, api_name, expected_pubkey
|
|
):
|
|
"""The bug: every one of these returned 0."""
|
|
h, d = handler
|
|
n = h.companion_import_repeater_contacts("0x01", contact_types=[api_name])
|
|
assert n == 1, "contact_types=[%r] imported nothing" % api_name
|
|
assert set(_imported(d)) == {expected_pubkey}
|
|
|
|
|
|
def test_all_four_filters_together(handler):
|
|
h, d = handler
|
|
n = h.companion_import_repeater_contacts(
|
|
"0x01", contact_types=["companion", "repeater", "room_server", "sensor"]
|
|
)
|
|
assert n == len(SEEDS)
|
|
|
|
|
|
def test_adv_type_is_correct_for_every_stored_name(handler):
|
|
"""'Chat Node' must import as adv_type 1, not 0."""
|
|
h, d = handler
|
|
h.companion_import_repeater_contacts("0x01")
|
|
got = _imported(d)
|
|
for pk, _name, stored, expected in SEEDS:
|
|
assert got[pk] == expected, "%r imported as adv_type %s, expected %s" % (
|
|
stored,
|
|
got[pk],
|
|
expected,
|
|
)
|
|
assert 0 not in got.values(), "some contact imported with adv_type 0 (unset)"
|
|
|
|
|
|
def test_unknown_contact_type_still_imports_nothing(handler):
|
|
"""A type the API would reject must not silently widen the query."""
|
|
h, _ = handler
|
|
assert h.companion_import_repeater_contacts("0x01", contact_types=["nonsense"]) == 0
|