feat: 立即开始停止学习并整理学时历史与界面
This commit is contained in:
@@ -445,6 +445,7 @@ def test_check_skips_empty_credential_and_updates_saved_one() -> None:
|
||||
"last_refresh_at",
|
||||
"credential_valid",
|
||||
"month_hours",
|
||||
"monthly_target",
|
||||
"has_proxy",
|
||||
"proxy",
|
||||
"study_start",
|
||||
@@ -453,6 +454,9 @@ def test_check_skips_empty_credential_and_updates_saved_one() -> None:
|
||||
"study_start_minute",
|
||||
"study_start_second",
|
||||
"course_workers",
|
||||
"study_paused",
|
||||
"study_ignore_window",
|
||||
"study_started",
|
||||
}
|
||||
assert saved["has_credential"] is True
|
||||
assert saved["refresh_paused"] is False
|
||||
@@ -791,3 +795,60 @@ def test_admin_can_turn_off_proxy_with_scheme() -> None:
|
||||
detail = client.get("/api/admin/users/off-user/proxy")
|
||||
assert detail.json()["enabled"] is False
|
||||
assert detail.json()["scheme"] == "off"
|
||||
|
||||
|
||||
def test_admin_can_start_and_stop_study_for_selected_users() -> None:
|
||||
_add_account("run-user")
|
||||
_add_account("wait-user")
|
||||
api = _app()
|
||||
client = TestClient(api.app)
|
||||
assert client.post("/api/login", json={"employee_id": "admin", "password": INITIAL_PASSWORD}).status_code == 200
|
||||
started = client.post("/api/admin/users/study", json={"employee_ids": ["run-user", "wait-user"], "action": "start"})
|
||||
assert started.status_code == 200
|
||||
assert started.json()["count"] == 2
|
||||
listed = client.get("/api/admin/users").json()
|
||||
one = next(item for item in listed if item["employee_id"] == "run-user")
|
||||
two = next(item for item in listed if item["employee_id"] == "wait-user")
|
||||
assert one["study_started"] is True
|
||||
assert two["study_paused"] is False
|
||||
stopped = client.post("/api/admin/users/study", json={"employee_ids": ["run-user"], "action": "stop"})
|
||||
assert stopped.status_code == 200
|
||||
listed = client.get("/api/admin/users").json()
|
||||
one = next(item for item in listed if item["employee_id"] == "run-user")
|
||||
two = next(item for item in listed if item["employee_id"] == "wait-user")
|
||||
assert one["study_started"] is False
|
||||
assert one["study_paused"] is True
|
||||
assert two["study_started"] is True
|
||||
bad = client.post("/api/admin/users/study", json={"employee_ids": ["run-user"], "action": "pause"})
|
||||
assert bad.status_code == 400
|
||||
|
||||
|
||||
def test_admin_can_save_study_settings_in_one_request() -> None:
|
||||
_add_account("set-user")
|
||||
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/users/study-settings",
|
||||
json={
|
||||
"employee_ids": ["set-user"],
|
||||
"monthly_target": 120,
|
||||
"day": 8,
|
||||
"hour": 9,
|
||||
"minute": 10,
|
||||
"second": 11,
|
||||
"workers": 2,
|
||||
"scheme": "http",
|
||||
"host": "10.0.0.8",
|
||||
"port": 8080,
|
||||
"username": "",
|
||||
"password": "",
|
||||
},
|
||||
)
|
||||
assert saved.status_code == 200
|
||||
listed = client.get("/api/admin/users").json()
|
||||
one = next(item for item in listed if item["employee_id"] == "set-user")
|
||||
assert one["monthly_target"] == 120
|
||||
assert one["study_start"] == "每月8日 09:10:11"
|
||||
assert one["course_workers"] == 2
|
||||
assert one["proxy"] == "HTTP 10.0.0.8:8080"
|
||||
|
||||
@@ -65,6 +65,16 @@ class FakeClient:
|
||||
"unionId": credential["ucs_union_id"],
|
||||
}
|
||||
|
||||
def study_history(self) -> list[dict]:
|
||||
return [
|
||||
{
|
||||
"title": "ZXCTN-A-CH-EVPN原理及配置介绍(IPRAN)",
|
||||
"minutes": 2,
|
||||
"studied_at": "2026-11-14 23:55",
|
||||
"path": "/kng/view/document/125c7576c22e4ae7be92ce005171facb.html",
|
||||
}
|
||||
]
|
||||
|
||||
def home_snapshot(self) -> dict:
|
||||
if self._token == "yxt-1":
|
||||
self._token = "yxt-2"
|
||||
@@ -123,6 +133,8 @@ def test_login_stores_password_and_credential_only() -> None:
|
||||
"study_start_second",
|
||||
"course_workers",
|
||||
"use_proxy_pool",
|
||||
"study_paused",
|
||||
"study_ignore_window",
|
||||
}
|
||||
|
||||
home = client.get("/api/home")
|
||||
@@ -538,6 +550,82 @@ def test_user_can_set_monthly_study_start() -> None:
|
||||
assert bad.status_code == 400
|
||||
|
||||
|
||||
def test_user_can_start_and_stop_study_immediately() -> 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
|
||||
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
|
||||
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
|
||||
stopped = client.post("/api/settings/study-control", json={"action": "stop"})
|
||||
assert stopped.status_code == 200
|
||||
assert stopped.json()["started"] is False
|
||||
assert stopped.json()["paused"] is True
|
||||
assert stopped.json()["ignore_window"] is False
|
||||
bad = client.post("/api/settings/study-control", json={"action": "pause"})
|
||||
assert bad.status_code == 400
|
||||
|
||||
|
||||
def test_user_can_read_study_history() -> 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_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
|
||||
history = client.get("/api/home/history")
|
||||
assert history.status_code == 200
|
||||
body = history.json()
|
||||
assert body["total"] == 1
|
||||
assert body["items"][0]["title"].startswith("ZXCTN")
|
||||
assert body["items"][0]["minutes"] == 2
|
||||
|
||||
|
||||
def test_study_history_parses_period_table() -> None:
|
||||
from euniv.client import EunivClient
|
||||
|
||||
html = """
|
||||
<table class="table mt5 mb10">
|
||||
<tr><th>来源</th><th>学时</th><th>日期</th></tr>
|
||||
<tr class="alter">
|
||||
<td class="text-left" title="ZXCTN-A-CH-EVPN原理及配置介绍(IPRAN)">
|
||||
ZXCTN-A-CH-EVPN原理及配置介绍(IPRAN)
|
||||
<span onclick="GoToSource('/kng/view/document/125c7576c22e4ae7be92ce005171facb.html','SingleStudy','125c7576-c22e-4ae7-be92-ce005171facb')"
|
||||
class='text-info hand '>[详情]</span>
|
||||
</td>
|
||||
<td class="text-center">2</td>
|
||||
<td class="text-center">2026-11-14 23:55</td>
|
||||
</tr>
|
||||
</table>
|
||||
"""
|
||||
client = EunivClient.__new__(EunivClient)
|
||||
client._study_period_page = lambda: html # type: ignore[method-assign]
|
||||
rows = client._study_history_page()
|
||||
assert rows == [
|
||||
{
|
||||
"title": "ZXCTN-A-CH-EVPN原理及配置介绍(IPRAN)",
|
||||
"minutes": 2,
|
||||
"studied_at": "2026-11-14 23:55",
|
||||
"path": "/kng/view/document/125c7576c22e4ae7be92ce005171facb.html",
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
def test_exam_list_rejects_unrecognized_page() -> None:
|
||||
from euniv.client import EunivClient
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ from datetime import datetime
|
||||
from euniv.autostudy import CourseCursor, HoursWatcher, _parse_minutes, _study_video, wait_stop
|
||||
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
|
||||
from euniv.study_window import CN_TZ, StudyWindowError, validate_start, window_open
|
||||
|
||||
|
||||
@@ -172,6 +173,8 @@ class _Start:
|
||||
self.study_start_hour = hour
|
||||
self.study_start_minute = minute
|
||||
self.study_start_second = second
|
||||
self.study_paused = False
|
||||
self.study_ignore_window = False
|
||||
|
||||
|
||||
def test_study_window_opens_after_configured_time() -> None:
|
||||
@@ -188,6 +191,19 @@ def test_study_window_clamps_missing_day() -> None:
|
||||
assert window_open(account, datetime(2026, 2, 28, 16, 0, 0, tzinfo=CN_TZ)) is True
|
||||
|
||||
|
||||
def test_immediate_start_skips_monthly_window() -> None:
|
||||
account = _Start(28, 23, 59, 59)
|
||||
assert study_eligible(account) is False
|
||||
start_now(account)
|
||||
assert account.study_paused is False
|
||||
assert account.study_ignore_window is True
|
||||
assert study_eligible(account) is True
|
||||
stop_now(account)
|
||||
assert account.study_paused is True
|
||||
assert account.study_ignore_window is False
|
||||
assert study_eligible(account) is False
|
||||
|
||||
|
||||
def test_study_window_rejects_invalid_parts() -> None:
|
||||
with pytest.raises(StudyWindowError):
|
||||
validate_start(32, 0, 0, 0)
|
||||
|
||||
@@ -7,7 +7,7 @@ from euniv.db import SessionLocal
|
||||
from euniv.models import Account, ProxyNode
|
||||
from euniv.proxy import apply as apply_proxy
|
||||
from euniv.proxy import bind_url, is_proxy_failure, view_of
|
||||
from euniv.proxy_pool import acquire, mark_failed, page, refresh_once, release
|
||||
from euniv.proxy_pool import PROBE_URLS, acquire, mark_failed, page, probe, refresh_once, release
|
||||
from euniv.proxy_pool import _holds
|
||||
|
||||
INITIAL_PASSWORD = "242520"
|
||||
@@ -189,6 +189,57 @@ def test_failed_proxy_is_not_reprobed(monkeypatch) -> None:
|
||||
assert node.status == "dead"
|
||||
|
||||
|
||||
def test_probe_averages_euniv_related_sites(monkeypatch) -> None:
|
||||
seen: list[str] = []
|
||||
|
||||
class FakeResponse:
|
||||
status_code = 200
|
||||
|
||||
class FakeClient:
|
||||
def __init__(self, **_kwargs) -> None:
|
||||
return None
|
||||
|
||||
def __enter__(self):
|
||||
return self
|
||||
|
||||
def __exit__(self, *_args) -> bool:
|
||||
return False
|
||||
|
||||
def get(self, url: str):
|
||||
seen.append(url)
|
||||
return FakeResponse()
|
||||
|
||||
ticks = iter([0.0, 0.12, 0.12, 0.34, 0.34, 0.64])
|
||||
monkeypatch.setattr("euniv.proxy_pool.httpx.Client", FakeClient)
|
||||
monkeypatch.setattr("euniv.proxy_pool.time.perf_counter", lambda: next(ticks))
|
||||
ok, latency, reason = probe("http", "10.0.0.9", 8080)
|
||||
assert seen == list(PROBE_URLS)
|
||||
assert ok is True
|
||||
assert latency == 213
|
||||
assert reason == ""
|
||||
|
||||
|
||||
def test_admin_can_probe_one_and_all_proxies(monkeypatch) -> None:
|
||||
import euniv.api as api_module
|
||||
|
||||
_holds.clear()
|
||||
one = _node("10.0.0.7", 8007)
|
||||
two = _node("10.0.0.8", 8008, status="new")
|
||||
monkeypatch.setattr("euniv.proxy_pool.probe", lambda scheme, host, port: (True, 321 if host.endswith("7") else 654, ""))
|
||||
client = TestClient(api_module.app)
|
||||
assert client.post("/api/login", json={"employee_id": "admin", "password": INITIAL_PASSWORD}).status_code == 200
|
||||
single = client.post(f"/api/admin/proxies/{one.id}/probe")
|
||||
assert single.status_code == 200
|
||||
assert single.json()["latency_ms"] == 321
|
||||
assert single.json()["status"] == "alive"
|
||||
bulk = client.post("/api/admin/proxies/probe", json={"ids": [two.id]})
|
||||
assert bulk.status_code == 200
|
||||
assert bulk.json()["count"] == 1
|
||||
assert bulk.json()["items"][0]["latency_ms"] == 654
|
||||
missing = client.post("/api/admin/proxies/99999/probe")
|
||||
assert missing.status_code == 404
|
||||
|
||||
|
||||
def test_is_proxy_failure_detects_connect_errors() -> None:
|
||||
assert is_proxy_failure(httpx.ConnectError("boom"))
|
||||
assert is_proxy_failure(httpx.ConnectTimeout("late"))
|
||||
|
||||
Reference in New Issue
Block a user