mirror of
https://github.com/TheRealOwenRees/chess-endgame-trainer.git
synced 2026-11-18 03:32:06 +00:00
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
This commit is contained in:
@@ -276,6 +276,21 @@ defmodule Chesstrainer.AccountsTest do
|
||||
end
|
||||
end
|
||||
|
||||
describe "update_user_plan/2" do
|
||||
test "changes the user's plan" do
|
||||
user = user_fixture()
|
||||
|
||||
assert {:ok, %User{plan: :premium}} = Accounts.update_user_plan(user, :premium)
|
||||
end
|
||||
|
||||
test "rejects a plan that is not in the enum" do
|
||||
user = user_fixture()
|
||||
|
||||
assert {:error, changeset} = Accounts.update_user_plan(user, :platinum)
|
||||
assert %{plan: ["is invalid"]} = errors_on(changeset)
|
||||
end
|
||||
end
|
||||
|
||||
describe "sudo_mode?/2" do
|
||||
test "validates the authenticated_at time" do
|
||||
now = DateTime.utc_now()
|
||||
|
||||
@@ -1,13 +1,21 @@
|
||||
defmodule Chesstrainer.ThemesTest do
|
||||
use Chesstrainer.DataCase
|
||||
|
||||
import Ecto.Query
|
||||
|
||||
import Chesstrainer.AccountsFixtures
|
||||
import Chesstrainer.EndgamesFixtures
|
||||
import Chesstrainer.ThemesFixtures
|
||||
|
||||
alias Chesstrainer.Accounts
|
||||
alias Chesstrainer.Accounts.Scope
|
||||
alias Chesstrainer.Endgames
|
||||
alias Chesstrainer.Repo
|
||||
alias Chesstrainer.Themes
|
||||
|
||||
describe "themes" do
|
||||
alias Chesstrainer.Themes.Theme
|
||||
|
||||
import Chesstrainer.ThemesFixtures
|
||||
|
||||
@invalid_attrs %{name: nil, category: nil}
|
||||
|
||||
test "list_themes/0 returns all themes" do
|
||||
@@ -76,4 +84,163 @@ defmodule Chesstrainer.ThemesTest do
|
||||
assert changeset.errors[:name]
|
||||
end
|
||||
end
|
||||
|
||||
describe "theme identity" do
|
||||
test "generates a slug from the name" do
|
||||
assert {:ok, theme} = Themes.create_theme(%{name: " Mate in 2! ", category: :endgame})
|
||||
assert theme.slug == "mate-in-2"
|
||||
end
|
||||
|
||||
test "normalises an explicit slug" do
|
||||
assert {:ok, theme} =
|
||||
Themes.create_theme(%{
|
||||
name: "Lucena",
|
||||
slug: " Lucena-Position ",
|
||||
category: :endgame
|
||||
})
|
||||
|
||||
assert theme.slug == "lucena-position"
|
||||
end
|
||||
|
||||
test "keeps the slug stable when the name changes" do
|
||||
theme = theme_fixture(%{name: "Lucena"})
|
||||
|
||||
assert {:ok, updated} = Themes.update_theme(theme, %{name: "Lucena position"})
|
||||
assert updated.slug == "lucena"
|
||||
end
|
||||
|
||||
test "rejects a slug that is already taken, even in another category" do
|
||||
assert {:ok, _theme} = Themes.create_theme(%{name: "Lucena", category: :endgame})
|
||||
|
||||
assert {:error, changeset} = Themes.create_theme(%{name: "Lucena", category: :tactic})
|
||||
assert changeset.errors[:slug]
|
||||
end
|
||||
|
||||
test "defaults the kind to technique" do
|
||||
assert {:ok, theme} = Themes.create_theme(%{name: "Opposition", category: :endgame})
|
||||
assert theme.kind == :technique
|
||||
end
|
||||
|
||||
test "accepts a material kind" do
|
||||
assert {:ok, theme} =
|
||||
Themes.create_theme(%{
|
||||
name: "Rook endgame",
|
||||
kind: :material,
|
||||
category: :endgame
|
||||
})
|
||||
|
||||
assert theme.kind == :material
|
||||
end
|
||||
|
||||
test "rejects an unknown kind" do
|
||||
assert {:error, changeset} =
|
||||
Themes.create_theme(%{
|
||||
name: "Zugzwang",
|
||||
kind: :bogus,
|
||||
category: :endgame
|
||||
})
|
||||
|
||||
assert changeset.errors[:kind]
|
||||
end
|
||||
|
||||
test "get_theme_by_slug/1 finds an endgame theme and ignores other categories" do
|
||||
theme = theme_fixture(%{name: "Philidor"})
|
||||
tactic = theme_fixture(%{name: "Philidor's pin", category: :tactic})
|
||||
|
||||
assert Themes.get_theme_by_slug("philidor").id == theme.id
|
||||
assert Themes.get_theme_by_slug(tactic.slug) == nil
|
||||
assert Themes.get_theme_by_slug("unknown") == nil
|
||||
end
|
||||
end
|
||||
|
||||
describe "create_theme/2 authorization" do
|
||||
test "an admin scope may create a theme" do
|
||||
{:ok, admin} = Accounts.update_user_role(user_fixture(), :admin)
|
||||
scope = Scope.for_user(admin)
|
||||
|
||||
assert {:ok, %Themes.Theme{}} =
|
||||
Themes.create_theme(scope, %{name: "Opposition", category: :endgame})
|
||||
end
|
||||
|
||||
test "a member scope cannot create a theme" do
|
||||
scope = user_scope_fixture()
|
||||
|
||||
assert {:error, :unauthorized} =
|
||||
Themes.create_theme(scope, %{name: "Opposition", category: :endgame})
|
||||
|
||||
assert Themes.list_themes() == []
|
||||
end
|
||||
end
|
||||
|
||||
describe "theme link provenance" do
|
||||
test "links created from the endgame context default to curated" do
|
||||
theme = theme_fixture()
|
||||
endgame = endgame_fixture()
|
||||
|
||||
assert {:ok, _endgame} = Endgames.update_endgame(endgame, %{}, [theme])
|
||||
|
||||
assert [%{theme_id: theme_id, source: "curated"}] = link_rows(endgame.id)
|
||||
assert theme_id == theme.id
|
||||
end
|
||||
|
||||
test "sync_derived_themes/2 adds derived links and leaves curated and community links alone" do
|
||||
curated = theme_fixture(%{name: "Lucena"})
|
||||
community = theme_fixture(%{name: "Philidor"})
|
||||
derived = theme_fixture(%{name: "Rook endgame"})
|
||||
endgame = endgame_fixture()
|
||||
|
||||
{:ok, _} = Endgames.update_endgame(endgame, %{}, [curated])
|
||||
insert_link(endgame.id, community.id, "community")
|
||||
|
||||
assert :ok = Themes.sync_derived_themes(endgame.id, [derived.id, curated.id])
|
||||
|
||||
sources = link_sources(endgame.id)
|
||||
assert sources[curated.id] == "curated"
|
||||
assert sources[community.id] == "community"
|
||||
assert sources[derived.id] == "derived"
|
||||
end
|
||||
|
||||
test "sync_derived_themes/2 replaces only its own derived links" do
|
||||
curated = theme_fixture(%{name: "Lucena"})
|
||||
old_derived = theme_fixture(%{name: "Rook endgame"})
|
||||
new_derived = theme_fixture(%{name: "Two pawns"})
|
||||
endgame = endgame_fixture()
|
||||
|
||||
{:ok, _} = Endgames.update_endgame(endgame, %{}, [curated])
|
||||
Themes.sync_derived_themes(endgame.id, [old_derived.id])
|
||||
|
||||
assert :ok = Themes.sync_derived_themes(endgame.id, [new_derived.id])
|
||||
|
||||
sources = link_sources(endgame.id)
|
||||
assert sources[curated.id] == "curated"
|
||||
refute Map.has_key?(sources, old_derived.id)
|
||||
assert sources[new_derived.id] == "derived"
|
||||
end
|
||||
end
|
||||
|
||||
defp link_rows(endgame_id) do
|
||||
Repo.all(
|
||||
from l in "endgames_themes",
|
||||
where: l.endgame_id == type(^endgame_id, :binary_id),
|
||||
select: %{theme_id: l.theme_id, source: l.source},
|
||||
order_by: l.theme_id
|
||||
)
|
||||
end
|
||||
|
||||
defp link_sources(endgame_id) do
|
||||
endgame_id
|
||||
|> link_rows()
|
||||
|> Map.new(&{&1.theme_id, &1.source})
|
||||
end
|
||||
|
||||
defp insert_link(endgame_id, theme_id, source) do
|
||||
Repo.insert_all("endgames_themes", [
|
||||
%{
|
||||
endgame_id: Ecto.UUID.dump!(endgame_id),
|
||||
theme_id: theme_id,
|
||||
source: source,
|
||||
inserted_at: DateTime.utc_now() |> DateTime.truncate(:second)
|
||||
}
|
||||
])
|
||||
end
|
||||
end
|
||||
|
||||
@@ -17,7 +17,30 @@ defmodule ChesstrainerWeb.Admin.ThemeLive.FormTest do
|
||||
|
||||
assert_redirect(view, ~p"/admin/themes")
|
||||
|
||||
assert [%{name: "Lucena", category: :endgame}] = Themes.search("Lucena", "endgame")
|
||||
assert [%{name: "Lucena", slug: "lucena", kind: :technique, category: :endgame}] =
|
||||
Themes.search("Lucena", "endgame")
|
||||
end
|
||||
|
||||
test "creates a theme with a kind and description", %{conn: conn} do
|
||||
{:ok, view, _html} = live(conn, ~p"/admin/themes/new")
|
||||
|
||||
view
|
||||
|> form("#theme-form",
|
||||
theme: %{
|
||||
name: "Rook endgame",
|
||||
kind: "material",
|
||||
category: "endgame",
|
||||
description: "Positions where a rook and pawn face a rook."
|
||||
}
|
||||
)
|
||||
|> render_submit()
|
||||
|
||||
assert_redirect(view, ~p"/admin/themes")
|
||||
|
||||
assert [theme] = Themes.search("Rook endgame", "endgame")
|
||||
assert theme.slug == "rook-endgame"
|
||||
assert theme.kind == :material
|
||||
assert theme.description == "Positions where a rook and pawn face a rook."
|
||||
end
|
||||
|
||||
test "renames and recategorises a theme", %{conn: conn} do
|
||||
|
||||
@@ -11,8 +11,8 @@ defmodule ChesstrainerWeb.Admin.ThemeLive.IndexTest do
|
||||
|
||||
setup :register_and_log_in_admin
|
||||
|
||||
test "lists themes with their usage count", %{conn: conn} do
|
||||
used_theme = theme_fixture(%{name: "Lucena"})
|
||||
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])
|
||||
@@ -20,8 +20,9 @@ defmodule ChesstrainerWeb.Admin.ThemeLive.IndexTest do
|
||||
{:ok, view, _html} = live(conn, ~p"/admin/themes")
|
||||
|
||||
assert has_element?(view, "#theme-#{used_theme.id}")
|
||||
assert has_element?(view, "#theme-usage-#{used_theme.id}", "1 endgame")
|
||||
assert has_element?(view, "#theme-usage-#{unused_theme.id}", "0 endgames")
|
||||
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
|
||||
@@ -57,7 +58,7 @@ defmodule ChesstrainerWeb.Admin.ThemeLive.IndexTest do
|
||||
|
||||
{:ok, view, html} = live(conn, ~p"/admin/themes")
|
||||
|
||||
assert html =~ "Used by 1 endgame"
|
||||
assert html =~ "Used by 1 of 1 endgame"
|
||||
assert html =~ "The endgames will be kept"
|
||||
|
||||
view |> element("#theme-#{theme.id} a", "Delete") |> render_click()
|
||||
|
||||
Reference in New Issue
Block a user