Files
owenrees 80cb033077 Add theme slugs, kinds and link provenance (#192)
* add function for upgrading a user's plan in interactive Elixir

* Add theme slugs, kinds and link provenance

Themes gain a stable slug, an optional description and a kind so the app can
tell material, motif and technique labels apart. Slugs are generated from the
name on first save and keep their value when the theme is renamed, so links to
a theme stay valid.

Endgame-to-theme links now record their source (derived, curated or community)
and creation time. Existing links are backfilled as curated. A derived pass can
therefore replace only its own links, leaving human work untouched.

The admin index shows each theme's kind and its coverage across the library.
Creating a theme name requires the manage_themes capability, and members get an
unauthorized error instead, ahead of the suggestion flow.

Closes #185
2026-11-17 20:46:43 +01:00

74 lines
2.5 KiB
Elixir

defmodule ChesstrainerWeb.Admin.ThemeLive.IndexTest do
use ChesstrainerWeb.ConnCase
import Phoenix.LiveViewTest
import Chesstrainer.EndgamesFixtures
import Chesstrainer.ThemesFixtures
alias Chesstrainer.Endgames
alias Chesstrainer.Repo
alias Chesstrainer.Themes
setup :register_and_log_in_admin
test "lists themes with their kind and coverage", %{conn: conn} do
used_theme = theme_fixture(%{name: "Lucena", kind: :technique})
unused_theme = theme_fixture(%{name: "Philidor"})
endgame = endgame_fixture()
{:ok, _} = Endgames.update_endgame(endgame, %{}, [used_theme])
{:ok, view, _html} = live(conn, ~p"/admin/themes")
assert has_element?(view, "#theme-#{used_theme.id}")
assert has_element?(view, "#theme-#{used_theme.id}", "technique")
assert has_element?(view, "#theme-usage-#{used_theme.id}", "1 of 1 endgame")
assert has_element?(view, "#theme-usage-#{unused_theme.id}", "0 of 1 endgame")
end
test "filters by name and category", %{conn: conn} do
theme_fixture(%{name: "Lucena", category: :endgame})
theme_fixture(%{name: "Philidor", category: :endgame})
theme_fixture(%{name: "Pin", category: :tactic})
{:ok, view, _html} = live(conn, ~p"/admin/themes")
view |> form("#theme-admin-filter-form", %{search: "Luc"}) |> render_change()
assert has_element?(view, "td", "Lucena")
refute has_element?(view, "td", "Philidor")
refute has_element?(view, "td", "Pin")
view |> form("#theme-admin-filter-form", %{search: "", category: "tactic"}) |> render_change()
assert has_element?(view, "td", "Pin")
refute has_element?(view, "td", "Lucena")
end
test "shows an empty state that explains what themes do", %{conn: conn} do
{:ok, _view, html} = live(conn, ~p"/admin/themes")
assert html =~ "No themes yet"
assert html =~ "Themes power endgame filtering"
end
test "deleting a theme detaches it from endgames and keeps the endgame", %{conn: conn} do
theme = theme_fixture(%{name: "Lucena"})
endgame = endgame_fixture()
{:ok, _} = Endgames.update_endgame(endgame, %{}, [theme])
{:ok, view, html} = live(conn, ~p"/admin/themes")
assert html =~ "Used by 1 of 1 endgame"
assert html =~ "The endgames will be kept"
view |> element("#theme-#{theme.id} a", "Delete") |> render_click()
assert_raise Ecto.NoResultsError, fn -> Themes.get_theme!(theme.id) end
assert Endgames.get_endgame!(endgame.id)
endgame = Endgames.get_endgame!(endgame.id) |> Repo.preload(:themes)
assert endgame.themes == []
end
end