mirror of
https://github.com/kuhyx/screen-locker.git
synced 2026-07-04 11:43:09 +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
307 lines
11 KiB
Python
307 lines
11 KiB
Python
"""Tests targeting remaining screen_lock.py coverage gaps."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import datetime, timezone
|
|
import json
|
|
from typing import TYPE_CHECKING
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
from screen_locker.tests.conftest import create_locker
|
|
|
|
if TYPE_CHECKING:
|
|
from pathlib import Path
|
|
|
|
|
|
class TestCheckNonVerifyExitsExtras:
|
|
"""Tests for _check_non_verify_exits coverage gaps (lines 228, 233, 251-254)."""
|
|
|
|
def test_logs_auto_filled_runnerup_entries(
|
|
self,
|
|
mock_tk: MagicMock,
|
|
mock_sys_exit: MagicMock,
|
|
tmp_path: Path,
|
|
) -> None:
|
|
"""_scan_and_fill_week_runnerup > 0 + bonus > 0 → bonus logger.info (line 188)."""
|
|
locker = create_locker(mock_tk, tmp_path)
|
|
object.__setattr__(
|
|
locker,
|
|
"_scan_and_fill_week_runnerup",
|
|
MagicMock(return_value=2),
|
|
)
|
|
# Short-circuit _check_today_state_exits so the test is time-independent.
|
|
object.__setattr__(
|
|
locker,
|
|
"_check_today_state_exits",
|
|
MagicMock(return_value=False),
|
|
)
|
|
object.__setattr__(
|
|
locker,
|
|
"_adjust_shutdown_time_by",
|
|
MagicMock(return_value=True),
|
|
)
|
|
with (
|
|
patch("screen_locker.screen_lock.reset_to_base_if_new_day"),
|
|
patch(
|
|
"screen_locker.screen_lock.count_weekly_workouts", side_effect=[0, 5]
|
|
),
|
|
patch(
|
|
"screen_locker.screen_lock.process_week_transition",
|
|
return_value=[],
|
|
),
|
|
patch("screen_locker.screen_lock.is_relaxed_day", return_value=False),
|
|
patch("screen_locker.screen_lock.has_weekly_minimum", return_value=True),
|
|
patch("screen_locker.screen_lock.sys.exit"),
|
|
):
|
|
locker._check_non_verify_exits()
|
|
locker._adjust_shutdown_time_by.assert_called_once_with(1) # bonus = 5-max(4,0)
|
|
|
|
def test_auto_fill_no_bonus_when_min_not_exceeded(
|
|
self,
|
|
mock_tk: MagicMock,
|
|
mock_sys_exit: MagicMock,
|
|
tmp_path: Path,
|
|
) -> None:
|
|
"""n_filled > 0 but new_count <= min → bonus=0 → branch 187->190 (no bonus log)."""
|
|
locker = create_locker(mock_tk, tmp_path)
|
|
object.__setattr__(
|
|
locker,
|
|
"_scan_and_fill_week_runnerup",
|
|
MagicMock(return_value=1),
|
|
)
|
|
object.__setattr__(
|
|
locker,
|
|
"_check_today_state_exits",
|
|
MagicMock(return_value=False),
|
|
)
|
|
with (
|
|
patch("screen_locker.screen_lock.reset_to_base_if_new_day"),
|
|
# prev=2, new=3 → bonus=max(0,3-max(4,2))=0 → no bonus logger call
|
|
patch(
|
|
"screen_locker.screen_lock.count_weekly_workouts", side_effect=[2, 3]
|
|
),
|
|
patch(
|
|
"screen_locker.screen_lock.process_week_transition",
|
|
return_value=[],
|
|
),
|
|
patch("screen_locker.screen_lock.is_relaxed_day", return_value=False),
|
|
patch("screen_locker.screen_lock.has_weekly_minimum", return_value=False),
|
|
patch("screen_locker.screen_lock.has_skip_credit", return_value=False),
|
|
patch("screen_locker.screen_lock.sys.exit"),
|
|
):
|
|
locker._check_non_verify_exits()
|
|
|
|
def test_logs_weekly_reward_message(
|
|
self,
|
|
mock_tk: MagicMock,
|
|
mock_sys_exit: MagicMock,
|
|
tmp_path: Path,
|
|
) -> None:
|
|
"""process_week_transition returning messages → logger.info at line 233."""
|
|
locker = create_locker(mock_tk, tmp_path)
|
|
object.__setattr__(
|
|
locker,
|
|
"_scan_and_fill_week_runnerup",
|
|
MagicMock(return_value=0),
|
|
)
|
|
with (
|
|
patch("screen_locker.screen_lock.reset_to_base_if_new_day"),
|
|
patch(
|
|
"screen_locker.screen_lock.process_week_transition",
|
|
return_value=["🎉 +1 skip credit for 5-workout week!"],
|
|
),
|
|
patch("screen_locker.screen_lock.is_relaxed_day", return_value=False),
|
|
patch("screen_locker.screen_lock.has_weekly_minimum", return_value=True),
|
|
patch("screen_locker.screen_lock.sys.exit"),
|
|
):
|
|
locker._check_non_verify_exits()
|
|
|
|
def test_uses_skip_credit_when_minimum_not_met(
|
|
self,
|
|
mock_tk: MagicMock,
|
|
mock_sys_exit: MagicMock,
|
|
tmp_path: Path,
|
|
) -> None:
|
|
"""has_skip_credit True + weekly min not met → consume credit and exit (251-254)."""
|
|
locker = create_locker(mock_tk, tmp_path)
|
|
object.__setattr__(
|
|
locker,
|
|
"_scan_and_fill_week_runnerup",
|
|
MagicMock(return_value=0),
|
|
)
|
|
# Prevent time-dependent early-exit that would skip the skip-credit branch.
|
|
object.__setattr__(
|
|
locker,
|
|
"_check_today_state_exits",
|
|
MagicMock(return_value=False),
|
|
)
|
|
mock_exit = MagicMock()
|
|
with (
|
|
patch("screen_locker.screen_lock.reset_to_base_if_new_day"),
|
|
patch(
|
|
"screen_locker.screen_lock.process_week_transition",
|
|
return_value=[],
|
|
),
|
|
patch("screen_locker.screen_lock.is_relaxed_day", return_value=False),
|
|
patch("screen_locker.screen_lock.has_weekly_minimum", return_value=False),
|
|
patch("screen_locker.screen_lock.has_skip_credit", return_value=True),
|
|
patch("screen_locker.screen_lock.consume_skip_credit"),
|
|
patch("screen_locker.screen_lock.sys.exit", mock_exit),
|
|
):
|
|
locker._check_non_verify_exits()
|
|
mock_exit.assert_called_once_with(0)
|
|
|
|
|
|
class TestTryAutoUpgradeSickDayRunnerUp:
|
|
"""Tests for RunnerUp paths in _try_auto_upgrade_sick_day (lines 273-286)."""
|
|
|
|
def test_runnerup_exception_returns_false(
|
|
self,
|
|
mock_tk: MagicMock,
|
|
mock_sys_exit: MagicMock,
|
|
tmp_path: Path,
|
|
) -> None:
|
|
"""OSError from _verify_runnerup_workout → returns False (lines 273-275)."""
|
|
locker = create_locker(mock_tk, tmp_path)
|
|
object.__setattr__(
|
|
locker,
|
|
"_verify_phone_workout",
|
|
MagicMock(return_value=("no_phone", "no phone")),
|
|
)
|
|
object.__setattr__(
|
|
locker,
|
|
"_verify_runnerup_workout",
|
|
MagicMock(side_effect=OSError("adb fail")),
|
|
)
|
|
assert locker._try_auto_upgrade_sick_day() is False
|
|
|
|
def test_runnerup_verified_saves_entry(
|
|
self,
|
|
mock_tk: MagicMock,
|
|
mock_sys_exit: MagicMock,
|
|
tmp_path: Path,
|
|
) -> None:
|
|
"""RunnerUp returns verified → saves runnerup_verified entry (lines 281-286)."""
|
|
log_file = tmp_path / "workout_log.json"
|
|
locker = create_locker(mock_tk, tmp_path)
|
|
locker.log_file = log_file
|
|
locker.workout_data = {}
|
|
object.__setattr__(
|
|
locker,
|
|
"_verify_phone_workout",
|
|
MagicMock(return_value=("no_phone", "no phone")),
|
|
)
|
|
object.__setattr__(
|
|
locker,
|
|
"_verify_runnerup_workout",
|
|
MagicMock(return_value=("verified", "Running: 6 km in 40 min")),
|
|
)
|
|
object.__setattr__(
|
|
locker, "_adjust_shutdown_time_later", MagicMock(return_value=True)
|
|
)
|
|
with patch("screen_locker._log_mixin.compute_entry_hmac", return_value=None):
|
|
result = locker._try_auto_upgrade_sick_day()
|
|
|
|
assert result is True
|
|
today = datetime.now(tz=timezone.utc).strftime("%Y-%m-%d")
|
|
data = json.loads(log_file.read_text())
|
|
assert data[today]["workout_data"]["type"] == "runnerup_verified"
|
|
assert data[today]["workout_data"]["after_sick_day"] == "true"
|
|
|
|
def test_runnerup_not_verified_returns_false(
|
|
self,
|
|
mock_tk: MagicMock,
|
|
mock_sys_exit: MagicMock,
|
|
tmp_path: Path,
|
|
) -> None:
|
|
"""RunnerUp not verified → returns False."""
|
|
locker = create_locker(mock_tk, tmp_path)
|
|
object.__setattr__(
|
|
locker,
|
|
"_verify_phone_workout",
|
|
MagicMock(return_value=("no_phone", "no phone")),
|
|
)
|
|
object.__setattr__(
|
|
locker,
|
|
"_verify_runnerup_workout",
|
|
MagicMock(return_value=("not_verified", "no run")),
|
|
)
|
|
assert locker._try_auto_upgrade_sick_day() is False
|
|
|
|
|
|
class TestTryAutoUpgradeEarlyBirdRunnerUp:
|
|
"""Tests for RunnerUp paths in screen_lock.py _try_auto_upgrade_early_bird (305-318)."""
|
|
|
|
def test_runnerup_exception_returns_false(
|
|
self,
|
|
mock_tk: MagicMock,
|
|
mock_sys_exit: MagicMock,
|
|
tmp_path: Path,
|
|
) -> None:
|
|
"""RuntimeError from _verify_runnerup_workout → returns False (lines 305-307)."""
|
|
locker = create_locker(mock_tk, tmp_path)
|
|
object.__setattr__(
|
|
locker,
|
|
"_verify_phone_workout",
|
|
MagicMock(return_value=("no_phone", "no phone")),
|
|
)
|
|
object.__setattr__(
|
|
locker,
|
|
"_verify_runnerup_workout",
|
|
MagicMock(side_effect=RuntimeError("adb gone")),
|
|
)
|
|
assert locker._try_auto_upgrade_early_bird() is False
|
|
|
|
def test_runnerup_verified_saves_entry(
|
|
self,
|
|
mock_tk: MagicMock,
|
|
mock_sys_exit: MagicMock,
|
|
tmp_path: Path,
|
|
) -> None:
|
|
"""RunnerUp returns verified → saves runnerup_verified entry (lines 313-318)."""
|
|
log_file = tmp_path / "workout_log.json"
|
|
locker = create_locker(mock_tk, tmp_path)
|
|
locker.log_file = log_file
|
|
locker.workout_data = {}
|
|
object.__setattr__(
|
|
locker,
|
|
"_verify_phone_workout",
|
|
MagicMock(return_value=("no_phone", "no phone")),
|
|
)
|
|
object.__setattr__(
|
|
locker,
|
|
"_verify_runnerup_workout",
|
|
MagicMock(return_value=("verified", "Running: 6 km in 40 min")),
|
|
)
|
|
object.__setattr__(
|
|
locker, "_adjust_shutdown_time_later", MagicMock(return_value=True)
|
|
)
|
|
with patch("screen_locker._log_mixin.compute_entry_hmac", return_value=None):
|
|
result = locker._try_auto_upgrade_early_bird()
|
|
|
|
assert result is True
|
|
today = datetime.now(tz=timezone.utc).strftime("%Y-%m-%d")
|
|
data = json.loads(log_file.read_text())
|
|
assert data[today]["workout_data"]["type"] == "runnerup_verified"
|
|
assert data[today]["workout_data"]["after_early_bird"] == "true"
|
|
|
|
def test_runnerup_not_verified_returns_false(
|
|
self,
|
|
mock_tk: MagicMock,
|
|
mock_sys_exit: MagicMock,
|
|
tmp_path: Path,
|
|
) -> None:
|
|
"""RunnerUp not verified → returns False."""
|
|
locker = create_locker(mock_tk, tmp_path)
|
|
object.__setattr__(
|
|
locker,
|
|
"_verify_phone_workout",
|
|
MagicMock(return_value=("no_phone", "no phone")),
|
|
)
|
|
object.__setattr__(
|
|
locker,
|
|
"_verify_runnerup_workout",
|
|
MagicMock(return_value=("not_verified", "no run")),
|
|
)
|
|
assert locker._try_auto_upgrade_early_bird() is False
|