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

  import Mox

  alias Towerops.Snmp.Profiles.Vendors.Altalabs
  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 altalabs and altalabs-ap profile names" do
      assert Altalabs.profile_names() == ["altalabs", "altalabs-ap"]
    end
  end

  describe "detect_hardware/1" do
    test "returns nil (hardware detection handled by YAML)" do
      assert Altalabs.detect_hardware(@client_opts) == nil
    end
  end

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

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

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

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

    test "includes memory sensors" do
      defs = Altalabs.wireless_oid_defs()
      mem_used = Enum.find(defs, &(&1.sensor_descr == "Memory Used"))
      mem_total = Enum.find(defs, &(&1.sensor_descr == "Total Memory"))

      assert mem_used
      assert mem_total
    end
  end

  describe "discover_wireless_sensors/1" do
    test "discovers sensors when SNMP responds" do
      expect(SnmpMock, :get, 3, fn _, oid, _ ->
        cond do
          String.contains?(oid, "2021.10.1.3.1") -> {:ok, 125}
          String.contains?(oid, "2021.4.6.0") -> {:ok, 524_288}
          String.contains?(oid, "2021.4.5.0") -> {:ok, 1_048_576}
          true -> {:error, :no_such_object}
        end
      end)

      sensors = Altalabs.discover_wireless_sensors(@client_opts)

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

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

      sensors = Altalabs.discover_wireless_sensors(@client_opts)

      assert sensors == []
    end
  end
end
