mirror of
https://github.com/This-weiyuqing/mi-fitness-health.git
synced 2026-10-18 03:03:49 +08:00
feat: improve cloud sync reliability and background status
This commit is contained in:
1 parent
ebc94b50e9
commit
7fcd06980f
12 files changed
+711
-82
No files matched your search
@@ -1,6 +1,9 @@
|
||||
import pytest
|
||||
|
||||
from mi_fitness_mcp.adapters.mi_fitness_cloud import MiFitnessCloudAdapter
|
||||
from mi_fitness_mcp.adapters.mi_fitness_cloud import (
|
||||
MiFitnessCloudAdapter,
|
||||
_is_authentication_error,
|
||||
)
|
||||
|
||||
|
||||
async def _collect(async_iterable):
|
||||
@@ -29,6 +32,7 @@ def test_record_datetime_uses_zone_offset():
|
||||
dt = adapter._record_datetime({"time": 0, "zone_offset": 10800})
|
||||
assert dt.isoformat().startswith("1970-01-01T03:00:00")
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_iter_daily_activity_aggregates_steps_and_calories(monkeypatch):
|
||||
adapter = MiFitnessCloudAdapter(user_id="u1", pass_token="p1")
|
||||
@@ -63,3 +67,36 @@ async def test_iter_daily_activity_aggregates_steps_and_calories(monkeypatch):
|
||||
assert items[0].steps == 30
|
||||
assert items[0].distance_m == 24
|
||||
assert items[0].active_kcal == 12
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_fetch_key_rejects_repeated_pagination_cursor(monkeypatch):
|
||||
adapter = MiFitnessCloudAdapter(user_id="u1", pass_token="p1", region="cn")
|
||||
adapter._client = object()
|
||||
|
||||
async def fake_request(base_url, api_path, payload):
|
||||
return {"data_list": [], "has_more": True, "next_key": "same"}
|
||||
|
||||
monkeypatch.setattr(adapter, "_request", fake_request)
|
||||
with pytest.raises(RuntimeError, match="cursor loop"):
|
||||
await adapter._fetch_key("steps", "2026-07-06", "2026-07-12")
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_connect_failure_closes_client(monkeypatch):
|
||||
adapter = MiFitnessCloudAdapter(user_id="u1", pass_token="bad", region="cn")
|
||||
|
||||
async def failed_login(user_id, pass_token):
|
||||
raise RuntimeError("invalid credentials")
|
||||
|
||||
monkeypatch.setattr(adapter, "_login_with_token", failed_login)
|
||||
assert await adapter.connect() is False
|
||||
assert adapter._client is None
|
||||
assert "invalid credentials" in adapter.last_error
|
||||
|
||||
|
||||
def test_authentication_error_detection():
|
||||
assert _is_authentication_error(401, "denied")
|
||||
assert _is_authentication_error(0, "session expired")
|
||||
assert _is_authentication_error(-10001, "unknown")
|
||||
assert not _is_authentication_error(500, "temporary server failure")
|
||||
Reference in new issue
Block a user