screen-locker/screen_locker/tests/test_verify_workout.py
Krzysztof kuhy Rudnicki 600d0fd084 Migrate screen_locker to gatelock v0.2.0 per-output locking
The lock now paints one container per live output instead of one window on
the primary, and claims the screen through an Arbiter at RANK_SCREEN_LOCKER
so the wake alarm can outrank it.

The fan-out happens at the widget factory, not the render loop. Painting a
screen here is not a pure function -- the ~20 clear_container() flows record
workouts, credit weeks and branch on state while they paint -- so re-running
a flow per surface would double those side effects. Instead `_label`,
`_button_row` and friends build N copies and return a group that mirrors
later configure calls to all of them, and a flow body still runs once. Frame
groups nest, so `_button(button_row, ...)` reads exactly as it did.

The degenerate case carries as much weight as the general one: the status
window, the post-sick-day verify window and the relaxed-day prompt have no
lock and no surfaces, and share this same factory. They get a group of
exactly one frame and behave as they always did.

Text boxes are the one thing that cannot be shared. Entries fan out over a
single StringVar, but tk.Text has no such variable, so the sick-day
description genuinely diverges per monitor; TextGroup.get() returns whichever
copy was actually typed into, preserving "answer on any monitor".

Tests: the suite hung rather than failed on first run, which is the trap
gatelock's own conftest documents -- screen_locker patches its own `tk`, but
LockWindow builds Toplevels through gatelock's, and real tkinter over a
MagicMock root recurses without bound. `_gatelock_fixtures` closes that and
redirects the runtime dir so no test can stand a production locker down.
Every other test runs single-output, where fan-out is unobservable, so
test_surface_fanout runs on two outputs; each assertion was checked by
cropping the group loops to the primary and confirming it goes red.

874 tests, 100% branch coverage, pre-commit clean, no lint suppressions.
2026-07-25 16:26:39 +02:00

342 lines
9.9 KiB
Python

