diff --git a/lib/chesstrainer/endgames/bulk_import.ex b/lib/chesstrainer/endgames/bulk_import.ex new file mode 100644 index 0000000..745d661 --- /dev/null +++ b/lib/chesstrainer/endgames/bulk_import.ex @@ -0,0 +1,218 @@ +defmodule Chesstrainer.Endgames.BulkImport do + @moduledoc """ + Classifies pasted FENs for the admin bulk importer and inserts the positions + the local Syzygy tablebase can answer. + + Results always come from `Chesstrainer.Endgames.Tablebase.probe_local_result/1`, + so a bulk import never consults the remote (Lichess) backend. + """ + + import Ecto.Query, warn: false + + alias Chesstrainer.Endgames.Endgame + alias Chesstrainer.Endgames.Tablebase + alias Chesstrainer.{FEN, Key, Repo} + + @max_fens 1000 + @max_pieces 5 + @default_rating 1500.0 + + @type status :: + :ready + | :duplicate + | :invalid_fen + | :too_many_pieces + | :illegal + | :terminal + | :unsupported + + @type entry :: %{ + line: pos_integer(), + fen: String.t(), + key: String.t() | nil, + color: :white | :black | nil, + result: Tablebase.result() | nil, + status: status(), + reason: String.t() | nil + } + + @doc """ + The most FENs a single import will consider. + """ + @spec max_fens() :: pos_integer() + def max_fens, do: @max_fens + + @doc """ + Parses one FEN per line, skipping blank lines and lines beginning with `#`. + + Returns entries with a status explaining whether the line can be imported, + and `too_many?` when the input exceeded `max_fens/0` (only the first + `max_fens/0` lines are considered). + + FENs already in the database and duplicates within the batch are reported as + duplicates without probing the tablebase. + """ + @spec preview(String.t()) :: %{entries: [entry()], too_many?: boolean()} + def preview(text) when is_binary(text) do + lines = parse_lines(text) + {lines, rest} = Enum.split(lines, @max_fens) + + fens = Enum.map(lines, fn {_line, fen} -> FEN.normalize(fen) end) + existing = existing_fens(fens) + + {entries, _seen} = + Enum.map_reduce(lines, %{}, fn {line, raw_fen}, seen -> + classify(line, FEN.normalize(raw_fen), seen, existing) + end) + + %{entries: entries, too_many?: rest != []} + end + + @doc """ + Inserts every `:ready` entry, attaching `themes` and `rating` to each. + + Rows that collide with an existing FEN are skipped. Returns the number + imported and the number of entries that were not ready or were skipped. + """ + @spec commit([entry()], [Chesstrainer.Themes.Theme.t()], number()) :: + %{imported: non_neg_integer(), skipped: non_neg_integer()} + def commit(entries, themes \\ [], rating \\ @default_rating) do + ready = Enum.filter(entries, &(&1.status == :ready)) + now = DateTime.utc_now() |> DateTime.truncate(:second) + + rows = + Enum.map(ready, fn entry -> + %{ + id: Ecto.UUID.generate(), + public_code: Endgame.generate_public_code(), + fen: entry.fen, + color: entry.color, + key: entry.key, + result: entry.result, + rating: (rating || @default_rating) * 1.0, + rating_deviation: 350.0, + times_attempted: 0, + times_solved: 0, + inserted_at: now, + updated_at: now + } + end) + + {imported, inserted_rows} = + Repo.insert_all(Endgame, rows, on_conflict: :nothing, returning: [:id]) + + join_rows = + for %Endgame{id: id} <- inserted_rows, theme <- themes do + %{ + endgame_id: Ecto.UUID.dump!(id), + theme_id: theme.id + } + end + + if join_rows != [] do + Repo.insert_all("endgames_themes", join_rows, on_conflict: :nothing) + end + + %{imported: imported, skipped: length(entries) - imported} + end + + defp parse_lines(text) do + text + |> String.split(~r/\r?\n/) + |> Enum.with_index(1) + |> Enum.reduce([], fn {raw, index}, acc -> + trimmed = String.trim(raw) + + if trimmed == "" or String.starts_with?(trimmed, "#") do + acc + else + [{index, trimmed} | acc] + end + end) + |> Enum.reverse() + end + + defp existing_fens(fens) do + fens + |> Enum.uniq() + |> then(fn fens -> Repo.all(from e in Endgame, where: e.fen in ^fens, select: e.fen) end) + |> Map.new(&{&1, true}) + end + + defp classify(line, fen, seen, existing) do + cond do + Map.has_key?(seen, fen) -> + {entry(line, fen, :duplicate, "Duplicate FEN in this batch"), seen} + + Map.has_key?(existing, fen) -> + {entry(line, fen, :duplicate, "FEN already exists"), seen} + + true -> + {classify_new(line, fen), Map.put(seen, fen, true)} + end + end + + defp classify_new(line, fen) do + issues = Endgame.fen_issues(fen) + pieces = FEN.piece_count(fen) + + cond do + issues != [] -> + entry(line, fen, :invalid_fen, Enum.join(issues, "; ")) + + is_integer(pieces) and pieces > @max_pieces -> + entry( + line, + fen, + :too_many_pieces, + "Only positions with #{@max_pieces} or fewer pieces are supported" + ) + + true -> + classify_probe(line, fen) + end + end + + defp classify_probe(line, fen) do + case Tablebase.probe_local_result(fen) do + {:ok, result} -> + entry(line, fen, :ready, nil) |> Map.put(:result, result) + + {:error, :terminal} -> + entry(line, fen, :terminal, "Checkmate, stalemate, or no legal moves") + + {:error, :illegal} -> + entry(line, fen, :illegal, "The tablebase rejected this position") + + {:error, :not_configured} -> + entry( + line, + fen, + :unsupported, + "Local tablebase is not configured (set TABLEBASE_PATH)" + ) + + {:error, :unsupported} -> + entry(line, fen, :unsupported, "The local tablebase cannot answer this position") + end + end + + defp entry(line, fen, status, reason) do + %{ + line: line, + fen: fen, + key: key_for(fen), + color: FEN.color_from_fen(fen), + result: nil, + status: status, + reason: reason + } + end + + defp key_for(fen) do + case Key.from_fen(fen) do + {:ok, key} -> key + _ -> nil + end + end +end diff --git a/lib/chesstrainer/endgames/endgame.ex b/lib/chesstrainer/endgames/endgame.ex index e305c11..ec3e7d6 100644 --- a/lib/chesstrainer/endgames/endgame.ex +++ b/lib/chesstrainer/endgames/endgame.ex @@ -90,10 +90,15 @@ defmodule Chesstrainer.Endgames.Endgame do defp normalize_attrs(attrs), do: attrs - # Strict structural validation. Mirrors chess.js rules so the FEN survives - # both the server-side parser (Chex, permissive) and the client-side parser - # (chess.js, strict). - defp fen_issues(fen) do + @doc """ + Returns the structural problems with a FEN, or `[]` when it is well formed. + + Strict structural validation. Mirrors chess.js rules so the FEN survives + both the server-side parser (Chex, permissive) and the client-side parser + (chess.js, strict). Used by the admin bulk importer to explain why a line + was rejected. + """ + def fen_issues(fen) do case String.split(fen, " ") do [board, active, _castling, _en_passant, halfmove, fullmove] -> board_valid? = valid_board?(board) diff --git a/lib/chesstrainer/endgames/tablebase.ex b/lib/chesstrainer/endgames/tablebase.ex index 6df7418..13f4451 100644 --- a/lib/chesstrainer/endgames/tablebase.ex +++ b/lib/chesstrainer/endgames/tablebase.ex @@ -12,6 +12,7 @@ defmodule Chesstrainer.Endgames.Tablebase do """ alias Chesstrainer.Endgames.Tablebase + alias Chesstrainer.Endgames.Tablebase.Backend.Fathom, as: FathomBackend alias Chesstrainer.FEN @type result :: :win | :draw | :loss | :unknown @@ -67,6 +68,19 @@ defmodule Chesstrainer.Endgames.Tablebase do def flip(:draw), do: :draw def flip(_outcome), do: :unknown + @doc """ + Probes a FEN using only the local Syzygy tablebase and returns the result + from the side-to-move's perspective. + + Unlike `new/1` this never falls back to the remote backend and never reads or + writes the cache. It is the primitive behind the admin bulk importer, so an + imported result always comes from our own tablebases. + """ + @spec probe_local_result(String.t()) :: + {:ok, result()} + | {:error, :not_configured | :illegal | :terminal | :unsupported} + def probe_local_result(fen), do: FathomBackend.probe_result_local(fen) + @spec new(String.t()) :: {:ok, t()} | {:cooldown, non_neg_integer()} diff --git a/lib/chesstrainer/endgames/tablebase/backend/fathom.ex b/lib/chesstrainer/endgames/tablebase/backend/fathom.ex index 652b1ab..fd3f1e0 100644 --- a/lib/chesstrainer/endgames/tablebase/backend/fathom.ex +++ b/lib/chesstrainer/endgames/tablebase/backend/fathom.ex @@ -18,6 +18,7 @@ defmodule Chesstrainer.Endgames.Tablebase.Backend.Fathom do alias Chesstrainer.Endgames.Tablebase.Local alias Chesstrainer.Endgames.Tablebase.Move alias Chesstrainer.Endgames.Tablebase.San + alias Chesstrainer.FEN @impl true def probe(fen) do @@ -32,6 +33,53 @@ defmodule Chesstrainer.Endgames.Tablebase.Backend.Fathom do end end + @doc """ + Probes the local tablebase without the remote fallback and returns only the + position's result. + + Used by the admin bulk importer, which must never import a result from + Lichess. The result is from the side-to-move's perspective, matching the + `category` on `Chesstrainer.Endgames.Tablebase`. + + Returns `{:error, :terminal}` for checkmate, stalemate, and positions with no + legal moves, because they cannot be played as puzzles. + """ + @spec probe_result_local(String.t()) :: + {:ok, Tablebase.result()} + | {:error, :not_configured | :illegal | :terminal | :unsupported} + def probe_result_local(fen) do + with {:ok, largest} <- Local.check(), + pieces when is_integer(pieces) <- FEN.piece_count(fen), + true <- pieces <= largest || {:error, :unsupported}, + {:ok, {root, moves}} <- Nif.probe(fen) do + case root do + :failed -> + {:error, :illegal} + + :checkmate -> + {:error, :terminal} + + :stalemate -> + {:error, :terminal} + + {wdl, _from, _to, _promotes, _ep, _dtz} -> + if moves == [] do + {:error, :terminal} + else + case Tablebase.normalize_wdl(wdl) do + :unknown -> {:error, :unsupported} + category -> {:ok, category} + end + end + end + else + {:error, :not_configured} = error -> error + {:error, :unsupported} = error -> error + {:error, _reason} -> {:error, :unsupported} + nil -> {:error, :unsupported} + end + end + defp fallback(fen) do (Application.get_env(:chesstrainer, :tablebase_fallback) || Lichess).probe(fen) end diff --git a/lib/chesstrainer/themes/theme.ex b/lib/chesstrainer/themes/theme.ex index 86dad1e..78c2eb8 100644 --- a/lib/chesstrainer/themes/theme.ex +++ b/lib/chesstrainer/themes/theme.ex @@ -2,6 +2,8 @@ defmodule Chesstrainer.Themes.Theme do use Ecto.Schema import Ecto.Changeset + @type t :: %__MODULE__{} + schema "themes" do field :name, :string field :category, Ecto.Enum, values: [endgame: "endgame", opening: "opening", tactic: "tactic"] diff --git a/lib/chesstrainer_web/components/theme_picker.ex b/lib/chesstrainer_web/components/theme_picker.ex new file mode 100644 index 0000000..d76cabe --- /dev/null +++ b/lib/chesstrainer_web/components/theme_picker.ex @@ -0,0 +1,131 @@ +defmodule ChesstrainerWeb.ThemePicker do + @moduledoc """ + Search-and-select control shared by the endgame form and bulk importer. + + The parent LiveView owns the assignments and handles the events: + `search_themes`, `add_theme`, `create_theme`, `create_theme_from_search` and + `remove_theme`. + """ + + use ChesstrainerWeb, :html + + alias Chesstrainer.Themes + + attr :selected_themes, :list, required: true + attr :theme_query, :string, required: true + attr :theme_results, :list, required: true + attr :can_create, :boolean, required: true + + def theme_picker(assigns) do + ~H""" +
+ Themes + +
+ <.badge :for={theme <- @selected_themes} color="primary"> + {theme.name} + + +
+ +
+ <.input + name="theme_search" + value={@theme_query} + phx-debounce="200" + autocomplete="off" + placeholder="Search or create a theme…" + /> + + +
+
+ """ + end + + @doc """ + Updates the search query, matching endgame themes and whether the query can be + created as a new theme. + """ + def assign_search(socket, query) do + trimmed = String.trim(query) + results = Themes.search(query, "endgame") + + can_create = + trimmed != "" and + not Enum.any?(results, &(String.downcase(&1.name) == String.downcase(trimmed))) + + socket + |> assign(:theme_query, query) + |> assign(:theme_results, results) + |> assign(:can_create, can_create) + end + + @doc """ + Creates a theme from the search box (or selects the existing match) and adds + it to the selected themes. + """ + def create_theme(socket, name) do + if String.trim(name) == "" do + socket + else + case Themes.get_or_create(name, "endgame") do + {:ok, theme} -> + socket |> add_selected_theme(theme) |> clear_search() + + {:error, _reason} -> + Phoenix.LiveView.put_flash(socket, :error, "Could not create that theme.") + end + end + end + + @doc """ + Clears the theme search results. + """ + def clear_search(socket) do + socket + |> assign(:theme_query, "") + |> assign(:theme_results, []) + |> assign(:can_create, false) + end + + @doc """ + Adds a theme to the selected themes, ignoring duplicates. + """ + def add_selected_theme(socket, theme) do + if Enum.any?(socket.assigns.selected_themes, &(&1.id == theme.id)) do + socket + else + assign(socket, :selected_themes, socket.assigns.selected_themes ++ [theme]) + end + end +end diff --git a/lib/chesstrainer_web/live/endgame_live/form.ex b/lib/chesstrainer_web/live/endgame_live/form.ex index 50bb606..56c8ba7 100644 --- a/lib/chesstrainer_web/live/endgame_live/form.ex +++ b/lib/chesstrainer_web/live/endgame_live/form.ex @@ -8,6 +8,7 @@ defmodule ChesstrainerWeb.EndgameLive.Form do alias Chesstrainer.Endgames.Endgame import ChesstrainerWeb.Board.BoardComponent + import ChesstrainerWeb.ThemePicker @impl true def render(assigns) do @@ -20,61 +21,12 @@ defmodule ChesstrainerWeb.EndgameLive.Form do <:subtitle>Use this form to manage endgame records in your database. -
- Themes - -
- <.badge :for={theme <- @selected_themes} color="primary"> - {theme.name} - - -
- -
- <.input - name="theme_search" - value={@theme_query} - phx-debounce="200" - autocomplete="off" - placeholder="Search or create a theme…" - /> - - -
-
+ <.theme_picker + selected_themes={@selected_themes} + theme_query={@theme_query} + theme_results={@theme_results} + can_create={@can_create} + /> <.form for={@form} id="endgame-form" phx-change="validate" phx-submit="save">
@@ -87,6 +39,7 @@ defmodule ChesstrainerWeb.EndgameLive.Form do value={rating_display(@form[:rating])} label="Rating" /> + <.input field={@form[:message]} type="textarea" label="Message" /> <.input field={@form[:notes]} type="textarea" label="Notes" /> <.input @@ -231,46 +184,6 @@ defmodule ChesstrainerWeb.EndgameLive.Form do end end - defp assign_search(socket, query) do - trimmed = String.trim(query) - results = Themes.search(query, "endgame") - - can_create = - trimmed != "" and - not Enum.any?(results, &(String.downcase(&1.name) == String.downcase(trimmed))) - - socket - |> assign(:theme_query, query) - |> assign(:theme_results, results) - |> assign(:can_create, can_create) - end - - defp create_theme(socket, name) do - if String.trim(name) == "" do - socket - else - case Themes.get_or_create(name, "endgame") do - {:ok, theme} -> socket |> add_selected_theme(theme) |> clear_search() - {:error, _reason} -> put_flash(socket, :error, "Could not create that theme.") - end - end - end - - defp clear_search(socket) do - socket - |> assign(:theme_query, "") - |> assign(:theme_results, []) - |> assign(:can_create, false) - end - - defp add_selected_theme(socket, theme) do - if Enum.any?(socket.assigns.selected_themes, &(&1.id == theme.id)) do - socket - else - assign(socket, :selected_themes, socket.assigns.selected_themes ++ [theme]) - end - end - # Ratings are stored as floats (Glicko); the admin form presents them as whole numbers. defp rating_display(%Phoenix.HTML.FormField{value: value}) when is_float(value), do: round(value) @@ -280,13 +193,8 @@ defmodule ChesstrainerWeb.EndgameLive.Form do # Shows the position as the admin types a FEN; hidden while the FEN is invalid or blank. # Parses directly rather than through Game.game_from_fen/2 so keystrokes never probe a tablebase. defp preview_game(fen) when is_binary(fen) do - case Chex.Parser.FEN.parse(fen) do - {:ok, game} -> - %{board: game.board, orientation: game.active_color, move_from_square: nil} - - _ -> - nil - end + {:ok, game} = Chex.Parser.FEN.parse(fen) + %{board: game.board, orientation: game.active_color, move_from_square: nil} rescue MatchError -> nil end diff --git a/lib/chesstrainer_web/live/endgame_live/import.ex b/lib/chesstrainer_web/live/endgame_live/import.ex new file mode 100644 index 0000000..36a8334 --- /dev/null +++ b/lib/chesstrainer_web/live/endgame_live/import.ex @@ -0,0 +1,333 @@ +defmodule ChesstrainerWeb.EndgameLive.Import do + @moduledoc """ + Bulk FEN importer for the admin endgame section. + + Pasted FENs are validated and probed against the local Syzygy tablebase only; + the remote (Lichess) fallback is deliberately not used here. + """ + + use ChesstrainerWeb, :live_view + + alias Chesstrainer.Endgames.BulkImport + alias Chesstrainer.Endgames.Tablebase.Local + + import ChesstrainerWeb.ThemePicker + + @default_rating 1500 + + @impl true + def render(assigns) do + ~H""" + + + + <.header> + Import endgames + <:subtitle> + Paste one FEN per line. Results are read from the local Syzygy tablebase, + so positions must have five pieces or fewer. + + <:actions> + <.button navigate={~p"/admin/endgames"}> + <.icon name="hero-arrow-left" /> All endgames + + + + +

