"""Tests for chord_game.game.scoring.""" from chord_game.game.scoring import RoundScore, ScoreKeeper class TestScoreKeeper: def setup_method(self): self.sk = ScoreKeeper() def test_first_try_no_hint_gives_3_stars(self): result = self.sk.score_attempt(correct=True, attempt_number=1, hint_used=False) assert result.stars == 3 assert not result.streak_bonus def test_second_try_no_hint_gives_2_stars(self): result = self.sk.score_attempt(correct=True, attempt_number=2, hint_used=False) assert result.stars == 2 def test_third_try_no_hint_gives_1_star(self): result = self.sk.score_attempt(correct=True, attempt_number=3, hint_used=False) assert result.stars == 1 def test_first_try_with_hint_gives_2_stars(self): result = self.sk.score_attempt(correct=True, attempt_number=1, hint_used=True) assert result.stars == 2 def test_second_try_with_hint_gives_1_star(self): result = self.sk.score_attempt(correct=True, attempt_number=2, hint_used=True) assert result.stars == 1 def test_incorrect_gives_0_stars(self): result = self.sk.score_attempt(correct=False, attempt_number=3, hint_used=False) assert result.stars == 0 assert not result.streak_bonus def test_streak_bonus_after_3_correct(self): self.sk.score_attempt(correct=True, attempt_number=1, hint_used=False) self.sk.score_attempt(correct=True, attempt_number=1, hint_used=False) result = self.sk.score_attempt(correct=True, attempt_number=1, hint_used=False) assert result.streak_bonus # 3 stars base + 1 bonus = 4 assert result.stars == 4 def test_streak_resets_on_incorrect(self): self.sk.score_attempt(correct=True, attempt_number=1, hint_used=False) self.sk.score_attempt(correct=True, attempt_number=1, hint_used=False) self.sk.score_attempt(correct=False, attempt_number=3, hint_used=False) assert self.sk.current_streak == 0 def test_best_streak_preserved(self): # Build streak of 3 for _ in range(3): self.sk.score_attempt(correct=True, attempt_number=1, hint_used=False) assert self.sk.best_streak == 3 # Break and build new streak of 2 self.sk.score_attempt(correct=False, attempt_number=3, hint_used=False) for _ in range(2): self.sk.score_attempt(correct=True, attempt_number=1, hint_used=False) assert self.sk.best_streak == 3 # Still 3 def test_total_stars_accumulate(self): self.sk.score_attempt(correct=True, attempt_number=1, hint_used=False) # 3 self.sk.score_attempt(correct=True, attempt_number=2, hint_used=False) # 2 self.sk.score_attempt(correct=True, attempt_number=1, hint_used=False) # 3+1 streak assert self.sk.total_stars == 9 def test_rounds_played_counts_all(self): self.sk.score_attempt(correct=True, attempt_number=1, hint_used=False) self.sk.score_attempt(correct=False, attempt_number=3, hint_used=False) assert self.sk.rounds_played == 2 def test_correct_answers_only_counts_correct(self): self.sk.score_attempt(correct=True, attempt_number=1, hint_used=False) self.sk.score_attempt(correct=False, attempt_number=3, hint_used=False) assert self.sk.correct_answers == 1 def test_total_attempts_sum(self): self.sk.score_attempt(correct=True, attempt_number=1, hint_used=False) self.sk.score_attempt(correct=True, attempt_number=2, hint_used=False) assert self.sk.total_attempts == 3 class TestScoreKeeperSerialization: def test_round_trip(self): sk = ScoreKeeper( total_stars=15, current_streak=2, best_streak=5, rounds_played=10, correct_answers=8, total_attempts=14, ) data = sk.to_dict() restored = ScoreKeeper.from_dict(data) assert restored.total_stars == 15 assert restored.current_streak == 2 assert restored.best_streak == 5 assert restored.rounds_played == 10 assert restored.correct_answers == 8 assert restored.total_attempts == 14 def test_from_empty_dict(self): sk = ScoreKeeper.from_dict({}) assert sk.total_stars == 0 assert sk.best_streak == 0