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

  import Mox

  alias Towerops.Snmp.Profiles.Vendors.Dragonwave
  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 dragonwave profile names" do
      assert Dragonwave.profile_names() == [
               "horizon-compact",
               "horizon-compactplus",
               "horizon-duo",
               "horizon-quantum"
             ]
    end
  end

  describe "detect_hardware/1" do
    test "detects hardware from assembly number OID" do
      expect(SnmpMock, :get, fn _, "1.3.6.1.4.1.7262.2.2.1.1.8.0", _ ->
        {:ok, "Horizon Compact+"}
      end)

      assert Dragonwave.detect_hardware(@client_opts) == "Horizon Compact+"
    end

    test "returns nil when OID unavailable" do
      expect(SnmpMock, :get, fn _, _, _ ->
        {:error, :no_such_object}
      end)

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

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

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

    test "includes temperature sensor with divisor" do
      defs = Dragonwave.wireless_oid_defs()
      temp = Enum.find(defs, &(&1.sensor_type == "temperature"))

      assert temp
      assert temp.sensor_divisor == 10
    end

    test "includes SNR sensor" do
      defs = Dragonwave.wireless_oid_defs()
      snr = Enum.find(defs, &(&1.sensor_type == "snr"))

      assert snr
      assert snr.sensor_unit == "dB"
    end

    test "includes RSSI sensor" do
      defs = Dragonwave.wireless_oid_defs()
      rssi = Enum.find(defs, &(&1.sensor_type == "rssi"))

      assert rssi
      assert rssi.sensor_descr == "RSL"
    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, "7262.2.2.5.1.3.6.0") -> {:ok, 450}
          String.contains?(oid, "7262.2.2.5.1.2.3.0") -> {:ok, 280}
          String.contains?(oid, "7262.2.2.5.1.2.2.0") -> {:ok, -550}
          String.contains?(oid, "7262.2.2.5.1.3.2.0") -> {:ok, 180}
          true -> {:error, :no_such_object}
        end
      end)

      sensors = Dragonwave.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 = Dragonwave.discover_wireless_sensors(@client_opts)

      assert sensors == []
    end
  end
end
