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

  import Mox

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

  describe "detect_hardware/1" do
    test "detects hardware from model OID" do
      expect(SnmpMock, :get, fn _, "1.3.6.1.4.1.32761.1.1.1.0", _ ->
        {:ok, "DLB APC 5M"}
      end)

      assert Deliberant.detect_hardware(@client_opts) == "DLB APC 5M"
    end

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

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

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

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

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

      assert signal
      assert signal.sensor_type == "rssi"
      assert signal.sensor_unit == "dBm"
    end

    test "includes noise floor sensor" do
      defs = Deliberant.wireless_oid_defs()
      noise = Enum.find(defs, &(&1.sensor_descr == "Noise Floor"))

      assert noise
      assert noise.sensor_type == "noise"
    end

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

      assert temp
      assert temp.sensor_descr == "CPU 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, "32761.3.5.1.2.1.6.1") -> {:ok, -65}
          String.contains?(oid, "32761.3.5.1.2.1.7.1") -> {:ok, -95}
          String.contains?(oid, "32761.3.5.1.2.1.8.1") -> {:ok, 85}
          String.contains?(oid, "32761.3.5.1.2.1.9.1") -> {:ok, 12}
          String.contains?(oid, "32761.3.1.1.2.0") -> {:ok, 55}
          true -> {:error, :no_such_object}
        end
      end)

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

      assert sensors == []
    end
  end
end
