From 5e5f3bd1bd0849d1312b53a882d1790bdac53716 Mon Sep 17 00:00:00 2001 From: Nixevol Date: Mon, 16 Nov 2026 02:29:50 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=AE=A9=E4=BE=9D=E8=B5=96=E5=BD=93?= =?UTF-8?q?=E5=89=8D=E6=97=A5=E6=9C=9F=E7=9A=84=E6=B5=8B=E8=AF=95=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E5=9B=BA=E5=AE=9A=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/tests/test_api.py | 30 +++++++++++++++++++++++++++--- backend/tests/test_autostudy.py | 5 +++-- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/backend/tests/test_api.py b/backend/tests/test_api.py index fd1260c..e38fc6d 100644 --- a/backend/tests/test_api.py +++ b/backend/tests/test_api.py @@ -1,4 +1,5 @@ import json +from datetime import datetime import pytest from fastapi.testclient import TestClient @@ -577,7 +578,20 @@ def test_user_can_set_monthly_study_start() -> None: assert bad.status_code == 400 -def test_user_can_start_and_stop_study_immediately() -> None: +def _freeze_beijing(monkeypatch, year: int, month: int, day: int) -> None: + from euniv.study_window import CN_TZ, as_cn + + moment = datetime(year, month, day, 12, 0, tzinfo=CN_TZ) + + def frozen(now=None): + if now is not None: + return as_cn(now) + return moment + + monkeypatch.setattr("euniv.study_window.as_cn", frozen) + + +def test_user_can_start_and_stop_study_immediately(monkeypatch) -> None: import euniv.api as api_module import euniv.service as service_module @@ -585,6 +599,7 @@ def test_user_can_start_and_stop_study_immediately() -> None: db, employee_id, password, FakeClient, manual=manual, auto_attempts=auto_attempts ) client = TestClient(api_module.app) + _freeze_beijing(monkeypatch, 2026, 11, 15) 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") @@ -629,7 +644,7 @@ def test_study_control_stays_stopped_when_month_hours_met() -> None: assert started.json()["paused"] is False -def test_study_control_websocket_sends_snapshot() -> None: +def test_study_control_websocket_sends_snapshot(monkeypatch) -> None: import euniv.api as api_module import euniv.service as service_module from starlette.websockets import WebSocketDisconnect @@ -638,6 +653,7 @@ def test_study_control_websocket_sends_snapshot() -> None: db, employee_id, password, FakeClient, manual=manual, auto_attempts=auto_attempts ) client = TestClient(api_module.app) + _freeze_beijing(monkeypatch, 2026, 11, 15) with pytest.raises(WebSocketDisconnect) as closed: with client.websocket_connect("/api/settings/study-control/ws") as ws: ws.receive_json() @@ -691,7 +707,7 @@ def _set_hours_source(value: str) -> None: db.commit() -def test_home_and_history_hours_follow_source() -> None: +def test_home_and_history_hours_follow_source(monkeypatch) -> None: import euniv.api as api_module import euniv.service as service_module @@ -704,6 +720,14 @@ def test_home_and_history_hours_follow_source() -> None: api_module.fetch_study_history = lambda db, employee_id: service_module.fetch_study_history( db, employee_id, FakeClient ) + from euniv.client import history_hour_totals + from euniv.study_window import CN_TZ + + frozen = datetime(2026, 11, 15, 12, 0, tzinfo=CN_TZ) + monkeypatch.setattr( + "euniv.service.history_hour_totals", + lambda items, now=None: history_hour_totals(items, frozen), + ) client = TestClient(api_module.app) assert client.post("/api/login", json={"employee_id": "6650000146", "password": "secret-pass"}).status_code == 200 diff --git a/backend/tests/test_autostudy.py b/backend/tests/test_autostudy.py index 462b942..e7837b1 100644 --- a/backend/tests/test_autostudy.py +++ b/backend/tests/test_autostudy.py @@ -395,12 +395,13 @@ def test_study_window_clamps_missing_day() -> None: def test_immediate_start_skips_monthly_window() -> None: + frozen = datetime(2026, 11, 15, 12, 0, tzinfo=CN_TZ) account = _Start(28, 23, 59, 59) - assert study_eligible(account) is False + assert study_eligible(account, frozen) is False start_now(account) assert account.study_paused is False assert account.study_ignore_window is True - assert study_eligible(account) is True + assert study_eligible(account, frozen) is True stop_now(account, datetime(2026, 11, 15, 12, 0, tzinfo=CN_TZ)) assert account.study_paused is True assert account.study_stop_month == "2026-11"