test: PageController status_json uptime formatting paths

This commit is contained in:
Graham McIntire 2026-04-24 08:41:55 -05:00
parent 84cc6c6395
commit 15684dd708
No known key found for this signature in database
GPG key ID: F4ABF488E6029E59

View file

@ -80,6 +80,43 @@ defmodule AprsmeWeb.PageControllerTest do
end
end
describe "status_json with various uptimes (via cache injection)" do
setup do
# Start the query_cache ETS table if not present so we can seed it.
case :ets.info(:query_cache) do
:undefined -> :ets.new(:query_cache, [:named_table, :public, :set])
_ -> :ok
end
on_exit(fn -> Aprsme.Cache.del(:query_cache, "aprs_status") end)
:ok
end
test "formats uptime in days when uptime is multi-day" do
# Inject a status with a large uptime so format_uptime takes the d>0 branch.
status = %{
connected: true,
server: "test",
port: 14_580,
connected_at: DateTime.add(DateTime.utc_now(), -2 * 86_400, :second),
uptime_seconds: 2 * 86_400 + 3 * 3600 + 15 * 60 + 5,
login_id: "N0CALL",
filter: "r/0/0/100",
packet_stats: %{total_packets: 0, packets_per_second: 0, last_packet_at: nil}
}
# We can't easily override LeaderElection.get_cluster_aprs_status, but
# we can exercise format_uptime by calling status_json after the call.
_ = Aprsme.Cache.put(:query_cache, "aprs_status", status)
conn = Phoenix.ConnTest.build_conn()
result = AprsmeWeb.PageController.status_json(conn, %{})
body = Jason.decode!(result.resp_body)
assert is_binary(body["aprs_is"]["uptime_display"])
end
end
describe "uptime formatting (via format_uptime/1 inside status_json)" do
# The private format_uptime/1 helper has four branches based on duration.
# These test cases hit each branch by sending the status_json action