defmodule Microwaveprop.Rover.HilltopTest do
  use ExUnit.Case, async: true

  alias Microwaveprop.Rover.Hilltop

  test "snap/2 returns the highest sampled SRTM point inside the cell" do
    cell_center = {33.0, -96.0}

    # Stub elev lookup: highest elevation NE of cell center.
    elev_lookup = fn points ->
      Map.new(points, fn {lat, lon} ->
        elev = round((lat - 33.0) * 1000 + (lon + 96.0) * 1000)
        {{lat, lon}, elev}
      end)
    end

    {lat, lon, elev} = Hilltop.snap(cell_center, elev_lookup: elev_lookup)

    # Best point should be the corner with the largest +lat, +lon offsets
    # (NE corner of the sample grid).
    assert lat > 33.0
    assert lon > -96.0
    assert is_integer(elev)
    assert elev > 0
  end

  test "snap/2 returns nil when SRTM lookup yields no values" do
    elev_lookup = fn points -> Map.new(points, fn p -> {p, nil} end) end

    assert Hilltop.snap({33.0, -96.0}, elev_lookup: elev_lookup) == nil
  end

  test "snap/2 stays within the requested half-degree radius" do
    elev_lookup = fn points -> Map.new(points, fn p -> {p, 100} end) end

    {lat, lon, _} = Hilltop.snap({33.0, -96.0}, elev_lookup: elev_lookup, half_deg: 0.0625)

    assert_in_delta lat, 33.0, 0.0625 + 1.0e-6
    assert_in_delta lon, -96.0, 0.0625 + 1.0e-6
  end
end
