feat: 按账号调度自动学习并让首页实时跟上状态

This commit is contained in:
2026-11-16 01:14:36 +08:00
parent 158578d17c
commit 7f0466b0f6
31 changed files with 2699 additions and 631 deletions
+154 -2
View File
@@ -75,6 +75,20 @@ class FakeClient:
}
]
def home_summary(self) -> dict:
return {
"name": "测试用户",
"avatar": "https://example.test/a.jpg",
"department": "测试部门",
"level": "Scientist",
"year_hours": "100",
"month_hours": "60",
"today_hours": 1,
"total_hours": "10",
"total_score": "3.00",
"points": 20,
}
def home_snapshot(self) -> dict:
if self._token == "yxt-1":
self._token = "yxt-2"
@@ -121,6 +135,7 @@ def test_login_stores_password_and_credential_only() -> None:
"last_refresh_at",
"credential_valid",
"month_hours",
"hours_month",
"monthly_target",
"proxy_scheme",
"proxy_host",
@@ -134,7 +149,9 @@ def test_login_stores_password_and_credential_only() -> None:
"course_workers",
"use_proxy_pool",
"study_paused",
"study_stop_month",
"study_ignore_window",
"study_schedule_enabled",
}
home = client.get("/api/home")
@@ -542,10 +559,20 @@ def test_user_can_set_monthly_study_start() -> None:
assert client.post("/api/login", json={"employee_id": "6650000146", "password": "secret-pass"}).status_code == 200
empty = client.get("/api/settings/study-start")
assert empty.status_code == 200
assert empty.json() == {"day": 1, "hour": 0, "minute": 0, "second": 0, "label": "每月1日 00:00:00"}
saved = client.put("/api/settings/study-start", json={"day": 5, "hour": 16, "minute": 0, "second": 7})
assert empty.json() == {
"day": 1,
"hour": 0,
"minute": 0,
"second": 0,
"enabled": True,
"label": "每月1日 00:00:00",
}
saved = client.put("/api/settings/study-start", json={"day": 5, "hour": 16, "minute": 0, "second": 7, "enabled": False})
assert saved.status_code == 200
assert saved.json()["label"] == "每月5日 16:00:07"
assert saved.json()["enabled"] is False
again = client.put("/api/settings/study-start", json={"day": 5, "hour": 16, "minute": 0, "second": 7, "enabled": True})
assert again.json()["enabled"] is True
bad = client.put("/api/settings/study-start", json={"day": 32, "hour": 16, "minute": 0, "second": 0})
assert bad.status_code == 400
@@ -563,11 +590,13 @@ def test_user_can_start_and_stop_study_immediately() -> None:
idle = client.get("/api/settings/study-control")
assert idle.status_code == 200
assert idle.json()["workers"] == []
assert idle.json()["hours_met"] is False
started = client.post("/api/settings/study-control", json={"action": "start"})
assert started.status_code == 200
assert started.json()["started"] is True
assert started.json()["ignore_window"] is True
assert started.json()["paused"] is False
assert started.json()["hours_met"] is False
assert started.json()["workers"] == []
stopped = client.post("/api/settings/study-control", json={"action": "stop"})
assert stopped.status_code == 200
@@ -578,6 +607,56 @@ def test_user_can_start_and_stop_study_immediately() -> None:
assert bad.status_code == 400
def test_study_control_stays_stopped_when_month_hours_met() -> None:
import euniv.api as api_module
import euniv.service as service_module
api_module.login_and_store = lambda db, employee_id, password, manual=False, auto_attempts=3: service_module.login_and_store(
db, employee_id, password, FakeClient, manual=manual, auto_attempts=auto_attempts
)
client = TestClient(api_module.app)
assert client.post("/api/login", json={"employee_id": "6650000146", "password": "secret-pass"}).status_code == 200
with SessionLocal() as db:
account = db.get(Account, "6650000146")
assert account is not None
account.monthly_target = 70
account.month_hours = "78"
db.commit()
started = client.post("/api/settings/study-control", json={"action": "start"})
assert started.status_code == 200
assert started.json()["hours_met"] is True
assert started.json()["started"] is False
assert started.json()["paused"] is False
def test_study_control_websocket_sends_snapshot() -> None:
import euniv.api as api_module
import euniv.service as service_module
from starlette.websockets import WebSocketDisconnect
api_module.login_and_store = lambda db, employee_id, password, manual=False, auto_attempts=3: service_module.login_and_store(
db, employee_id, password, FakeClient, manual=manual, auto_attempts=auto_attempts
)
client = TestClient(api_module.app)
with pytest.raises(WebSocketDisconnect) as closed:
with client.websocket_connect("/api/settings/study-control/ws") as ws:
ws.receive_json()
assert closed.value.code == 4401
assert client.post("/api/login", json={"employee_id": "6650000146", "password": "secret-pass"}).status_code == 200
client.put("/api/settings/study-start", json={"day": 31, "hour": 23, "minute": 59, "second": 59})
with client.websocket_connect("/api/settings/study-control/ws") as ws:
data = ws.receive_json()
assert data["started"] is False
assert data["hours_met"] is False
assert data["window_open"] is False
assert data["workers"] == []
assert "month_hours" in data
ws.send_text("ping")
pong = ws.receive_json()
assert pong["paused"] is False
assert pong["started"] is False
def test_user_can_read_study_history() -> None:
import euniv.api as api_module
import euniv.service as service_module
@@ -596,6 +675,79 @@ def test_user_can_read_study_history() -> None:
assert body["total"] == 1
assert body["items"][0]["title"].startswith("ZXCTN")
assert body["items"][0]["minutes"] == 2
assert body["hours_source"] == "api"
assert body["month_hours"] == "60"
assert body["year_hours"] == "100"
assert body["today_hours"] == 1
assert body["total_hours"] == "10"
def _set_hours_source(value: str) -> None:
from admin.settings import load_config
with SessionLocal() as db:
row = load_config(db)
row.hours_source = value
db.commit()
def test_home_and_history_hours_follow_source() -> None:
import euniv.api as api_module
import euniv.service as service_module
api_module.login_and_store = lambda db, employee_id, password, manual=False, auto_attempts=3: service_module.login_and_store(
db, employee_id, password, FakeClient, manual=manual, auto_attempts=auto_attempts
)
api_module.fetch_home_summary = lambda db, employee_id: service_module.fetch_home_summary(
db, employee_id, FakeClient
)
api_module.fetch_study_history = lambda db, employee_id: service_module.fetch_study_history(
db, employee_id, FakeClient
)
client = TestClient(api_module.app)
assert client.post("/api/login", json={"employee_id": "6650000146", "password": "secret-pass"}).status_code == 200
home = client.get("/api/home/summary")
history = client.get("/api/home/history")
assert home.status_code == 200
assert history.status_code == 200
assert home.json()["month_hours"] == history.json()["month_hours"] == "60"
assert home.json()["year_hours"] == history.json()["year_hours"] == "100"
assert home.json()["hours_source"] == history.json()["hours_source"] == "api"
_set_hours_source("history")
home = client.get("/api/home/summary")
history = client.get("/api/home/history")
assert home.json()["hours_source"] == history.json()["hours_source"] == "history"
assert home.json()["month_hours"] == history.json()["month_hours"] == 2
assert home.json()["year_hours"] == history.json()["year_hours"] == 2
assert home.json()["total_hours"] == history.json()["total_hours"] == 2
assert home.json()["today_hours"] == history.json()["today_hours"] == 0
assert home.json()["total_score"] == "3.00"
def test_month_hours_refresh_follows_source() -> None:
from euniv.service import _month_hours
class ApiClient:
def month_hours(self) -> str:
return "58"
def month_history_minutes(self) -> int:
raise AssertionError("api 模式不应读历史")
assert _month_hours(ApiClient()) == "58"
_set_hours_source("history")
class HistoryClient:
def month_hours(self) -> str:
raise AssertionError("history 模式不应读接口")
def month_history_minutes(self) -> int:
return 80
assert _month_hours(HistoryClient()) == "80"
def test_study_history_parses_period_table() -> None: