From 19953ae2897a6ab504e50538759266e732d80b79 Mon Sep 17 00:00:00 2001 From: Graham McIntire Date: Tue, 13 Jan 2026 08:41:07 -0600 Subject: [PATCH] Add AgentLive.Index edge case tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- test/towerops_web/live/agent_live_test.exs | 75 ++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/test/towerops_web/live/agent_live_test.exs b/test/towerops_web/live/agent_live_test.exs index f1416383..cc1e45c0 100644 --- a/test/towerops_web/live/agent_live_test.exs +++ b/test/towerops_web/live/agent_live_test.exs @@ -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