mirror of
https://github.com/kuhyx/screen-locker.git
synced 2026-11-10 20:27:13 +01:00
`requires-python` and the CI pin already said 3.14, but ruff, mypy and pylint were still grading against 3.10 -- three tools judging an interpreter nothing runs any more, and ruff withholding the pyupgrade fixes that only become safe at the newer target. `pin_ci_python.py` rewrites exactly these three knobs. What the stale target was hiding: 118 sites on `datetime.timezone.utc` instead of `datetime.UTC` (UP017), two classes still written with `Generic[...]` rather than PEP 695 syntax, and ruff's stable formatter applying PEP 758 -- `except (A, B):` becomes `except A, B:`. That last one makes the source 3.14-only at the syntax level, which is what `requires-python = ">=3.14"` already promised; kuhy chose it deliberately over holding ruff a release back (2026-08-29). The PEP 695 fix needed a hand: ruff rewrote `class WidgetGroup(Generic[_W])` to `class WidgetGroup[W: tk.Misc]` but left the body annotated with the module-level `_W`, a *different* type variable -- mypy caught it as "a function returning TypeVar should receive at least one argument containing the same TypeVar". Renamed the body to the new parameter, gave `FrameGroup.__init__` the concrete `list[tk.Frame]` it always wrapped, and deleted the two now-dead module-level TypeVars. ruff, ruff-format, mypy --strict and both pylint passes clean at the new target. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SfnJmxxsZ534AKk16rC2Gr
168 lines
5.9 KiB
Python
168 lines
5.9 KiB
Python
"""Tests for pushing the PC's workouts (from log.json) to the sync repo."""
|
|
# pylint: disable=protected-access
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import UTC, datetime
|
|
import json
|
|
from typing import TYPE_CHECKING
|
|
from unittest.mock import patch
|
|
|
|
from screen_locker._manual_push import (
|
|
_decode_log,
|
|
_encode_log,
|
|
_entry_wall_ms,
|
|
records_from_workout_log,
|
|
)
|
|
|
|
if TYPE_CHECKING:
|
|
from pathlib import Path
|
|
|
|
|
|
_MANUAL = {
|
|
"type": "manual_workout",
|
|
"source": "table tennis at Solec",
|
|
"start_time": "18:00",
|
|
"rpe": 6,
|
|
}
|
|
_RUN = {
|
|
"type": "runnerup_verified",
|
|
"source": "Running: 9.8 km in 55 min",
|
|
"distance_km": 9.8,
|
|
}
|
|
|
|
|
|
def _write_log(log_file: Path, data: dict) -> None:
|
|
log_file.write_text(json.dumps(data))
|
|
|
|
|
|
def _entry(workout_data: dict, ts: str, workout_id: str | None = None) -> dict:
|
|
entry: dict = {"timestamp": ts, "workout_data": workout_data}
|
|
if workout_id is not None:
|
|
entry["workout_id"] = workout_id
|
|
return entry
|
|
|
|
|
|
class TestEncodeDecodeLog:
|
|
"""The crdt log blob round-trips."""
|
|
|
|
def test_round_trips(self, tmp_path: Path) -> None:
|
|
"""Round trips."""
|
|
log_file = tmp_path / "log.json"
|
|
_write_log(
|
|
log_file, {"2026-07-13": [_entry(_RUN, "2026-07-13T10:00:00+00:00")]}
|
|
)
|
|
log = records_from_workout_log(log_file)
|
|
assert _decode_log(_encode_log(log)).keys() == log.keys()
|
|
|
|
|
|
class TestEntryWallMs:
|
|
"""The HLC clock is derived from the entry's own timestamp — stably."""
|
|
|
|
def test_uses_the_entry_timestamp(self) -> None:
|
|
"""Uses the entry timestamp."""
|
|
expected = int(datetime(2026, 7, 13, 12, tzinfo=UTC).timestamp() * 1000)
|
|
ms = _entry_wall_ms({"timestamp": "2026-07-13T12:00:00+00:00"}, "2026-07-13")
|
|
assert ms == expected
|
|
|
|
def test_unparsable_timestamp_falls_back_to_midnight(self) -> None:
|
|
"""Unparsable timestamp falls back to midnight."""
|
|
ms = _entry_wall_ms({"timestamp": "not-a-time"}, "2026-07-13")
|
|
assert ms == _entry_wall_ms({"timestamp": "2026-07-13T00:00:00+00:00"}, "x")
|
|
|
|
def test_missing_timestamp_falls_back_to_midnight(self) -> None:
|
|
"""Missing timestamp falls back to midnight."""
|
|
expected = int(datetime(2026, 7, 13, tzinfo=UTC).timestamp() * 1000)
|
|
assert _entry_wall_ms({}, "2026-07-13") == expected
|
|
|
|
def test_fallback_is_stable_across_calls(self) -> None:
|
|
"""The fallback must not be 'now' — that would churn the repo."""
|
|
assert _entry_wall_ms({}, "2026-07-13") == _entry_wall_ms({}, "2026-07-13")
|
|
|
|
|
|
class TestRecordsFromWorkoutLog:
|
|
"""log.json is the single source of truth for what gets pushed."""
|
|
|
|
def test_derives_a_record_per_counted_workout(self, tmp_path: Path) -> None:
|
|
"""Derives a record per counted workout."""
|
|
log_file = tmp_path / "log.json"
|
|
_write_log(
|
|
log_file,
|
|
{
|
|
"2026-07-13": [
|
|
_entry(_MANUAL, "2026-07-13T07:25:12+00:00"),
|
|
_entry(
|
|
_RUN,
|
|
"2026-07-13T23:10:00+00:00",
|
|
workout_id="runnerup_verified:2026-07-13",
|
|
),
|
|
]
|
|
},
|
|
)
|
|
log = records_from_workout_log(log_file)
|
|
assert set(log) == {"manual:2026-07-13T18:00", "runnerup_verified:2026-07-13"}
|
|
|
|
def test_payload_carries_kind_and_date(self, tmp_path: Path) -> None:
|
|
"""Payload carries kind and date."""
|
|
log_file = tmp_path / "log.json"
|
|
_write_log(
|
|
log_file, {"2026-07-13": [_entry(_RUN, "2026-07-13T10:00:00+00:00")]}
|
|
)
|
|
payload, _hlc = records_from_workout_log(log_file)[
|
|
"runnerup_verified:2026-07-13"
|
|
].fields["payload"]
|
|
assert payload["kind"] == "runnerup_verified"
|
|
assert payload["date"] == "2026-07-13"
|
|
assert payload["distance_km"] == 9.8
|
|
|
|
def test_legacy_entry_without_workout_id_still_publishes(
|
|
self, tmp_path: Path
|
|
) -> None:
|
|
"""Entries predating workout_id get the id they would have had."""
|
|
log_file = tmp_path / "log.json"
|
|
_write_log(
|
|
log_file, {"2026-07-13": [_entry(_MANUAL, "2026-07-13T07:25+00:00")]}
|
|
)
|
|
assert "manual:2026-07-13T18:00" in records_from_workout_log(log_file)
|
|
|
|
def test_uncounted_types_are_not_pushed(self, tmp_path: Path) -> None:
|
|
"""Uncounted types are not pushed."""
|
|
log_file = tmp_path / "log.json"
|
|
_write_log(
|
|
log_file,
|
|
{"2026-07-13": [_entry({"type": "sick_day"}, "2026-07-13T07:00:00+00:00")]},
|
|
)
|
|
assert records_from_workout_log(log_file) == {}
|
|
|
|
def test_non_dict_workout_data_is_skipped(self, tmp_path: Path) -> None:
|
|
"""Non dict workout data is skipped."""
|
|
log_file = tmp_path / "log.json"
|
|
_write_log(
|
|
log_file,
|
|
{
|
|
"2026-07-13": [
|
|
{"timestamp": "2026-07-13T07:00:00+00:00", "workout_data": "x"}
|
|
]
|
|
},
|
|
)
|
|
assert records_from_workout_log(log_file) == {}
|
|
|
|
def test_entry_without_derivable_id_is_skipped(self, tmp_path: Path) -> None:
|
|
"""A counted type with no id can't be deduped, so it isn't synced."""
|
|
log_file = tmp_path / "log.json"
|
|
_write_log(
|
|
log_file, {"2026-07-13": [_entry(_MANUAL, "2026-07-13T07:00:00+00:00")]}
|
|
)
|
|
with patch("screen_locker._manual_push._derive_workout_id", return_value=None):
|
|
assert records_from_workout_log(log_file) == {}
|
|
|
|
def test_is_deterministic_across_calls(self, tmp_path: Path) -> None:
|
|
"""Same log → identical records+HLCs, so a re-push is a no-op."""
|
|
log_file = tmp_path / "log.json"
|
|
_write_log(
|
|
log_file, {"2026-07-13": [_entry(_RUN, "2026-07-13T10:00:00+00:00")]}
|
|
)
|
|
assert _encode_log(records_from_workout_log(log_file)) == _encode_log(
|
|
records_from_workout_log(log_file)
|
|
)
|