Spaces:
Runtime error
Runtime error
| """미니게임 MVP — 밸런스 게임 (C2) 테스트.""" | |
| from __future__ import annotations | |
| from datetime import datetime, timezone | |
| import pytest | |
| from matching_engine.models import ConfidenceLevel, MatchSession | |
| from minigame.models import BalanceQuestion, GameResult | |
| from minigame.balance_game import create_balance_game, submit_answer, finish_game | |
| from minigame.confidence import update_confidence | |
| def _make_session(**overrides) -> MatchSession: | |
| defaults = { | |
| "id": "sess-001", | |
| "user_id": "user-a", | |
| "partner_id": "user-b", | |
| "confidence": ConfidenceLevel.FIRST_INTERACT, | |
| "created_at": datetime.now(timezone.utc), | |
| } | |
| defaults.update(overrides) | |
| return MatchSession(**defaults) | |
| # ── 모델 테스트 ────────────────────────────────────────── | |
| class TestBalanceQuestion: | |
| def test_has_two_choices(self): | |
| q = BalanceQuestion(round=1, prompt="산 vs 바다", choice_a="산", choice_b="바다") | |
| assert q.choice_a == "산" | |
| assert q.choice_b == "바다" | |
| # ── 밸런스 게임 로직 테스트 ────────────────────────────── | |
| class TestCreateBalanceGame: | |
| def test_returns_five_questions(self): | |
| questions = create_balance_game() | |
| assert len(questions) == 5 | |
| def test_rounds_are_sequential(self): | |
| questions = create_balance_game() | |
| for i, q in enumerate(questions, start=1): | |
| assert q.round == i | |
| class TestSubmitAnswer: | |
| def setup_method(self): | |
| self.questions = create_balance_game() | |
| def test_submit_valid_answer(self): | |
| result = submit_answer(self.questions, round_num=1, choice="a") | |
| assert result["round"] == 1 | |
| assert result["choice"] in ("a", "b") | |
| def test_submit_invalid_round(self): | |
| with pytest.raises(ValueError): | |
| submit_answer(self.questions, round_num=0, choice="a") | |
| with pytest.raises(ValueError): | |
| submit_answer(self.questions, round_num=6, choice="a") | |
| def test_submit_invalid_choice(self): | |
| with pytest.raises(ValueError): | |
| submit_answer(self.questions, round_num=1, choice="c") | |
| class TestFinishGame: | |
| def test_finish_returns_game_result(self): | |
| user_answers = [ | |
| {"round": 1, "choice": "a"}, | |
| {"round": 2, "choice": "b"}, | |
| {"round": 3, "choice": "a"}, | |
| {"round": 4, "choice": "a"}, | |
| {"round": 5, "choice": "b"}, | |
| ] | |
| partner_answers = [ | |
| {"round": 1, "choice": "a"}, | |
| {"round": 2, "choice": "a"}, | |
| {"round": 3, "choice": "a"}, | |
| {"round": 4, "choice": "b"}, | |
| {"round": 5, "choice": "b"}, | |
| ] | |
| result = finish_game(user_answers, partner_answers) | |
| assert isinstance(result, GameResult) | |
| assert result.total_rounds == 5 | |
| def test_same_choices_score_point(self): | |
| same = [{"round": i, "choice": "a"} for i in range(1, 6)] | |
| result = finish_game(same, same) | |
| assert result.match_count == 5 | |
| def test_different_choices_no_score(self): | |
| user = [{"round": i, "choice": "a"} for i in range(1, 6)] | |
| partner = [{"round": i, "choice": "b"} for i in range(1, 6)] | |
| result = finish_game(user, partner) | |
| assert result.match_count == 0 | |
| # ── confidence 반영 테스트 ─────────────────────────────── | |
| class TestUpdateConfidence: | |
| def test_confidence_increases(self): | |
| session = _make_session(confidence=ConfidenceLevel.FIRST_INTERACT) | |
| result = GameResult(total_rounds=5, match_count=3) | |
| new_level = update_confidence(session, result) | |
| assert new_level == ConfidenceLevel.POSITIVE | |
| def test_confidence_max_is_high_trust(self): | |
| """미니게임으로는 최대 HIGH_TRUST(3)까지만 상승.""" | |
| session = _make_session(confidence=ConfidenceLevel.HIGH_TRUST) | |
| result = GameResult(total_rounds=5, match_count=5) | |
| new_level = update_confidence(session, result) | |
| assert new_level == ConfidenceLevel.HIGH_TRUST | |
| def test_session_confidence_is_updated(self): | |
| session = _make_session(confidence=ConfidenceLevel.FIRST_INTERACT) | |
| result = GameResult(total_rounds=5, match_count=3) | |
| update_confidence(session, result) | |
| assert session.confidence == ConfidenceLevel.POSITIVE | |