@@ -155,6 +176,19 @@ defmodule ChesstrainerWeb.EndgameLive.Train do
<.rating_guide rating={@user_rating} deviation={@user_deviation} />
+
+
No puzzles available right now
+
+ There are no {difficulty_label(@pool.difficulty)} puzzles yet. Try again, or choose another difficulty.
+
+ <.button id="retry-pool" phx-click="retry_pool" class="mt-3">
+ <.icon name="hero-arrow-path" /> Retry
+
+
<%= for {difficulty, label, description} <- difficulty_options(@user_rating) do %>
<.button
@@ -188,62 +222,32 @@ defmodule ChesstrainerWeb.EndgameLive.Train do
|> assign(:last_session, nil)
|> assign(:puzzle_started_mono, nil)
|> assign(:puzzle_counted, false)
+ |> assign(:pool, nil)
|> assign(:user_rating, socket.assigns.current_scope.user.rating)
|> assign(:user_deviation, socket.assigns.current_scope.user.rating_deviation)}
end
@impl true
def handle_event("start_session", %{"difficulty" => difficulty}, socket) do
- difficulty = parse_difficulty(difficulty)
-
- case Puzzles.next_endgame(selection_user(socket), difficulty) do
- nil ->
- {:noreply, put_flash(socket, :error, "No puzzles are available right now.")}
-
- endgame ->
- session = %{
- difficulty: difficulty,
- started_at_ms: System.system_time(:millisecond),
- baseline: socket.assigns.user_rating,
- count: 0,
- sum_ms: 0
- }
-
- socket =
- socket
- |> assign(:session, session)
- |> assign(:puzzle_started_mono, System.monotonic_time(:millisecond))
- |> assign(:puzzle_counted, false)
- |> assign(:last_rating_delta, nil)
- |> PlayCore.init(endgame, page_title: "Train")
-
- {:noreply, socket}
- end
+ {:noreply, start_session(socket, parse_difficulty(difficulty))}
end
- def handle_event("next_puzzle", _params, socket) do
- session = socket.assigns.session
+ def handle_event("next_puzzle", _params, socket), do: {:noreply, next_puzzle(socket)}
- if session do
- case Puzzles.next_endgame(selection_user(socket), session.difficulty,
- exclude_ids: [socket.assigns.endgame.id]
- ) do
+ def handle_event("retry_pool", _params, socket) do
+ socket =
+ case socket.assigns.session do
nil ->
- {:noreply, put_flash(socket, :info, "No more puzzles at this difficulty.")}
+ case socket.assigns.pool do
+ %{difficulty: difficulty} -> start_session(socket, difficulty)
+ _ -> socket
+ end
- endgame ->
- socket =
- socket
- |> assign(:puzzle_started_mono, System.monotonic_time(:millisecond))
- |> assign(:puzzle_counted, false)
- |> assign(:last_rating_delta, nil)
- |> PlayCore.init(endgame, page_title: "Train")
-
- {:noreply, socket}
+ _session ->
+ next_puzzle(socket)
end
- else
- {:noreply, socket}
- end
+
+ {:noreply, socket}
end
def handle_event("stop_session", _params, socket) do
@@ -253,7 +257,7 @@ defmodule ChesstrainerWeb.EndgameLive.Train do
session -> assign(socket, :last_session, summarise(session, socket.assigns.user_rating))
end
- {:noreply, assign(socket, :session, nil)}
+ {:noreply, socket |> assign(:session, nil) |> assign(:pool, nil)}
end
def handle_event("endgame_move_played", params, socket) do
@@ -300,6 +304,53 @@ defmodule ChesstrainerWeb.EndgameLive.Train do
def handle_event("back_to_game", _params, socket), do: PlayCore.back_to_game(socket)
+ defp start_session(socket, difficulty) do
+ case Puzzles.next_endgame(selection_user(socket), difficulty) do
+ nil ->
+ assign(socket, :pool, %{difficulty: difficulty})
+
+ endgame ->
+ session = %{
+ difficulty: difficulty,
+ started_at_ms: System.system_time(:millisecond),
+ baseline: socket.assigns.user_rating,
+ count: 0,
+ sum_ms: 0
+ }
+
+ socket
+ |> assign(:session, session)
+ |> assign(:pool, nil)
+ |> assign(:puzzle_started_mono, System.monotonic_time(:millisecond))
+ |> assign(:puzzle_counted, false)
+ |> assign(:last_rating_delta, nil)
+ |> PlayCore.init(endgame, page_title: "Train")
+ end
+ end
+
+ defp next_puzzle(socket) do
+ session = socket.assigns.session
+
+ if session do
+ case Puzzles.next_endgame(selection_user(socket), session.difficulty,
+ exclude_ids: [socket.assigns.endgame.id]
+ ) do
+ nil ->
+ assign(socket, :pool, %{difficulty: session.difficulty})
+
+ endgame ->
+ socket
+ |> assign(:pool, nil)
+ |> assign(:puzzle_started_mono, System.monotonic_time(:millisecond))
+ |> assign(:puzzle_counted, false)
+ |> assign(:last_rating_delta, nil)
+ |> PlayCore.init(endgame, page_title: "Train")
+ end
+ else
+ socket
+ end
+ end
+
# A completed puzzle contributes one first-attempt time to the average. A
# retry happens after this point and is deliberately ignored.
defp account_completion(socket) do
diff --git a/test/chesstrainer/guest_limits_test.exs b/test/chesstrainer/guest_limits_test.exs
index 762c713..6afc689 100644
--- a/test/chesstrainer/guest_limits_test.exs
+++ b/test/chesstrainer/guest_limits_test.exs
@@ -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
diff --git a/test/chesstrainer_web/live/endgame_live/guest_train_test.exs b/test/chesstrainer_web/live/endgame_live/guest_train_test.exs
index acc4f41..8946f3d 100644
--- a/test/chesstrainer_web/live/endgame_live/guest_train_test.exs
+++ b/test/chesstrainer_web/live/endgame_live/guest_train_test.exs
@@ -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
diff --git a/test/chesstrainer_web/live/endgame_live/play_test.exs b/test/chesstrainer_web/live/endgame_live/play_test.exs
index 0c21ade..a3de5b6 100644
--- a/test/chesstrainer_web/live/endgame_live/play_test.exs
+++ b/test/chesstrainer_web/live/endgame_live/play_test.exs
@@ -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'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
diff --git a/test/chesstrainer_web/live/endgame_live/train_test.exs b/test/chesstrainer_web/live/endgame_live/train_test.exs
index e2342fe..742a4fe 100644
--- a/test/chesstrainer_web/live/endgame_live/train_test.exs
+++ b/test/chesstrainer_web/live/endgame_live/train_test.exs
@@ -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