mirror of
https://github.com/kuhyx/screen-locker.git
synced 2026-07-04 14:43:14 +02:00
- Refactor RunnerUp verification: extract RunnerUpDbMixin (_runnerup_db.py), split _scan_and_fill_week_runnerup into a helper _try_fill_runnerup_for_date to keep cyclomatic complexity ≤10 - Generalise TCX lookup to any date in the ISO week (was today-only); all gap days Mon→today auto-filled on every startup and 08:30 timer firing - Add _adjust_shutdown_time_by(): +1h per extra workout beyond the 4-workout minimum, capped at midnight (hour=24) - Add _shutdown_base.py: daily reset of shutdown config to a stored base so the bonus doesn't silently accumulate across days - Add _extra_benefits.py: streak tracking, skip credits (earn (n-4) credits for 5+ workout weeks), early-bird extension to 09:00 for eligible weeks - Add --status mode (_status.py): non-locking CLI view showing per-day breakdown (✓/✗), RunnerUp auto-scan, bonus status, shutdown time, streak, skip credits, and early-bird status - Hook carrot into _check_non_verify_exits: bonus applied whenever auto-fill pushes weekly count above the minimum - Pass all pre-commit hooks (ruff, mypy, pylint, bandit, shellcheck, codespell, max-file-length); 508 tests at 100% branch coverage Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017auyHmf2ZwQcDAwXaSo7KX
173 lines
5.2 KiB
Python
173 lines
5.2 KiB
Python
"""Tests for RunnerUpVerificationMixin in _runnerup_verification.py."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import TYPE_CHECKING
|
|
from unittest.mock import MagicMock
|
|
|
|
from screen_locker.tests.conftest import create_locker
|
|
|
|
if TYPE_CHECKING:
|
|
from pathlib import Path
|
|
|
|
# Minimal valid TCX XML for a 40-minute, 6-km run.
|
|
_TCX_RUNNING = """\
|
|
<?xml version="1.0" encoding="UTF-8"?>
|
|
<TrainingCenterDatabase
|
|
xmlns="http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2">
|
|
<Activities>
|
|
<Activity Sport="Running">
|
|
<Lap>
|
|
<TotalTimeSeconds>2400.0</TotalTimeSeconds>
|
|
<DistanceMeters>6000.0</DistanceMeters>
|
|
</Lap>
|
|
</Activity>
|
|
</Activities>
|
|
</TrainingCenterDatabase>
|
|
"""
|
|
|
|
# TCX with an unrecognised sport tag (not in RUNNERUP_ACCEPTED_SPORTS).
|
|
_TCX_GYM = """\
|
|
<?xml version="1.0" encoding="UTF-8"?>
|
|
<TrainingCenterDatabase
|
|
xmlns="http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2">
|
|
<Activities>
|
|
<Activity Sport="Gym">
|
|
<Lap>
|
|
<TotalTimeSeconds>3600.0</TotalTimeSeconds>
|
|
<DistanceMeters>0.0</DistanceMeters>
|
|
</Lap>
|
|
</Activity>
|
|
</Activities>
|
|
</TrainingCenterDatabase>
|
|
"""
|
|
|
|
# Two laps that together make a valid run.
|
|
_TCX_MULTI_LAP = """\
|
|
<?xml version="1.0" encoding="UTF-8"?>
|
|
<TrainingCenterDatabase
|
|
xmlns="http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2">
|
|
<Activities>
|
|
<Activity Sport="Running">
|
|
<Lap>
|
|
<TotalTimeSeconds>1200.0</TotalTimeSeconds>
|
|
<DistanceMeters>3000.0</DistanceMeters>
|
|
</Lap>
|
|
<Lap>
|
|
<TotalTimeSeconds>1200.0</TotalTimeSeconds>
|
|
<DistanceMeters>3000.0</DistanceMeters>
|
|
</Lap>
|
|
</Activity>
|
|
</Activities>
|
|
</TrainingCenterDatabase>
|
|
"""
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Helpers
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def _write_tcx(tmp_path: Path, content: str, name: str = "activity.tcx") -> str:
|
|
"""Write TCX content to a temp file and return the path string."""
|
|
p = tmp_path / name
|
|
p.write_text(content)
|
|
return str(p)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# _validate_runnerup_data
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class TestVerifyRunnerupViaFiles:
|
|
"""Tests for _verify_runnerup_via_files (lines 147-165)."""
|
|
|
|
def test_returns_none_when_no_exports_found(
|
|
self,
|
|
mock_tk: MagicMock,
|
|
mock_sys_exit: MagicMock,
|
|
tmp_path: Path,
|
|
) -> None:
|
|
"""No exports for today → None (caller tries DB path)."""
|
|
locker = create_locker(mock_tk, tmp_path)
|
|
object.__setattr__(
|
|
locker,
|
|
"_find_runnerup_exports_for_date",
|
|
MagicMock(return_value=[]),
|
|
)
|
|
assert locker._verify_runnerup_via_files() is None
|
|
|
|
def test_returns_verified_when_file_passes(
|
|
self,
|
|
mock_tk: MagicMock,
|
|
mock_sys_exit: MagicMock,
|
|
tmp_path: Path,
|
|
) -> None:
|
|
"""First valid file → verified immediately."""
|
|
locker = create_locker(mock_tk, tmp_path)
|
|
object.__setattr__(
|
|
locker,
|
|
"_find_runnerup_exports_for_date",
|
|
MagicMock(return_value=["/sdcard/run.tcx"]),
|
|
)
|
|
object.__setattr__(
|
|
locker,
|
|
"_pull_and_parse_tcx",
|
|
MagicMock(
|
|
return_value={"sport": 0, "duration_seconds": 2400, "distance_m": 6000}
|
|
),
|
|
)
|
|
status, _ = locker._verify_runnerup_via_files()
|
|
assert status == "verified"
|
|
|
|
def test_returns_best_when_no_file_verified(
|
|
self,
|
|
mock_tk: MagicMock,
|
|
mock_sys_exit: MagicMock,
|
|
tmp_path: Path,
|
|
) -> None:
|
|
"""Files found but none verified → returns first non-None validation result."""
|
|
locker = create_locker(mock_tk, tmp_path)
|
|
object.__setattr__(
|
|
locker,
|
|
"_find_runnerup_exports_for_date",
|
|
MagicMock(return_value=["/sdcard/run.tcx"]),
|
|
)
|
|
object.__setattr__(
|
|
locker,
|
|
"_pull_and_parse_tcx",
|
|
MagicMock(
|
|
return_value={"sport": 0, "duration_seconds": 60, "distance_m": 6000}
|
|
),
|
|
)
|
|
result = locker._verify_runnerup_via_files()
|
|
assert result is not None
|
|
status, _ = result
|
|
assert status == "too_short"
|
|
|
|
def test_returns_fallback_when_all_files_unreadable(
|
|
self,
|
|
mock_tk: MagicMock,
|
|
mock_sys_exit: MagicMock,
|
|
tmp_path: Path,
|
|
) -> None:
|
|
"""_pull_and_parse_tcx returns None for every file → fallback not_verified."""
|
|
locker = create_locker(mock_tk, tmp_path)
|
|
object.__setattr__(
|
|
locker,
|
|
"_find_runnerup_exports_for_date",
|
|
MagicMock(return_value=["/sdcard/run.tcx"]),
|
|
)
|
|
object.__setattr__(locker, "_pull_and_parse_tcx", MagicMock(return_value=None))
|
|
status, _ = locker._verify_runnerup_via_files()
|
|
assert status == "not_verified"
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# _scan_and_fill_week_runnerup
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|