Files
meshcore-bot/tests/unit/test_location_position_rows.py
agessaman 596a369204 refactor(location): one sender-position and one repeater-name query for every command
S6. The sender's latest position (airplanes, path, test) and the repeater-or-room-server lookup by name (prefix, solarforecast) were each pasted into the commands, alongside location.py's own copies for get_companion_lat_lon and lookup_repeater_lat_lon. They now run through location.latest_contact_position_rows and location.repeater_by_name_rows. The copies differed only in how they drop a zero coordinate, so that is a zero_rule parameter: "both" (location.py's helpers: drop only 0,0) or "either" (the commands: drop a fix with either coordinate zero), and each caller passes the rule it had. Each command keeps its private wrapper, its pubkey access, try/except, log text and value conversion; airplanes' _get_bot_location now calls get_bot_lat_lon, which is the same code.

I did not move the commands onto resolve_location: its labels and geocoding rules differ from what each command does today. Old-vs-new over 20,000 randomized cases against a real SQLite table (zero coordinates, ties, near-duplicate names, missing pubkeys, database errors, no db_manager) matched on results and log calls for all eight functions; four mutations were each caught.
2026-10-03 22:24:48 -07:00

59 lines
2.3 KiB
Python

"""The shared position queries behind the commands' sender and repeater-name lookups."""
import sqlite3
from types import SimpleNamespace
import pytest
from modules.location import latest_contact_position_rows, repeater_by_name_rows
class _DB:
def __init__(self, conn):
self.conn = conn
def execute_query(self, query, params=()):
self.conn.row_factory = sqlite3.Row
return [dict(row) for row in self.conn.execute(query, params).fetchall()]
@pytest.fixture
def bot():
conn = sqlite3.connect(":memory:")
conn.execute(
"CREATE TABLE complete_contact_tracking (public_key TEXT, name TEXT, role TEXT,"
" latitude REAL, longitude REAL, last_advert_timestamp TEXT, last_heard TEXT)"
)
rows = [
("pk1", "Equator", "companion", 0.0, 10.0, "2026-03-01", None),
("pk1", "Old", "companion", 47.0, -122.0, "2026-01-01", None),
("pk2", "Null Island", "companion", 0.0, 0.0, "2026-03-01", None),
("rp1", "Hill Alpha", "repeater", 1.0, 1.0, "2026-03-01", None),
("rp2", "Alpha Hill", "repeater", 2.0, 2.0, "2026-01-01", None),
("rp3", "alpha", "roomserver", 3.0, 3.0, "2025-01-01", None),
("rp4", "Alpha", "companion", 4.0, 4.0, "2026-04-01", None),
("rp5", "Prime", "repeater", 5.0, 0.0, "2026-04-01", None),
]
conn.executemany("INSERT INTO complete_contact_tracking VALUES (?,?,?,?,?,?,?)", rows)
return SimpleNamespace(db_manager=_DB(conn))
def test_latest_position_both_rule_keeps_a_single_zero_coordinate(bot):
assert latest_contact_position_rows(bot, "pk1") == [{"latitude": 0.0, "longitude": 10.0}]
assert latest_contact_position_rows(bot, "pk2") == []
def test_latest_position_either_rule_drops_a_single_zero_coordinate(bot):
assert latest_contact_position_rows(bot, "pk1", zero_rule="either") == [{"latitude": 47.0, "longitude": -122.0}]
def test_repeater_by_name_prefers_exact_then_prefix_then_substring(bot):
assert repeater_by_name_rows(bot, " ALPHA ")[0]["name"] == "alpha"
assert repeater_by_name_rows(bot, "alpha h")[0]["name"] == "Alpha Hill"
assert repeater_by_name_rows(bot, "lpha")[0]["name"] == "Hill Alpha"
def test_repeater_by_name_zero_rules(bot):
assert repeater_by_name_rows(bot, "Prime")[0]["name"] == "Prime"
assert repeater_by_name_rows(bot, "Prime", zero_rule="either") == []