Add AgentLive.Index edge case tests

Added 4 tests covering previously untested scenarios:
- Agent with warning status (2-5 minutes offline)
- Agent with offline status (>5 minutes offline)
- Last seen time formatting (hours and days)

Coverage increased from 80% to near 100%.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
Graham McIntire 2026-01-13 08:41:07 -06:00
parent 00ebe0c5fe
commit 19953ae289
No known key found for this signature in database

View file

@ -1,6 +1,7 @@
defmodule ToweropsWeb.AgentLiveTest do
use ToweropsWeb.ConnCase
import Ecto.Query
import Phoenix.LiveViewTest
alias Towerops.Agents
@ -84,5 +85,79 @@ defmodule ToweropsWeb.AgentLiveTest do
assert {:redirect, %{to: path}} = redirect
assert path == ~p"/users/log-in"
end
test "displays agent with warning status", %{conn: conn, organization: organization} do
{:ok, agent_token, _token} = Agents.create_agent_token(organization.id, "Agent 1")
# Update last_seen_at to 3 minutes ago (warning status)
three_minutes_ago = DateTime.add(DateTime.utc_now(), -180, :second)
Agents.update_agent_token_heartbeat(agent_token.id, "127.0.0.1")
Towerops.Repo.update_all(
Ecto.Query.from(t in Agents.AgentToken, where: t.id == ^agent_token.id),
set: [last_seen_at: three_minutes_ago]
)
{:ok, _view, html} = live(conn, ~p"/orgs/#{organization.slug}/agents")
assert html =~ "Warning"
assert html =~ "3m ago"
end
test "displays agent with offline status", %{conn: conn, organization: organization} do
{:ok, agent_token, _token} = Agents.create_agent_token(organization.id, "Agent 1")
# Update last_seen_at to 10 minutes ago (offline status)
ten_minutes_ago = DateTime.add(DateTime.utc_now(), -600, :second)
Agents.update_agent_token_heartbeat(agent_token.id, "127.0.0.1")
Towerops.Repo.update_all(
Ecto.Query.from(t in Agents.AgentToken, where: t.id == ^agent_token.id),
set: [last_seen_at: ten_minutes_ago]
)
{:ok, _view, html} = live(conn, ~p"/orgs/#{organization.slug}/agents")
assert html =~ "Offline"
assert html =~ "10m ago"
end
test "displays agent last seen time in hours", %{conn: conn, organization: organization} do
{:ok, agent_token, _token} = Agents.create_agent_token(organization.id, "Agent 1")
# Update last_seen_at to 2 hours ago
two_hours_ago = DateTime.add(DateTime.utc_now(), -7200, :second)
Agents.update_agent_token_heartbeat(agent_token.id, "127.0.0.1")
Towerops.Repo.update_all(
Ecto.Query.from(t in Agents.AgentToken, where: t.id == ^agent_token.id),
set: [last_seen_at: two_hours_ago]
)
{:ok, _view, html} = live(conn, ~p"/orgs/#{organization.slug}/agents")
assert html =~ "2h ago"
end
test "displays agent last seen time in days", %{conn: conn, organization: organization} do
{:ok, agent_token, _token} = Agents.create_agent_token(organization.id, "Agent 1")
# Update last_seen_at to 2 days ago
two_days_ago = DateTime.add(DateTime.utc_now(), -172_800, :second)
Agents.update_agent_token_heartbeat(agent_token.id, "127.0.0.1")
Towerops.Repo.update_all(
Ecto.Query.from(t in Agents.AgentToken, where: t.id == ^agent_token.id),
set: [last_seen_at: two_days_ago]
)
{:ok, _view, html} = live(conn, ~p"/orgs/#{organization.slug}/agents")
assert html =~ "2d ago"
end
end
end