earned-time-v0.2.0 (anki); shutdown projection prices each day on its own date

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Krzysztof Rudnicki 2026-10-05 14:18:42 +02:00
parent 275a0862fc
commit 2dd719e6a4
7 changed files with 59 additions and 32 deletions

View File

@ -134,7 +134,7 @@ repos:
# than merely an old one.
- gatelock @ git+https://github.com/kuhyx/utils@gatelock-v0.9.0#subdirectory=gatelock
- crdt-sync @ git+https://github.com/kuhyx/utils@crdt-sync-v0.9.1#subdirectory=crdt-sync
- earned-time @ git+https://github.com/kuhyx/utils@earned-time-v0.1.0#subdirectory=earned_time
- earned-time @ git+https://github.com/kuhyx/utils@earned-time-v0.2.0#subdirectory=earned_time
- freedays @ git+https://github.com/kuhyx/utils@freedays-v0.3.0#subdirectory=freedays
- mcp>=2,<3
- pydantic
@ -155,7 +155,7 @@ repos:
# than merely an old one.
- gatelock @ git+https://github.com/kuhyx/utils@gatelock-v0.9.0#subdirectory=gatelock
- crdt-sync @ git+https://github.com/kuhyx/utils@crdt-sync-v0.9.1#subdirectory=crdt-sync
- earned-time @ git+https://github.com/kuhyx/utils@earned-time-v0.1.0#subdirectory=earned_time
- earned-time @ git+https://github.com/kuhyx/utils@earned-time-v0.2.0#subdirectory=earned_time
- freedays @ git+https://github.com/kuhyx/utils@freedays-v0.3.0#subdirectory=freedays
- mcp>=2,<3
- pydantic

View File

@ -10,7 +10,7 @@ dependencies = [
"gatelock @ git+https://github.com/kuhyx/utils@gatelock-v0.9.0#subdirectory=gatelock",
"crdt-sync @ git+https://github.com/kuhyx/utils@crdt-sync-v0.9.1#subdirectory=crdt-sync",
"freedays @ git+https://github.com/kuhyx/utils@freedays-v0.3.0#subdirectory=freedays",
"earned-time @ git+https://github.com/kuhyx/utils@earned-time-v0.1.0#subdirectory=earned_time",
"earned-time @ git+https://github.com/kuhyx/utils@earned-time-v0.2.0#subdirectory=earned_time",
]
[project.scripts]

View File

@ -4,7 +4,7 @@ bandit==1.9.4
codespell==2.4.3
coverage==7.16.2
crdt-sync @ git+https://github.com/kuhyx/utils@crdt-sync-v0.9.1#subdirectory=crdt-sync
earned-time @ git+https://github.com/kuhyx/utils@earned-time-v0.1.0#subdirectory=earned_time
earned-time @ git+https://github.com/kuhyx/utils@earned-time-v0.2.0#subdirectory=earned_time
freedays @ git+https://github.com/kuhyx/utils@freedays-v0.3.0#subdirectory=freedays
gatelock @ git+https://github.com/kuhyx/utils@gatelock-v0.9.0#subdirectory=gatelock
mcp==2.3.0

View File

