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.
This commit is contained in:
2026-11-01 21:24:51 +01:00
parent 6f08cdd10f
commit 8b894465cc
14 changed files with 359 additions and 83 deletions
@@ -0,0 +1,82 @@
defmodule Chesstrainer.Endgames.TablebaseTest do
use ExUnit.Case, async: false
alias Chesstrainer.Endgames.Tablebase
alias Chesstrainer.Endgames.Tablebase.Cache, as: TablebaseCache
alias Chesstrainer.Endgames.Tablebase.Parser
describe "normalize_category/1" do
test "maps winning categories to :win" do
for category <- ["win", "syzygy-win", "maybe-win"] do
assert Tablebase.normalize_category(category) == :win
end
end
test "maps 50-move-rule draws to :draw" do
for category <- ["draw", "cursed-win", "blessed-loss"] do
assert Tablebase.normalize_category(category) == :draw
end
end
test "maps losing categories to :loss" do
for category <- ["loss", "syzygy-loss", "maybe-loss"] do
assert Tablebase.normalize_category(category) == :loss
end
end
test "falls back to :unknown for anything else" do
for category <- ["unknown", "banana", nil] do
assert Tablebase.normalize_category(category) == :unknown
end
end
end
describe "Parser.from_map/1" do
test "normalises the position and move categories" do
assert {:ok, tablebase} =
Parser.from_map(%{
"category" => "cursed-win",
"checkmate" => false,
"dtc" => nil,
"dtm" => 20,
"dtw" => nil,
"dtz" => 3,
"insufficient_material" => false,
"moves" => [
%{
"category" => "blessed-loss",
"checkmate" => false,
"conversion" => false,
"dtc" => nil,
"dtm" => -19,
"dtw" => nil,
"dtz" => -2,
"insufficient_material" => false,
"precise_dtz" => -2,
"san" => "Kd7",
"stalemate" => false,
"uci" => "e8d7",
"variant_loss" => false,
"variant_win" => false,
"zeroing" => false
}
]
})
assert tablebase.category == :draw
assert [%Tablebase.Move{category: :draw, uci: "e8d7"}] = tablebase.moves
end
end
describe "new/1" do
test "returns {:ok, tablebase} on a cache hit" do
fen = "8/8/3k4/8/8/3K1R2/8/8 w - - 0 1"
tablebase = %Tablebase{category: :win, moves: []}
TablebaseCache.put(fen, tablebase)
on_exit(fn -> TablebaseCache.delete(fen) end)
assert {:ok, ^tablebase} = Tablebase.new(fen)
end
end
end