Add public codes for player URLs and admin endgame search

Player-facing endgame links now use a 10-character Crockford base32 public code instead of the database UUID; admin routes keep the UUID. Existing rows are backfilled by migration, and the admin Show page displays and copies the code.

The admin endgame index gains its own search box: exact database id, exact public code, or a FEN / FEN fragment. A single match opens the endgame; multiple matches list below; nothing found says so.

Closes #44
Closes #45
This commit is contained in:
2026-11-03 22:46:38 +01:00
parent 160e8629b7
commit 49736d186c
18 changed files with 544 additions and 64 deletions
@@ -38,7 +38,7 @@ defmodule ChesstrainerWeb.EndgameLive.FormTest do
attrs = %{fen: @fen, color: :white, result: :win, rating: 1625}
{:ok, endgame} = Endgames.create_endgame(attrs)
{:ok, view, _html} = live(conn, ~p"/admin/endgames/#{endgame}/edit")
{:ok, view, _html} = live(conn, ~p"/admin/endgames/#{endgame.id}/edit")
html = view |> element("#endgame-form input[name='endgame[rating]']") |> render()
@@ -103,7 +103,7 @@ defmodule ChesstrainerWeb.EndgameLive.FormTest do
attrs = %{fen: @fen, color: :white, result: :win, rating: 1500}
{:ok, endgame} = Endgames.create_endgame(attrs, [theme])
{:ok, view, _html} = live(conn, ~p"/admin/endgames/#{endgame}/edit")
{:ok, view, _html} = live(conn, ~p"/admin/endgames/#{endgame.id}/edit")
assert render(view) =~ "Lucena"
@@ -43,16 +43,68 @@ defmodule ChesstrainerWeb.EndgameLive.IndexTest do
{:ok, view, html} = live(conn, ~p"/admin/endgames")
# collapsed: the endgame row (and its themes) are not rendered yet
refute html =~ ~p"/endgames/#{endgame}/play"
refute html =~ ~p"/endgames/#{endgame.public_code}/play"
refute html =~ "Lucena"
view |> element("#key-group-K-v-K button") |> render_click()
html = render(view)
assert html =~ ~p"/endgames/#{endgame}/play"
assert html =~ ~p"/endgames/#{endgame.public_code}/play"
assert html =~ "Lucena"
end
describe "search" do
test "opens the endgame found by public code", %{conn: conn} do
endgame = endgame_fixture()
{:ok, view, _html} = live(conn, ~p"/admin/endgames")
assert {:error, {:live_redirect, %{to: path}}} =
view
|> form("#endgame-search-form", %{query: endgame.public_code})
|> render_submit()
assert path == "/admin/endgames/#{endgame.id}"
end
test "opens the endgame found by database id", %{conn: conn} do
endgame = endgame_fixture()
{:ok, view, _html} = live(conn, ~p"/admin/endgames")
assert {:error, {:live_redirect, %{to: path}}} =
view |> form("#endgame-search-form", %{query: endgame.id}) |> render_submit()
assert path == "/admin/endgames/#{endgame.id}"
end
test "lists every endgame containing a FEN fragment", %{conn: conn} do
kr = endgame_fixture(fen: "8/8/3k4/8/8/3K1R2/8/8 w - - 0 1")
kq = endgame_fixture(fen: "8/8/3k4/8/8/3K1Q2/8/8 w - - 0 1")
{:ok, view, _html} = live(conn, ~p"/admin/endgames")
html = view |> form("#endgame-search-form", %{query: "3k4"}) |> render_submit()
assert html =~ ~s{/admin/endgames/#{kr.id}}
assert html =~ ~s{/admin/endgames/#{kq.id}}
end
test "reports when nothing matches, and clears back to the grouped list", %{conn: conn} do
endgame_fixture()
{:ok, view, _html} = live(conn, ~p"/admin/endgames")
html = view |> form("#endgame-search-form", %{query: "no such endgame"}) |> render_submit()
assert html =~ "No endgame matches"
view |> element("#clear-endgame-search") |> render_click()
assert has_element?(view, "#key-group-K-v-K")
refute render(view) =~ "No endgame matches"
end
end
test "filters by theme", %{conn: conn} do
lucena = theme_fixture(%{name: "Lucena"})
philidor = theme_fixture(%{name: "Philidor"})
@@ -69,6 +69,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
{:ok, key} = Chesstrainer.Key.from_fen(fen)
Chesstrainer.Repo.insert!(%Chesstrainer.Endgames.Endgame{
public_code: Chesstrainer.Endgames.Endgame.generate_public_code(),
fen: fen,
color: :black,
key: key,
@@ -95,7 +96,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
seed_tablebase(after_reply, category: :win, moves: grading_moves())
seed_tablebase(after_losing, category: :draw, moves: [losing_reply])
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame}/play")
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame.public_code}/play")
%{proxy: {ref, _topic, _}} = view
assert_receive {^ref, {:push_event, "init_board", _payload}}, 100
@@ -137,7 +138,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
endgame = endgame_fixture(color: :white)
seed_tablebase(endgame.fen)
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame}/play")
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame.public_code}/play")
assert has_element?(view, "#endgame-board")
assert_init_board(view, endgame.fen, "white", "white")
@@ -147,7 +148,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
endgame = endgame_fixture(color: :black)
seed_tablebase(endgame.fen)
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame}/play")
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame.public_code}/play")
assert_init_board(view, endgame.fen, "black", "black")
end
@@ -163,7 +164,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
seed_tablebase(endgame.fen)
{:ok, _view, html} = live(conn, ~p"/endgames/#{endgame}/play")
{:ok, _view, html} = live(conn, ~p"/endgames/#{endgame.public_code}/play")
assert html =~ "KQ v K"
assert html =~ "White to move"
@@ -176,6 +177,14 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
live(conn, ~p"/endgames/00000000-0000-0000-0000-000000000000/play")
end
end
test "does not resolve by database id", %{conn: conn} do
endgame = endgame_fixture()
assert_raise Ecto.NoResultsError, fn ->
live(conn, "/endgames/#{endgame.id}/play")
end
end
end
describe "current scope" do
@@ -187,7 +196,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
{:ok, view, _html} =
conn
|> log_in_user(user)
|> live(~p"/endgames/#{endgame}/play")
|> live(~p"/endgames/#{endgame.public_code}/play")
assert :sys.get_state(view.pid).socket.assigns.current_scope.user.id == user.id
end
@@ -196,7 +205,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
endgame = endgame_fixture()
seed_tablebase(endgame.fen)
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame}/play")
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame.public_code}/play")
assert :sys.get_state(view.pid).socket.assigns.current_scope == nil
end
@@ -211,7 +220,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
Ratelimiters.lichess_add_cooldown()
on_exit(fn -> Ratelimiters.lichess_reset_cooldown() end)
{:ok, view, html} = live(conn, ~p"/endgames/#{endgame}/play")
{:ok, view, html} = live(conn, ~p"/endgames/#{endgame.public_code}/play")
assert html =~ "Tablebase unavailable"
@@ -222,7 +231,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
test "blocks an out-of-range endgame as not covered", %{conn: conn} do
endgame = insert_out_of_range_endgame()
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame}/play")
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame.public_code}/play")
html = render(view)
assert html =~ "Tablebase unavailable"
@@ -235,7 +244,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
endgame = endgame_fixture()
seed_tablebase(endgame.fen)
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame}/play")
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame.public_code}/play")
view
|> render_hook("chess_fen_invalid", %{
@@ -254,7 +263,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
endgame = endgame_fixture()
seed_tablebase(endgame.fen, moves: grading_moves())
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame}/play")
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame.public_code}/play")
play_move(view, endgame, "d1", "d7", "Qd7+")
html = render(view)
@@ -267,7 +276,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
endgame = endgame_fixture()
seed_tablebase(endgame.fen, moves: grading_moves())
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame}/play")
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame.public_code}/play")
play_move(view, endgame, "d1", "d5", "Qd5")
@@ -278,7 +287,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
endgame = endgame_fixture()
seed_tablebase(endgame.fen, moves: grading_moves())
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame}/play")
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame.public_code}/play")
play_move(view, endgame, "d1", "d4", "Qd4")
@@ -289,7 +298,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
endgame = endgame_fixture()
seed_tablebase(endgame.fen, moves: grading_moves())
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame}/play")
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame.public_code}/play")
render_hook(view, "endgame_move_played", %{
"from" => "d1",
@@ -315,7 +324,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
seed_tablebase(fen_after, category: :loss, moves: [reply])
seed_tablebase(fen_after_reply, category: :win, moves: grading_moves())
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame}/play")
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame.public_code}/play")
render_hook(view, "endgame_move_played", %{
"from" => "d1",
@@ -341,7 +350,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
endgame = endgame_fixture()
seed_tablebase(endgame.fen, moves: grading_moves())
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame}/play")
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame.public_code}/play")
play_move(view, endgame, "d1", "d4", "Qd4")
@@ -357,7 +366,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
endgame = endgame_fixture()
seed_tablebase(endgame.fen)
{:ok, _view, _html} = live(conn, ~p"/endgames/#{endgame}/play")
{:ok, _view, _html} = live(conn, ~p"/endgames/#{endgame.public_code}/play")
assert Endgames.get_endgame!(endgame.id).times_attempted == 1
end
@@ -369,7 +378,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
seed_tablebase(endgame.fen, moves: grading_moves())
seed_tablebase(fen_after, checkmate: true, moves: [])
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame}/play")
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame.public_code}/play")
render_hook(view, "endgame_move_played", %{
"from" => "d1",
@@ -389,7 +398,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
moves = [%Tablebase.Move{uci: "a7a8q", san: "a8=Q", category: :loss, dtz: -1}]
seed_tablebase(endgame.fen, moves: moves)
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame}/play")
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame.public_code}/play")
render_hook(view, "endgame_move_played", %{
"from" => "a7",
@@ -409,7 +418,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
endgame = endgame_fixture()
seed_tablebase(endgame.fen, moves: grading_moves())
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame}/play")
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame.public_code}/play")
play_move(view, endgame, "d1", "d4", "Qd4")
@@ -430,7 +439,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
seed_tablebase(endgame.fen, moves: grading_moves())
seed_tablebase(fen_after, checkmate: true, moves: [])
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame}/play")
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame.public_code}/play")
render_hook(view, "endgame_move_played", %{
"from" => "d1",
@@ -452,7 +461,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
endgame = endgame_fixture()
seed_tablebase(endgame.fen, moves: grading_moves())
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame}/play")
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame.public_code}/play")
play_move(view, endgame, "d1", "d4", "Qd4")
@@ -469,7 +478,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
seed_tablebase(endgame.fen, moves: grading_moves())
seed_tablebase(fen_after, checkmate: true, moves: [])
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame}/play")
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame.public_code}/play")
render_hook(view, "endgame_move_played", %{
"from" => "d1",
@@ -494,7 +503,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
seed_tablebase(endgame.fen, moves: grading_moves())
seed_tablebase(fen_after, checkmate: true, moves: [])
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame}/play")
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame.public_code}/play")
play = fn ->
render_hook(view, "endgame_move_played", %{
@@ -527,7 +536,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
endgame = endgame_fixture(rating: 1800)
seed_tablebase(endgame.fen, moves: grading_moves())
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame}/play")
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame.public_code}/play")
play_move(view, endgame, "d1", "d4", "Qd4")
@@ -545,7 +554,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
endgame = endgame_fixture(result: :draw)
seed_tablebase(endgame.fen, category: :draw, moves: [])
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame}/play")
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame.public_code}/play")
assert has_element?(view, "#endgame-board")
assert render(view) =~ "Make a move"
@@ -559,7 +568,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
seed_tablebase(endgame.fen, category: :draw, moves: draw_moves())
seed_tablebase(fen_after, category: :draw, moves: [reply])
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame}/play")
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame.public_code}/play")
render_hook(view, "endgame_move_played", %{
"from" => "d1",
@@ -581,7 +590,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
endgame = endgame_fixture(result: :draw)
seed_tablebase(endgame.fen, category: :draw, moves: draw_moves())
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame}/play")
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame.public_code}/play")
render_hook(view, "endgame_move_played", %{
"from" => "d1",
@@ -603,7 +612,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
endgame = endgame_fixture(result: :draw)
seed_tablebase(endgame.fen, category: :draw, moves: draw_moves())
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame}/play")
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame.public_code}/play")
render_hook(view, "endgame_move_played", %{
"from" => "d1",
@@ -627,7 +636,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
seed_tablebase(endgame.fen, category: :draw, moves: draw_moves())
seed_tablebase(fen_after, category: :draw, moves: [reply])
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame}/play")
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame.public_code}/play")
render_hook(view, "endgame_move_played", %{
"from" => "d1",
@@ -651,7 +660,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
endgame = endgame_fixture()
seed_tablebase(endgame.fen, moves: grading_moves())
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame}/play")
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame.public_code}/play")
play_move(view, endgame, "d1", "d7", "Qd7+")
assert render(view) =~ "Qd7+"
@@ -694,7 +703,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
seed_tablebase(endgame.fen, category: :draw, moves: moves)
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame}/play")
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame.public_code}/play")
render_hook(view, "endgame_move_played", %{
"from" => "d1",
@@ -730,7 +739,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
moves = [%Tablebase.Move{uci: "a7a8q", san: "a8=Q", category: :loss, dtz: -1}]
seed_tablebase(endgame.fen, moves: moves)
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame}/play")
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame.public_code}/play")
render_hook(view, "endgame_move_played", %{
"from" => "a7",
@@ -23,11 +23,22 @@ defmodule ChesstrainerWeb.EndgameLive.PlayerIndexTest do
view |> element("#key-group-K-v-K button") |> render_click()
html = render(view)
assert html =~ ~p"/endgames/#{endgame}/play"
assert html =~ ~p"/endgames/#{other}/play"
assert html =~ ~p"/endgames/#{endgame.public_code}/play"
assert html =~ ~p"/endgames/#{other.public_code}/play"
assert html =~ "Lucena"
end
test "links to the public code, never the database id", %{conn: conn} do
endgame = endgame_fixture()
{:ok, view, _html} = live(conn, ~p"/endgames")
view |> element("#key-group-K-v-K button") |> render_click()
html = render(view)
assert html =~ ~s{/endgames/#{endgame.public_code}/play}
refute html =~ endgame.id
end
test "filters by theme", %{conn: conn} do
lucena = theme_fixture(%{name: "Lucena"})
philidor = theme_fixture(%{name: "Philidor"})
@@ -49,7 +60,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayerIndexTest do
test "navigating to a play page renders the board", %{conn: conn} do
endgame = endgame_fixture()
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame}/play")
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame.public_code}/play")
assert has_element?(view, "#endgame-board")
assert has_element?(view, ~s{a[href="/endgames"]})
@@ -0,0 +1,26 @@
defmodule ChesstrainerWeb.EndgameLive.ShowTest do
use ChesstrainerWeb.ConnCase
import Phoenix.LiveViewTest
import Chesstrainer.EndgamesFixtures
setup :register_and_log_in_admin
test "shows the public code alongside the database id", %{conn: conn} do
endgame = endgame_fixture()
{:ok, view, _html} = live(conn, ~p"/admin/endgames/#{endgame.id}")
assert has_element?(view, "li.list-row", "Public code")
assert has_element?(view, "li.list-row", endgame.public_code)
assert has_element?(view, "h1", endgame.id)
end
test "links to the player surface by public code", %{conn: conn} do
endgame = endgame_fixture()
{:ok, view, _html} = live(conn, ~p"/admin/endgames/#{endgame.id}")
assert has_element?(view, ~s{a[href="/endgames/#{endgame.public_code}/play"]})
end
end