refactor(gameplay): extract start/show transitions from lobby views
Some checks failed
CI / test-and-quality (push) Failing after 11s
CI / test-and-quality (pull_request) Failing after 12s

This commit is contained in:
2026-03-17 19:44:13 +00:00
parent 16c9cf6b57
commit 03850b5ed5
3 changed files with 190 additions and 113 deletions

View File

@@ -57,29 +57,29 @@ class LobbyGameplayExtractionTests(TestCase):
self.assertIs(lobby_views._prepare_mixed_answers, gameplay_services.prepare_mixed_answers)
self.assertIs(lobby_views._resolve_scores, gameplay_services.resolve_scores)
self.assertIs(lobby_views._promote_reveal_to_scoreboard, gameplay_services.promote_reveal_to_scoreboard)
self.assertIs(lobby_views._start_round, gameplay_services.start_round)
self.assertIs(lobby_views._show_question, gameplay_services.show_question)
self.assertIs(lobby_views._start_next_round, gameplay_services.start_next_round)
self.assertIs(lobby_views._finish_game, gameplay_services.finish_game)
self.assertIs(lobby_views._build_phase_view_model, gameplay_payloads.build_phase_view_model)
self.assertIs(lobby_views._build_round_question_payload, gameplay_payloads.build_round_question_payload)
self.assertIs(lobby_views._build_scoreboard_phase_event, gameplay_payloads.build_scoreboard_phase_event)
self.assertIs(lobby_views._build_start_round_response, gameplay_payloads.build_start_round_response)
self.assertIs(lobby_views._build_question_shown_payload, gameplay_payloads.build_question_shown_payload)
self.assertIs(lobby_views._build_question_shown_response, gameplay_payloads.build_question_shown_response)
def test_start_round_view_source_stays_http_thin(self):
source = inspect.getsource(inspect.unwrap(lobby_views.start_round))
self.assertIn("lie_started_payload = _build_lie_started_payload(session, round_config, round_question)", source)
self.assertIn("_build_start_round_response(session, round_config, round_question)", source)
self.assertNotIn('"round_question": {', source)
self.assertIn("transition = _start_round(session, category_slug)", source)
self.assertNotIn("RoundConfig", source)
self.assertNotIn("RoundQuestion", source)
self.assertNotIn("build_start_round_response", source)
def test_show_question_view_source_stays_http_thin(self):
source = inspect.getsource(inspect.unwrap(lobby_views.show_question))
self.assertIn("_build_question_shown_payload(round_question, lie_deadline_iso, round_config.lie_seconds)", source)
self.assertIn("_build_question_shown_response(round_question, lie_deadline_iso, round_config.lie_seconds)", source)
self.assertNotIn('"round_question": {', source)
self.assertNotIn('"round_question_id": round_question.id', source)
self.assertIn("transition = _show_question(session)", source)
self.assertNotIn("RoundConfig", source)
self.assertNotIn("RoundQuestion", source)
self.assertNotIn("build_question_shown_response", source)
def test_start_next_round_view_source_stays_http_thin(self):
source = inspect.getsource(inspect.unwrap(lobby_views.start_next_round))
@@ -99,6 +99,81 @@ class LobbyGameplayExtractionTests(TestCase):
self.assertNotIn("build_finish_game_response", source)
self.assertNotIn("build_finish_game_phase_event", source)
@patch("lobby.views.sync_broadcast_phase_event")
@patch("lobby.views._start_round")
def test_start_round_view_delegates_transition_to_service(
self,
mock_start_round,
mock_sync_broadcast_phase_event,
):
lobby_session = GameSession.objects.create(host=self.host, code="LOBBY1", status=GameSession.Status.LOBBY)
transition = gameplay_services.RoundTransitionResult(
session=lobby_session,
round_config=self.round_config,
round_question=RoundQuestion.objects.create(
session=lobby_session,
round_number=1,
question=self.question,
correct_answer=self.question.correct_answer,
),
should_broadcast=True,
response_payload={"ok": True},
phase_event_name="phase.lie_started",
phase_event_payload={"round_question_id": 123},
)
mock_start_round.return_value = transition
response = self.client.post(
reverse("lobby:start_round", kwargs={"code": lobby_session.code}),
data=json.dumps({"category_slug": self.category.slug}),
content_type="application/json",
)
self.assertEqual(response.status_code, 201)
self.assertEqual(response.json(), {"ok": True})
mock_start_round.assert_called_once_with(lobby_session, self.category.slug)
mock_sync_broadcast_phase_event.assert_called_once_with(
lobby_session.code,
"phase.lie_started",
{"round_question_id": 123},
)
@patch("lobby.views.sync_broadcast_phase_event")
@patch("lobby.views._show_question")
def test_show_question_view_delegates_transition_to_service(
self,
mock_show_question,
mock_sync_broadcast_phase_event,
):
lie_session = GameSession.objects.create(host=self.host, code="LIE123", status=GameSession.Status.LIE)
transition = gameplay_services.RoundTransitionResult(
session=lie_session,
round_config=self.round_config,
round_question=RoundQuestion.objects.create(
session=lie_session,
round_number=1,
question=self.question,
correct_answer=self.question.correct_answer,
),
should_broadcast=True,
response_payload={"ok": True},
phase_event_name="phase.question_shown",
phase_event_payload={"round_question_id": 456},
)
mock_show_question.return_value = transition
response = self.client.post(reverse("lobby:show_question", kwargs={"code": lie_session.code}))
self.assertEqual(response.status_code, 201)
self.assertEqual(response.json(), {"ok": True})
mock_show_question.assert_called_once_with(lie_session)
mock_sync_broadcast_phase_event.assert_called_once_with(
lie_session.code,
"phase.question_shown",
{"round_question_id": 456},
)
@patch("lobby.views.sync_broadcast_phase_event")
@patch("lobby.views._start_next_round")
def test_start_next_round_view_delegates_transition_to_service(
@@ -512,7 +587,7 @@ class StartRoundTests(TestCase):
self.assertEqual(response.json()["locale"], "en")
self.assertEqual(response.json()["error"], "Only host can start round")
@patch("lobby.views._select_round_question", side_effect=ValueError("no_available_questions"))
@patch("fupogfakta.services.select_round_question", side_effect=ValueError("no_available_questions"))
def test_start_round_does_not_persist_round_config_when_question_selection_fails(self, _mock_select_round_question):
self.client.login(username="host", password="secret123")