refactor(sync): 手动同步与自动同步彻底分开

「历史范围」原本放在设置页,却只对同步页的一个按钮起作用;而同步页最
显眼的主按钮「同步最新数据」写死 2 天,根本不看这个设置。选了「全部
历史」再点主按钮,表现就是应用无视你 —— 这正是反复出现的「只同步下来
两天」。

现在两条链路各管各的:

* 自动同步:只在设置页配置(开关 + 频率),窗口固定 SYNC_DAYS,不再
  读 history_days。措辞也改成「拉取最近几天」,不再暗示会补历史。
* 手动同步:范围就在同步页当场选,紧挨着用它的按钮,并标出每个范围的
  实际代价(自上次同步 / 7 天 / … / 全部历史约 730 天、20-40 分钟)。
  两个按钮合成一个「开始同步」,写死 2 天的那个删掉。

history_days 保留为「上次手动选的范围」,只有同步页读它;默认值改成
-1(自上次同步),对日常使用是正确的起点。

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
ericwyuan
2026-09-01 09:20:52 +08:00
parent 4d55fb4776
commit 526ece7d14
13 changed files with 156 additions and 116 deletions

View File

@@ -187,12 +187,11 @@ class TestEndpoints:
class TestSyncWindowResolution:
"""Which window each account is synced over.
"""Auto-sync must be blind to the manual sync range.
`query_one` returns a dict, so reading the saved 历史范围 as `s[0]` raised
KeyError — swallowed by the per-account handler, which meant every
scheduled tick failed for every account that had ever opened 设置, and
nothing was synced automatically at all.
They used to share `history_days`: one setting on the 设置 page that only
took effect on the 数据同步 page, and that a half-hourly tick also used to
re-pull 730 days with. Separating them is the point.
"""
def _account(self, user, monkeypatch):
@@ -208,34 +207,23 @@ class TestSyncWindowResolution:
monkeypatch.setattr(garmin_svc, "sync_data", record)
return seen
def test_saved_history_days_is_honoured(self, db, user, monkeypatch):
from services import settings as settings_svc
def test_the_default_window_is_used(self, db, user, monkeypatch):
seen = self._account(user, monkeypatch)
settings_svc.save_settings(user["id"], {"historyDays": 90})
results = scheduler.sync_all_accounts()
assert [r["status"] for r in results] == ["success"]
assert seen["days"] == 90
assert seen["days"] == scheduler.SYNC_DAYS
def test_full_history_becomes_the_maximum(self, db, user, monkeypatch):
def test_the_manual_range_is_ignored(self, db, user, monkeypatch):
"""全部历史 on the sync page must not turn every tick into 730 days."""
from services import settings as settings_svc
seen = self._account(user, monkeypatch)
settings_svc.save_settings(user["id"], {"historyDays": 0})
scheduler.sync_all_accounts()
assert seen["days"] == 730
def test_the_scheduled_tick_ignores_the_history_setting(
self, db, user, monkeypatch
):
"""历史范围 is the window for the manual 全量同步. A half-hourly tick
re-pulling 全部历史 is what kept the account in a 429 loop."""
from services import settings as settings_svc
seen = self._account(user, monkeypatch)
settings_svc.save_settings(user["id"], {"historyDays": 0, "autoSync": True})
scheduler.sync_all_accounts(days=scheduler.SYNC_DAYS, respect_schedule=True)
scheduler.sync_all_accounts(respect_schedule=True)
assert seen["days"] == scheduler.SYNC_DAYS
def test_an_explicit_override_still_wins(self, db, user, monkeypatch):
seen = self._account(user, monkeypatch)
scheduler.sync_all_accounts(days=30)
assert seen["days"] == 30