mirror of
https://github.com/kuhyx/screen-locker.git
synced 2026-11-10 18:07:22 +01:00
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
138 lines
5.0 KiB
Python
138 lines
5.0 KiB
Python
"""The workout day is the LOCAL calendar day, on every writer and reader.
|
|
|
|
Regression for 2026-09-13: a manual workout logged at 00:02 local (CEST,
|
|
``22:02Z`` the day before) was filed under the previous day because every
|
|
day-key derivation used ``datetime.now(tz=UTC)``. The lock check then looked
|
|
for the local date, found nothing, and locked the PC — while the trailing
|
|
window budget (which does not key on an exact date) happily counted it. Any
|
|
log between 00:00 and 02:00 local landed on yesterday.
|
|
|
|
Each test freezes the clock at that exact instant through
|
|
``screen_locker._day`` so the same mismatch can never come back on one of the
|
|
fourteen former ``now(tz=UTC)`` sites without failing here.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import UTC, datetime, tzinfo
|
|
import json
|
|
import time
|
|
from typing import TYPE_CHECKING
|
|
from unittest.mock import patch
|
|
|
|
import pytest
|
|
|
|
from screen_locker import _day
|
|
from screen_locker._compliance_predicates import has_logged_today
|
|
from screen_locker._compliance_state import explain_lock_decision
|
|
from screen_locker._log_io import load_workout_log
|
|
from screen_locker._log_mixin import write_signed_entry
|
|
|
|
if TYPE_CHECKING:
|
|
from collections.abc import Iterator
|
|
from pathlib import Path
|
|
|
|
# 00:02:35 on 2026-09-13 in Europe/Warsaw — the football was logged then.
|
|
JUST_AFTER_MIDNIGHT_UTC = datetime(2026, 9, 12, 22, 2, 35, tzinfo=UTC)
|
|
LOCAL_DAY = "2026-09-13"
|
|
UTC_DAY = "2026-09-12"
|
|
|
|
|
|
@pytest.fixture
|
|
def warsaw_after_midnight(monkeypatch: pytest.MonkeyPatch) -> Iterator[None]:
|
|
"""Pin TZ to Europe/Warsaw and the clock to 00:02:35 local on the 13th."""
|
|
monkeypatch.setenv("TZ", "Europe/Warsaw")
|
|
time.tzset()
|
|
|
|
class _FrozenClock:
|
|
"""Stands in for ``datetime`` inside ``_day``; only ``now`` is called."""
|
|
|
|
@staticmethod
|
|
def now(tz: tzinfo | None = None) -> datetime:
|
|
return JUST_AFTER_MIDNIGHT_UTC.astimezone(tz or UTC)
|
|
|
|
monkeypatch.setattr(_day, "datetime", _FrozenClock)
|
|
yield
|
|
monkeypatch.delenv("TZ")
|
|
time.tzset()
|
|
|
|
|
|
class TestTodayStr:
|
|
"""``_day.today_str`` is the single source of "what day is it"."""
|
|
|
|
def test_uses_the_local_calendar_day(self, warsaw_after_midnight: None) -> None:
|
|
assert _day.today_str() == LOCAL_DAY
|
|
|
|
def test_explicit_instant_is_converted_to_local(
|
|
self, warsaw_after_midnight: None
|
|
) -> None:
|
|
assert _day.today_str(JUST_AFTER_MIDNIGHT_UTC) == LOCAL_DAY
|
|
|
|
|
|
class TestAfterMidnightLog:
|
|
"""A workout logged just after local midnight counts for the new day."""
|
|
|
|
def test_write_and_lock_check_agree(
|
|
self, tmp_path: Path, warsaw_after_midnight: None
|
|
) -> None:
|
|
log_file = tmp_path / "log.json"
|
|
write_signed_entry(
|
|
log_file,
|
|
_day.today_str(),
|
|
{"type": "manual_workout", "start_time": "14:30"},
|
|
)
|
|
assert list(load_workout_log(log_file)) == [LOCAL_DAY]
|
|
with patch(
|
|
"screen_locker._compliance_predicates.verify_entry_hmac", return_value=True
|
|
):
|
|
assert has_logged_today(log_file) is True
|
|
|
|
def test_explain_sees_the_workout(
|
|
self, tmp_path: Path, warsaw_after_midnight: None
|
|
) -> None:
|
|
"""The read-only decision chain keys on the same local day."""
|
|
log_file = tmp_path / "log.json"
|
|
log_file.write_text(json.dumps({LOCAL_DAY: [{"workout_data": {"type": "x"}}]}))
|
|
with patch(
|
|
"screen_locker._compliance_predicates.verify_entry_hmac", return_value=True
|
|
):
|
|
result = explain_lock_decision(
|
|
log_file=log_file,
|
|
early_bird_pending_file=tmp_path / "early_bird_pending.json",
|
|
weekly_minimum_met=True,
|
|
relaxed_day=False,
|
|
now=JUST_AFTER_MIDNIGHT_UTC,
|
|
)
|
|
assert result.fired is False
|
|
assert result.stage == "already_logged"
|
|
|
|
def test_utc_day_entry_does_not_count(
|
|
self, tmp_path: Path, warsaw_after_midnight: None
|
|
) -> None:
|
|
"""An entry filed under the UTC day is yesterday's, not today's."""
|
|
log_file = tmp_path / "log.json"
|
|
log_file.write_text(json.dumps({UTC_DAY: [{"workout_data": {"type": "x"}}]}))
|
|
with patch(
|
|
"screen_locker._compliance_predicates.verify_entry_hmac", return_value=True
|
|
):
|
|
assert has_logged_today(log_file) is False
|
|
|
|
|
|
class TestCrossDayDedup:
|
|
"""``write_signed_entry`` dedups by workout_id across ALL days.
|
|
|
|
Moving an entry to its correct day must not let its synced copy re-ingest
|
|
under the old key: the id is globally unique, so the log is searched
|
|
whole, not per day.
|
|
"""
|
|
|
|
def test_same_workout_id_on_another_day_is_not_appended(
|
|
self, tmp_path: Path
|
|
) -> None:
|
|
log_file = tmp_path / "log.json"
|
|
data = {"type": "manual_workout", "workout_id": "manual:2026-09-12T14:30"}
|
|
assert write_signed_entry(log_file, LOCAL_DAY, data).appended is True
|
|
result = write_signed_entry(log_file, UTC_DAY, data)
|
|
assert result.appended is False
|
|
assert UTC_DAY not in load_workout_log(log_file)
|