3.3 KiB · text History bbb2a48
defmodule VigilWeb.TraceLiveTest do
  use VigilWeb.ConnCase, async: true

  import Phoenix.LiveViewTest
  import Vigil.AccountsFixtures
  import Vigil.OrganizationsFixtures
  import Vigil.PerformanceFixtures

  alias Vigil.Issues.IssueEvent
  alias Vigil.Repo

  setup :register_and_log_in_user

  setup %{user: user} do
    org = organization_fixture(user)
    project = project_fixture(org_scope(user, org), %{"name" => "web"})
    %{org: org, project: project}
  end

  test "the waterfall: a row per span, by depth, expandable", ctx do
    %{conn: conn, org: org, project: project} = ctx
    trace = transaction_fixture(project, ms: 120)

    {:ok, view, _} = live(conn, ~p"/orgs/#{org.slug}/traces/#{trace}")
    assert has_element?(view, "#trace-id", trace)
    assert has_element?(view, "#trace-duration", "120ms")
    assert has_element?(view, "#trace-span-count", "3")

    rows =
      view
      |> render()
      |> LazyHTML.from_fragment()
      |> LazyHTML.query("#spans > li[data-depth]")

    assert LazyHTML.attribute(rows, "data-depth") == ["0", "1", "2"]

    [root_id, db_id, _] = LazyHTML.attribute(rows, "id")
    assert has_element?(view, "##{root_id} .span-op", "http.server")
    assert has_element?(view, "##{root_id} .span-desc", "GET /users/:id")
    assert has_element?(view, "##{db_id} .span-desc", "SELECT * FROM users")
    assert has_element?(view, "##{db_id} .span-duration", "6.0ms")
    assert has_element?(view, "##{db_id}-detail.hidden", "postgresql")
    assert has_element?(view, "##{db_id}-row[aria-controls='#{db_id}-detail']")
    refute has_element?(view, "#trace-errors")
  end

  test "lists the trace's errors, linked to their events", ctx do
    %{conn: conn, org: org, project: project} = ctx
    trace = transaction_fixture(project)
    trace_error_fixture(project, trace, "kaboom")
    event = Repo.get_by!(IssueEvent, trace_id: trace) |> Repo.preload(:issue)

    {:ok, view, _} = live(conn, ~p"/orgs/#{org.slug}/traces/#{trace}")
    assert has_element?(view, "#trace-error-#{event.id}", "kaboom")

    assert has_element?(
             view,
             "#trace-error-#{event.id} a[href='/orgs/#{org.slug}/projects/web/issues/#{event.issue.short_id}/events/#{event.id}']"
           )
  end

  test "an unknown trace is not found", %{conn: conn, org: org} do
    {:ok, view, _} = live(conn, ~p"/orgs/#{org.slug}/traces/#{trace_id()}")
    assert has_element?(view, "#trace-missing")
  end

  test "another organization's trace is not visible", %{conn: conn, org: org} do
    owner = user_fixture()
    other = organization_fixture(owner)
    hidden = project_fixture(org_scope(owner, other))
    trace = transaction_fixture(hidden)

    {:ok, view, _} = live(conn, ~p"/orgs/#{org.slug}/traces/#{trace}")
    assert has_element?(view, "#trace-missing")

    assert {:error, {:live_redirect, %{to: "/orgs"}}} =
             live(conn, ~p"/orgs/#{other.slug}/traces/#{trace}")
  end

  test "spans in a project the member can't see are left out", %{conn: conn, user: user} do
    owner = user_fixture()
    other = organization_fixture(owner)
    hidden = project_fixture(org_scope(owner, other))
    member_fixture(other, user, :member)
    trace = transaction_fixture(hidden)

    {:ok, view, _} = live(conn, ~p"/orgs/#{other.slug}/traces/#{trace}")
    assert has_element?(view, "#trace-missing")
  end
end