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

  import Mox

  alias Towerops.Snmp.Profiles.Vendors.Tranzeo
  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 tranzeo profile name" do
      assert Tranzeo.profile_names() == ["tranzeo"]
    end
  end

  describe "detect_hardware/1" do
    test "detects hardware from sysDescr containing Tranzeo" do
      expect(SnmpMock, :get, fn _, "1.3.6.1.2.1.1.1.0", _ ->
        {:ok, "Tranzeo TR-5A Plus"}
      end)

      assert Tranzeo.detect_hardware(@client_opts) == "TR-5A Plus"
    end

    test "returns nil when sysDescr does not contain Tranzeo" do
      expect(SnmpMock, :get, fn _, _, _ ->
        {:ok, "Some Other Device"}
      end)

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

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

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

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

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

    test "includes associated clients sensor" do
      defs = Tranzeo.wireless_oid_defs()
      clients = Enum.find(defs, &(&1.sensor_descr == "Associated Clients"))

      assert clients
      assert clients.sensor_type == "clients"
    end

    test "includes signal and power sensors" do
      defs = Tranzeo.wireless_oid_defs()
      signal = Enum.find(defs, &(&1.sensor_descr == "Signal Strength"))
      tx_power = Enum.find(defs, &(&1.sensor_descr == "TX Power"))

      assert signal
      assert signal.sensor_type == "rssi"
      assert tx_power
      assert tx_power.sensor_type == "power"
    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, "24350.2.1.1.0") -> {:ok, 15}
          String.contains?(oid, "24350.2.2.1.0") -> {:ok, -55}
          String.contains?(oid, "24350.2.2.2.0") -> {:ok, -95}
          String.contains?(oid, "24350.2.2.3.0") -> {:ok, 20}
          true -> {:error, :no_such_object}
        end
      end)

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

      assert sensors == []
    end
  end
end
