2.3 KiB · text History 5944a1f
defmodule Mix.Tasks.Vigil.TestTaskTest do
  use Vigil.DataCase, async: false

  import ExUnit.CaptureIO
  import Ecto.Query
  import Vigil.AccountsFixtures
  import Vigil.OrganizationsFixtures

  alias Vigil.Issues.Issue

  setup do
    Mix.shell(Mix.Shell.IO)
    on_exit(fn -> Mix.shell(Mix.Shell.IO) end)
    owner = user_fixture()
    org = organization_fixture(owner)
    project = project_fixture(org_scope(owner, org), %{"name" => "web"})
    %{org: org, project: project}
  end

  defp issues(project),
    do: Repo.all(from i in Issue, where: i.project_id == ^project.id, order_by: i.short_id)

  test "an error and a message become two issues; again, their counts go up", %{
    org: org,
    project: project
  } do
    out = capture_io(fn -> Mix.Tasks.Vigil.Test.run([org.slug, "web"]) end)
    assert out =~ "Sent 2 item(s)"
    assert [%{type: :error, count: 1}, %{type: :default, count: 1}] = issues(project)

    capture_io(fn ->
      Mix.Tasks.Vigil.Test.run([org.slug, "web", "--kind", "error", "--count", "3"])
    end)

    assert [%{type: :error, count: 4}, %{count: 1}] = issues(project)
  end

  test "--kind transaction sends traced requests to three endpoints", %{
    org: org,
    project: project
  } do
    out =
      capture_io(fn ->
        Mix.Tasks.Vigil.Test.run([org.slug, "web", "--kind", "transaction", "--count", "6"])
      end)

    assert out =~ "Sent 6 item(s)"
    groups = Vigil.Performance.list_groups(project.id)
    assert groups |> Enum.map(& &1.stats.count) |> Enum.sort() == [2, 2, 2]
    assert Enum.all?(groups, &(&1.op == "http.server"))
    assert Repo.aggregate(Vigil.Performance.Span, :count) > 6
  end

  test "--unique makes new issues each run", %{org: org, project: project} do
    capture_io(fn ->
      Mix.Tasks.Vigil.Test.run([org.slug, "web", "--kind", "error", "--unique"])
    end)

    capture_io(fn ->
      Mix.Tasks.Vigil.Test.run([org.slug, "web", "--kind", "error", "--unique"])
    end)

    assert [_, _] = issues(project)
  end

  test "unknown org or project says so", %{org: org} do
    assert_raise Mix.Error, ~r/No organization/, fn ->
      Mix.Tasks.Vigil.Test.run(["nope", "web"])
    end

    assert_raise Mix.Error, ~r/No project/, fn -> Mix.Tasks.Vigil.Test.run([org.slug, "nope"]) end
    assert_raise Mix.Error, ~r/Usage/, fn -> Mix.Tasks.Vigil.Test.run([]) end
  end
end