From 9c2efec473053164a9a4769b484b9d620a796ef2 Mon Sep 17 00:00:00 2001 From: Graham McIntire Date: Sun, 25 Jan 2026 12:34:19 -0600 Subject: [PATCH] test: add coverage for MikroTik wrong unit type override MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds test case for the real-world scenario where MikroTik devices report incorrect mtxrGaugeUnit values (e.g., reporting unit type 1 for celsius/temperature when the sensor is actually a voltage sensor). This test verifies that the name-based override logic in maybe_override_sensor_type/2 correctly handles this case: - Sensor name: "psu1-voltage" or "psu2-voltage" - Reported unit type: 1 (celsius/temperature) - Expected result: voltage sensor with divisor 10 The override code was added in commit f402dcb (Jan 22, 2026) and works correctly for new discoveries. Existing sensors created before this date need the migration from commit 68a4ebf to fix their values. Related: 20260125182836_fix_misclassified_mikrotik_voltage_sensors.exs 🤖 Generated with Claude Code Co-Authored-By: Claude Sonnet 4.5 --- .../snmp/profiles/vendors/routeros_test.exs | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/test/towerops/snmp/profiles/vendors/routeros_test.exs b/test/towerops/snmp/profiles/vendors/routeros_test.exs index b071ad3d..b6158ffa 100644 --- a/test/towerops/snmp/profiles/vendors/routeros_test.exs +++ b/test/towerops/snmp/profiles/vendors/routeros_test.exs @@ -343,5 +343,48 @@ defmodule Towerops.Snmp.Profiles.Vendors.RouterosTest do assert voltage_sensor.sensor_divisor == 10 assert voltage_sensor.last_value == 12.0 end + + test "overrides sensor type when device reports wrong unit type" do + expect(SnmpMock, :walk, fn _, @wl_ap_table, _ -> {:ok, []} end) + expect(SnmpMock, :walk, fn _, @wl_stat_table, _ -> {:ok, []} end) + expect(SnmpMock, :walk, fn _, @wl_60g_table, _ -> {:ok, []} end) + expect(SnmpMock, :walk, fn _, @lte_modem_table, _ -> {:ok, []} end) + + # Device incorrectly reports unit type 1 (celsius/temperature) for voltage sensor + # This is the real-world bug case: sensor named "psu2-voltage" with unit type 1 + expect(SnmpMock, :walk, fn _, @gauge_table, _ -> + {:ok, + [ + # PSU voltage sensor incorrectly reported as temperature (unit type 1) + %{oid: "#{@gauge_table}.2.7201", value: "psu1-voltage"}, + %{oid: "#{@gauge_table}.3.7201", value: 469}, + %{oid: "#{@gauge_table}.4.7201", value: 1}, + # Another voltage sensor with wrong unit type + %{oid: "#{@gauge_table}.2.7202", value: "psu2-voltage"}, + %{oid: "#{@gauge_table}.3.7202", value: 469}, + %{oid: "#{@gauge_table}.4.7202", value: 1} + ]} + end) + + expect(SnmpMock, :walk, fn _, @optical_table, _ -> {:ok, []} end) + expect(SnmpMock, :get, 2, fn _, _oid, _ -> {:error, :timeout} end) + + sensors = Routeros.discover_wireless_sensors(@client_opts) + + # Verify both sensors are correctly overridden to voltage with divisor 10 + psu1_sensor = Enum.find(sensors, &(&1.sensor_descr == "psu1-voltage")) + assert psu1_sensor + assert psu1_sensor.sensor_type == "voltage" + assert psu1_sensor.sensor_unit == "V" + assert psu1_sensor.sensor_divisor == 10 + assert psu1_sensor.last_value == 46.9 + + psu2_sensor = Enum.find(sensors, &(&1.sensor_descr == "psu2-voltage")) + assert psu2_sensor + assert psu2_sensor.sensor_type == "voltage" + assert psu2_sensor.sensor_unit == "V" + assert psu2_sensor.sensor_divisor == 10 + assert psu2_sensor.last_value == 46.9 + end end end