screen-locker/screen_locker/tests/test_status_part2.py
Krzysztof kuhy Rudnicki e25d806742 Fix silent skip-credit bypass; replace with weekly shutdown-time bonus
The screen locker skipped enforcement on 2026-07-03 without ever showing
a lock: a banked skip credit (earned from a prior 5+/week streak) was
consumed automatically with no confirmation and no visible log. Reworked
the whole reward mechanic instead of just gating it, since banking a
"skip a future workout" credit works against maximizing weekly workouts:

- Removed skip credits entirely (has_skip_credit/consume_skip_credit and
  the confirmation dialog built to gate them). The only same-day skip
  paths left are heat_skip and sick_day, both requiring a genuine reason.
- Extra workouts (5+/week) now bank shutdown-time-later hours for the
  following week instead — comfort, not reduced enforcement. Reuses the
  existing _adjust_shutdown_time_by and reset_to_base_if_new_day's
  previously-discarded return value as the once-per-day gate.
- early_bird and sick_day no longer pollute workout_log.json. early_bird
  is a same-day pending marker now stored in its own self-expiring,
  HMAC-signed file; sick_day is sourced entirely from sick_history.json
  (already the real source of truth). Fixes an accidental-safety gap
  where "already took a sick day today" only halted startup by luck.
- Cleaned up 3 stale non-workout entries already in workout_log.json.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QdTccgbK7624kfoaV6CtXS
2026-07-03 15:27:08 +02:00

161 lines
6.8 KiB
Python

