feat: 学习进度轮询、学时依据与首页代理弹窗
This commit is contained in:
@@ -642,7 +642,9 @@ def test_settings_do_not_expose_process_switches() -> None:
|
||||
"heartbeat_video_seconds",
|
||||
"heartbeat_document_seconds",
|
||||
"heartbeat_scorm_seconds",
|
||||
"hours_source",
|
||||
}
|
||||
assert body["hours_source"] == "api"
|
||||
assert identity.password_cipher()
|
||||
assert decrypt_secret(identity.password_cipher()) == INITIAL_PASSWORD
|
||||
|
||||
@@ -852,3 +854,16 @@ def test_admin_can_save_study_settings_in_one_request() -> None:
|
||||
assert one["study_start"] == "每月8日 09:10:11"
|
||||
assert one["course_workers"] == 2
|
||||
assert one["proxy"] == "HTTP 10.0.0.8:8080"
|
||||
|
||||
|
||||
def test_admin_can_switch_hours_source() -> None:
|
||||
api = _app()
|
||||
client = TestClient(api.app)
|
||||
assert client.post("/api/login", json={"employee_id": "admin", "password": INITIAL_PASSWORD}).status_code == 200
|
||||
saved = client.put("/api/admin/settings", json=_settings_body(hours_source="history"))
|
||||
assert saved.status_code == 200
|
||||
assert saved.json()["hours_source"] == "history"
|
||||
assert client.get("/api/admin/settings").json()["hours_source"] == "history"
|
||||
bad = client.put("/api/admin/settings", json=_settings_body(hours_source="other"))
|
||||
assert bad.status_code == 400
|
||||
assert bad.json()["detail"] == "学时达标依据须为 api 或 history"
|
||||
|
||||
@@ -562,11 +562,13 @@ def test_user_can_start_and_stop_study_immediately() -> None:
|
||||
client.put("/api/settings/study-start", json={"day": 31, "hour": 23, "minute": 59, "second": 59})
|
||||
idle = client.get("/api/settings/study-control")
|
||||
assert idle.status_code == 200
|
||||
assert idle.json()["workers"] == []
|
||||
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()["workers"] == []
|
||||
stopped = client.post("/api/settings/study-control", json={"action": "stop"})
|
||||
assert stopped.status_code == 200
|
||||
assert stopped.json()["started"] is False
|
||||
|
||||
@@ -4,7 +4,18 @@ import pytest
|
||||
|
||||
from datetime import datetime
|
||||
|
||||
from euniv.autostudy import CourseCursor, HoursWatcher, _parse_minutes, _study_video, wait_stop
|
||||
from euniv.autostudy import (
|
||||
CourseCursor,
|
||||
HoursWatcher,
|
||||
_clear_user_status,
|
||||
_parse_minutes,
|
||||
_set_user_status,
|
||||
_set_worker_status,
|
||||
_study_video,
|
||||
autostudy_status,
|
||||
wait_stop,
|
||||
)
|
||||
from euniv.client import accumulate_month_minutes, fold_month_minutes
|
||||
from euniv.course_workers import CourseWorkersError, of as workers_of, validate as validate_workers
|
||||
from euniv.proxy import ProxyError, apply as apply_proxy, build_proxy_url, parse_proxy, view_of as proxy_view
|
||||
from euniv.study_control import eligible as study_eligible, start_now, stop_now
|
||||
@@ -81,6 +92,70 @@ def test_hours_watcher_stops_when_target_reached(monkeypatch) -> None:
|
||||
asyncio.run(main())
|
||||
|
||||
|
||||
def test_hours_watcher_history_source_uses_month_history(monkeypatch) -> None:
|
||||
saved: list[tuple[str, str]] = []
|
||||
monkeypatch.setattr("euniv.autostudy._update_month_hours", lambda eid, hours: saved.append((eid, hours)))
|
||||
|
||||
class _HistorySession:
|
||||
async def month_history_minutes(self) -> int:
|
||||
return 600
|
||||
|
||||
async def month_hours_text(self) -> str:
|
||||
raise AssertionError("history 模式不应读接口本月学时")
|
||||
|
||||
async def main() -> None:
|
||||
user_stop = asyncio.Event()
|
||||
watcher = HoursWatcher(_HistorySession(), "u1", 600, user_stop, source="history")
|
||||
assert await watcher.refresh() is True
|
||||
assert watcher.reached
|
||||
assert saved == [("u1", "600")]
|
||||
|
||||
asyncio.run(main())
|
||||
|
||||
|
||||
def test_accumulate_month_minutes_stops_at_previous_month() -> None:
|
||||
pages = [
|
||||
[
|
||||
{"title": "a", "minutes": 10, "studied_at": "2026-11-15 09:00:00"},
|
||||
{"title": "b", "minutes": 20, "studied_at": "2026-11-02 09:00:00"},
|
||||
],
|
||||
[
|
||||
{"title": "c", "minutes": 5, "studied_at": "2026-11-01 09:00:00"},
|
||||
{"title": "d", "minutes": 99, "studied_at": "2026-10-31 23:00:00"},
|
||||
{"title": "e", "minutes": 1, "studied_at": "2026-10-01 00:00:00"},
|
||||
],
|
||||
[
|
||||
{"title": "should-not", "minutes": 1000, "studied_at": "2026-09-01 00:00:00"},
|
||||
],
|
||||
]
|
||||
calls: list[tuple[int, int]] = []
|
||||
|
||||
def fetch(offset: int, limit: int):
|
||||
calls.append((offset, limit))
|
||||
return pages[offset // limit]
|
||||
|
||||
assert accumulate_month_minutes(fetch, 2026, 11, page_size=2) == 35
|
||||
assert calls == [(0, 2), (2, 2)]
|
||||
dumped = [
|
||||
{"title": "a", "minutes": 8, "studied_at": "2026-11-14 10:00:00"},
|
||||
{"title": "b", "minutes": 4, "studied_at": "2026-10-30 10:00:00"},
|
||||
]
|
||||
assert fold_month_minutes(dumped, 2026, 11) == (8, True)
|
||||
assert accumulate_month_minutes(lambda _offset, _limit: dumped, 2026, 11, page_size=100) == 8
|
||||
|
||||
|
||||
def test_autostudy_status_lists_workers_in_id_order() -> None:
|
||||
_clear_user_status("u1")
|
||||
_set_user_status("u1", state="学习中")
|
||||
_set_worker_status("u1", 2, course="乙", progress=80, ok=True)
|
||||
_set_worker_status("u1", 1, course="甲", progress=10, ok=False)
|
||||
users = autostudy_status()["users"]["u1"]
|
||||
assert [row["id"] for row in users["workers"]] == [1, 2]
|
||||
assert users["workers"][0]["ok"] is False
|
||||
assert users["course"] == "甲"
|
||||
_clear_user_status("u1")
|
||||
|
||||
|
||||
def test_hours_watcher_continues_below_target(monkeypatch) -> None:
|
||||
monkeypatch.setattr("euniv.autostudy._update_month_hours", lambda *_args: None)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user