defmodule Microwaveprop.RoverPlanning.StationTest do use Microwaveprop.DataCase, async: true alias Microwaveprop.RoverPlanning.Station describe "changeset/2" do test "resolves coordinate input into lat/lon" do changeset = Station.changeset(%Station{}, %{input: "32.9, -96.8"}) assert changeset.valid? assert get_change(changeset, :lat) == 32.9 assert get_change(changeset, :lon) == -96.8 end test "resolves Maidenhead grid into lat/lon and grid field" do changeset = Station.changeset(%Station{}, %{input: "EM12kx"}) assert changeset.valid? assert get_change(changeset, :grid) == "EM12KX" assert is_float(get_change(changeset, :lat)) assert is_float(get_change(changeset, :lon)) end test "blank input is invalid" do changeset = Station.changeset(%Station{}, %{input: ""}) refute changeset.valid? assert "can't be blank" in errors_on(changeset).input end test "missing input is invalid" do changeset = Station.changeset(%Station{}, %{}) refute changeset.valid? assert "can't be blank" in errors_on(changeset).input end test "out-of-range latitude is rejected" do # Stuff a coordinate input that LocationResolver returns happily # but is out of physical range — validate_number should catch it. changeset = Station.changeset(%Station{}, %{input: "95.0, 0.0"}) refute changeset.valid? assert Map.has_key?(errors_on(changeset), :lat) end test "out-of-range longitude is rejected" do changeset = Station.changeset(%Station{}, %{input: "0.0, 200.0"}) refute changeset.valid? assert Map.has_key?(errors_on(changeset), :lon) end test "re-typing input wipes previously resolved fields before re-resolving" do stale = %Station{ callsign: "OLDCALL", grid: "FN42", lat: 40.0, lon: -74.0, input: "FN42" } changeset = Station.changeset(stale, %{input: "32.9, -96.8"}) assert changeset.valid? # New coordinate input clears stale callsign/grid… assert get_change(changeset, :callsign) == nil assert get_change(changeset, :grid) == nil # …and replaces lat/lon with the new resolution. assert get_change(changeset, :lat) == 32.9 assert get_change(changeset, :lon) == -96.8 end test "keeps existing lat/lon when no input change and no new resolution needed" do preset = %Station{input: "EM12", lat: 32.5, lon: -97.0} changeset = Station.changeset(preset, %{position: 1}) assert changeset.valid? assert get_change(changeset, :position) == 1 # lat/lon already set, no input change → no re-resolution assert get_field(changeset, :lat) == 32.5 assert get_field(changeset, :lon) == -97.0 end test "accepts position field with default 0" do assert %Station{}.position == 0 changeset = Station.changeset(%Station{}, %{input: "32.9, -96.8", position: 3}) assert changeset.valid? assert get_change(changeset, :position) == 3 end end describe "schema metadata" do test "has expected fields" do fields = Station.__schema__(:fields) assert :callsign in fields assert :grid in fields assert :input in fields assert :lat in fields assert :lon in fields assert :position in fields assert :mission_id in fields end end end