defmodule VigilWeb.IssueLive.IndexTest do
use VigilWeb.ConnCase, async: true
import Phoenix.LiveViewTest
import Vigil.AccountsFixtures
import Vigil.OrganizationsFixtures
alias Vigil.Ingest
alias Vigil.Ingest.{Drain, Envelope}
alias Vigil.Issues
alias VigilWeb.IssueListComponents
setup :register_and_log_in_user
setup %{user: user} do
org = organization_fixture(user)
scope = org_scope(user, org)
project = project_fixture(scope)
target = %Ingest.Target{
project_id: project.id,
dsn_id: project.dsn_id,
organization_id: org.id,
key_id: Ecto.UUID.generate()
}
path = ~p"/orgs/#{org.slug}/projects/#{project.slug}"
%{org: org, scope: scope, project: project, target: target, path: path}
end
defp send_event(target, type, extra \\ %{}) do
event =
Map.merge(
%{
"event_id" => Vigil.Schema.uuid7() |> String.replace("-", ""),
"exception" => [%{"type" => type, "value" => "v"}],
"platform" => "python"
},
extra
)
item = %Envelope.Item{type: "event", headers: %{}, payload: Jason.encode!(event)}
Ingest.accept(target, %{headers: %{}, items: [item]}, %{})
Drain.drain(target.project_id)
end
defp issue(scope, project, title) do
%{issues: issues} = Issues.search(scope, project.id, %{status: :all, text: title})
Enum.find(issues, &(&1.title == title))
end
defp row(issue), do: "#issues-#{issue.id}"
test "a project without events shows its DSN and setup", %{conn: conn, path: path} do
{:ok, view, _html} = live(conn, path)
assert has_element?(view, "#setup #dsn")
assert has_element?(view, "#copy-dsn")
assert has_element?(view, "#snippet")
assert has_element?(view, "#setup-guide")
refute has_element?(view, "#issues")
end
test "issues are listed once events arrive", ctx do
%{conn: conn, path: path, target: target, scope: scope, project: project} = ctx
send_event(target, "KeyError")
send_event(target, "ValueError", %{"level" => "warning"})
{:ok, view, _html} = live(conn, path)
refute has_element?(view, "#setup")
key_error = issue(scope, project, "KeyError: v")
assert has_element?(view, row(key_error))
assert has_element?(view, "#link-issues-#{key_error.id}", "KeyError: v")
assert has_element?(view, "#spark-issues-#{key_error.id}[role=img]")
assert has_element?(view, row(key_error), "#{String.upcase(project.slug)}-")
assert has_element?(view, "#keyboard-hint")
assert has_element?(view, "#issue-list[phx-hook]")
end
test "status tabs, search and environment change the list by patch", ctx do
%{conn: conn, path: path, target: target, scope: scope, project: project} = ctx
send_event(target, "KeyError", %{"environment" => "production"})
send_event(target, "ValueError", %{"environment" => "staging"})
key_error = issue(scope, project, "KeyError: v")
value_error = issue(scope, project, "ValueError: v")
{:ok, _} = Issues.resolve(scope, value_error)
{:ok, view, _html} = live(conn, path)
assert has_element?(view, row(key_error))
refute has_element?(view, row(value_error))
view |> element("#status-resolved") |> render_click()
assert_patch(view, path <> "?status=resolved")
assert has_element?(view, row(value_error))
refute has_element?(view, row(key_error))
assert has_element?(view, "#status-resolved.tab-active")
view |> element("#status-all") |> render_click()
assert_patch(view, path <> "?status=all")
assert has_element?(view, row(key_error))
assert has_element?(view, row(value_error))
view |> form("#issue-search", search: %{q: "KeyError"}) |> render_submit()
assert_patch(view, path <> "?q=KeyError&status=all")
assert has_element?(view, row(key_error))
refute has_element?(view, row(value_error))
view |> form("#issue-search", search: %{q: ""}) |> render_submit()
assert_patch(view, path <> "?status=all")
view |> form("#issue-filters", filters: %{env: "staging"}) |> render_change()
assert_patch(view, path <> "?env=staging&status=all")
assert has_element?(view, row(value_error))
refute has_element?(view, row(key_error))
{:ok, view, _html} = live(conn, path <> "?env=nowhere")
assert has_element?(view, "#filter-env option[value=nowhere][selected]")
assert has_element?(view, "#issues-empty")
end
test "the search box's is: works, and an explicit status wins", ctx do
%{conn: conn, path: path, target: target, scope: scope, project: project} = ctx
send_event(target, "KeyError")
key_error = issue(scope, project, "KeyError: v")
{:ok, _} = Issues.ignore(scope, key_error)
{:ok, view, _html} = live(conn, path <> "?q=is:ignored")
assert has_element?(view, row(key_error))
assert has_element?(view, "#status-ignored.tab-active")
{:ok, view, _html} = live(conn, path <> "?q=is:ignored&status=unresolved")
refute has_element?(view, row(key_error))
end
test "sort and period are kept in the URL", %{conn: conn, path: path, target: target} do
send_event(target, "KeyError")
{:ok, view, _html} = live(conn, path)
view |> form("#issue-filters", filters: %{sort: "count", period: "24h"}) |> render_change()
assert_patch(view, path <> "?period=24h&sort=count")
assert has_element?(view, "#filter-sort option[value=count][selected]")
end
test "bulk resolve takes the selected rows out of the unresolved view", ctx do
%{conn: conn, path: path, target: target, scope: scope, project: project} = ctx
send_event(target, "KeyError")
send_event(target, "ValueError")
key_error = issue(scope, project, "KeyError: v")
value_error = issue(scope, project, "ValueError: v")
{:ok, view, _html} = live(conn, path)
refute has_element?(view, "#bulk-actions")
view |> element("#select-issues-#{key_error.id}") |> render_click()
assert has_element?(view, "#bulk-actions")
assert has_element?(view, "#selected-count", "1 selected")
assert has_element?(view, "#bulk-delete[data-confirm]")
view |> element("#bulk-resolve") |> render_click()
refute has_element?(view, row(key_error))
assert has_element?(view, row(value_error))
refute has_element?(view, "#bulk-actions")
assert render(view) =~ "1 issue resolved."
assert issue(scope, project, "KeyError: v").status == :resolved
end
test "select all, then assign to me and delete", ctx do
%{conn: conn, path: path, target: target, scope: scope, project: project, user: user} = ctx
send_event(target, "KeyError")
send_event(target, "ValueError")
{:ok, view, _html} = live(conn, path)
view |> element("#select-all") |> render_click()
assert has_element?(view, "#selected-count", "2 selected")
assert has_element?(view, "#select-all[checked]")
view |> element("#bulk-assign-me") |> render_click()
assert issue(scope, project, "KeyError: v").assignee_id == user.id
view |> element("#select-all") |> render_click()
view |> element("#bulk-delete") |> render_click()
assert has_element?(view, "#issues-empty")
assert Issues.search(scope, project.id, %{status: :all}).issues == []
end
test "Delete shows only to callers with project:admin" do
html = render_component(&IssueListComponents.bulk_actions/1, count: 2, can_delete: false)
doc = LazyHTML.from_fragment(html)
assert LazyHTML.query(doc, "#bulk-resolve") |> Enum.count() == 1
assert LazyHTML.query(doc, "#bulk-delete") |> Enum.empty?()
html = render_component(&IssueListComponents.bulk_actions/1, count: 2, can_delete: true)
assert LazyHTML.from_fragment(html) |> LazyHTML.query("#bulk-delete") |> Enum.count() == 1
end
test "a manager can triage, Delete included", ctx do
%{org: org, target: target, path: path} = ctx
manager = user_fixture()
member_fixture(org, manager, :manager)
send_event(target, "KeyError")
{:ok, view, _html} = live(log_in_user(build_conn(), manager), path)
view |> element("#select-all") |> render_click()
assert has_element?(view, "#bulk-resolve")
assert has_element?(view, "#bulk-delete")
end
test "a new event's issue appears live", ctx do
%{conn: conn, path: path, target: target, scope: scope, project: project} = ctx
send_event(target, "KeyError")
{:ok, view, _html} = live(conn, path)
Phoenix.PubSub.subscribe(Vigil.PubSub, "project:#{project.id}:issues")
send_event(target, "ValueError")
assert_receive {:issue_event, %{new: true}}
# The view got the same broadcast and waits out its coalescing tick;
# skip the wait.
_ = :sys.get_state(view.pid)
send(view.pid, :refresh)
assert has_element?(view, row(issue(scope, project, "ValueError: v")))
end
test "the setup panel gives way when the first event arrives", ctx do
%{conn: conn, path: path, target: target, scope: scope, project: project} = ctx
{:ok, view, _html} = live(conn, path)
assert has_element?(view, "#dsn")
send_event(target, "KeyError")
send(view.pid, {:issue_event, %{issue_id: nil, event_id: nil, new: true}})
send(view.pid, :refresh)
refute has_element?(view, "#dsn")
assert has_element?(view, row(issue(scope, project, "KeyError: v")))
end
test "pages of 50", %{conn: conn, path: path, target: target} do
for n <- 1..51, do: send_event(target, "Error#{n}")
{:ok, view, _html} = live(conn, path)
refute has_element?(view, "#page-prev")
assert has_element?(view, "#page-next")
view |> element("#page-next") |> render_click()
assert_patch(view, path <> "?page=2")
assert has_element?(view, "#page-prev")
refute has_element?(view, "#page-next")
html = render(view)
rows = html |> LazyHTML.from_fragment() |> LazyHTML.query("#issues li[data-issue-id]")
assert Enum.count(rows) == 1
view |> element("#page-prev") |> render_click()
assert_patch(view, path)
end
test "keyboard: r resolves and i ignores the focused issue", ctx do
%{conn: conn, path: path, target: target, scope: scope, project: project} = ctx
send_event(target, "KeyError")
send_event(target, "ValueError")
key_error = issue(scope, project, "KeyError: v")
value_error = issue(scope, project, "ValueError: v")
{:ok, view, _html} = live(conn, path)
view |> element("#issue-list") |> render_hook("resolve_issue", %{"id" => key_error.id})
refute has_element?(view, row(key_error))
assert issue(scope, project, "KeyError: v").status == :resolved
view |> element("#issue-list") |> render_hook("ignore_issue", %{"id" => value_error.id})
refute has_element?(view, row(value_error))
assert issue(scope, project, "ValueError: v").status == :ignored
view |> element("#issue-list") |> render_hook("ignore_issue", %{"id" => "nope"})
assert render(view) =~ "Couldn't change that issue."
end
describe "helpers" do
test "ago and compact" do
now = ~U[2026-01-10 12:00:00Z]
assert IssueListComponents.ago(~U[2026-01-10 11:59:50Z], now) == "just now"
assert IssueListComponents.ago(~U[2026-01-10 11:55:00Z], now) == "5m ago"
assert IssueListComponents.ago(~U[2026-01-10 09:00:00Z], now) == "3h ago"
assert IssueListComponents.ago(~U[2026-01-08 12:00:00Z], now) == "2d ago"
assert IssueListComponents.ago(~U[2025-06-01 12:00:00Z], now) == "Jun 1, 2025"
assert IssueListComponents.compact(999) == "999"
assert IssueListComponents.compact(1234) == "1.2k"
assert IssueListComponents.compact(45_000) == "45k"
assert IssueListComponents.compact(1_500_000) == "1.5M"
end
end
end
neiam
/vigil
public · Issues · Pulls · Labels · Forks · Compare · Actions queued · Badges · Packages
⭐
Log in to mark this repository.
11.4 KiB · text
History
573fef1