Files
chess-endgame-trainer/test/chesstrainer_web/live/endgame_live/play_test.exs
T
owenrees 8b894465cc Implement tablebase parsing fixes and LFU cache
#5: normalise the full lichess tablebase category vocabulary (cursed-win
and blessed-loss classify as draws), return {:ok, tablebase} on a cache
hit, and unwrap the tablebase in game_from_fen.

#6: replace the LRU cache with an LFU cache storing
{key, value, frequency, last_access}, evicting the least-frequently-used
entry (LRU tie-break) and halving every frequency on a weekly sweep.
Persistence moves to a versioned v2 dets file, dropping the legacy
backup; the cache restores synchronously at startup.

Also updates stale tags enum and Play LiveView tests, and drops the
tracked legacy cache backup.
2026-11-01 21:24:51 +01:00

114 lines
3.3 KiB
Elixir

defmodule ChesstrainerWeb.EndgameLive.PlayTest do
use ChesstrainerWeb.ConnCase
import Phoenix.LiveViewTest
import Chesstrainer.EndgamesFixtures
defp assert_init_board(view, expected_fen, expected_orientation, expected_player_color) do
%{proxy: {ref, _topic, _}} = view
assert_receive {^ref, {:push_event, "init_board", payload}}, 100
assert payload.fen == expected_fen
assert payload.orientation == expected_orientation
assert payload.player_color == expected_player_color
end
describe "mount" do
test "renders the chessboard hook div and pushes init_board", %{conn: conn} do
endgame = endgame_fixture(color: :white)
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame}/play")
assert has_element?(view, "#endgame-board")
assert_init_board(view, endgame.fen, "white", "white")
end
test "pushes black orientation when the endgame color is black", %{conn: conn} do
endgame = endgame_fixture(color: :black)
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame}/play")
assert_init_board(view, endgame.fen, "black", "black")
end
test "shows endgame metadata in the header", %{conn: conn} do
endgame = endgame_fixture(key: "KQ v K", color: :white, result: :win, rating: 1800)
{:ok, _view, html} = live(conn, ~p"/endgames/#{endgame}/play")
assert html =~ "KQ v K"
assert html =~ "White to move"
assert html =~ "1800"
end
test "raises when the endgame does not exist", %{conn: conn} do
assert_raise Ecto.NoResultsError, fn ->
live(conn, ~p"/endgames/00000000-0000-0000-0000-000000000000/play")
end
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()
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame}/play")
view
|> render_hook("chess_fen_invalid", %{
"fen" => endgame.fen,
"message" => "Invalid FEN: move number must be a positive integer"
})
html = render(view)
assert html =~ "Endgame FEN is invalid"
assert html =~ "move number must be a positive integer"
end
end
describe "endgame_move_played" do
test "appends the SAN move and updates last_san / last_from_to", %{conn: conn} do
endgame = endgame_fixture()
{: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
})
html = render(view)
assert html =~ "Qd7+"
end
end
describe "reset" do
test "clears move list and re-pushes init_board", %{conn: conn} do
endgame = endgame_fixture()
{: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
})
assert render(view) =~ "Qd7+"
view |> element("button", "Reset") |> render_click()
assert render(view) =~ "No Moves"
assert_init_board(view, endgame.fen, "white", "white")
end
end
end