mirror of
https://github.com/kuhyx/utils.git
synced 2026-11-23 21:46:29 +01:00
A finished coach review pays 20 shutdown minutes, once a day, as a top-up: never a rung, never in the floor, added last and capped at the ceiling (sick days too). A row pays only if signed, sourced, first for its slug ever, and backed by a signed non-manual accepted submission in the guard ledger from the 14 days before. From 2026-11-23. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WzxdWXLvi9aq4MxL4XHKJ4
150 lines
5.3 KiB
Python
150 lines
5.3 KiB
Python
# Copyright (c) 2026 Krzysztof Rudnicki
|
|
"""``python -m earned_time maturity``: table, JSON, and the paths it reads."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from dataclasses import replace
|
|
from datetime import date, datetime, time
|
|
import json
|
|
import runpy
|
|
import sys
|
|
from typing import TYPE_CHECKING
|
|
|
|
import pytest
|
|
|
|
import earned_time
|
|
from earned_time import LEETCODE, READING, Earner, entry_signature
|
|
from earned_time import _cli as cli
|
|
|
|
if TYPE_CHECKING:
|
|
from pathlib import Path
|
|
|
|
KEY = b"test-key"
|
|
DAY = date(2026, 10, 9)
|
|
|
|
|
|
@pytest.fixture
|
|
def home(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path:
|
|
"""A fake home with a key and one LeetCode solve on DAY."""
|
|
key = tmp_path / "hmac.key"
|
|
key.write_bytes(KEY)
|
|
monkeypatch.setattr(cli, "KEY_FILE", key)
|
|
monkeypatch.setattr(cli, "home", lambda: tmp_path)
|
|
stamp = datetime.combine(DAY, time(12)).astimezone().timestamp()
|
|
row: dict[str, object] = {
|
|
"kind": "credit",
|
|
"entry_id": "ac:1",
|
|
"day": DAY.isoformat(),
|
|
"amount": 1,
|
|
"detail": {"submitted_at": str(stamp)},
|
|
}
|
|
ledger = tmp_path / str(LEETCODE.ledger)
|
|
ledger.parent.mkdir(parents=True)
|
|
entries = [{**row, "hmac": entry_signature(row, KEY)}]
|
|
ledger.write_text(json.dumps({"entries": entries}), encoding="utf-8")
|
|
return tmp_path
|
|
|
|
|
|
def test_table(home: Path, capsys: pytest.CaptureFixture[str]) -> None:
|
|
assert cli.main(["maturity", "--day", DAY.isoformat()]) == 0
|
|
out = capsys.readouterr().out.splitlines()
|
|
assert out[0] == f"gate maturity on {DAY}"
|
|
assert out[1].split()[:3] == ["gate", "level", "days"]
|
|
rows = {line.split()[0]: line for line in out[2:-1]}
|
|
assert set(rows) == {
|
|
"workout",
|
|
"leetcode",
|
|
"reading",
|
|
"anki",
|
|
"automation",
|
|
"tutor",
|
|
"review",
|
|
}
|
|
assert rows["leetcode"].split()[1:5] == ["maturing", "1", str(DAY), str(DAY)]
|
|
assert "not in the registry on 2026-10-09" in rows["anki"]
|
|
assert "a top-up: nothing to cut, never penalised" in rows["review"]
|
|
assert "not in the registry on 2026-10-09" in rows["review"]
|
|
assert "could not check: ledger" not in rows["reading"]
|
|
assert rows["reading"].endswith(
|
|
"no verified credit but confirmed_on 2026-10-02: "
|
|
"fail closed, penalty from 2026-10-03"
|
|
)
|
|
assert out[-1].startswith("not classified (no earn-back): diet-guard")
|
|
|
|
|
|
def test_json(home: Path, capsys: pytest.CaptureFixture[str]) -> None:
|
|
assert cli.main(["maturity", "--json", "--day", DAY.isoformat()]) == 0
|
|
payload = json.loads(capsys.readouterr().out)
|
|
assert payload["day"] == DAY.isoformat()
|
|
assert payload["not_classified"] == list(cli.NOT_CLASSIFIED)
|
|
gates = {g["gate"]: g for g in payload["gates"]}
|
|
assert gates["leetcode"]["first_credit"] == DAY.isoformat()
|
|
assert gates["leetcode"]["checked"] is True
|
|
assert gates["reading"]["penalty_from"] == READING.penalty_from.isoformat()
|
|
# No reading ledger, but confirmed_on is set: fail closed, not lifted.
|
|
assert gates["reading"]["penalty_start"] == "2026-10-03"
|
|
assert gates["anki"]["penalty_start"] is None
|
|
assert gates["anki"]["last_credit"] is None
|
|
|
|
|
|
def test_default_day_and_unregistered_earners(
|
|
home: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
|
|
) -> None:
|
|
consumer = Earner(name="piano", label="piano", gaming_minutes=1, shutdown_minutes=1)
|
|
monkeypatch.setattr(earned_time, "EARNERS", (consumer, LEETCODE))
|
|
assert cli.main(["maturity"]) == 0
|
|
lines = capsys.readouterr().out.splitlines()
|
|
assert [line.split()[0] for line in lines[2:-1]] == [
|
|
"leetcode",
|
|
"workout",
|
|
"reading",
|
|
"tutor",
|
|
"review",
|
|
]
|
|
|
|
|
|
def test_render_table_pads_all_but_the_reasons() -> None:
|
|
verdict = earned_time.Maturity(
|
|
name="x",
|
|
level="new",
|
|
reasons=("a", "b"),
|
|
credit_days=0,
|
|
credit_rows=0,
|
|
first_credit=None,
|
|
last_credit=None,
|
|
confirmed_on=None,
|
|
penalty_from=None,
|
|
penalty_start=None,
|
|
checked=True,
|
|
)
|
|
table = cli.render_table([("x", verdict)]).splitlines()
|
|
assert table[1].split() == ["x", "new", "0", *["-"] * 5, "a;", "b"]
|
|
assert len(table) == 2
|
|
|
|
|
|
def test_home_is_the_users_home() -> None:
|
|
assert cli.home().is_absolute()
|
|
|
|
|
|
def test_python_dash_m(
|
|
home: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
|
|
) -> None:
|
|
monkeypatch.setattr(sys, "argv", ["earned_time", "maturity", "--json"])
|
|
with pytest.raises(SystemExit) as exit_info:
|
|
runpy.run_module("earned_time", run_name="__main__")
|
|
assert exit_info.value.code == 0
|
|
assert json.loads(capsys.readouterr().out)["gates"]
|
|
|
|
|
|
def test_a_name_shared_across_registries_gets_its_ledger_dir(
|
|
home: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
|
|
) -> None:
|
|
# ``automation`` was both the anki quota and the tutor until 0.9.0.
|
|
old = replace(LEETCODE, name="twin", ledger="a/l.json")
|
|
new = replace(LEETCODE, name="twin", ledger="b/l.json")
|
|
monkeypatch.setattr(earned_time, "EARNERS", (old,))
|
|
monkeypatch.setattr(earned_time, "TUTOR_EARNERS", (new,))
|
|
assert cli.main(["maturity", "--day", DAY.isoformat()]) == 0
|
|
rows = [line.split()[0] for line in capsys.readouterr().out.splitlines()[2:-1]]
|
|
assert rows == ["twin:a", "twin:b", "review"]
|