feat: 学习状态改为内存快照,页面连接每秒自取并自动重连

This commit is contained in:
2026-11-17 17:36:22 +08:00
parent 42b29fb4d6
commit 4d1a90c321
11 changed files with 675 additions and 256 deletions
+49
View File
@@ -885,6 +885,55 @@ def test_start_saves_the_settings_sent_with_it(monkeypatch) -> None:
assert work_running("6650000146") is False
def test_study_state_is_served_from_memory_until_the_account_changes(monkeypatch) -> None:
import euniv.api as api_module
import euniv.service as service_module
import euniv.study_control as control
monkeypatch.setattr(
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
loads: list[str] = []
original = control.load_state
def counted(employee_id: str) -> dict:
loads.append(employee_id)
return original(employee_id)
monkeypatch.setattr(control, "load_state", counted)
control.forget("6650000146")
first = control.state_for("6650000146")
assert loads == ["6650000146"]
assert first["monthly_target"] == 600
for _ in range(5):
assert control.state_for("6650000146") == first
assert loads == ["6650000146"]
held = control._snaps["6650000146"][1]
for secret in ("password_cipher", "credential_cipher", "proxy_password_cipher"):
assert not hasattr(held, secret)
with SessionLocal() as db:
account = db.get(Account, "6650000146")
assert account is not None
account.monthly_target = 100
db.flush()
db.rollback()
assert control.state_for("6650000146")["monthly_target"] == 600
assert loads == ["6650000146"]
saved = client.put("/api/settings/study", json=_study_body(monthly_target=900))
assert saved.status_code == 200
changed = control.state_for("6650000146")
assert changed["monthly_target"] == 900
assert loads == ["6650000146", "6650000146"]
assert client.get("/api/settings/study-control").json()["monthly_target"] == 900
assert loads == ["6650000146", "6650000146"]
def test_changing_the_course_count_restarts_a_running_study(monkeypatch) -> None:
import euniv.api as api_module
import euniv.autostudy as autostudy_module