let a finished island quiz open the next island, and ground ask-AI in the student's book
The frontend plays node quizzes, but a node only completed once every one of its
8-13 lessons was completed separately, so students were stuck after the first
island of each unit. POST /api/nodes/{id}/quiz/complete now completes the node
(any score; score only sets stars/xp) and returns nextNodeId.
Also closes the other backend gaps found while auditing the frontend:
- /api/ai/ask without lessonId now picks the book from the user's grade instead
of skipping retrieval, so the "ask Pi" tab is grounded in the textbook.
- guest sessions return a refreshToken; they used to go silently 401 after 1h.
- lesson_complete throttle 10/hour -> 120/hour (a lesson here is 1-2 questions).
- progressPercent is per grade, not over all 2,617 lessons of every grade.
- a one-year plan (299,000 toman) is created by migration when no plan exists.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
86
ai_tutor/tests_grade_grounding.py
Normal file
86
ai_tutor/tests_grade_grounding.py
Normal file
@@ -0,0 +1,86 @@
|
||||
"""
|
||||
«از پی بپرس» بدونِ lessonId هم به کتاب مستند میشود.
|
||||
|
||||
آن صفحه از نوارِ پایینِ اپ باز میشود و درسی در کار نیست؛ قبلاً در این حالت
|
||||
retrieval کلاً رد میشد و همهی جوابها عمومی بودند. حالا پایهی خودِ کاربر
|
||||
کتاب را معلوم میکند.
|
||||
"""
|
||||
from unittest import mock
|
||||
|
||||
from django.core.cache import cache
|
||||
from django.test import TestCase
|
||||
|
||||
from accounts.authentication import issue_access_token
|
||||
from accounts.models import User
|
||||
from ai_tutor.models import Conversation
|
||||
from content.models import Grade
|
||||
|
||||
|
||||
class AskWithoutLessonUsesTheStudentsBookTests(TestCase):
|
||||
def setUp(self):
|
||||
cache.clear()
|
||||
|
||||
def _ask(self, user, **extra):
|
||||
return self.client.post(
|
||||
'/api/ai/ask', {'userQuestion': 'توان چیست؟', **extra},
|
||||
content_type='application/json',
|
||||
HTTP_AUTHORIZATION=f'Bearer {issue_access_token(user.id)}',
|
||||
)
|
||||
|
||||
def _patched(self):
|
||||
retrieve = mock.patch(
|
||||
'rag.services.retrieval.retrieve_context',
|
||||
return_value=([{'title': 'فصل', 'summary': 'متن'}], {'gradeTitle': 'نهم'}),
|
||||
)
|
||||
generate = mock.patch(
|
||||
'rag.services.generation.generate_answer', return_value=('جواب', [], {}),
|
||||
)
|
||||
configured = mock.patch('rag.services.config.rag_is_configured', return_value=True)
|
||||
return retrieve, generate, configured
|
||||
|
||||
def test_math_grade_picks_that_grades_book(self):
|
||||
grade = Grade.objects.create(title='پایه نهم', icon='', color='')
|
||||
user = User.objects.create(phone='09120000011', grade_id=grade.id)
|
||||
retrieve, generate, configured = self._patched()
|
||||
|
||||
with retrieve as retrieve_mock, generate, configured:
|
||||
response = self._ask(user)
|
||||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
kwargs = retrieve_mock.call_args.kwargs
|
||||
self.assertEqual(kwargs['subject'], 'ریاضی')
|
||||
self.assertEqual(kwargs['grade_title'], 'نهم')
|
||||
self.assertEqual(response.json()['usedChunks'], [{'title': 'فصل', 'summary': 'متن'}])
|
||||
|
||||
def test_calculus_grades_use_the_calculus_book(self):
|
||||
grade = Grade.objects.create(title='پایه دوازدهم', icon='', color='')
|
||||
user = User.objects.create(phone='09120000012', grade_id=grade.id)
|
||||
retrieve, generate, configured = self._patched()
|
||||
|
||||
with retrieve as retrieve_mock, generate, configured:
|
||||
self._ask(user)
|
||||
|
||||
self.assertEqual(retrieve_mock.call_args.kwargs['subject'], 'حسابان')
|
||||
|
||||
def test_the_follow_up_message_keeps_the_book(self):
|
||||
grade = Grade.objects.create(title='پایه نهم', icon='', color='')
|
||||
user = User.objects.create(phone='09120000013', grade_id=grade.id)
|
||||
retrieve, generate, configured = self._patched()
|
||||
|
||||
with retrieve as retrieve_mock, generate, configured:
|
||||
first = self._ask(user).json()
|
||||
self._ask(user, conversationId=first['conversationId'])
|
||||
|
||||
self.assertEqual(retrieve_mock.call_args.kwargs['subject'], 'ریاضی')
|
||||
conversation = Conversation.objects.get(pk=first['conversationId'])
|
||||
self.assertEqual(conversation.subject, 'ریاضی')
|
||||
|
||||
def test_a_user_without_a_grade_still_gets_an_answer(self):
|
||||
user = User.objects.create(phone='09120000014')
|
||||
retrieve, generate, configured = self._patched()
|
||||
|
||||
with retrieve as retrieve_mock, generate, configured:
|
||||
response = self._ask(user)
|
||||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(retrieve_mock.call_args.kwargs['subject'], '')
|
||||
Reference in New Issue
Block a user