Spaces:
Sleeping
Sleeping
File size: 7,662 Bytes
f8120eb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | from __future__ import annotations
import shutil
from pathlib import Path
import pytest
from fastapi.testclient import TestClient
from main import create_app
from services import JsonRepository
class FakeAIService:
mode = "mock"
def generate_idea_feedback(self, idea_text, context, history, references):
return {
"reply": f"Разбор идеи: {idea_text[:40]}",
"risks": ["Риск спроса", "Риск экономики"],
"next_questions": ["Кто первый сегмент?", "Как проверите спрос?"],
"advice": ["Сузьте сегмент", "Выберите метрику успеха"],
}
def evaluate_case_solution(self, case_data, solution_text, history):
return {
"summary": f"Оценка кейса {case_data['title']}",
"score": 81,
"criteria_scores": [
{"name": "problem_clarity", "score": 82, "rationale": "Есть структура"},
{"name": "market_business_logic", "score": 80, "rationale": "Есть бизнес-логика"},
{"name": "feasibility", "score": 79, "rationale": "Реализуемо"},
{"name": "differentiation", "score": 83, "rationale": "Есть новая идея"},
],
"strengths": ["Есть структура"],
"weaknesses": ["Не хватает цифр"],
"improvements": ["Добавить метрики"],
"novel_ideas": ["Нестандартный канал"],
}
def answer_case_followup(self, case_data, question, history, evaluation=None):
return {
"reply": f"Follow-up по кейсу: {question}",
"risks": ["Слишком общий план"],
"next_questions": ["Какая одна метрика важнее всего?"],
"advice": ["Уточните первый эксперимент"],
}
@pytest.fixture()
def client(tmp_path: Path) -> TestClient:
project_data = Path(__file__).resolve().parents[1] / "data"
temp_data = tmp_path / "data"
shutil.copytree(project_data, temp_data)
repository = JsonRepository(temp_data)
app = create_app(repository=repository, ai_service=FakeAIService())
return TestClient(app)
def test_health_and_openapi(client: TestClient) -> None:
health_response = client.get("/health")
assert health_response.status_code == 200
assert health_response.json()["llm_mode"] == "mock"
openapi_response = client.get("/openapi.json")
assert openapi_response.status_code == 200
assert openapi_response.json()["info"]["title"] == "AI Business Trainer MVP"
def test_cases_list_and_detail(client: TestClient) -> None:
list_response = client.get("/api/v1/cases")
assert list_response.status_code == 200
cases = list_response.json()
assert len(cases) >= 5
detail_response = client.get(f"/api/v1/cases/{cases[0]['id']}")
assert detail_response.status_code == 200
assert "reference_solution_summary" in detail_response.json()
def test_idea_session_flow(client: TestClient) -> None:
create_response = client.post(
"/api/v1/idea-sessions",
json={
"client_id": "demo-user",
"idea_text": "AI-платформа для развития бизнес-навыков студентов",
"context": "Интеграция с бизнес-клубом университета",
},
)
assert create_response.status_code == 201
payload = create_response.json()
assert payload["session_id"]
assert payload["risks"]
message_response = client.post(
f"/api/v1/idea-sessions/{payload['session_id']}/messages",
json={"client_id": "demo-user", "message": "Хочу сфокусироваться на первокурсниках"},
)
assert message_response.status_code == 200
assert message_response.json()["reply"].startswith("Разбор идеи:")
def test_case_session_submit_followup_and_stats(client: TestClient) -> None:
cases = client.get("/api/v1/cases").json()
case_id = cases[0]["id"]
create_response = client.post(
"/api/v1/case-sessions",
json={"client_id": "demo-user", "case_id": case_id},
)
assert create_response.status_code == 201
session_id = create_response.json()["session_id"]
submit_response = client.post(
f"/api/v1/case-sessions/{session_id}/submit",
json={
"client_id": "demo-user",
"solution_text": "Сужаем сегмент, сокращаем онбординг и считаем time-to-value.",
},
)
assert submit_response.status_code == 200
submit_payload = submit_response.json()
assert submit_payload["score"] == 81
assert submit_payload["criteria_scores"]
followup_response = client.post(
f"/api/v1/case-sessions/{session_id}/messages",
json={"client_id": "demo-user", "message": "Какой эксперимент лучше начать первым?"},
)
assert followup_response.status_code == 200
assert "Follow-up" in followup_response.json()["reply"]
complete_response = client.post(
"/api/v1/progress/complete",
json={
"client_id": "demo-user",
"task_type": "case_submission",
"task_id": session_id,
"self_marked_complete": True,
},
)
assert complete_response.status_code == 200
assert complete_response.json()["stats"]["completed_count"] == 1
stats_response = client.get("/api/v1/stats/demo-user")
assert stats_response.status_code == 200
stats_payload = stats_response.json()
assert stats_payload["sessions_count"] == 1
assert stats_payload["average_score"] == 81.0
def test_wrong_client_gets_403(client: TestClient) -> None:
create_response = client.post(
"/api/v1/idea-sessions",
json={"client_id": "owner", "idea_text": "Платформа для кейс-чемпионатов"},
)
session_id = create_response.json()["session_id"]
response = client.post(
f"/api/v1/idea-sessions/{session_id}/messages",
json={"client_id": "intruder", "message": "Попробую залезть в чужую сессию"},
)
assert response.status_code == 403
def test_missing_ids_and_corrupted_state_are_handled(client: TestClient) -> None:
repository = client.app.state.repository
assert client.get("/api/v1/cases/unknown-case").status_code == 404
assert client.post(
"/api/v1/idea-sessions/unknown-session/messages",
json={"client_id": "demo", "message": "hello"},
).status_code == 404
sessions_path = repository.state_dir / "sessions.json"
progress_path = repository.state_dir / "progress.json"
sessions_path.write_text("{broken", encoding="utf-8")
progress_path.write_text("", encoding="utf-8")
stats_response = client.get("/api/v1/stats/demo-user")
assert stats_response.status_code == 200
assert stats_response.json()["completed_count"] == 0
create_response = client.post(
"/api/v1/idea-sessions",
json={"client_id": "demo-user", "idea_text": "Новая идея после повреждения state"},
)
assert create_response.status_code == 201
def test_validation_errors(client: TestClient) -> None:
response = client.post(
"/api/v1/idea-sessions",
json={"client_id": "demo-user", "idea_text": " "},
)
assert response.status_code == 422
|