defmodule Microwaveprop.Radio.BandResolverTest do
  use ExUnit.Case, async: true

  alias Microwaveprop.Radio.BandResolver

  describe "resolve/1" do
    test "resolves wavelength labels" do
      assert BandResolver.resolve("6m") == 50
      assert BandResolver.resolve("2m") == 144
      assert BandResolver.resolve("1.25m") == 222
      assert BandResolver.resolve("70cm") == 432
      assert BandResolver.resolve("9cm") == 3_400
      assert BandResolver.resolve("3cm") == 10_000
    end

    test "resolves canonical values through passthrough" do
      for band <- BandResolver.allowed_bands() do
        assert BandResolver.resolve(band) == band
        assert BandResolver.resolve(Integer.to_string(band)) == band
      end
    end

    test "resolves the legacy 3456 MHz calling frequency to the current 9cm band" do
      # 9cm was cut from 3300-3500 to 3300-3450 MHz in 2020, so the old
      # weak-signal calling frequency 3456 is no longer in the allocation
      # but historical logs still carry it.
      assert BandResolver.resolve("3456") == 3_400
      assert BandResolver.resolve(3456) == 3_400
      assert BandResolver.resolve(3456.1) == 3_400
    end

    test "snaps near-band frequencies to amateur allocations" do
      assert BandResolver.resolve("50.125") == 50
      assert BandResolver.resolve("144.200") == 144
      assert BandResolver.resolve("222.100") == 222
      assert BandResolver.resolve("432.100") == 432
      assert BandResolver.resolve("10368.100") == 10_000
    end

    test "returns nil for HF and gaps between amateur bands" do
      assert BandResolver.resolve("14.200") == nil
      assert BandResolver.resolve("28.400") == nil
      assert BandResolver.resolve("500") == nil
      assert BandResolver.resolve("800") == nil
    end

    test "returns nil for unparseable input" do
      assert BandResolver.resolve(nil) == nil
      assert BandResolver.resolve("") == nil
      assert BandResolver.resolve("banana") == nil
    end
  end
end
