mirror of
https://github.com/TheRealOwenRees/chess-endgame-trainer.git
synced 2026-11-18 11:42:07 +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
|
||||
|
||||
Reference in New Issue
Block a user