A third section, alongside the aircraft and the weather sensors, and for the same reason as both: a scan stops on a signal, records it and moves on, while APRS is a two-second transmission every few minutes from a hundred stations sharing one frequency. A sweep catches whichever happened to key up as it passed. `bandsaunter aprs` parks on the channel and catches all of them; `bandsaunter packets` reads a log back. Four layers, three of them new. The link layer was already here, opportunistically, in the generic decoder -- a correlator, NRZI, HDLC and a checksum, run on whatever a scan happened to record. It is now a receiver. What had to change is the state that survives a block boundary: the tail of the audio so the correlators see no edge, the phase of the sampling loop so a bit is not lost where one block meets the next, the tone the line was last at, and the bits themselves. A packet is most of a second and a block is about one, so frames straddling the boundary are not an edge case, they are most of them. Above that, the APRS information field, which is not one format but about twenty, chosen by the first character and accreted over thirty years. Positions uncompressed and compressed; Mic-E, which every Kenwood and Yaesu mobile sends and which hides the latitude inside the destination callsign because in 1995 those six bytes were carrying the word "APRS" and nothing else; weather with a position and without; messages, acknowledgements, rejections and bulletins; objects and items; status; telemetry; third-party traffic, credited to whoever originally sent it rather than to the gateway. Course and speed, altitude, power and antenna height, range and the precision extension, all of which ride in the comment. Every one has a writer beside its reader, so a packet goes in and the same packet comes out. Above that the section: a registry of who is out there and what each last said of each kind, distances and bearings from --at, a log keeping the whole frame under whatever was made of it, a spreadsheet, a map, and a channel full of stations that are not there for --simulate. One rule is worth naming because it is the difference between a decoder and a liar. A packet whose format does not match what its first character promised comes back as unparsed with its text intact. Thirteen characters of a *malformed* uncompressed position are perfectly good base-91, so trying one format and falling back to the other does not fail on a bad packet -- it succeeds, as a confident and completely different place, usually a thousand miles away. The specification makes the two unambiguous, a leading digit always meaning uncompressed, and the rule is read rather than guessed at. Two faults found by building it, both by measurement rather than by reading the code again. The framer handed back frames it had already reported, because it trimmed its buffer to before them rather than after -- every packet counted twice, for ever, which only shows up once the same signal is read across more than one block. And the invented channel truncated a transmission at the end of the block it began in rather than carrying the remainder over, which was invisible for as long as the simulated clock advanced in exact seconds and put every transmission at a block boundary; the moment it was paced against a real clock, nothing decoded at all. 204 new tests against seven deliberately broken builds, one of which survived until a test was written for the case it actually breaks. Full suite 2597 passed. Built as 2026-09-20_02. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016PsWPTweCT6pwxKngvVxcg
629 lines
24 KiB
Python
629 lines
24 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
|