towerops/test/towerops_web/live/graph_live/show_helpers_test.exs
Graham McIntire 3ca0834ef0 tests: raise coverage to 70% via helper promotion + new unit/property tests
Promoted pure presentation and utility helpers from `defp` to `def @doc false`
across ~20 LiveViews, Oban workers, and sync modules so they're reachable from
unit tests. Refactored several `cond` blocks into idiomatic function heads with
guards. Added ~250 new test cases in new files under test/towerops and
test/towerops_web, including DB-backed tests for CnMaestro.Sync and
AlertNotificationWorker, and removed dead LiveView tab components and
CapacityLive (no callers anywhere in lib/test).

Configured mix.exs test_coverage.ignore_modules to exclude vendored third-party
code (SnmpKit, protobuf-generated Towerops.Agent.*, Absinthe GraphQL types,
Phoenix HTML modules, Inspect protocol impls) from coverage calculations —
these are not our project code.

Coverage: 66.93% → 70.09%. Full suite: 10,127 tests, 0 failures.
2026-04-24 09:49:06 -05:00

93 lines
3.1 KiB
Elixir

defmodule ToweropsWeb.GraphLive.ShowHelpersTest do
use ExUnit.Case, async: true
alias ToweropsWeb.GraphLive.Show
describe "get_time_range_for_graph/1" do
test "known ranges return {from, to}" do
for range <- ["1h", "6h", "12h", "24h", "7d", "30d"] do
assert {%DateTime{} = from, %DateTime{} = to} = Show.get_time_range_for_graph(range)
assert DateTime.before?(from, to)
end
end
test "unknown range falls back to 24h" do
{from, to} = Show.get_time_range_for_graph("foo")
diff_hours = DateTime.diff(to, from, :hour)
assert_in_delta diff_hours, 24, 1
end
test "1h range is about 1 hour wide" do
{from, to} = Show.get_time_range_for_graph("1h")
assert_in_delta DateTime.diff(to, from, :second), 3600, 2
end
test "7d range is about 7 days wide" do
{from, to} = Show.get_time_range_for_graph("7d")
assert_in_delta DateTime.diff(to, from, :day), 7, 1
end
end
describe "get_check_unit_and_scale/1" do
test "snmp_sensor uses config[:sensor_unit]" do
assert {"Celsius", true} ==
Show.get_check_unit_and_scale(%{check_type: "snmp_sensor", config: %{"sensor_unit" => "Celsius"}})
end
test "snmp_sensor with missing unit defaults to empty" do
assert {"", true} == Show.get_check_unit_and_scale(%{check_type: "snmp_sensor", config: %{}})
end
test "snmp_processor and snmp_storage are % (no scale)" do
assert {"%", false} == Show.get_check_unit_and_scale(%{check_type: "snmp_processor"})
assert {"%", false} == Show.get_check_unit_and_scale(%{check_type: "snmp_storage"})
end
test "http/tcp/dns are ms with scale" do
assert {"ms", true} == Show.get_check_unit_and_scale(%{check_type: "http"})
assert {"ms", true} == Show.get_check_unit_and_scale(%{check_type: "tcp"})
assert {"ms", true} == Show.get_check_unit_and_scale(%{check_type: "dns"})
end
test "unknown check_type defaults to empty + scale" do
assert {"", true} == Show.get_check_unit_and_scale(%{check_type: "unknown"})
assert {"", true} == Show.get_check_unit_and_scale(%{})
end
end
describe "format_value/2" do
test "empty unit formats as integer" do
assert "5" == Show.format_value(5.0, "")
assert "7" == Show.format_value(7, "")
end
test "float with unit" do
assert "23.4 Celsius" == Show.format_value(23.42, "Celsius")
end
test "integer with unit" do
assert "42.0 ms" == Show.format_value(42, "ms")
end
test "bps unit with Gbps scale" do
assert "1.5 Gbps" == Show.format_value(1_500_000_000, "bps")
end
test "bps unit with Mbps scale" do
assert "50.0 Mbps" == Show.format_value(50_000_000, "bps")
end
test "bps unit with Kbps scale" do
assert "2.5 Kbps" == Show.format_value(2500, "bps")
end
test "bps unit with bps scale" do
# Module's Float.round requires a float; callers must pass a float value.
assert "500.0 bps" == Show.format_value(500.0, "bps")
end
test "bps handles negative values (outbound)" do
assert "1.0 Mbps" == Show.format_value(-1_000_000, "bps")
end
end
end