defmodule ToweropsWeb.DashboardLiveTest do use ToweropsWeb.ConnCase import Phoenix.LiveViewTest setup :register_and_log_in_user setup %{conn: conn, user: user} do {:ok, organization} = Towerops.Organizations.create_organization(%{name: "Test Org", use_sites: true}, user.id) {:ok, site} = Towerops.Sites.create_site(%{ name: "Test Site", organization_id: organization.id }) # Set the organization in session so dashboard can load conn = Plug.Conn.put_session(conn, :current_organization_id, organization.id) %{conn: conn, organization: organization, site: site} end describe "Dashboard" do test "displays organization name and stats", %{conn: conn, organization: organization, site: site} do # Create a device so the dashboard shows stats instead of empty state {:ok, _device} = Towerops.Devices.create_device(%{ name: "Test Device", ip_address: "192.168.1.1", site_id: site.id, organization_id: organization.id }) {:ok, _view, html} = live(conn, ~p"/dashboard") assert html =~ organization.name assert html =~ "Sites" assert html =~ "Device" assert html =~ "Active Alerts" end test "displays correct site count", %{conn: conn, organization: organization} do # Create additional sites {:ok, _site2} = Towerops.Sites.create_site(%{ name: "Site 2", organization_id: organization.id }) {:ok, _view, html} = live(conn, ~p"/dashboard") assert html =~ "Sites" end test "displays correct device counts", %{conn: conn, organization: organization, site: site} do # Create devices with different statuses {:ok, device_up} = Towerops.Devices.create_device(%{ name: "Router 1", ip_address: "192.168.1.1", site_id: site.id, organization_id: organization.id, monitoring_enabled: true }) Towerops.Devices.update_device_status(device_up, :up) {:ok, device_down} = Towerops.Devices.create_device(%{ name: "Router 2", ip_address: "192.168.1.2", site_id: site.id, organization_id: organization.id, monitoring_enabled: true }) Towerops.Devices.update_device_status(device_down, :down) {:ok, _view, html} = live(conn, ~p"/dashboard") assert html =~ "Device" assert html =~ "Up" assert html =~ "Down" end test "displays active alerts count", %{conn: conn, organization: organization, site: site} do {:ok, device} = Towerops.Devices.create_device(%{ name: "Router 1", ip_address: "192.168.1.1", site_id: site.id, organization_id: organization.id }) # Create an active alert {:ok, _alert} = Towerops.Alerts.create_alert(%{ device_id: device.id, alert_type: :device_down, triggered_at: DateTime.utc_now(), message: "Device is down" }) {:ok, _view, html} = live(conn, ~p"/dashboard") assert html =~ "Active Alerts" end test "updates in real-time when new alert is triggered", %{ conn: conn, organization: organization, site: site } do {:ok, device} = Towerops.Devices.create_device(%{ name: "Router 1", ip_address: "192.168.1.1", site_id: site.id, organization_id: organization.id }) {:ok, view, _html} = live(conn, ~p"/dashboard") # Trigger a new alert via PubSub Phoenix.PubSub.broadcast( Towerops.PubSub, "alerts:new", {:new_alert, device.id, :device_down} ) # View should update assert render(view) end test "stat cards are clickable links", %{conn: conn} do {:ok, _view, html} = live(conn, ~p"/dashboard") # Check for navigation links assert html =~ ~p"/sites" assert html =~ ~p"/devices" assert html =~ ~p"/alerts" end test "requires authentication" do conn = build_conn() {:error, redirect} = live(conn, ~p"/dashboard") assert {:redirect, %{to: path}} = redirect assert path == ~p"/users/log-in" end end end