prop/test/microwaveprop/weather/nexrad_cache_test.exs
Graham McIntire 250709a1b2 Add more caching to make the map feel instant
- ScoreCache stores {band, valid_time} as %{{lat, lon} => score} map so
  point lookups are O(1); adds fetch_point/4 and valid_times/1
- available_valid_times/1 reads directly from ScoreCache when warm,
  falls back to DB on cold start
- point_forecast/3 iterates cached valid_times and uses fetch_point/4
  instead of hitting the DB per click
- NexradCache: node-local ETS cache of decoded n0q PNG pixel buffers
  keyed by 5-minute rounded timestamp; skips ~1-5s HTTP+decode on
  concurrent/repeat clicks within the same window
- MapLive: start_async the rain_scatter fetch so point_detail renders
  immediately with a pending marker; push rain_scatter_update when
  NEXRAD resolves
- MapLive: preload all 18 remaining forecast hours for the current
  viewport after mount/band change/propagation_updated; client caches
  them and renders timeline scrubs instantly without a server roundtrip.
  Adds set_selected_time event for fast-path state sync.
- Propagation map JS: forecastCache map + drawScatterMarkers helper,
  timeline click uses preloaded cache when available
2026-04-12 12:26:25 -05:00

47 lines
1.5 KiB
Elixir

defmodule Microwaveprop.Weather.NexradCacheTest do
use ExUnit.Case, async: false
alias Microwaveprop.Weather.NexradCache
setup do
NexradCache.clear()
:ok
end
describe "fetch/1" do
test "returns :miss when nothing is cached" do
assert NexradCache.fetch(~U[2026-04-12 12:00:00Z]) == :miss
end
test "returns cached pixels + width after put" do
NexradCache.put(~U[2026-04-12 12:00:00Z], <<1, 2, 3, 4>>, 2)
assert {:ok, <<1, 2, 3, 4>>, 2} = NexradCache.fetch(~U[2026-04-12 12:00:00Z])
end
test "isolates entries by timestamp" do
NexradCache.put(~U[2026-04-12 12:00:00Z], <<1>>, 1)
NexradCache.put(~U[2026-04-12 12:05:00Z], <<2>>, 1)
assert {:ok, <<1>>, 1} = NexradCache.fetch(~U[2026-04-12 12:00:00Z])
assert {:ok, <<2>>, 1} = NexradCache.fetch(~U[2026-04-12 12:05:00Z])
end
end
describe "prune_older_than/1" do
test "removes entries with a timestamp strictly before the cutoff" do
NexradCache.put(~U[2026-04-12 11:50:00Z], <<1>>, 1)
NexradCache.put(~U[2026-04-12 12:00:00Z], <<2>>, 1)
NexradCache.prune_older_than(~U[2026-04-12 11:55:00Z])
assert NexradCache.fetch(~U[2026-04-12 11:50:00Z]) == :miss
assert {:ok, <<2>>, 1} = NexradCache.fetch(~U[2026-04-12 12:00:00Z])
end
test "keeps entries equal to the cutoff" do
NexradCache.put(~U[2026-04-12 12:00:00Z], <<1>>, 1)
NexradCache.prune_older_than(~U[2026-04-12 12:00:00Z])
assert {:ok, <<1>>, 1} = NexradCache.fetch(~U[2026-04-12 12:00:00Z])
end
end
end