mirror of
https://github.com/TheRealOwenRees/chess-endgame-trainer.git
synced 2026-11-18 11:42:07 +00:00
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:
@@ -3,20 +3,20 @@ defmodule Chesstrainer.CacheTest do
|
||||
|
||||
alias Chesstrainer.Cache
|
||||
|
||||
setup do
|
||||
setup context do
|
||||
table = :"test_#{System.unique_integer([:positive])}"
|
||||
{:ok, pid} = Cache.start_link(table)
|
||||
{:ok, pid} = Cache.start_link(table, Map.get(context, :cache_opts, []))
|
||||
on_exit(fn -> if Process.alive?(pid), do: GenServer.stop(pid) end)
|
||||
{:ok, %{pid: pid, table: table}}
|
||||
end
|
||||
|
||||
describe "ChessTrainer.Cache function tests" do
|
||||
describe "basic operations" do
|
||||
test "cache is started", %{pid: pid, table: table} do
|
||||
assert is_pid(pid)
|
||||
assert 0 = Cache.size(table)
|
||||
end
|
||||
|
||||
test "put/3", %{table: table} do
|
||||
test "put/3 and get/2", %{table: table} do
|
||||
assert :ok = Cache.put(table, "key1", "value1")
|
||||
assert {:ok, "value1"} = Cache.get(table, "key1")
|
||||
assert 1 = Cache.size(table)
|
||||
@@ -36,4 +36,82 @@ defmodule Chesstrainer.CacheTest do
|
||||
assert :error = Cache.get(table, "key1")
|
||||
end
|
||||
end
|
||||
|
||||
describe "frequency tracking" do
|
||||
test "get/2 increments the entry frequency", %{table: table} do
|
||||
Cache.put(table, "key", "value")
|
||||
Cache.get(table, "key")
|
||||
Cache.get(table, "key")
|
||||
Cache.get(table, "key")
|
||||
|
||||
assert [{"key", "value", frequency, _last_access}] = Cache.show(table)
|
||||
assert frequency == 4
|
||||
end
|
||||
end
|
||||
|
||||
describe "LFU eviction" do
|
||||
@describetag cache_opts: [max_size: 2]
|
||||
|
||||
test "evicts the least-frequently-used entry on overflow", %{table: table} do
|
||||
Cache.put(table, "hot", "value")
|
||||
for _ <- 1..5, do: Cache.get(table, "hot")
|
||||
|
||||
Cache.put(table, "cold", "value")
|
||||
Cache.put(table, "new", "value")
|
||||
|
||||
assert Cache.size(table) == 2
|
||||
assert {:ok, "value"} = Cache.get(table, "hot")
|
||||
assert :error = Cache.get(table, "cold")
|
||||
assert {:ok, "value"} = Cache.get(table, "new")
|
||||
end
|
||||
|
||||
test "breaks frequency ties by least-recently-used", %{table: table} do
|
||||
Cache.put(table, "older", "value")
|
||||
Cache.put(table, "newer", "value")
|
||||
Cache.put(table, "newest", "value")
|
||||
|
||||
assert Cache.size(table) == 2
|
||||
assert :error = Cache.get(table, "older")
|
||||
assert {:ok, "value"} = Cache.get(table, "newer")
|
||||
assert {:ok, "value"} = Cache.get(table, "newest")
|
||||
end
|
||||
end
|
||||
|
||||
describe "weekly sweep" do
|
||||
test "halves every entry frequency", %{table: table} do
|
||||
Cache.put(table, "busy", "value")
|
||||
for _ <- 1..5, do: Cache.get(table, "busy")
|
||||
|
||||
Cache.put(table, "idle", "value")
|
||||
|
||||
Chesstrainer.Cache.Sweep.halve_frequencies(table)
|
||||
|
||||
frequencies = Map.new(Cache.show(table), fn {k, _v, f, _t} -> {k, f} end)
|
||||
assert frequencies["busy"] == 3
|
||||
assert frequencies["idle"] == 0
|
||||
end
|
||||
end
|
||||
|
||||
describe "persistence" do
|
||||
test "backup and restore preserve frequencies" do
|
||||
table = :"test_persist_#{System.unique_integer([:positive])}"
|
||||
backup = Path.join("priv/static/backups", "#{table}.v2.det")
|
||||
on_exit(fn -> File.rm(backup) end)
|
||||
|
||||
{:ok, pid} = Cache.start_link(table)
|
||||
|
||||
Cache.put(table, "key", "value")
|
||||
Cache.get(table, "key")
|
||||
Cache.get(table, "key")
|
||||
|
||||
assert :ok = Cache.backup(table)
|
||||
assert :ok = GenServer.stop(pid)
|
||||
|
||||
{:ok, restored_pid} = Cache.start_link(table)
|
||||
on_exit(fn -> if Process.alive?(restored_pid), do: GenServer.stop(restored_pid) end)
|
||||
|
||||
assert [{"key", "value", frequency, _last_access}] = Cache.show(table)
|
||||
assert frequency == 3
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -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
|
||||
@@ -21,11 +21,11 @@ defmodule Chesstrainer.TagsTest do
|
||||
end
|
||||
|
||||
test "create_tag/1 with valid data creates a tag" do
|
||||
valid_attrs = %{name: "some name", category: "some category"}
|
||||
valid_attrs = %{name: "some name", category: :endgame}
|
||||
|
||||
assert {:ok, %Tag{} = tag} = Tags.create_tag(valid_attrs)
|
||||
assert tag.name == "some name"
|
||||
assert tag.category == "some category"
|
||||
assert tag.category == :endgame
|
||||
end
|
||||
|
||||
test "create_tag/1 with invalid data returns error changeset" do
|
||||
@@ -34,11 +34,11 @@ defmodule Chesstrainer.TagsTest do
|
||||
|
||||
test "update_tag/2 with valid data updates the tag" do
|
||||
tag = tag_fixture()
|
||||
update_attrs = %{name: "some updated name", category: "some updated category"}
|
||||
update_attrs = %{name: "some updated name", category: :opening}
|
||||
|
||||
assert {:ok, %Tag{} = tag} = Tags.update_tag(tag, update_attrs)
|
||||
assert tag.name == "some updated name"
|
||||
assert tag.category == "some updated category"
|
||||
assert tag.category == :opening
|
||||
end
|
||||
|
||||
test "update_tag/2 with invalid data returns error changeset" do
|
||||
|
||||
Reference in New Issue
Block a user