mirror of
https://github.com/TheRealOwenRees/chess-endgame-trainer.git
synced 2026-11-18 11:42:07 +00:00
The Endgame key is now a canonical, colour-agnostic material signature derived from the FEN instead of a hand-typed field: each side's pieces, strongest side first by material value (e.g. "KQ v KR"). This fixes the seed ordering inconsistency and guarantees the key always matches the position. - Add Chesstrainer.Key.from_fen/1. - Compute the key in the Endgame changeset; drop the manual input and its regex validation. - Index endgames.key and derive keys in the seeds. - Document the Key term and the decision (ADR 0003). Closes #13
84 lines
2.5 KiB
Elixir
84 lines
2.5 KiB
Elixir
defmodule ChesstrainerWeb.EndgameLive.FormTest do
|
|
use ChesstrainerWeb.ConnCase
|
|
|
|
import Phoenix.LiveViewTest
|
|
import Chesstrainer.ThemesFixtures
|
|
|
|
alias Chesstrainer.Endgames
|
|
alias Chesstrainer.Repo
|
|
alias Chesstrainer.Themes
|
|
|
|
@fen "8/8/3k4/8/8/3K1R2/8/8 w - - 0 1"
|
|
|
|
test "creates an endgame with staged themes", %{conn: conn} do
|
|
theme = theme_fixture(%{name: "Lucena"})
|
|
|
|
{:ok, view, _html} = live(conn, ~p"/admin/endgames/new")
|
|
|
|
view |> form("#theme-form", %{theme_search: "Luc"}) |> render_change()
|
|
view |> element("button", "Lucena") |> render_click()
|
|
|
|
view
|
|
|> form("#endgame-form",
|
|
endgame: %{fen: @fen, rating: 1500, result: "win"}
|
|
)
|
|
|> render_submit()
|
|
|
|
assert_redirect(view, ~p"/admin/endgames")
|
|
|
|
endgame =
|
|
Endgames.list_endgames()
|
|
|> Enum.find(&(&1.fen == @fen))
|
|
|> Repo.preload(:themes)
|
|
|
|
assert Enum.map(endgame.themes, & &1.id) == [theme.id]
|
|
end
|
|
|
|
test "attaches multiple themes", %{conn: conn} do
|
|
theme_fixture(%{name: "Lucena"})
|
|
theme_fixture(%{name: "Philidor"})
|
|
|
|
{:ok, view, _html} = live(conn, ~p"/admin/endgames/new")
|
|
|
|
view |> form("#theme-form", %{theme_search: "Luc"}) |> render_change()
|
|
view |> element("button", "Lucena") |> render_click()
|
|
|
|
view |> form("#theme-form", %{theme_search: "Phil"}) |> render_change()
|
|
view |> element("button", "Philidor") |> render_click()
|
|
|
|
html = render(view)
|
|
assert html =~ "Lucena"
|
|
assert html =~ "Philidor"
|
|
end
|
|
|
|
test "creates and attaches a new theme inline", %{conn: conn} do
|
|
{:ok, view, _html} = live(conn, ~p"/admin/endgames/new")
|
|
|
|
view |> form("#theme-form", %{theme_search: "Lucerne"}) |> render_submit()
|
|
|
|
assert render(view) =~ "Lucerne"
|
|
assert [%{name: "Lucerne"}] = Themes.search("Lucerne", "endgame")
|
|
end
|
|
|
|
test "removes a theme from an existing endgame", %{conn: conn} do
|
|
theme = theme_fixture(%{name: "Lucena"})
|
|
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")
|
|
|
|
assert render(view) =~ "Lucena"
|
|
|
|
view |> element("button[aria-label='Remove Lucena']") |> render_click()
|
|
|
|
view
|
|
|> form("#endgame-form", endgame: %{rating: 1500, result: "win"})
|
|
|> render_submit()
|
|
|
|
assert_redirect(view, ~p"/admin/endgames")
|
|
|
|
updated = Endgames.get_endgame!(endgame.id) |> Repo.preload(:themes)
|
|
assert updated.themes == []
|
|
end
|
|
end
|