Skip to content

Commit 8f58dc8

Browse files
committed
updated tests
1 parent fdfca3e commit 8f58dc8

3 files changed

Lines changed: 60 additions & 3 deletions

File tree

‎hackrfpy/tests/test_features.py‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,30 @@ def empty_stream(*a, **k):
117117
assert "snapped to MHz" in "\n".join(r.message for r in caplog.records)
118118

119119

120+
def test_sweep_top_edge_ceiled(monkeypatch):
121+
# regression: both edges used to be FLOORED, so sweep(433.9e6, 434.1e6)
122+
# ran 433:434 and never covered 434.0-434.1 MHz. The high edge is now
123+
# ceiled so the swept range always contains the requested band.
124+
h = HackRF()
125+
seen = {}
126+
def rec_stream(argv, *a, **k):
127+
seen["argv"] = [str(x) for x in argv]
128+
if False:
129+
yield
130+
monkeypatch.setattr(h, "_run", rec_stream)
131+
list(h.sweep(433_900_000, 434_100_000))
132+
i = seen["argv"].index("-f")
133+
assert seen["argv"][i + 1] == "433:435"
134+
135+
136+
def test_from_device_unparsed_info_raises_typed(monkeypatch):
137+
# regression: this guard was a bare `assert`, stripped under python -O
138+
h = HackRF()
139+
monkeypatch.setattr(HackRF, "info", lambda self, **k: "raw text")
140+
with pytest.raises(HackRFDeviceError):
141+
HackRF.from_device(tools_dir=h.tools_dir)
142+
143+
120144
# ---- tx: max_duration converts an open-ended repeat into a timed run -------
121145
def test_tx_max_duration_forces_timed(monkeypatch, tmp_path):
122146
h = HackRF()

‎hackrfpy/tests/test_metadata.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,15 @@ def test_user_toml_overrides_builtin(tmp_path, monkeypatch):
7575
pre = P.load_presets()
7676
assert pre["ads-b"]["center"] == 1_100_000_000 # user wins
7777
assert pre["ads-b"]["desc"] == "overridden"
78+
79+
80+
# ---- package version attribute ---------------------------------------------
81+
def test_dunder_version_present():
82+
import hackrfpy
83+
v = hackrfpy.__version__
84+
assert isinstance(v, str) and v
85+
try:
86+
from importlib.metadata import version
87+
assert v == version("hackrfpy")
88+
except Exception:
89+
assert v.startswith("0.0.0") # uninstalled checkout fallback

‎hackrfpy/tests/test_monitor.py‎

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,12 @@ def test_monitor_maps_freqs_to_segments(stub_device):
3535
def test_monitor_tracks_power_change(stub_device):
3636
h = stub_device(sweep=dict(stdout_lines=_TWO_PASS))
3737
out = h.monitor_frequencies([433e6], span_hz=2e6)
38-
# 433 MHz segment rises from mean(-55,-50,-52,-58)=-53.75 to
39-
# mean(-45,-40,-42,-48)=-43.75 between passes
38+
# 433 MHz sits in bin idx 2 of the 431-435 segment (1 MHz bins). The
39+
# reading is the max of bins 1..3: pass 1 max(-50,-52,-58) = -50, pass 2
40+
# max(-40,-42,-48) = -40 -- the covering bin, not the segment mean, so a
41+
# narrowband carrier is no longer diluted by its quiet neighbors.
4042
assert out[0][433e6] < out[1][433e6] # power increased
41-
assert abs(out[1][433e6] - (-43.75)) < 0.1
43+
assert abs(out[1][433e6] - (-40.0)) < 0.1
4244

4345

4446
def test_monitor_callback_mode(stub_device):
@@ -59,3 +61,22 @@ def cb(u):
5961

6062
h.monitor_frequencies([100e6], on_update=cb)
6163
assert calls["n"] == 1
64+
65+
66+
# ---- regression: covering-bin power, not segment mean ----------------------
67+
# A strong narrowband carrier in one bin of a wide segment must dominate the
68+
# reading. The old segment-mean proxy diluted it toward the noise floor.
69+
_HOT_BIN = [
70+
"2026-06-15, 12:00:00.000000, 430000000, 440000000, 1000000.00, 4, "
71+
"-80, -80, -80, -20, -80, -80, -80, -80, -80, -80",
72+
"2026-06-15, 12:00:01.000000, 430000000, 440000000, 1000000.00, 4, "
73+
"-80, -80, -80, -20, -80, -80, -80, -80, -80, -80",
74+
]
75+
76+
77+
def test_monitor_reads_covering_bin_not_segment_mean(stub_device):
78+
h = stub_device(sweep=dict(stdout_lines=_HOT_BIN))
79+
out = h.monitor_frequencies([433.5e6], span_hz=1e6)
80+
# 433.5 MHz -> bin idx 3 (the -20 dB carrier). The segment mean would
81+
# have read -74; the covering bin reads the carrier itself.
82+
assert abs(out[0][433.5e6] - (-20.0)) < 0.1

0 commit comments

Comments
 (0)