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

  import Mox

  alias Towerops.Snmp.Profiles.Vendors.Vyos
  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 expected profile names" do
      assert Vyos.profile_names() == ["vyos", "vyatta"]
    end
  end

  describe "detect_hardware/1" do
    test "returns hardware description for VyOS" do
      stub(SnmpMock, :get, fn _, oid, _ ->
        case oid do
          "1.3.6.1.2.1.1.1.0" ->
            {:ok, "VyOS 1.3.0 Linux 4.19.0-amd64"}

          _ ->
            {:error, :no_such_object}
        end
      end)

      assert Vyos.detect_hardware(@client_opts) == "VyOS 1.3.0 Linux 4.19.0-amd64"
    end

    test "returns hardware description for Vyatta" do
      stub(SnmpMock, :get, fn _, oid, _ ->
        case oid do
          "1.3.6.1.2.1.1.1.0" ->
            {:ok, "Vyatta 6.6R1"}

          _ ->
            {:error, :no_such_object}
        end
      end)

      assert Vyos.detect_hardware(@client_opts) == "Vyatta 6.6R1"
    end

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

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

    test "truncates long descriptions to 64 characters" do
      long_descr = String.duplicate("A", 100)

      stub(SnmpMock, :get, fn _, oid, _ ->
        case oid do
          "1.3.6.1.2.1.1.1.0" ->
            {:ok, long_descr}

          _ ->
            {:error, :no_such_object}
        end
      end)

      result = Vyos.detect_hardware(@client_opts)
      assert String.length(result) == 64
    end
  end

  describe "wireless_oid_defs/0" do
    test "returns empty list (software routers have no hardware sensors)" do
      assert Vyos.wireless_oid_defs() == []
    end
  end

  describe "discover_wireless_sensors/1" do
    test "returns empty list when no sensors defined" do
      sensors = Vyos.discover_wireless_sensors(@client_opts)
      assert sensors == []
    end
  end
end
