feat: 学习进度轮询、学时依据与首页代理弹窗

This commit is contained in:
2026-11-15 05:13:48 +08:00
parent f0a2bdd854
commit 5acbea35fb
17 changed files with 921 additions and 221 deletions
+76 -1
View File
@@ -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)