The region was in the menu, in the receiver group, between the sample rate and the invented channel. That is the wrong place for it. It is the setting that decides whether anything is heard at all, and being on the wrong one sounds exactly like having no aerial, so it does not belong a level down among the things that make a working receiver work slightly better. It is now on the front page of the APRS menu, named as well as numbered, and so is the Listen line: "north-america 144.39 MHz" rather than "144.39 MHz", because the number alone does not say whether it is the right one and the name alone does not say what will be tuned. A channel that is no region's says so rather than claiming one. The region and the frequency are separate settings -- somebody may want a local packet network on neither -- which means they can be made to disagree. Every place that chooses a region now goes through one function, so they cannot. And there is a search. `bandsaunter aprs --find-channel`, or f in the menu, listens on each region's channel in turn and prints what was on each, then offers to use the busiest. This answers the one question about APRS that cannot be answered on any single frequency, because the answer *is* a frequency: somebody who has just plugged a dongle in cannot tell a wrong channel from a dead aerial, and that is worth a minute of listening rather than an evening of doubt. What it does not do is claim more than it found. A quiet channel is not proof of an empty one -- a fixed station beacons every half hour -- so what it finds is traffic, and when every channel comes back silent it says that this is not the same as an empty band and points at the aerial instead. The invented channel now honours tuning, and only carries its stations on its own frequency. Without that the search would pass on a simulated band having never searched anything, which is the kind of test that is worse than none. Sixteen new tests against six deliberately broken builds. The three that searched a whole simulated band were doing the same expensive thing three times over and now build their results directly, leaving one real end-to-end search; that file went from nine and a half minutes to two. Full suite 2612 passed. Built as 2026-09-20_03. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016PsWPTweCT6pwxKngvVxcg
818 lines
31 KiB
Python
818 lines
31 KiB
Python
"""The APRS section: the registry, the log, the report and the two front ends.
|
|
|
|
The radio is in test_ax25.py and the formats are in test_packets.py. What is
|
|
here is everything between a parsed packet and a person.
|
|
"""
|
|
import json
|
|
import time
|
|
|
|
import numpy as np
|
|
import pytest
|
|
from rich.console import Console
|
|
|
|
from bandsaunter import aprs as ap
|
|
from bandsaunter import aprslog as al
|
|
from bandsaunter import ax25, packets
|
|
from bandsaunter.ui import AprsDisplay
|
|
|
|
|
|
RATE = 240_000.0
|
|
|
|
|
|
def packet(info="=4903.50N/07201.75W>088/036 mobile", source="W1AW-9",
|
|
at=1_000.0, snr=20.0, path=(), destination="APRS"):
|
|
got = packets.parse_info(info, destination)
|
|
got.source, got.at, got.snr = source, at, snr
|
|
got.path, got.destination = tuple(path), destination
|
|
return got
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# The options
|
|
# ---------------------------------------------------------------------------
|
|
|
|
def test_every_option_names_a_field_that_exists():
|
|
fields = set(ap.AprsOptions().__dict__)
|
|
for option in ap.OPTIONS:
|
|
assert option.key in fields, f"{option.key} is not an option"
|
|
|
|
|
|
def test_every_field_is_an_option_somebody_can_reach():
|
|
assert set(ap.AprsOptions().__dict__) == {o.key for o in ap.OPTIONS}
|
|
|
|
|
|
def test_every_option_is_in_a_group_and_says_what_it_does():
|
|
for option in ap.OPTIONS:
|
|
assert option.group in ap.OPTION_GROUPS
|
|
assert option.help and option.detail and option.flags
|
|
if option.kind == "bool":
|
|
assert option.off_flags, f"{option.key} cannot be turned off"
|
|
for group in ap.OPTION_GROUPS:
|
|
assert ap.in_group(group)
|
|
|
|
|
|
def test_every_option_flag_is_one_the_parser_accepts():
|
|
from bandsaunter.cli import build_parser
|
|
|
|
known = set()
|
|
for action in build_parser()._subparsers._group_actions[0].choices[
|
|
"aprs"]._actions:
|
|
known.update(action.option_strings)
|
|
for option in ap.OPTIONS:
|
|
for flag in option.flags + option.off_flags:
|
|
assert flag in known, f"{flag} is in the table and not the parser"
|
|
|
|
|
|
def test_the_options_survive_being_saved_and_read_back(tmp_path):
|
|
options = ap.AprsOptions(units="imperial", region="europe", hold=90.0,
|
|
location="51.5,-0.1", digipeated=False)
|
|
ap.save_options(options, tmp_path)
|
|
assert ap.load_options(tmp_path) == options
|
|
|
|
|
|
def test_a_settings_file_with_a_mistake_in_it_costs_the_defaults(tmp_path):
|
|
(tmp_path / "aprs.yaml").write_text("units: [not a unit\n")
|
|
assert ap.load_options(tmp_path) == ap.AprsOptions()
|
|
|
|
|
|
@pytest.mark.parametrize("options,broken", [
|
|
(ap.AprsOptions(rate=48_000.0), "sample rate"),
|
|
(ap.AprsOptions(seconds=-1.0), "negative"),
|
|
(ap.AprsOptions(hold=0.0), "display"),
|
|
(ap.AprsOptions(frequency=10.0), "frequency"),
|
|
])
|
|
def test_a_setting_that_cannot_work_is_refused(options, broken):
|
|
assert any(broken in err for err in options.validate())
|
|
|
|
|
|
def test_the_defaults_are_all_workable():
|
|
assert ap.AprsOptions().validate() == []
|
|
|
|
|
|
@pytest.mark.parametrize("region,megahertz", [
|
|
("north-america", 144.390), ("europe", 144.800), ("australia", 145.175),
|
|
])
|
|
def test_each_region_has_its_own_channel(region, megahertz):
|
|
assert ap.channel_named(region) == pytest.approx(megahertz * 1e6)
|
|
|
|
|
|
def test_an_unknown_region_falls_back_rather_than_failing():
|
|
"""Refusing to listen over a misspelt region would be the wrong trade."""
|
|
assert ap.channel_named("narnia") == ax25.APRS_HZ
|
|
|
|
|
|
def test_the_region_sets_the_frequency_unless_one_was_given():
|
|
from bandsaunter.cli import build_parser
|
|
|
|
args = build_parser().parse_args(["aprs", "--region", "europe"])
|
|
assert args.region == "europe" and args.frequency is None
|
|
args = build_parser().parse_args(["aprs", "--region", "europe",
|
|
"--freq", "144900000"])
|
|
assert args.frequency == 144_900_000.0
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# How far off, and which way
|
|
# ---------------------------------------------------------------------------
|
|
|
|
def test_a_distance_is_the_great_circle_one():
|
|
london, paris = (51.5074, -0.1278), (48.8566, 2.3522)
|
|
assert ap.distance_km(london, paris) == pytest.approx(343.0, abs=5.0)
|
|
assert ap.bearing_deg(london, paris) == pytest.approx(148.0, abs=3.0)
|
|
|
|
|
|
def test_a_station_at_the_aerial_is_no_distance_away():
|
|
here = (47.55, -122.30)
|
|
assert ap.distance_km(here, here) == pytest.approx(0.0, abs=0.001)
|
|
|
|
|
|
def test_distances_are_only_worked_out_when_there_is_somewhere_to_measure_from():
|
|
station = ap.Station(call="W1AW")
|
|
station.add(packet())
|
|
assert station.away(None) is None
|
|
assert station.away((47.55, -122.30))[0] > 0
|
|
|
|
|
|
@pytest.mark.parametrize("text,ok", [
|
|
("47.55,-122.30", True), ("47.55, -122.30", True), ("", False),
|
|
("nowhere", False), ("91,0", False), ("0,181", False),
|
|
])
|
|
def test_a_location_that_is_not_one_is_refused(text, ok):
|
|
assert (ap.coordinates(text) is not None) is ok
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Who is out there
|
|
# ---------------------------------------------------------------------------
|
|
|
|
def test_a_station_keeps_the_latest_of_each_kind_and_not_the_latest_packet():
|
|
"""A station sends a position, then a status, then weather; a display
|
|
showing only the newest packet loses two of the three every time."""
|
|
net = ap.Net()
|
|
net.add(packet(at=1_000.0))
|
|
net.add(packet(">Monitoring 146.52", at=1_016.0))
|
|
station = net.stations["W1AW-9"]
|
|
assert station.position is not None # kept from the first
|
|
assert station.status == "Monitoring 146.52"
|
|
assert station.packets == 2
|
|
|
|
|
|
def test_a_moving_station_keeps_a_track_and_a_still_one_does_not_repeat_itself():
|
|
net = ap.Net()
|
|
for i in range(5):
|
|
net.add(packet(packets.position_report(49.0 + i * 0.01, -72.0, "/>"),
|
|
at=1_000.0 + i * 20))
|
|
assert len(net.stations["W1AW-9"].track) == 5
|
|
for i in range(5):
|
|
net.add(packet(packets.position_report(49.0, -72.0, "/-"),
|
|
source="KU0W", at=2_000.0 + i * 20))
|
|
assert len(net.stations["KU0W"].track) == 1
|
|
|
|
|
|
def test_a_track_cannot_grow_without_limit():
|
|
# A step of a thousandth of a degree, which is coarser than the
|
|
# hundredth of a minute the format resolves to -- a finer one would be
|
|
# rounded to the same place and correctly not recorded twice.
|
|
net = ap.Net()
|
|
for i in range(ap.TRACK_POINTS + 50):
|
|
net.add(packet(packets.position_report(49.0 + i * 0.001, -72.0),
|
|
at=1_000.0 + i))
|
|
assert len(net.stations["W1AW-9"].track) == ap.TRACK_POINTS
|
|
|
|
|
|
def test_a_station_that_has_not_moved_is_not_recorded_as_having_moved():
|
|
"""Two packets from the same place are one point, not two."""
|
|
net = ap.Net()
|
|
for i in range(6):
|
|
net.add(packet(packets.position_report(49.0, -72.0),
|
|
at=1_000.0 + i * 60))
|
|
assert len(net.stations["W1AW-9"].track) == 1
|
|
|
|
|
|
def test_a_packet_that_arrived_through_a_digipeater_is_counted_apart():
|
|
"""Most of what a receiver hears is a hilltop repeating somebody, which
|
|
is the whole design; what was heard directly is the honest measure of
|
|
what the aerial can reach."""
|
|
net = ap.Net()
|
|
net.add(packet(at=1_000.0, path=("WIDE1-1*", "WIDE2-1")))
|
|
net.add(packet(at=1_060.0, path=()))
|
|
station = net.stations["W1AW-9"]
|
|
assert station.packets == 2 and station.direct == 1
|
|
|
|
|
|
def test_an_object_is_filed_under_its_own_name_not_its_senders():
|
|
net = ap.Net()
|
|
net.add(packet(packets.object_report("LEADER", 49.0, -72.0),
|
|
source="W1AW-9", at=1_000.0))
|
|
assert "LEADER" in net.stations and "W1AW-9" not in net.stations
|
|
assert net.stations["LEADER"].object_of == "W1AW-9"
|
|
|
|
|
|
def test_messages_are_kept_in_order_as_a_conversation():
|
|
net = ap.Net()
|
|
net.add(packet(packets.message_text("KU0W", "first"), at=1_000.0))
|
|
net.add(packet(packets.message_text("W1AW", "second"), source="KU0W",
|
|
at=1_060.0))
|
|
assert [m.message.text for m in net.messages] == ["first", "second"]
|
|
|
|
|
|
def test_a_station_that_has_gone_quiet_leaves_the_display_but_not_the_log():
|
|
net = ap.Net()
|
|
net.add(packet(at=1_000.0))
|
|
assert len(net.showing(300.0, now=1_200.0)) == 1
|
|
assert len(net.showing(300.0, now=1_400.0)) == 0
|
|
assert len(net.all()) == 1
|
|
|
|
|
|
def test_the_newest_station_is_at_the_top():
|
|
"""The opposite of the other displays, and deliberately: what somebody
|
|
watching a busy channel wants to know is what just came in."""
|
|
net = ap.Net()
|
|
net.add(packet(source="OLD", at=1_000.0))
|
|
net.add(packet(source="NEW", at=2_000.0))
|
|
assert [s.call for s in net.showing(1e6, now=2_000.0)] == ["NEW", "OLD"]
|
|
|
|
|
|
def test_a_net_can_be_rebuilt_from_a_log():
|
|
heard = [packet(at=1_000.0), packet(source="KU0W", at=1_001.0)]
|
|
assert {s.call for s in ap.Net.of(heard).all()} == {"W1AW-9", "KU0W"}
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# The log
|
|
# ---------------------------------------------------------------------------
|
|
|
|
def test_a_packet_survives_being_written_and_read_back(tmp_path):
|
|
log = al.AprsLog(tmp_path / "aprs_x.jsonl", frequency=ax25.APRS_HZ)
|
|
sent = packet(path=("WIDE1-1*",))
|
|
log.append(sent, ax25.frame_from(ax25.frame_bytes("W1AW-9", "APRS",
|
|
sent.info)))
|
|
log.close()
|
|
back = al.read_logs([log.path])
|
|
assert len(back) == 1
|
|
got = back[0]
|
|
assert got.source == "W1AW-9" and got.kind == "position"
|
|
assert got.position.latitude == pytest.approx(49.0583, abs=0.0002)
|
|
assert got.course == 88 and got.path == ("WIDE1-1*",)
|
|
assert got.snr == pytest.approx(20.0)
|
|
|
|
|
|
def test_the_whole_frame_goes_down_underneath_whatever_was_understood(tmp_path):
|
|
"""APRS has a long tail of formats, so a packet this version cannot read
|
|
is kept in full for a version that can."""
|
|
log = al.AprsLog(tmp_path / "aprs_x.jsonl")
|
|
raw = ax25.frame_bytes("W1AW", "APRS", "!!! not a format !!!")
|
|
frame = ax25.frame_from(raw)
|
|
log.append(packets.parse(frame), frame)
|
|
log.close()
|
|
body = json.loads(log.path.read_text().splitlines()[1])
|
|
assert bytes.fromhex(body["hex"]) == raw
|
|
assert body["kind"] == "unparsed"
|
|
assert body["info"] == "!!! not a format !!!"
|
|
|
|
|
|
def test_weather_and_messages_survive_the_log(tmp_path):
|
|
log = al.AprsLog(tmp_path / "aprs_x.jsonl")
|
|
log.append(packet("@092345z4903.50N/07201.75W_220/004g005t077h50b09900",
|
|
source="KB1XYZ"))
|
|
log.append(packet(packets.message_text("KU0W", "on my way", "7")))
|
|
log.close()
|
|
back = al.read_logs([log.path])
|
|
assert back[0].weather["temperature"].value == pytest.approx(25.0, abs=0.1)
|
|
assert back[1].message.to == "KU0W" and back[1].message.number == "7"
|
|
|
|
|
|
def test_a_half_written_last_line_costs_that_line_and_not_the_evening(tmp_path):
|
|
log = al.AprsLog(tmp_path / "aprs_x.jsonl")
|
|
for i in range(5):
|
|
log.append(packet(at=1_000.0 + i))
|
|
log.close()
|
|
with open(log.path, "a") as fh:
|
|
fh.write('{"t": 9, "src": "W1A')
|
|
assert len(al.read_logs([log.path])) == 5
|
|
|
|
|
|
def test_the_header_line_is_not_read_back_as_a_packet(tmp_path):
|
|
log = al.AprsLog(tmp_path / "aprs_x.jsonl", receiver="device 0")
|
|
log.close()
|
|
head = json.loads(log.path.read_text().splitlines()[0])
|
|
assert head["log"] == "bandsaunter-aprs"
|
|
assert al.read_logs([log.path]) == []
|
|
|
|
|
|
def test_a_log_is_written_as_it_goes_and_not_when_it_closes(tmp_path):
|
|
log = al.AprsLog(tmp_path / "aprs_x.jsonl")
|
|
log.append(packet())
|
|
assert len(log.path.read_text().splitlines()) == 2
|
|
log.close()
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Out to a spreadsheet and a globe
|
|
# ---------------------------------------------------------------------------
|
|
|
|
def test_the_spreadsheet_has_a_column_for_everything_anything_reported(tmp_path):
|
|
heard = [packet(at=1_000.0),
|
|
packet("@092345z4903.50N/07201.75W_220/004g005t077h50b09900",
|
|
source="KB1XYZ", at=1_001.0)]
|
|
where = al.write_csv(tmp_path / "a.csv", heard)
|
|
head = where.read_text().splitlines()[0].split(",")
|
|
assert "latitude" in head and "speed (km/h)" in head
|
|
assert any(h.startswith("temperature") for h in head)
|
|
|
|
|
|
def test_the_spreadsheet_can_be_written_the_other_way_round(tmp_path):
|
|
heard = [packet("@092345z4903.50N/07201.75W_220/004t032", source="KB1XYZ",
|
|
at=1_000.0)]
|
|
where = al.write_csv(tmp_path / "a.csv", heard, imperial=True)
|
|
head, row = where.read_text().splitlines()[:2]
|
|
columns = head.split(",")
|
|
assert "temperature (F)" in columns
|
|
assert row.split(",")[columns.index("temperature (F)")] == "32.0"
|
|
|
|
|
|
def test_a_map_has_a_pin_for_every_station_and_a_line_for_what_moved(tmp_path):
|
|
heard = [packet(packets.position_report(49.0 + i * 0.01, -72.0, "/>"),
|
|
at=1_000.0 + i * 20) for i in range(4)]
|
|
heard.append(packet(packets.position_report(50.0, -73.0), source="KU0W",
|
|
at=1_100.0))
|
|
where = al.write_kml(tmp_path / "a.kml", heard)
|
|
text = where.read_text()
|
|
assert text.count("<Placemark>") == 3 # a track and two pins
|
|
assert "<LineString>" in text and "W1AW-9" in text and "KU0W" in text
|
|
|
|
|
|
def test_a_map_of_stations_that_never_said_where_they_were_is_not_drawn(tmp_path):
|
|
"""A pin at nowhere puts a station off the west coast of Africa."""
|
|
heard = [packet(">Monitoring 146.52", at=1_000.0)]
|
|
assert al.write_kml(tmp_path / "a.kml", heard) is None
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Listening
|
|
# ---------------------------------------------------------------------------
|
|
|
|
class Silence:
|
|
"""A receiver that hears nothing, for testing the loop rather than the air."""
|
|
|
|
def __init__(self, blocks=2):
|
|
self.blocks = blocks
|
|
self.tuned = 0.0
|
|
|
|
def tune(self, hz, settle=True):
|
|
self.tuned = hz
|
|
return int(hz)
|
|
|
|
def read_samples(self, count, flush=False):
|
|
if self.blocks <= 0:
|
|
return np.zeros(0, dtype=np.complex64)
|
|
self.blocks -= 1
|
|
return np.zeros(count, dtype=np.complex64)
|
|
|
|
def close(self):
|
|
return None
|
|
|
|
|
|
class Channel:
|
|
"""The invented channel, wrapped so a test can drive it a block at a time."""
|
|
|
|
def __init__(self, blocks, rate=RATE, seed=3):
|
|
self.sky = ap.SimulatedChannel(sample_rate=rate, seed=seed)
|
|
self.left = blocks
|
|
|
|
def tune(self, hz, settle=True):
|
|
return int(hz)
|
|
|
|
def read_samples(self, count, flush=False):
|
|
if self.left <= 0:
|
|
return np.zeros(0, dtype=np.complex64)
|
|
self.left -= 1
|
|
return self.sky.read_samples(count)
|
|
|
|
def close(self):
|
|
return None
|
|
|
|
|
|
def test_the_receiver_is_tuned_to_the_channel():
|
|
device = Silence()
|
|
ap.pump(device, ap.AprsOptions(rate=RATE, region="europe",
|
|
frequency=ap.channel_named("europe")),
|
|
ap.Net(), None, time.time())
|
|
assert device.tuned == pytest.approx(144_800_000.0)
|
|
|
|
|
|
def test_listening_stops_when_the_receiver_does():
|
|
device = Silence(blocks=3)
|
|
assert ap.pump(device, ap.AprsOptions(rate=RATE), ap.Net(), None,
|
|
time.time()) == 0
|
|
assert device.blocks == 0
|
|
|
|
|
|
def test_a_whole_session_hears_the_invented_channel(tmp_path, monkeypatch):
|
|
monkeypatch.setattr(ap, "open_device",
|
|
lambda console, opts: Channel(120))
|
|
options = ap.AprsOptions(rate=RATE, simulate=True, packets_seen=True,
|
|
csv=True, kml=True, report=False)
|
|
console = Console(width=120, force_terminal=False)
|
|
with console.capture():
|
|
heard = ap.listen(console, options, str(tmp_path))
|
|
assert heard.stations >= 4
|
|
assert heard.log_path.exists() and heard.csv_path.exists()
|
|
assert heard.kml_path.exists()
|
|
assert len(al.read_logs([heard.log_path])) == heard.packets
|
|
kinds = {s.kind for s in heard.net.all()}
|
|
assert {"position", "weather", "object"} <= kinds
|
|
assert heard.net.messages
|
|
|
|
|
|
def test_a_transmission_that_straddles_a_block_is_still_heard():
|
|
"""A packet is most of a second and a block is about one, so this is not
|
|
an edge case -- it is most of them. A simulated clock advancing in exact
|
|
seconds hides it by putting every transmission at a block boundary.
|
|
"""
|
|
sky = ap.SimulatedChannel(sample_rate=RATE, seed=3)
|
|
demod, receiver = ap.make_receiver(ap.AprsOptions(rate=RATE))
|
|
found = 0
|
|
for _ in range(50):
|
|
# Blocks of an awkward length, so nothing lines up.
|
|
audio, _iq = demod.step(sky.read_samples(int(RATE * 0.77)))
|
|
found += len(receiver.feed(audio))
|
|
assert found >= 3, "transmissions are being cut off at the block boundary"
|
|
|
|
|
|
def test_relayed_packets_can_be_left_out(tmp_path, monkeypatch):
|
|
monkeypatch.setattr(ap, "open_device", lambda console, opts: Channel(120))
|
|
console = Console(width=120, force_terminal=False)
|
|
options = ap.AprsOptions(rate=RATE, packets_seen=True, log=False,
|
|
report=False, digipeated=False)
|
|
with console.capture():
|
|
heard = ap.listen(console, options, str(tmp_path))
|
|
for station in heard.net.all():
|
|
assert station.direct == station.packets
|
|
|
|
|
|
def test_a_receiver_that_will_not_open_is_a_message_and_not_a_traceback(
|
|
tmp_path, monkeypatch):
|
|
monkeypatch.setattr(ap, "open_device", lambda console, opts: None)
|
|
console = Console(width=120, force_terminal=False)
|
|
with console.capture():
|
|
heard = ap.listen(console, ap.AprsOptions(), str(tmp_path))
|
|
assert heard.stations == 0 and heard.log_path is None
|
|
|
|
|
|
def test_hearing_nothing_says_what_to_check(tmp_path, monkeypatch):
|
|
monkeypatch.setattr(ap, "open_device", lambda console, opts: Silence(2))
|
|
console = Console(width=120, force_terminal=False)
|
|
with console.capture() as cap:
|
|
ap.listen(console, ap.AprsOptions(rate=RATE, packets_seen=True,
|
|
log=False), str(tmp_path))
|
|
assert "region" in cap.get()
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# The report and the display
|
|
# ---------------------------------------------------------------------------
|
|
|
|
def rendered(net, options=None):
|
|
console = Console(width=130, force_terminal=False)
|
|
with console.capture() as cap:
|
|
ap.report(console, net, options or ap.AprsOptions())
|
|
return cap.get()
|
|
|
|
|
|
def test_the_report_says_who_was_heard_and_what_they_said():
|
|
net = ap.Net()
|
|
net.add(packet(at=1_000.0))
|
|
net.add(packet("@092345z4903.50N/07201.75W_220/004g005t077h50b09900",
|
|
source="KB1XYZ", at=1_001.0))
|
|
net.add(packet(packets.message_text("KU0W", "on my way"), at=1_002.0))
|
|
out = rendered(net)
|
|
assert "W1AW-9" in out and "KB1XYZ" in out
|
|
assert "stations heard" in out and "what they said" in out
|
|
assert "what passed between them" in out
|
|
assert "on my way" in out
|
|
|
|
|
|
def test_the_report_shows_distances_only_when_it_knows_where_the_aerial_is():
|
|
net = ap.Net()
|
|
net.add(packet(at=1_000.0))
|
|
assert "away" not in rendered(net)
|
|
assert "away" in rendered(net, ap.AprsOptions(location="47.55,-122.30"))
|
|
|
|
|
|
def test_the_report_counts_what_it_could_not_read():
|
|
net = ap.Net()
|
|
net.add(packet("!!! not a format !!!", at=1_000.0))
|
|
assert "cannot read" in rendered(net)
|
|
|
|
|
|
@pytest.mark.parametrize("level,colour", [
|
|
(4.0, "red"), (12.0, "yellow"), (24.0, "green"),
|
|
])
|
|
def test_the_signal_is_coloured_so_an_aerial_can_be_aimed_by_it(level, colour):
|
|
assert colour in ap.signal_text(level)
|
|
|
|
|
|
def test_no_signal_is_shown_as_nothing_rather_than_as_zero():
|
|
assert ap.signal_text(0.0) == ""
|
|
|
|
|
|
def shown(net, width=120, now=None, **kw):
|
|
console = Console(width=width, force_terminal=False)
|
|
display = AprsDisplay(console, **kw)
|
|
display.started = (now or time.time()) - 60
|
|
display.update(net, net.packets)
|
|
with console.capture() as cap:
|
|
console.print(display.render(now=now))
|
|
return cap.get()
|
|
|
|
|
|
def test_the_display_shows_a_station_and_what_it_last_said():
|
|
net = ap.Net()
|
|
now = time.time()
|
|
net.add(packet(at=now))
|
|
out = shown(net, now=now)
|
|
assert "W1AW-9" in out and "49.0583N" in out and "144.39 MHz" in out
|
|
|
|
|
|
def test_the_display_still_fits_a_narrow_terminal():
|
|
net = ap.Net()
|
|
now = time.time()
|
|
net.add(packet(at=now))
|
|
for width in (60, 70, 86, 100, 140):
|
|
out = shown(net, width=width, now=now, home=(47.55, -122.30))
|
|
assert max(len(line) for line in out.splitlines()) <= width
|
|
assert "W1AW-9" in out
|
|
|
|
|
|
def test_an_empty_display_says_what_to_check():
|
|
assert "region" in shown(ap.Net())
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# The command line and the menu
|
|
# ---------------------------------------------------------------------------
|
|
|
|
def test_reading_a_log_back_prints_it_and_writes_the_files(tmp_path,
|
|
monkeypatch):
|
|
from bandsaunter.cli import build_parser, cmd_packets
|
|
import bandsaunter.cli as cli
|
|
|
|
log = tmp_path / "aprs_x.jsonl"
|
|
with al.AprsLog(log) as fh:
|
|
for i in range(4):
|
|
fh.append(packet(packets.position_report(49.0 + i * 0.01, -72.0),
|
|
at=1_000.0 + i * 20))
|
|
console = Console(width=130, force_terminal=False)
|
|
monkeypatch.setattr(cli, "console", console)
|
|
args = build_parser().parse_args(["packets", str(log), "--csv", "--kml"])
|
|
with console.capture() as cap:
|
|
assert cmd_packets(args) == 0
|
|
assert "W1AW-9" in cap.get()
|
|
assert log.with_suffix(".csv").exists() and log.with_suffix(".kml").exists()
|
|
|
|
|
|
def test_reading_a_log_back_can_be_narrowed_to_one_station(tmp_path,
|
|
monkeypatch):
|
|
from bandsaunter.cli import build_parser, cmd_packets
|
|
import bandsaunter.cli as cli
|
|
|
|
log = tmp_path / "aprs_x.jsonl"
|
|
with al.AprsLog(log) as fh:
|
|
fh.append(packet(at=1_000.0))
|
|
fh.append(packet(source="KU0W", at=1_001.0))
|
|
console = Console(width=130, force_terminal=False)
|
|
monkeypatch.setattr(cli, "console", console)
|
|
args = build_parser().parse_args(["packets", str(log), "--station", "KU0W"])
|
|
with console.capture() as cap:
|
|
assert cmd_packets(args) == 0
|
|
out = cap.get()
|
|
assert "KU0W" in out and "W1AW-9" not in out
|
|
|
|
|
|
def test_asking_for_a_log_that_is_not_there_is_a_message(tmp_path,
|
|
monkeypatch):
|
|
from bandsaunter.cli import build_parser, cmd_packets
|
|
import bandsaunter.cli as cli
|
|
|
|
console = Console(width=120, force_terminal=False)
|
|
monkeypatch.setattr(cli, "console", console)
|
|
args = build_parser().parse_args(["packets", str(tmp_path / "no.jsonl")])
|
|
with console.capture() as cap:
|
|
assert cmd_packets(args) == 1
|
|
assert "no such file" in cap.get()
|
|
|
|
|
|
def test_the_menu_can_be_opened_and_left(monkeypatch):
|
|
import bandsaunter.tui as tui
|
|
from bandsaunter.config import ScanConfig
|
|
|
|
answers = iter(["b"])
|
|
monkeypatch.setattr(tui, "_ask",
|
|
lambda console, prompt, default="": next(answers))
|
|
console = Console(width=120, force_terminal=False)
|
|
with console.capture() as cap:
|
|
tui.aprs_menu(console, ScanConfig())
|
|
out = cap.get()
|
|
assert "144.390 MHz" in out
|
|
for group in ap.OPTION_GROUPS:
|
|
assert group.lower() in out
|
|
|
|
|
|
def test_one_set_of_option_screens_drives_all_three_sections():
|
|
import bandsaunter.tui as tui
|
|
from bandsaunter import aircraft, weather
|
|
|
|
for module in (aircraft, weather, ap):
|
|
assert tui._section(module) is module
|
|
assert callable(module.defaults)
|
|
assert module.OPTION_GROUPS and module.OPTIONS
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Finding the channel
|
|
# ---------------------------------------------------------------------------
|
|
|
|
def test_the_channel_is_named_as_well_as_numbered():
|
|
"""The number alone does not say whether it is the right one, and the
|
|
name alone does not say what will be tuned."""
|
|
options = ap.AprsOptions()
|
|
assert ap.channel_text(options) == "north-america 144.39 MHz"
|
|
ap.use_region(options, "europe")
|
|
assert ap.channel_text(options) == "europe 144.8 MHz"
|
|
|
|
|
|
def test_a_channel_that_is_no_regions_says_so_rather_than_claiming_one():
|
|
options = ap.AprsOptions(frequency=144_500_000.0)
|
|
assert "not a region" in ap.channel_text(options)
|
|
|
|
|
|
def test_choosing_a_region_moves_the_frequency_with_it():
|
|
"""They are separate settings, so they can be made to disagree; every
|
|
place that chooses a region goes through one function so they cannot."""
|
|
options = ap.use_region(ap.AprsOptions(), "australia")
|
|
assert options.region == "australia"
|
|
assert options.frequency == pytest.approx(145_175_000.0)
|
|
|
|
|
|
def test_the_menu_shows_the_channel_without_going_a_level_down():
|
|
"""It is the setting that decides whether anything is heard at all."""
|
|
assert "north-america 144.39 MHz" in ap.describe(ap.AprsOptions())
|
|
|
|
|
|
def test_the_invented_band_only_carries_traffic_on_its_own_channel():
|
|
"""A simulated band that answered the same everywhere would let the
|
|
channel search pass without ever having searched anything."""
|
|
def listened(hz, blocks=40):
|
|
sky = ap.SimulatedChannel(sample_rate=RATE, seed=3)
|
|
demod, receiver = ap.make_receiver(ap.AprsOptions(rate=RATE))
|
|
sky.tune(hz)
|
|
return sum(len(receiver.feed(demod.step(sky.read_samples(int(RATE)))[0]))
|
|
for _ in range(blocks))
|
|
|
|
assert listened(ap.channel_named("europe")) == 0
|
|
assert listened(ax25.APRS_HZ) >= 2
|
|
|
|
|
|
def test_the_search_finds_the_channel_that_has_traffic_on_it():
|
|
"""The one end-to-end search: every channel really listened to.
|
|
|
|
The rest of these build the results directly, because six listens of a
|
|
simulated band is the most expensive thing in this file and searching it
|
|
twice proves nothing the first search did not.
|
|
"""
|
|
console = Console(width=120, force_terminal=False)
|
|
device = ap.SimulatedChannel(sample_rate=RATE, seed=3)
|
|
with console.capture():
|
|
found = ap.find_channel(console, ap.AprsOptions(rate=RATE),
|
|
seconds=16.0, device=device)
|
|
assert len(found) == len(ax25.APRS_CHANNELS)
|
|
busy = [one for one in found if one.busy]
|
|
assert [one.region for one in busy] == ["north-america"]
|
|
assert busy[0].stations >= 1 and busy[0].best > 0
|
|
|
|
|
|
def searched(busiest="north-america", packets=21, stations=6):
|
|
"""What a search came to, without having to run one."""
|
|
return [ap.Found(region=r, frequency=hz, where=w,
|
|
packets=packets if r == busiest else 0,
|
|
stations=stations if r == busiest else 0,
|
|
best=38.0 if r == busiest else 0.0)
|
|
for r, hz, w in ax25.APRS_CHANNELS]
|
|
|
|
|
|
def test_the_search_reports_which_channel_to_use():
|
|
console = Console(width=120, force_terminal=False)
|
|
with console.capture() as cap:
|
|
best = ap.report_channels(console, searched(), 30.0)
|
|
out = cap.get()
|
|
assert best is not None and best.region == "north-america"
|
|
assert "had the most on it" in out
|
|
for region, _hz, _where in ax25.APRS_CHANNELS:
|
|
assert region in out # every channel accounted for
|
|
|
|
|
|
def test_the_busiest_channel_is_the_one_recommended():
|
|
console = Console(width=120, force_terminal=False)
|
|
with console.capture():
|
|
best = ap.report_channels(console, searched(busiest="australia"), 30.0)
|
|
assert best.region == "australia"
|
|
|
|
|
|
def test_a_silent_band_is_reported_as_silent_and_not_as_an_answer():
|
|
"""A quiet channel is not proof of an empty one: a fixed station beacons
|
|
every half hour, so what this finds is traffic and what it misses is only
|
|
the absence of traffic while it listened."""
|
|
console = Console(width=120, force_terminal=False)
|
|
found = [ap.Found(region=r, frequency=hz, where=w)
|
|
for r, hz, w in ax25.APRS_CHANNELS]
|
|
with console.capture() as cap:
|
|
assert ap.report_channels(console, found, 20.0) is None
|
|
out = cap.get()
|
|
assert "not proof of an empty band" in out
|
|
assert "49 cm" in out # and what to check instead
|
|
|
|
|
|
def test_the_search_leaves_the_log_and_the_files_alone():
|
|
"""It is a measurement, not a session: six short listens should not
|
|
leave six logs and six maps behind."""
|
|
from dataclasses import replace
|
|
|
|
options = ap.AprsOptions(rate=RATE, log=True, csv=True, kml=True)
|
|
trial = replace(options, log=False, report=False, csv=False, kml=False)
|
|
assert not (trial.log or trial.csv or trial.kml or trial.report)
|
|
|
|
|
|
def test_the_search_is_reachable_from_the_command_line():
|
|
from bandsaunter.cli import build_parser
|
|
|
|
args = build_parser().parse_args(["aprs", "--find-channel"])
|
|
assert args.find_channel == 20.0 # a default, not a flag
|
|
args = build_parser().parse_args(["aprs", "--find-channel", "5"])
|
|
assert args.find_channel == 5.0
|
|
assert build_parser().parse_args(["aprs"]).find_channel is None
|
|
|
|
|
|
def test_the_search_runs_from_the_command_line_and_says_what_to_do(monkeypatch):
|
|
from bandsaunter.cli import build_parser, cmd_aprs
|
|
import bandsaunter.cli as cli
|
|
|
|
monkeypatch.setattr(ap, "load_options",
|
|
lambda *a, **kw: ap.AprsOptions(rate=RATE))
|
|
monkeypatch.setattr(ap, "find_channel",
|
|
lambda console, options, seconds: searched())
|
|
console = Console(width=130, force_terminal=False)
|
|
monkeypatch.setattr(cli, "console", console)
|
|
args = build_parser().parse_args(["aprs", "--find-channel", "30"])
|
|
with console.capture() as cap:
|
|
assert cmd_aprs(args) == 0
|
|
out = cap.get()
|
|
assert "north-america had the most on it" in out
|
|
assert "--region north-america" in out
|
|
|
|
|
|
def test_hearing_nothing_points_at_the_channel_search(tmp_path, monkeypatch):
|
|
from bandsaunter.cli import build_parser, cmd_aprs
|
|
import bandsaunter.cli as cli
|
|
|
|
monkeypatch.setattr(ap, "load_options",
|
|
lambda *a, **kw: ap.AprsOptions(rate=RATE, log=False,
|
|
packets_seen=True))
|
|
monkeypatch.setattr(ap, "open_device", lambda console, opts: Silence(2))
|
|
console = Console(width=130, force_terminal=False)
|
|
monkeypatch.setattr(cli, "console", console)
|
|
with console.capture() as cap:
|
|
assert cmd_aprs(build_parser().parse_args(["aprs"])) == 1
|
|
assert "--find-channel" in cap.get()
|
|
|
|
|
|
def test_the_channel_can_be_chosen_from_the_menu(monkeypatch):
|
|
import bandsaunter.tui as tui
|
|
|
|
answers = iter(["c", "2", "b"])
|
|
monkeypatch.setattr(tui, "_ask",
|
|
lambda console, prompt, default="": next(answers))
|
|
console = Console(width=120, force_terminal=False)
|
|
with console.capture() as cap:
|
|
tui.aprs_menu(console, __import__("bandsaunter.config",
|
|
fromlist=["x"]).ScanConfig())
|
|
out = cap.get()
|
|
assert "Channel / region" in out
|
|
assert "Find the channel" in out
|
|
assert "europe 144.8 MHz" in out # the menu adopted the choice
|
|
for region, _hz, _where in ax25.APRS_CHANNELS:
|
|
assert region in out
|
|
|
|
|
|
def test_a_frequency_can_be_typed_straight_into_the_channel_screen(monkeypatch):
|
|
import bandsaunter.tui as tui
|
|
|
|
answers = iter(["144.5", ""])
|
|
monkeypatch.setattr(tui, "_ask",
|
|
lambda console, prompt, default="": next(answers))
|
|
console = Console(width=120, force_terminal=False)
|
|
options = ap.AprsOptions()
|
|
with console.capture() as cap:
|
|
tui._aprs_channel(console, options)
|
|
assert options.frequency == pytest.approx(144_500_000.0)
|
|
assert "not a region" in cap.get()
|