feat(f3): calculate round scores and move to reveal phase
All checks were successful
CI / test-and-quality (push) Successful in 57s
CI / test-and-quality (pull_request) Successful in 59s

This commit is contained in:
2026-02-27 17:01:36 +01:00
parent 49e9d1be41
commit 1017ed0c4c
4 changed files with 220 additions and 1 deletions

View File

@@ -443,3 +443,106 @@ class GuessSubmissionTests(TestCase):
self.assertEqual(response.status_code, 400)
self.assertEqual(response.json()["error"], "Guess submission window has closed")
class ScoreCalculationTests(TestCase):
def setUp(self):
self.host = User.objects.create_user(username="host_score", password="secret123")
self.other_user = User.objects.create_user(username="other_score", password="secret123")
self.session = GameSession.objects.create(host=self.host, code="SC0RE1", status=GameSession.Status.GUESS)
self.category = Category.objects.create(name="Sport", slug="sport", is_active=True)
self.question = Question.objects.create(
category=self.category,
prompt="Hvilken sport spiller man i Wimbledon?",
correct_answer="Tennis",
is_active=True,
)
RoundConfig.objects.create(
session=self.session,
number=1,
category=self.category,
points_correct=5,
points_bluff=2,
)
self.round_question = RoundQuestion.objects.create(
session=self.session,
round_number=1,
question=self.question,
correct_answer="Tennis",
)
self.player_one = Player.objects.create(session=self.session, nickname="Luna")
self.player_two = Player.objects.create(session=self.session, nickname="Mads")
self.player_three = Player.objects.create(session=self.session, nickname="Nora")
def test_host_can_calculate_scores_and_transition_to_reveal(self):
Guess.objects.create(round_question=self.round_question, player=self.player_one, selected_text="Tennis", is_correct=True)
Guess.objects.create(
round_question=self.round_question,
player=self.player_two,
selected_text="Padel",
is_correct=False,
fooled_player=self.player_three,
)
Guess.objects.create(
round_question=self.round_question,
player=self.player_three,
selected_text="Padel",
is_correct=False,
fooled_player=self.player_three,
)
self.client.login(username="host_score", password="secret123")
response = self.client.post(
reverse(
"lobby:calculate_scores",
kwargs={"code": self.session.code, "round_question_id": self.round_question.id},
)
)
self.assertEqual(response.status_code, 200)
payload = response.json()
self.assertEqual(payload["session"]["status"], GameSession.Status.REVEAL)
self.assertEqual(payload["events_created"], 2)
self.player_one.refresh_from_db()
self.player_three.refresh_from_db()
self.session.refresh_from_db()
self.assertEqual(self.player_one.score, 5)
self.assertEqual(self.player_three.score, 4)
self.assertEqual(self.session.status, GameSession.Status.REVEAL)
def test_calculate_scores_requires_host(self):
self.client.login(username="other_score", password="secret123")
response = self.client.post(
reverse(
"lobby:calculate_scores",
kwargs={"code": self.session.code, "round_question_id": self.round_question.id},
)
)
self.assertEqual(response.status_code, 403)
self.assertEqual(response.json()["error"], "Only host can calculate scores")
def test_calculate_scores_rejects_duplicate_calculation(self):
Guess.objects.create(round_question=self.round_question, player=self.player_one, selected_text="Tennis", is_correct=True)
self.client.login(username="host_score", password="secret123")
first = self.client.post(
reverse(
"lobby:calculate_scores",
kwargs={"code": self.session.code, "round_question_id": self.round_question.id},
)
)
second = self.client.post(
reverse(
"lobby:calculate_scores",
kwargs={"code": self.session.code, "round_question_id": self.round_question.id},
)
)
self.assertEqual(first.status_code, 200)
self.assertEqual(second.status_code, 409)
self.assertEqual(second.json()["error"], "Scores already calculated for this round question")