- Fix first daily record 422 (expectedVersion now accepts 0)
- Add period_window + summarize; /api/summary and /api/tasks/{id}/summary
aggregate each metric inside its own day/week/month/lifetime window,
reading raw facts only (never the rebuildable month archives)
- Drive frontend rendering from per-page layout JSON with a component
registry; scope/cardinality enforced server-side, invalid layouts fall back
- Add quick checkin and duration input in H:M:S that round-trips integer seconds
- Fix heatmap/completedAt timezone drift to Asia/Shanghai
- Add server-side session TTL (30d) matching the login cookie
- Handle date.min/date.max boundaries without OverflowError
- Half-up rounding to match browser Math.round
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
55 lines
2.8 KiB
Python
55 lines
2.8 KiB
Python
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_summary_endpoints(client):
|
|
task = client.post("/api/tasks", json=payload("周期任务")).json()
|
|
metric_id = task["metrics"][0]["metricId"]
|
|
client.put(f"/api/tasks/{task['taskId']}/daily/2026-08-20", json={"expectedVersion": 0, "values": [{"metricId": metric_id, "value": 1}]})
|
|
|
|
scoped = client.get(f"/api/tasks/{task['taskId']}/summary?date=2026-08-20").json()
|
|
assert scoped["tasks"][task["taskId"]]["metrics"][0]["value"] == 1
|
|
assert scoped["tasks"][task["taskId"]]["today"]["recorded"] is True
|
|
|
|
assert client.get("/api/summary?date=2026-08-21").json()["tasks"][task["taskId"]]["metrics"][0]["value"] == 0
|
|
|
|
|
|
def test_first_daily_write_is_accepted(client):
|
|
task = client.post("/api/tasks", json=payload("首次记录")).json()
|
|
metric_id = task["metrics"][0]["metricId"]
|
|
current = client.get(f"/api/tasks/{task['taskId']}/daily/2026-08-20").json()
|
|
saved = client.put(
|
|
f"/api/tasks/{task['taskId']}/daily/2026-08-20",
|
|
json={"expectedVersion": current["version"], "values": [{"metricId": metric_id, "value": 1}]},
|
|
)
|
|
assert saved.status_code == 200, saved.text
|
|
|
|
|
|
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
|
|
|