🧪 Smoke-test: волны 2-5 — 25 сценариев
Покрытие: - pronunciation, hints, game (волна 1) - wordquiz, bubblewords, sentences, typing (волна 2) - users, achievements (волна 2) - learning: levels, topics, session/start, session/full_flow, progress (волна 3) - teacher: progress, phrase-of-day, start (волна 3) - leaderboard: top (волна 3) - assistant: limit, history (волна 4) - support: user-tickets (волна 4) - admin: dashboard forbidden (волна 5) - feed: news (волна 5) Все проверки через реальную БД. Отчёт группируется по секциям.
This commit is contained in:
+475
-189
@@ -17,19 +17,12 @@ test_user, которая чистит более короткий список
|
||||
|
||||
ФИКС (26.09.2026): testclient_asyncpg_conflict —
|
||||
TestClient (starlette) создаёт свой event loop для каждого запроса,
|
||||
а asyncpg-пул привязан к event loop, в котором создан. Второй запрос
|
||||
падал с InterfaceError: cannot perform operation: another operation
|
||||
is in progress. Решение: httpx.AsyncClient с ASGITransport — все
|
||||
запросы идут в том же event loop, что и pool, конфликта нет.
|
||||
а asyncpg-пул привязан к event loop, в котором создан. Решение:
|
||||
httpx.AsyncClient с ASGITransport.
|
||||
|
||||
РАСШИРЕНО (26.09.2026, вечер): волна 2 —
|
||||
+ wordquiz_check_flow (правильный ответ → total_score растёт)
|
||||
+ wordquiz_wrong_answer (неверный ответ → current_mistakes растёт)
|
||||
+ bubblewords_complete_flow (levels_completed и total_score растут)
|
||||
+ sentences_check_wrong (неверный ответ → score НЕ растёт)
|
||||
+ typing_progress_flow (check → typing_progress.completed_at записан)
|
||||
+ users_set_language (app_language меняется)
|
||||
+ achievements_track_login (daily_logins.login_date записан)
|
||||
РАСШИРЕНО (26.09.2026): волна 2 + волна 3 + волна 4 + волна 5 —
|
||||
25 сценариев: игры, learning, teacher, users, achievements,
|
||||
leaderboard, assistant, support, admin, feed.
|
||||
"""
|
||||
import hashlib
|
||||
import hmac
|
||||
@@ -58,8 +51,7 @@ def _build_init_data(user_id: int, first_name: str, bot_token: str) -> str:
|
||||
"""Официальный алгоритм Telegram WebApp initData.
|
||||
|
||||
quote(..., safe='') — НЕ urlencode() — потому что auth.py декодирует
|
||||
вручную через unquote(), которая не превращает '+' обратно в пробел
|
||||
(а urlencode() по умолчанию кодирует пробелы именно как '+').
|
||||
вручную через unquote(), которая не превращает '+' обратно в пробел.
|
||||
"""
|
||||
auth_date = int(time.time())
|
||||
user_json = json.dumps(
|
||||
@@ -88,22 +80,16 @@ class ScenarioResult:
|
||||
details: dict[str, Any] = field(default_factory=dict)
|
||||
|
||||
|
||||
RESULTS: list[ScenarioResult] = [] # копится по ходу прогона, пишется в отчёт в конце
|
||||
RESULTS: list[ScenarioResult] = []
|
||||
|
||||
|
||||
# ============ Очистка (полный список из database.py) ============
|
||||
# ============ Очистка ============
|
||||
|
||||
# Таблицы БЕЗ FK на users(telegram_id) — каскад их не подчистит.
|
||||
NON_FK_TABLES = [
|
||||
"learning_sessions", "user_learning_answers", "user_lesson_progress",
|
||||
"user_topic_progress", "user_skill_mastery", "ai_user_context",
|
||||
"teacher_chat_history", "teacher_ai_usage",
|
||||
]
|
||||
# Каскадные через users(telegram_id) ON DELETE CASCADE — отдельно чистить
|
||||
# не нужно, снесутся при DELETE FROM users: user_progress, sentence_progress,
|
||||
# typing_progress, chat_history, chat_usage, user_achievements, daily_logins,
|
||||
# word_quiz_progress, bubble_words_progress, sentence_hints_used,
|
||||
# typing_hints_used, voice_usage.
|
||||
|
||||
|
||||
async def _cleanup(conn) -> None:
|
||||
@@ -111,7 +97,6 @@ async def _cleanup(conn) -> None:
|
||||
for table in NON_FK_TABLES:
|
||||
await conn.execute(f"DELETE FROM {table} WHERE user_id = $1", SMOKE_USER_ID)
|
||||
|
||||
# support_messages ссылается на ticket_id, не напрямую на user_id.
|
||||
await conn.execute(
|
||||
"DELETE FROM support_messages WHERE ticket_id IN "
|
||||
"(SELECT id FROM support_tickets WHERE user_id = $1)",
|
||||
@@ -119,7 +104,6 @@ async def _cleanup(conn) -> None:
|
||||
)
|
||||
await conn.execute("DELETE FROM support_tickets WHERE user_id = $1", SMOKE_USER_ID)
|
||||
|
||||
# users — последним, каскад подчищает всё перечисленное выше.
|
||||
await conn.execute("DELETE FROM users WHERE telegram_id = $1", SMOKE_USER_ID)
|
||||
|
||||
|
||||
@@ -127,7 +111,6 @@ async def _cleanup(conn) -> None:
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
async def smoke_conn():
|
||||
"""Отдельное соединение из общего пула приложения (database.get_pool())."""
|
||||
pool = await get_pool()
|
||||
async with pool.acquire() as connection:
|
||||
yield connection
|
||||
@@ -135,13 +118,7 @@ async def smoke_conn():
|
||||
|
||||
@pytest.fixture(scope="session", autouse=True)
|
||||
async def smoke_user(smoke_conn):
|
||||
"""Цикл на всю сессию: удалить → зарегистрировать → (тесты) → удалить → отчёт.
|
||||
|
||||
ФИКС (26.09.2026): testclient_asyncpg_conflict — вместо TestClient
|
||||
используется httpx.AsyncClient с ASGITransport. Все запросы идут
|
||||
в том же event loop, что и asyncpg-пул (smoke_conn), поэтому
|
||||
соединение не «зависает» между запросами.
|
||||
"""
|
||||
"""Цикл на всю сессию: удалить → зарегистрировать → (тесты) → удалить → отчёт."""
|
||||
await _cleanup(smoke_conn)
|
||||
|
||||
init_data = _build_init_data(SMOKE_USER_ID, SMOKE_USER_FIRST_NAME, BOT_TOKEN)
|
||||
@@ -163,14 +140,11 @@ async def smoke_user(smoke_conn):
|
||||
|
||||
|
||||
# ============================================================
|
||||
# ВОЛНА 1 — игры (базовые, уже работают)
|
||||
# ВОЛНА 1 — игры (базовые)
|
||||
# ============================================================
|
||||
|
||||
async def test_pronunciation_pool_stress(smoke_user):
|
||||
"""В routers/pronunciation.py нет persistence-вызова — только get_next_word.
|
||||
Проверять запись в БД не на чем. Вместо этого — утечку conn/пула:
|
||||
если conn не освобождается, часть из N подряд идущих запросов упадёт.
|
||||
"""
|
||||
"""30 запросов подряд — проверка утечки пула."""
|
||||
client = smoke_user
|
||||
N = 30
|
||||
seen_words: set[str] = set()
|
||||
@@ -189,18 +163,16 @@ async def test_pronunciation_pool_stress(smoke_user):
|
||||
passed = failures == 0
|
||||
RESULTS.append(ScenarioResult(
|
||||
"pronunciation_pool_stress", "pronunciation", passed,
|
||||
f"{N - failures}/{N} запросов успешны, {len(seen_words)} уникальных слов",
|
||||
f"{N - failures}/{N} запросов, {len(seen_words)} уникальных слов",
|
||||
{"failures": failures, "unique_words": len(seen_words)},
|
||||
))
|
||||
assert passed, f"{failures}/{N} запросов к /pronunciation/next упали — похоже на утечку conn/пула"
|
||||
assert passed, f"{failures}/{N} запросов упали"
|
||||
|
||||
|
||||
async def test_hints_first_letter(smoke_user, smoke_conn):
|
||||
"""GET /hints/first-letter/{levelId} — читаем hints_used в user_progress
|
||||
до и после. 200 без реального инкремента = баг.
|
||||
"""
|
||||
"""hints_used растёт в user_progress."""
|
||||
client = smoke_user
|
||||
level_id = 1 # подтверждено: level_id=1 существует
|
||||
level_id = 1
|
||||
|
||||
before = await smoke_conn.fetchrow(
|
||||
"SELECT hints_used FROM user_progress WHERE user_id = $1 AND level_id = $2",
|
||||
@@ -220,28 +192,23 @@ async def test_hints_first_letter(smoke_user, smoke_conn):
|
||||
passed = ok and after_count > before_count
|
||||
RESULTS.append(ScenarioResult(
|
||||
"hints_db_increment", "hints", passed,
|
||||
f"GET вернул {resp.status_code}, hints_used {before_count} → {after_count}",
|
||||
f"GET={resp.status_code}, hints_used {before_count}→{after_count}",
|
||||
{"before": before_count, "after": after_count, "status": resp.status_code},
|
||||
))
|
||||
assert ok, f"GET /hints/first-letter/{level_id} вернул {resp.status_code}"
|
||||
assert after_count > before_count, f"hints_used не вырос: {before_count} → {after_count}"
|
||||
assert passed
|
||||
|
||||
|
||||
async def test_game_complete_level_flow(smoke_user, smoke_conn):
|
||||
"""GET /game/level/{n} → POST /game/complete-level (дважды).
|
||||
Проверяем: (1) повторный вызов даёт already_completed=True, а не
|
||||
повторное начисление; (2) score из ответа API реально совпадает
|
||||
со score в user_progress.
|
||||
"""
|
||||
"""Идемпотентность + сверка score с БД."""
|
||||
client = smoke_user
|
||||
level_number = 1
|
||||
|
||||
resp_level = await client.get(f"/game/level/{level_number}")
|
||||
assert resp_level.status_code == 200, f"GET /game/level/{level_number} вернул {resp_level.status_code}"
|
||||
assert resp_level.status_code == 200
|
||||
level_id = resp_level.json().get("level", {}).get("id")
|
||||
|
||||
resp1 = await client.post("/game/complete-level", json={"level_id": level_id, "hints_used": 0})
|
||||
assert resp1.status_code == 200, f"POST /game/complete-level (1-й раз) вернул {resp1.status_code}"
|
||||
assert resp1.status_code == 200
|
||||
body1 = resp1.json()
|
||||
|
||||
resp2 = await client.post("/game/complete-level", json={"level_id": level_id, "hints_used": 0})
|
||||
@@ -258,11 +225,10 @@ async def test_game_complete_level_flow(smoke_user, smoke_conn):
|
||||
passed = idempotent and score_matches
|
||||
RESULTS.append(ScenarioResult(
|
||||
"game_complete_level_full_flow", "game", passed,
|
||||
f"idempotent={idempotent}, api_score={body1.get('score')}, db_score={db_score}",
|
||||
f"idempotent={idempotent}, api={body1.get('score')}, db={db_score}",
|
||||
{"api_score": body1.get("score"), "db_score": db_score, "idempotent": idempotent},
|
||||
))
|
||||
assert idempotent, f"Повторный complete-level не вернул already_completed=True: {body2}"
|
||||
assert score_matches, f"API score={body1.get('score')}, но в БД score={db_score}"
|
||||
assert passed
|
||||
|
||||
|
||||
# ============================================================
|
||||
@@ -270,17 +236,10 @@ async def test_game_complete_level_flow(smoke_user, smoke_conn):
|
||||
# ============================================================
|
||||
|
||||
async def test_wordquiz_correct_answer(smoke_user, smoke_conn):
|
||||
"""POST /wordquiz/check с правильным ответом → total_score растёт.
|
||||
|
||||
ВАЖНО: wordquiz.check доверяет клиенту (correct_polish приходит в body).
|
||||
Это by design (см. AI-находку wordquiz_check_trusts_client_correct_polish).
|
||||
Здесь проверяем только ФАКТ начисления очков в БД.
|
||||
"""
|
||||
client = smoke_user
|
||||
|
||||
before = await smoke_conn.fetchval(
|
||||
"SELECT total_score FROM word_quiz_progress WHERE user_id = $1",
|
||||
SMOKE_USER_ID,
|
||||
"SELECT total_score FROM word_quiz_progress WHERE user_id = $1", SMOKE_USER_ID
|
||||
) or 0
|
||||
|
||||
resp = await client.post(
|
||||
@@ -291,8 +250,7 @@ async def test_wordquiz_correct_answer(smoke_user, smoke_conn):
|
||||
body = resp.json() if ok else {}
|
||||
|
||||
after = await smoke_conn.fetchval(
|
||||
"SELECT total_score FROM word_quiz_progress WHERE user_id = $1",
|
||||
SMOKE_USER_ID,
|
||||
"SELECT total_score FROM word_quiz_progress WHERE user_id = $1", SMOKE_USER_ID
|
||||
) or 0
|
||||
|
||||
correct_flag = body.get("correct") is True
|
||||
@@ -301,21 +259,17 @@ async def test_wordquiz_correct_answer(smoke_user, smoke_conn):
|
||||
|
||||
RESULTS.append(ScenarioResult(
|
||||
"wordquiz_correct_answer", "wordquiz", passed,
|
||||
f"status={resp.status_code}, correct={correct_flag}, total_score {before} → {after}",
|
||||
{"before": before, "after": after, "correct": correct_flag, "status": resp.status_code},
|
||||
f"status={resp.status_code}, correct={correct_flag}, score {before}→{after}",
|
||||
{"before": before, "after": after, "correct": correct_flag},
|
||||
))
|
||||
assert ok, f"POST /wordquiz/check вернул {resp.status_code}"
|
||||
assert correct_flag, f"API вернул correct=False на правильный ответ: {body}"
|
||||
assert score_grew, f"total_score не вырос: {before} → {after}"
|
||||
assert passed
|
||||
|
||||
|
||||
async def test_wordquiz_wrong_answer(smoke_user, smoke_conn):
|
||||
"""POST /wordquiz/check с неверным ответом → current_mistakes растёт."""
|
||||
client = smoke_user
|
||||
|
||||
before = await smoke_conn.fetchval(
|
||||
"SELECT current_mistakes FROM word_quiz_progress WHERE user_id = $1",
|
||||
SMOKE_USER_ID,
|
||||
"SELECT current_mistakes FROM word_quiz_progress WHERE user_id = $1", SMOKE_USER_ID
|
||||
) or 0
|
||||
|
||||
resp = await client.post(
|
||||
@@ -325,99 +279,66 @@ async def test_wordquiz_wrong_answer(smoke_user, smoke_conn):
|
||||
ok = resp.status_code == 200
|
||||
|
||||
after = await smoke_conn.fetchval(
|
||||
"SELECT current_mistakes FROM word_quiz_progress WHERE user_id = $1",
|
||||
SMOKE_USER_ID,
|
||||
"SELECT current_mistakes FROM word_quiz_progress WHERE user_id = $1", SMOKE_USER_ID
|
||||
) or 0
|
||||
|
||||
mistakes_grew = after > before
|
||||
passed = ok and mistakes_grew
|
||||
|
||||
passed = ok and after > before
|
||||
RESULTS.append(ScenarioResult(
|
||||
"wordquiz_wrong_answer", "wordquiz", passed,
|
||||
f"status={resp.status_code}, current_mistakes {before} → {after}",
|
||||
{"before": before, "after": after, "status": resp.status_code},
|
||||
f"status={resp.status_code}, mistakes {before}→{after}",
|
||||
{"before": before, "after": after},
|
||||
))
|
||||
assert ok, f"POST /wordquiz/check вернул {resp.status_code}"
|
||||
assert mistakes_grew, f"current_mistakes не вырос: {before} → {after}"
|
||||
assert passed
|
||||
|
||||
|
||||
async def test_bubblewords_complete_flow(smoke_user, smoke_conn):
|
||||
"""POST /bubblewords/complete → levels_completed и total_score растут.
|
||||
|
||||
ВАЖНО: complete_bubble_level НЕ принимает hints_used от клиента —
|
||||
читает current_hints_used из БД (см. AI-фикс
|
||||
client_controlled_hints_bubblewords).
|
||||
"""
|
||||
client = smoke_user
|
||||
|
||||
before = await smoke_conn.fetchrow(
|
||||
"SELECT levels_completed, total_score FROM bubble_words_progress WHERE user_id = $1",
|
||||
SMOKE_USER_ID,
|
||||
)
|
||||
before_levels = before["levels_completed"] if before else 0
|
||||
before_score = before["total_score"] if before else 0
|
||||
bl = before["levels_completed"] if before else 0
|
||||
bs = before["total_score"] if before else 0
|
||||
|
||||
resp = await client.post(
|
||||
"/bubblewords/complete",
|
||||
json={"words_count": 3, "completion_time": 30},
|
||||
)
|
||||
ok = resp.status_code == 200
|
||||
body = resp.json() if ok else {}
|
||||
|
||||
after = await smoke_conn.fetchrow(
|
||||
"SELECT levels_completed, total_score FROM bubble_words_progress WHERE user_id = $1",
|
||||
SMOKE_USER_ID,
|
||||
)
|
||||
after_levels = after["levels_completed"] if after else 0
|
||||
after_score = after["total_score"] if after else 0
|
||||
|
||||
levels_grew = after_levels > before_levels
|
||||
score_grew = after_score > before_score
|
||||
passed = ok and levels_grew and score_grew
|
||||
al = after["levels_completed"] if after else 0
|
||||
as_ = after["total_score"] if after else 0
|
||||
|
||||
passed = ok and al > bl and as_ > bs
|
||||
RESULTS.append(ScenarioResult(
|
||||
"bubblewords_complete_flow", "bubblewords", passed,
|
||||
f"status={resp.status_code}, levels {before_levels}→{after_levels}, score {before_score}→{after_score}",
|
||||
{
|
||||
"before_levels": before_levels, "after_levels": after_levels,
|
||||
"before_score": before_score, "after_score": after_score,
|
||||
"status": resp.status_code,
|
||||
},
|
||||
f"status={resp.status_code}, levels {bl}→{al}, score {bs}→{as_}",
|
||||
{"before_levels": bl, "after_levels": al, "before_score": bs, "after_score": as_},
|
||||
))
|
||||
assert ok, f"POST /bubblewords/complete вернул {resp.status_code}"
|
||||
assert levels_grew, f"levels_completed не вырос: {before_levels} → {after_levels}"
|
||||
assert score_grew, f"total_score не вырос: {before_score} → {after_score}"
|
||||
assert passed
|
||||
|
||||
|
||||
async def test_sentences_check_wrong(smoke_user, smoke_conn):
|
||||
"""POST /sentences/check с неверными словами → score НЕ растёт.
|
||||
|
||||
Сложность: правильный порядок слов угадать нельзя. Проверяем
|
||||
негативный сценарий: неверный ответ не должен начислять очки.
|
||||
"""
|
||||
client = smoke_user
|
||||
|
||||
# Берём первое предложение из базы
|
||||
sentence_row = await smoke_conn.fetchrow(
|
||||
"SELECT id, sentence_pl FROM sentences ORDER BY id LIMIT 1"
|
||||
"SELECT id FROM sentences ORDER BY id LIMIT 1"
|
||||
)
|
||||
if not sentence_row:
|
||||
RESULTS.append(ScenarioResult(
|
||||
"sentences_check_wrong", "sentences", False,
|
||||
"В таблице sentences нет ни одного предложения",
|
||||
{},
|
||||
))
|
||||
pytest.skip("Нет предложений для теста")
|
||||
return
|
||||
|
||||
sentence_id = sentence_row["id"]
|
||||
|
||||
before = await smoke_conn.fetchval(
|
||||
"SELECT score FROM sentence_progress WHERE user_id = $1 AND sentence_id = $2",
|
||||
SMOKE_USER_ID, sentence_id,
|
||||
) or 0
|
||||
|
||||
# Отправляем заведомо неверный набор слов
|
||||
resp = await client.post(
|
||||
"/sentences/check",
|
||||
json={
|
||||
@@ -433,73 +354,44 @@ async def test_sentences_check_wrong(smoke_user, smoke_conn):
|
||||
SMOKE_USER_ID, sentence_id,
|
||||
) or 0
|
||||
|
||||
score_unchanged = after == before
|
||||
passed = ok and score_unchanged
|
||||
|
||||
passed = ok and after == before
|
||||
RESULTS.append(ScenarioResult(
|
||||
"sentences_check_wrong", "sentences", passed,
|
||||
f"status={resp.status_code}, score {before} → {after} (неверный ответ)",
|
||||
{"before": before, "after": after, "status": resp.status_code},
|
||||
f"status={resp.status_code}, score {before}→{after} (неверный)",
|
||||
{"before": before, "after": after},
|
||||
))
|
||||
assert ok, f"POST /sentences/check вернул {resp.status_code}"
|
||||
assert score_unchanged, f"Очки начислены за неверный ответ: {before} → {after}"
|
||||
assert passed
|
||||
|
||||
|
||||
async def test_typing_progress_flow(smoke_user, smoke_conn):
|
||||
"""GET /typing/next → POST /typing/check → typing_progress.completed_at записан.
|
||||
|
||||
ВАЖНО: GET /typing/next возвращает sentence_id + translation, но
|
||||
НЕ sentence_pl. Правильный текст берём из БД напрямую.
|
||||
"""
|
||||
client = smoke_user
|
||||
|
||||
resp_next = await client.get("/typing/next")
|
||||
if resp_next.status_code != 200:
|
||||
RESULTS.append(ScenarioResult(
|
||||
"typing_progress_flow", "typing", False,
|
||||
f"GET /typing/next вернул {resp_next.status_code}",
|
||||
{"status": resp_next.status_code},
|
||||
))
|
||||
pytest.skip(f"GET /typing/next вернул {resp_next.status_code}")
|
||||
return
|
||||
|
||||
body = resp_next.json()
|
||||
|
||||
# Если предложения закончились — не FAIL, а SKIP
|
||||
if body.get("completed") is True:
|
||||
RESULTS.append(ScenarioResult(
|
||||
"typing_progress_flow", "typing", True,
|
||||
"Все предложения уровня пройдены (completed=True)",
|
||||
{"completed": True},
|
||||
"Все предложения пройдены", {"completed": True},
|
||||
))
|
||||
pytest.skip("Все предложения уровня пройдены")
|
||||
pytest.skip("Все предложения пройдены")
|
||||
return
|
||||
|
||||
sentence_id = body.get("sentence_id")
|
||||
if not sentence_id:
|
||||
RESULTS.append(ScenarioResult(
|
||||
"typing_progress_flow", "typing", False,
|
||||
f"GET /typing/next не вернул sentence_id: {body}",
|
||||
{"body": body},
|
||||
))
|
||||
pytest.skip("Нет sentence_id")
|
||||
return
|
||||
|
||||
# 🔧 Берём sentence_pl напрямую из базы — API его не возвращает
|
||||
sentence_pl = await smoke_conn.fetchval(
|
||||
"SELECT sentence_pl FROM sentences WHERE id = $1",
|
||||
sentence_id,
|
||||
"SELECT sentence_pl FROM sentences WHERE id = $1", sentence_id,
|
||||
)
|
||||
if not sentence_pl:
|
||||
RESULTS.append(ScenarioResult(
|
||||
"typing_progress_flow", "typing", False,
|
||||
f"В БД нет sentence_pl для sentence_id={sentence_id}",
|
||||
{"sentence_id": sentence_id},
|
||||
))
|
||||
pytest.skip("Нет sentence_pl в БД")
|
||||
return
|
||||
|
||||
# Отправляем правильный текст
|
||||
resp_check = await client.post(
|
||||
"/typing/check",
|
||||
json={"sentence_id": sentence_id, "text": sentence_pl},
|
||||
@@ -515,11 +407,10 @@ async def test_typing_progress_flow(smoke_user, smoke_conn):
|
||||
|
||||
RESULTS.append(ScenarioResult(
|
||||
"typing_progress_flow", "typing", passed,
|
||||
f"status={resp_check.status_code}, sentence_id={sentence_id}, typing_progress записан={recorded}",
|
||||
{"status": resp_check.status_code, "recorded": recorded, "sentence_id": sentence_id},
|
||||
f"status={resp_check.status_code}, sentence_id={sentence_id}, recorded={recorded}",
|
||||
{"status": resp_check.status_code, "recorded": recorded},
|
||||
))
|
||||
assert ok, f"POST /typing/check вернул {resp_check.status_code}"
|
||||
assert recorded, "typing_progress.completed_at не записан в БД"
|
||||
assert passed
|
||||
|
||||
|
||||
# ============================================================
|
||||
@@ -527,64 +418,449 @@ async def test_typing_progress_flow(smoke_user, smoke_conn):
|
||||
# ============================================================
|
||||
|
||||
async def test_users_set_language(smoke_user, smoke_conn):
|
||||
"""POST /users/set-language → users.app_language меняется."""
|
||||
client = smoke_user
|
||||
|
||||
before = await smoke_conn.fetchval(
|
||||
"SELECT app_language FROM users WHERE telegram_id = $1",
|
||||
SMOKE_USER_ID,
|
||||
"SELECT app_language FROM users WHERE telegram_id = $1", SMOKE_USER_ID,
|
||||
)
|
||||
|
||||
# Меняем на 'ru' (не 'pl', чтобы гарантированно отличалось)
|
||||
new_lang = "ru" if before != "ru" else "en"
|
||||
|
||||
resp = await client.post("/users/set-language", json={"language": new_lang})
|
||||
ok = resp.status_code == 200
|
||||
|
||||
after = await smoke_conn.fetchval(
|
||||
"SELECT app_language FROM users WHERE telegram_id = $1",
|
||||
SMOKE_USER_ID,
|
||||
"SELECT app_language FROM users WHERE telegram_id = $1", SMOKE_USER_ID,
|
||||
)
|
||||
|
||||
lang_changed = after == new_lang
|
||||
passed = ok and lang_changed
|
||||
|
||||
passed = ok and after == new_lang
|
||||
RESULTS.append(ScenarioResult(
|
||||
"users_set_language", "users", passed,
|
||||
f"status={resp.status_code}, app_language {before} → {after} (ожидали {new_lang})",
|
||||
{"before": before, "after": after, "expected": new_lang, "status": resp.status_code},
|
||||
f"status={resp.status_code}, lang {before}→{after}",
|
||||
{"before": before, "after": after, "expected": new_lang},
|
||||
))
|
||||
assert ok, f"POST /users/set-language вернул {resp.status_code}"
|
||||
assert lang_changed, f"app_language не изменился: {before} → {after}"
|
||||
assert passed
|
||||
|
||||
|
||||
async def test_achievements_track_login(smoke_user, smoke_conn):
|
||||
"""POST /achievements/track-login → daily_logins.login_date записан."""
|
||||
client = smoke_user
|
||||
|
||||
before_count = await smoke_conn.fetchval(
|
||||
"SELECT COUNT(*) FROM daily_logins WHERE user_id = $1",
|
||||
SMOKE_USER_ID,
|
||||
before = await smoke_conn.fetchval(
|
||||
"SELECT COUNT(*) FROM daily_logins WHERE user_id = $1", SMOKE_USER_ID,
|
||||
) or 0
|
||||
|
||||
resp = await client.post("/achievements/track-login")
|
||||
ok = resp.status_code == 200
|
||||
|
||||
after_count = await smoke_conn.fetchval(
|
||||
"SELECT COUNT(*) FROM daily_logins WHERE user_id = $1",
|
||||
SMOKE_USER_ID,
|
||||
after = await smoke_conn.fetchval(
|
||||
"SELECT COUNT(*) FROM daily_logins WHERE user_id = $1", SMOKE_USER_ID,
|
||||
) or 0
|
||||
|
||||
logins_grew = after_count > before_count
|
||||
passed = ok and logins_grew
|
||||
|
||||
passed = ok and after > before
|
||||
RESULTS.append(ScenarioResult(
|
||||
"achievements_track_login", "achievements", passed,
|
||||
f"status={resp.status_code}, daily_logins {before_count} → {after_count}",
|
||||
{"before": before_count, "after": after_count, "status": resp.status_code},
|
||||
f"status={resp.status_code}, logins {before}→{after}",
|
||||
{"before": before, "after": after},
|
||||
))
|
||||
assert ok, f"POST /achievements/track-login вернул {resp.status_code}"
|
||||
assert logins_grew, f"daily_logins не вырос: {before_count} → {after_count}"
|
||||
assert passed
|
||||
|
||||
|
||||
# ============================================================
|
||||
# ВОЛНА 3 — learning
|
||||
# ============================================================
|
||||
|
||||
async def test_learning_levels_list(smoke_user):
|
||||
"""GET /learning/levels → список уровней."""
|
||||
client = smoke_user
|
||||
resp = await client.get("/learning/levels")
|
||||
ok = resp.status_code == 200
|
||||
body = resp.json() if ok else {}
|
||||
levels = body.get("levels", [])
|
||||
|
||||
passed = ok and len(levels) > 0
|
||||
RESULTS.append(ScenarioResult(
|
||||
"learning_levels_list", "learning", passed,
|
||||
f"status={resp.status_code}, уровней={len(levels)}",
|
||||
{"status": resp.status_code, "count": len(levels)},
|
||||
))
|
||||
assert passed
|
||||
|
||||
|
||||
async def test_learning_topics_list(smoke_user):
|
||||
"""GET /learning/topics?level=A1 → список тем."""
|
||||
client = smoke_user
|
||||
resp = await client.get("/learning/topics?level=A1")
|
||||
ok = resp.status_code == 200
|
||||
body = resp.json() if ok else {}
|
||||
topics = body.get("topics", [])
|
||||
|
||||
passed = ok and len(topics) > 0
|
||||
RESULTS.append(ScenarioResult(
|
||||
"learning_topics_list", "learning", passed,
|
||||
f"status={resp.status_code}, тем A1={len(topics)}",
|
||||
{"status": resp.status_code, "count": len(topics)},
|
||||
))
|
||||
assert passed
|
||||
|
||||
|
||||
async def test_learning_topic_detail(smoke_user, smoke_conn):
|
||||
"""GET /learning/topics/{topic_id} → тема + уроки."""
|
||||
client = smoke_user
|
||||
|
||||
topic_row = await smoke_conn.fetchrow(
|
||||
"SELECT topic_id FROM learning_topics WHERE level_code = 'A1' AND is_active = TRUE ORDER BY order_number LIMIT 1"
|
||||
)
|
||||
if not topic_row:
|
||||
pytest.skip("Нет тем A1")
|
||||
return
|
||||
|
||||
topic_id = topic_row["topic_id"]
|
||||
resp = await client.get(f"/learning/topics/{topic_id}")
|
||||
ok = resp.status_code == 200
|
||||
body = resp.json() if ok else {}
|
||||
lessons = body.get("lessons", [])
|
||||
|
||||
passed = ok and "topic" in body
|
||||
RESULTS.append(ScenarioResult(
|
||||
"learning_topic_detail", "learning", passed,
|
||||
f"status={resp.status_code}, topic_id={topic_id}, уроков={len(lessons)}",
|
||||
{"status": resp.status_code, "lessons": len(lessons)},
|
||||
))
|
||||
assert passed
|
||||
|
||||
|
||||
async def test_learning_session_start(smoke_user, smoke_conn):
|
||||
"""POST /learning/session/start → session_id в БД."""
|
||||
client = smoke_user
|
||||
|
||||
topic_row = await smoke_conn.fetchrow(
|
||||
"SELECT topic_id FROM learning_topics WHERE level_code = 'A1' AND is_active = TRUE ORDER BY order_number LIMIT 1"
|
||||
)
|
||||
if not topic_row:
|
||||
pytest.skip("Нет тем A1")
|
||||
return
|
||||
|
||||
topic_id = topic_row["topic_id"]
|
||||
|
||||
resp = await client.post(
|
||||
"/learning/session/start",
|
||||
json={"level_code": "A1", "topic_id": topic_id, "session_type": "lesson"},
|
||||
)
|
||||
ok = resp.status_code == 200
|
||||
body = resp.json() if ok else {}
|
||||
session_id = body.get("session_id")
|
||||
|
||||
recorded = False
|
||||
if session_id:
|
||||
row = await smoke_conn.fetchrow(
|
||||
"SELECT id FROM learning_sessions WHERE id = $1 AND user_id = $2",
|
||||
session_id, SMOKE_USER_ID,
|
||||
)
|
||||
recorded = row is not None
|
||||
|
||||
passed = ok and recorded
|
||||
RESULTS.append(ScenarioResult(
|
||||
"learning_session_start", "learning", passed,
|
||||
f"status={resp.status_code}, session_id={session_id}, в БД={recorded}",
|
||||
{"status": resp.status_code, "session_id": session_id, "recorded": recorded},
|
||||
))
|
||||
assert passed
|
||||
|
||||
|
||||
async def test_learning_session_full_flow(smoke_user, smoke_conn):
|
||||
"""Полный цикл: start → answer → complete."""
|
||||
client = smoke_user
|
||||
|
||||
topic_row = await smoke_conn.fetchrow(
|
||||
"SELECT topic_id FROM learning_topics WHERE level_code = 'A1' AND is_active = TRUE ORDER BY order_number LIMIT 1"
|
||||
)
|
||||
if not topic_row:
|
||||
pytest.skip("Нет тем A1")
|
||||
return
|
||||
|
||||
topic_id = topic_row["topic_id"]
|
||||
|
||||
# 1. Start
|
||||
resp_start = await client.post(
|
||||
"/learning/session/start",
|
||||
json={"level_code": "A1", "topic_id": topic_id, "session_type": "lesson"},
|
||||
)
|
||||
assert resp_start.status_code == 200, f"start вернул {resp_start.status_code}"
|
||||
session_id = resp_start.json()["session_id"]
|
||||
|
||||
# 2. Берём первый вопрос урока
|
||||
question_row = await smoke_conn.fetchrow(
|
||||
"""
|
||||
SELECT q.question_id, q.correct_answer
|
||||
FROM learning_questions q
|
||||
JOIN learning_lessons l ON q.lesson_id = l.lesson_id
|
||||
WHERE l.topic_id = $1 AND q.is_test_question = FALSE
|
||||
ORDER BY q.id LIMIT 1
|
||||
""",
|
||||
topic_id,
|
||||
)
|
||||
if not question_row:
|
||||
pytest.skip("Нет вопросов для темы")
|
||||
return
|
||||
|
||||
question_id = question_row["question_id"]
|
||||
correct = question_row["correct_answer"] or ""
|
||||
|
||||
# 3. Answer
|
||||
resp_ans = await client.post(
|
||||
"/learning/answer",
|
||||
json={
|
||||
"session_id": session_id,
|
||||
"question_id": question_id,
|
||||
"answer": correct,
|
||||
"time_taken_ms": 3000,
|
||||
"is_final_answer": True,
|
||||
"hint_used": False,
|
||||
},
|
||||
)
|
||||
ans_ok = resp_ans.status_code == 200
|
||||
ans_body = resp_ans.json() if ans_ok else {}
|
||||
|
||||
# Проверяем, что ответ записан
|
||||
answer_row = await smoke_conn.fetchrow(
|
||||
"SELECT id, correct FROM user_learning_answers WHERE session_id = $1 AND question_id = $2",
|
||||
session_id, question_id,
|
||||
)
|
||||
answer_recorded = answer_row is not None
|
||||
|
||||
# 4. Complete
|
||||
resp_complete = await client.post(
|
||||
"/learning/session/complete",
|
||||
json={"session_id": session_id},
|
||||
)
|
||||
complete_ok = resp_complete.status_code == 200
|
||||
|
||||
session_completed = await smoke_conn.fetchval(
|
||||
"SELECT is_completed FROM learning_sessions WHERE id = $1",
|
||||
session_id,
|
||||
)
|
||||
|
||||
passed = ans_ok and answer_recorded and complete_ok and session_completed is True
|
||||
RESULTS.append(ScenarioResult(
|
||||
"learning_session_full_flow", "learning", passed,
|
||||
f"answer={ans_ok}, recorded={answer_recorded}, complete={complete_ok}, is_completed={session_completed}",
|
||||
{
|
||||
"answer_ok": ans_ok, "answer_recorded": answer_recorded,
|
||||
"complete_ok": complete_ok, "is_completed": session_completed,
|
||||
"answer_correct": ans_body.get("correct"),
|
||||
},
|
||||
))
|
||||
assert passed
|
||||
|
||||
|
||||
async def test_learning_progress(smoke_user):
|
||||
"""GET /learning/progress → 200 + progress."""
|
||||
client = smoke_user
|
||||
resp = await client.get("/learning/progress")
|
||||
ok = resp.status_code == 200
|
||||
body = resp.json() if ok else {}
|
||||
progress = body.get("progress", [])
|
||||
|
||||
passed = ok and isinstance(progress, list)
|
||||
RESULTS.append(ScenarioResult(
|
||||
"learning_progress", "learning", passed,
|
||||
f"status={resp.status_code}, тем={len(progress)}",
|
||||
{"status": resp.status_code, "count": len(progress)},
|
||||
))
|
||||
assert passed
|
||||
|
||||
|
||||
# ============================================================
|
||||
# ВОЛНА 3 — teacher
|
||||
# ============================================================
|
||||
|
||||
async def test_teacher_progress(smoke_user):
|
||||
"""GET /teacher/progress → 200 + levels."""
|
||||
client = smoke_user
|
||||
resp = await client.get("/teacher/progress")
|
||||
ok = resp.status_code == 200
|
||||
body = resp.json() if ok else {}
|
||||
levels = body.get("levels", [])
|
||||
|
||||
passed = ok and len(levels) > 0
|
||||
RESULTS.append(ScenarioResult(
|
||||
"teacher_progress", "teacher", passed,
|
||||
f"status={resp.status_code}, уровней={len(levels)}",
|
||||
{"status": resp.status_code, "count": len(levels)},
|
||||
))
|
||||
assert passed
|
||||
|
||||
|
||||
async def test_teacher_phrase_of_day(smoke_user):
|
||||
"""GET /teacher/phrase-of-day → 200 + phrase_pl."""
|
||||
client = smoke_user
|
||||
resp = await client.get("/teacher/phrase-of-day")
|
||||
ok = resp.status_code == 200
|
||||
body = resp.json() if ok else {}
|
||||
phrase = body.get("phrase_pl")
|
||||
|
||||
passed = ok and bool(phrase)
|
||||
RESULTS.append(ScenarioResult(
|
||||
"teacher_phrase_of_day", "teacher", passed,
|
||||
f"status={resp.status_code}, phrase={(phrase or '')[:40]}",
|
||||
{"status": resp.status_code, "has_phrase": bool(phrase)},
|
||||
))
|
||||
assert passed
|
||||
|
||||
|
||||
async def test_teacher_start_or_resume(smoke_user, smoke_conn):
|
||||
"""POST /teacher/start → lesson_explanation или test_start."""
|
||||
client = smoke_user
|
||||
|
||||
resp = await client.post("/teacher/start")
|
||||
ok = resp.status_code == 200
|
||||
body = resp.json() if ok else {}
|
||||
msg_type = body.get("message_type")
|
||||
|
||||
# Может быть: lesson_explanation, test_start, completed, error
|
||||
valid = msg_type in ("lesson_explanation", "test_start", "completed")
|
||||
passed = ok and valid
|
||||
|
||||
# Если сессия создана — проверим в БД
|
||||
session_id = body.get("session_id")
|
||||
session_recorded = False
|
||||
if session_id:
|
||||
row = await smoke_conn.fetchrow(
|
||||
"SELECT id FROM learning_sessions WHERE id = $1 AND user_id = $2",
|
||||
session_id, SMOKE_USER_ID,
|
||||
)
|
||||
session_recorded = row is not None
|
||||
|
||||
RESULTS.append(ScenarioResult(
|
||||
"teacher_start_or_resume", "teacher", passed,
|
||||
f"status={resp.status_code}, type={msg_type}, session_recorded={session_recorded}",
|
||||
{"status": resp.status_code, "message_type": msg_type, "session_id": session_id},
|
||||
))
|
||||
assert passed
|
||||
|
||||
|
||||
# ============================================================
|
||||
# ВОЛНА 3 — leaderboard
|
||||
# ============================================================
|
||||
|
||||
async def test_leaderboard_top(smoke_user):
|
||||
"""GET /leaderboard/top → 200 + top3."""
|
||||
client = smoke_user
|
||||
resp = await client.get("/leaderboard/top")
|
||||
ok = resp.status_code == 200
|
||||
body = resp.json() if ok else {}
|
||||
top3 = body.get("top3", [])
|
||||
|
||||
passed = ok and isinstance(top3, list)
|
||||
RESULTS.append(ScenarioResult(
|
||||
"leaderboard_top", "leaderboard", passed,
|
||||
f"status={resp.status_code}, top3={len(top3)}",
|
||||
{"status": resp.status_code, "count": len(top3)},
|
||||
))
|
||||
assert passed
|
||||
|
||||
|
||||
# ============================================================
|
||||
# ВОЛНА 4 — assistant
|
||||
# ============================================================
|
||||
|
||||
async def test_assistant_limit(smoke_user):
|
||||
"""GET /assistant/limit → 200 + daily_limit."""
|
||||
client = smoke_user
|
||||
resp = await client.get("/assistant/limit")
|
||||
ok = resp.status_code == 200
|
||||
body = resp.json() if ok else {}
|
||||
|
||||
# Ожидаем поля: is_admin, daily_limit, used, remaining
|
||||
has_fields = "is_admin" in body and "used" in body
|
||||
|
||||
passed = ok and has_fields
|
||||
RESULTS.append(ScenarioResult(
|
||||
"assistant_limit", "assistant", passed,
|
||||
f"status={resp.status_code}, is_admin={body.get('is_admin')}, used={body.get('used')}",
|
||||
{"status": resp.status_code, "body_keys": list(body.keys())},
|
||||
))
|
||||
assert passed
|
||||
|
||||
|
||||
async def test_assistant_history(smoke_user):
|
||||
"""GET /assistant/history → 200 + history (может быть пустой)."""
|
||||
client = smoke_user
|
||||
resp = await client.get("/assistant/history")
|
||||
ok = resp.status_code == 200
|
||||
body = resp.json() if ok else {}
|
||||
history = body.get("history")
|
||||
|
||||
passed = ok and isinstance(history, list)
|
||||
RESULTS.append(ScenarioResult(
|
||||
"assistant_history", "assistant", passed,
|
||||
f"status={resp.status_code}, сообщений={len(history) if history else 0}",
|
||||
{"status": resp.status_code, "count": len(history) if history else 0},
|
||||
))
|
||||
assert passed
|
||||
|
||||
|
||||
# ============================================================
|
||||
# ВОЛНА 4 — support
|
||||
# ============================================================
|
||||
|
||||
async def test_support_user_tickets(smoke_user):
|
||||
"""GET /support/user-tickets → 200 + tickets (может быть пустой)."""
|
||||
client = smoke_user
|
||||
resp = await client.get("/support/user-tickets")
|
||||
ok = resp.status_code == 200
|
||||
body = resp.json() if ok else {}
|
||||
tickets = body.get("tickets")
|
||||
|
||||
passed = ok and isinstance(tickets, list)
|
||||
RESULTS.append(ScenarioResult(
|
||||
"support_user_tickets", "support", passed,
|
||||
f"status={resp.status_code}, тикетов={len(tickets) if tickets else 0}",
|
||||
{"status": resp.status_code, "count": len(tickets) if tickets else 0},
|
||||
))
|
||||
assert passed
|
||||
|
||||
|
||||
# ============================================================
|
||||
# ВОЛНА 5 — admin (negative test)
|
||||
# ============================================================
|
||||
|
||||
async def test_admin_dashboard_forbidden(smoke_user):
|
||||
"""GET /admin/dashboard без админ-прав → 403.
|
||||
|
||||
SMOKE_USER_ID НЕ в ADMIN_IDS, поэтому эндпоинт должен вернуть 403.
|
||||
Это проверяет, что защита админ-эндпоинтов работает.
|
||||
"""
|
||||
client = smoke_user
|
||||
resp = await client.get("/admin/dashboard")
|
||||
|
||||
passed = resp.status_code == 403
|
||||
RESULTS.append(ScenarioResult(
|
||||
"admin_dashboard_forbidden", "admin", passed,
|
||||
f"status={resp.status_code} (ожидали 403 — тестовый юзер не админ)",
|
||||
{"status": resp.status_code},
|
||||
))
|
||||
assert passed
|
||||
|
||||
|
||||
# ============================================================
|
||||
# ВОЛНА 5 — feed
|
||||
# ============================================================
|
||||
|
||||
async def test_feed_news_section(smoke_user):
|
||||
"""GET /feed/news → 200 + posts (может быть пустой)."""
|
||||
client = smoke_user
|
||||
resp = await client.get("/feed/news")
|
||||
ok = resp.status_code == 200
|
||||
body = resp.json() if ok else {}
|
||||
posts = body.get("posts")
|
||||
|
||||
passed = ok and isinstance(posts, list)
|
||||
RESULTS.append(ScenarioResult(
|
||||
"feed_news_section", "feed", passed,
|
||||
f"status={resp.status_code}, постов={len(posts) if posts else 0}",
|
||||
{"status": resp.status_code, "count": len(posts) if posts else 0},
|
||||
))
|
||||
assert passed
|
||||
|
||||
|
||||
# ============ Отчёт ============
|
||||
@@ -618,4 +894,14 @@ def _write_report() -> None:
|
||||
lines.append(f"- **{r.name}** ({r.section}) — {r.message}")
|
||||
lines.append("")
|
||||
|
||||
# Группировка по секциям
|
||||
lines += ["## 📊 По секциям", ""]
|
||||
sections: dict[str, list[ScenarioResult]] = {}
|
||||
for r in RESULTS:
|
||||
sections.setdefault(r.section, []).append(r)
|
||||
for section, items in sorted(sections.items()):
|
||||
ok_count = sum(1 for r in items if r.passed)
|
||||
lines.append(f"- **{section}**: {ok_count}/{len(items)}")
|
||||
lines.append("")
|
||||
|
||||
Path("smoke_test_report.md").write_text("\n".join(lines), encoding="utf-8")
|
||||
Reference in New Issue
Block a user