Bumps coverage by exercising several previously-uncovered branches: - GraphLive.Show: new test file driving `range=live` for every sensor_type (latency, processors, memory, traffic, temperature, storage, voltage, count, pppoe_sessions, connections, wireless, unknown), plus `get_title_suffix/1` across sensor_id / interface_id / storage_id, and `change_range` flipping into live mode. - DeviceLive.Show: format_check_value/2 now covers the snmp_sensor branch across temperature, voltage, current, power, frequency, humidity, fanspeed (default + custom unit), and an unknown sensor_type with/without a configured unit. - ActivityFeedLive: clear_search and load_more events; PubSub handle_info clauses for new_alert / alert_resolved / device_added / config_changed / sync_completed / device_event / :tick / catch-all. - AlertLive.Index: acknowledge / resolve event handlers across success, not-found, and cross-org unauthorized branches. Coverage: 86.42% → 86.52%.
176 lines
6.3 KiB
Elixir
176 lines
6.3 KiB
Elixir
defmodule ToweropsWeb.DeviceLive.ShowHelpersTest do
|
|
use ExUnit.Case, async: true
|
|
|
|
alias ToweropsWeb.DeviceLive.Show
|
|
|
|
describe "utilization_text_color/1" do
|
|
test ">= 90 is red" do
|
|
assert Show.utilization_text_color(90) =~ "red"
|
|
assert Show.utilization_text_color(100) =~ "red"
|
|
end
|
|
|
|
test ">= 70 < 90 is yellow" do
|
|
assert Show.utilization_text_color(70) =~ "yellow"
|
|
assert Show.utilization_text_color(89) =~ "yellow"
|
|
end
|
|
|
|
test "< 70 is green" do
|
|
assert Show.utilization_text_color(0) =~ "green"
|
|
assert Show.utilization_text_color(69) =~ "green"
|
|
end
|
|
end
|
|
|
|
describe "format_event_type/1" do
|
|
test "replaces underscores with spaces and capitalizes" do
|
|
assert "Interface down" == Show.format_event_type("interface_down")
|
|
assert "Config changed" == Show.format_event_type("config_changed")
|
|
end
|
|
|
|
test "single word is capitalized" do
|
|
assert "Reboot" == Show.format_event_type("reboot")
|
|
end
|
|
end
|
|
|
|
describe "extract_version_number/1" do
|
|
test "nil returns nil" do
|
|
assert nil == Show.extract_version_number(nil)
|
|
end
|
|
|
|
test "empty string returns nil" do
|
|
assert nil == Show.extract_version_number("")
|
|
end
|
|
|
|
test "extracts version from text" do
|
|
assert "7.12" == Show.extract_version_number("RouterOS 7.12 stable")
|
|
assert "1.2.3" == Show.extract_version_number("v1.2.3")
|
|
assert "7.12.1" == Show.extract_version_number("Version 7.12.1 rc1")
|
|
end
|
|
|
|
test "no version in string returns nil" do
|
|
assert nil == Show.extract_version_number("no version here")
|
|
end
|
|
|
|
test "non-string returns nil" do
|
|
assert nil == Show.extract_version_number(123)
|
|
end
|
|
end
|
|
|
|
describe "firmware_update_available?/2" do
|
|
test "nil device returns false" do
|
|
assert false == Show.firmware_update_available?(nil, %{version: "7.13"})
|
|
end
|
|
|
|
test "nil available returns false" do
|
|
assert false == Show.firmware_update_available?(%{firmware_version: "7.12"}, nil)
|
|
end
|
|
|
|
test "newer available version returns true" do
|
|
snmp = %{firmware_version: "RouterOS 7.12"}
|
|
avail = %{version: "7.13"}
|
|
assert Show.firmware_update_available?(snmp, avail)
|
|
end
|
|
|
|
test "same version returns falsy" do
|
|
snmp = %{firmware_version: "7.12"}
|
|
avail = %{version: "7.12"}
|
|
refute Show.firmware_update_available?(snmp, avail)
|
|
end
|
|
|
|
test "older available returns falsy" do
|
|
snmp = %{firmware_version: "7.13"}
|
|
avail = %{version: "7.12"}
|
|
refute Show.firmware_update_available?(snmp, avail)
|
|
end
|
|
end
|
|
|
|
describe "group_title/1" do
|
|
test "known groups" do
|
|
assert "SNMP Sensors" == Show.group_title(:snmp_sensors)
|
|
assert "SNMP Interfaces" == Show.group_title(:snmp_interfaces)
|
|
assert "HTTP Checks" == Show.group_title(:http_checks)
|
|
assert "DNS Checks" == Show.group_title(:dns_checks)
|
|
assert "SSL Certificate Checks" == Show.group_title(:ssl_checks)
|
|
assert "Ping Checks" == Show.group_title(:ping_checks)
|
|
assert "Other Checks" == Show.group_title(:other_checks)
|
|
end
|
|
end
|
|
|
|
describe "format_check_value/2" do
|
|
test "nil value returns dash" do
|
|
assert "-" == Show.format_check_value(%{value: nil}, %{check_type: "any"})
|
|
end
|
|
|
|
test "non-map result returns dash" do
|
|
assert "-" == Show.format_check_value(%{something: :else}, %{check_type: "any"})
|
|
end
|
|
|
|
test "processor value includes %" do
|
|
assert "75.0%" == Show.format_check_value(%{value: 75}, %{check_type: "snmp_processor"})
|
|
end
|
|
|
|
test "storage value includes %" do
|
|
assert "80.1%" == Show.format_check_value(%{value: 80.12}, %{check_type: "snmp_storage"})
|
|
end
|
|
|
|
test "ping/http/tcp/dns include ms" do
|
|
for t <- ["ping", "http", "tcp", "dns"] do
|
|
assert "12.34 ms" == Show.format_check_value(%{value: 12.34}, %{check_type: t})
|
|
end
|
|
end
|
|
|
|
test "unknown check_type rounds to 2 decimals" do
|
|
assert "42.5" == Show.format_check_value(%{value: 42.5}, %{check_type: "weird"})
|
|
end
|
|
|
|
test "snmp_sensor with temperature uses 1 decimal + °C" do
|
|
check = %{check_type: "snmp_sensor", config: %{"sensor_type" => "temperature"}}
|
|
assert "23.5°C" == Show.format_check_value(%{value: 23.456}, check)
|
|
end
|
|
|
|
test "snmp_sensor with voltage uses 2 decimals + V" do
|
|
check = %{check_type: "snmp_sensor", config: %{"sensor_type" => "voltage"}}
|
|
assert "12.34V" == Show.format_check_value(%{value: 12.341}, check)
|
|
end
|
|
|
|
test "snmp_sensor with current uses 2 decimals + A" do
|
|
check = %{check_type: "snmp_sensor", config: %{"sensor_type" => "current"}}
|
|
result = Show.format_check_value(%{value: 1.523}, check)
|
|
assert result == "1.52A"
|
|
end
|
|
|
|
test "snmp_sensor with power uses 1 decimal + W" do
|
|
check = %{check_type: "snmp_sensor", config: %{"sensor_type" => "power"}}
|
|
assert "75.5W" == Show.format_check_value(%{value: 75.456}, check)
|
|
end
|
|
|
|
test "snmp_sensor with frequency uses 0 decimals + Hz" do
|
|
check = %{check_type: "snmp_sensor", config: %{"sensor_type" => "frequency"}}
|
|
assert "60.0Hz" == Show.format_check_value(%{value: 60.0}, check)
|
|
end
|
|
|
|
test "snmp_sensor with humidity uses 1 decimal + %" do
|
|
check = %{check_type: "snmp_sensor", config: %{"sensor_type" => "humidity"}}
|
|
assert "55.5%" == Show.format_check_value(%{value: 55.55}, check)
|
|
end
|
|
|
|
test "snmp_sensor with fanspeed uses RPM by default" do
|
|
check = %{check_type: "snmp_sensor", config: %{"sensor_type" => "fanspeed", "sensor_unit" => nil}}
|
|
assert "3000 RPM" == Show.format_check_value(%{value: 3000}, check)
|
|
end
|
|
|
|
test "snmp_sensor with fanspeed honors a configured unit" do
|
|
check = %{check_type: "snmp_sensor", config: %{"sensor_type" => "fanspeed", "sensor_unit" => "rpm"}}
|
|
assert "3000rpm" == Show.format_check_value(%{value: 3000}, check)
|
|
end
|
|
|
|
test "snmp_sensor with unknown sensor_type uses default 2-decimal precision and the configured unit" do
|
|
check = %{check_type: "snmp_sensor", config: %{"sensor_type" => "lux", "sensor_unit" => "lx"}}
|
|
assert "123.46lx" == Show.format_check_value(%{value: 123.456}, check)
|
|
end
|
|
|
|
test "snmp_sensor with unknown sensor_type and no unit defaults to no unit suffix" do
|
|
check = %{check_type: "snmp_sensor", config: %{"sensor_type" => "lux"}}
|
|
assert "123.46" == Show.format_check_value(%{value: 123.456}, check)
|
|
end
|
|
end
|
|
end
|