mirror of
https://github.com/kuhyx/screen-locker.git
synced 2026-11-10 20:27:13 +01:00
verify_lock_popup_safety.py (318) -> _xvfb_reexec.py shared re-exec
_popup_form_check.py HostileLock, ticks,
the real-form check
verify_screen_fits.py (266) -> uses _xvfb_reexec too
conftest.py (329) -> _tk_mocks.py the tk mock + the
module patch list
_opt_in_fixtures.py requestable fixtures
three test modules -> _part4/_part5/_part15 siblings
conftest keeps every autouse fixture. pytest applies autouse fixtures from
conftest only, so one moved to a sibling would silently stop running and take
its safety guard with it -- verified by asserting the fixtures are still
listed, not just that the suite is green. The five near-identical _isolate_*
fixtures collapse into one table-driven _isolate_state_files, which also picks
up the _shutdown_sick_state binding an earlier split created.
scripts/ becomes a real package so the shared helpers are importable. Both
gates now run as `python3 -m scripts.<name>`, including CI -- the path form put
scripts/ on sys.path instead of the repo root, and the alternative was a
try/except ImportError that this repo's no-silent-failures hook rightly
rejects. Each gate was run in its exact invocation form.
Note: verify_lock_popup_safety still fails on
`RecoveryLoop.__init__() takes 3 positional arguments but 6 were given`. That
is pre-existing -- confirmed by running it at HEAD in a clean worktree before
touching it -- a gatelock API drift, not this refactor. Left as found.
978 tests at 100% branch coverage. Gate: 41 -> 22 files, all remaining ones
Dart plus DESIGN_AUDIT_TODO.md.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EveB813BQ8abAP68VSqZ5Y
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(1)
|
|
|
|
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, after=23 → delta=1
|
|
old_cfg = (22, 22, 5)
|
|
new_cfg = (23, 23, 5)
|
|
locker._read_shutdown_config.side_effect = [old_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 = [(22, 22, 5), 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)
|