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

  import Mox

  alias Towerops.Snmp.Profiles.Vendors.Ciscowlc
  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 cisco wlc profile names" do
      assert Ciscowlc.profile_names() == ["ciscowlc"]
    end
  end

  describe "detect_hardware/1" do
    test "detects hardware from model OID" do
      expect(SnmpMock, :get, fn _, "1.3.6.1.4.1.14179.1.1.1.3.0", _ ->
        {:ok, "AIR-CT5508-K9"}
      end)

      assert Ciscowlc.detect_hardware(@client_opts) == "AIR-CT5508-K9"
    end

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

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

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

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

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

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

    test "includes AP count sensor" do
      defs = Ciscowlc.wireless_oid_defs()
      aps = Enum.find(defs, &(&1.sensor_descr == "Total APs"))

      assert aps
      assert aps.sensor_type == "ap-count"
    end

    test "includes 5GHz clients sensor" do
      defs = Ciscowlc.wireless_oid_defs()
      clients_5g = Enum.find(defs, &(&1.sensor_descr == "5GHz Clients"))

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

    test "includes rogue AP sensor" do
      defs = Ciscowlc.wireless_oid_defs()
      rogue = Enum.find(defs, &(&1.sensor_descr == "Rogue APs"))

      assert rogue
      assert rogue.sensor_type == "count"
    end
  end

  describe "discover_wireless_sensors/1" do
    test "discovers sensors when SNMP responds" do
      expect(SnmpMock, :get, 6, fn _, oid, _ ->
        cond do
          String.contains?(oid, "14179.2.1.1.1.38.0") -> {:ok, 500}
          String.contains?(oid, "14179.2.1.1.1.12.0") -> {:ok, 25}
          String.contains?(oid, "14179.2.1.1.1.2.0") -> {:ok, 300}
          String.contains?(oid, "14179.2.1.1.1.3.0") -> {:ok, 200}
          String.contains?(oid, "14179.2.1.1.1.14.0") -> {:ok, 3}
          String.contains?(oid, "14179.2.1.1.1.15.0") -> {:ok, 1}
          true -> {:error, :no_such_object}
        end
      end)

      sensors = Ciscowlc.discover_wireless_sensors(@client_opts)

      assert is_list(sensors)
      assert length(sensors) == 6
    end

    test "returns empty list when no sensors respond" do
      expect(SnmpMock, :get, 6, fn _, _, _ ->
        {:error, :no_such_object}
      end)

      sensors = Ciscowlc.discover_wireless_sensors(@client_opts)

      assert sensors == []
    end
  end
end