+ {elem(@tablebase_status, 1)} +

+ +
+ <.input + id="import-fens" + name="fens" + type="textarea" + label="FENs" + value={@fen_text} + rows="10" + class="font-mono" + placeholder="8/8/3k4/8/8/3K1R2/8/8 w - - 0 1" + autocomplete="off" + /> + +
+ <.input + id="import-rating" + name="rating" + type="number" + label="Rating for all imported endgames" + value={@rating} + min="1" + /> +
+ + <.button type="submit" variant="primary" phx-disable-with="Looking up…"> + Look up results + +
+ + <.theme_picker + selected_themes={@selected_themes} + theme_query={@theme_query} + theme_results={@theme_results} + can_create={@can_create} + /> + +
+
+
+ {summary(@entries)} +
+ +
+ <.button type="button" phx-click="toggle_all" phx-value-selected="true"> + Select ready + + <.button type="button" phx-click="toggle_all" phx-value-selected="false"> + Clear + +
+
+ +

+ Only the first {BulkImport.max_fens()} FENs were considered. +

+ + + + + + + + + + + + + + + + + + + + + + + + +
IncludeLineFENKeyColorResultStatus
+ + {entry.line}{entry.fen}{entry.key || "—"}{entry.color || "—"}{entry.result || "—"} + <.badge color={status_color(entry.status)}>{status_label(entry.status)} + + {entry.reason} + +
+ +
+ <.button + type="button" + variant="primary" + phx-click="import" + disabled={selected_count(@entries) == 0} + phx-disable-with="Importing…" + > + Import {selected_count(@entries)} endgame(s) + + + + {selected_count(@entries)} of {count(@entries, :ready)} ready line(s) selected. + +
+
+
+ """ + end + + @impl true + def mount(_params, _session, socket) do + {:ok, + socket + |> assign(:page_title, "Import endgames") + |> assign(:tablebase_status, tablebase_status()) + |> assign(:fen_text, "") + |> assign(:rating, @default_rating) + |> assign(:entries, nil) + |> assign(:too_many?, false) + |> assign(:selected_themes, []) + |> assign(:theme_query, "") + |> assign(:theme_results, []) + |> assign(:can_create, false)} + end + + @impl true + def handle_event("search_themes", %{"theme_search" => query}, socket) do + {:noreply, assign_search(socket, query)} + end + + def handle_event("add_theme", %{"id" => id}, socket) do + theme = Chesstrainer.Themes.get_theme!(id) + {:noreply, socket |> add_selected_theme(theme) |> clear_search()} + end + + def handle_event("create_theme", %{"name" => name}, socket) do + {:noreply, create_theme(socket, name)} + end + + def handle_event("create_theme_from_search", %{"theme_search" => name}, socket) do + {:noreply, create_theme(socket, name)} + end + + def handle_event("remove_theme", %{"id" => id}, socket) do + selected = Enum.reject(socket.assigns.selected_themes, &(Integer.to_string(&1.id) == id)) + {:noreply, assign(socket, :selected_themes, selected)} + end + + def handle_event("lookup", %{"fens" => fens} = params, socket) do + rating = parse_rating(params["rating"]) + + socket = + assign(socket, + fen_text: fens, + rating: rating, + entries: nil, + too_many?: false + ) + + if String.trim(fens) == "" do + {:noreply, put_flash(socket, :error, "Paste at least one FEN.")} + else + {:noreply, start_async(socket, :lookup, fn -> BulkImport.preview(fens) end)} + end + end + + def handle_event("toggle_entry", %{"line" => line}, socket) do + line = String.to_integer(line) + + entries = + Enum.map(socket.assigns.entries, fn entry -> + if entry.line == line, do: %{entry | selected: not entry.selected}, else: entry + end) + + {:noreply, assign(socket, :entries, entries)} + end + + def handle_event("toggle_all", %{"selected" => selected}, socket) do + selected? = selected == "true" + + entries = + Enum.map(socket.assigns.entries, fn entry -> + if entry.status == :ready, do: %{entry | selected: selected?}, else: entry + end) + + {:noreply, assign(socket, :entries, entries)} + end + + def handle_event("import", _params, socket) do + entries = + Enum.filter(socket.assigns.entries || [], &(&1.status == :ready and &1.selected)) + + if entries == [] do + {:noreply, put_flash(socket, :error, "Select at least one ready endgame to import.")} + else + %{imported: imported, skipped: skipped} = + BulkImport.commit(entries, socket.assigns.selected_themes, socket.assigns.rating) + + message = + "Imported #{imported} endgame(s)." <> + if(skipped > 0, do: " #{skipped} skipped.", else: "") + + {:noreply, + socket + |> put_flash(:info, message) + |> push_navigate(to: ~p"/admin/endgames")} + end + end + + @impl true + def handle_async(:lookup, {:ok, %{entries: entries, too_many?: too_many?}}, socket) do + entries = Enum.map(entries, &Map.put(&1, :selected, &1.status == :ready)) + + {:noreply, assign(socket, entries: entries, too_many?: too_many?)} + end + + def handle_async(:lookup, {:exit, reason}, socket) do + {:noreply, put_flash(socket, :error, "Could not look up the FENs: #{inspect(reason)}")} + end + + defp tablebase_status do + case Local.check() do + {:ok, 0} -> + {:error, + "The local Syzygy tablebase directory contains no files, so imports are disabled."} + + {:ok, largest} -> + {:ok, "Local Syzygy tablebase ready (up to #{largest} pieces)."} + + {:error, :not_configured} -> + {:error, "Local tablebase is not configured. Set TABLEBASE_PATH to enable imports."} + + {:error, reason} -> + {:error, "Local tablebase failed to load: #{inspect(reason)}"} + end + end + + defp parse_rating(rating) when is_integer(rating) and rating > 0, do: rating + + defp parse_rating(rating) when is_binary(rating) do + case Integer.parse(String.trim(rating)) do + {value, ""} when value > 0 -> value + _ -> @default_rating + end + end + + defp parse_rating(_rating), do: @default_rating + + defp count(entries, status), do: Enum.count(entries, &(&1.status == status)) + + defp summary(entries) do + "#{length(entries)} line(s): " <> + "#{count(entries, :ready)} ready, " <> + "#{count(entries, :duplicate)} duplicate(s), " <> + "#{count(entries, :invalid_fen)} invalid, " <> + "#{count(entries, :too_many_pieces)} too many pieces, " <> + "#{count(entries, :terminal)} terminal, " <> + "#{count(entries, :illegal)} illegal, " <> + "#{count(entries, :unsupported)} unsupported." + end + + defp selected_count(entries) do + Enum.count(entries, &(&1.status == :ready and &1.selected)) + end + + defp status_label(:ready), do: "Ready" + defp status_label(:duplicate), do: "Duplicate" + defp status_label(:invalid_fen), do: "Invalid FEN" + defp status_label(:too_many_pieces), do: "Too many pieces" + defp status_label(:illegal), do: "Illegal" + defp status_label(:terminal), do: "Terminal" + defp status_label(:unsupported), do: "Unsupported" + + defp status_color(:ready), do: "primary" + defp status_color(_status), do: "warning" +end diff --git a/lib/chesstrainer_web/live/endgame_live/index.ex b/lib/chesstrainer_web/live/endgame_live/index.ex index 8b0a8f3..c22bc1c 100644 --- a/lib/chesstrainer_web/live/endgame_live/index.ex +++ b/lib/chesstrainer_web/live/endgame_live/index.ex @@ -14,6 +14,9 @@ defmodule ChesstrainerWeb.EndgameLive.Index do <.header> Listing Endgames <:actions> + <.button navigate={~p"/admin/endgames/import"}> + <.icon name="hero-arrow-up-tray" /> Bulk import + <.button variant="primary" navigate={~p"/admin/endgames/new"}> <.icon name="hero-plus" /> New Endgame diff --git a/lib/chesstrainer_web/router.ex b/lib/chesstrainer_web/router.ex index f948f84..6a41f48 100644 --- a/lib/chesstrainer_web/router.ex +++ b/lib/chesstrainer_web/router.ex @@ -50,6 +50,7 @@ defmodule ChesstrainerWeb.Router do ] do live "/endgames", EndgameLive.Index, :index live "/endgames/new", EndgameLive.Form, :new + live "/endgames/import", EndgameLive.Import, :new live "/endgames/:id", EndgameLive.Show, :show live "/endgames/:id/edit", EndgameLive.Form, :edit diff --git a/test/chesstrainer/endgames/bulk_import_test.exs b/test/chesstrainer/endgames/bulk_import_test.exs new file mode 100644 index 0000000..a1e94c4 --- /dev/null +++ b/test/chesstrainer/endgames/bulk_import_test.exs @@ -0,0 +1,141 @@ +defmodule Chesstrainer.Endgames.BulkImportTest do + # Not async: the tablebase path is application config and Fathom keeps global + # state, so other tests must not flip the local backend while this runs. + use Chesstrainer.DataCase, async: false + + import Chesstrainer.ThemesFixtures + + alias Chesstrainer.Endgames + alias Chesstrainer.Endgames.BulkImport + alias Chesstrainer.Endgames.Endgame + alias Chesstrainer.Repo + + @krk "4k3/8/8/8/8/8/8/R3K3 w - - 0 1" + @mate "7k/6Q1/5K2/8/8/8/8/8 b - - 0 1" + @six_pieces "4k2r/8/8/8/8/8/8/R2QK2R w - - 0 1" + + describe "preview/1" do + test "classifies a position the local tablebase can answer" do + assert %{entries: [entry], too_many?: false} = BulkImport.preview(@krk) + + assert entry.status == :ready + assert entry.result == :win + assert entry.color == :white + assert entry.key == "KR v K" + assert entry.fen == @krk + assert entry.reason == nil + end + + test "marks checkmate positions as terminal" do + assert %{entries: [entry]} = BulkImport.preview(@mate) + + assert entry.status == :terminal + assert entry.result == nil + end + + test "rejects positions with more than five pieces" do + assert %{entries: [entry]} = BulkImport.preview(@six_pieces) + + assert entry.status == :too_many_pieces + assert entry.reason =~ "5 or fewer pieces" + end + + test "rejects malformed FENs" do + assert %{entries: [entry]} = BulkImport.preview("not a fen") + + assert entry.status == :invalid_fen + assert entry.reason =~ "6 space-separated fields" + end + + test "reports duplicates within the batch without probing twice" do + assert %{entries: [first, second]} = BulkImport.preview("#{@krk}\n#{@krk}") + + assert first.status == :ready + assert second.status == :duplicate + assert second.reason == "Duplicate FEN in this batch" + end + + test "reports FENs already stored as duplicates" do + {:ok, _endgame} = + Endgames.create_endgame(%{fen: @krk, color: :white, result: :win}) + + assert %{entries: [entry]} = BulkImport.preview(@krk) + + assert entry.status == :duplicate + assert entry.reason == "FEN already exists" + end + + test "ignores blank lines and comments" do + text = "\n# a comment\n#{@krk}\n\n" + + assert %{entries: [entry]} = BulkImport.preview(text) + assert entry.status == :ready + end + + test "caps the input at max_fens/0" do + text = Enum.join(List.duplicate("not a fen", BulkImport.max_fens() + 1), "\n") + + assert %{entries: entries, too_many?: true} = BulkImport.preview(text) + assert length(entries) == BulkImport.max_fens() + end + + test "reports unsupported when the local tablebase is not configured" do + previous = Application.get_env(:chesstrainer, :tablebase_path) + + on_exit(fn -> Application.put_env(:chesstrainer, :tablebase_path, previous) end) + + Application.put_env(:chesstrainer, :tablebase_path, nil) + + assert %{entries: [entry]} = BulkImport.preview(@krk) + assert entry.status == :unsupported + assert entry.reason =~ "not configured" + end + end + + describe "commit/3" do + test "inserts ready entries with the batch rating and themes" do + theme = theme_fixture(%{name: "Lucena"}) + + assert %{entries: entries} = BulkImport.preview(@krk) + assert %{imported: 1, skipped: 0} = BulkImport.commit(entries, [theme], 1700) + + endgame = + Repo.get_by!(Endgame, fen: @krk) + |> Repo.preload(:themes) + + assert endgame.color == :white + assert endgame.result == :win + assert endgame.key == "KR v K" + assert endgame.rating == 1700.0 + assert [%{id: theme_id}] = endgame.themes + assert theme_id == theme.id + end + + test "ignores entries that are not ready" do + assert %{entries: entries} = BulkImport.preview("not a fen") + + assert %{imported: 0, skipped: 1} = BulkImport.commit(entries) + assert Repo.aggregate(Endgame, :count) == 0 + end + + test "skips rows that collide with an existing FEN" do + {:ok, _existing} = + Endgames.create_endgame(%{fen: @krk, color: :white, result: :win}) + + entries = [ + %{ + line: 1, + fen: @krk, + key: "KR v K", + color: :white, + result: :win, + status: :ready, + reason: nil + } + ] + + assert %{imported: 0, skipped: 1} = BulkImport.commit(entries) + assert Repo.aggregate(Endgame, :count) == 1 + end + end +end diff --git a/test/chesstrainer_web/admin_gating_test.exs b/test/chesstrainer_web/admin_gating_test.exs index 8543feb..6b110b8 100644 --- a/test/chesstrainer_web/admin_gating_test.exs +++ b/test/chesstrainer_web/admin_gating_test.exs @@ -46,7 +46,8 @@ defmodule ChesstrainerWeb.AdminGatingTest do ~p"/admin/themes", ~p"/admin/themes/new", ~p"/admin/users", - ~p"/admin/users/new" + ~p"/admin/users/new", + ~p"/admin/endgames/import" ] do assert redirected_to(get(conn, path)) == ~p"/" end diff --git a/test/chesstrainer_web/live/endgame_live/import_test.exs b/test/chesstrainer_web/live/endgame_live/import_test.exs new file mode 100644 index 0000000..9bf08a6 --- /dev/null +++ b/test/chesstrainer_web/live/endgame_live/import_test.exs @@ -0,0 +1,98 @@ +defmodule ChesstrainerWeb.EndgameLive.ImportTest do + use ChesstrainerWeb.ConnCase + + import Phoenix.LiveViewTest + import Chesstrainer.ThemesFixtures + + alias Chesstrainer.Endgames + alias Chesstrainer.Endgames.Endgame + alias Chesstrainer.Repo + + @krk "4k3/8/8/8/8/8/8/R3K3 w - - 0 1" + @mate "7k/6Q1/5K2/8/8/8/8/8 b - - 0 1" + @six_pieces "4k2r/8/8/8/8/8/8/R2QK2R w - - 0 1" + + setup :register_and_log_in_admin + + test "shows the local tablebase status", %{conn: conn} do + {:ok, _view, html} = live(conn, ~p"/admin/endgames/import") + + assert html =~ "Local Syzygy tablebase ready" + end + + test "looks up FENs and imports the selected ones", %{conn: conn} do + theme = theme_fixture(%{name: "Lucena"}) + + {:ok, view, _html} = live(conn, ~p"/admin/endgames/import") + + view + |> form("#endgame-import-form", %{fens: @krk, rating: "1700"}) + |> render_submit() + + html = render_async(view, 5_000) + + assert html =~ "Ready" + assert html =~ "KR v K" + assert html =~ "win" + + view |> form("#theme-form", %{theme_search: "Luc"}) |> render_change() + view |> element("button", "Lucena") |> render_click() + + view |> element("button", "Import 1 endgame(s)") |> render_click() + + assert_redirect(view, ~p"/admin/endgames") + + endgame = Repo.get_by!(Endgame, fen: @krk) |> Repo.preload(:themes) + + assert endgame.rating == 1700.0 + assert endgame.result == :win + assert endgame.color == :white + assert [%{id: theme_id}] = endgame.themes + assert theme_id == theme.id + end + + test "explains why lines cannot be imported", %{conn: conn} do + {:ok, view, _html} = live(conn, ~p"/admin/endgames/import") + + view + |> form("#endgame-import-form", %{ + fens: "#{@six_pieces}\nnot a fen\n#{@mate}", + rating: "1500" + }) + |> render_submit() + + html = render_async(view, 5_000) + + assert html =~ "Too many pieces" + assert html =~ "Invalid FEN" + assert html =~ "Terminal" + refute html =~ "Import 1 endgame(s)" + end + + test "does not import when every line is a duplicate", %{conn: conn} do + {:ok, _endgame} = Endgames.create_endgame(%{fen: @krk, color: :white, result: :win}) + + {:ok, view, _html} = live(conn, ~p"/admin/endgames/import") + + view + |> form("#endgame-import-form", %{fens: @krk, rating: "1500"}) + |> render_submit() + + html = render_async(view, 5_000) + + assert html =~ "Duplicate" + assert html =~ "FEN already exists" + assert html =~ "Import 0 endgame(s)" + end + + test "warns when the local tablebase is not configured", %{conn: conn} do + previous = Application.get_env(:chesstrainer, :tablebase_path) + on_exit(fn -> Application.put_env(:chesstrainer, :tablebase_path, previous) end) + + Application.put_env(:chesstrainer, :tablebase_path, nil) + + {:ok, _view, html} = live(conn, ~p"/admin/endgames/import") + + assert html =~ "Local tablebase is not configured" + end +end