Add a workspace for tagging technique themes by family (#194)

An admin picks a technique theme and sees candidate endgames grouped by Key
with counts. Expanding a family previews its boards without starting a puzzle,
and selecting positions applies the theme in bulk as curated. Large families are
paginated so a 600-endgame group stays usable, and curated links can be removed
from the same surface.

Removal is scoped to curated links, so derived and community links are never
deleted by hand. Material themes are refused because derivation owns them.

Closes #187
This commit is contained in:
2026-11-17 21:55:37 +01:00
committed by GitHub
parent 946fc38756
commit e6f08e0ca6
8 changed files with 775 additions and 4 deletions
+28
View File
@@ -1,6 +1,8 @@
defmodule Chesstrainer.EndgamesTest do
use Chesstrainer.DataCase
import Chesstrainer.EndgamesFixtures
alias Chesstrainer.Endgames
alias Chesstrainer.Endgames.Endgame
alias Chesstrainer.Themes
@@ -387,4 +389,30 @@ defmodule Chesstrainer.EndgamesTest do
assert Endgames.curated_endgames() == []
end
end
describe "theme filters and pagination" do
test "groups endgames by whether they carry a theme" do
{:ok, theme} = Themes.create_theme(%{name: "Lucena", category: :endgame})
tagged = create_endgame!(@kr)
create_endgame!(@kq)
{:ok, _} = Endgames.update_endgame(tagged, %{}, [theme])
assert Endgames.list_key_groups(%{with_theme: {theme.id, :curated}}) == [{"KR v K", 1}]
assert Endgames.list_key_groups(%{without_theme: theme.id}) == [{"KQ v K", 1}]
end
test "paginates the endgames in one key group" do
endgames = for _ <- 1..3, do: endgame_fixture()
first = Endgames.list_endgames_for_key("K v K", %{}, limit: 2, offset: 0)
second = Endgames.list_endgames_for_key("K v K", %{}, limit: 2, offset: 2)
assert length(first) == 2
assert length(second) == 1
ids = Enum.map(first ++ second, & &1.id)
assert Enum.sort(ids) == Enum.sort(Enum.map(endgames, & &1.id))
assert length(Enum.uniq(ids)) == 3
end
end
end
+37
View File
@@ -218,6 +218,43 @@ defmodule Chesstrainer.ThemesTest do
end
end
describe "tag_endgames/3 and untag_endgame/3" do
test "adds curated links without duplicating them" do
theme = theme_fixture()
first = endgame_fixture()
second = endgame_fixture()
assert {:ok, 2} = Themes.tag_endgames(theme, [first.id, second.id])
assert {:ok, 2} = Themes.tag_endgames(theme, [first.id, second.id])
assert link_sources(first.id) == %{theme.id => "curated"}
assert link_sources(second.id) == %{theme.id => "curated"}
end
test "never overwrites a derived link" do
theme = theme_fixture()
endgame = endgame_fixture()
:ok = Themes.sync_derived_themes(endgame.id, [theme.id])
assert {:ok, 1} = Themes.tag_endgames(theme, [endgame.id])
assert link_sources(endgame.id) == %{theme.id => "derived"}
end
test "removes only the requested source" do
theme = theme_fixture()
curated = endgame_fixture()
community = endgame_fixture()
{:ok, _} = Themes.tag_endgames(theme, [curated.id], :curated)
{:ok, _} = Themes.tag_endgames(theme, [community.id], :community)
assert {:ok, 1} = Themes.untag_endgame(theme, curated.id, :curated)
assert link_sources(curated.id) == %{}
assert link_sources(community.id) == %{theme.id => "community"}
end
end
defp link_rows(endgame_id) do
Repo.all(
from l in "endgames_themes",
@@ -0,0 +1,144 @@
defmodule ChesstrainerWeb.Admin.ThemeLive.TagTest do
use ChesstrainerWeb.ConnCase
import Ecto.Query
import Phoenix.LiveViewTest
import Chesstrainer.AccountsFixtures
import Chesstrainer.ThemesFixtures
alias Chesstrainer.Endgames
alias Chesstrainer.Repo
alias Chesstrainer.Themes
setup :register_and_log_in_admin
@kp_v_kp_1 "4k3/8/8/8/8/4p3/4P3/4K3 w - - 0 1"
@kp_v_kp_2 "3k4/8/8/8/8/4p3/4P3/4K3 w - - 0 1"
@kr_v_kr "8/8/3k4/8/8/3K1R2/8/5r2 b - - 0 1"
test "lists candidate groups with counts and previews boards", %{conn: conn} do
theme = technique_theme()
kp_1 = create_endgame!(@kp_v_kp_1)
kp_2 = create_endgame!(@kp_v_kp_2)
kr = create_endgame!(@kr_v_kr)
{:ok, view, _html} = live(conn, ~p"/admin/themes/#{theme.id}/tag")
assert has_element?(view, "#candidate-count", "3")
assert has_element?(view, "#candidates-KP-v-KP", "2")
view |> element("#toggle-candidates-KP-v-KP") |> render_click()
assert has_element?(view, "#endgame-#{kp_1.id}")
assert has_element?(view, "#endgame-#{kp_2.id}")
refute has_element?(view, "#endgame-#{kr.id}")
assert render(view) =~ "Chess position"
end
test "applies the theme in bulk and updates coverage", %{conn: conn} do
theme = technique_theme()
kp_1 = create_endgame!(@kp_v_kp_1)
kp_2 = create_endgame!(@kp_v_kp_2)
_kr = create_endgame!(@kr_v_kr)
{:ok, view, _html} = live(conn, ~p"/admin/themes/#{theme.id}/tag")
view |> element("#toggle-candidates-KP-v-KP") |> render_click()
view |> element("#select-#{kp_1.id}") |> render_click()
view |> element("#select-#{kp_2.id}") |> render_click()
view |> element("#apply-theme") |> render_click()
assert render(view) =~ "Tagged 2 endgame(s)"
assert has_element?(view, "#tagged-count", "2")
assert has_element?(view, "#candidate-count", "1")
assert Enum.sort(curated_endgame_ids(theme)) == Enum.sort([kp_1.id, kp_2.id])
{:ok, _index, html} = live(conn, ~p"/admin/themes")
assert html =~ "2 of 3 endgames"
end
test "removes a curated link but leaves a community link alone", %{conn: conn} do
theme = technique_theme()
curated = create_endgame!(@kp_v_kp_1)
community = create_endgame!(@kr_v_kr)
{:ok, _} = Themes.tag_endgames(theme, [curated.id], :curated)
{:ok, _} = Themes.tag_endgames(theme, [community.id], :community)
{:ok, view, _html} = live(conn, ~p"/admin/themes/#{theme.id}/tag")
view |> element("#toggle-tagged-KP-v-KP") |> render_click()
view |> element("#remove-#{curated.id}") |> render_click()
assert render(view) =~ "Removed"
assert curated_endgame_ids(theme) == []
assert {Ecto.UUID.dump!(community.id), "community"} in link_rows(theme.id)
end
test "refuses a material theme", %{conn: conn} do
{:ok, theme} =
Themes.create_theme(%{name: "Rook endgame", kind: :material, category: :endgame})
assert {:error, {:live_redirect, %{to: "/admin/themes"}}} =
live(conn, ~p"/admin/themes/#{theme.id}/tag")
end
test "switching the theme navigates to its workspace", %{conn: conn} do
first = technique_theme(%{name: "Lucena"})
second = technique_theme(%{name: "Philidor"})
{:ok, view, _html} = live(conn, ~p"/admin/themes/#{first.id}/tag")
assert {:error, {:live_redirect, %{to: path}}} =
view
|> form("#theme-workspace-form", %{theme_id: to_string(second.id)})
|> render_change()
assert path == "/admin/themes/#{second.id}/tag"
end
test "members cannot open the workspace", %{conn: conn} do
theme = technique_theme()
conn = log_in_user(conn, user_fixture())
assert redirected_to(get(conn, ~p"/admin/themes/#{theme.id}/tag")) == ~p"/"
end
defp technique_theme(attrs \\ %{}) do
attrs =
Map.merge(
%{name: unique_theme_name(), kind: :technique, category: :endgame},
attrs
)
{:ok, theme} = Themes.create_theme(attrs)
theme
end
defp create_endgame!(fen) do
{:ok, endgame} =
Endgames.create_endgame(%{fen: fen, color: :white, result: :win, rating: 1500})
endgame
end
defp curated_endgame_ids(theme) do
Repo.all(
from l in "endgames_themes",
where: l.theme_id == ^theme.id and l.source == "curated",
select: l.endgame_id
)
|> Enum.map(&Ecto.UUID.load!/1)
end
defp link_rows(theme_id) do
Repo.all(
from l in "endgames_themes",
where: l.theme_id == ^theme_id,
select: {l.endgame_id, l.source}
)
end
end