defmodule Towerops.Snmp.Profiles.Vendors.AdtranTest do
  use Towerops.DataCase, async: true

  import Mox

  alias Towerops.Snmp.Profiles.Vendors.Adtran
  alias Towerops.Snmp.SnmpMock

  setup :verify_on_exit!

  @client_opts [
    ip: "192.168.1.1",
    community: "public",
    version: "2c",
    port: 161,
    timeout: 5000
  ]

  describe "profile_names/0" do
    test "returns ADTRAN profile names" do
      assert Adtran.profile_names() == ["adtran-aos"]
    end
  end

  describe "detect_hardware/1" do
    test "returns model when SNMP responds" do
      expect(SnmpMock, :get, fn _, "1.3.6.1.4.1.664.1.1.2.0", _ ->
        {:ok, "NetVanta 1531P"}
      end)

      assert Adtran.detect_hardware(@client_opts) == "NetVanta 1531P"
    end

    test "returns nil when SNMP fails" do
      expect(SnmpMock, :get, fn _, _, _ ->
        {:error, :timeout}
      end)

      assert Adtran.detect_hardware(@client_opts) == nil
    end
  end

  describe "wireless_oid_defs/0" do
    test "returns list of sensor definitions" do
      defs = Adtran.wireless_oid_defs()

      assert is_list(defs)
      assert [_ | _] = defs
    end

    test "includes CPU utilization sensor" do
      defs = Adtran.wireless_oid_defs()
      cpu = Enum.find(defs, &(&1.sensor_descr == "CPU Utilization"))

      assert cpu
      assert cpu.sensor_type == "load"
      assert cpu.sensor_unit == "%"
    end

    test "includes CPU 5min average sensor" do
      defs = Adtran.wireless_oid_defs()
      cpu_avg = Enum.find(defs, &(&1.sensor_descr == "CPU 5min Average"))

      assert cpu_avg
      assert cpu_avg.sensor_type == "load"
    end
  end

  describe "discover_wireless_sensors/1" do
    test "discovers sensors when SNMP responds" do
      expect(SnmpMock, :get, 4, fn _, oid, _ ->
        cond do
          String.contains?(oid, "664.5.53.1.4.1.0") -> {:ok, 25}
          String.contains?(oid, "664.5.53.1.4.4.0") -> {:ok, 20}
          String.contains?(oid, "664.5.53.1.4.8.0") -> {:ok, 512_000_000}
          String.contains?(oid, "1.3.0") -> {:ok, 123_456_789}
          true -> {:error, :no_such_object}
        end
      end)

      sensors = Adtran.discover_wireless_sensors(@client_opts)

      assert is_list(sensors)
      assert length(sensors) == 4
    end

    test "returns empty list when no sensors respond" do
      expect(SnmpMock, :get, 4, fn _, _, _ ->
        {:error, :no_such_object}
      end)

      sensors = Adtran.discover_wireless_sensors(@client_opts)

      assert sensors == []
    end
  end
end
