screen-locker/screen_locker/tests/_isolated_state.py
Krzysztof kuhy Rudnicki d6e8c0973f Replace scheduled skips with the shared free-day pool
Fifth and last app onto freedays (~/utils/freedays). screen-locker is the
one that already had this idea: scheduled_skips.json was a bare list of
dates, checked first in the lock chain, that only this app could see.
That is exactly what the shared pool generalises, so the chain now asks
freedays and a day taken here also stands down diet-guard, wake-alarm,
leetcode-guard and home-guard.

Sick days are deliberately untouched. Their rolling windows (1/7d, 3/30d,
10/90d), the 120-character justification and the escalating countdown all
exist to make a sick day *hard* to take. Free days are the opposite --
declared ahead, no reason, consecutive runs allowed. Merging the two
policies would have had to ruin one of them, so only the storage of the
frictionless half moved.

This fixes a real bug in passing. is_scheduled_skip_today compared
against a UTC day while add_scheduled_skip.py wrote a local one, so a
skip added near midnight could name a date the chain never matched.
freedays owns "what is today" for both sides now, in local time, and the
disagreement cannot recur.

scripts/migrate_scheduled_skips.py moves the existing dates across; it is
idempotent and has a --dry-run. Already run here: the 8 real skips
(2026-05-19/20/21, 05-29, 05-30, 08-17, 08-22, 08-29) are in the pool and
count against 2026, leaving 27 of 35. The original file is gitignored and
was left on disk untouched.

add_scheduled_skip.py becomes a thin wrapper over freedays rather than
being deleted, because arm.sh calls it and the name is in muscle memory.
Left as-is it would have kept writing to a file nothing reads.

conftest.py went over the 250-line cap, so the two fixtures that keep the
suite off real user state moved to _real_state_fixtures.py. They belong
together and neither subsumes the other: _isolate_home covers paths
resolved from Path.home() at call time, while the freedays redirect
covers paths bound into module constants at import time, which
redirecting home afterwards cannot reach.

requirements.txt also takes pytest-randomly 5.0.0, ruff 0.16.6 and
types-requests 2.33.0.20260906, and the pylint hooks' gatelock/crdt-sync
pins move off v0.7.1/v0.9.0 -- pip refuses two direct URLs for one
distribution, so those stale pins would have made the hook environments
unresolvable rather than merely old.

1256 tests, 100% branch coverage.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-07 15:42:41 +02:00

68 lines
2.3 KiB
Python

"""On-disk state paths every test must redirect away from the real files.
Split out of ``conftest.py`` (250-line cap) -- the *fixture* itself has to
stay in ``conftest.py`` (pytest only applies autouse fixtures declared
there), but this static table has no such constraint.
"""
from __future__ import annotations
# Each on-disk state path, and every module that bound it by value at import
# time. All of them need patching, not just the _constants source -- a missed
# binding lets a test write to the real file.
ISOLATED_STATE: tuple[tuple[str, tuple[str, ...]], ...] = (
(
"sick_history.json",
("_sick_tracker.SICK_HISTORY_FILE", "_constants.SICK_HISTORY_FILE"),
),
(
"early_bird_pending.json",
(
"_early_bird.EARLY_BIRD_PENDING_FILE",
"_constants.EARLY_BIRD_PENDING_FILE",
),
),
(
"extra_benefits_state.json",
(
"_constants.EXTRA_BENEFITS_FILE",
"_startup_checks.EXTRA_BENEFITS_FILE",
"_sync_mixin.EXTRA_BENEFITS_FILE",
"_early_bird.EXTRA_BENEFITS_FILE",
"_status.EXTRA_BENEFITS_FILE",
),
),
(
"sick_day_state.json",
(
"_constants.SICK_DAY_STATE_FILE",
"_startup_checks.SICK_DAY_STATE_FILE",
"_shutdown_sick_state.SICK_DAY_STATE_FILE",
),
),
# The durable lock-decision trail. Written on EVERY locker run, so without
# this the suite would append test decisions to the user's real
# enforcement history in ~/.local/share/screen_locker/.
# BOTH bindings: _decision_trail owns the writer and reads its own
# module global, so patching only _decision_log would silently let
# the suite write into the real enforcement history.
(
"decisions.jsonl",
(
"_decision_log.DECISION_LOG_FILE",
"_decision_trail.DECISION_LOG_FILE",
),
),
# Written whenever a locker run waits in gatelock's queue.
(
"queue_state.json",
("_constants.QUEUE_STATE_FILE", "_queue_state.QUEUE_STATE_FILE"),
),
# Real $XDG_RUNTIME_DIR/gatelock file; unisolated, only the first test in
# the whole suite would win it, since it is shared across all of them.
(
"instance.lock",
("_constants.INSTANCE_LOCK_FILE", "screen_lock.INSTANCE_LOCK_FILE"),
),
)