defmodule GitGudWeb.SymsLive.TopTest do
use GitGudWeb.ConnCase, async: false
import Phoenix.LiveViewTest
import GitGud.AccountsFixtures
import GitGud.ForgeFixtures
alias GitGud.Syms
setup do
on_exit(fn ->
Application.delete_env(:git_gud, :syms)
Application.delete_env(:git_gud, :syms_top_limit)
end)
Application.put_env(:git_gud, :syms, ["⭐", "🚀"])
:ok
end
defp mark(repo, symbol, n) do
for _ <- 1..n do
{:ok, :marked} = Syms.toggle(user_fixture(), repo, symbol)
end
end
test "ranks the most-marked repositories" do
{_u, popular} = repository_fixture(%{visibility: "public", name: "popular"})
{_u, quiet} = repository_fixture(%{visibility: "public", name: "quiet"})
mark(popular, "⭐", 3)
mark(quiet, "⭐", 1)
{:ok, _lv, html} = live(build_conn(), ~p"/syms")
assert html =~ "popular"
assert html =~ "quiet"
# Most-marked first.
assert :binary.match(html, "popular") < :binary.match(html, "quiet")
end
test "an anonymous visitor sees only public repos" do
{owner, private} = repository_fixture(%{visibility: "private", name: "hushhush"})
mark(private, "⭐", 2)
{:ok, _lv, html} = live(build_conn(), ~p"/syms")
refute html =~ "hushhush"
# …and the owner does see it.
conn = log_in_user(build_conn(), owner)
{:ok, _lv, html} = live(conn, ~p"/syms")
assert html =~ "hushhush"
end
test "internal repos appear only once logged in" do
{_u, internal} = repository_fixture(%{visibility: "internal", name: "staffonly"})
mark(internal, "🚀", 1)
{:ok, _lv, html} = live(build_conn(), ~p"/syms")
refute html =~ "staffonly"
conn = log_in_user(build_conn(), user_fixture())
{:ok, _lv, html} = live(conn, ~p"/syms")
assert html =~ "staffonly"
end
test "hidden repos don't consume slots in the top N" do
Application.put_env(:git_gud, :syms_top_limit, 2)
{_u, secret} = repository_fixture(%{visibility: "private", name: "secretive"})
{_u, a} = repository_fixture(%{visibility: "public", name: "alpharepo"})
{_u, b} = repository_fixture(%{visibility: "public", name: "betarepo"})
# The private repo outranks both, so a naive filter-after-limit
# would leave an anonymous visitor with only one row.
mark(secret, "⭐", 5)
mark(a, "⭐", 3)
mark(b, "⭐", 2)
{:ok, _lv, html} = live(build_conn(), ~p"/syms")
refute html =~ "secretive"
assert html =~ "alpharepo"
assert html =~ "betarepo"
end
test "the per-symbol page ranks only that symbol" do
{_u, starred} = repository_fixture(%{visibility: "public", name: "starrepo"})
{_u, rocketed} = repository_fixture(%{visibility: "public", name: "rocketrepo"})
mark(starred, "⭐", 2)
mark(rocketed, "🚀", 2)
{:ok, _lv, html} = live(build_conn(), ~p"/syms/#{"⭐"}")
assert html =~ "starrepo"
refute html =~ "rocketrepo"
end
test "an unconfigured symbol redirects to the index" do
assert {:error, {:live_redirect, %{to: "/syms"}}} = live(build_conn(), ~p"/syms/#{"🍕"}")
end
describe "the Explore nav dropdown" do
test "carries a link to /syms, for anonymous visitors too" do
{:ok, lv, _html} = live(build_conn(), ~p"/syms")
assert lv |> element("#nav-explore") |> has_element?()
assert lv |> element(~s{#nav-explore ~ ul a[href="/syms"]}) |> has_element?()
end
test "sits to the right of Orgs when signed in", %{} do
conn = log_in_user(build_conn(), user_fixture())
{:ok, _lv, html} = live(conn, ~p"/repositories")
assert :binary.match(html, "nav-orgs") < :binary.match(html, "nav-explore")
end
test "still shows for anonymous visitors, who have no Orgs" do
{:ok, lv, html} = live(build_conn(), ~p"/repositories")
refute html =~ "nav-orgs"
assert lv |> element("#nav-explore") |> has_element?()
end
test "drops Syms but keeps Tags when syms are disabled" do
# Tags is unconditional, so the dropdown no longer disappears
# when syms are switched off — it just loses that entry.
Application.put_env(:git_gud, :syms, [])
{:ok, lv, html} = live(build_conn(), ~p"/repositories")
assert lv |> element("#nav-explore") |> has_element?()
assert html =~ ~s(href="/tags")
refute html =~ ~s(href="/syms")
end
end
test "no configured symbols disables the page" do
Application.put_env(:git_gud, :syms, [])
{:ok, _lv, html} = live(build_conn(), ~p"/syms")
assert html =~ "syms are disabled"
end
test "the limit comes from config" do
Application.put_env(:git_gud, :syms_top_limit, 1)
{_u, a} = repository_fixture(%{visibility: "public", name: "firstrepo"})
{_u, b} = repository_fixture(%{visibility: "public", name: "secondrepo"})
mark(a, "⭐", 2)
mark(b, "⭐", 1)
{:ok, _lv, html} = live(build_conn(), ~p"/syms")
assert html =~ "firstrepo"
refute html =~ "secondrepo"
assert html =~ "Showing the top 1"
end
end
neiam /gitgud
Git Gud
public · Issues · Pulls · Labels · Forks · Compare · Actions success · Packages
⭐
Log in to mark this repository.
4.9 KiB · text
History
6280797