defmodule VigilWeb.EventComponentsTest do
use ExUnit.Case, async: true
import Phoenix.LiveViewTest, only: [rendered_to_string: 1]
import Phoenix.Component, only: [sigil_H: 2]
alias VigilWeb.EventComponents
defp render_event(data, opts \\ []) do
assigns = %{
event: %{data: data, tags: Keyword.get(opts, :tags, %{})},
public: Keyword.get(opts, :public, false)
}
~H"""
<EventComponents.event_body id="ev" event={@event} public={@public} />
"""
|> rendered_to_string()
|> LazyHTML.from_fragment()
end
defp count(doc, selector), do: doc |> LazyHTML.query(selector) |> Enum.count()
defp text(doc, selector), do: doc |> LazyHTML.query(selector) |> LazyHTML.text()
test "the chain shows the raised exception first, then causes" do
doc =
render_event(%{
"exception" => %{
"values" => [
%{"type" => "Root", "value" => "first"},
%{"type" => "Top", "value" => "last", "mechanism" => %{"handled" => true}}
]
}
})
assert text(doc, "#ev-exceptions-0") =~ "Top"
assert text(doc, "#ev-exceptions-0") =~ "handled"
assert text(doc, "#ev-exceptions-1") =~ "Caused by"
assert text(doc, "#ev-exceptions-1") =~ "Root"
end
test "frames: most recent first; no in-app frame means all are shown" do
frames = for n <- 1..3, do: %{"filename" => "lib#{n}.py", "lineno" => n, "in_app" => false}
doc =
render_event(%{"exception" => [%{"type" => "E", "stacktrace" => %{"frames" => frames}}]})
assert text(doc, "#ev-exceptions-0-st-f0") =~ "lib3.py"
assert text(doc, "#ev-exceptions-0-st-f2") =~ "lib1.py"
assert count(doc, "[id^='ev-exceptions-0-st-more']") == 0
end
test "raw stack trace toggle and source context with line numbers" do
frame = %{
"filename" => "app.py",
"lineno" => 10,
"colno" => 4,
"in_app" => true,
"pre_context" => ["a", "b"],
"context_line" => "boom()",
"post_context" => ["c"]
}
doc =
render_event(%{
"exception" => %{
"values" => [
%{
"type" => "E",
"stacktrace" => %{"frames" => [frame]},
"raw_stacktrace" => %{"frames" => [%{frame | "filename" => "app.min.js"}]}
}
]
}
})
assert count(doc, "#ev-exceptions-0-raw-toggle") == 1
assert text(doc, "#ev-exceptions-0-raw.hidden") =~ "app.min.js"
assert text(doc, "#ev-exceptions-0-st-f0 summary") =~ "10:4"
context = text(doc, "#ev-exceptions-0-st-f0 ol")
assert context =~ "8" and context =~ "10" and context =~ "boom()"
end
test "odd and malformed data renders without crashing" do
odd = [
%{},
%{"exception" => nil},
%{"exception" => "boom"},
%{"exception" => %{"values" => "nope"}},
%{"exception" => %{"values" => [nil, 3, %{"type" => 5, "value" => %{"a" => 1}}]}},
%{"exception" => [%{"stacktrace" => %{"frames" => [nil, "x", %{"lineno" => "12"}]}}]},
%{
"exception" => [
%{
"stacktrace" => %{
"frames" => [
%{
"in_app" => true,
"pre_context" => [nil, 1, %{"x" => 1}],
"context_line" => 7,
"vars" => %{"n" => nil, "l" => [1, %{"b" => true}], "f" => 1.5}
}
]
},
"mechanism" => "string"
}
]
},
%{
"threads" => %{
"values" => [
%{
"id" => 1,
"crashed" => true,
"stacktrace" => %{"frames" => [%{"function" => "run"}]}
},
%{"id" => 2, "stacktrace" => %{"frames" => [%{}]}}
]
}
},
%{"threads" => [1, 2]},
%{"stacktrace" => %{"frames" => [%{"filename" => "top.c"}]}},
%{"logentry" => %{"message" => "hi %s", "params" => ["x"]}},
%{"logentry" => 42, "message" => %{"formatted" => "nested"}},
%{"breadcrumbs" => [%{"timestamp" => 1_700_000_000.5, "data" => [1, 2]}, nil, "x"]},
%{"breadcrumbs" => %{"values" => [%{"timestamp" => "garbage", "level" => 3}]}},
%{
"request" => %{
"headers" => %{"A" => "b"},
"data" => "raw body",
"query_string" => [["a", "1"]]
}
},
%{
"request" => %{"headers" => [["only-one"], "x", nil], "env" => "nope", "cookies" => "a=b"}
},
%{"user" => %{"geo" => %{"city" => "Oslo"}}, "extra" => %{"n" => nil, "t" => true}},
%{"contexts" => %{"os" => "not a map", "gpu" => %{"name" => 1}}},
%{"sdk" => %{"name" => nil, "integrations" => ["a"]}, "modules" => %{"x" => "1.0"}},
%{"fingerprint" => ["{{ default }}", 1, nil]},
%{"csp" => %{"blocked_uri" => "https://evil.test", "effective_directive" => "script-src"}},
%{"errors" => [%{"type" => "invalid_data", "name" => "timestamp"}, "plain", nil]}
]
for data <- odd do
doc = render_event(data, tags: %{"k" => 1})
assert count(doc, "#ev") == 1, "failed on #{inspect(data)}"
assert count(render_event(data, public: true), "#ev") == 1
end
end
test "threads: the crashed thread is selected" do
doc =
render_event(%{
"threads" => %{
"values" => [
%{
"id" => 1,
"name" => "main",
"stacktrace" => %{"frames" => [%{"function" => "idle"}]}
},
%{
"id" => 2,
"name" => "worker",
"crashed" => true,
"stacktrace" => %{"frames" => [%{"function" => "crash_here"}]}
}
]
}
})
assert count(doc, "#ev-threads-t0.hidden") == 1
assert text(doc, "#ev-threads-t1") =~ "crash_here"
assert count(doc, "#ev-threads-t1.hidden") == 0
end
test "CSP reports, processing errors and the message" do
doc =
render_event(%{
"csp" => %{"blocked_uri" => "https://evil.test"},
"errors" => [%{"type" => "invalid_data", "name" => "timestamp"}],
"logentry" => %{"formatted" => "hello there"}
})
assert text(doc, "#ev-csp") =~ "https://evil.test"
assert text(doc, "#ev-errors") =~ "invalid_data"
assert text(doc, "#ev-message") =~ "hello there"
end
test "relative_time" do
now = DateTime.utc_now()
assert EventComponents.relative_time(now) == "just now"
assert EventComponents.relative_time(DateTime.add(now, -2, :hour)) == "2h ago"
assert EventComponents.relative_time(DateTime.add(now, -3, :day)) == "3d ago"
assert EventComponents.relative_time(nil) == "—"
assert EventComponents.relative_time("x") == "—"
end
end
neiam
/vigil
public · Issues · Pulls · Labels · Forks · Compare · Actions running · Badges · Packages
⭐
Log in to mark this repository.
6.6 KiB · text
History
18b6032