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
+5
View File
@@ -31,6 +31,11 @@ pieces each side has, written strongest side first and colour-agnostic
(e.g. "KQ v KR"). Used to group and search endgames by material. (e.g. "KQ v KR"). Used to group and search endgames by material.
_Avoid_: Material signature, hash _Avoid_: Material signature, hash
**Public code**:
The short, stable identifier an endgame is played under in player-facing
links, distinct from its database id and safe to share.
_Avoid_: Slug, UUID, endgame id, token
## Identity & access ## Identity & access
**User**: **User**:
@@ -0,0 +1,18 @@
# Player-facing endgame URLs use a public code
Player links address an endgame by a short random **public code** rather than
its database UUID. The UUID stays the admin identity: admin routes mount by
id and the Show page displays both. The code is stable for the row's lifetime
and unguessable, so a shared play link neither exposes a primary key nor
invites enumeration. Keys were rejected as the handle because many endgames
share one (that is how the list groups them), and Key-plus-ordinal would break
when endgames are added or removed. A FEN hash was rejected as longer and
derivable without hiding anything new.
## Consequences
- Admin URLs and the admin list keep the UUID, so debugging still lines up
with the database. Admin search accepts either identifier plus a FEN or FEN
fragment.
- A public code is generated on insert and backfilled by migration; it is
never regenerated on update.
+71 -1
View File
@@ -92,6 +92,63 @@ defmodule Chesstrainer.Endgames do
""" """
def get_endgame!(id), do: Repo.get!(Endgame, id) def get_endgame!(id), do: Repo.get!(Endgame, id)
@doc """
Gets a single endgame by its public code, raising if it does not exist.
"""
def get_endgame_by_public_code!(public_code) do
Repo.get_by!(Endgame, public_code: public_code)
end
@doc """
Finds endgames by database id, public code, or FEN.
A full FEN matches exactly (after normalisation); a fragment matches any FEN
containing it. Themes are preloaded. A blank query returns `[]`.
"""
def search(query) do
query = String.trim(query || "")
if query == "" do
[]
else
[search_by_id(query), search_by_public_code(query), search_by_fen(query)]
|> Enum.reject(&is_nil/1)
|> List.flatten()
|> Enum.uniq_by(& &1.id)
end
end
defp search_by_id(query) do
case Ecto.UUID.cast(query) do
{:ok, id} -> Repo.get(Endgame, id) |> preload_themes()
:error -> nil
end
end
defp search_by_public_code(query) do
Repo.get_by(Endgame, public_code: query) |> preload_themes()
end
defp search_by_fen(query) do
pattern = "%" <> escape_like(Chesstrainer.FEN.normalize(query)) <> "%"
Endgame
|> where([e], like(e.fen, ^pattern))
|> preload(:themes)
|> order_by([e], asc: e.key, asc: e.fen)
|> Repo.all()
end
defp preload_themes(nil), do: nil
defp preload_themes(endgame), do: Repo.preload(endgame, :themes)
defp escape_like(value) do
value
|> String.replace("\\", "\\\\")
|> String.replace("%", "\\%")
|> String.replace("_", "\\_")
end
@doc """ @doc """
Creates a endgame. Creates a endgame.
@@ -104,11 +161,24 @@ defmodule Chesstrainer.Endgames do
{:error, %Ecto.Changeset{}} {:error, %Ecto.Changeset{}}
""" """
def create_endgame(attrs, themes \\ []) do def create_endgame(attrs, themes \\ []), do: insert_endgame(attrs, themes, 3)
defp insert_endgame(attrs, themes, attempts) do
result =
%Endgame{} %Endgame{}
|> Endgame.changeset(attrs) |> Endgame.changeset(attrs)
|> Ecto.Changeset.put_assoc(:themes, themes) |> Ecto.Changeset.put_assoc(:themes, themes)
|> Repo.insert() |> Repo.insert()
case result do
{:error, changeset} when attempts > 1 ->
if Keyword.has_key?(changeset.errors, :public_code),
do: insert_endgame(attrs, themes, attempts - 1),
else: result
other ->
other
end
end end
@doc """ @doc """
+30
View File
@@ -8,9 +8,13 @@ defmodule Chesstrainer.Endgames.Endgame do
alias Chesstrainer.Key alias Chesstrainer.Key
@public_code_alphabet ~c"0123456789abcdefghjkmnpqrstvwxyz"
@public_code_length 10
@primary_key {:id, :binary_id, autogenerate: true} @primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id @foreign_key_type :binary_id
schema "endgames" do schema "endgames" do
field :public_code, :string
field :fen, :string field :fen, :string
field :color, Ecto.Enum, values: [:white, :black] field :color, Ecto.Enum, values: [:white, :black]
field :key, :string field :key, :string
@@ -42,7 +46,33 @@ defmodule Chesstrainer.Endgames.Endgame do
end end
end) end)
|> unique_constraint(:fen, message: "FEN already exists") |> unique_constraint(:fen, message: "FEN already exists")
|> unique_constraint(:public_code)
|> put_key() |> put_key()
|> put_public_code()
end
@doc """
Generates a public code: a short, random identifier for player-facing URLs.
10 characters of Crockford base32 (no `i`, `l`, `o` or `u`) give roughly 50
bits, so collisions are vanishingly rare; the unique index is the backstop.
"""
@spec generate_public_code() :: String.t()
def generate_public_code do
@public_code_length
|> :crypto.strong_rand_bytes()
|> :binary.bin_to_list()
|> Enum.map(fn byte ->
Enum.at(@public_code_alphabet, rem(byte, length(@public_code_alphabet)))
end)
|> List.to_string()
end
defp put_public_code(changeset) do
case get_field(changeset, :public_code) do
nil -> put_change(changeset, :public_code, generate_public_code())
_ -> changeset
end
end end
defp put_key(changeset) do defp put_key(changeset) do
@@ -256,5 +256,5 @@ defmodule ChesstrainerWeb.EndgameLive.Form do
defp rating_display(%Phoenix.HTML.FormField{value: value}), do: value defp rating_display(%Phoenix.HTML.FormField{value: value}), do: value
defp return_path("index", _endgame), do: ~p"/admin/endgames" defp return_path("index", _endgame), do: ~p"/admin/endgames"
defp return_path("show", endgame), do: ~p"/admin/endgames/#{endgame}" defp return_path("show", endgame), do: ~p"/admin/endgames/#{endgame.id}"
end end
+112 -9
View File
@@ -18,7 +18,63 @@ defmodule ChesstrainerWeb.EndgameLive.Index do
</:actions> </:actions>
</.header> </.header>
<.form
for={@search_form}
id="endgame-search-form"
phx-submit="endgame_search"
class="mb-6 flex items-end gap-2"
>
<div class="flex-1">
<.input
field={@search_form[:query]}
type="text"
label="Find by ID or FEN"
autocomplete="off"
placeholder="Public code, database id, or FEN…"
class="input input-bordered w-full"
/>
</div>
<.button variant="primary">Find</.button>
</.form>
<div :if={@search_query} id="endgame-search-results" class="space-y-3">
<div class="flex items-center justify-between">
<p class="text-sm text-base-content/70">Results for “{@search_query}”</p>
<button type="button" id="clear-endgame-search" phx-click="clear_search" class="link">
Clear
</button>
</div>
<p :if={@search_results == []} class="text-base-content/70">
No endgame matches “{@search_query}”.
</p>
<table :if={@search_results != []} class="table table-zebra">
<thead>
<tr>
<th>Key</th>
<th>Color</th>
<th>Result</th>
<th>Rating</th>
<th><span class="sr-only">Actions</span></th>
</tr>
</thead>
<tbody>
<tr :for={endgame <- @search_results} id={"search-result-" <> endgame.id}>
<td>{endgame.key}</td>
<td>{endgame.color}</td>
<td>{endgame.result}</td>
<td>{round(endgame.rating)}</td>
<td class="w-0 font-semibold">
<.link navigate={~p"/admin/endgames/#{endgame.id}"}>Show</.link>
</td>
</tr>
</tbody>
</table>
</div>
<.filter_bar <.filter_bar
:if={!@search_query}
selected_themes={@selected_themes} selected_themes={@selected_themes}
theme_query={@theme_query} theme_query={@theme_query}
theme_results={@theme_results} theme_results={@theme_results}
@@ -26,7 +82,7 @@ defmodule ChesstrainerWeb.EndgameLive.Index do
selected_key={@selected_key} selected_key={@selected_key}
/> />
<.grouped_list :if={@groups != []} groups={@groups}> <.grouped_list :if={!@search_query and @groups != []} groups={@groups}>
<:header> <:header>
<tr> <tr>
<th>Key</th> <th>Key</th>
@@ -40,13 +96,22 @@ defmodule ChesstrainerWeb.EndgameLive.Index do
<:row :let={endgame}> <:row :let={endgame}>
<tr id={"endgame-" <> endgame.id}> <tr id={"endgame-" <> endgame.id}>
<td phx-click={JS.navigate(~p"/admin/endgames/#{endgame}")} class="hover:cursor-pointer"> <td
phx-click={JS.navigate(~p"/admin/endgames/#{endgame.id}")}
class="hover:cursor-pointer"
>
{endgame.key} {endgame.key}
</td> </td>
<td phx-click={JS.navigate(~p"/admin/endgames/#{endgame}")} class="hover:cursor-pointer"> <td
phx-click={JS.navigate(~p"/admin/endgames/#{endgame.id}")}
class="hover:cursor-pointer"
>
{endgame.color} {endgame.color}
</td> </td>
<td phx-click={JS.navigate(~p"/admin/endgames/#{endgame}")} class="hover:cursor-pointer"> <td
phx-click={JS.navigate(~p"/admin/endgames/#{endgame.id}")}
class="hover:cursor-pointer"
>
{endgame.result} {endgame.result}
</td> </td>
<td> <td>
@@ -57,14 +122,17 @@ defmodule ChesstrainerWeb.EndgameLive.Index do
</span> </span>
<span :if={endgame.themes == []} class="text-base-content/50">None</span> <span :if={endgame.themes == []} class="text-base-content/50">None</span>
</td> </td>
<td phx-click={JS.navigate(~p"/admin/endgames/#{endgame}")} class="hover:cursor-pointer"> <td
phx-click={JS.navigate(~p"/admin/endgames/#{endgame.id}")}
class="hover:cursor-pointer"
>
{round(endgame.rating)} {round(endgame.rating)}
</td> </td>
<td class="w-0 font-semibold"> <td class="w-0 font-semibold">
<div class="flex gap-4"> <div class="flex gap-4">
<.link navigate={~p"/admin/endgames/#{endgame}"}>Show</.link> <.link navigate={~p"/admin/endgames/#{endgame.id}"}>Show</.link>
<.link navigate={~p"/endgames/#{endgame}/play"}>Play</.link> <.link navigate={~p"/endgames/#{endgame.public_code}/play"}>Play</.link>
<.link navigate={~p"/admin/endgames/#{endgame}/edit"}>Edit</.link> <.link navigate={~p"/admin/endgames/#{endgame.id}/edit"}>Edit</.link>
<.link <.link
phx-click={JS.push("delete", value: %{id: endgame.id})} phx-click={JS.push("delete", value: %{id: endgame.id})}
data-confirm="Are you sure?" data-confirm="Are you sure?"
@@ -77,7 +145,7 @@ defmodule ChesstrainerWeb.EndgameLive.Index do
</:row> </:row>
</.grouped_list> </.grouped_list>
<p :if={@groups == []} class="text-base-content/70"> <p :if={!@search_query and @groups == []} class="text-base-content/70">
No endgames match these filters. No endgames match these filters.
</p> </p>
</Layouts.app> </Layouts.app>
@@ -89,10 +157,37 @@ defmodule ChesstrainerWeb.EndgameLive.Index do
{:ok, {:ok,
socket socket
|> assign(:page_title, "Listing Endgames") |> assign(:page_title, "Listing Endgames")
|> assign(:search_form, to_form(%{"query" => ""}))
|> assign(:search_query, nil)
|> assign(:search_results, [])
|> init_state()} |> init_state()}
end end
@impl true @impl true
def handle_event("endgame_search", %{"query" => query}, socket) do
query = String.trim(query)
case {query, Endgames.search(query)} do
{"", _results} ->
{:noreply, clear_search(socket)}
{_query, [endgame]} ->
{:noreply, push_navigate(socket, to: ~p"/admin/endgames/#{endgame.id}")}
{query, results} ->
{:noreply,
assign(socket,
search_form: to_form(%{"query" => query}),
search_query: query,
search_results: results
)}
end
end
def handle_event("clear_search", _params, socket) do
{:noreply, clear_search(socket)}
end
def handle_event("delete", %{"id" => id}, socket) do def handle_event("delete", %{"id" => id}, socket) do
endgame = Endgames.get_endgame!(id) endgame = Endgames.get_endgame!(id)
{:ok, _} = Endgames.delete_endgame(endgame) {:ok, _} = Endgames.delete_endgame(endgame)
@@ -103,4 +198,12 @@ defmodule ChesstrainerWeb.EndgameLive.Index do
def handle_event(event, params, socket) do def handle_event(event, params, socket) do
handle_filter_event(event, params, socket) handle_filter_event(event, params, socket)
end end
defp clear_search(socket) do
assign(socket,
search_form: to_form(%{"query" => ""}),
search_query: nil,
search_results: []
)
end
end end
@@ -178,8 +178,8 @@ defmodule ChesstrainerWeb.EndgameLive.Play do
end end
@impl true @impl true
def mount(%{"id" => id}, _session, socket) do def mount(%{"public_code" => public_code}, _session, socket) do
endgame = Endgames.get_endgame!(id) endgame = Endgames.get_endgame_by_public_code!(public_code)
socket = socket =
socket socket
@@ -32,14 +32,23 @@ defmodule ChesstrainerWeb.EndgameLive.PlayerIndex do
</:header> </:header>
<:row :let={endgame}> <:row :let={endgame}>
<tr id={"endgame-" <> endgame.id}> <tr id={"endgame-" <> endgame.public_code}>
<td phx-click={JS.navigate(~p"/endgames/#{endgame}/play")} class="hover:cursor-pointer"> <td
phx-click={JS.navigate(~p"/endgames/#{endgame.public_code}/play")}
class="hover:cursor-pointer"
>
{endgame.color} {endgame.color}
</td> </td>
<td phx-click={JS.navigate(~p"/endgames/#{endgame}/play")} class="hover:cursor-pointer"> <td
phx-click={JS.navigate(~p"/endgames/#{endgame.public_code}/play")}
class="hover:cursor-pointer"
>
{endgame.result} {endgame.result}
</td> </td>
<td phx-click={JS.navigate(~p"/endgames/#{endgame}/play")} class="hover:cursor-pointer"> <td
phx-click={JS.navigate(~p"/endgames/#{endgame.public_code}/play")}
class="hover:cursor-pointer"
>
{round(endgame.rating)} {round(endgame.rating)}
</td> </td>
<td> <td>
@@ -51,7 +60,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayerIndex do
<span :if={endgame.themes == []} class="text-base-content/50">None</span> <span :if={endgame.themes == []} class="text-base-content/50">None</span>
</td> </td>
<td class="w-0 font-semibold"> <td class="w-0 font-semibold">
<.link navigate={~p"/endgames/#{endgame}/play"}>Play</.link> <.link navigate={~p"/endgames/#{endgame.public_code}/play"}>Play</.link>
</td> </td>
</tr> </tr>
</:row> </:row>
+25 -2
View File
@@ -33,10 +33,10 @@ defmodule ChesstrainerWeb.EndgameLive.Show do
<.button navigate={~p"/admin/endgames"}> <.button navigate={~p"/admin/endgames"}>
<.icon name="hero-arrow-left" /> <.icon name="hero-arrow-left" />
</.button> </.button>
<.button navigate={~p"/endgames/#{@endgame}/play"}> <.button navigate={~p"/endgames/#{@endgame.public_code}/play"}>
<.icon name="hero-play" /> Play (opens trainer) <.icon name="hero-play" /> Play (opens trainer)
</.button> </.button>
<.button variant="primary" navigate={~p"/admin/endgames/#{@endgame}/edit?return_to=show"}> <.button variant="primary" navigate={~p"/admin/endgames/#{@endgame.id}/edit?return_to=show"}>
<.icon name="hero-pencil-square" /> Edit endgame <.icon name="hero-pencil-square" /> Edit endgame
</.button> </.button>
</:actions> </:actions>
@@ -47,6 +47,19 @@ defmodule ChesstrainerWeb.EndgameLive.Show do
<.list> <.list>
<:item title="Fen">{@endgame.fen}</:item> <:item title="Fen">{@endgame.fen}</:item>
<:item title="Key">{@endgame.key}</:item> <:item title="Key">{@endgame.key}</:item>
<:item title="Public code">
<span id="public-code" class="select-all">{@endgame.public_code}</span>
<button
id="copy-public-code"
type="button"
phx-hook=".CopyPublicCode"
data-code={@endgame.public_code}
class="ml-2 inline-flex cursor-pointer items-center"
title="Copy public code"
>
<.icon name="hero-document-duplicate" class="w-3.5 h-3.5" />
</button>
</:item>
<:item title="Color">{@endgame.color}</:item> <:item title="Color">{@endgame.color}</:item>
<:item title="Rating">{round(@endgame.rating)}</:item> <:item title="Rating">{round(@endgame.rating)}</:item>
<:item title="Message">{@endgame.message}</:item> <:item title="Message">{@endgame.message}</:item>
@@ -56,6 +69,16 @@ defmodule ChesstrainerWeb.EndgameLive.Show do
<:item title="Attempted">{@endgame.times_attempted}</:item> <:item title="Attempted">{@endgame.times_attempted}</:item>
<:item title="Solved">{@endgame.times_solved}</:item> <:item title="Solved">{@endgame.times_solved}</:item>
</.list> </.list>
<script :type={Phoenix.LiveView.ColocatedHook} name=".CopyPublicCode">
export default {
mounted() {
this.el.addEventListener("click", () => {
navigator.clipboard.writeText(this.el.dataset.code)
})
}
}
</script>
</Layouts.app> </Layouts.app>
""" """
end end
+1 -1
View File
@@ -80,7 +80,7 @@ defmodule ChesstrainerWeb.Router do
live_session :current_user, live_session :current_user,
on_mount: [{ChesstrainerWeb.UserAuth, :mount_current_scope}] do on_mount: [{ChesstrainerWeb.UserAuth, :mount_current_scope}] do
live "/endgames/:id/play", EndgameLive.Play, :play live "/endgames/:public_code/play", EndgameLive.Play, :play
live "/users/register", UserLive.Registration, :new live "/users/register", UserLive.Registration, :new
live "/users/log-in", UserLive.Login, :new live "/users/log-in", UserLive.Login, :new
end end
@@ -0,0 +1,47 @@
defmodule Chesstrainer.Repo.Migrations.AddPublicCodeToEndgames do
use Ecto.Migration
@alphabet ~c"0123456789abcdefghjkmnpqrstvwxyz"
@length 10
def up do
alter table(:endgames) do
add :public_code, :string
end
flush()
repo().query!("SELECT id FROM endgames").rows
|> Enum.reduce(MapSet.new(), fn [id], used ->
code = Stream.repeatedly(&generate_code/0) |> Enum.find(&(not MapSet.member?(used, &1)))
repo().query!("UPDATE endgames SET public_code = $1 WHERE id = $2", [code, id])
MapSet.put(used, code)
end)
alter table(:endgames) do
modify :public_code, :string, null: false
end
create unique_index(:endgames, [:public_code])
end
def down do
drop unique_index(:endgames, [:public_code])
alter table(:endgames) do
remove :public_code
end
end
# Inlined rather than calling the schema, so the migration stays frozen even
# if the app's generator changes. `rem(byte, 32)` is unbiased: 256 = 8 * 32.
defp generate_code do
@length
|> :crypto.strong_rand_bytes()
|> :binary.bin_to_list()
|> Enum.map(fn byte -> Enum.at(@alphabet, rem(byte, length(@alphabet))) end)
|> List.to_string()
end
end
+1
View File
@@ -34,6 +34,7 @@ Repo.insert_all(
endgame endgame
|> Map.put(:fen, fen) |> Map.put(:fen, fen)
|> Map.put(:id, Ecto.UUID.generate()) |> Map.put(:id, Ecto.UUID.generate())
|> Map.put(:public_code, Endgame.generate_public_code())
|> Map.put(:key, key) |> Map.put(:key, key)
|> Map.merge(%{inserted_at: now, updated_at: now}) |> Map.merge(%{inserted_at: now, updated_at: now})
end), end),
+76
View File
@@ -174,6 +174,28 @@ defmodule Chesstrainer.EndgamesTest do
end end
end end
describe "public code" do
test "create_endgame/1 assigns a 10-character lowercase Crockford code" do
endgame = create_endgame!(@kr)
assert String.length(endgame.public_code) == 10
assert endgame.public_code =~ ~r/^[0-9abcdefghjkmnpqrstvwxyz]{10}$/
end
test "create_endgame/1 gives each endgame a distinct code" do
one = create_endgame!(@kr)
two = create_endgame!(@kr_other)
refute one.public_code == two.public_code
end
test "get_endgame_by_public_code!/1 finds the endgame" do
endgame = create_endgame!(@kr)
assert Endgames.get_endgame_by_public_code!(endgame.public_code).id == endgame.id
end
end
describe "rating default" do describe "rating default" do
test "a new endgame changeset defaults rating to 1500" do test "a new endgame changeset defaults rating to 1500" do
changeset = Endgames.change_endgame(%Endgame{}) changeset = Endgames.change_endgame(%Endgame{})
@@ -182,6 +204,60 @@ defmodule Chesstrainer.EndgamesTest do
end end
end end
describe "search/1" do
test "finds an endgame by its public code" do
endgame = create_endgame!(@kr)
create_endgame!(@kq)
assert [found] = Endgames.search(endgame.public_code)
assert found.id == endgame.id
end
test "finds an endgame by its database id" do
endgame = create_endgame!(@kr)
assert [found] = Endgames.search(endgame.id)
assert found.id == endgame.id
end
test "finds an endgame by an exactly matching FEN, ignoring surrounding whitespace" do
endgame = create_endgame!(@kr)
assert [found] = Endgames.search(" #{@kr} ")
assert found.id == endgame.id
end
test "normalizes a fullmove 0 FEN before matching" do
endgame = create_endgame!("8/8/3k4/8/8/3K1R2/8/8 w - - 0 0")
assert [found] = Endgames.search("8/8/3k4/8/8/3K1R2/8/8 w - - 0 0")
assert found.id == endgame.id
end
test "finds endgames containing a FEN fragment" do
kr = create_endgame!(@kr)
kq = create_endgame!(@kq)
assert Enum.sort(Enum.map(Endgames.search("3k4"), & &1.id)) == Enum.sort([kr.id, kq.id])
end
test "preloads themes" do
{:ok, lucena} = Themes.create_theme(%{name: "Lucena", category: :endgame})
create_endgame!(@kr, [lucena])
assert [found] = Endgames.search(@kr)
assert Enum.map(found.themes, & &1.name) == ["Lucena"]
end
test "returns no results for an unknown search or a blank query" do
create_endgame!(@kr)
assert Endgames.search("8/8/8/8/8/8/8/8 w - - 0 1") == []
assert Endgames.search("not an identifier") == []
assert Endgames.search(" ") == []
end
end
describe "listing and filtering" do describe "listing and filtering" do
test "list_keys/0 returns the distinct keys sorted" do test "list_keys/0 returns the distinct keys sorted" do
create_endgame!(@kr) create_endgame!(@kr)
@@ -38,7 +38,7 @@ defmodule ChesstrainerWeb.EndgameLive.FormTest do
attrs = %{fen: @fen, color: :white, result: :win, rating: 1625} attrs = %{fen: @fen, color: :white, result: :win, rating: 1625}
{:ok, endgame} = Endgames.create_endgame(attrs) {: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() 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} attrs = %{fen: @fen, color: :white, result: :win, rating: 1500}
{:ok, endgame} = Endgames.create_endgame(attrs, [theme]) {: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" assert render(view) =~ "Lucena"
@@ -43,16 +43,68 @@ defmodule ChesstrainerWeb.EndgameLive.IndexTest do
{:ok, view, html} = live(conn, ~p"/admin/endgames") {:ok, view, html} = live(conn, ~p"/admin/endgames")
# collapsed: the endgame row (and its themes) are not rendered yet # 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" refute html =~ "Lucena"
view |> element("#key-group-K-v-K button") |> render_click() view |> element("#key-group-K-v-K button") |> render_click()
html = render(view) html = render(view)
assert html =~ ~p"/endgames/#{endgame}/play" assert html =~ ~p"/endgames/#{endgame.public_code}/play"
assert html =~ "Lucena" assert html =~ "Lucena"
end 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 test "filters by theme", %{conn: conn} do
lucena = theme_fixture(%{name: "Lucena"}) lucena = theme_fixture(%{name: "Lucena"})
philidor = theme_fixture(%{name: "Philidor"}) philidor = theme_fixture(%{name: "Philidor"})
@@ -69,6 +69,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
{:ok, key} = Chesstrainer.Key.from_fen(fen) {:ok, key} = Chesstrainer.Key.from_fen(fen)
Chesstrainer.Repo.insert!(%Chesstrainer.Endgames.Endgame{ Chesstrainer.Repo.insert!(%Chesstrainer.Endgames.Endgame{
public_code: Chesstrainer.Endgames.Endgame.generate_public_code(),
fen: fen, fen: fen,
color: :black, color: :black,
key: key, key: key,
@@ -95,7 +96,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
seed_tablebase(after_reply, category: :win, moves: grading_moves()) seed_tablebase(after_reply, category: :win, moves: grading_moves())
seed_tablebase(after_losing, category: :draw, moves: [losing_reply]) 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 %{proxy: {ref, _topic, _}} = view
assert_receive {^ref, {:push_event, "init_board", _payload}}, 100 assert_receive {^ref, {:push_event, "init_board", _payload}}, 100
@@ -137,7 +138,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
endgame = endgame_fixture(color: :white) endgame = endgame_fixture(color: :white)
seed_tablebase(endgame.fen) 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 has_element?(view, "#endgame-board")
assert_init_board(view, endgame.fen, "white", "white") assert_init_board(view, endgame.fen, "white", "white")
@@ -147,7 +148,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
endgame = endgame_fixture(color: :black) endgame = endgame_fixture(color: :black)
seed_tablebase(endgame.fen) 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") assert_init_board(view, endgame.fen, "black", "black")
end end
@@ -163,7 +164,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
seed_tablebase(endgame.fen) 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 =~ "KQ v K"
assert html =~ "White to move" assert html =~ "White to move"
@@ -176,6 +177,14 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
live(conn, ~p"/endgames/00000000-0000-0000-0000-000000000000/play") live(conn, ~p"/endgames/00000000-0000-0000-0000-000000000000/play")
end end
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 end
describe "current scope" do describe "current scope" do
@@ -187,7 +196,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
{:ok, view, _html} = {:ok, view, _html} =
conn conn
|> log_in_user(user) |> 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 assert :sys.get_state(view.pid).socket.assigns.current_scope.user.id == user.id
end end
@@ -196,7 +205,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
endgame = endgame_fixture() endgame = endgame_fixture()
seed_tablebase(endgame.fen) 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 assert :sys.get_state(view.pid).socket.assigns.current_scope == nil
end end
@@ -211,7 +220,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
Ratelimiters.lichess_add_cooldown() Ratelimiters.lichess_add_cooldown()
on_exit(fn -> Ratelimiters.lichess_reset_cooldown() end) 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" 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 test "blocks an out-of-range endgame as not covered", %{conn: conn} do
endgame = insert_out_of_range_endgame() 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) html = render(view)
assert html =~ "Tablebase unavailable" assert html =~ "Tablebase unavailable"
@@ -235,7 +244,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
endgame = endgame_fixture() endgame = endgame_fixture()
seed_tablebase(endgame.fen) 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 view
|> render_hook("chess_fen_invalid", %{ |> render_hook("chess_fen_invalid", %{
@@ -254,7 +263,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
endgame = endgame_fixture() endgame = endgame_fixture()
seed_tablebase(endgame.fen, moves: grading_moves()) 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+") play_move(view, endgame, "d1", "d7", "Qd7+")
html = render(view) html = render(view)
@@ -267,7 +276,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
endgame = endgame_fixture() endgame = endgame_fixture()
seed_tablebase(endgame.fen, moves: grading_moves()) 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") play_move(view, endgame, "d1", "d5", "Qd5")
@@ -278,7 +287,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
endgame = endgame_fixture() endgame = endgame_fixture()
seed_tablebase(endgame.fen, moves: grading_moves()) 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") play_move(view, endgame, "d1", "d4", "Qd4")
@@ -289,7 +298,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
endgame = endgame_fixture() endgame = endgame_fixture()
seed_tablebase(endgame.fen, moves: grading_moves()) 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", %{ render_hook(view, "endgame_move_played", %{
"from" => "d1", "from" => "d1",
@@ -315,7 +324,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
seed_tablebase(fen_after, category: :loss, moves: [reply]) seed_tablebase(fen_after, category: :loss, moves: [reply])
seed_tablebase(fen_after_reply, category: :win, moves: grading_moves()) 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", %{ render_hook(view, "endgame_move_played", %{
"from" => "d1", "from" => "d1",
@@ -341,7 +350,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
endgame = endgame_fixture() endgame = endgame_fixture()
seed_tablebase(endgame.fen, moves: grading_moves()) 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") play_move(view, endgame, "d1", "d4", "Qd4")
@@ -357,7 +366,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
endgame = endgame_fixture() endgame = endgame_fixture()
seed_tablebase(endgame.fen) 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 assert Endgames.get_endgame!(endgame.id).times_attempted == 1
end end
@@ -369,7 +378,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
seed_tablebase(endgame.fen, moves: grading_moves()) seed_tablebase(endgame.fen, moves: grading_moves())
seed_tablebase(fen_after, checkmate: true, 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", %{ render_hook(view, "endgame_move_played", %{
"from" => "d1", "from" => "d1",
@@ -389,7 +398,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
moves = [%Tablebase.Move{uci: "a7a8q", san: "a8=Q", category: :loss, dtz: -1}] moves = [%Tablebase.Move{uci: "a7a8q", san: "a8=Q", category: :loss, dtz: -1}]
seed_tablebase(endgame.fen, moves: moves) 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", %{ render_hook(view, "endgame_move_played", %{
"from" => "a7", "from" => "a7",
@@ -409,7 +418,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
endgame = endgame_fixture() endgame = endgame_fixture()
seed_tablebase(endgame.fen, moves: grading_moves()) 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") 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(endgame.fen, moves: grading_moves())
seed_tablebase(fen_after, checkmate: true, 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", %{ render_hook(view, "endgame_move_played", %{
"from" => "d1", "from" => "d1",
@@ -452,7 +461,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
endgame = endgame_fixture() endgame = endgame_fixture()
seed_tablebase(endgame.fen, moves: grading_moves()) 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") 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(endgame.fen, moves: grading_moves())
seed_tablebase(fen_after, checkmate: true, 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", %{ render_hook(view, "endgame_move_played", %{
"from" => "d1", "from" => "d1",
@@ -494,7 +503,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
seed_tablebase(endgame.fen, moves: grading_moves()) seed_tablebase(endgame.fen, moves: grading_moves())
seed_tablebase(fen_after, checkmate: true, 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 -> play = fn ->
render_hook(view, "endgame_move_played", %{ render_hook(view, "endgame_move_played", %{
@@ -527,7 +536,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
endgame = endgame_fixture(rating: 1800) endgame = endgame_fixture(rating: 1800)
seed_tablebase(endgame.fen, moves: grading_moves()) 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") play_move(view, endgame, "d1", "d4", "Qd4")
@@ -545,7 +554,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
endgame = endgame_fixture(result: :draw) endgame = endgame_fixture(result: :draw)
seed_tablebase(endgame.fen, category: :draw, moves: []) 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 has_element?(view, "#endgame-board")
assert render(view) =~ "Make a move" 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(endgame.fen, category: :draw, moves: draw_moves())
seed_tablebase(fen_after, category: :draw, moves: [reply]) 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", %{ render_hook(view, "endgame_move_played", %{
"from" => "d1", "from" => "d1",
@@ -581,7 +590,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
endgame = endgame_fixture(result: :draw) endgame = endgame_fixture(result: :draw)
seed_tablebase(endgame.fen, category: :draw, moves: draw_moves()) 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", %{ render_hook(view, "endgame_move_played", %{
"from" => "d1", "from" => "d1",
@@ -603,7 +612,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
endgame = endgame_fixture(result: :draw) endgame = endgame_fixture(result: :draw)
seed_tablebase(endgame.fen, category: :draw, moves: draw_moves()) 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", %{ render_hook(view, "endgame_move_played", %{
"from" => "d1", "from" => "d1",
@@ -627,7 +636,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
seed_tablebase(endgame.fen, category: :draw, moves: draw_moves()) seed_tablebase(endgame.fen, category: :draw, moves: draw_moves())
seed_tablebase(fen_after, category: :draw, moves: [reply]) 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", %{ render_hook(view, "endgame_move_played", %{
"from" => "d1", "from" => "d1",
@@ -651,7 +660,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
endgame = endgame_fixture() endgame = endgame_fixture()
seed_tablebase(endgame.fen, moves: grading_moves()) 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+") play_move(view, endgame, "d1", "d7", "Qd7+")
assert render(view) =~ "Qd7+" assert render(view) =~ "Qd7+"
@@ -694,7 +703,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
seed_tablebase(endgame.fen, category: :draw, moves: moves) 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", %{ render_hook(view, "endgame_move_played", %{
"from" => "d1", "from" => "d1",
@@ -730,7 +739,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
moves = [%Tablebase.Move{uci: "a7a8q", san: "a8=Q", category: :loss, dtz: -1}] moves = [%Tablebase.Move{uci: "a7a8q", san: "a8=Q", category: :loss, dtz: -1}]
seed_tablebase(endgame.fen, moves: moves) 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", %{ render_hook(view, "endgame_move_played", %{
"from" => "a7", "from" => "a7",
@@ -23,11 +23,22 @@ defmodule ChesstrainerWeb.EndgameLive.PlayerIndexTest do
view |> element("#key-group-K-v-K button") |> render_click() view |> element("#key-group-K-v-K button") |> render_click()
html = render(view) html = render(view)
assert html =~ ~p"/endgames/#{endgame}/play" assert html =~ ~p"/endgames/#{endgame.public_code}/play"
assert html =~ ~p"/endgames/#{other}/play" assert html =~ ~p"/endgames/#{other.public_code}/play"
assert html =~ "Lucena" assert html =~ "Lucena"
end 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 test "filters by theme", %{conn: conn} do
lucena = theme_fixture(%{name: "Lucena"}) lucena = theme_fixture(%{name: "Lucena"})
philidor = theme_fixture(%{name: "Philidor"}) 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 test "navigating to a play page renders the board", %{conn: conn} do
endgame = endgame_fixture() 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, "#endgame-board")
assert has_element?(view, ~s{a[href="/endgames"]}) 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