mirror of
https://github.com/TheRealOwenRees/chess-endgame-trainer.git
synced 2026-11-18 03:32:06 +00:00
Add admin bulk FEN importer (#156)
* change position of theme picker * Add admin bulk FEN importer Paste FENs, resolve results from the local Syzygy tablebase only, preview each line's status, and bulk insert with themes and a batch rating.
This commit is contained in:
@@ -0,0 +1,141 @@
|
||||
defmodule Chesstrainer.Endgames.BulkImportTest do
|
||||
# Not async: the tablebase path is application config and Fathom keeps global
|
||||
# state, so other tests must not flip the local backend while this runs.
|
||||
use Chesstrainer.DataCase, async: false
|
||||
|
||||
import Chesstrainer.ThemesFixtures
|
||||
|
||||
alias Chesstrainer.Endgames
|
||||
alias Chesstrainer.Endgames.BulkImport
|
||||
alias Chesstrainer.Endgames.Endgame
|
||||
alias Chesstrainer.Repo
|
||||
|
||||
@krk "4k3/8/8/8/8/8/8/R3K3 w - - 0 1"
|
||||
@mate "7k/6Q1/5K2/8/8/8/8/8 b - - 0 1"
|
||||
@six_pieces "4k2r/8/8/8/8/8/8/R2QK2R w - - 0 1"
|
||||
|
||||
describe "preview/1" do
|
||||
test "classifies a position the local tablebase can answer" do
|
||||
assert %{entries: [entry], too_many?: false} = BulkImport.preview(@krk)
|
||||
|
||||
assert entry.status == :ready
|
||||
assert entry.result == :win
|
||||
assert entry.color == :white
|
||||
assert entry.key == "KR v K"
|
||||
assert entry.fen == @krk
|
||||
assert entry.reason == nil
|
||||
end
|
||||
|
||||
test "marks checkmate positions as terminal" do
|
||||
assert %{entries: [entry]} = BulkImport.preview(@mate)
|
||||
|
||||
assert entry.status == :terminal
|
||||
assert entry.result == nil
|
||||
end
|
||||
|
||||
test "rejects positions with more than five pieces" do
|
||||
assert %{entries: [entry]} = BulkImport.preview(@six_pieces)
|
||||
|
||||
assert entry.status == :too_many_pieces
|
||||
assert entry.reason =~ "5 or fewer pieces"
|
||||
end
|
||||
|
||||
test "rejects malformed FENs" do
|
||||
assert %{entries: [entry]} = BulkImport.preview("not a fen")
|
||||
|
||||
assert entry.status == :invalid_fen
|
||||
assert entry.reason =~ "6 space-separated fields"
|
||||
end
|
||||
|
||||
test "reports duplicates within the batch without probing twice" do
|
||||
assert %{entries: [first, second]} = BulkImport.preview("#{@krk}\n#{@krk}")
|
||||
|
||||
assert first.status == :ready
|
||||
assert second.status == :duplicate
|
||||
assert second.reason == "Duplicate FEN in this batch"
|
||||
end
|
||||
|
||||
test "reports FENs already stored as duplicates" do
|
||||
{:ok, _endgame} =
|
||||
Endgames.create_endgame(%{fen: @krk, color: :white, result: :win})
|
||||
|
||||
assert %{entries: [entry]} = BulkImport.preview(@krk)
|
||||
|
||||
assert entry.status == :duplicate
|
||||
assert entry.reason == "FEN already exists"
|
||||
end
|
||||
|
||||
test "ignores blank lines and comments" do
|
||||
text = "\n# a comment\n#{@krk}\n\n"
|
||||
|
||||
assert %{entries: [entry]} = BulkImport.preview(text)
|
||||
assert entry.status == :ready
|
||||
end
|
||||
|
||||
test "caps the input at max_fens/0" do
|
||||
text = Enum.join(List.duplicate("not a fen", BulkImport.max_fens() + 1), "\n")
|
||||
|
||||
assert %{entries: entries, too_many?: true} = BulkImport.preview(text)
|
||||
assert length(entries) == BulkImport.max_fens()
|
||||
end
|
||||
|
||||
test "reports unsupported when the local tablebase is not configured" do
|
||||
previous = Application.get_env(:chesstrainer, :tablebase_path)
|
||||
|
||||
on_exit(fn -> Application.put_env(:chesstrainer, :tablebase_path, previous) end)
|
||||
|
||||
Application.put_env(:chesstrainer, :tablebase_path, nil)
|
||||
|
||||
assert %{entries: [entry]} = BulkImport.preview(@krk)
|
||||
assert entry.status == :unsupported
|
||||
assert entry.reason =~ "not configured"
|
||||
end
|
||||
end
|
||||
|
||||
describe "commit/3" do
|
||||
test "inserts ready entries with the batch rating and themes" do
|
||||
theme = theme_fixture(%{name: "Lucena"})
|
||||
|
||||
assert %{entries: entries} = BulkImport.preview(@krk)
|
||||
assert %{imported: 1, skipped: 0} = BulkImport.commit(entries, [theme], 1700)
|
||||
|
||||
endgame =
|
||||
Repo.get_by!(Endgame, fen: @krk)
|
||||
|> Repo.preload(:themes)
|
||||
|
||||
assert endgame.color == :white
|
||||
assert endgame.result == :win
|
||||
assert endgame.key == "KR v K"
|
||||
assert endgame.rating == 1700.0
|
||||
assert [%{id: theme_id}] = endgame.themes
|
||||
assert theme_id == theme.id
|
||||
end
|
||||
|
||||
test "ignores entries that are not ready" do
|
||||
assert %{entries: entries} = BulkImport.preview("not a fen")
|
||||
|
||||
assert %{imported: 0, skipped: 1} = BulkImport.commit(entries)
|
||||
assert Repo.aggregate(Endgame, :count) == 0
|
||||
end
|
||||
|
||||
test "skips rows that collide with an existing FEN" do
|
||||
{:ok, _existing} =
|
||||
Endgames.create_endgame(%{fen: @krk, color: :white, result: :win})
|
||||
|
||||
entries = [
|
||||
%{
|
||||
line: 1,
|
||||
fen: @krk,
|
||||
key: "KR v K",
|
||||
color: :white,
|
||||
result: :win,
|
||||
status: :ready,
|
||||
reason: nil
|
||||
}
|
||||
]
|
||||
|
||||
assert %{imported: 0, skipped: 1} = BulkImport.commit(entries)
|
||||
assert Repo.aggregate(Endgame, :count) == 1
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -46,7 +46,8 @@ defmodule ChesstrainerWeb.AdminGatingTest do
|
||||
~p"/admin/themes",
|
||||
~p"/admin/themes/new",
|
||||
~p"/admin/users",
|
||||
~p"/admin/users/new"
|
||||
~p"/admin/users/new",
|
||||
~p"/admin/endgames/import"
|
||||
] do
|
||||
assert redirected_to(get(conn, path)) == ~p"/"
|
||||
end
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
defmodule ChesstrainerWeb.EndgameLive.ImportTest do
|
||||
use ChesstrainerWeb.ConnCase
|
||||
|
||||
import Phoenix.LiveViewTest
|
||||
import Chesstrainer.ThemesFixtures
|
||||
|
||||
alias Chesstrainer.Endgames
|
||||
alias Chesstrainer.Endgames.Endgame
|
||||
alias Chesstrainer.Repo
|
||||
|
||||
@krk "4k3/8/8/8/8/8/8/R3K3 w - - 0 1"
|
||||
@mate "7k/6Q1/5K2/8/8/8/8/8 b - - 0 1"
|
||||
@six_pieces "4k2r/8/8/8/8/8/8/R2QK2R w - - 0 1"
|
||||
|
||||
setup :register_and_log_in_admin
|
||||
|
||||
test "shows the local tablebase status", %{conn: conn} do
|
||||
{:ok, _view, html} = live(conn, ~p"/admin/endgames/import")
|
||||
|
||||
assert html =~ "Local Syzygy tablebase ready"
|
||||
end
|
||||
|
||||
test "looks up FENs and imports the selected ones", %{conn: conn} do
|
||||
theme = theme_fixture(%{name: "Lucena"})
|
||||
|
||||
{:ok, view, _html} = live(conn, ~p"/admin/endgames/import")
|
||||
|
||||
view
|
||||
|> form("#endgame-import-form", %{fens: @krk, rating: "1700"})
|
||||
|> render_submit()
|
||||
|
||||
html = render_async(view, 5_000)
|
||||
|
||||
assert html =~ "Ready"
|
||||
assert html =~ "KR v K"
|
||||
assert html =~ "win"
|
||||
|
||||
view |> form("#theme-form", %{theme_search: "Luc"}) |> render_change()
|
||||
view |> element("button", "Lucena") |> render_click()
|
||||
|
||||
view |> element("button", "Import 1 endgame(s)") |> render_click()
|
||||
|
||||
assert_redirect(view, ~p"/admin/endgames")
|
||||
|
||||
endgame = Repo.get_by!(Endgame, fen: @krk) |> Repo.preload(:themes)
|
||||
|
||||
assert endgame.rating == 1700.0
|
||||
assert endgame.result == :win
|
||||
assert endgame.color == :white
|
||||
assert [%{id: theme_id}] = endgame.themes
|
||||
assert theme_id == theme.id
|
||||
end
|
||||
|
||||
test "explains why lines cannot be imported", %{conn: conn} do
|
||||
{:ok, view, _html} = live(conn, ~p"/admin/endgames/import")
|
||||
|
||||
view
|
||||
|> form("#endgame-import-form", %{
|
||||
fens: "#{@six_pieces}\nnot a fen\n#{@mate}",
|
||||
rating: "1500"
|
||||
})
|
||||
|> render_submit()
|
||||
|
||||
html = render_async(view, 5_000)
|
||||
|
||||
assert html =~ "Too many pieces"
|
||||
assert html =~ "Invalid FEN"
|
||||
assert html =~ "Terminal"
|
||||
refute html =~ "Import 1 endgame(s)"
|
||||
end
|
||||
|
||||
test "does not import when every line is a duplicate", %{conn: conn} do
|
||||
{:ok, _endgame} = Endgames.create_endgame(%{fen: @krk, color: :white, result: :win})
|
||||
|
||||
{:ok, view, _html} = live(conn, ~p"/admin/endgames/import")
|
||||
|
||||
view
|
||||
|> form("#endgame-import-form", %{fens: @krk, rating: "1500"})
|
||||
|> render_submit()
|
||||
|
||||
html = render_async(view, 5_000)
|
||||
|
||||
assert html =~ "Duplicate"
|
||||
assert html =~ "FEN already exists"
|
||||
assert html =~ "Import 0 endgame(s)"
|
||||
end
|
||||
|
||||
test "warns when the local tablebase is not configured", %{conn: conn} do
|
||||
previous = Application.get_env(:chesstrainer, :tablebase_path)
|
||||
on_exit(fn -> Application.put_env(:chesstrainer, :tablebase_path, previous) end)
|
||||
|
||||
Application.put_env(:chesstrainer, :tablebase_path, nil)
|
||||
|
||||
{:ok, _view, html} = live(conn, ~p"/admin/endgames/import")
|
||||
|
||||
assert html =~ "Local tablebase is not configured"
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user