defmodule MicrowavepropWeb.PathLiveTest do use MicrowavepropWeb.ConnCase, async: false import Phoenix.LiveViewTest alias Microwaveprop.Propagation alias Microwaveprop.Propagation.ScoreCache alias Microwaveprop.Terrain.ElevationClient describe "propagation_updated handler" do setup do ScoreCache.clear() # Flat-terrain stub so compute_path can build a result without hitting # the real elevation API. Req.Test.stub(ElevationClient, fn conn -> params = Plug.Conn.fetch_query_params(conn).query_params lat_count = params["latitude"] |> String.split(",") |> length() Req.Test.json(conn, %{"elevation" => List.duplicate(200.0, lat_count)}) end) on_exit(fn -> ScoreCache.clear() end) :ok end test "re-reads and re-renders the forecast when new scores arrive", %{conn: conn} do # Path midpoint is (33.0, -97.0) — the point point_forecast queries. {mid_lat, mid_lon} = {33.0, -97.0} # Seed three hourly valid_times with a LOW score of 30. base = DateTime.utc_now() |> DateTime.truncate(:second) |> Map.put(:minute, 0) |> Map.put(:second, 0) times = for h <- 0..2, do: DateTime.add(base, h * 3600, :second) Enum.each(times, fn t -> Propagation.replace_scores( [%{lat: mid_lat, lon: mid_lon, valid_time: t, band_mhz: 10_000, score: 30, factors: nil}], t ) end) {:ok, lv, _html} = live(conn, ~p"/path?source=32.5,-97.0&destination=33.5,-97.0&band=10000") initial = render(lv) assert initial =~ "Propagation Forecast" assert initial =~ ~r/Best:.*?]*>\s*30\s*<\/span>/s # Publish a new forecast: same times, new score of 80. Enum.each(times, fn t -> Propagation.replace_scores( [%{lat: mid_lat, lon: mid_lon, valid_time: t, band_mhz: 10_000, score: 80, factors: nil}], t ) end) send(lv.pid, {:propagation_updated, times}) refreshed = render(lv) assert refreshed =~ ~r/Best:.*?]*>\s*80\s*<\/span>/s refute refreshed =~ ~r/Best:.*?]*>\s*30\s*<\/span>/s end end end