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
+44 -4
View File
@@ -111,8 +111,13 @@ defmodule Chesstrainer.Endgames do
@doc """
Returns `{key, count}` groups for endgames matching the filters, sorted by key.
Filters is a map with optional `:themes` (a list of theme ids; an endgame must
carry any one of them) and `:key` (an exact Key).
Filters is a map with optional:
* `:themes` - a list of theme ids; an endgame must carry any one of them
* `:key` - an exact Key
* `:with_theme` - `{theme_id, source}`; only endgames linked to the theme
with that source
* `:without_theme` - a theme id; only endgames not linked to the theme at all
"""
def list_key_groups(filters \\ %{}) do
Endgame
@@ -125,20 +130,31 @@ defmodule Chesstrainer.Endgames do
@doc """
Returns the endgames for one Key, matching the filters, with themes preloaded.
Accepts `:limit` and `:offset` options for paginating large Key groups.
"""
def list_endgames_for_key(key, filters \\ %{}) do
def list_endgames_for_key(key, filters \\ %{}, opts \\ []) do
skip = Keyword.get(opts, :offset, 0)
Endgame
|> apply_filters(filters)
|> where([e], e.key == ^key)
|> preload(:themes)
|> order_by([e], asc: e.fen)
|> order_by([e], asc: e.fen, asc: e.id)
|> limit_query(Keyword.get(opts, :limit))
|> offset(^skip)
|> Repo.all()
end
defp limit_query(query, nil), do: query
defp limit_query(query, limit), do: limit(query, ^limit)
defp apply_filters(query, filters) do
query
|> filter_themes(Map.get(filters, :themes, []))
|> filter_key(Map.get(filters, :key))
|> filter_with_theme(Map.get(filters, :with_theme))
|> filter_without_theme(Map.get(filters, :without_theme))
end
defp filter_themes(query, []), do: query
@@ -156,6 +172,30 @@ defmodule Chesstrainer.Endgames do
defp filter_key(query, key) when key in [nil, ""], do: query
defp filter_key(query, key), do: where(query, [e], e.key == ^key)
defp filter_with_theme(query, nil), do: query
defp filter_with_theme(query, {theme_id, source}) do
endgame_ids =
from(t in "endgames_themes",
where: t.theme_id == ^theme_id and t.source == ^to_string(source),
select: t.endgame_id
)
where(query, [e], e.id in subquery(endgame_ids))
end
defp filter_without_theme(query, nil), do: query
defp filter_without_theme(query, theme_id) do
endgame_ids =
from(t in "endgames_themes",
where: t.theme_id == ^theme_id,
select: t.endgame_id
)
where(query, [e], e.id not in subquery(endgame_ids))
end
@doc """
Gets a single endgame.
+55
View File
@@ -263,6 +263,61 @@ defmodule Chesstrainer.Themes do
:ok
end
@link_sources [:curated, :community]
@doc """
Links `theme` to many endgames at once with the given source.
Defaults to `:curated`, which is what the admin tagging workspace writes.
Existing links are left alone, so re-applying is safe. Returns the number of
endgames the link was attempted for.
"""
@spec tag_endgames(Theme.t(), [Ecto.UUID.t()], :curated | :community) ::
{:ok, non_neg_integer()}
def tag_endgames(%Theme{id: theme_id}, endgame_ids, source \\ :curated)
when is_list(endgame_ids) and source in @link_sources do
now = DateTime.utc_now() |> DateTime.truncate(:second)
endgame_ids
|> Enum.uniq()
|> Enum.map(fn endgame_id ->
%{
endgame_id: Ecto.UUID.dump!(endgame_id),
theme_id: theme_id,
source: to_string(source),
inserted_at: now
}
end)
|> Enum.chunk_every(5_000)
|> Enum.each(fn rows ->
Repo.insert_all("endgames_themes", rows, on_conflict: :nothing)
end)
{:ok, length(Enum.uniq(endgame_ids))}
end
@doc """
Removes a link from `theme` to one endgame, but only if it has the given
source.
The tagging workspace removes `:curated` links only, so a derived or
community link can never be deleted by hand. Returns the number removed.
"""
@spec untag_endgame(Theme.t(), Ecto.UUID.t(), :curated | :community) ::
{:ok, non_neg_integer()}
def untag_endgame(%Theme{id: theme_id}, endgame_id, source \\ :curated)
when source in @link_sources do
{count, _} =
Repo.delete_all(
from l in "endgames_themes",
where:
l.theme_id == ^theme_id and l.endgame_id == type(^endgame_id, :binary_id) and
l.source == ^to_string(source)
)
{:ok, count}
end
@doc """
Replaces the derived theme links for one endgame.
@@ -89,6 +89,12 @@ defmodule ChesstrainerWeb.Admin.ThemeLive.Index do
</td>
<td class="w-0 font-semibold">
<div class="flex gap-4">
<.link
:if={theme.category == :endgame and theme.kind == :technique}
navigate={~p"/admin/themes/#{theme.id}/tag"}
>
Tag
</.link>
<.link navigate={~p"/admin/themes/#{theme.id}/edit"}>Edit</.link>
<.link
phx-click="delete"
@@ -0,0 +1,460 @@
defmodule ChesstrainerWeb.Admin.ThemeLive.Tag do
@moduledoc """
Workspace for applying a technique theme to endgames by family.
Candidates are grouped by Key so an admin tags a whole family rather than
individual rows. Boards are previews only; nothing here starts a puzzle.
Only technique themes can be tagged, because material themes are derived and
a hand-made link would be erased by the next refresh.
"""
use ChesstrainerWeb, :live_view
alias Chesstrainer.Endgames
alias Chesstrainer.Game
alias Chesstrainer.Themes
import ChesstrainerWeb.Board.BoardComponent
@page_size 24
@impl true
def render(assigns) do
~H"""
<Layouts.app flash={@flash} wide>
<Layouts.admin_nav current_scope={@current_scope} />
<.header>
Tag “{@theme.name}”
<:subtitle>
Pick a family, preview the positions and apply the theme in bulk. The links are
recorded as curated, so derivation never overwrites them.
</:subtitle>
<:actions>
<.button navigate={~p"/admin/themes"}>
<.icon name="hero-arrow-left" /> All themes
</.button>
</:actions>
</.header>
<.form
for={@theme_form}
id="theme-workspace-form"
phx-change="select_theme"
class="mb-6 max-w-md"
>
<.input
field={@theme_form[:theme_id]}
type="select"
label="Technique theme"
options={@theme_options}
/>
</.form>
<div class="mb-6 flex flex-wrap gap-6 text-sm">
<span>
<span class="font-semibold" id="tagged-count">{@tagged_count}</span> tagged
</span>
<span>
<span class="font-semibold" id="candidate-count">{@candidate_count}</span> candidates
</span>
</div>
<div
:if={MapSet.size(@selected) > 0}
id="selection-bar"
class="sticky top-0 z-10 mb-6 flex flex-wrap items-center gap-3 rounded-lg border border-primary/30 bg-base-100 p-3 text-sm"
>
<span class="font-semibold">{MapSet.size(@selected)} selected</span>
<button
type="button"
id="apply-theme"
phx-click="apply"
class="font-semibold text-primary-text"
>
Apply “{@theme.name}”
</button>
<button type="button" phx-click="clear_selection" class="text-base-content/70">
Clear selection
</button>
</div>
<section class="mb-12">
<h2 class="mb-3 text-lg font-semibold">Candidates</h2>
<p :if={@candidate_groups == []} class="text-base-content/70">
Every endgame already carries this theme.
</p>
<table :if={@candidate_groups != []} class={table_class()}>
<thead>
<tr>
<th>Key</th>
<th>Endgames</th>
<th><span class="sr-only">Actions</span></th>
</tr>
</thead>
<tbody>
<tr :for={{key, count} <- @candidate_groups} id={"candidates-#{group_id(key)}"}>
<td class="font-mono">{key}</td>
<td>{count}</td>
<td class="w-0">
<button
type="button"
id={"toggle-candidates-#{group_id(key)}"}
phx-click="toggle_group"
phx-value-key={key}
phx-value-view="candidates"
class="font-semibold text-primary-text"
>
{open_label(@open, key, "candidates")}
</button>
</td>
</tr>
</tbody>
</table>
<.group_panel
:if={@open && @open.view == "candidates"}
view="candidates"
group={@open.key}
items={@items}
selected={@selected}
page={@page}
pages={@pages}
/>
</section>
<section>
<h2 class="mb-3 text-lg font-semibold">Curated links</h2>
<p :if={@tagged_groups == []} class="text-base-content/70">
No endgame carries this theme yet.
</p>
<table :if={@tagged_groups != []} class={table_class()}>
<thead>
<tr>
<th>Key</th>
<th>Endgames</th>
<th><span class="sr-only">Actions</span></th>
</tr>
</thead>
<tbody>
<tr :for={{key, count} <- @tagged_groups} id={"tagged-#{group_id(key)}"}>
<td class="font-mono">{key}</td>
<td>{count}</td>
<td class="w-0">
<button
type="button"
id={"toggle-tagged-#{group_id(key)}"}
phx-click="toggle_group"
phx-value-key={key}
phx-value-view="tagged"
class="font-semibold text-primary-text"
>
{open_label(@open, key, "tagged")}
</button>
</td>
</tr>
</tbody>
</table>
<.group_panel
:if={@open && @open.view == "tagged"}
view="tagged"
group={@open.key}
items={@items}
selected={@selected}
page={@page}
pages={@pages}
/>
</section>
</Layouts.app>
"""
end
attr :view, :string, required: true
attr :group, :string, required: true
attr :items, :list, required: true
attr :selected, :any, required: true
attr :page, :integer, required: true
attr :pages, :integer, required: true
defp group_panel(assigns) do
~H"""
<div id="group-panel" class="mt-4">
<div :if={@view == "candidates"} class="mb-3 flex gap-3 text-sm">
<button
type="button"
id="select-page"
phx-click="select_page"
class="font-semibold text-primary-text"
>
Select this page
</button>
<span class="text-base-content/60">{@group}</span>
</div>
<p :if={@items == []} class="text-base-content/70">No endgames on this page.</p>
<div class="grid grid-cols-1 gap-4 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4">
<div
:for={endgame <- @items}
id={"endgame-#{endgame.id}"}
class="rounded-lg border border-base-300 p-3"
>
<.board
game={endgame.game}
myself={nil}
interactive={false}
class="mb-2 w-full max-w-[240px]"
/>
<p class="break-all font-mono text-xs text-base-content/60">{endgame.fen}</p>
<label :if={@view == "candidates"} class="mt-2 flex items-center gap-2 text-sm">
<input
type="checkbox"
id={"select-#{endgame.id}"}
checked={MapSet.member?(@selected, endgame.id)}
phx-click="toggle_select"
phx-value-id={endgame.id}
/> Select
</label>
<button
:if={@view == "tagged"}
type="button"
id={"remove-#{endgame.id}"}
phx-click="remove"
phx-value-id={endgame.id}
class="mt-2 text-sm font-semibold text-error"
>
Remove
</button>
</div>
</div>
<div :if={@pages > 1} class="mt-4 flex items-center gap-3 text-sm">
<button
type="button"
phx-click="page"
phx-value-page={@page - 1}
disabled={@page <= 1}
class="font-semibold disabled:opacity-40"
>
Previous
</button>
<span>Page {@page} of {@pages}</span>
<button
type="button"
phx-click="page"
phx-value-page={@page + 1}
disabled={@page >= @pages}
class="font-semibold disabled:opacity-40"
>
Next
</button>
</div>
</div>
"""
end
@impl true
def mount(%{"id" => id}, _session, socket) do
theme = Themes.get_theme!(id)
if theme.category == :endgame and theme.kind == :technique do
{:ok,
socket
|> assign(:page_title, "Tag #{theme.name}")
|> assign(:theme, theme)
|> assign(:selected, MapSet.new())
|> assign(:open, nil)
|> assign(:items, [])
|> assign(:page, 1)
|> assign(:pages, 1)
|> assign_theme_options()
|> load_counts()}
else
{:ok,
socket
|> put_flash(
:error,
"Only technique themes can be tagged by hand; material themes are derived."
)
|> push_navigate(to: ~p"/admin/themes")}
end
end
@impl true
def handle_event("select_theme", %{"theme_id" => id}, socket) do
if to_string(socket.assigns.theme.id) == id do
{:noreply, socket}
else
{:noreply, push_navigate(socket, to: ~p"/admin/themes/#{id}/tag")}
end
end
def handle_event("toggle_group", %{"key" => key, "view" => view}, socket) do
case socket.assigns.open do
%{key: ^key, view: ^view} ->
{:noreply, assign(socket, open: nil, items: [], page: 1, pages: 1)}
_ ->
{:noreply, open_group(socket, key, view, 1)}
end
end
def handle_event("page", %{"page" => page}, socket) do
case socket.assigns.open do
nil ->
{:noreply, socket}
%{key: key, view: view} ->
{:noreply, open_group(socket, key, view, parse_page(page))}
end
end
def handle_event("toggle_select", %{"id" => id}, socket) do
selected =
if MapSet.member?(socket.assigns.selected, id) do
MapSet.delete(socket.assigns.selected, id)
else
MapSet.put(socket.assigns.selected, id)
end
{:noreply, assign(socket, :selected, selected)}
end
def handle_event("select_page", _params, socket) do
page_ids = Enum.map(socket.assigns.items, & &1.id)
selected = Enum.reduce(page_ids, socket.assigns.selected, &MapSet.put(&2, &1))
{:noreply, assign(socket, :selected, selected)}
end
def handle_event("clear_selection", _params, socket) do
{:noreply, assign(socket, :selected, MapSet.new())}
end
def handle_event("apply", _params, socket) do
selected = MapSet.to_list(socket.assigns.selected)
if selected == [] do
{:noreply, put_flash(socket, :error, "Select at least one endgame.")}
else
{:ok, count} = Themes.tag_endgames(socket.assigns.theme, selected, :curated)
{:noreply,
socket
|> put_flash(:info, "Tagged #{count} endgame(s) with “#{socket.assigns.theme.name}”.")
|> assign(:selected, MapSet.new())
|> assign(open: nil, items: [], page: 1, pages: 1)
|> load_counts()}
end
end
def handle_event("remove", %{"id" => id}, socket) do
{:ok, removed} = Themes.untag_endgame(socket.assigns.theme, id, :curated)
socket =
socket
|> put_flash(:info, removal_message(removed, socket.assigns.theme))
|> load_counts()
|> refresh_open()
{:noreply, socket}
end
defp removal_message(0, theme), do: "That endgame had no curated “#{theme.name}” link."
defp removal_message(_, theme), do: "Removed “#{theme.name}” from the endgame."
defp refresh_open(%{assigns: %{open: nil}} = socket), do: socket
defp refresh_open(%{assigns: %{open: %{key: key, view: view}, page: page}} = socket) do
open_group(socket, key, view, page)
end
defp open_group(socket, key, view, page) do
count = group_count(socket, key, view)
pages = max(ceil_div(count, @page_size), 1)
page = page |> max(1) |> min(pages)
endgames =
Endgames.list_endgames_for_key(key, filters_for(socket.assigns.theme, view),
limit: @page_size,
offset: (page - 1) * @page_size
)
|> Enum.map(&Map.put(&1, :game, board_game(&1.fen)))
assign(socket,
open: %{key: key, view: view},
items: endgames,
page: page,
pages: pages
)
end
defp filters_for(%{id: theme_id}, "tagged"), do: %{with_theme: {theme_id, :curated}}
defp filters_for(%{id: theme_id}, "candidates"), do: %{without_theme: theme_id}
defp group_count(socket, key, view) do
groups =
if view == "tagged", do: socket.assigns.tagged_groups, else: socket.assigns.candidate_groups
Enum.find_value(groups, 0, fn
{^key, count} -> count
_ -> nil
end)
end
defp parse_page(page) when is_integer(page), do: page
defp parse_page(page) when is_binary(page) do
case Integer.parse(page) do
{value, _} -> value
:error -> 1
end
end
defp ceil_div(0, _), do: 0
defp ceil_div(count, size), do: div(count + size - 1, size)
defp load_counts(socket) do
theme = socket.assigns.theme
tagged = Endgames.list_key_groups(%{with_theme: {theme.id, :curated}})
candidates = Endgames.list_key_groups(%{without_theme: theme.id})
assign(socket,
tagged_groups: tagged,
candidate_groups: candidates,
tagged_count: total(tagged),
candidate_count: total(candidates)
)
end
defp total(groups), do: Enum.reduce(groups, 0, fn {_key, count}, acc -> acc + count end)
defp assign_theme_options(socket) do
options =
Themes.list_themes()
|> Enum.filter(&(&1.category == :endgame and &1.kind == :technique))
|> Enum.sort_by(& &1.name)
|> Enum.map(&{&1.name, &1.id})
assign(socket,
theme_options: options,
theme_form: to_form(%{"theme_id" => to_string(socket.assigns.theme.id)})
)
end
defp board_game(fen) do
case Game.game_from_fen(fen, :endgame) do
{:ok, game} -> game
{:error, :invalid_fen, game} -> game
end
end
defp open_label(%{key: key, view: view}, key, view), do: "Hide"
defp open_label(_open, _key, _view), do: "Show"
defp group_id(key), do: String.replace(key, ~r/[^A-Za-z0-9]+/, "-")
end
+1
View File
@@ -55,6 +55,7 @@ defmodule ChesstrainerWeb.Router do
live "/themes", Admin.ThemeLive.Index, :index
live "/themes/new", Admin.ThemeLive.Form, :new
live "/themes/:id/edit", Admin.ThemeLive.Form, :edit
live "/themes/:id/tag", Admin.ThemeLive.Tag, :tag
live "/users", Admin.UserLive.Index, :index
live "/users/new", Admin.UserLive.New, :new
+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