"""Tests for screen_locker._status.run_status() -- RunnerUp fill + minimum summary.
Split from test_status.py to stay under the repo's 400-line file limit.
"""
from __future__ import annotations
from typing import TYPE_CHECKING
from unittest.mock import MagicMock, patch
from screen_locker._status import run_status
from screen_locker.tests.conftest import _make_locker
if TYPE_CHECKING:
from pathlib import Path
import pytest
class TestRunStatusFill:
"""Tests for RunnerUp scan paths in run_status."""
def test_fill_with_bonus_applied(
self, tmp_path: Path, capsys: pytest.CaptureFixture
) -> None:
"""n_filled > 0, bonus > 0, adjust succeeds → bonus line shown."""
eb_file = tmp_path / "eb.json"
locker = _make_locker(tmp_path / "log.json", n_filled=2, bonus_applied=True)
# after_count=5 (> WEEKLY_WORKOUT_MINIMUM=4), before_count=3
with (
patch("screen_locker._status.EXTRA_BENEFITS_FILE", eb_file),
patch("screen_locker._status.current_streak", return_value=0),
patch("screen_locker._status.has_extended_early_bird", return_value=False),
patch("screen_locker._status.count_weekly_workouts", return_value=5),
patch("sys.exit"),
):
run_status(locker)
out = capsys.readouterr().out
assert "Auto-filled 2 workout(s)" in out
def test_fill_bonus_pending_when_adjust_fails(
self, tmp_path: Path, capsys: pytest.CaptureFixture
) -> None:
"""n_filled > 0, bonus > 0, adjust returns False → 'bonus pending' shown."""
eb_file = tmp_path / "eb.json"
locker = _make_locker(tmp_path / "log.json", n_filled=2, bonus_applied=False)
with (
patch("screen_locker._status.EXTRA_BENEFITS_FILE", eb_file),
patch("screen_locker._status.current_streak", return_value=0),
patch("screen_locker._status.has_extended_early_bird", return_value=False),
patch("screen_locker._status.count_weekly_workouts", return_value=5),
patch("sys.exit"),
):
run_status(locker)
out = capsys.readouterr().out
assert "bonus pending" in out
def test_fill_no_bonus_when_still_below_min(
self, tmp_path: Path, capsys: pytest.CaptureFixture
) -> None:
"""n_filled=1 but count still < 4 → no bonus line."""
eb_file = tmp_path / "eb.json"
locker = _make_locker(tmp_path / "log.json", n_filled=1, bonus_applied=False)
with (
patch("screen_locker._status.EXTRA_BENEFITS_FILE", eb_file),
patch("screen_locker._status.current_streak", return_value=0),
patch("screen_locker._status.has_extended_early_bird", return_value=False),
patch("screen_locker._status.count_weekly_workouts", return_value=3),
patch("sys.exit"),
):
run_status(locker)
out = capsys.readouterr().out
assert "shutdown bonus" not in out
class TestRunStatusMinimumStatus:
"""Tests for the 'remaining/extra/exactly met' summary lines."""
def test_extra_above_minimum(
self, tmp_path: Path, capsys: pytest.CaptureFixture
) -> None:
"""after_count > WEEKLY_WORKOUT_MINIMUM → 'above minimum' line.
n_filled=1 triggers the count_weekly_workouts() branch so after_count
is taken from that mock (5), not from the per-day log loop (0).
"""
eb_file = tmp_path / "eb.json"
locker = _make_locker(tmp_path / "log.json", n_filled=1, bonus_applied=False)
with (
patch("screen_locker._status.EXTRA_BENEFITS_FILE", eb_file),
patch("screen_locker._status.current_streak", return_value=0),
patch("screen_locker._status.has_extended_early_bird", return_value=False),
patch("screen_locker._status.count_weekly_workouts", return_value=5),
patch("sys.exit"),
):
run_status(locker)
out = capsys.readouterr().out
assert "above minimum" in out
def test_exactly_at_minimum(
self, tmp_path: Path, capsys: pytest.CaptureFixture
) -> None:
"""after_count == WEEKLY_WORKOUT_MINIMUM → 'met exactly' line.
n_filled=1 so after_count = count_weekly_workouts() = 4 = WEEKLY_WORKOUT_MINIMUM.
bonus = max(0, 4 - max(4, 0)) = 0, so no bonus line is printed.
"""
eb_file = tmp_path / "eb.json"
locker = _make_locker(tmp_path / "log.json", n_filled=1, bonus_applied=False)
with (
patch("screen_locker._status.EXTRA_BENEFITS_FILE", eb_file),
patch("screen_locker._status.current_streak", return_value=0),
patch("screen_locker._status.has_extended_early_bird", return_value=False),
patch("screen_locker._status.count_weekly_workouts", return_value=4),
patch("sys.exit"),
):
run_status(locker)
out = capsys.readouterr().out
assert "Weekly minimum met exactly" in out
def test_sys_exit_called(self, tmp_path: Path) -> None:
"""run_status always calls sys.exit(0)."""
eb_file = tmp_path / "eb.json"
locker = _make_locker(tmp_path / "log.json", n_filled=0)
mock_exit = MagicMock()
with (
patch("screen_locker._status.EXTRA_BENEFITS_FILE", eb_file),
patch("screen_locker._status.current_streak", return_value=0),
patch("screen_locker._status.has_extended_early_bird", return_value=False),
patch("screen_locker._status.count_weekly_workouts", return_value=0),
patch("sys.exit", mock_exit),
):
run_status(locker)
mock_exit.assert_called_once_with(0)
def test_loop_breaks_on_future_day(
self, tmp_path: Path, capsys: pytest.CaptureFixture
) -> None:
"""Pin today to Monday so the loop hits d > today on day 2, covering line 64."""
from datetime import datetime, timezone
fake_now = datetime(2026, 6, 22, 12, 0, tzinfo=timezone.utc)
class _FakeDatetime(datetime):
@classmethod
def now(cls, tz=None): # type: ignore[override]
return fake_now.astimezone(tz) if tz else fake_now
with (
patch("screen_locker._status.datetime", _FakeDatetime),
patch("screen_locker._status.EXTRA_BENEFITS_FILE", tmp_path / "eb.json"),
patch("screen_locker._status.current_streak", return_value=0),
patch("screen_locker._status.has_extended_early_bird", return_value=False),
patch("screen_locker._status.count_weekly_workouts", return_value=0),
patch("sys.exit"),
):
run_status(_make_locker(tmp_path / "log.json", n_filled=0))
out = capsys.readouterr().out
assert "Mon Jun 22" in out
assert "Tue Jun 23" not in out