screen-locker/screen_locker/tests/test_decision_reasons.py
Krzysztof kuhy Rudnicki 589b9e227d
Some checks failed
pre-commit / pre-commit (push) Has been cancelled
Tests / test (3.10) (push) Has been cancelled
Tests / test (3.11) (push) Has been cancelled
Tests / test (3.12) (push) Has been cancelled
Tests / lock-popup-safety (push) Has been cancelled
release-apk / release (APK) (push) Has been cancelled
Say every reason a lock decision had, not just the first one
The decision ladders are first-match-wins: the matching branch calls
sys.exit(0), so no later condition is ever evaluated or recorded. That made

    DECISION lock=no reason=early_bird_window_active

ambiguous in the one way that matters -- it cannot say whether the workout was
also already done, or the weekly minimum already met. Reading it, the 08:30
re-check looks like it is about to lock a machine that has nothing left to
prove, and the only way to find out was to re-derive the ladder by hand.

Keep the acting reason exactly as it was: it is the branch that actually
controlled behaviour, and it stays a stable grep-able slug that can be counted
across runs. Add the conditions that also held as an "also=" extra, which
as_line() already renders as key=value. A decision with a single reason still
renders the line it always did, so the common case needs no new format.

Only side-effect-free predicates are collected. _try_auto_upgrade_* writes log
entries and does ADB/network work, and _save_early_bird_pending writes state;
running either to produce a log line would turn a reporting change into a
state mutation in the enforcer. Verified that annotating leaves log.json and
early_bird_pending.json byte-identical.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PCUN9mdfJzJXqEZatHnUCD
2026-08-23 07:02:41 +02:00

79 lines
2.5 KiB
Python

"""Tests for the multi-reason annotation on lock decisions."""
from __future__ import annotations
import logging
from typing import TYPE_CHECKING
from screen_locker._decision_reasons import collect_reasons, reasons_extra
if TYPE_CHECKING:
import pytest
class TestCollectReasons:
"""Every condition that holds is reported, not just the first."""
def test_returns_only_the_predicates_that_hold(self) -> None:
held = collect_reasons(
{"a": lambda: True, "b": lambda: False, "c": lambda: True}
)
assert held == ["a", "c"]
def test_empty_when_nothing_holds(self) -> None:
assert collect_reasons({"a": lambda: False}) == []
def test_a_failing_predicate_is_reported_and_skipped(
self, caplog: pytest.LogCaptureFixture
) -> None:
"""A broken annotation must not take down the decision it annotates."""
def boom() -> bool:
msg = "log unreadable"
raise OSError(msg)
with caplog.at_level(logging.WARNING):
held = collect_reasons({"a": lambda: True, "boom": boom})
assert held == ["a"]
# The omission must be loud: an invisible gap would read as "the
# condition did not hold", which is the opposite of what happened.
assert "boom" in caplog.text
assert "log unreadable" in caplog.text
class TestReasonsExtra:
"""The acting reason is never repeated back as an 'also'."""
def test_excludes_the_acting_reason(self) -> None:
extra = reasons_extra(
"early_bird_window_active",
{
"early_bird_window_active": lambda: True,
"workout_logged_today": lambda: True,
},
)
assert extra == {"also": "workout_logged_today"}
def test_orders_and_joins_every_other_condition(self) -> None:
extra = reasons_extra(
"early_bird_window_active",
{
"early_bird_window_active": lambda: True,
"workout_logged_today": lambda: True,
"weekly_minimum_met": lambda: True,
},
)
assert extra == {"also": "workout_logged_today,weekly_minimum_met"}
def test_empty_when_the_acting_reason_is_the_only_one(self) -> None:
"""A single-reason decision keeps rendering exactly its old line."""
extra = reasons_extra(
"early_bird_window_active",
{
"early_bird_window_active": lambda: True,
"workout_logged_today": lambda: False,
},
)
assert extra == {}