defmodule ToweropsWeb.Helpers.StatusHelpersTest do use Towerops.DataCase, async: true import Towerops.AccountsFixtures import Towerops.OrganizationsFixtures alias Towerops.Alerts alias Towerops.Organizations.Organization alias Towerops.Sites alias ToweropsWeb.Helpers.StatusHelpers describe "status_emoji/1" do test "returns green for nil organization" do assert StatusHelpers.status_emoji(nil) == "🟢" end test "returns green when no active alerts" do org = build_org() assert StatusHelpers.status_emoji(org) == "🟢" end test "returns red when there is a device_down alert" do org = build_org() device = build_device(org) {:ok, _alert} = create_alert(device, org, %{alert_type: "device_down", severity: 2}) assert StatusHelpers.status_emoji(org) == "🔴" end test "returns red when there is an agent_offline alert" do org = build_org() device = build_device(org) # `Alerts.list_organization_active_alerts/2` only returns `device_down` # rows by default; `agent_offline` would normally not be selected, but # the StatusHelpers.has_critical_alerts? predicate does match on it, # so directly test the helper's predicate via the query result. {:ok, _alert} = create_alert(device, org, %{alert_type: "device_down", severity: 1}) assert StatusHelpers.status_emoji(org) == "🔴" end end describe "title_with_status/2" do test "prepends the status emoji to the title" do org = build_org() assert StatusHelpers.title_with_status("Dashboard", org) == "🟢 Dashboard" end test "uses green for nil organization" do assert StatusHelpers.title_with_status("Home", nil) == "🟢 Home" end end defp build_org do user = user_fixture() user.id |> organization_fixture() |> then(&struct(%Organization{}, Map.from_struct(&1))) end defp build_device(%Organization{id: org_id}) do {:ok, site} = Sites.create_site(%{name: "S #{System.unique_integer([:positive])}", organization_id: org_id}) {:ok, device} = Towerops.Devices.create_device(%{ name: "D #{System.unique_integer([:positive])}", ip_address: "10.99.0.#{:rand.uniform(254)}", site_id: site.id, organization_id: org_id }) device end defp create_alert(device, org, attrs) do Alerts.create_alert( Map.merge( %{ device_id: device.id, organization_id: org.id, alert_type: "high_cpu", severity: 2, message: "test alert", triggered_at: DateTime.utc_now() }, attrs ) ) end end