mirror of
https://github.com/kuhyx/screen-locker.git
synced 2026-11-10 18:07:22 +01:00
Some checks are pending
pre-commit / pre-commit (push) Waiting to run
Tests / test (3.10) (push) Waiting to run
Tests / test (3.11) (push) Waiting to run
Tests / test (3.12) (push) Waiting to run
Tests / lock-popup-safety (push) Waiting to run
release-apk / release (APK) (push) Waiting to run
sync_log's eleven flat keywords became SyncTarget / LogCodec / RevisionTracking upstream. Call sites and test doubles follow; the hook's own pinned crdt-sync moves to v0.9.0 too, so the gate checks against the API the code actually imports rather than an older tag. No behaviour change. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01VCucBhUkHBBHHbsY7bSdGV
170 lines
6.3 KiB
Python
170 lines
6.3 KiB
Python
"""Tests for push_pc_workouts.
|
|
|
|
Split out of test_manual_push.py for the 250-line cap; the shared _entry /
|
|
_write_log helpers stay in that module and are imported here.
|
|
"""
|
|
# pylint: disable=protected-access
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import TYPE_CHECKING
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
from crdt_sync import GitHubSyncError, RepoNotFoundError
|
|
|
|
from screen_locker import _manual_push
|
|
from screen_locker._manual_push import (
|
|
PushResult,
|
|
push_pc_workouts,
|
|
)
|
|
from screen_locker.tests.test_manual_push import _entry, _write_log
|
|
|
|
if TYPE_CHECKING:
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
_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,
|
|
}
|
|
|
|
|
|
class TestPushPcWorkouts:
|
|
"""Every outcome is reported — never a silent no-op."""
|
|
|
|
def test_no_token_reports_why(self, tmp_path: Path) -> None:
|
|
"""No token reports why."""
|
|
log_file = tmp_path / "log.json"
|
|
_write_log(
|
|
log_file, {"2026-07-13": [_entry(_RUN, "2026-07-13T10:00:00+00:00")]}
|
|
)
|
|
with patch.object(_manual_push, "read_sync_token", return_value=None):
|
|
result = push_pc_workouts(log_file)
|
|
assert result == PushResult(pushed=False, record_count=0, reason=result.reason)
|
|
assert "no sync token" in result.reason
|
|
|
|
def test_empty_log_reports_why(self, tmp_path: Path) -> None:
|
|
"""Empty log reports why."""
|
|
log_file = tmp_path / "log.json"
|
|
_write_log(log_file, {})
|
|
with patch.object(_manual_push, "read_sync_token", return_value="t"):
|
|
result = push_pc_workouts(log_file)
|
|
assert result.pushed is False
|
|
assert result.record_count == 0
|
|
assert "no counted workouts" in result.reason
|
|
|
|
def test_sync_error_reports_why(self, tmp_path: Path) -> None:
|
|
"""Sync error reports why."""
|
|
log_file = tmp_path / "log.json"
|
|
_write_log(
|
|
log_file, {"2026-07-13": [_entry(_RUN, "2026-07-13T10:00:00+00:00")]}
|
|
)
|
|
with (
|
|
patch.object(_manual_push, "read_sync_token", return_value="t"),
|
|
patch.object(_manual_push, "GitHubSyncClient", MagicMock()),
|
|
patch.object(
|
|
_manual_push, "sync_log", side_effect=GitHubSyncError("403 forbidden")
|
|
),
|
|
):
|
|
result = push_pc_workouts(log_file)
|
|
assert result.pushed is False
|
|
assert result.record_count == 1
|
|
assert "403 forbidden" in result.reason
|
|
|
|
def test_network_error_is_not_blamed_on_the_token(
|
|
self, tmp_path: Path, caplog: pytest.LogCaptureFixture
|
|
) -> None:
|
|
"""A network failure must not be reported as a permissions problem.
|
|
|
|
Regression guard for 2026-07-20: the morning routine runs seconds after
|
|
boot/resume, so its pushes failed with the network still down — and the
|
|
log asserted "a 403 here means the token lacks contents:write", which
|
|
sent an investigation chasing a permissions bug that did not exist.
|
|
"""
|
|
log_file = tmp_path / "log.json"
|
|
_write_log(
|
|
log_file, {"2026-07-13": [_entry(_RUN, "2026-07-13T10:00:00+00:00")]}
|
|
)
|
|
with (
|
|
patch.object(_manual_push, "read_sync_token", return_value="t"),
|
|
patch.object(_manual_push, "GitHubSyncClient", MagicMock()),
|
|
patch.object(
|
|
_manual_push,
|
|
"sync_log",
|
|
side_effect=GitHubSyncError("network error reading x"),
|
|
),
|
|
caplog.at_level("WARNING"),
|
|
):
|
|
result = push_pc_workouts(log_file)
|
|
assert result.pushed is False
|
|
assert "network error reading x" in caplog.text
|
|
assert "contents:write" not in caplog.text
|
|
|
|
def test_repo_not_found_still_points_at_the_token(
|
|
self, tmp_path: Path, caplog: pytest.LogCaptureFixture
|
|
) -> None:
|
|
"""The permission hint survives for the error that actually implies it."""
|
|
log_file = tmp_path / "log.json"
|
|
_write_log(
|
|
log_file, {"2026-07-13": [_entry(_RUN, "2026-07-13T10:00:00+00:00")]}
|
|
)
|
|
with (
|
|
patch.object(_manual_push, "read_sync_token", return_value="t"),
|
|
patch.object(_manual_push, "GitHubSyncClient", MagicMock()),
|
|
patch.object(
|
|
_manual_push,
|
|
"sync_log",
|
|
side_effect=RepoNotFoundError("no access"),
|
|
),
|
|
caplog.at_level("WARNING"),
|
|
):
|
|
result = push_pc_workouts(log_file)
|
|
assert result.pushed is False
|
|
assert "contents:write" in caplog.text
|
|
|
|
def test_successful_push_reports_count(self, tmp_path: Path) -> None:
|
|
"""Successful push reports count."""
|
|
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"),
|
|
]
|
|
},
|
|
)
|
|
fake_sync = MagicMock()
|
|
with (
|
|
patch.object(_manual_push, "read_sync_token", return_value="t"),
|
|
patch.object(_manual_push, "GitHubSyncClient", MagicMock()),
|
|
patch.object(_manual_push, "sync_log", fake_sync),
|
|
):
|
|
result = push_pc_workouts(log_file)
|
|
assert result == PushResult(pushed=True, record_count=2, reason="pushed")
|
|
assert fake_sync.call_count == 1
|
|
|
|
def test_pushes_runs_not_just_manuals(self, tmp_path: Path) -> None:
|
|
"""Regression: the phone must converge on the PC's runs too."""
|
|
log_file = tmp_path / "log.json"
|
|
_write_log(
|
|
log_file, {"2026-07-12": [_entry(_RUN, "2026-07-12T10:00:00+00:00")]}
|
|
)
|
|
with (
|
|
patch.object(_manual_push, "read_sync_token", return_value="t"),
|
|
patch.object(_manual_push, "GitHubSyncClient", MagicMock()),
|
|
patch.object(_manual_push, "sync_log", MagicMock()) as fake_sync,
|
|
):
|
|
push_pc_workouts(log_file)
|
|
# local_log is the second positional arg since crdt-sync v0.9.0.
|
|
pushed = fake_sync.call_args.args[1]
|
|
assert "runnerup_verified:2026-07-12" in pushed
|