Show the run's reward in the credit toast

A late credit moves shutdown from an hour already past to the run's
end plus its reward, so the toast read "+3h58m" for a 2h run
(18:00 -> 21:58, 2026-11-18). The delta is now capped at the reward
the credit earned: still the real move when the ceiling cut it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WzxdWWLoUxxSPf1m8KUVo8
This commit is contained in:
Krzysztof Rudnicki 2026-11-18 20:53:52 +01:00
parent 95adc8699c
commit 7539a82429
4 changed files with 35 additions and 11 deletions

View File

@ -38,11 +38,16 @@ _NOTIFY_TIMEOUT_SECONDS: Final = 5
def credit_body(
before: tuple[int, int, int] | None,
after: tuple[int, int, int] | None,
reward: int | None = None,
) -> str:
"""``⏻ 23:00 (+2h)`` -- whichever parts are known.
``before``/``after`` are the shutdown config around the credit; the delta
is tonight's actual move, so a capped or rest-day credit shows ``+0h``.
``before``/``after`` are the shutdown config around the credit. The delta
is what the workout earned: tonight's actual move, so a capped or rest-day
credit shows ``+0h``, but never more than ``reward``. A late credit moves
the shutdown from an hour already past to the run's end plus the reward
(:mod:`._late_credit`); that jump is not what the run earned
(18:00 -> 21:58 read "+3h58m" for a 2h run, 2026-11-18).
"""
parts: list[str] = []
if after is None:
@ -56,6 +61,8 @@ def credit_body(
text = f"{_SHUTDOWN} {hhmm(tonight)}"
if before is not None:
moved = max(0, tonight - tonight_minutes(before, day))
if reward is not None:
moved = min(moved, reward)
text += f" (+{span(moved)})"
parts.append(text)
return " \N{MIDDLE DOT} ".join(parts) or "\N{CHECK MARK}"
@ -93,7 +100,8 @@ def send_notification(title: str, body: str) -> bool:
def notify_workout_credit(
before: tuple[int, int, int] | None,
after: tuple[int, int, int] | None,
reward: int | None,
) -> bool:
"""Notify a newly earned workout credit. Never raises."""
body = credit_body(before, after)
body = credit_body(before, after, reward)
return send_notification(TITLE, body)

View File

@ -20,7 +20,7 @@ from screen_locker import _credit_notify
from screen_locker._constants import SHUTDOWN_BASE_FILE
from screen_locker._credit_slot import CreditSlot, credit_slot, locked_shutdown_state
from screen_locker._day import today_str
from screen_locker._earned import extra_minutes
from screen_locker._earned import extra_minutes, first_minutes
from screen_locker._rest_day import is_rest_day
from screen_locker._weekly_check import COUNTED_WORKOUT_TYPES, count_weekly_workouts
from screen_locker._workout_ledger import entry_done_at
@ -160,7 +160,12 @@ class WorkoutCreditMixin:
after = self._read_shutdown_config()
if slot is not CreditSlot.NONE:
self._notify_credit(before, after)
reward = (
first_minutes(earned_time.WORKOUT, day)
if first_counted_today
else extra
)
self._notify_credit(before, after, reward)
return WorkoutCreditResult(
shutdown_adjusted=shutdown_adjusted,
@ -173,6 +178,7 @@ class WorkoutCreditMixin:
self,
before: tuple[int, int, int] | None,
after: tuple[int, int, int] | None,
reward: int | None,
) -> None:
"""Pop the "Workout credited" notification; never fails the credit.
@ -182,7 +188,7 @@ class WorkoutCreditMixin:
batch uncredited, so every exception is logged and dropped here.
"""
try:
_credit_notify.notify_workout_credit(before, after)
_credit_notify.notify_workout_credit(before, after, reward)
except Exception: # pylint: disable=broad-exception-caught # notification must never fail the credit
_logger.exception(
"Workout credit notification failed — the credit itself is "

View File

@ -55,6 +55,16 @@ class TestCreditBody:
body = _credit_notify.credit_body((0, 1380, 0), (0, 1320, 0))
assert body == f"{_SHUTDOWN} 22:00 (+0h)"
def test_a_late_credit_shows_the_reward_not_the_jump(self) -> None:
"""18:00 -> 21:58 after a run ending 19:58: the toast says +2h, not +3h58m."""
body = _credit_notify.credit_body((0, 1080, 0), (0, 1318, 0), reward=120)
assert body == f"{_SHUTDOWN} 21:58 (+2h)"
def test_a_ceiling_cut_reward_shows_what_was_left(self) -> None:
"""A 2h reward that met the 23:00 ceiling after 1h shows +1h."""
body = _credit_notify.credit_body((0, 1320, 0), (0, 1380, 0), reward=120)
assert body == f"{_SHUTDOWN} 23:00 (+1h)"
def test_unknown_before_drops_the_delta(self) -> None:
"""Without the old config there is no move to show."""
assert _credit_notify.credit_body(None, (0, 1320, 0)) == f"{_SHUTDOWN} 22:00"
@ -132,8 +142,8 @@ def test_notify_workout_credit_sends_the_composed_body() -> None:
with (
patch(f"{_MOD}.send_notification", return_value=True) as send,
):
assert _real_notify((0, 1200, 0), (0, 1260, 0))
send.assert_called_once_with(_credit_notify.TITLE, f"{_SHUTDOWN} 21:00 (+1h)")
assert _real_notify((0, 1080, 0), (0, 1318, 0), 120)
send.assert_called_once_with(_credit_notify.TITLE, f"{_SHUTDOWN} 21:58 (+2h)")
class TestCreditSideOfTheNotifier:
@ -149,7 +159,7 @@ class TestCreditSideOfTheNotifier:
"""Anything the notifier raises is logged at ERROR and dropped."""
locker = create_locker(mock_tk, tmp_path)
with patch(f"{_MOD}.notify_workout_credit", side_effect=KeyError("x")):
locker._notify_credit(None, None)
locker._notify_credit(None, None, None)
assert any(
r.levelno == logging.ERROR and "notification failed" in r.getMessage()
for r in caplog.records

View File

@ -129,7 +129,7 @@ class TestSlotCreditMinutes:
result, notify = _credit(locker, [])
assert result.shutdown_adjusted is True
assert locker._read_shutdown_config() == (1320, 1320, 300)
notify.assert_called_once_with(_START, (1320, 1320, 300))
notify.assert_called_once_with(_START, (1320, 1320, 300), 120)
def test_distinct_slot_earns_one_hour(
self, mock_tk: MagicMock, mock_sys_exit: MagicMock, tmp_path: Path
@ -139,7 +139,7 @@ class TestSlotCreditMinutes:
result, notify = _credit(locker, [_entry("phone_verified")])
assert result.extra_bonus_delta == 60
assert locker._read_shutdown_config() == (1260, 1260, 300)
notify.assert_called_once()
notify.assert_called_once_with(_START, (1260, 1260, 300), 60)
def test_same_slot_earns_nothing_and_warns(
self,