screen-locker/screen_locker/tests/test_manual_sync.py
Krzysztof kuhy Rudnicki 870a65015f Manual-workout phone sync P2: PC ingestion (multi-record + manual branch)
Ingest manual/off-app workouts synced from the phone (or another device) into
workout_log.json so they count toward the weekly-minimum unlock. No phone UI
yet — fully driven by a crafted sync log.

- _workout_sync.py: pull_all_manual_records() merges every device log under the
  sync prefix (phone, pc, …), keeping the highest-HLC copy per stable record id;
  and the session-verify path (_latest_payload) now skips kind="manual_workout"
  records so a manual can never be treated as a StrongLifts session.
- _manual_sync.py (new): reconstruct_draft() rebuilds a ManualWorkoutDraft from a
  synced payload (raw inputs only — never trusting the phone's derived fields);
  ingest_manual_records() re-validates with the same validate_manual_workout the
  local form uses, enforces the rate budget, then HMAC-signs and writes each
  under its own date. Idempotent (dedup by sync_record_id) and non-destructive
  (never clobbers an existing counted day).
- _log_mixin.py: extract write_signed_entry(log_file, date, workout_data) so the
  live save path and ingestion share one date-keyed signer; _load_existing_logs
  now delegates to the shared _read_logs.
- screen_lock.py: run _ingest_synced_manual_workouts() at startup, beside the
  RunnerUp auto-fill, before the lock-decision early-exits.

Trust: a phone can at most add a budget-limited honour-system manual_workout,
never a phone_verified one; the PC re-validates and signs on ingest. Full suite
green at 100% coverage.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PM3auNTifaVgcXWDizZrjH
2026-07-13 11:15:07 +02:00

205 lines
7.6 KiB
Python

"""Tests for ingesting synced manual workouts into workout_log.json."""
# pylint: disable=protected-access
from __future__ import annotations
import json
from typing import TYPE_CHECKING
from unittest.mock import patch
from screen_locker import _manual_sync
from screen_locker._manual_sync import (
ingest_manual_records,
reconstruct_draft,
)
from screen_locker._manual_workout import (
SPORT_OTHER,
SPORT_TABLE_TENNIS,
ManualWorkoutDraft,
build_sync_payload,
manual_sync_record_id,
)
from screen_locker._weekly_check import count_weekly_workouts
if TYPE_CHECKING:
from pathlib import Path
_DATE = "2026-07-13"
_TT_DRAFT = ManualWorkoutDraft(
sport=SPORT_TABLE_TENNIS,
start_time="18:00",
end_time="19:30",
location_name="Solec",
transport_method="bike",
cost="40 PLN",
rpe=6,
went_well="Served consistently and moved feet well",
to_improve="Backhand topspin needs more consistency",
overall_feeling="Felt strong focused and in good rhythm",
matches_won=3,
matches_lost=1,
sets_won=7,
sets_lost=4,
racket="Butterfly",
balls="Nittaku 3-star",
)
_OTHER_DRAFT = ManualWorkoutDraft(
sport=SPORT_OTHER,
start_time="09:00",
end_time="10:00",
location_name="Squash club",
transport_method="car",
cost="30 PLN",
rpe=5,
went_well="Moved well and kept long rallies going",
to_improve="Need to volley earlier off the back wall",
overall_feeling="Tired but satisfied with the effort",
activity_type_other="squash",
activity_details="Full-court squash drills and three practice games total",
equipment="racket, goggles",
)
def _manual_record(draft: ManualWorkoutDraft, date: str = _DATE) -> tuple[str, dict]:
"""Build a (record_id, payload) pair as it would arrive from sync."""
payload = build_sync_payload(draft, date)
return manual_sync_record_id(date, draft.start_time), payload
class TestReconstructDraft:
def test_rebuilds_a_table_tennis_draft(self) -> None:
_, payload = _manual_record(_TT_DRAFT)
draft = reconstruct_draft(payload)
assert draft is not None
assert draft.sport == SPORT_TABLE_TENNIS
assert draft.racket == "Butterfly"
def test_rebuilds_an_other_sport_draft(self) -> None:
_, payload = _manual_record(_OTHER_DRAFT)
draft = reconstruct_draft(payload)
assert draft is not None
assert draft.activity_type_other == "squash"
def test_returns_none_when_a_required_field_is_missing(self) -> None:
_, payload = _manual_record(_TT_DRAFT)
del payload["start_time"]
assert reconstruct_draft(payload) is None
def test_returns_none_on_a_mistyped_field(self) -> None:
_, payload = _manual_record(_TT_DRAFT)
payload["rpe"] = None
assert reconstruct_draft(payload) is None
class TestIngestManualRecords:
def test_ingests_a_valid_record(self, tmp_path: Path) -> None:
log_file = tmp_path / "workout_log.json"
record = _manual_record(_TT_DRAFT)
ingested = ingest_manual_records(log_file, [record], today=_DATE)
assert ingested == [record[0]]
stored = json.loads(log_file.read_text())
workout = stored[_DATE]["workout_data"]
assert workout["type"] == "manual_workout"
assert workout["sync_record_id"] == record[0]
assert count_weekly_workouts(log_file, today=_dt(_DATE)) == 1
def test_is_idempotent(self, tmp_path: Path) -> None:
log_file = tmp_path / "workout_log.json"
record = _manual_record(_TT_DRAFT)
ingest_manual_records(log_file, [record], today=_DATE)
again = ingest_manual_records(log_file, [record], today=_DATE)
assert again == []
def test_skips_a_non_manual_payload(self, tmp_path: Path) -> None:
log_file = tmp_path / "workout_log.json"
assert (
ingest_manual_records(log_file, [("s", {"succeeded": True})], today=_DATE)
== []
)
def test_skips_a_record_without_a_date(self, tmp_path: Path) -> None:
log_file = tmp_path / "workout_log.json"
_, payload = _manual_record(_TT_DRAFT)
del payload["date"]
assert ingest_manual_records(log_file, [("m", payload)], today=_DATE) == []
def test_does_not_clobber_an_existing_counted_day(self, tmp_path: Path) -> None:
log_file = tmp_path / "workout_log.json"
log_file.write_text(
json.dumps(
{_DATE: {"workout_data": {"type": "phone_verified"}}},
)
)
record = _manual_record(_TT_DRAFT)
assert ingest_manual_records(log_file, [record], today=_DATE) == []
# The original phone_verified entry is untouched.
stored = json.loads(log_file.read_text())
assert stored[_DATE]["workout_data"]["type"] == "phone_verified"
def test_skips_a_malformed_payload(self, tmp_path: Path) -> None:
log_file = tmp_path / "workout_log.json"
_, payload = _manual_record(_TT_DRAFT)
payload["rpe"] = None # reconstruct_draft -> None
assert ingest_manual_records(log_file, [("m", payload)], today=_DATE) == []
def test_skips_an_invalid_draft(self, tmp_path: Path) -> None:
log_file = tmp_path / "workout_log.json"
_, payload = _manual_record(_TT_DRAFT)
payload["location_name"] = " " # fails validate_manual_workout
assert ingest_manual_records(log_file, [("m", payload)], today=_DATE) == []
def test_skips_when_budget_exhausted(self, tmp_path: Path) -> None:
log_file = tmp_path / "workout_log.json"
record = _manual_record(_TT_DRAFT)
with patch.object(_manual_sync, "is_budget_exhausted", return_value=True):
assert ingest_manual_records(log_file, [record], today=_DATE) == []
def test_writes_unsigned_entry_when_hmac_key_unavailable(
self, tmp_path: Path
) -> None:
log_file = tmp_path / "workout_log.json"
record = _manual_record(_TT_DRAFT)
with patch("screen_locker._log_mixin.compute_entry_hmac", return_value=None):
ingest_manual_records(log_file, [record], today=_DATE)
stored = json.loads(log_file.read_text())
assert "hmac" not in stored[_DATE]
class TestIngestHelpers:
def test_already_ingested_ignores_non_dict_entries(self) -> None:
logs = {"a": "not-a-dict", "b": {"workout_data": {"sync_record_id": "x"}}}
assert _manual_sync._already_ingested(logs, "x") is True
assert _manual_sync._already_ingested(logs, "y") is False
def test_date_has_counted_workout_variants(self) -> None:
logs = {
"d1": {"workout_data": {"type": "manual_workout"}},
"d2": {"workout_data": {"type": "nope"}},
"d3": "not-a-dict",
}
assert _manual_sync._date_has_counted_workout(logs, "d1") is True
assert _manual_sync._date_has_counted_workout(logs, "d2") is False
assert _manual_sync._date_has_counted_workout(logs, "d3") is False
assert _manual_sync._date_has_counted_workout(logs, "missing") is False
class TestReadLogs:
def test_missing_file_returns_empty(self, tmp_path: Path) -> None:
from screen_locker._log_mixin import _read_logs
assert _read_logs(tmp_path / "nope.json") == {}
def test_corrupt_file_returns_empty(self, tmp_path: Path) -> None:
from screen_locker._log_mixin import _read_logs
log_file = tmp_path / "workout_log.json"
log_file.write_text("{not valid json")
assert _read_logs(log_file) == {}
def _dt(date: str) -> object:
from datetime import datetime, timezone
return datetime.strptime(date, "%Y-%m-%d").replace(tzinfo=timezone.utc)