utils/gatelock/gatelock/tests/test_evening_cli.py
Krzysztof kuhy Rudnicki b84a27eecf Evening chain: settled LeetCode day not pending
A day closed by a non-paying charge (escape hatch, outage) never gets a
source=leetcode row, so its LeetCode gate could never be satisfied. The
rung is now pending only while payable: done_today false AND no
verified charge row for today, any source.

Failure paths: charge check unreadable (ledger, key, verify error) ->
rung unchecked -> pending exit 2 (FAIL CLOSED). An unsigned or old
charge never settles a day, so the gate still arms (closed). earned_time
has no public "any charge" helper, so the rows are read from the ledger
and checked with earned_time.verified.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WzxdWYGbnVt12uFXKgNYm8
2026-11-17 21:45:17 +01:00

65 lines
2.2 KiB
Python

# Copyright (c) 2026 Krzysztof Rudnicki
"""The chain's process edges: gate subprocesses, production paths, the unit."""
from __future__ import annotations
from pathlib import Path
import runpy
import subprocess
import sys
from types import SimpleNamespace
from unittest import mock
import pytest
from gatelock import evening_chain as chain
def test_subprocess_runner_returns_the_exit_code_with_the_timeout() -> None:
done = SimpleNamespace(returncode=7)
with mock.patch("gatelock.evening_chain.subprocess.run", return_value=done) as run:
assert chain.subprocess_runner(("gate", "--x"), 12.5) == 7
run.assert_called_once_with(("gate", "--x"), check=False, timeout=12.5)
def test_subprocess_runner_deadline_is_none() -> None:
expired = subprocess.TimeoutExpired(("gate",), 1)
with mock.patch("gatelock.evening_chain.subprocess.run", side_effect=expired):
assert chain.subprocess_runner(("gate",), 1) is None
def test_subprocess_runner_unstartable_gate_is_a_failure() -> None:
with mock.patch("gatelock.evening_chain.subprocess.run", side_effect=OSError):
assert chain.subprocess_runner(("gate",), 1) == 127
def test_production_sources_names_the_real_key_and_home() -> None:
sources = chain.production_sources()
assert str(sources.key_file) == "/etc/workout-locker/hmac.key"
assert sources.home == Path.home()
assert sources.free_log is None
assert sources.sick_log is None
def test_module_entry_point_rejects_an_unknown_command() -> None:
with (
pytest.warns(RuntimeWarning),
pytest.raises(SystemExit) as raised,
mock.patch.object(sys, "argv", ["evening_chain", "bogus"]),
):
runpy.run_module("gatelock.evening_chain", run_name="__main__")
assert raised.value.code == 2
UNIT = Path(__file__).parents[2] / "systemd" / "evening-gates.service"
def test_unit_matches_the_contract() -> None:
lines = UNIT.read_text(encoding="utf-8").splitlines()
assert "Type=simple" in lines
assert "Restart=no" in lines
assert "Environment=DISPLAY=:0" in lines
assert "Environment=XAUTHORITY=%h/.Xauthority" in lines
assert "ExecStart=/usr/bin/python3 -m gatelock.evening_chain run" in lines
assert "[Install]" not in lines