@ -10,7 +10,7 @@ and renders the compact status line.
from __future__ import annotations
from datetime import datetime
from datetime import date, datetime, timedelta
from typing import TYPE_CHECKING
from screen_locker._earned import hhmm
@ -41,13 +41,17 @@ _MINUTES_PER_HOUR = 60
def _week_rows(
mon_wed: int, thu_sun: int, *, speculative: bool
monday: date, bonus: int, *, speculative: bool
) -> tuple[ShutdownProjectionDay, ...]:
"""Build 7 labeled rows, one per weekday, from a Mon-Wed/Thu-Sun band pair."""
"""Build 7 labeled rows, Mon-Sun from ``monday``, each on its own day's base.
Each row asks ``base_minutes`` for its own date, so a registry cut dated
mid-week (or next week) shows on the day it lands, not from today's base.
"""
return tuple(
ShutdownProjectionDay(
label=label,
minutes=mon_wed if weekday in _MON_WED_WEEKDAYS else thu_sun,
minutes=base_minutes(monday + timedelta(days=weekday)) + bonus,
speculative=speculative,
)
for weekday, label in enumerate(_WEEKDAY_LABELS)
@ -67,16 +71,19 @@ def _shutdown_projection(
weekly_shutdown_bonus_hours(extra_benefits_file, today=today_local)
* _MINUTES_PER_HOUR
)
base = base_minutes(today_local.date())
rest_of_week = _week_rows(base + bonus, base + bonus, speculative=False)
monday = today_local.date() - timedelta(days=today_local.weekday())
rest_of_week = _week_rows(monday, bonus, speculative=False)
this_week_count = count_weekly_workouts(log_file, today=today_local)
streak = current_streak(extra_benefits_file)
_would_be_streak, would_be_bonus = preview_bonus_if_week_ended_now(
this_week_count, streak
)
would_be = base + would_be_bonus * _MINUTES_PER_HOUR
next_week_preview = _week_rows(would_be, would_be, speculative=True)
next_week_preview = _week_rows(
monday + timedelta(weeks=1),
would_be_bonus * _MINUTES_PER_HOUR,
speculative=True,
)
return ShutdownProjection(
tonight=tonight,

View File

@ -101,7 +101,11 @@ class TestLedgerFile:
class TestFlatEarners:
def test_registry_order_without_the_counted_workout(self) -> None:
assert flat_earners() == (earned_time.LEETCODE, earned_time.READING)
assert flat_earners() == (
earned_time.LEETCODE,
earned_time.READING,
earned_time.ANKI,
)
def test_a_newly_registered_earner_is_picked_up(
self, monkeypatch: pytest.MonkeyPatch
@ -211,7 +215,7 @@ class TestCannotCheck:
class TestFlatAnswers:
def test_every_flat_earner_answers_by_name(self, key: Path) -> None:
write_ledger(earned_time.LEETCODE, [credit(earned_time.LEETCODE)])
assert flat_answers() == {"leetcode": True, "reading": False}
assert flat_answers() == {"leetcode": True, "reading": False, "anki": False}
def test_an_unknown_answer_is_none_and_warned(
self,
@ -221,14 +225,19 @@ class TestFlatAnswers:
) -> None:
monkeypatch.setattr(_earned, "HMAC_KEY_FILE", tmp_path / "no.key")
with caplog.at_level(logging.WARNING):
assert flat_answers() == {"leetcode": None, "reading": None}
assert flat_answers() == {"leetcode": None, "reading": None, "anki": None}
assert "LeetCode state could not be checked" in caplog.text
assert "reading state could not be checked" in caplog.text
assert "Anki state could not be checked" in caplog.text
def test_a_no_is_not_warned(
self, key: Path, caplog: pytest.LogCaptureFixture
) -> None:
write_ledger(earned_time.LEETCODE, [])
with caplog.at_level(logging.WARNING):
assert flat_answers() == {"leetcode": False, "reading": False}
assert flat_answers() == {
"leetcode": False,
"reading": False,
"anki": False,
}
assert "could not be checked" not in caplog.text

View File

@ -36,6 +36,8 @@ if TYPE_CHECKING:
from collections.abc import Iterator
from pathlib import Path
from screen_locker._status_types import ShutdownProjection
def _mixin() -> MagicMock:
mixin = MagicMock()
@ -160,19 +162,26 @@ class TestRealLedgers:
class TestProjectionFollowsTheCut:
@pytest.mark.parametrize(
("now", "expected"),
[
(datetime(2026, 9, 30, 12, 0, tzinfo=UTC), 20 * 60),
(datetime(2026, 10, 2, 12, 0, tzinfo=UTC), 19 * 60),
],
)
def test_rest_of_week_uses_the_base_for_that_day(
self, tmp_path: Path, now: datetime, expected: int
) -> None:
"""Every projected row is priced on its own date, not on today's base."""
def _shutdown(self, tmp_path: Path, now: datetime) -> ShutdownProjection:
with patch(
"screen_locker._status_data.has_workout_skip_today", return_value=False
):
snap = gather_status(**_files(tmp_path), now=now)
assert snap.shutdown.rest_of_week[0].minutes == expected
assert snap.shutdown.next_week_preview[0].minutes == expected
return gather_status(**_files(tmp_path), now=now).shutdown
def test_the_reading_cut_lands_mid_week(self, tmp_path: Path) -> None:
"""Wed 2026-09-30 is the last 20:00 day; Thu 2026-10-01 is the first 19:00."""
shutdown = self._shutdown(tmp_path, datetime(2026, 9, 30, 12, 0, tzinfo=UTC))
assert [d.minutes for d in shutdown.rest_of_week] == [20 * 60] * 3 + [
19 * 60
] * 4
def test_next_week_preview_includes_a_cut_dated_after_today(
self, tmp_path: Path
) -> None:
"""On Mon 2026-10-05, Anki's cut from Tue already shows; next week has it."""
shutdown = self._shutdown(tmp_path, datetime(2026, 10, 5, 12, 0, tzinfo=UTC))
lowered = 19 * 60 - earned_time.ANKI.shutdown_minutes
assert [d.minutes for d in shutdown.rest_of_week] == [19 * 60] + [lowered] * 6
assert [d.minutes for d in shutdown.next_week_preview] == [lowered] * 7

View File

@ -61,8 +61,10 @@ class TestBaseHour:
assert earned_time.READING.penalty_from == date(2026, 10, 1)
assert base_minutes(date(2026, 10, 1)) == 19 * 60
def test_long_after_the_cut_stays_nineteen(self) -> None:
assert base_minutes(date(2030, 1, 1)) == 19 * 60
def test_long_after_the_cut_stays_lowered(self) -> None:
"""No drift back to 20:00; Anki's cut (from 2026-10-06) holds too."""
later_cuts = earned_time.ANKI.shutdown_minutes
assert base_minutes(date(2030, 1, 1)) == 19 * 60 - later_cuts
def test_default_is_the_local_today(self) -> None:
assert base_minutes() == base_minutes(datetime.now().astimezone().date())
@ -148,7 +150,7 @@ class TestResetIncludesReading:
base = base_minutes()
assert (
f"Daily base reset: {hhmm(base + 60)} (base {hhmm(base)} + 0h workout + "
"0h LeetCode + 1h reading already earned today)."
"0h LeetCode + 1h reading + 0h Anki already earned today)."
) in caplog.text