From ff7adcf30806aac4ead623bdbfa044980fbe08cf Mon Sep 17 00:00:00 2001 From: Owen Date: Tue, 3 Nov 2026 12:18:22 +0100 Subject: [PATCH] Reject out-of-range endgame FENs at entry (#25) Add FEN.piece_count/1 and reject positions with more than 7 pieces in the Endgame changeset, so an 8+ piece FEN fails at entry instead of saving and then blocking at play. Accumulate all FEN issues rather than letting a later check overwrite an earlier one, and guard the seed FENs with a test (dropping the out-of-range seed that bypassed the changeset). --- lib/chesstrainer/endgames/endgame.ex | 18 ++++++++---- lib/chesstrainer/fen.ex | 44 ++++++++++++++++++++++++++++ priv/repo/seeds/endgames.ex | 5 ---- test/chesstrainer/endgames_test.exs | 24 +++++++++++++++ test/chesstrainer/fen_test.exs | 23 +++++++++++++++ test/chesstrainer/seeds_test.exs | 24 +++++++++++++++ 6 files changed, 128 insertions(+), 10 deletions(-) create mode 100644 test/chesstrainer/seeds_test.exs diff --git a/lib/chesstrainer/endgames/endgame.ex b/lib/chesstrainer/endgames/endgame.ex index 9accf28..ec7af38 100644 --- a/lib/chesstrainer/endgames/endgame.ex +++ b/lib/chesstrainer/endgames/endgame.ex @@ -66,27 +66,35 @@ defmodule Chesstrainer.Endgames.Endgame do defp fen_issues(fen) do case String.split(fen, " ") do [board, active, _castling, _en_passant, halfmove, fullmove] -> + board_valid? = valid_board?(board) + pieces = Chesstrainer.FEN.piece_count(fen) + issues = [] issues = - if valid_board?(board), + if board_valid?, do: issues, - else: ["board must be 8 ranks of 8 squares each"] + else: issues ++ ["board must be 8 ranks of 8 squares each"] issues = if active in ["w", "b"], do: issues, - else: ["active color must be 'w' or 'b'"] + else: issues ++ ["active color must be 'w' or 'b'"] issues = if non_negative_integer?(halfmove), do: issues, - else: ["halfmove clock must be a non-negative integer"] + else: issues ++ ["halfmove clock must be a non-negative integer"] issues = if positive_integer?(fullmove), do: issues, - else: ["fullmove number must be a positive integer"] + else: issues ++ ["fullmove number must be a positive integer"] + + issues = + if board_valid? and is_integer(pieces) and pieces > 7, + do: issues ++ ["positions with more than 7 pieces are not covered by tablebases"], + else: issues issues diff --git a/lib/chesstrainer/fen.ex b/lib/chesstrainer/fen.ex index eca1ec2..26e503d 100644 --- a/lib/chesstrainer/fen.ex +++ b/lib/chesstrainer/fen.ex @@ -40,6 +40,50 @@ defmodule Chesstrainer.FEN do end end + @doc """ + Counts the pieces on the board, or returns `nil` if the board field is not a + well-formed FEN board. + + Kings count as pieces; empty-square digits are ignored. This is the number + tablebases are sized by, so a position with more than seven pieces is beyond + every tablebase. + + ## Examples + + iex> Chesstrainer.FEN.piece_count("8/8/3k4/8/8/3K1R2/8/8 w - - 0 1") + 3 + + iex> Chesstrainer.FEN.piece_count("not a fen") + nil + """ + @spec piece_count(String.t() | nil) :: non_neg_integer() | nil + def piece_count(fen) when is_binary(fen) do + case String.split(fen, " ") do + [board | _] -> board_piece_count(board) + _ -> nil + end + end + + def piece_count(_fen), do: nil + + defp board_piece_count(board) do + board + |> String.graphemes() + |> Enum.reduce_while(0, fn + "/", acc -> + {:cont, acc} + + <>, acc when c in ?1..?8 -> + {:cont, acc} + + piece, acc when piece in ~w(p n b r q k P N B R Q K) -> + {:cont, acc + 1} + + _other, _acc -> + {:halt, nil} + end) + end + defp color_initial_to_color_atom("b"), do: :black defp color_initial_to_color_atom("w"), do: :white defp color_initial_to_color_atom(_), do: nil diff --git a/priv/repo/seeds/endgames.ex b/priv/repo/seeds/endgames.ex index eeb19ac..6a439c6 100644 --- a/priv/repo/seeds/endgames.ex +++ b/priv/repo/seeds/endgames.ex @@ -8,11 +8,6 @@ defmodule Chesstrainer.Repo.Seeds.Endgames do notes: "Basic rook endgame", result: :win }, - %{ - fen: "3k4/2n2B2/1KP5/2B2p2/5b1p/7P/8/8 b - - 0 0", - color: :black, - result: :win - }, %{ fen: "r7/4R2P/3p4/3k1K2/2p5/8/8/8 b - - 0 0", color: :black, diff --git a/test/chesstrainer/endgames_test.exs b/test/chesstrainer/endgames_test.exs index a083366..7fe9eca 100644 --- a/test/chesstrainer/endgames_test.exs +++ b/test/chesstrainer/endgames_test.exs @@ -115,6 +115,30 @@ defmodule Chesstrainer.EndgamesTest do assert [msg] = errors_on(changeset).fen assert msg =~ "active color" end + + test "rejects a FEN with more than 7 pieces" do + attrs = %{ + fen: "3k4/2n2B2/1KP5/2B2p2/5b1p/7P/8/8 b - - 0 1", + color: :black, + result: :win, + rating: 1500 + } + + assert {:error, changeset} = Endgames.create_endgame(attrs) + assert [msg] = errors_on(changeset).fen + assert msg =~ "more than 7 pieces" + end + + test "accepts a FEN with exactly 7 pieces" do + attrs = %{ + fen: "6k1/5p2/6p1/8/7p/8/6PP/6K1 b - - 0 1", + color: :black, + result: :win, + rating: 1500 + } + + assert {:ok, %Endgame{}} = Endgames.create_endgame(attrs) + end end describe "themes" do diff --git a/test/chesstrainer/fen_test.exs b/test/chesstrainer/fen_test.exs index d61ffcd..4bca2c3 100644 --- a/test/chesstrainer/fen_test.exs +++ b/test/chesstrainer/fen_test.exs @@ -59,4 +59,27 @@ defmodule Chesstrainer.FENTest do assert FEN.normalize(fen) == fen end end + + describe "piece_count/1" do + test "counts kings and pieces, ignoring empty squares and slashes" do + assert FEN.piece_count("8/8/3k4/8/8/3K1R2/8/8 w - - 0 1") == 3 + end + + test "counts a full seven-piece position" do + assert FEN.piece_count("6k1/5p2/6p1/8/7p/8/6PP/6K1 b - - 0 0") == 7 + end + + test "counts a position beyond the tablebase range" do + assert FEN.piece_count("3k4/2n2B2/1KP5/2B2p2/5b1p/7P/8/8 b - - 0 1") == 10 + end + + test "counts a board field without clocks" do + assert FEN.piece_count("8/8/3k4/8/8/3K1R2/8/8") == 3 + end + + test "returns nil for a malformed board" do + assert FEN.piece_count("not a fen") == nil + assert FEN.piece_count(nil) == nil + end + end end diff --git a/test/chesstrainer/seeds_test.exs b/test/chesstrainer/seeds_test.exs new file mode 100644 index 0000000..764d6ba --- /dev/null +++ b/test/chesstrainer/seeds_test.exs @@ -0,0 +1,24 @@ +defmodule Chesstrainer.SeedsTest do + use ExUnit.Case, async: true + + alias Chesstrainer.FEN + + Code.require_file("../../priv/repo/seeds/endgames.ex", __DIR__) + + describe "seed endgames" do + test "are normalised and within tablebase range" do + for endgame <- Chesstrainer.Repo.Seeds.Endgames.all() do + fen = FEN.normalize(endgame.fen) + + assert length(String.split(fen, " ")) == 6, + "seed FEN must have 6 fields: #{endgame.fen}" + + refute String.ends_with?(fen, " 0"), + "seed FEN keeps fullmove 0 after normalisation: #{endgame.fen}" + + assert FEN.piece_count(fen) <= 7, + "seed FEN has more than 7 pieces: #{endgame.fen}" + end + end + end +end