screen-locker/screen_locker/tests/test_workday_penalty.py
Krzysztof kuhy Rudnicki aa3e290391 Document every test, split files at the cap
Real one-line docstrings stating what each test pins down. Files that
would pass 250 lines moved whole classes into _part2 siblings
(sick_panel, earned, poke_server, shutdown_reading,
manual_workout_dialog).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WzxdWYdwRAtMg7ki6TKxuU
2026-11-01 21:11:34 +01:00

130 lines
5.3 KiB
Python

"""screen_locker._workday_penalty — the stick file, read and never re-derived."""
from __future__ import annotations
from datetime import datetime
import json
from typing import TYPE_CHECKING
from unittest.mock import patch
from gatelock.log_integrity import compute_entry_hmac
import pytest
import sickdays
from screen_locker import _workday_penalty
from screen_locker._workday_penalty import workday_penalty_today
from screen_locker.tests._sick_helpers import mark_sick
if TYPE_CHECKING:
from pathlib import Path
NOW = datetime(2026, 9, 23, 9, 0).astimezone()
def _write(entry: dict[str, object], *, sign: bool = True) -> Path:
"""Write the penalty file, signed unless told otherwise, and return its path."""
if sign:
entry["hmac"] = compute_entry_hmac(entry)
path = _workday_penalty.WORKDAY_PENALTY_FILE
path.write_text(json.dumps(entry))
return path
@pytest.fixture(autouse=True)
def _signing_key(tmp_path: Path) -> None:
"""Point the HMAC key at a throwaway file for every test."""
key = tmp_path / "hmac.key"
key.write_bytes(b"1" * 32)
with patch("gatelock.log_integrity.DEFAULT_HMAC_KEY_FILE", key):
yield
class TestVerdict:
"""workday_penalty_today reads the stick file and fails closed to no penalty."""
def test_todays_penalty_applies(self) -> None:
"""A signed penalty dated today applies."""
_write({"penalty_date": "2026-09-23", "issued_date": "2026-09-22"})
assert workday_penalty_today(NOW) is True
def test_a_different_dates_penalty_does_not_apply(self) -> None:
"""A penalty dated for another day does not apply."""
_write({"penalty_date": "2026-09-24", "issued_date": "2026-09-23"})
assert workday_penalty_today(NOW) is False
def test_missing_unreadable_tampered_are_all_no_penalty(
self, tmp_path: Path
) -> None:
"""A missing, unsigned, unparsable or non-object file means no penalty."""
assert workday_penalty_today(NOW) is False
path = _write({"penalty_date": "2026-09-23"}, sign=False)
assert workday_penalty_today(NOW) is False
path.write_text("not json")
assert workday_penalty_today(NOW) is False
path.write_text("[]")
assert workday_penalty_today(NOW) is False
path.unlink()
assert workday_penalty_today(NOW) is False
def test_default_now_is_used_when_unset(self) -> None:
# Not asserting a value -- just that omitting `now` does not raise.
"""Omitting now falls back to the current time without raising."""
assert workday_penalty_today() is False
class TestSickDayVoidsThePenalty:
"""Void when EITHER the missed ring's day OR the penalised day is sick."""
def test_a_sick_penalty_date_voids_it(self) -> None:
"""A penalty falling on a sick day is void."""
mark_sick("2026-09-23")
_write({"penalty_date": "2026-09-23", "issued_date": "2026-09-22"})
assert workday_penalty_today(NOW) is False
def test_a_sick_issued_date_voids_it(self) -> None:
"""Missed at 08:15, marked sick at 14:00: wake-alarm's retraction lags."""
mark_sick("2026-09-22")
_write({"penalty_date": "2026-09-23", "issued_date": "2026-09-22"})
assert workday_penalty_today(NOW) is False
def test_an_older_file_without_issued_date_still_applies(self) -> None:
"""A file lacking issued_date still applies on its penalty date."""
_write({"penalty_date": "2026-09-23"})
assert workday_penalty_today(NOW) is True
def test_a_malformed_issued_date_is_logged_not_sick(
self, caplog: pytest.LogCaptureFixture
) -> None:
"""A non-date issued_date is logged and does not void the penalty."""
_write({"penalty_date": "2026-09-23", "issued_date": "yesterday"})
assert workday_penalty_today(NOW) is True
assert "not a date" in caplog.text
class TestKeptLockersDecideThePenalty:
"""Ring date -> the alarm switch; penalty date -> the workout lock."""
def test_a_kept_alarm_on_the_ring_day_keeps_the_penalty(self) -> None:
"""Keeping the alarm locker on the ring day keeps the penalty in force."""
mark_sick("2026-09-22", keep=[sickdays.LOCKER_ALARM])
_write({"penalty_date": "2026-09-23", "issued_date": "2026-09-22"})
assert workday_penalty_today(NOW) is True
def test_a_kept_workout_on_the_ring_day_still_voids_it(self) -> None:
"""Keeping only the workout on the ring day still voids the penalty."""
mark_sick("2026-09-22", keep=[sickdays.LOCKER_WORKOUT])
_write({"penalty_date": "2026-09-23", "issued_date": "2026-09-22"})
assert workday_penalty_today(NOW) is False
def test_a_kept_workout_on_the_penalty_day_keeps_it(self) -> None:
"""Keeping the workout locker on the penalty day keeps the penalty in force."""
mark_sick("2026-09-23", keep=[sickdays.LOCKER_WORKOUT])
_write({"penalty_date": "2026-09-23", "issued_date": "2026-09-22"})
assert workday_penalty_today(NOW) is True
def test_a_kept_alarm_on_the_penalty_day_still_voids_it(self) -> None:
"""Keeping only the alarm on the penalty day still voids the penalty."""
mark_sick("2026-09-23", keep=[sickdays.LOCKER_ALARM])
_write({"penalty_date": "2026-09-23", "issued_date": "2026-09-22"})
assert workday_penalty_today(NOW) is False