fix(sync): 「全部历史」真的拉全部历史,自动同步不再每次静默失败
四个独立的 bug 叠在一起,表现为「只同步两天、没有进度」:
* 前端 `...(days ? { days } : {})` 把 days=0 当成未传。「全部历史」
存的就是 0,请求体里根本没有 days,后端退回 7 天默认值。
* scheduler 用 `s[0]` 读 query_one 返回的 dict,抛 KeyError 后被
per-account 的 except 吞掉。只要用户存过一次设置,每 30 分钟的
自动同步就一次都没成功过——库里那 2 天全是手动点出来的。
* 增量同步查 `health_daily`(表其实叫 health_data),后台线程直接
死掉,状态永远卡在 syncing,进度条不动。
* UI 完全不看 /sync 的返回值,rate_limited 时按钮点了没反应;轮询
结束时又把 rate_limited 归进 else 分支报「同步完成」。
顺带:
* 日循环遇到 429 立即退避并保留已拉到的天数,而不是当成「跳过一天」
继续往下捶 700 天——这正是之前限流死循环的来源之一。
* 定时循环显式传 SYNC_DAYS。历史范围按 UI 文案只描述手动全量同步,
让半小时一次的 tick 重拉 730 天必然把限流撞得更深。
* 短同步逐天上报进度(原来每 5 天一次,7 天的同步全程停在 0)。
* /sync 路由重复解析 body,空 body 会 None.get 崩。
* 4 个 StubGarth 缺 configure(),7 个测试在此之前一直是红的。
新增 deploy/push.sh:NAS 只认密码,脚本开一个 ssh 复用连接,密码只
输一次,后面推送 / 重启 / 健康检查全走它。不碰 .env、.venv 和数据库。
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -184,3 +184,58 @@ class TestEndpoints:
|
||||
r = client.get("/api/garmin/auto-sync", headers=auth)
|
||||
assert r.status_code == 200
|
||||
assert "intervalSeconds" in r.get_json()
|
||||
|
||||
|
||||
class TestSyncWindowResolution:
|
||||
"""Which window each account is synced over.
|
||||
|
||||
`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.
|
||||
"""
|
||||
|
||||
def _account(self, user, monkeypatch):
|
||||
from services import garmin as garmin_svc
|
||||
|
||||
garmin_svc.save_token(user["id"], "t1")
|
||||
seen = {}
|
||||
|
||||
def record(uid, creds, days=None, client=None):
|
||||
seen["days"] = days
|
||||
return {"status": "success", "recordsSynced": 0}
|
||||
|
||||
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
|
||||
|
||||
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
|
||||
|
||||
def test_full_history_becomes_the_maximum(self, db, user, monkeypatch):
|
||||
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)
|
||||
assert seen["days"] == scheduler.SYNC_DAYS
|
||||
|
||||
Reference in New Issue
Block a user