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

  import Mox

  alias Towerops.Snmp.Profiles.Vendors.Racom
  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 racom profile names" do
      assert Racom.profile_names() == ["ray", "ray3"]
    end
  end

  describe "detect_hardware/1" do
    test "detects hardware from product name OID" do
      expect(SnmpMock, :get, fn _, "1.3.6.1.4.1.33555.1.1.1.3.0", _ ->
        {:ok, "RAy3-17"}
      end)

      assert Racom.detect_hardware(@client_opts) == "RAy3-17"
    end

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

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

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

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

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

      assert temp
      assert temp.sensor_divisor == 100
    end

    test "includes voltage sensor" do
      defs = Racom.wireless_oid_defs()
      voltage = Enum.find(defs, &(&1.sensor_type == "voltage"))

      assert voltage
      assert voltage.sensor_unit == "V"
    end

    test "includes BER sensor" do
      defs = Racom.wireless_oid_defs()
      ber = Enum.find(defs, &(&1.sensor_type == "ber"))

      assert ber
      assert ber.sensor_descr == "BER"
    end
  end

  describe "discover_wireless_sensors/1" do
    test "discovers sensors when SNMP responds" do
      expect(SnmpMock, :get, 5, fn _, oid, _ ->
        cond do
          String.contains?(oid, "33555.1.1.4.2.0") -> {:ok, 4500}
          String.contains?(oid, "33555.1.1.4.3.0") -> {:ok, 480}
          String.contains?(oid, "33555.1.3.2.11.0") -> {:ok, 0}
          String.contains?(oid, "33555.1.2.1.7.0") -> {:ok, 4}
          String.contains?(oid, "33555.1.2.1.8.0") -> {:ok, 4}
          true -> {:error, :no_such_object}
        end
      end)

      sensors = Racom.discover_wireless_sensors(@client_opts)

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

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

      sensors = Racom.discover_wireless_sensors(@client_opts)

      assert sensors == []
    end
  end
end
