diff --git a/earned_time/README.md b/earned_time/README.md index 4ec5501..b81873e 100644 --- a/earned_time/README.md +++ b/earned_time/README.md @@ -25,7 +25,7 @@ fact — HMAC-signed `credit` rows in its ledger — and is registered here once label="Anki", gaming_minutes=30, shutdown_minutes=30, - penalty_from=date(2026, 10, 10), # base drops by the same 30/30 from then on + penalty_from=date(2026, 10, 6), # base drops by the same 30/30 from then on ledger=".local/share/anki_guard/ledger.json", match=_anki_match, # which verified credit rows count today missing_ledger_is_no=True, diff --git a/earned_time/earned_time/__init__.py b/earned_time/earned_time/__init__.py index bfb5032..f58911e 100644 --- a/earned_time/earned_time/__init__.py +++ b/earned_time/earned_time/__init__.py @@ -30,6 +30,7 @@ from __future__ import annotations from earned_time._ledger import done_today, entry_signature, today_window, verified from earned_time._policy import ( + ANKI, EARNERS, GAMING_BASE_MINUTES, GAMING_CEILING_MINUTES, @@ -44,6 +45,7 @@ from earned_time._policy import ( from earned_time._resolve import Base, Resolution, Term, base_for, resolve __all__ = [ + "ANKI", "EARNERS", "GAMING_BASE_MINUTES", "GAMING_CEILING_MINUTES", diff --git a/earned_time/earned_time/_policy.py b/earned_time/earned_time/_policy.py index 6383b34..c9afa97 100644 --- a/earned_time/earned_time/_policy.py +++ b/earned_time/earned_time/_policy.py @@ -136,6 +136,17 @@ def _reading_match(row: dict[str, object], window: Window) -> bool: return landed +def _anki_match(row: dict[str, object], window: Window) -> bool: + """An Anki credit counts on its Anki day (``detail.anki_day``). + + anki-guard writes one row per Anki day, keyed on the collection's own day + boundary, so the day it names is the day it pays -- no stamp to parse. + """ + detail = row.get("detail") + local = datetime.fromtimestamp(window[0], tz=UTC).astimezone().date() + return isinstance(detail, dict) and detail.get("anki_day") == local.isoformat() + + WORKOUT: Final = Earner( name="workout", label="workout", @@ -162,9 +173,19 @@ READING: Final = Earner( match=_reading_match, missing_ledger_is_no=True, ) +ANKI: Final = Earner( + name="anki", + label="Anki", + gaming_minutes=30, + shutdown_minutes=30, + penalty_from=date(2026, 10, 6), + ledger=".local/share/anki_guard/ledger.json", + match=_anki_match, + missing_ledger_is_no=True, +) # Order is the order of reason strings and of screen-locker's live pass. -EARNERS: Final[tuple[Earner, ...]] = (WORKOUT, LEETCODE, READING) +EARNERS: Final[tuple[Earner, ...]] = (WORKOUT, LEETCODE, READING, ANKI) def earner(name: str, earners: tuple[Earner, ...] = EARNERS) -> Earner: diff --git a/earned_time/earned_time/tests/test_policy.py b/earned_time/earned_time/tests/test_policy.py index 15627d8..b7bf8c2 100644 --- a/earned_time/earned_time/tests/test_policy.py +++ b/earned_time/earned_time/tests/test_policy.py @@ -9,7 +9,7 @@ from typing import TYPE_CHECKING import pytest -from earned_time import EARNERS, LEETCODE, READING, WORKOUT, earner +from earned_time import ANKI, EARNERS, LEETCODE, READING, WORKOUT, earner from earned_time._ledger import today_window if TYPE_CHECKING: @@ -23,7 +23,7 @@ TODAY = datetime.fromtimestamp(WINDOW[0]).astimezone().date().isoformat() def test_registry_order_and_names() -> None: - assert [e.name for e in EARNERS] == ["workout", "leetcode", "reading"] + assert [e.name for e in EARNERS] == ["workout", "leetcode", "reading", "anki"] def test_lookup_by_name() -> None: @@ -97,3 +97,22 @@ def test_reading_unusable_end_is_logged(caplog: pytest.LogCaptureFixture) -> Non with caplog.at_level(logging.WARNING): assert not _match(READING, {"detail": {"bonus": "1"}, "entry_id": "r"}) assert "no usable ended_at" in caplog.text + + +@pytest.mark.parametrize( + ("detail", "expected"), + [ + ({"anki_day": TODAY}, True), + ({"anki_day": "2026-10-03"}, False), + ({}, False), + ("not a dict", False), + ], +) +def test_anki_match(detail: object, *, expected: bool) -> None: + assert _match(ANKI, {"detail": detail}) is expected + + +def test_anki_penalty_starts_the_day_after_it_shipped() -> None: + # Shipped 2026-10-05: that day is a pure bonus, never an unearnable cut. + assert not ANKI.penalised_on(date(2026, 10, 5)) + assert ANKI.penalised_on(date(2026, 10, 6)) diff --git a/earned_time/earned_time/tests/test_resolve.py b/earned_time/earned_time/tests/test_resolve.py index 103c40e..69acb76 100644 --- a/earned_time/earned_time/tests/test_resolve.py +++ b/earned_time/earned_time/tests/test_resolve.py @@ -73,24 +73,23 @@ def test_term_lookup_unknown_raises() -> None: def test_new_penalised_earner_keeps_the_best_case() -> None: - anki = Earner( - name="anki", - label="Anki", + # A hypothetical next earner on top of the real registry (reading and + # anki already penalised by 2026-10-11): 5h - 1h - 30m - 30m. + piano = Earner( + name="piano", + label="piano", gaming_minutes=30, shutdown_minutes=30, penalty_from=date(2026, 10, 10), ) from earned_time import EARNERS - earners = (*EARNERS, anki) + earners = (*EARNERS, piano) later = date(2026, 10, 11) - assert base_for(later, earners).gaming_minutes == 210 - assert base_for(later, earners).shutdown_minutes == 18 * 60 + 30 - skipped = resolve( - {"workout": 0, "leetcode": 0, "reading": 0, "anki": 0}, later, earners - ) - done = resolve( - {"workout": 0, "leetcode": 0, "reading": 0, "anki": 1}, later, earners - ) + assert base_for(later, earners).gaming_minutes == 180 + assert base_for(later, earners).shutdown_minutes == 18 * 60 + others = {"workout": 0, "leetcode": 0, "reading": 0, "anki": 0} + skipped = resolve({**others, "piano": 0}, later, earners) + done = resolve({**others, "piano": 1}, later, earners) assert done.gaming_minutes - skipped.gaming_minutes == 30 - assert done.shutdown_minutes == 19 * 60 + assert done.shutdown_minutes == 18 * 60 + 30 diff --git a/earned_time/pyproject.toml b/earned_time/pyproject.toml index 0263edd..aa7b12e 100644 --- a/earned_time/pyproject.toml +++ b/earned_time/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "earned-time" -version = "0.1.0" +version = "0.2.0" description = "Penalty-then-reward registry: what every gate app earns in gaming time and shutdown time" requires-python = ">=3.14" dependencies = []