diff --git a/pyproject.toml b/pyproject.toml index 88d9279..7ee3de6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -10,7 +10,7 @@ dependencies = [ "gatelock @ git+https://github.com/kuhyx/utils@gatelock-v0.11.0#subdirectory=gatelock", "crdt-sync @ git+https://github.com/kuhyx/utils@crdt-sync-v0.9.2#subdirectory=crdt-sync", "freedays @ git+https://github.com/kuhyx/utils@freedays-v0.4.0#subdirectory=freedays", - "earned-time @ git+https://github.com/kuhyx/utils@earned-time-v0.17.0#subdirectory=earned_time", + "earned-time @ git+https://github.com/kuhyx/utils@earned-time-v0.18.0#subdirectory=earned_time", "sickdays @ git+https://github.com/kuhyx/utils@sickdays-v0.3.0#subdirectory=sickdays", ] diff --git a/requirements.txt b/requirements.txt index 8f368e6..e2bb488 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,7 +4,7 @@ bandit==1.9.4 codespell==2.4.3 coverage==7.17.0 crdt-sync @ git+https://github.com/kuhyx/utils@crdt-sync-v0.9.2#subdirectory=crdt-sync -earned-time @ git+https://github.com/kuhyx/utils@earned-time-v0.17.0#subdirectory=earned_time +earned-time @ git+https://github.com/kuhyx/utils@earned-time-v0.18.0#subdirectory=earned_time freedays @ git+https://github.com/kuhyx/utils@freedays-v0.4.0#subdirectory=freedays gatelock @ git+https://github.com/kuhyx/utils@gatelock-v0.11.0#subdirectory=gatelock mcp==2.3.0 diff --git a/screen_locker/_earned.py b/screen_locker/_earned.py index 5b5923d..546b5c4 100644 --- a/screen_locker/_earned.py +++ b/screen_locker/_earned.py @@ -16,7 +16,7 @@ midnight, so a 30-minute earner is applied exactly. from __future__ import annotations -from datetime import date, datetime +from datetime import UTC, date, datetime import functools import logging from pathlib import Path @@ -26,6 +26,7 @@ import earned_time from screen_locker._constants import HMAC_KEY_FILE from screen_locker._day import today_str +from screen_locker._gate_off import gate_off _logger: Final = logging.getLogger(__name__) @@ -105,12 +106,25 @@ def earned_today( earner: earned_time.Earner, now: datetime | None = None ) -> bool | None: """Whether ``earner``'s gate recorded today's credit; ``None`` if unknown.""" - return earned_time.done_today(earner, ledger_file(earner), HMAC_KEY_FILE, now=now) + day = (now or datetime.now(tz=UTC)).astimezone().date() + return earned_time.done_today( + earner, + ledger_file(earner), + HMAC_KEY_FILE, + now=now, + gate_off=gate_off(earner, day), + ) def earned_units(earner: earned_time.Earner, day: date) -> int | None: """How many of a counted earner's units count for ``day``; ``None`` if unknown.""" - return earned_time.credit_units(earner, ledger_file(earner), HMAC_KEY_FILE, day) + return earned_time.credit_units( + earner, + ledger_file(earner), + HMAC_KEY_FILE, + day, + gate_off=gate_off(earner, day), + ) def gate_answers(day: date | None = None) -> dict[str, int | bool | None]: @@ -179,7 +193,9 @@ def first_credit_time(earner: earned_time.Earner, day: date) -> float | None: if fn is None: _warn_no_first_credit() return None - stamp: float | None = fn(earner, ledger_file(earner), HMAC_KEY_FILE, day) + stamp: float | None = fn( + earner, ledger_file(earner), HMAC_KEY_FILE, day, gate_off=gate_off(earner, day) + ) return stamp diff --git a/screen_locker/_gate_off.py b/screen_locker/_gate_off.py new file mode 100644 index 0000000..4b86582 --- /dev/null +++ b/screen_locker/_gate_off.py @@ -0,0 +1,28 @@ +"""Whether an earner's gate stands down today, asked live (earned_time 0.18.0). + +Only LeetCode has a price: while its gate is on, earned_time pays only the +signed charge that covers the whole day, never one solve of five (kuhy, +2026-10-24). A free day or a sick day not keeping ``leetcode`` has no price, +so one verified solve earns. Never stored: clearing the sick day takes the +exception back on the next read. +""" + +from __future__ import annotations + +from typing import TYPE_CHECKING + +import earned_time +import freedays +import sickdays + +from screen_locker._sick import stands_down + +if TYPE_CHECKING: + from datetime import date + + +def gate_off(earner: earned_time.Earner, day: date) -> bool: + """Whether ``earner``'s gate is off on ``day`` (always False but LeetCode).""" + if earner.name != earned_time.LEETCODE.name: + return False + return freedays.is_free_day(day) or stands_down(sickdays.LOCKER_LEETCODE, day) diff --git a/screen_locker/tests/_real_state_fixtures.py b/screen_locker/tests/_real_state_fixtures.py index e7d7546..f1eb7fc 100644 --- a/screen_locker/tests/_real_state_fixtures.py +++ b/screen_locker/tests/_real_state_fixtures.py @@ -10,8 +10,10 @@ other. ``_isolate_home`` covers paths resolved at *call* time from ``Path.home()``; ``_no_free_days_by_default`` covers paths bound into module constants at *import* time, which redirecting home afterwards cannot reach. -``pre_ladder`` is the one opt-in fixture here: it keeps a test off the real -calendar's ladder cutover, the date-dependent half of the same question. +``pre_ladder`` and ``leetcode_gate_off`` are the opt-in fixtures here: +``pre_ladder`` keeps a test off the real calendar's ladder cutover, the +date-dependent half of the same question; ``leetcode_gate_off`` keeps +ledger-plumbing tests off LeetCode's paid-in-full rule. """ from __future__ import annotations @@ -172,6 +174,17 @@ def pre_ladder(monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setattr("earned_time._ladder.LADDER_FROM", date.max) +@pytest.fixture +def leetcode_gate_off(monkeypatch: pytest.MonkeyPatch) -> None: + """Let a bare LeetCode credit earn, as on a free or stood-down sick day. + + These tests pin the shared ledger plumbing with one credit row per earner. + Since earned_time 0.18.0 a LeetCode credit alone earns only while the gate + is off; the paid-in-full rule itself is ``test_leetcode_gate_off.py``. + """ + monkeypatch.setattr("screen_locker._earned.gate_off", lambda _earner, _day: True) + + @pytest.fixture(autouse=True) def _no_real_night_lockdown(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: """Never let the lock chain read the machine's real night-lockdown stamp. diff --git a/screen_locker/tests/test_earned.py b/screen_locker/tests/test_earned.py index 351c301..728c18f 100644 --- a/screen_locker/tests/test_earned.py +++ b/screen_locker/tests/test_earned.py @@ -133,6 +133,7 @@ class TestFlatEarners: assert f"{earner.name}_bonus_date" in seeded +@pytest.mark.usefixtures("leetcode_gate_off") class TestEarnedToday: @_LEDGERED def test_signed_credit_today_earns( @@ -215,6 +216,7 @@ class TestCannotCheck: assert "not valid JSON" in caplog.text +@pytest.mark.usefixtures("leetcode_gate_off") class TestFlatAnswers: def test_every_flat_earner_answers_by_name(self, key: Path) -> None: write_ledger(earned_time.LEETCODE, [credit(earned_time.LEETCODE)]) diff --git a/screen_locker/tests/test_leetcode_gate_off.py b/screen_locker/tests/test_leetcode_gate_off.py new file mode 100644 index 0000000..9fe2369 --- /dev/null +++ b/screen_locker/tests/test_leetcode_gate_off.py @@ -0,0 +1,66 @@ +"""The LeetCode shutdown hour needs the day paid in full (earned_time 0.18.0). + +kuhy, 2026-10-24: Saturday cost 5 credits, one problem was solved, and the ++1 h shutdown paid anyway. earned_time now counts only the signed ``leetcode`` +charge while the gate is on; screen-locker has to say, live, when the gate is +off (a free day, or a sick day not keeping ``leetcode``), because only then +does one verified solve earn on its own. +""" + +from __future__ import annotations + +from datetime import date +from unittest.mock import patch + +import earned_time +import freedays +import pytest +import sickdays + +from screen_locker import _earned, _gate_off + +SAT = date(2026, 10, 24) + + +@pytest.fixture +def pools(monkeypatch: pytest.MonkeyPatch) -> dict[str, bool]: + """Fake the free-day and sick-day pools; flip the returned flags.""" + flags = {"free": False, "sick_off": False} + monkeypatch.setattr(freedays, "is_free_day", lambda _day=None: flags["free"]) + + def stands_down(locker: str, _day: date | None = None) -> bool: + return locker == sickdays.LOCKER_LEETCODE and flags["sick_off"] + + monkeypatch.setattr(sickdays, "stands_down", stands_down) + return flags + + +@pytest.mark.parametrize( + ("free", "sick_off", "expected"), + [(False, False, False), (True, False, True), (False, True, True)], +) +def test_gate_off_follows_the_pools( + pools: dict[str, bool], *, free: bool, sick_off: bool, expected: bool +) -> None: + pools.update(free=free, sick_off=sick_off) + assert _gate_off.gate_off(earned_time.LEETCODE, SAT) is expected + + +def test_only_leetcode_has_a_gate_to_switch_off(pools: dict[str, bool]) -> None: + pools.update(free=True, sick_off=True) + assert _gate_off.gate_off(earned_time.READING, SAT) is False + + +def test_every_ledger_read_passes_it(pools: dict[str, bool]) -> None: + pools.update(free=True) + leet = earned_time.LEETCODE + with ( + patch.object(earned_time, "done_today", return_value=True) as done, + patch.object(earned_time, "first_credit_at", return_value=1.0) as first, + patch.object(earned_time, "credit_units", return_value=1) as units, + ): + _earned.earned_today(leet) + _earned.first_credit_time(leet, SAT) + _earned.earned_units(leet, SAT) + for mock in (done, first, units): + assert mock.call_args.kwargs["gate_off"] is True diff --git a/screen_locker/tests/test_shutdown_flat_earners.py b/screen_locker/tests/test_shutdown_flat_earners.py index 6426a94..1686a0f 100644 --- a/screen_locker/tests/test_shutdown_flat_earners.py +++ b/screen_locker/tests/test_shutdown_flat_earners.py @@ -145,6 +145,7 @@ class TestANewlyRegisteredEarner: assert (base_minutes(eve), base_minutes(cut)) == (before[0], before[1] - 60) +@pytest.mark.usefixtures("leetcode_gate_off") class TestRealLedgers: @pytest.mark.usefixtures("pre_ladder") def test_signed_credits_on_disk_reach_the_reset(self, tmp_path: Path) -> None: