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

  import Mox

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

  describe "detect_hardware/1" do
    test "detects hardware from model OID" do
      expect(SnmpMock, :get, fn _, "1.3.6.1.4.1.15983.1.1.3.1.1.1.0", _ ->
        {:ok, "FortiWLC-500D"}
      end)

      assert Fortiwlc.detect_hardware(@client_opts) == "FortiWLC-500D"
    end

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

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

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

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

    test "includes clients sensor" do
      defs = Fortiwlc.wireless_oid_defs()
      clients = Enum.find(defs, &(&1.sensor_descr == "Total Wireless Clients"))

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

    test "includes AP count sensors" do
      defs = Fortiwlc.wireless_oid_defs()
      connected_aps = Enum.find(defs, &(&1.sensor_descr == "Connected APs"))
      total_aps = Enum.find(defs, &(&1.sensor_descr == "Total APs"))

      assert connected_aps
      assert connected_aps.sensor_type == "ap-count"
      assert total_aps
      assert total_aps.sensor_type == "ap-count"
    end

    test "includes SSID count sensor" do
      defs = Fortiwlc.wireless_oid_defs()
      ssids = Enum.find(defs, &(&1.sensor_descr == "Active SSIDs"))

      assert ssids
      assert ssids.sensor_type == "count"
    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, "15983.1.1.3.1.13.11.0") -> {:ok, 150}
          String.contains?(oid, "15983.1.1.3.1.13.9.0") -> {:ok, 25}
          String.contains?(oid, "15983.1.1.3.1.13.8.0") -> {:ok, 30}
          String.contains?(oid, "15983.1.1.3.1.13.10.0") -> {:ok, 5}
          true -> {:error, :no_such_object}
        end
      end)

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

      assert sensors == []
    end
  end
end
