screen-locker/screen_locker/tests/test_sync_fixtures.py
Krzysztof kuhy Rudnicki 094a1fe5ed Split _workout_sync.py and _sick_tracker.py
_workout_sync.py (332) -> _sync_client.py   token, Firebase/GitHub client
                            _sync_records.py  payload predicates + log decoding
  _sick_tracker.py (317) -> _sick_budget.py   SickHistory + rolling budget

This one was not a pure move until the fixtures followed it. Three autouse
test guards patch names by module, and all three pointed at _workout_sync:

  isolate_sync_token      SYNC_TOKEN_FILE -> now also on _sync_client
  _no_real_firebase_config CONFIG_FILE    -> now on _sync_client
  _firebase_config helper  (7 call sites) -> now passed _sync_client

With them stale, sync_client built a REAL client: the suite went from 14s to
over 120s and test_workout_sync_part5 asserted against live workout data
pulled from the actual backend. Fixed, the same tests run in 0.7s. Both
modules keep SYNC_TOKEN_FILE so either patch point works, and every relocated
guard target is asserted to exist.

977 tests pass at 100% branch coverage; pylint back to 8.70 after fixing the
findings the split introduced (a dropped `return None` in _parse_iso and
leftover dead imports). `screen_lock --status` unchanged.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EveB813BQ8abAP68VSqZ5Y
2026-08-15 20:30:42 +02:00

69 lines
2.5 KiB
Python

"""Isolation fixture for the phone-workout GitHub sync token.
Split out of ``conftest.py`` (already at the repo's 400-line-per-file cap)
and re-exported there so pytest still picks this up as an autouse fixture
for every test in this directory.
"""
from __future__ import annotations
from typing import TYPE_CHECKING
from unittest.mock import patch
import pytest
if TYPE_CHECKING:
from collections.abc import Iterator
from pathlib import Path
@pytest.fixture(autouse=True)
def isolate_sync_token(tmp_path: Path) -> Iterator[None]:
"""Redirect SYNC_TOKEN_FILE to tmp_path so tests never see a real token.
Without this, any test calling ``_verify_phone_workout`` would fall
through to ``pull_synced_workout()``, which -- if a real
``~/.config/screen_locker/sync_token`` happens to exist on the host --
would make a real GitHub API call. Defaulting to a nonexistent tmp_path
file makes ``read_sync_token()`` return None, the same benign "sync not
configured" state every existing test already assumes.
"""
# read_sync_token moved to _sync_client when _workout_sync was split;
# _workout_sync still re-exports the constant, so both names must be
# redirected or a real token on the host leaks into the tests.
token = tmp_path / "sync_token"
with (
patch("screen_locker._sync_client.SYNC_TOKEN_FILE", token),
patch("screen_locker._workout_sync.SYNC_TOKEN_FILE", token),
):
yield
@pytest.fixture(autouse=True)
def isolate_sync_device_id(tmp_path: Path) -> Iterator[None]:
"""Redirect the persisted device id so tests never mint one for real.
Without this the first test that syncs writes a uuid into the real
``~/.local/share/screen_locker/.device_id``, and this machine's live sync
identity ends up decided by whichever test happened to run first.
"""
with patch(
"screen_locker._device.SYNC_DEVICE_ID_FILE",
tmp_path / ".device_id",
):
yield
@pytest.fixture(autouse=True)
def no_sync_retry_sleep() -> Iterator[None]:
"""Make the sync retry's backoff instant for every test.
``with_sync_retry`` waits 2s + 4s + 8s before giving up, which is right in
production (it covers the network coming up after boot/resume) but would
add ~14s to every test that exercises a sync failure. Tests that care about
the backoff assert on the patched ``sleep`` calls instead -- see
``test_sync_retry.py``.
"""
with patch("screen_locker._sync_retry.sleep"):
yield