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

  import Mox

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

  describe "detect_hardware/1" do
    test "returns E7-2 when found in sysDescr" do
      expect(SnmpMock, :get, fn _, "1.3.6.1.2.1.1.1.0", _ ->
        {:ok, "Calix E7-2 AXOS 21.3"}
      end)

      assert Calix.detect_hardware(@client_opts) == "E7-2"
    end

    test "returns E7 when found in sysDescr" do
      expect(SnmpMock, :get, fn _, "1.3.6.1.2.1.1.1.0", _ ->
        {:ok, "Calix E7 EXA"}
      end)

      assert Calix.detect_hardware(@client_opts) == "E7"
    end

    test "returns AXOS when found in sysDescr" do
      expect(SnmpMock, :get, fn _, "1.3.6.1.2.1.1.1.0", _ ->
        {:ok, "AXOS Platform"}
      end)

      assert Calix.detect_hardware(@client_opts) == "AXOS"
    end

    test "returns nil when model not in sysDescr" do
      expect(SnmpMock, :get, fn _, "1.3.6.1.2.1.1.1.0", _ ->
        {:ok, "Some other device"}
      end)

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

    test "returns nil when SNMP fails" do
      expect(SnmpMock, :get, fn _, _, _ ->
        {:error, :timeout}
      end)

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

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

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

    test "includes CPU load sensor" do
      defs = Calix.wireless_oid_defs()
      cpu = Enum.find(defs, &(&1.sensor_descr == "CPU Load"))

      assert cpu
      assert cpu.sensor_type == "load"
    end
  end

  describe "discover_wireless_sensors/1" do
    test "discovers sensors when SNMP responds" do
      expect(SnmpMock, :get, 2, fn _, oid, _ ->
        cond do
          String.contains?(oid, "25.3.3.1.2.1") -> {:ok, 25}
          String.contains?(oid, "1.3.0") -> {:ok, 987_654_321}
          true -> {:error, :no_such_object}
        end
      end)

      sensors = Calix.discover_wireless_sensors(@client_opts)

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

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

      sensors = Calix.discover_wireless_sensors(@client_opts)

      assert sensors == []
    end
  end
end
