feat: scaffold personal progress monitor MVP

This commit is contained in:
MobKBK
2026-08-24 06:18:21 +08:00
commit 094e03f341
21 changed files with 1480 additions and 0 deletions

23
tests/conftest.py Normal file
View File

@@ -0,0 +1,23 @@
from pathlib import Path
import pytest
from fastapi.testclient import TestClient
from app.auth import SessionManager, make_password_hash
from app.main import create_app
@pytest.fixture()
def client(tmp_path: Path):
app = create_app(tmp_path / "data", Path(__file__).parents[1] / "config" / "layouts", SessionManager(None))
with TestClient(app) as test_client:
yield test_client
@pytest.fixture()
def auth_client(tmp_path: Path):
manager = SessionManager(make_password_hash("secret"))
app = create_app(tmp_path / "data", Path(__file__).parents[1] / "config" / "layouts", manager)
with TestClient(app) as test_client:
yield test_client

31
tests/test_api.py Normal file
View File

@@ -0,0 +1,31 @@
from datetime import date
def payload(name="API 任务"):
return {"name": name, "startDate": "2026-08-01", "priority": 3, "metrics": [{"name": "完成", "kind": "checkin", "period": "day"}]}
def test_api_crud_order_layout_and_daily(client):
response = client.get("/api/health"); assert response.status_code == 200
assert client.get("/api/layouts/home").json()["page"] == "home"
first = client.post("/api/tasks", json=payload("第一项")); assert first.status_code == 201
second = client.post("/api/tasks", json=payload("第二项")).json()
task = first.json(); metric_id = task["metrics"][0]["metricId"]
listed = client.get("/api/tasks").json(); assert len(listed["items"]) == 2
reordered = client.put("/api/tasks/reorder/all", json={"taskIds": [second["taskId"], task["taskId"]], "expectedVersion": listed["version"]})
assert reordered.status_code == 200
saved = client.put(f"/api/tasks/{task['taskId']}/daily/2026-08-20", json={"values": [{"metricId": metric_id, "value": 1}], "note": "完成"})
assert saved.status_code == 200
assert client.get(f"/api/tasks/{task['taskId']}/history?start=2026-08-20&end=2026-08-20").json()["records"]
def test_invalid_input_and_auth(auth_client):
assert auth_client.get("/api/tasks").status_code == 401
login = auth_client.post("/api/auth/login", json={"password": "secret"}); assert login.status_code == 200
assert auth_client.get("/api/tasks").status_code == 200
csrf = login.json()["csrfToken"]
bad = auth_client.post("/api/tasks", json={"name": "", "startDate": "2026-08-01", "metrics": []}, headers={"X-CSRF-Token": csrf})
assert bad.status_code == 422
forbidden = auth_client.post("/api/tasks", json=payload())
assert forbidden.status_code == 403

48
tests/test_repository.py Normal file
View File

@@ -0,0 +1,48 @@
from datetime import date, timedelta
from app.models import DailyRecordInput, MetricValue, TaskInput
from app.repository import ConflictError, JsonRepository
def task_payload():
return TaskInput.model_validate({
"name": "学习计划", "startDate": "2026-08-01", "priority": 2,
"metrics": [
{"name": "打卡", "kind": "checkin", "period": "day"},
{"name": "刷题", "kind": "quantity", "period": "lifetime", "target": 100, "unit": "题"},
{"name": "专注", "kind": "duration", "period": "day", "target": 3600},
],
})
def test_crud_revision_soft_delete_and_purge(tmp_path):
repo = JsonRepository(tmp_path)
task = repo.create_task(task_payload())
assert len(task["metrics"]) == 3
updated = repo.update_task(task["taskId"], task_payload().model_copy(update={"name": "改名后的计划"}), task["version"])
assert updated["taskId"] == task["taskId"] and updated["revision"] == 2
assert repo.history(task["taskId"], date(2026, 8, 1), date(2026, 8, 1))["revisions"]
deleted = repo.soft_delete_task(task["taskId"], updated["version"])
assert repo.list_tasks()["items"] == []
repo.restore_task(task["taskId"], deleted["version"])
repo.soft_delete_task(task["taskId"], deleted["version"] + 1)
repo.purge_task(task["taskId"], task["taskId"])
assert repo.list_tasks(include_deleted=True)["items"] == []
def test_daily_mixed_metrics_history_and_idempotent_archive(tmp_path):
repo = JsonRepository(tmp_path)
task = repo.create_task(task_payload())
values = [MetricValue(metricId=item["metricId"], value=1 if item["kind"] == "checkin" else 120) for item in task["metrics"]]
record = repo.upsert_daily(task["taskId"], date(2026, 8, 10), DailyRecordInput(note="不错", values=values))
assert record["version"] == 1
repo.close_day(date(2026, 8, 10)); repo.close_day(date(2026, 8, 10))
history = repo.history(task["taskId"], date(2026, 8, 10), date(2026, 8, 10))
assert history["records"][0]["note"] == "不错"
try:
repo.upsert_daily(task["taskId"], date(2026, 8, 10), DailyRecordInput(expectedVersion=0, values=[]))
except ConflictError:
pass
else:
raise AssertionError("expected version conflict")