add gamification characters (Duolingo-style mascot catalog)

New progress.Character model: an admin-managed catalog (same pattern as
subscription.Plan) of selectable mascots, each unlocked once the user's
XP crosses unlock_xp. No separate ownership table yet — unlocking is a
pure function of UserStats.xp, kept intentionally minimal since this is
the first of an incremental rollout the senior asked for.

- GET /api/characters — catalog with per-user unlocked/selected flags
- POST /api/characters/select — picks an active character, rejecting
  ones the user hasn't unlocked yet
- UserStats.selected_character_id tracks the pick
- seed_test_data now seeds 4 sample characters (owl/fox/turtle/lion)
  for local/staging testing

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
2026-09-06 13:53:17 +03:30
parent b5476e1c5d
commit 37a1741e0e
9 changed files with 266 additions and 6 deletions

View File

@@ -7,7 +7,7 @@ from rest_framework.test import APIClient
from accounts.authentication import issue_access_token
from accounts.models import User
from .models import HEART_REFILL_INTERVAL, UserHearts
from .models import HEART_REFILL_INTERVAL, Character, UserHearts, UserStats
class UserHeartsRegenTests(TestCase):
@@ -60,3 +60,72 @@ class UserHeartsRegenTests(TestCase):
response = self._get_hearts()
self.assertEqual(response.data['currentHearts'], 5)
self.assertIsNone(response.data['nextRefillAt'])
class CharacterGamificationTests(TestCase):
"""کاتالوگِ کاراکترها (مثلِ ماسکات‌های Duolingo): باز/بسته‌بودن تابعی از
XP کاربره، و فقط کاراکترِ باز‌شده قابلِ انتخاب‌شدنه."""
def setUp(self):
self.client = APIClient()
self.user = User.objects.create(device_id='device-characters-1', is_guest=True, name='')
self.token = issue_access_token(self.user.id)
self.owl = Character.objects.create(slug='owl', name='جغد', icon='owl', unlock_xp=0, order=1)
self.fox = Character.objects.create(slug='fox', name='روباه', icon='fox', unlock_xp=100, order=2)
def _list(self):
return self.client.get('/api/characters', HTTP_AUTHORIZATION=f'Bearer {self.token}')
def _select(self, character_id):
return self.client.post(
'/api/characters/select', {'characterId': str(character_id)},
format='json', HTTP_AUTHORIZATION=f'Bearer {self.token}',
)
def test_list_reports_locked_and_unlocked_by_xp(self):
UserStats.objects.create(user_id=self.user.id, xp=50)
response = self._list()
self.assertEqual(response.status_code, 200)
by_slug = {c['slug']: c for c in response.data['characters']}
self.assertTrue(by_slug['owl']['unlocked'])
self.assertFalse(by_slug['fox']['unlocked'])
def test_list_with_no_stats_yet_only_zero_xp_characters_unlocked(self):
response = self._list()
self.assertEqual(response.status_code, 200)
by_slug = {c['slug']: c for c in response.data['characters']}
self.assertTrue(by_slug['owl']['unlocked'])
self.assertFalse(by_slug['fox']['unlocked'])
def test_can_select_unlocked_character(self):
UserStats.objects.create(user_id=self.user.id, xp=10)
response = self._select(self.owl.id)
self.assertEqual(response.status_code, 200)
self.assertTrue(response.data['selected'])
stats = UserStats.objects.get(user_id=self.user.id)
self.assertEqual(stats.selected_character_id, self.owl.id)
def test_cannot_select_locked_character(self):
UserStats.objects.create(user_id=self.user.id, xp=10)
response = self._select(self.fox.id)
self.assertEqual(response.status_code, 400)
stats = UserStats.objects.get(user_id=self.user.id)
self.assertIsNone(stats.selected_character_id)
def test_selection_reflected_in_list(self):
UserStats.objects.create(user_id=self.user.id, xp=10)
self._select(self.owl.id)
response = self._list()
by_slug = {c['slug']: c for c in response.data['characters']}
self.assertTrue(by_slug['owl']['selected'])
self.assertFalse(by_slug['fox']['selected'])