Make tablebase and empty-pool failures recoverable (#163) (#170)

Tablebase failures now render one message per class, with a Retry action
when fetching again can help. The generic class no longer interpolates
inspect(reason) into the page; the reason goes to the log instead.
Positions outside the tablebase offer the next puzzle (in a session) or the
existing back link, rather than a futile retry.

Train's no-puzzles and exhausted-difficulty flashes become in-page states
with Retry and Change difficulty. The guest pool gets the same treatment,
and GuestLimits.remaining/1 lets a guest see the wall, or the empty pool,
without spending a puzzle.
This commit is contained in:
2026-11-13 15:36:59 +01:00
committed by GitHub
parent 0563b5aca2
commit 021e412167
11 changed files with 476 additions and 133 deletions
+12
View File
@@ -37,9 +37,21 @@ defmodule Chesstrainer.GuestLimitsTest do
assert {:ok, 4} = GuestLimits.take(@other, @today)
end
test "remaining reports the budget without consuming it" do
assert {:ok, 5} = GuestLimits.remaining(@address, @today)
assert {:ok, 5} = GuestLimits.remaining(@address, @today)
assert {:ok, 4} = GuestLimits.take(@address, @today)
assert {:ok, 4} = GuestLimits.remaining(@address, @today)
for _ <- 1..4, do: assert({:ok, _} = GuestLimits.take(@address, @today))
assert {:limit_reached, _} = GuestLimits.remaining(@address, @today)
end
test "does not limit a missing address" do
assert {:ok, nil} = GuestLimits.take(nil, @today)
assert {:ok, nil} = GuestLimits.take(nil, @today)
assert {:ok, nil} = GuestLimits.remaining(nil, @today)
end
test "reset clears spent budgets" do
@@ -80,11 +80,21 @@ defmodule ChesstrainerWeb.EndgameLive.GuestTrainTest do
end
describe "without puzzles" do
test "shows an empty state", %{conn: conn} do
test "shows an empty state with a retry that does not spend a puzzle", %{conn: conn} do
{:ok, view, _html} = live(conn, ~p"/guest-train")
assert render(view) =~ "No endgames are available yet"
assert has_element?(view, "#guest-pool-empty")
assert has_element?(view, "#retry-pool")
refute has_element?(view, "#endgame-board")
assert {:ok, 5} = GuestLimits.remaining(@guest_address)
endgame = endgame_fixture()
seed_tablebase(endgame.fen, moves: grading_moves())
view |> element("#retry-pool") |> render_click()
assert has_element?(view, "#endgame-board")
assert :sys.get_state(view.pid).socket.assigns.guest_remaining == 4
end
end
@@ -133,6 +143,31 @@ defmodule ChesstrainerWeb.EndgameLive.GuestTrainTest do
assert :sys.get_state(view.pid).socket.assigns.guest_remaining == 4
end
test "shows an in-page state when no puzzles are left, and retry loads one", %{conn: conn} do
endgame = endgame_fixture()
seed_tablebase(endgame.fen, moves: grading_moves())
{:ok, view, _html} = live(conn, ~p"/guest-train")
solve_current(view)
Repo.delete!(endgame)
view |> element("#next-puzzle-button") |> render_click()
assert has_element?(view, "#guest-pool-exhausted")
assert has_element?(view, "#retry-pool")
refute has_element?(view, "#endgame-board")
assert :sys.get_state(view.pid).socket.assigns.guest_remaining == 4
new_endgame = endgame_fixture()
seed_tablebase(new_endgame.fen, moves: grading_moves())
view |> element("#retry-pool") |> render_click()
assert :sys.get_state(view.pid).socket.assigns.endgame.id == new_endgame.id
assert :sys.get_state(view.pid).socket.assigns.guest_remaining == 3
end
end
describe "daily budget" do
@@ -12,6 +12,7 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
alias Chesstrainer.FEN
alias Chesstrainer.Ratelimiters
alias Chesstrainer.Repo
alias ChesstrainerWeb.EndgameLive.PlayCore
defp assert_init_board(view, expected_fen, expected_orientation, expected_player_color) do
%{proxy: {ref, _topic, _}} = view
@@ -266,10 +267,22 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
{:ok, view, html} = live(conn, ~p"/endgames/#{endgame.public_code}/play")
assert html =~ "Tablebase unavailable"
assert html =~ "Tablebase cooling down"
refute html =~ "inspect("
refute html =~ "too_many_requests"
play_move(view, endgame, "d1", "d7", "Qd7+")
assert render(view) =~ "cannot be graded"
# Retry re-fetches: with the cache seeded and the cooldown cleared, the
# same puzzle becomes playable in one click.
seed_tablebase(endgame.fen)
Ratelimiters.lichess_reset_cooldown()
view |> element("#retry-blocked") |> render_click()
assert :sys.get_state(view.pid).socket.assigns.status == :playing
refute render(view) =~ "Tablebase cooling down"
end
test "blocks an out-of-range endgame as not covered", %{conn: conn} do
@@ -278,8 +291,37 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame.public_code}/play")
html = render(view)
assert html =~ "Tablebase unavailable"
assert html =~ "not covered by the tablebase"
assert html =~ "Position not covered"
assert html =~ "can&#39;t be graded"
refute has_element?(view, "#retry-blocked")
refute html =~ "not_covered"
refute html =~ "inspect("
end
@tag capture_log: true
test "classifies every failure class without exposing the reason" do
cases = [
{{:cooldown, 30_000}, :cooldown, true},
{{:error, {:too_many_requests, 429, "upstream body"}}, :rate_limited, true},
{{:error, {:client_error, 404, "upstream body"}}, :not_covered, false},
{{:error, :not_covered}, :not_covered, false},
{{:error, :invalid_endgame}, :not_covered, false},
{{:error, {:server_error, 500, "upstream body"}}, :unavailable, true},
{{:error, :econnrefused}, :unavailable, true}
]
for {input, kind, retry?} <- cases do
blocked = PlayCore.blocked(input)
assert blocked.kind == kind
assert blocked.retry == retry?
refute blocked.message =~ "inspect"
refute blocked.message =~ "server_error"
refute blocked.message =~ "econnrefused"
refute blocked.message =~ "upstream"
refute blocked.message =~ "500"
refute blocked.message =~ "429"
end
end
end
@@ -225,6 +225,63 @@ defmodule ChesstrainerWeb.EndgameLive.TrainTest do
end
end
describe "empty pools" do
setup :register_and_log_in_user
test "shows an in-page state when a difficulty has no puzzles", %{conn: conn} do
{:ok, view, _html} = live(conn, ~p"/train")
view |> element("#difficulty-medium") |> render_click()
assert has_element?(view, "#pool-empty")
assert has_element?(view, "#retry-pool")
assert has_element?(view, "#difficulty-picker")
refute has_element?(view, "#session-bar")
endgame = endgame_fixture(rating: 1500)
seed_tablebase(endgame.fen)
view |> element("#retry-pool") |> render_click()
assert has_element?(view, "#session-bar")
refute has_element?(view, "#pool-empty")
end
test "shows an in-page end-of-run state when the difficulty is exhausted", %{conn: conn} do
{view, [_endgame]} = start_medium_session(conn, 1)
solve_current(view)
view |> element("#next-puzzle-button") |> render_click()
assert has_element?(view, "#pool-exhausted")
assert has_element?(view, "#retry-pool")
assert has_element?(view, "#change-difficulty")
refute has_element?(view, "#endgame-board")
view |> element("#change-difficulty") |> render_click()
assert has_element?(view, "#difficulty-picker")
assert :sys.get_state(view.pid).socket.assigns.session == nil
end
test "retry loads a puzzle once the pool grows", %{conn: conn} do
{view, [_endgame]} = start_medium_session(conn, 1)
solve_current(view)
view |> element("#next-puzzle-button") |> render_click()
assert has_element?(view, "#pool-exhausted")
rating = :sys.get_state(view.pid).socket.assigns.user_rating
new_endgame = endgame_fixture(fen: "4k3/8/8/8/8/8/8/4K3 w - - 0 1", rating: round(rating))
seed_tablebase(new_endgame.fen)
view |> element("#retry-pool") |> render_click()
assert :sys.get_state(view.pid).socket.assigns.endgame.id == new_endgame.id
refute has_element?(view, "#pool-exhausted")
end
end
defp elapsed_id(view) do
:sys.get_state(view.pid).socket.assigns.session.started_at_ms
end