154 lines
4.9 KiB
Python
154 lines
4.9 KiB
Python
import asyncio
|
|
|
|
import pytest
|
|
|
|
from euniv.autostudy import HoursWatcher, _parse_minutes, _study_video, wait_stop
|
|
from euniv.proxy import parse_proxy
|
|
|
|
|
|
def test_parse_proxy_accepts_http_and_socks() -> None:
|
|
assert parse_proxy(None) is None
|
|
assert parse_proxy("") is None
|
|
assert parse_proxy("127.0.0.1:7890") == "http://127.0.0.1:7890"
|
|
assert parse_proxy("http://user:pass@10.0.0.1:8080") == "http://user:pass@10.0.0.1:8080"
|
|
assert parse_proxy("https://10.0.0.1:8443") == "https://10.0.0.1:8443"
|
|
assert parse_proxy("socks4://127.0.0.1:1080") == "socks4://127.0.0.1:1080"
|
|
assert parse_proxy("socks5://user:pass@127.0.0.1:1080") == "socks5://user:pass@127.0.0.1:1080"
|
|
|
|
|
|
def test_parse_proxy_rejects_other_schemes() -> None:
|
|
with pytest.raises(ValueError):
|
|
parse_proxy("ss://secret")
|
|
with pytest.raises(ValueError):
|
|
parse_proxy("openvpn://gateway")
|
|
with pytest.raises(ValueError):
|
|
parse_proxy("http://")
|
|
|
|
|
|
def test_parse_minutes() -> None:
|
|
assert _parse_minutes("") == 0
|
|
assert _parse_minutes("600") == 600
|
|
assert _parse_minutes("600分钟") == 600
|
|
|
|
|
|
def test_wait_stop_timeout_and_event() -> None:
|
|
async def main() -> None:
|
|
event = asyncio.Event()
|
|
assert await wait_stop(0.05, event) is False
|
|
event.set()
|
|
assert await wait_stop(1, event) is True
|
|
|
|
asyncio.run(main())
|
|
|
|
|
|
class _HourSession:
|
|
def __init__(self, hours: str) -> None:
|
|
self.hours = hours
|
|
self.calls = 0
|
|
|
|
async def month_hours_text(self) -> str:
|
|
self.calls += 1
|
|
return self.hours
|
|
|
|
|
|
def test_hours_watcher_stops_when_target_reached(monkeypatch) -> None:
|
|
saved: list[tuple[str, str]] = []
|
|
monkeypatch.setattr("euniv.autostudy._update_month_hours", lambda eid, hours: saved.append((eid, hours)))
|
|
|
|
async def main() -> None:
|
|
session = _HourSession("600")
|
|
user_stop = asyncio.Event()
|
|
watcher = HoursWatcher(session, "u1", 600, user_stop)
|
|
assert await watcher.refresh() is True
|
|
assert watcher.reached
|
|
assert user_stop.is_set()
|
|
assert saved == [("u1", "600")]
|
|
|
|
asyncio.run(main())
|
|
|
|
|
|
def test_hours_watcher_continues_below_target(monkeypatch) -> None:
|
|
monkeypatch.setattr("euniv.autostudy._update_month_hours", lambda *_args: None)
|
|
|
|
async def main() -> None:
|
|
session = _HourSession("100")
|
|
user_stop = asyncio.Event()
|
|
watcher = HoursWatcher(session, "u1", 600, user_stop)
|
|
assert await watcher.refresh() is False
|
|
assert not user_stop.is_set()
|
|
|
|
asyncio.run(main())
|
|
|
|
|
|
def test_hours_ticker_refreshes_while_running(monkeypatch) -> None:
|
|
monkeypatch.setattr("euniv.autostudy._update_month_hours", lambda *_args: None)
|
|
|
|
async def main() -> None:
|
|
session = _HourSession("10")
|
|
user_stop = asyncio.Event()
|
|
global_stop = asyncio.Event()
|
|
watcher = HoursWatcher(session, "u1", 600, user_stop, interval=0.05)
|
|
task = asyncio.create_task(watcher.ticker(global_stop))
|
|
await asyncio.sleep(0.14)
|
|
global_stop.set()
|
|
await task
|
|
assert session.calls >= 1
|
|
|
|
asyncio.run(main())
|
|
|
|
|
|
class _VideoSession:
|
|
def __init__(self, hours: str) -> None:
|
|
self.hours = hours
|
|
self.progress_calls = 0
|
|
self.hour_calls = 0
|
|
|
|
async def update_progress(self, _kid: str, _pkg: str) -> dict:
|
|
self.progress_calls += 1
|
|
if self.progress_calls == 1:
|
|
return {"studyschedule": 90, "viewSchedule": 30}
|
|
return {"studyschedule": 100, "viewSchedule": 60}
|
|
|
|
async def submit_video_heartbeat(self, *_args, **_kwargs) -> dict:
|
|
return {"studySchedule": 100}
|
|
|
|
async def month_hours_text(self) -> str:
|
|
self.hour_calls += 1
|
|
return self.hours
|
|
|
|
|
|
def test_video_refresh_hours_when_finished(monkeypatch) -> None:
|
|
monkeypatch.setattr("euniv.autostudy._update_month_hours", lambda *_args: None)
|
|
|
|
async def main() -> None:
|
|
session = _VideoSession("700")
|
|
global_stop = asyncio.Event()
|
|
user_stop = asyncio.Event()
|
|
watcher = HoursWatcher(session, "u1", 600, user_stop)
|
|
await _study_video(session, "kid", "", 30, global_stop, watcher)
|
|
assert session.hour_calls == 1
|
|
assert watcher.reached
|
|
assert user_stop.is_set()
|
|
|
|
asyncio.run(main())
|
|
|
|
|
|
class _AlreadyDoneSession:
|
|
async def update_progress(self, _kid: str, _pkg: str) -> dict:
|
|
return {"studyschedule": 100, "viewSchedule": 0}
|
|
|
|
async def month_hours_text(self) -> str:
|
|
raise AssertionError("跳过已完成视频时不应刷新学时")
|
|
|
|
|
|
def test_video_skip_already_complete_does_not_refresh() -> None:
|
|
async def main() -> None:
|
|
session = _AlreadyDoneSession()
|
|
global_stop = asyncio.Event()
|
|
user_stop = asyncio.Event()
|
|
watcher = HoursWatcher(session, "u1", 600, user_stop)
|
|
await _study_video(session, "kid", "", 30, global_stop, watcher)
|
|
assert not watcher.reached
|
|
|
|
asyncio.run(main())
|