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

174 lines
6.3 KiB
Python

"""Per-output fan-out: the point of the gatelock v0.2.0 migration.
Every other test in this suite runs against the single-output default, where
"built on every monitor" and "built on the primary and nowhere else" are
indistinguishable. These run on a two-monitor desk, so a loop that only ever
touched ``surfaces[0]`` fails here and nowhere else.
"""
from __future__ import annotations
import tkinter as tk
from typing import TYPE_CHECKING
from unittest.mock import MagicMock
import pytest
from screen_locker._surface_group import FrameGroup, WidgetGroup
from screen_locker.tests.conftest import TWO_OUTPUTS, create_locker
if TYPE_CHECKING:
from pathlib import Path
@pytest.mark.usefixtures("dual_output")
class TestLockerFansOut:
"""The locker's container group, on two live outputs."""
def test_one_container_per_output(
self, mock_tk: MagicMock, mock_sys_exit: MagicMock, tmp_path: Path
) -> None:
"""The premise the rest of this class rests on."""
del mock_sys_exit
locker = create_locker(mock_tk, tmp_path)
assert len(locker.container.surfaces) == len(TWO_OUTPUTS)
def test_a_label_is_built_on_every_monitor(
self, mock_tk: MagicMock, mock_sys_exit: MagicMock, tmp_path: Path
) -> None:
"""One ``_label`` call paints both screens, not just the primary."""
del mock_sys_exit
locker = create_locker(mock_tk, tmp_path)
mock_tk.Label.reset_mock()
locker._label("Great job!")
assert mock_tk.Label.call_count == len(TWO_OUTPUTS)
def test_a_button_is_built_on_every_monitor(
self, mock_tk: MagicMock, mock_sys_exit: MagicMock, tmp_path: Path
) -> None:
"""Buttons fan out through the row group they are parented to."""
del mock_sys_exit
locker = create_locker(mock_tk, tmp_path)
row = locker._button_row()
mock_tk.Button.reset_mock()
locker._button(
row, "OK", bg=locker._colors.palette.accent, command=lambda: None
)
assert mock_tk.Button.call_count == len(TWO_OUTPUTS)
def test_clearing_clears_every_monitor(
self, mock_tk: MagicMock, mock_sys_exit: MagicMock, tmp_path: Path
) -> None:
"""A screen change wipes both surfaces, not just the primary."""
del mock_sys_exit
locker = create_locker(mock_tk, tmp_path)
# conftest's tk mock hands out one shared Frame, so identity cannot
# tell the surfaces apart here -- call counts can. Two surfaces means
# two sweeps; a loop cropped to the primary would make it one.
child = MagicMock()
for frame in locker.container.surfaces:
frame.winfo_children.reset_mock()
frame.winfo_children.return_value = [child]
locker.clear_container()
surface = locker.container.surfaces[0]
assert surface.winfo_children.call_count == len(TWO_OUTPUTS)
assert child.destroy.call_count == len(TWO_OUTPUTS)
def test_a_dark_output_drops_only_its_own_container(
self, mock_tk: MagicMock, mock_sys_exit: MagicMock, tmp_path: Path
) -> None:
"""Teardown is by output name, so the surviving monitor keeps painting."""
del mock_sys_exit
locker = create_locker(mock_tk, tmp_path)
survivor = locker.container.surfaces[1]
locker.teardown_surface(MagicMock(output_name=TWO_OUTPUTS[0].name))
assert locker.container.surfaces == [survivor]
class TestGroupsInIsolation:
"""The group primitives, without a locker around them."""
def test_configure_reaches_a_dead_copy_without_raising(self) -> None:
"""A monitor that vanished mid-update must not break the others."""
dead, alive = MagicMock(), MagicMock()
dead.configure.side_effect = tk.TclError("bad window path name")
WidgetGroup([dead, alive]).configure(text="hi")
alive.configure.assert_called_once_with(text="hi")
def test_pack_and_destroy_survive_a_dead_copy(self) -> None:
"""Same tolerance for the other fan-out operations."""
dead, alive = MagicMock(), MagicMock()
dead.pack.side_effect = tk.TclError("gone")
dead.destroy.side_effect = tk.TclError("gone")
dead.pack_forget.side_effect = tk.TclError("gone")
group = WidgetGroup([dead, alive])
group.pack(pady=2)
group.pack_forget()
group.destroy()
alive.pack.assert_called_once_with(pady=2)
alive.pack_forget.assert_called_once_with()
alive.destroy.assert_called_once_with()
def test_clear_survives_a_dead_copy(self) -> None:
"""A frame whose window died is skipped, the live one is emptied."""
dead, alive = MagicMock(), MagicMock()
dead.winfo_children.side_effect = tk.TclError("gone")
child = MagicMock()
alive.winfo_children.return_value = [child]
FrameGroup([dead, alive]).clear()
child.destroy.assert_called_once_with()
def test_single_builds_a_group_of_one(self) -> None:
"""The non-lock windows' degenerate case."""
parent = MagicMock()
group = FrameGroup.single(parent, bg="#000000")
assert len(group.surfaces) == 1
group.first.place.assert_called_once()
def test_discard_is_a_no_op_for_an_unknown_output(self) -> None:
"""Tearing down an output that was never built changes nothing."""
group = FrameGroup([])
frame = MagicMock()
group.add(frame, "DP-0")
group.discard("HDMI-9")
assert group.surfaces == [frame]
def test_child_frame_and_widgets_nest(self) -> None:
"""A group can parent another group, which is what button rows need."""
group = FrameGroup([MagicMock(), MagicMock()])
row = group.child_frame(bg="#000000")
labels = row.child_widgets(lambda _parent, **_kw: MagicMock(), text="hi")
assert len(row.surfaces) == 2
assert len(list(labels)) == 2
def test_winfo_helpers_answer_from_the_primary(self) -> None:
"""Reads that cannot fan out use the primary monitor's copy."""
first, second = MagicMock(), MagicMock()
child = MagicMock()
first.winfo_children.return_value = [child]
group = FrameGroup([first, second])
assert group.winfo_children() == [child]
assert group.winfo_toplevel() is first.winfo_toplevel.return_value