aprs.me/test/aprsme_web/live/status_live/index_helpers_test.exs
Graham McIntire ee02769d38
refactor: pattern-match in HealthCheck, EncodingUtils, and StatusLive helpers
- HealthCheck.check_health(:readiness): collapse the 3-branch cond into
  readiness_status/2 dispatched on (health_status, shutting_down?).
- HealthCheck.shutting_down?: the whereis + alive? + try/catch nest
  becomes a small pipeline of function heads (ask_shutting_down?/1 +
  ask_if_alive/2), with the catch isolated to the single place it fires.
- EncodingUtils.sanitize_string_fields and sanitize_nested_map shared a
  three-branch atom-or-string key lookup. Extract update_existing_key/3
  + first_present_key/3 + nil_aware/1 helpers; the original functions
  become two-liners.
- StatusLive.Index.format_uptime: splits the cond into
  format_uptime_parts/4 clauses with numeric guards.
- StatusLive.Index.calculate_health_score: struct-shape pattern match
  on %{connected: false} / %{uptime_seconds: s} guards replaces the cond.
- StatusLive.Index.format_time_ago: pipe the diff through
  format_seconds_ago/1 with four clauses.

Adds tests for the four public StatusLive.Index helpers
(format_uptime, format_time_ago, get_health_description, format_number).
Coverage 67.29 → 67.43%.
2026-04-23 14:02:50 -05:00

100 lines
3.2 KiB
Elixir

defmodule AprsmeWeb.StatusLive.IndexHelpersTest do
use ExUnit.Case, async: true
alias AprsmeWeb.StatusLive.Index
describe "format_uptime/1" do
test "returns 'Not connected' for zero and negative" do
assert Index.format_uptime(0) =~ "Not connected"
assert Index.format_uptime(-10) =~ "Not connected"
end
test "seconds-only for values under a minute" do
assert Index.format_uptime(5) == "5s"
end
test "minutes and seconds for values under an hour" do
assert Index.format_uptime(125) == "2m 5s"
end
test "hours/minutes/seconds for values under a day" do
assert Index.format_uptime(3 * 3600 + 4 * 60 + 5) == "3h 4m 5s"
end
test "days/hours/minutes/seconds for longer values" do
assert Index.format_uptime(2 * 86_400 + 1 * 3600 + 30 * 60 + 15) == "2d 1h 30m 15s"
end
end
describe "format_time_ago/1" do
test "returns 'Never' for nil" do
assert Index.format_time_ago(nil) =~ "Never"
end
test "formats seconds" do
dt = DateTime.add(DateTime.utc_now(), -10, :second)
result = Index.format_time_ago(dt)
assert result =~ "seconds ago"
end
test "formats minutes" do
dt = DateTime.add(DateTime.utc_now(), -120, :second)
result = Index.format_time_ago(dt)
assert result =~ "minutes ago"
end
test "formats hours" do
dt = DateTime.add(DateTime.utc_now(), -3 * 3600, :second)
result = Index.format_time_ago(dt)
assert result =~ "hours ago"
end
test "formats days" do
dt = DateTime.add(DateTime.utc_now(), -3 * 86_400, :second)
result = Index.format_time_ago(dt)
assert result =~ "days ago"
end
end
describe "get_health_description/2" do
test "returns connection-issues text for score 1 with disconnect" do
assert Index.get_health_description(1, false) =~ "Disconnected"
end
test "returns stability text for scores 2-5 when connected" do
assert Index.get_health_description(2, true) =~ "Recently"
assert Index.get_health_description(3, true) =~ "Good"
assert Index.get_health_description(4, true) =~ "Very good"
assert Index.get_health_description(5, true) =~ "Excellent"
end
test "returns unknown text for mismatched (score, connected) combos" do
assert Index.get_health_description(5, false) =~ "Unknown"
assert Index.get_health_description(99, true) =~ "Unknown"
end
end
describe "format_number/1" do
test "adds comma thousand separators to integers" do
assert Index.format_number(1_000) == "1,000"
assert Index.format_number(1_234_567) == "1,234,567"
end
test "leaves small integers untouched" do
assert Index.format_number(42) == "42"
assert Index.format_number(999) == "999"
end
test "handles zero and negative integers" do
assert Index.format_number(0) == "0"
# Negative number formatting: preserve the minus sign in front.
assert Index.format_number(-1_234) =~ "1,234"
end
test "converts non-integer input to string" do
assert Index.format_number(1.5) == "1.5"
assert Index.format_number(nil) == ""
assert Index.format_number(:atom) == "atom"
end
end
end