screen-locker/screen_locker/tests/test_ui_and_timers.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

169 lines
5.3 KiB
Python

"""Tests for UI transitions, timer logic, and the retry screen."""
from __future__ import annotations
from typing import TYPE_CHECKING
from unittest.mock import MagicMock
from screen_locker import _sync_client, _ui_flows
from screen_locker.tests.conftest import create_locker
if TYPE_CHECKING:
from pathlib import Path
import pytest
class TestUITransitions:
"""Tests for UI state transitions."""
def test_clear_container(
self,
mock_tk: MagicMock,
mock_sys_exit: MagicMock,
tmp_path: Path,
) -> None:
"""Test clear_container destroys all child widgets."""
locker = create_locker(mock_tk, tmp_path)
# Set up mock children
mock_child1 = MagicMock()
mock_child2 = MagicMock()
locker.container.first.winfo_children.return_value = [
mock_child1,
mock_child2,
]
locker.clear_container()
mock_child1.destroy.assert_called_once()
mock_child2.destroy.assert_called_once()
def test_unlock_screen_saves_and_schedules_close(
self,
mock_tk: MagicMock,
mock_sys_exit: MagicMock,
tmp_path: Path,
) -> None:
"""Test unlock_screen saves log and schedules close."""
locker = create_locker(mock_tk, tmp_path)
locker.log_file = tmp_path / "log.json"
locker.workout_data = {"type": "phone_verified"}
locker.unlock_screen()
# Check that after() was called to schedule close
locker.root.after.assert_called()
def test_lockout_starts_countdown(
self,
mock_tk: MagicMock,
mock_sys_exit: MagicMock,
tmp_path: Path,
) -> None:
"""Test lockout initializes countdown timer."""
locker = create_locker(mock_tk, tmp_path)
locker.lockout()
# lockout() sets remaining_time to lockout_time (10 in demo mode)
# then calls update_lockout_countdown() which decrements it by 1
assert locker.remaining_time == 9 # 10 - 1 after first update
def test_close_destroys_root_and_exits(
self,
mock_tk: MagicMock,
mock_sys_exit: MagicMock,
tmp_path: Path,
) -> None:
"""Test close destroys root window and exits."""
locker = create_locker(mock_tk, tmp_path)
locker.close()
locker.root.destroy.assert_called_once()
mock_sys_exit.assert_called_with(0)
class TestTimerLogic:
"""Tests for timer countdown logic."""
def test_update_lockout_countdown_decrements(
self,
mock_tk: MagicMock,
mock_sys_exit: MagicMock,
tmp_path: Path,
) -> None:
"""Test countdown decrements remaining time."""
locker = create_locker(mock_tk, tmp_path)
locker.remaining_time = 5
locker.countdown_label = MagicMock()
locker.update_lockout_countdown()
assert locker.remaining_time == 4
locker.root.after.assert_called_with(1000, locker.update_lockout_countdown)
def test_update_lockout_countdown_at_zero(
self,
mock_tk: MagicMock,
mock_sys_exit: MagicMock,
tmp_path: Path,
) -> None:
"""Test countdown at zero restarts phone check."""
locker = create_locker(mock_tk, tmp_path)
locker.remaining_time = 0
locker.countdown_label = MagicMock()
object.__setattr__(locker, "_start_phone_check", MagicMock())
locker.update_lockout_countdown()
locker._start_phone_check.assert_called_once()
class TestShowRetryAndSick:
"""Tests for _show_retry_and_sick method."""
def test_displays_buttons(
self, mock_tk: MagicMock, mock_sys_exit: MagicMock, tmp_path: Path
) -> None:
"""Test _show_retry_and_sick shows retry and sick buttons."""
locker = create_locker(mock_tk, tmp_path)
object.__setattr__(locker, "clear_container", MagicMock())
locker._show_retry_and_sick("Test message")
locker.clear_container.assert_called_once()
mock_tk.Label.assert_called()
mock_tk.Button.assert_called()
def test_healthy_sources_add_no_diagnosis_block(
self,
mock_tk: MagicMock,
mock_sys_exit: MagicMock,
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""With every source healthy the screen must not hedge.
The "why this may be wrong" panel exists for 2026-08-24, when the
machine genuinely could not see the workout. When it *can* see, an
unconditional caveat would teach the user to ignore the one message
that matters.
"""
_sync_client.clear_degraded_sources()
# A healthy machine has a readable backend AND a non-empty log, which
# this tmp_path fixture cannot provide -- an empty sync is itself a
# finding. Stub the collector so the branch under test is "nothing to
# report", not "no workouts synced".
monkeypatch.setattr(_ui_flows, "collect_source_findings", list)
locker = create_locker(mock_tk, tmp_path)
object.__setattr__(locker, "clear_container", MagicMock())
locker._show_retry_and_sick("Test message")
shown = " ".join(
str(call.kwargs.get("text", "")) for call in mock_tk.Label.call_args_list
)
assert "may be wrong" not in shown