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

  import Mox

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

  describe "detect_hardware/1" do
    test "detects hardware from model OID" do
      expect(SnmpMock, :get, fn _, "1.3.6.1.4.1.17713.60.1.1.1.0", _ ->
        {:ok, "cnWave V3000"}
      end)

      assert Cnwave60.detect_hardware(@client_opts) == "cnWave V3000"
    end

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

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

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

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

    test "includes receive signal level sensor" do
      defs = Cnwave60.wireless_oid_defs()
      rsl = Enum.find(defs, &(&1.sensor_descr == "Receive Signal Level"))

      assert rsl
      assert rsl.sensor_type == "rssi"
      assert rsl.sensor_divisor == 10
    end

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

      assert snr
      assert snr.sensor_type == "snr"
    end

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

      assert temp
      assert temp.sensor_descr == "System Temperature"
    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, "17713.60.12.1.0") -> {:ok, -550}
          String.contains?(oid, "17713.60.12.2.0") -> {:ok, 200}
          String.contains?(oid, "17713.60.12.3.0") -> {:ok, 280}
          String.contains?(oid, "17713.60.12.4.0") -> {:ok, 1000}
          String.contains?(oid, "17713.60.13.1.0") -> {:ok, 45}
          true -> {:error, :no_such_object}
        end
      end)

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

      assert sensors == []
    end
  end
end
