Implement tablebase move grading in the play view

#7: prefetch the tablebase at mount and grade a played move locally
against its moves list (optimal / winning / losing), blocking the
attempt when the tablebase is rate-limited or unavailable. The client
now reports prev_fen and promotion, and the trainer board locks after a
graded move until reset.

Fix the 429 clause ordering in the tablebase parser so the cooldown
actually fires, and use the HTTPS endpoint.

Add dialyxir to the dev/test deps.
This commit is contained in:
2026-11-01 21:37:27 +01:00
parent e5f9365ed7
commit 758629565b
5 changed files with 299 additions and 65 deletions
@@ -4,6 +4,7 @@ defmodule Chesstrainer.Endgames.TablebaseTest do
alias Chesstrainer.Endgames.Tablebase
alias Chesstrainer.Endgames.Tablebase.Cache, as: TablebaseCache
alias Chesstrainer.Endgames.Tablebase.Parser
alias Chesstrainer.Ratelimiters
describe "normalize_category/1" do
test "maps winning categories to :win" do
@@ -68,6 +69,19 @@ defmodule Chesstrainer.Endgames.TablebaseTest do
end
end
describe "Parser.parse_response/1" do
test "a 429 sets the cooldown and returns a rate-limit error" do
Ratelimiters.lichess_reset_cooldown()
on_exit(fn -> Ratelimiters.lichess_reset_cooldown() end)
assert {:error, {:too_many_requests, 429, "rate limited"}} =
Parser.parse_response({:ok, %Req.Response{status: 429, body: "rate limited"}})
assert {:cooldown, remaining} = Ratelimiters.lichess_check_cooldown()
assert remaining > 0
end
end
describe "new/1" do
test "returns {:ok, tablebase} on a cache hit" do
fen = "8/8/3k4/8/8/3K1R2/8/8 w - - 0 1"
@@ -4,6 +4,10 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
import Phoenix.LiveViewTest
import Chesstrainer.EndgamesFixtures
alias Chesstrainer.Endgames.Tablebase
alias Chesstrainer.Endgames.Tablebase.Cache, as: TablebaseCache
alias Chesstrainer.Ratelimiters
defp assert_init_board(view, expected_fen, expected_orientation, expected_player_color) do
%{proxy: {ref, _topic, _}} = view
@@ -14,9 +18,40 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
assert payload.player_color == expected_player_color
end
# Seed the tablebase cache so mount never touches the network in tests.
defp seed_tablebase(fen, opts \\ []) do
tablebase = %Tablebase{
category: Keyword.get(opts, :category, :win),
moves: Keyword.get(opts, :moves, [])
}
TablebaseCache.put(fen, tablebase)
on_exit(fn -> TablebaseCache.delete(fen) end)
end
defp grading_moves do
[
%Tablebase.Move{uci: "d1d7", san: "Qd7+", category: :loss, dtz: -1},
%Tablebase.Move{uci: "d1d5", san: "Qd5", category: :loss, dtz: -9},
%Tablebase.Move{uci: "d1d4", san: "Qd4", category: :draw, dtz: 0}
]
end
defp play_move(view, endgame, from, to, san) do
render_hook(view, "endgame_move_played", %{
"from" => from,
"to" => to,
"san" => san,
"prev_fen" => endgame.fen,
"fen" => endgame.fen,
"endgame_id" => endgame.id
})
end
describe "mount" do
test "renders the chessboard hook div and pushes init_board", %{conn: conn} do
endgame = endgame_fixture(color: :white)
seed_tablebase(endgame.fen)
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame}/play")
@@ -26,6 +61,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
test "pushes black orientation when the endgame color is black", %{conn: conn} do
endgame = endgame_fixture(color: :black)
seed_tablebase(endgame.fen)
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame}/play")
@@ -34,6 +70,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
test "shows endgame metadata in the header", %{conn: conn} do
endgame = endgame_fixture(key: "KQ v K", color: :white, result: :win, rating: 1800)
seed_tablebase(endgame.fen)
{:ok, _view, html} = live(conn, ~p"/endgames/#{endgame}/play")
@@ -49,9 +86,26 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
end
end
describe "tablebase availability" do
test "blocks the attempt when the tablebase is cooling down", %{conn: conn} do
endgame = endgame_fixture()
TablebaseCache.delete(endgame.fen)
Ratelimiters.lichess_add_cooldown()
on_exit(fn -> Ratelimiters.lichess_reset_cooldown() end)
{:ok, view, html} = live(conn, ~p"/endgames/#{endgame}/play")
assert html =~ "Tablebase unavailable"
play_move(view, endgame, "d1", "d7", "Qd7+")
assert render(view) =~ "cannot be graded"
end
end
describe "chess_fen_invalid" do
test "shows a flash when the chessground hook reports an unparseable FEN", %{conn: conn} do
endgame = endgame_fixture()
seed_tablebase(endgame.fen)
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame}/play")
@@ -68,45 +122,75 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
end
describe "endgame_move_played" do
test "appends the SAN move and updates last_san / last_from_to", %{conn: conn} do
test "grades the optimal move", %{conn: conn} do
endgame = endgame_fixture()
seed_tablebase(endgame.fen, moves: grading_moves())
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame}/play")
view
|> render_hook("endgame_move_played", %{
play_move(view, endgame, "d1", "d7", "Qd7+")
html = render(view)
assert html =~ "Optimal move"
assert html =~ "Qd7+"
end
test "grades a winning but slower move as winning", %{conn: conn} do
endgame = endgame_fixture()
seed_tablebase(endgame.fen, moves: grading_moves())
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame}/play")
play_move(view, endgame, "d1", "d5", "Qd5")
assert render(view) =~ "Winning — but not the sharpest"
end
test "grades a move that throws the win away as losing", %{conn: conn} do
endgame = endgame_fixture()
seed_tablebase(endgame.fen, moves: grading_moves())
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame}/play")
play_move(view, endgame, "d1", "d4", "Qd4")
assert render(view) =~ "Losing move"
end
test "rejects a move that is out of sequence", %{conn: conn} do
endgame = endgame_fixture()
seed_tablebase(endgame.fen, moves: grading_moves())
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame}/play")
render_hook(view, "endgame_move_played", %{
"from" => "d1",
"to" => "d7",
"san" => "Qd7+",
"prev_fen" => "8/8/8/8/8/8/8/8 w - - 0 1",
"fen" => endgame.fen,
"endgame_id" => endgame.id
})
html = render(view)
assert html =~ "Qd7+"
assert render(view) =~ "out of sequence"
end
end
describe "reset" do
test "clears move list and re-pushes init_board", %{conn: conn} do
test "clears the move list and grade, and re-pushes init_board", %{conn: conn} do
endgame = endgame_fixture()
seed_tablebase(endgame.fen, moves: grading_moves())
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame}/play")
view
|> render_hook("endgame_move_played", %{
"from" => "d1",
"to" => "d7",
"san" => "Qd7+",
"fen" => endgame.fen,
"endgame_id" => endgame.id
})
play_move(view, endgame, "d1", "d7", "Qd7+")
assert render(view) =~ "Qd7+"
view |> element("button", "Reset") |> render_click()
assert render(view) =~ "No Moves"
html = render(view)
assert html =~ "No Moves"
refute html =~ "Optimal move"
assert_init_board(view, endgame.fen, "white", "white")
end
end