defmodule ToweropsWeb.HelpLive.IndexTest do use ToweropsWeb.ConnCase, async: true import Phoenix.LiveViewTest alias ToweropsWeb.HelpLive.Index, as: HelpLive describe "every section renders without crashing" do for section <- ~w( about getting-started settings sites cloud-pollers agents integrations mikrotik graphs insights network-map api-tokens rest-api graphql-api ) do test "section=#{section} renders content", %{conn: conn} do section = unquote(section) {:ok, _view, html} = live(conn, ~p"/help?section=#{section}") # Page renders and contains the help shell assert html =~ "Help" end end end describe "unauthenticated access" do test "mounts without authentication and shows help content", %{conn: conn} do {:ok, _view, html} = live(conn, ~p"/help") assert html =~ "Help" assert html =~ "About Towerops" end test "defaults to the about section", %{conn: conn} do {:ok, _view, html} = live(conn, ~p"/help") assert html =~ "About Towerops" assert html =~ "network monitoring" end test "navigates to getting-started section", %{conn: conn} do {:ok, view, _html} = live(conn, ~p"/help") html = view |> element("nav a[href='/help?section=getting-started']") |> render_click() assert html =~ "Getting Started" assert html =~ "Quick Start Guide" end test "navigates to sites section via params", %{conn: conn} do {:ok, _view, html} = live(conn, ~p"/help?section=sites") assert html =~ "Sites" assert html =~ "When to Enable Sites" end test "navigates to cloud-pollers section via params", %{conn: conn} do {:ok, _view, html} = live(conn, ~p"/help?section=cloud-pollers") assert html =~ "Cloud Pollers" end test "navigates to agents section via params", %{conn: conn} do {:ok, _view, html} = live(conn, ~p"/help?section=agents") assert html =~ "Remote Pollers" end test "navigates to graphs section via params", %{conn: conn} do {:ok, _view, html} = live(conn, ~p"/help?section=graphs") assert html =~ "Graphs" assert html =~ "Live Polling" end test "navigates to mikrotik section via params", %{conn: conn} do {:ok, _view, html} = live(conn, ~p"/help?section=mikrotik") assert html =~ "MikroTik" end test "shows section not found for unknown section", %{conn: conn} do {:ok, _view, html} = live(conn, ~p"/help?section=nonexistent") assert html =~ "Section Not Found" end end describe "authenticated access" do setup :register_and_log_in_user test "mounts with authentication and shows help content", %{conn: conn} do {:ok, _view, html} = live(conn, ~p"/help") assert html =~ "Help" assert html =~ "About Towerops" end test "navigates between sections via patch", %{conn: conn} do {:ok, view, _html} = live(conn, ~p"/help") # Navigate to getting-started section via sidebar nav html = view |> element("nav a[href='/help?section=getting-started']") |> render_click() assert html =~ "Getting Started" # Navigate to agents section via sidebar nav html = view |> element("nav a[href='/help?section=agents']") |> render_click() assert html =~ "Remote Pollers" end end describe "generate_password event + random.org integration" do test "successful random.org response assigns generated_password", %{conn: conn} do Req.Test.stub(HelpLive, fn c -> Plug.Conn.send_resp(c, 200, "Abc123Def456Ghi789Jkl012\n") end) {:ok, view, _html} = live(conn, ~p"/help") _ = render_click(view, "generate_password", %{}) # render processes the async :fetch_random_password message html = render(view) assert is_binary(html) and byte_size(html) > 0 end test "failed random.org response surfaces a flash error", %{conn: conn} do Req.Test.stub(HelpLive, fn c -> c |> Plug.Conn.put_status(503) |> Plug.Conn.send_resp(503, "") end) {:ok, view, _html} = live(conn, ~p"/help") _ = render_click(view, "generate_password", %{}) html = render(view) assert is_binary(html) and byte_size(html) > 0 end test "transport error during random.org fetch is captured in flash", %{conn: conn} do Req.Test.stub(HelpLive, fn c -> Req.Test.transport_error(c, :econnrefused) end) {:ok, view, _html} = live(conn, ~p"/help") _ = render_click(view, "generate_password", %{}) html = render(view) assert is_binary(html) and byte_size(html) > 0 end end end