"""Tests for the `run_sync` orchestrator in `openrun.ingest.garmin_api`. `run_sync` is the auth-free body shared by the CLI and the web "Sync now" button. We stub every network-touching sub-sync so the test verifies only the orchestration contract: which steps run, what the summary holds, that progress is streamed, and that `last_sync_utc` is recorded. """ from __future__ import annotations import openrun.ingest.garmin_api as g from openrun.db import get_state def _stub_subsyncs(monkeypatch, tmp_path): monkeypatch.setattr(g, "sync_activities", lambda conn, **kw: 3) monkeypatch.setattr(g, "backfill_fits", lambda conn, **kw: 2) monkeypatch.setattr(g, "_fit_dir", lambda: tmp_path) for name in ( "sync_steps", "sync_sleep", "sync_stress", "sync_hrv", "sync_intensity_minutes", "sync_resting_hr", "sync_body_battery", ): monkeypatch.setattr(g, name, lambda conn, end, period: 1) def test_run_sync_default_pass(tmp_conn, tmp_path, monkeypatch) -> None: _stub_subsyncs(monkeypatch, tmp_path) msgs: list[str] = [] summary = g.run_sync(tmp_conn, progress=msgs.append) assert summary["activities"] == 3 assert "fit_backfill" not in summary # off by default assert summary["steps"] == 1 and summary["body battery"] == 1 assert any("activities" in m for m in msgs) assert get_state(tmp_conn, "last_sync_utc") is not None def test_run_sync_includes_backfill_when_requested(tmp_conn, tmp_path, monkeypatch) -> None: _stub_subsyncs(monkeypatch, tmp_path) summary = g.run_sync(tmp_conn, fit_backfill=True, progress=lambda _m: None) assert summary["fit_backfill"] == 2 def test_run_sync_skip_activities(tmp_conn, tmp_path, monkeypatch) -> None: _stub_subsyncs(monkeypatch, tmp_path) # If activities ran, the stub would put a key in the summary — assert it didn't. summary = g.run_sync(tmp_conn, skip_activities=True, progress=lambda _m: None) assert "activities" not in summary assert summary["HRV"] == 1 # wellness still runs def test_run_sync_forwards_fit_flag(tmp_conn, tmp_path, monkeypatch) -> None: seen: dict = {} monkeypatch.setattr(g, "sync_activities", lambda conn, **kw: seen.update(kw) or 0) monkeypatch.setattr(g, "_fit_dir", lambda: tmp_path) for name in ( "sync_steps", "sync_sleep", "sync_stress", "sync_hrv", "sync_intensity_minutes", "sync_resting_hr", "sync_body_battery", ): monkeypatch.setattr(g, name, lambda conn, end, period: 0) g.run_sync(tmp_conn, fetch_fit=False, full=True, progress=lambda _m: None) assert seen["fetch_fit"] is False assert seen["full"] is True