Spaces:
Running
Running
| import os | |
| import sys | |
| from fastapi.testclient import TestClient | |
| PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | |
| if PROJECT_ROOT not in sys.path: | |
| sys.path.insert(0, PROJECT_ROOT) | |
| import main # noqa: E402 | |
| def _make_test_client(): | |
| # use test db | |
| main.db_path = os.path.join(PROJECT_ROOT, "test_focus_guard.db") | |
| # clean old file | |
| if os.path.exists(main.db_path): | |
| os.remove(main.db_path) | |
| return TestClient(main.app) | |
| def test_start_session_returns_id(): | |
| client = _make_test_client() | |
| with client: | |
| resp = client.post("/api/sessions/start") | |
| assert resp.status_code == 200 | |
| data = resp.json() | |
| assert "session_id" in data | |
| assert isinstance(data["session_id"], int) | |
| assert data["session_id"] > 0 | |
| def test_end_session_after_start(): | |
| client = _make_test_client() | |
| with client: | |
| # create session | |
| r_start = client.post("/api/sessions/start") | |
| session_id = r_start.json()["session_id"] | |
| # end session | |
| r_end = client.post("/api/sessions/end", json={"session_id": session_id}) | |
| assert r_end.status_code == 200 | |
| summary = r_end.json() | |
| assert summary["session_id"] == session_id | |
| assert "duration_seconds" in summary | |
| assert "focus_score" in summary | |
| def test_get_sessions_list(): | |
| client = _make_test_client() | |
| with client: | |
| r_start = client.post("/api/sessions/start") | |
| session_id = r_start.json()["session_id"] | |
| client.post("/api/sessions/end", json={"session_id": session_id}) | |
| r_list = client.get("/api/sessions", params={"filter": "all", "limit": 10, "offset": 0}) | |
| assert r_list.status_code == 200 | |
| data = r_list.json() | |
| assert isinstance(data, list) | |
| if data: | |
| item = data[0] | |
| assert "id" in item | |
| assert "start_time" in item | |
| assert "focus_score" in item | |