Série nově ovlivňuje body, aby běžná hra a připravovaný živý režim pro třídu (`zivy-rezim-trida`) bodovaly srovnatelně. OpenSpec změna `bonus-za-serii`. ## Změny - Za každou odpověď `spravne` se k bodům z časové křivky přičte bonus `min(série − 1, 5) × 10 %` základního maxima režimu (*Psaní* +100 až +500 b., *A–D* +50 až +250 b.). Bonus nezávisí na čase. - `max_body` počítá s nepřerušenou sérií (4 otázky *Psaní* = 4600, 8 otázek *A–D* = 5250). `odpovedi.body` obsahují bonus, součet dál sedí s `kola.body`. - API po odpovědi vrací `streak_bonus`. Zpětná vazba ukazuje body za odpověď a štítek „+200 za sérii ×3“ zvlášť, pod prstencem je štítek s bonusem za příští správnou odpověď. - **BREAKING:** výchozí `POZNAVACKY_SCORE_GRACE_SECONDS` je `3.5` (dosud `2.0`). Starší kola se nepřepočítávají. - `docs/navrh.md` a hlavní specifikace `answer-scoring` a `quiz-gameplay` aktualizovány, změna archivována. ## Ověření - `pytest` 357 passed, `ruff check` bez chyb. - Ručně v Chrome nad syntetickou sadou o 8 položkách: *Psaní* bonus 0 / +100 / … / +500 / +500, *A–D* 0 / +50 / … / +250, po chybě bez bonusu, shrnutí „z 5 250 možných“. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_013nPfHU2PPEvpqm9w9WixvF Reviewed-on: #26 Co-authored-by: David Spáčil <spacildavi@gmail.com>
282 lines
10 KiB
Python
282 lines
10 KiB
Python
"""Jednotkové testy bodovací křivky a vyhodnocování volného textu."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import pytest
|
|
|
|
from poznavacky.config import ConfigError, load_config
|
|
from poznavacky.scoring import (
|
|
AnswerItemSpec,
|
|
ScoringConfig,
|
|
calculate_question_points,
|
|
calculate_question_score,
|
|
calculate_round_max_points,
|
|
calculate_weighted_accuracy,
|
|
damerau_levenshtein,
|
|
evaluate_writing_answer,
|
|
normalize_answer,
|
|
)
|
|
|
|
|
|
def _item(
|
|
item_id: int,
|
|
name: str,
|
|
*,
|
|
latin: str | None = None,
|
|
synonyms: tuple[str, ...] = (),
|
|
) -> AnswerItemSpec:
|
|
return AnswerItemSpec(
|
|
item_id=item_id,
|
|
canonical_name=name,
|
|
latin_name=latin,
|
|
synonyms=synonyms,
|
|
)
|
|
|
|
|
|
def test_scoring_config_defaults_and_custom_env() -> None:
|
|
cfg = load_config(environ={}, overrides={"TESTING": True})
|
|
assert cfg.score_grace_seconds == 3.5
|
|
assert cfg.score_max_seconds == 15.0
|
|
assert cfg.score_floor_ratio == 0.30
|
|
assert cfg.quick_round_size == 10
|
|
|
|
custom = load_config(
|
|
environ={
|
|
"POZNAVACKY_SECRET_KEY": "tajny",
|
|
"POZNAVACKY_SCORE_GRACE_SECONDS": "3.0",
|
|
"POZNAVACKY_SCORE_MAX_SECONDS": "13.0",
|
|
"POZNAVACKY_SCORE_FLOOR_RATIO": "0.5",
|
|
"POZNAVACKY_QUICK_ROUND_SIZE": "8",
|
|
}
|
|
)
|
|
assert custom.score_grace_seconds == 3.0
|
|
assert custom.score_max_seconds == 13.0
|
|
assert custom.score_floor_ratio == 0.5
|
|
assert custom.quick_round_size == 8
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"env_overrides",
|
|
[
|
|
{"POZNAVACKY_SCORE_GRACE_SECONDS": "-1"},
|
|
{
|
|
"POZNAVACKY_SCORE_GRACE_SECONDS": "10",
|
|
"POZNAVACKY_SCORE_MAX_SECONDS": "10",
|
|
},
|
|
{
|
|
"POZNAVACKY_SCORE_GRACE_SECONDS": "12",
|
|
"POZNAVACKY_SCORE_MAX_SECONDS": "8",
|
|
},
|
|
{"POZNAVACKY_SCORE_FLOOR_RATIO": "0"},
|
|
{"POZNAVACKY_SCORE_FLOOR_RATIO": "1.2"},
|
|
{"POZNAVACKY_QUICK_ROUND_SIZE": "0"},
|
|
],
|
|
)
|
|
def test_scoring_config_rejects_invalid_values(
|
|
env_overrides: dict[str, str],
|
|
) -> None:
|
|
env = {"POZNAVACKY_SECRET_KEY": "tajny", **env_overrides}
|
|
with pytest.raises(ConfigError):
|
|
load_config(environ=env)
|
|
|
|
|
|
def test_time_scoring_curve_default_boundaries() -> None:
|
|
cfg = ScoringConfig()
|
|
|
|
# Grace period 0-3.5 s -> 100 % maxima.
|
|
assert calculate_question_points("psani", "spravne", 0, cfg) == 1000
|
|
assert calculate_question_points("psani", "spravne", 3400, cfg) == 1000
|
|
assert calculate_question_points("psani", "spravne", 3500, cfg) == 1000
|
|
assert calculate_question_points("psani", "castecne", 1500, cfg) == 500
|
|
assert calculate_question_points("abcd", "spravne", 3500, cfg) == 500
|
|
assert calculate_question_points("psani", "spatne", 500, cfg) == 0
|
|
assert calculate_question_points("abcd", "spatne", 500, cfg) == 0
|
|
|
|
# Střed intervalu (9.25 s mezi 3.5 a 15.0 s -> koeficient 0.65).
|
|
assert calculate_question_points("psani", "spravne", 9250, cfg) == 650
|
|
assert calculate_question_points("psani", "castecne", 9250, cfg) == 325
|
|
assert calculate_question_points("abcd", "spravne", 9250, cfg) == 325
|
|
|
|
# Strop a nad stropem -> podlaha 30 %.
|
|
assert calculate_question_points("psani", "spravne", 15000, cfg) == 300
|
|
assert calculate_question_points("psani", "spravne", 20000, cfg) == 300
|
|
assert calculate_question_points("psani", "castecne", 20000, cfg) == 150
|
|
assert calculate_question_points("abcd", "spravne", 20000, cfg) == 150
|
|
assert calculate_question_points("psani", "spatne", 20000, cfg) == 0
|
|
|
|
|
|
def test_streak_bonus_grows_and_caps() -> None:
|
|
totals = [
|
|
calculate_question_score("psani", "spravne", 1000, streak).total
|
|
for streak in range(1, 8)
|
|
]
|
|
assert totals == [1000, 1100, 1200, 1300, 1400, 1500, 1500]
|
|
|
|
|
|
def test_streak_bonus_is_time_independent_and_halved_in_abcd() -> None:
|
|
score = calculate_question_score("abcd", "spravne", 20000, 3)
|
|
assert score.time_points == 150
|
|
assert score.streak_bonus == 100
|
|
assert score.total == 250
|
|
|
|
|
|
def test_partial_answer_gets_no_bonus_and_breaks_streak() -> None:
|
|
# Po dvou `spravne` přijde `castecne` (série -> 0) a pak `spravne`
|
|
# (série 1).
|
|
partial = calculate_question_score("psani", "castecne", 1000, 0)
|
|
assert (partial.time_points, partial.streak_bonus) == (500, 0)
|
|
# Ani případná nenulová série bonus k `castecne` nepřidá.
|
|
assert calculate_question_score("psani", "castecne", 1000, 3).total == 500
|
|
after = calculate_question_score("psani", "spravne", 1000, 1)
|
|
assert (after.time_points, after.streak_bonus) == (1000, 0)
|
|
assert calculate_question_score("psani", "spatne", 1000, 0).total == 0
|
|
|
|
|
|
def test_time_scoring_curve_custom_parameters() -> None:
|
|
cfg = ScoringConfig(grace_seconds=3.0, max_seconds=13.0, floor_ratio=0.5)
|
|
assert calculate_question_points("psani", "spravne", 3000, cfg) == 1000
|
|
assert calculate_question_points("psani", "spravne", 8000, cfg) == 750
|
|
assert calculate_question_points("psani", "spravne", 13000, cfg) == 500
|
|
assert calculate_question_points("abcd", "spravne", 15000, cfg) == 250
|
|
|
|
|
|
def test_weighted_accuracy_and_round_max_points() -> None:
|
|
assert calculate_weighted_accuracy(2, 1, 4) == pytest.approx(0.625)
|
|
assert calculate_weighted_accuracy(0, 0, 0) == 0.0
|
|
assert calculate_weighted_accuracy(3, 0, 3) == 1.0
|
|
assert calculate_round_max_points("psani", 4) == 4600
|
|
assert calculate_round_max_points("abcd", 8) == 5250
|
|
assert calculate_round_max_points("abcd", 0) == 0
|
|
|
|
|
|
def test_normalize_and_damerau_levenshtein() -> None:
|
|
assert normalize_answer(" SYKORA - koňadra! ") == "sykora konadra"
|
|
assert normalize_answer(" --- ") == ""
|
|
assert damerau_levenshtein("konadra", "konarda") == 1
|
|
assert damerau_levenshtein("vlastovka", "vlasofka") == 2
|
|
|
|
|
|
def test_writing_exact_diacritics_synonyms_latin_and_empty() -> None:
|
|
sykora = _item(
|
|
1,
|
|
"Sýkora koňadra",
|
|
latin="Parus major",
|
|
synonyms=("koňadra", "sýkora velká"),
|
|
)
|
|
items = [sykora, _item(2, "Kos černý", latin="Turdus merula")]
|
|
|
|
assert (
|
|
evaluate_writing_answer(" SYKORA - konadra! ", sykora, items).verdict
|
|
== "spravne"
|
|
)
|
|
assert (
|
|
evaluate_writing_answer("konadra", sykora, items).verdict == "spravne"
|
|
)
|
|
assert (
|
|
evaluate_writing_answer("parus major", sykora, items).verdict
|
|
== "spravne"
|
|
)
|
|
assert evaluate_writing_answer("", sykora, items).verdict == "spatne"
|
|
assert evaluate_writing_answer(" --- ", sykora, items).verdict == "spatne"
|
|
|
|
|
|
def test_writing_typo_tolerance_and_short_words() -> None:
|
|
sykora = _item(1, "Sýkora koňadra")
|
|
kos = _item(2, "Kos")
|
|
kane = _item(3, "Káně lesní")
|
|
items = [sykora, kos, kane]
|
|
|
|
# Překlep (transpozice) v delším slově >= 5 znaků bez kolize -> spravne.
|
|
res = evaluate_writing_answer("sykora konarda", sykora, items)
|
|
assert res.verdict == "spravne"
|
|
assert res.reason == "typo"
|
|
|
|
# Překlep v obou slovech >= 5 znaků bez kolize -> spravne.
|
|
res_both = evaluate_writing_answer("sikora konarda", sykora, items)
|
|
assert res_both.verdict == "spravne"
|
|
|
|
# Krátké slovo (< 5 znaků) netoleruje překlep.
|
|
assert evaluate_writing_answer("kas", kos, items).verdict == "spatne"
|
|
# "kane" má jen 4 znaky -> překlep "kano" nesplní plnou shodu.
|
|
assert (
|
|
evaluate_writing_answer("kano lesni", kane, items).verdict != "spravne"
|
|
)
|
|
|
|
|
|
def test_writing_collision_protection_in_set() -> None:
|
|
# Dva velmi podobné druhy v téže sadě -> vstup stejně blízko oběma
|
|
# nesmí projít jako plně správný.
|
|
a = _item(1, "Káně lesní")
|
|
b = _item(2, "Káně lesná")
|
|
items = [a, b]
|
|
|
|
res = evaluate_writing_answer("kane lesno", a, items)
|
|
assert res.verdict != "spravne"
|
|
|
|
# Přesný název jiného druhu téhož rodu v sadě není správně.
|
|
c = _item(3, "Káně rousná")
|
|
res_other = evaluate_writing_answer("kane rousna", a, [a, c])
|
|
assert res_other.verdict == "spatne"
|
|
|
|
|
|
def test_writing_partial_genus_with_and_without_typo() -> None:
|
|
konadra = _item(1, "Sýkora koňadra")
|
|
modrinka = _item(2, "Sýkora modřinka")
|
|
items = [konadra, modrinka]
|
|
|
|
# Více druhů stejného rodu v sadě nebrání částečnému uznání rodu.
|
|
assert (
|
|
evaluate_writing_answer("sykora", konadra, items).verdict == "castecne"
|
|
)
|
|
res_typo = evaluate_writing_answer("sikora", konadra, items)
|
|
assert res_typo.verdict == "castecne"
|
|
assert res_typo.reason == "genus_typo"
|
|
|
|
# Pokud jiná položka má odlišný rod "sikora", překlep koliduje -> spatne.
|
|
other_genus = _item(3, "Šikora obecná")
|
|
res_collision = evaluate_writing_answer(
|
|
"sikora", konadra, [konadra, modrinka, other_genus]
|
|
)
|
|
assert res_collision.verdict == "spatne"
|
|
|
|
|
|
def test_writing_partial_unambiguous_species_vs_ambiguous() -> None:
|
|
konadra = _item(1, "Sýkora koňadra")
|
|
kane = _item(2, "Káně lesní")
|
|
strakapoud = _item(3, "Strakapoud lesní")
|
|
items = [konadra, kane, strakapoud]
|
|
|
|
# Jednoznačný druh (přesně i s 1 překlepem >= 5 znaků) -> castecne.
|
|
assert (
|
|
evaluate_writing_answer("konadra", konadra, items).verdict == "castecne"
|
|
)
|
|
assert (
|
|
evaluate_writing_answer("konarda", konadra, items).verdict == "castecne"
|
|
)
|
|
|
|
# Nejednoznačný druh "lesní" (dvě položky v sadě) -> spatne.
|
|
assert evaluate_writing_answer("lesni", kane, items).verdict == "spatne"
|
|
assert evaluate_writing_answer("lesno", kane, items).verdict == "spatne"
|
|
|
|
|
|
def test_writing_partial_close_typo_and_collision() -> None:
|
|
vlastovka = _item(1, "Vlaštovka obecná")
|
|
sedmikraska = _item(2, "Sedmikráska")
|
|
items = [vlastovka, sedmikraska]
|
|
|
|
# DL = 2 u dlouhého názvu (>= 10 znaků) bez kolize -> castecne.
|
|
res = evaluate_writing_answer("vlasofka obecna", vlastovka, items)
|
|
assert res.verdict == "castecne"
|
|
assert res.reason == "close_typo"
|
|
|
|
res_single = evaluate_writing_answer("sedmkrazka", sedmikraska, items)
|
|
assert res_single.verdict == "castecne"
|
|
assert res_single.reason == "close_typo"
|
|
|
|
# Kolize s jinou položkou v sadě do vzdálenosti <= 2 -> spatne.
|
|
close_other = _item(3, "Vlasofka obecná")
|
|
res_coll = evaluate_writing_answer(
|
|
"vlasofka obecna", vlastovka, [vlastovka, close_other]
|
|
)
|
|
assert res_coll.verdict == "spatne"
|