add visual rating guide (#48)

This commit is contained in:
2026-11-06 21:52:07 +01:00
parent 64a9210580
commit f2d886d602
8 changed files with 178 additions and 5 deletions
@@ -0,0 +1,47 @@
defmodule ChesstrainerWeb.RatingComponentsTest do
use ChesstrainerWeb.ConnCase, async: true
import Phoenix.LiveViewTest
alias ChesstrainerWeb.RatingComponents
test "shows the rating and the ±2 deviation confidence band" do
html = render_component(&RatingComponents.rating_guide/1, %{rating: 1500.0, deviation: 100.0})
assert html =~ "Your rating: 1500"
assert html =~ "likely 1300–1700"
assert html =~ "left: 40.0%"
assert html =~ "left: 32.0%"
assert html =~ "width: 16.0%"
assert html =~ "Rating 1500, likely between 1300 and 1700"
end
test "omits the band when the deviation is unknown" do
html = render_component(&RatingComponents.rating_guide/1, %{rating: 1500.0, deviation: nil})
assert html =~ "Your rating: 1500"
refute html =~ ~s(id="rating-guide-range")
refute html =~ "bg-primary/30"
assert html =~ "Rating 1500 on a 500 to 3000 scale"
end
test "clamps the marker to the drawn scale" do
low = render_component(&RatingComponents.rating_guide/1, %{rating: 100.0, deviation: nil})
high = render_component(&RatingComponents.rating_guide/1, %{rating: 4000.0, deviation: nil})
assert low =~ "left: 0.0%"
assert high =~ "left: 100.0%"
end
test "accepts a custom id for the gauge" do
html =
render_component(&RatingComponents.rating_guide/1, %{
rating: 1500.0,
deviation: 100.0,
id: "session-rating"
})
assert html =~ ~s(id="session-rating")
assert html =~ ~s(id="session-rating-range")
end
end
@@ -501,6 +501,22 @@ defmodule ChesstrainerWeb.EndgameLive.PlayTest do
assert attempt.outcome == :solved
end
test "shows a rating guide with the deviation band", %{conn: conn, user: user} do
endgame = endgame_fixture()
seed_tablebase(endgame.fen)
{:ok, view, _html} = live(conn, ~p"/endgames/#{endgame.public_code}/play")
assert has_element?(view, "#rating-guide")
html = render(view)
low = round(user.rating - 2 * user.rating_deviation)
high = round(user.rating + 2 * user.rating_deviation)
assert html =~ "Your rating: #{round(user.rating)}"
assert html =~ "likely #{low}–#{high}"
end
test "records a failed attempt for the signed-in user", %{conn: conn, user: user} do
endgame = endgame_fixture()
seed_tablebase(endgame.fen, moves: grading_moves())
@@ -89,6 +89,7 @@ defmodule ChesstrainerWeb.EndgameLive.TrainTest do
assert has_element?(view, "#close-difficulty[href='/']")
assert has_element?(view, "a", "Cancel")
refute has_element?(view, "#puzzle-search")
assert has_element?(view, "#rating-guide")
assert render(view) =~ "Choose a difficulty"
end
@@ -147,6 +148,7 @@ defmodule ChesstrainerWeb.EndgameLive.TrainTest do
assert is_float(assigns.last_rating_delta)
assert assigns.last_rating_delta > 0
assert assigns.user_rating > 1500.0
assert assigns.user_deviation < 350.0
end
test "loads a different puzzle on next", %{conn: conn} do