screen-locker/screen_locker/tests/test_workday_penalty.py
Krzysztof kuhy Rudnicki 7d8daca111 sick days: one shared sickdays pool, 20:00 shutdown floor, one-tap lock button
A sick day is now a date in the shared sickdays pool (utils sickdays 0.1.0)
and nothing else. Removed: the 7/30/90-day budget, the escalating lockout
countdown, the justification form, the commitment prompt and its double
debt, the workout-debt counter, the -60 min shutdown cut and its
sick_day_state.json, the post-sick-day --verify-workout mode, the sick
auto-upgrade, scripts/revoke_sick_day.py and their tests.

- Lock screen: "I'm sick" on the retry, no-phone and both Tue-Thu screens;
  one tap -> sickdays.add_sick_day(today) -> sick floor -> best-effort
  background sickdays.sync_quietly -> unlock. Refusals (free day,
  unreadable pool) are shown and the lock stays.
- Shutdown: the reset resolves with earned_time.resolve(..., sick=True)
  (max(20:00, ladder), capped 23:00) and saves the sick lift with the grace
  lift, so later credits absorb it instead of stacking on 20:00. A day
  marked after the reset is lifted by the new _sick_floor live pass (every
  5-min tick, --sync-only, --apply-bonuses, and the tap itself).
- Workout lock and grace floor stand down on a sick day; workouts that day
  credit normally through the sync paths.
- wake-alarm workday_penalty.json is void when its issued_date or
  penalty_date is a sick day.
- Status window, i3 line, CLI --status, MCP flags and web reasons read
  sickdays.
- scripts/migrate_sick_history.py imports sick_history.json's dates via
  sickdays.import_legacy, verifies and removes the file; idempotent, run by
  install.sh.
- Pins: earned-time 0.11.0, freedays 0.4.0, sickdays 0.1.0 (tags pending);
  pre-commit 4.7.0, ruff 0.17.1 (freshness gate).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Xfj1sxKH9VBPqpcAni8Jry
2026-10-16 13:30:39 +02:00

90 lines
3.1 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
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:
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:
key = tmp_path / "hmac.key"
key.write_bytes(b"1" * 32)
with patch("gatelock.log_integrity.DEFAULT_HMAC_KEY_FILE", key):
yield
class TestVerdict:
def test_todays_penalty_applies(self) -> None:
_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:
_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:
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.
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:
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:
_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:
_write({"penalty_date": "2026-09-23", "issued_date": "yesterday"})
assert workday_penalty_today(NOW) is True
assert "not a date" in caplog.text