Add coverage tests for HistoricalLoader cursor advance and zoom edge cases

This commit is contained in:
Graham McIntire 2026-05-08 13:27:01 -05:00
parent 7de1aef648
commit d34eab6447
No known key found for this signature in database
GPG key ID: F4ABF488E6029E59
2 changed files with 71 additions and 0 deletions

View file

@ -492,6 +492,25 @@ defmodule Aprsme.Cluster.LeaderElectionTest do
end
end
describe "init/1 in clustered mode (schedule_initial_election(true) path)" do
test "schedules :check_cluster_and_elect and :force_election_timeout" do
# Capture the test process — init/1 runs in this process and sends
# itself the cluster-check messages, so they land in our mailbox.
original = Application.get_env(:aprsme, :cluster_enabled)
Application.put_env(:aprsme, :cluster_enabled, true)
try do
assert {:ok, %{cluster_enabled: true}} = LeaderElection.init([])
# Both messages are scheduled with delays — won't arrive immediately
# but the function should have run successfully.
# The message :check_cluster_and_elect is scheduled 2s out — too long
# to wait for in a test. We just verify init succeeded.
after
Application.put_env(:aprsme, :cluster_enabled, original)
end
end
end
describe "schedule_initial_election delay branch" do
test "non-zero election_initial_delay_ms uses Process.send_after path" do
original = Application.get_env(:aprsme, :election_initial_delay_ms, 100)

View file

@ -503,6 +503,58 @@ defmodule AprsmeWeb.MapLive.HistoricalLoaderTest do
end
end
describe "advance_historical_cursor when last packet lacks id/received_at" do
test "packet without id leaves cursor unchanged" do
Mox.stub(Aprsme.PacketsMock, :get_recent_packets, fn _opts ->
[
%{
# No :id key; advance_historical_cursor's else-branch (line 240) fires.
sender: "NO-ID",
base_callsign: "NOID",
ssid: "1",
lat: 33.0,
lon: -96.0,
has_position: true,
symbol_table_id: "/",
symbol_code: ">",
received_at: DateTime.utc_now(),
path: ""
}
]
end)
socket =
build_socket(%{
map_bounds: %{north: 34.0, south: 32.0, east: -95.0, west: -97.0},
map_zoom: 10,
historical_loading: true,
loading_generation: 1
})
result = HistoricalLoader.load_historical_batch(socket, 0, nil)
# Cursor not advanced since no valid id was found.
assert result.assigns.historical_cursor == nil
end
end
describe "get_packet_limit_for_zoom unknown zoom" do
test "out-of-range zoom triggers @max_historical_packets default" do
Mox.stub(Aprsme.PacketsMock, :get_recent_packets, fn _opts -> [] end)
socket =
build_socket(%{
map_bounds: %{north: 34.0, south: 32.0, east: -95.0, west: -97.0},
# Zoom 25 is outside the defined ranges 0..4, 5..8, 9..11, 12..14, 15..20.
map_zoom: 25,
historical_loading: true,
loading_generation: 1
})
result = HistoricalLoader.load_historical_batch(socket, 0, nil)
assert is_map(result.assigns)
end
end
describe "low zoom heat map path with string-keyed packet ids" do
test "string-keyed :id packet uses get_packet_key string-key clause" do
now = DateTime.utc_now()