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 describe "size cap" do test "evicts the oldest-by-timestamp entries once the cap is exceeded" do # Insert 25 entries; max is 20, so 5 oldest get dropped. base = ~U[2026-04-12 00:00:00Z] for i <- 1..25 do ts = DateTime.add(base, i * 300, :second) NexradCache.put(ts, <>, 1) end # The 5 oldest (i = 1..5) should have been evicted. for i <- 1..5 do ts = DateTime.add(base, i * 300, :second) assert NexradCache.fetch(ts) == :miss end # The most recent 20 should still be present. for i <- 6..25 do ts = DateTime.add(base, i * 300, :second) assert {:ok, <<^i>>, 1} = NexradCache.fetch(ts) end end end end