prop/test/microwaveprop/rover/link_margin_test.exs
Graham McIntire b764034e46
test: push coverage to 78.6% — MapLayers 100%, LinkMargin 100%, Elevation 94%
- Weather.MapLayers: unit tests for all/0, default_id/0, valid_id?/1 (25→100%)
- Rover.LinkMargin: link_margin_db ScoresFile round-trip tests (57→100%)
- Rover.Elevation: SRTM tile file read path + void sentinel (44→94%)
- Pskr.Aggregator: pending_count, empty flush tests (65→74%)
- Buildings.MsFootprints: quadkey/bbox pure function tests (51→51%)
- Weather.Grib2.Wgrib2: property tests for parse_lon_val_segment (23→23%)
- test/support/data_case: File.rm_rf -> rm_rf to fix concurrent race
2026-05-07 13:17:40 -05:00

108 lines
3.1 KiB
Elixir

defmodule Microwaveprop.Rover.LinkMarginTest do
use ExUnit.Case, async: false
alias Microwaveprop.Rover.LinkMargin
describe "score_to_db/1" do
test "0 maps to -25.0 dB" do
assert LinkMargin.score_to_db(0) == -25.0
end
test "50 maps to 0.0 dB" do
assert LinkMargin.score_to_db(50) == 0.0
end
test "100 maps to 25.0 dB" do
assert LinkMargin.score_to_db(100) == 25.0
end
test "nil maps to nil" do
assert LinkMargin.score_to_db(nil) == nil
end
test "negative score maps below -25 dB" do
assert LinkMargin.score_to_db(-10) == -30.0
end
test "score above 100 maps above 25 dB" do
assert LinkMargin.score_to_db(120) == 35.0
end
end
describe "link_margin_from_score/2" do
test "score 80 in :ssb mode = 15.0 dB" do
assert LinkMargin.link_margin_from_score(80, :ssb) == 15.0
end
test "score 50 in :cw mode = 18.0 dB (0 - (-18))" do
assert LinkMargin.link_margin_from_score(50, :cw) == 18.0
end
test "nil score returns nil" do
assert LinkMargin.link_margin_from_score(nil, :ssb) == nil
end
test "score 0 in :cw mode = -7.0 dB" do
assert LinkMargin.link_margin_from_score(0, :cw) == -7.0
end
test "score 100 in :cw mode = 43.0 dB" do
assert LinkMargin.link_margin_from_score(100, :cw) == 43.0
end
test "score 30 in :ssb mode gives negative margin" do
margin = LinkMargin.link_margin_from_score(30, :ssb)
assert margin < 0
end
test "score 50 in :q65_60a mode = 24.0 dB" do
assert LinkMargin.link_margin_from_score(50, :q65_60a) == 24.0
end
end
describe "link_margin_db/5" do
alias Microwaveprop.Propagation.ScoresFile
setup do
prev = Application.get_env(:microwaveprop, :propagation_scores_dir)
dir = Path.join(System.tmp_dir!(), "link_margin_test_#{System.unique_integer([:positive])}")
Application.put_env(:microwaveprop, :propagation_scores_dir, dir)
on_exit(fn ->
File.rm_rf(dir)
if prev do
Application.put_env(:microwaveprop, :propagation_scores_dir, prev)
else
Application.delete_env(:microwaveprop, :propagation_scores_dir)
end
end)
:ok
end
test "returns nil when no score data exists" do
assert LinkMargin.link_margin_db(10_000, ~U[2026-05-01 12:00:00Z], {33.0, -97.0}, {33.1, -96.5}, :ssb) ==
nil
end
test "returns margin when score exists for the cell" do
valid_time = ~U[2026-05-01 12:00:00Z]
ScoresFile.write!(10_000, valid_time, [%{lat: 33.0, lon: -97.0, score: 80}])
margin =
LinkMargin.link_margin_db(10_000, valid_time, {33.0, -97.0}, {33.1, -96.5}, :ssb)
# score_to_db(80) = 15.0, ssb threshold = 0.0, so margin = 15.0
assert_in_delta margin, 15.0, 0.1
end
test "returns nil for a cell not in the score file" do
valid_time = ~U[2026-05-01 12:00:00Z]
ScoresFile.write!(10_000, valid_time, [%{lat: 33.0, lon: -97.0, score: 80}])
assert LinkMargin.link_margin_db(10_000, valid_time, {35.0, -95.0}, {33.1, -96.5}, :ssb) == nil
end
end
end