mirror of
https://github.com/kuhyx/screen-locker.git
synced 2026-11-10 20:07:16 +01:00
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018RHwVPyiiQMPh4M3JQwQku
133 lines
4.3 KiB
Python
133 lines
4.3 KiB
Python
"""Tests targeting remaining screen_lock.py coverage gaps."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import TYPE_CHECKING
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
from screen_locker.tests._unlock_helpers import setup_unlock
|
|
|
|
if TYPE_CHECKING:
|
|
from pathlib import Path
|
|
|
|
|
|
class TestUnlockScreenExtras:
|
|
"""unlock_screen's extra-workout bonus and streak display."""
|
|
|
|
def test_extra_workout_bonus_shown(
|
|
self,
|
|
mock_tk: MagicMock,
|
|
mock_sys_exit: MagicMock,
|
|
tmp_path: Path,
|
|
) -> None:
|
|
"""An additional same-day verified workout earns the +1h bonus."""
|
|
locker = setup_unlock(
|
|
mock_tk, tmp_path, weekly_count=5, seed_today_type="manual_workout"
|
|
)
|
|
|
|
with (
|
|
patch(
|
|
"screen_locker._workout_credit.count_weekly_workouts",
|
|
return_value=5,
|
|
),
|
|
patch(
|
|
"screen_locker._unlock_view.current_streak",
|
|
return_value=0,
|
|
),
|
|
patch("screen_locker._log_mixin.compute_entry_hmac", return_value=None),
|
|
):
|
|
locker.unlock_screen()
|
|
|
|
locker._adjust_shutdown_time_by.assert_called_once_with(60)
|
|
|
|
def test_extra_bonus_delta_displayed(
|
|
self,
|
|
mock_tk: MagicMock,
|
|
mock_sys_exit: MagicMock,
|
|
tmp_path: Path,
|
|
) -> None:
|
|
"""extra_bonus_delta > 0 → _text called with 'Extra workout'."""
|
|
locker = setup_unlock(mock_tk, tmp_path, seed_today_type="manual_workout")
|
|
|
|
# Simulate before=22:00, after=23:00 → delta=60
|
|
old_cfg = (1320, 1320, 300)
|
|
new_cfg = (1380, 1380, 300)
|
|
locker._read_shutdown_config.side_effect = [old_cfg, new_cfg, new_cfg]
|
|
|
|
text_calls: list[str] = []
|
|
|
|
def _capture_text(msg: str, **kw: object) -> None:
|
|
text_calls.append(msg)
|
|
|
|
object.__setattr__(locker, "_text", _capture_text)
|
|
|
|
with (
|
|
patch(
|
|
"screen_locker._workout_credit.count_weekly_workouts",
|
|
return_value=5,
|
|
),
|
|
patch(
|
|
"screen_locker._unlock_view.current_streak",
|
|
return_value=0,
|
|
),
|
|
patch("screen_locker._log_mixin.compute_entry_hmac", return_value=None),
|
|
):
|
|
locker.unlock_screen()
|
|
|
|
assert any("Extra workout" in c for c in text_calls)
|
|
|
|
def test_no_extra_bonus_when_adjust_fails(
|
|
self,
|
|
mock_tk: MagicMock,
|
|
mock_sys_exit: MagicMock,
|
|
tmp_path: Path,
|
|
) -> None:
|
|
"""Additional verified workout but the +1h adjust fails → delta stays 0."""
|
|
locker = setup_unlock(
|
|
mock_tk,
|
|
tmp_path,
|
|
seed_today_type="manual_workout",
|
|
adjust_ok=False,
|
|
)
|
|
|
|
text_calls: list[str] = []
|
|
object.__setattr__(locker, "_text", lambda msg, **kw: text_calls.append(msg))
|
|
|
|
with (
|
|
patch(
|
|
"screen_locker._workout_credit.count_weekly_workouts",
|
|
return_value=5,
|
|
),
|
|
patch("screen_locker._unlock_view.current_streak", return_value=0),
|
|
patch("screen_locker._log_mixin.compute_entry_hmac", return_value=None),
|
|
):
|
|
locker.unlock_screen()
|
|
|
|
assert not any("Extra workout" in c for c in text_calls)
|
|
|
|
def test_no_extra_bonus_when_new_config_unreadable(
|
|
self,
|
|
mock_tk: MagicMock,
|
|
mock_sys_exit: MagicMock,
|
|
tmp_path: Path,
|
|
) -> None:
|
|
"""Re-reading the shutdown config fails after the +1h → delta stays 0."""
|
|
locker = setup_unlock(mock_tk, tmp_path, seed_today_type="manual_workout")
|
|
# old_cfg readable, new_cfg unreadable → no delta can be computed.
|
|
locker._read_shutdown_config.side_effect = [(1320, 1320, 300), None, None]
|
|
|
|
text_calls: list[str] = []
|
|
object.__setattr__(locker, "_text", lambda msg, **kw: text_calls.append(msg))
|
|
|
|
with (
|
|
patch(
|
|
"screen_locker._workout_credit.count_weekly_workouts",
|
|
return_value=5,
|
|
),
|
|
patch("screen_locker._unlock_view.current_streak", return_value=0),
|
|
patch("screen_locker._log_mixin.compute_entry_hmac", return_value=None),
|
|
):
|
|
locker.unlock_screen()
|
|
|
|
assert not any("Extra workout" in c for c in text_calls)
|