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

  import Mox

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

  describe "detect_hardware/1" do
    test "detects hardware from model OID" do
      expect(SnmpMock, :get, fn _, "1.3.6.1.4.1.2281.1.1.1.0", _ ->
        {:ok, "FibeAir IP-20C"}
      end)

      assert Ceragon.detect_hardware(@client_opts) == "FibeAir IP-20C"
    end

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

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

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

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

    test "includes TX and RX power sensors" do
      defs = Ceragon.wireless_oid_defs()
      tx = Enum.find(defs, &(&1.sensor_descr == "TX Power"))
      rx = Enum.find(defs, &(&1.sensor_descr == "RX Level"))

      assert tx
      assert tx.sensor_type == "power"
      assert rx
      assert rx.sensor_type == "rssi"
    end

    test "includes SNR sensor" do
      defs = Ceragon.wireless_oid_defs()
      snr = Enum.find(defs, &(&1.sensor_descr == "Signal to Noise Ratio"))

      assert snr
      assert snr.sensor_type == "snr"
    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, "2281.10.4.1.11.1.1.1") -> {:ok, 200}
          String.contains?(oid, "2281.10.4.1.11.1.2.1") -> {:ok, -450}
          String.contains?(oid, "2281.10.4.1.11.1.3.1") -> {:ok, 320}
          String.contains?(oid, "2281.10.4.1.11.1.4.1") -> {:ok, 1}
          true -> {:error, :no_such_object}
        end
      end)

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

      assert sensors == []
    end
  end
end
