mirror of
https://github.com/kuhyx/screen-locker.git
synced 2026-11-10 20:27:13 +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
217 lines
8.7 KiB
Python
217 lines
8.7 KiB
Python
"""Tests for the read-only status snapshot layer in _status_data.py."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import UTC, date, datetime
|
|
import json
|
|
from typing import TYPE_CHECKING
|
|
from unittest.mock import patch
|
|
|
|
from screen_locker._status_data import _day_status, gather_status
|
|
from screen_locker.tests.test_status_data import _files
|
|
|
|
if TYPE_CHECKING:
|
|
from pathlib import Path
|
|
|
|
# Fixed reference instant: Friday 2024-01-05, 12:00 UTC == 13:00 Europe/Warsaw.
|
|
# Outside the 05:00-09:00 early-bird window and not a Tue/Wed/Thu relaxed day,
|
|
# so lock-decision branches are fully deterministic regardless of wall clock.
|
|
_FRIDAY_NOON_UTC = datetime(2024, 1, 5, 12, 0, tzinfo=UTC)
|
|
# Monday of that same ISO week, for Mon-Wed shutdown-band assertions.
|
|
_MONDAY_NOON_UTC = datetime(2024, 1, 1, 12, 0, tzinfo=UTC)
|
|
|
|
|
|
class TestGatherStatus:
|
|
"""gather_status() end-to-end via tmp_path-backed state files."""
|
|
|
|
def test_empty_state_full_lock_by_default(self, tmp_path: Path) -> None:
|
|
"""Empty state full lock by default."""
|
|
with patch(
|
|
"screen_locker._status_data.has_workout_skip_today", return_value=False
|
|
):
|
|
snap = gather_status(**_files(tmp_path), now=_FRIDAY_NOON_UTC)
|
|
|
|
assert snap.today.date == "2024-01-05"
|
|
assert snap.today.entry_types == ()
|
|
assert snap.today.day_count == 0
|
|
assert snap.today.counted is False
|
|
assert snap.today.is_sick_day is False
|
|
assert snap.week.counted_count == 0
|
|
assert snap.week.remaining == 5
|
|
assert snap.week.extra == 0
|
|
assert snap.shutdown.tonight is None
|
|
assert snap.shutdown.rest_of_week[0].minutes == 20 * 60
|
|
assert snap.shutdown.rest_of_week[0].speculative is False
|
|
assert snap.streak == 0
|
|
assert snap.early_bird_extended is False
|
|
assert snap.lock_explanation.fired is True
|
|
assert snap.lock_explanation.stage == "would_lock"
|
|
|
|
def test_populated_week_counts_workouts(self, tmp_path: Path) -> None:
|
|
"""Populated week counts workouts."""
|
|
files = _files(tmp_path)
|
|
files["log_file"].write_text(
|
|
json.dumps(
|
|
{
|
|
"2024-01-01": {
|
|
"workout_data": {
|
|
"type": "heat_skip",
|
|
"temperature_celsius": "34",
|
|
}
|
|
},
|
|
"2024-01-03": {
|
|
"workout_data": {"type": "runnerup_verified", "source": "run"}
|
|
},
|
|
"2024-01-05": {
|
|
"workout_data": {"type": "phone_verified", "source": "gym"}
|
|
},
|
|
}
|
|
)
|
|
)
|
|
with (
|
|
patch(
|
|
"screen_locker._status_data.has_workout_skip_today", return_value=False
|
|
),
|
|
patch(
|
|
"screen_locker._compliance_predicates.verify_entry_hmac",
|
|
return_value=True,
|
|
),
|
|
):
|
|
snap = gather_status(**files, now=_FRIDAY_NOON_UTC)
|
|
|
|
assert snap.week.counted_count == 2
|
|
assert snap.today.entry_types == ("phone_verified",)
|
|
assert snap.today.day_count == 1
|
|
assert snap.today.source == "gym"
|
|
assert snap.today.counted is True
|
|
assert snap.lock_explanation.fired is False
|
|
assert snap.lock_explanation.stage == "already_logged"
|
|
|
|
def test_shutdown_config_present_reflected_in_tonight(self, tmp_path: Path) -> None:
|
|
"""Shutdown config present reflected in tonight."""
|
|
files = _files(tmp_path)
|
|
files["shutdown_config_file"].write_text(
|
|
"MON_WED_MINUTES=1320\nTHU_SUN_MINUTES=1380\nMORNING_END_MINUTES=300\n"
|
|
)
|
|
with patch(
|
|
"screen_locker._status_data.has_workout_skip_today", return_value=False
|
|
):
|
|
snap = gather_status(**files, now=_FRIDAY_NOON_UTC)
|
|
|
|
assert snap.shutdown.tonight == (1320, 1380, 300)
|
|
|
|
def test_streak_and_early_bird_reflected_legacy_bonus_ignored(
|
|
self, tmp_path: Path
|
|
) -> None:
|
|
"""Streak + early bird reflected; a legacy banked weekly bonus adds nothing."""
|
|
files = _files(tmp_path)
|
|
files["extra_benefits_file"].write_text(
|
|
json.dumps(
|
|
{
|
|
"consecutive_5plus_weeks": 2,
|
|
"weekly_shutdown_bonus_hours": {"2024-W01": 3},
|
|
"extended_early_bird_iso_weeks": ["2024-W01"],
|
|
}
|
|
)
|
|
)
|
|
with patch(
|
|
"screen_locker._status_data.has_workout_skip_today", return_value=False
|
|
):
|
|
snap = gather_status(**files, now=_FRIDAY_NOON_UTC)
|
|
|
|
assert snap.streak == 2
|
|
assert snap.early_bird_extended is True
|
|
assert snap.shutdown.rest_of_week[0].minutes == 20 * 60 # base, no bonus
|
|
|
|
def test_next_week_preview_is_speculative_rest_of_week_is_not(
|
|
self, tmp_path: Path
|
|
) -> None:
|
|
"""Next week preview is speculative rest of week is not."""
|
|
files = _files(tmp_path)
|
|
with patch(
|
|
"screen_locker._status_data.has_workout_skip_today", return_value=False
|
|
):
|
|
snap = gather_status(**files, now=_FRIDAY_NOON_UTC)
|
|
|
|
assert all(d.speculative is False for d in snap.shutdown.rest_of_week)
|
|
assert all(d.speculative is True for d in snap.shutdown.next_week_preview)
|
|
assert len(snap.shutdown.rest_of_week) == 7
|
|
assert len(snap.shutdown.next_week_preview) == 7
|
|
|
|
def test_corrupt_log_file_treated_as_empty(self, tmp_path: Path) -> None:
|
|
"""Corrupt log file treated as empty."""
|
|
files = _files(tmp_path)
|
|
files["log_file"].write_text("{not valid json")
|
|
with patch(
|
|
"screen_locker._status_data.has_workout_skip_today", return_value=False
|
|
):
|
|
snap = gather_status(**files, now=_FRIDAY_NOON_UTC)
|
|
|
|
assert snap.week.counted_count == 0
|
|
assert snap.today.entry_types == ()
|
|
|
|
def test_now_defaults_to_current_time_when_omitted(self, tmp_path: Path) -> None:
|
|
"""Covers the ``now is None`` branch — just needs to not raise."""
|
|
files = _files(tmp_path)
|
|
with patch(
|
|
"screen_locker._status_data.has_workout_skip_today", return_value=False
|
|
):
|
|
snap = gather_status(**files)
|
|
|
|
assert isinstance(snap.generated_at, str)
|
|
datetime.fromisoformat(snap.generated_at) # must parse without raising
|
|
|
|
|
|
class TestDayStatus:
|
|
"""Direct tests for the _day_status helper on a day's entry list."""
|
|
|
|
def test_no_entries_not_sick(self) -> None:
|
|
"""No entries not sick."""
|
|
day = _day_status(date(2024, 1, 5), [], frozenset())
|
|
assert day.entry_types == ()
|
|
assert day.counted is False
|
|
assert day.day_count == 0
|
|
assert day.is_sick_day is False
|
|
|
|
def test_no_entries_but_sick(self) -> None:
|
|
"""No entries but sick."""
|
|
day = _day_status(date(2024, 1, 5), [], frozenset({date(2024, 1, 5)}))
|
|
assert day.is_sick_day is True
|
|
|
|
def test_non_dict_entries_dropped(self) -> None:
|
|
"""Corrupt log data (non-dict entries in the list) are skipped."""
|
|
day = _day_status(date(2024, 1, 5), ["corrupt-string-entry"], frozenset())
|
|
assert day.entry_types == ()
|
|
assert day.counted is False
|
|
assert day.day_count == 0
|
|
assert day.source == ""
|
|
|
|
def test_multiple_entries_each_count_individually(self) -> None:
|
|
"""Two verified + two manual on one day → day_count 4, all types listed."""
|
|
day = _day_status(
|
|
date(2024, 1, 5),
|
|
[
|
|
{"workout_data": {"type": "runnerup_verified", "source": "run"}},
|
|
{"workout_data": {"type": "phone_verified", "source": "gym"}},
|
|
{"workout_data": {"type": "manual_workout", "source": "tt"}},
|
|
{"workout_data": {"type": "manual_workout", "source": "squash"}},
|
|
],
|
|
frozenset(),
|
|
)
|
|
assert day.entry_types == (
|
|
"runnerup_verified",
|
|
"phone_verified",
|
|
"manual_workout",
|
|
"manual_workout",
|
|
)
|
|
assert day.counted is True
|
|
# Every distinct session counts: 2 verified + 2 manual = 4. This used to
|
|
# assert 3, because _day_status re-derived the rule as
|
|
# "all of a day's manual entries count once" -- a rule
|
|
# count_weekly_workouts never had. The two disagreed on exactly this
|
|
# input, and _day_status feeds the status view's weekly_minimum_met
|
|
# explanation, so the window could explain a decision the locker did
|
|
# not make. Both now go through _weekly_check.count_day_credits.
|
|
assert day.day_count == 4
|
|
assert day.source == "run · gym · tt · squash"
|