diff --git a/database_schema.md b/database_schema.md index 486573e..d4b0eb5 100644 --- a/database_schema.md +++ b/database_schema.md @@ -283,7 +283,7 @@ | wrong_because | character varying | YES | | | edited_at | timestamp without time zone | YES | | -## sentence_hints_used +## sentence_hints_used ⚠️ устаревшая (не используется) | Колонка | Тип | NULL | Default | |---------|-----|------|---------| diff --git a/routers/sentences.py b/routers/sentences.py index 22b56bc..c172e62 100644 --- a/routers/sentences.py +++ b/routers/sentences.py @@ -7,39 +7,6 @@ from database import get_pool router = APIRouter(prefix="/sentences", tags=["sentences"]) -@router.get("/{sentence_id}/hint") -async def get_sentence_hint(sentence_id: int, request: Request): - """Получить правильное предложение в качестве подсказки (одноразово)""" - telegram_id, _ = await require_auth(request) - - pool = await get_pool() - async with pool.acquire() as conn: - row = await conn.fetchrow( - "SELECT sentence_pl FROM sentences WHERE id = $1", sentence_id - ) - if not row: - raise HTTPException(404, "Sentence not found") - - # 🔧 ФИКС (24.09.2026): sentence_hint_no_limit — лимит 1 раз на предложение. - already_used = await conn.fetchval( - "SELECT 1 FROM sentence_hints_used WHERE user_id = $1 AND sentence_id = $2", - telegram_id, sentence_id - ) - if already_used: - raise HTTPException(403, "Hint already used for this sentence") - - # Записываем использование - await conn.execute(""" - INSERT INTO sentence_hints_used (user_id, sentence_id) - VALUES ($1, $2) - ON CONFLICT (user_id, sentence_id) DO NOTHING - """, telegram_id, sentence_id) - - return { - "sentence_pl": row["sentence_pl"], - } - - def load_sentences_from_csv(filename): """Загружает предложения из CSV файла (формат: polish,english,russian,ukrainian).""" sentences = [] diff --git a/scripts/reset_user_progress.py b/scripts/reset_user_progress.py index 6b71259..5c0ac9a 100644 --- a/scripts/reset_user_progress.py +++ b/scripts/reset_user_progress.py @@ -53,7 +53,6 @@ USER_TABLES = [ "user_seen_bubble_levels", "user_word_stats", "xp_log", - "sentence_hints_used", "typing_hints_used", "chat_history", "chat_usage", diff --git a/tests/test_smoke.py b/tests/test_smoke.py index 03fef63..33881bd 100644 --- a/tests/test_smoke.py +++ b/tests/test_smoke.py @@ -1966,44 +1966,85 @@ async def test_sentences_stats(smoke_user): assert passed -async def test_sentences_hint(smoke_user, smoke_conn): - """GET /sentences/{id}/hint → sentence_pl, лимит 1 раз.""" +async def test_sentences_show_answer_medium(smoke_user, smoke_conn): + """POST /sentences/show-answer — Medium: лимит 2 (200, 200, 403).""" client = smoke_user - # Берём предложение, которое юзер ещё НЕ проходил и НЕ брал подсказку - sentence_id = await smoke_conn.fetchval(""" - SELECT id FROM sentences - WHERE id NOT IN ( - SELECT sentence_id FROM sentence_hints_used WHERE user_id = $1 - ) - ORDER BY id LIMIT 1 - """, SMOKE_USER_ID) + await smoke_conn.execute( + "UPDATE users SET difficulty = 'medium' WHERE telegram_id = $1", SMOKE_USER_ID + ) + sentence_id = await smoke_conn.fetchval("SELECT id FROM sentences ORDER BY id LIMIT 1") if not sentence_id: - pytest.skip("Нет предложений для hint-теста") + pytest.skip("Нет предложений для show-answer теста") return - # 1. Первый запрос — 200 - resp1 = await client.get(f"/sentences/{sentence_id}/hint") - ok1 = resp1.status_code == 200 - body1 = resp1.json() if ok1 else {} - has_sentence = bool(body1.get("sentence_pl")) - - # 2. Проверка в БД - recorded = await smoke_conn.fetchval( - "SELECT 1 FROM sentence_hints_used WHERE user_id = $1 AND sentence_id = $2", + # Чистим list_opened, чтобы начать с 0 + await smoke_conn.execute( + "DELETE FROM sentence_progress WHERE user_id = $1 AND sentence_id = $2", SMOKE_USER_ID, sentence_id, - ) is not None + ) - # 3. Второй запрос — 403 (лимит) - resp2 = await client.get(f"/sentences/{sentence_id}/hint") - limit_ok = resp2.status_code == 403 + resp1 = await client.post("/sentences/show-answer", json={"sentence_id": sentence_id}) + resp2 = await client.post("/sentences/show-answer", json={"sentence_id": sentence_id}) + resp3 = await client.post("/sentences/show-answer", json={"sentence_id": sentence_id}) - passed = ok1 and has_sentence and recorded and limit_ok + passed = resp1.status_code == 200 and resp2.status_code == 200 and resp3.status_code == 403 RESULTS.append(ScenarioResult( - "sentences_hint", "sentences", passed, - f"first={resp1.status_code}, recorded={recorded}, second={resp2.status_code} (ожидали 403)", - {"first_status": resp1.status_code, "recorded": recorded, "second_status": resp2.status_code}, + "sentences_show_answer_medium", "sentences", passed, + f"medium: 1={resp1.status_code}, 2={resp2.status_code}, 3={resp3.status_code} (ожидали 200/200/403)", + {"first": resp1.status_code, "second": resp2.status_code, "third": resp3.status_code}, + )) + assert passed + + +async def test_sentences_show_answer_hard(smoke_user, smoke_conn): + """POST /sentences/show-answer — Hard: сразу 403.""" + client = smoke_user + + await smoke_conn.execute( + "UPDATE users SET difficulty = 'hard' WHERE telegram_id = $1", SMOKE_USER_ID + ) + + sentence_id = await smoke_conn.fetchval("SELECT id FROM sentences ORDER BY id LIMIT 1") + if not sentence_id: + pytest.skip("Нет предложений для show-answer теста") + return + + resp = await client.post("/sentences/show-answer", json={"sentence_id": sentence_id}) + + passed = resp.status_code == 403 + RESULTS.append(ScenarioResult( + "sentences_show_answer_hard", "sentences", passed, + f"hard: status={resp.status_code} (ожидали 403)", + {"status": resp.status_code}, + )) + assert passed + + +async def test_sentences_show_answer_easy(smoke_user, smoke_conn): + """POST /sentences/show-answer — Easy: без лимита (несколько 200).""" + client = smoke_user + + await smoke_conn.execute( + "UPDATE users SET difficulty = 'easy' WHERE telegram_id = $1", SMOKE_USER_ID + ) + + sentence_id = await smoke_conn.fetchval("SELECT id FROM sentences ORDER BY id LIMIT 1") + if not sentence_id: + pytest.skip("Нет предложений для show-answer теста") + return + + statuses = [] + for _ in range(3): + r = await client.post("/sentences/show-answer", json={"sentence_id": sentence_id}) + statuses.append(r.status_code) + + passed = all(s == 200 for s in statuses) + RESULTS.append(ScenarioResult( + "sentences_show_answer_easy", "sentences", passed, + f"easy: statuses={statuses} (ожидали все 200)", + {"statuses": statuses}, )) assert passed