"""Tests for post-sick-day workout verification (--verify-workout)."""
from __future__ import annotations
from datetime import datetime, timezone
import json
from typing import TYPE_CHECKING
from unittest.mock import MagicMock
import pytest
from screen_locker.tests.conftest import create_locker
if TYPE_CHECKING:
from pathlib import Path
class TestIsSickDayToday:
"""Tests for _is_sick_day_today method.
sick_day is tracked in sick_history.json (via _sick_tracker.py) as the
sole source of truth -- not in workout_log.json. The autouse
_isolate_sick_history fixture redirects SICK_HISTORY_FILE to
tmp_path/sick_history.json for every test.
"""
def test_no_history_file(
self,
mock_tk: MagicMock,
mock_sys_exit: MagicMock,
tmp_path: Path,
) -> None:
"""Return False when sick_history.json does not exist."""
locker = create_locker(mock_tk, tmp_path)
assert locker._is_sick_day_today() is False
def test_today_not_sick_day(
self,
mock_tk: MagicMock,
mock_sys_exit: MagicMock,
tmp_path: Path,
) -> None:
"""Return False when today is not in sick_history's sick_days list."""
locker = create_locker(mock_tk, tmp_path)
history_file = tmp_path / "sick_history.json"
history_file.write_text(json.dumps({"sick_days": ["2020-01-01"]}))
assert locker._is_sick_day_today() is False
def test_today_is_sick_day(
self,
mock_tk: MagicMock,
mock_sys_exit: MagicMock,
tmp_path: Path,
) -> None:
"""Return True when today is in sick_history's sick_days list."""
locker = create_locker(mock_tk, tmp_path)
today = datetime.now(tz=timezone.utc).strftime("%Y-%m-%d")
history_file = tmp_path / "sick_history.json"
history_file.write_text(json.dumps({"sick_days": [today]}))
assert locker._is_sick_day_today() is True
class TestVerifyOnlyInit:
"""Tests for ScreenLocker initialization with verify_only=True."""
def test_verify_only_exits_when_no_sick_day(
self,
mock_tk: MagicMock,
mock_sys_exit: MagicMock,
tmp_path: Path,
) -> None:
"""Exit when verify_only but no sick day logged today."""
mock_sys_exit.side_effect = SystemExit(0)
with pytest.raises(SystemExit):
create_locker(
mock_tk,
tmp_path,
verify_only=True,
is_sick_day_log=False,
)
mock_sys_exit.assert_called_once_with(0)
def test_verify_only_starts_when_sick_day(
self,
mock_tk: MagicMock,
mock_sys_exit: MagicMock,
tmp_path: Path,
) -> None:
"""Start verification window when sick day is logged."""
locker = create_locker(
mock_tk,
tmp_path,
verify_only=True,
is_sick_day_log=True,
)
assert locker.verify_only is True
mock_sys_exit.assert_not_called()
def test_verify_only_sets_title(
self,
mock_tk: MagicMock,
mock_sys_exit: MagicMock,
tmp_path: Path,
) -> None:
"""Verify window title includes [VERIFY]."""
locker = create_locker(
mock_tk,
tmp_path,
verify_only=True,
is_sick_day_log=True,
)
locker.root.title.assert_called_with("Workout Locker [VERIFY]")
def test_close_and_run_use_root_directly(
self,
mock_tk: MagicMock,
mock_sys_exit: MagicMock,
tmp_path: Path,
) -> None:
"""No LockWindow is built for verify_only; close()/run() use root."""
locker = create_locker(
mock_tk,
tmp_path,
verify_only=True,
is_sick_day_log=True,
)
assert locker._lock is None
locker.run()
locker.root.mainloop.assert_called_once()
locker.close()
locker.root.destroy.assert_called_once()
class TestSetupVerifyWindow:
"""Tests for _setup_verify_window."""
def test_sets_geometry_and_protocol(
self,
mock_tk: MagicMock,
mock_sys_exit: MagicMock,
tmp_path: Path,
) -> None:
"""Verify window uses 600x400 geometry and WM_DELETE_WINDOW."""
locker = create_locker(
mock_tk,
tmp_path,
verify_only=True,
is_sick_day_log=True,
)
locker.root.geometry.assert_called_with("600x400")
locker.root.configure.assert_called_with(
bg=locker._colors.bg,
cursor="arrow",
)
locker.root.protocol.assert_called_with(
"WM_DELETE_WINDOW",
locker.close,
)
class TestStartVerifyWorkoutCheck:
"""Tests for _start_verify_workout_check."""
def test_starts_phone_check_and_polls(
self,
mock_tk: MagicMock,
mock_sys_exit: MagicMock,
tmp_path: Path,
) -> None:
"""Start phone verification and begin polling."""
locker = create_locker(mock_tk, tmp_path)
object.__setattr__(
locker,
"_verify_phone_workout",
MagicMock(return_value=("verified", "ok")),
)
object.__setattr__(
locker,
"_poll_verify_workout_check",
MagicMock(),
)
locker._start_verify_workout_check()
assert locker._phone_future is not None
locker._poll_verify_workout_check.assert_called_once()
class TestPollVerifyWorkoutCheck:
"""Tests for _poll_verify_workout_check."""
def test_schedules_retry_when_not_done(
self,
mock_tk: MagicMock,
mock_sys_exit: MagicMock,
tmp_path: Path,
) -> None:
"""Re-schedule polling when future is not done."""
locker = create_locker(mock_tk, tmp_path)
mock_future = MagicMock()
mock_future.done.return_value = False
locker._phone_future = mock_future
locker._poll_verify_workout_check()
locker.root.after.assert_called_with(
500,
locker._poll_verify_workout_check,
)
def test_handles_result_when_done(
self,
mock_tk: MagicMock,
mock_sys_exit: MagicMock,
tmp_path: Path,
) -> None:
"""Route to result handler when future is done."""
locker = create_locker(mock_tk, tmp_path)
mock_future = MagicMock()
mock_future.done.return_value = True
mock_future.result.return_value = ("verified", "Found workout")
locker._phone_future = mock_future
object.__setattr__(
locker,
"_handle_verify_workout_result",
MagicMock(),
)
locker._poll_verify_workout_check()
locker._handle_verify_workout_result.assert_called_once_with(
"verified",
"Found workout",
)
class TestHandleVerifyWorkoutResult:
"""Tests for _handle_verify_workout_result."""
def test_verified_adjusts_shutdown_and_saves(
self,
mock_tk: MagicMock,
mock_sys_exit: MagicMock,
tmp_path: Path,
) -> None:
"""On verified: adjust shutdown, save log, show success."""
locker = create_locker(mock_tk, tmp_path)
locker.log_file = tmp_path / "workout_log.json"
object.__setattr__(
locker,
"_adjust_shutdown_time_later",
MagicMock(return_value=True),
)
locker._handle_verify_workout_result("verified", "1 session found")
assert locker.workout_data["type"] == "phone_verified"
assert locker.workout_data["after_sick_day"] == "true"
locker._adjust_shutdown_time_later.assert_called_once()
locker.root.after.assert_called()
def test_verified_without_adjustment(
self,
mock_tk: MagicMock,
mock_sys_exit: MagicMock,
tmp_path: Path,
) -> None:
"""On verified but adjustment fails: still saves and shows success."""
locker = create_locker(mock_tk, tmp_path)
locker.log_file = tmp_path / "workout_log.json"
object.__setattr__(
locker,
"_adjust_shutdown_time_later",
MagicMock(return_value=False),
)
locker._handle_verify_workout_result("verified", "1 session found")
assert locker.workout_data["type"] == "phone_verified"
locker.root.after.assert_called()
def test_not_verified_shows_retry(
self,
mock_tk: MagicMock,
mock_sys_exit: MagicMock,
tmp_path: Path,
) -> None:
"""On not_verified: show retry screen."""
locker = create_locker(mock_tk, tmp_path)
object.__setattr__(
locker,
"_show_verify_retry",
MagicMock(),
)
locker._handle_verify_workout_result(
"not_verified",
"No workout today",
)
locker._show_verify_retry.assert_called_once_with(
"No workout today",
)
def test_error_shows_retry(
self,
mock_tk: MagicMock,
mock_sys_exit: MagicMock,
tmp_path: Path,
) -> None:
"""On error: show retry screen."""
locker = create_locker(mock_tk, tmp_path)
object.__setattr__(
locker,
"_show_verify_retry",
MagicMock(),
)
locker._handle_verify_workout_result("error", "ADB failed")
locker._show_verify_retry.assert_called_once_with("ADB failed")
class TestShowVerifyRetry:
"""Tests for _show_verify_retry."""
def test_shows_retry_and_close_buttons(
self,
mock_tk: MagicMock,
mock_sys_exit: MagicMock,
tmp_path: Path,
) -> None:
"""Show TRY AGAIN and Close buttons."""
locker = create_locker(mock_tk, tmp_path)
locker._show_verify_retry("No workout found")
# Verify container was cleared and buttons were packed
locker.container.first.winfo_children.return_value = []