wake-alarm/wake_alarm/tests/test_energy.py
Krzysztof kuhy Rudnicki c3c129b068 Make the morning session the carrot: earn 11:00 by finishing it in time
On 2026-09-19 the phone was powered off after the bathroom tag and nothing
on the PC noticed: every finished session was published and nothing read
it. Now wake-alarm distils the phone's morning into an HMAC-signed
~/.local/state/wake_alarm/morning_session.json (wake-alarm-session.timer,
07:00-11:00 every 2 min) that screen-locker and leetcode-guard read to
defer their locks -- provisionally until min(start + 90 min, 09:30) while
the session is live, until 11:00 once it completed inside that cutoff,
and not at all for a session finished later (the phone switched on at
noon completes the legs then; that must un-lock nothing).

The phone publishes the session at start as well as at finish
(current_session, cleared with the finish stamp), so the PC can tell
"started, not finished" from "never started". A required 1-5 energy tap
joins the weigh-in and ships as latest_energy with a PC reader mirroring
_weight (the shared half is _field_reader).

Also fixes the vmbox harness, red since the 2026-09-11 home-tidy: the
guest tree lives at ~/wake-alarm, and the INSTALL-OK grep matched vm's
echo of the command line. Takes Flutter 3.47.5.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Tp54ewdXRLaimUuT8LgYyn
2026-09-19 16:40:51 +02:00

84 lines
3.0 KiB
Python

"""The PC-side reader for the phone's morning energy tap."""
from __future__ import annotations
import json
from typing import TYPE_CHECKING
from unittest.mock import patch
from wake_alarm._energy import Energy, latest_energy, main, newest_energy
if TYPE_CHECKING:
import pytest
def _log(
level: object, hlc: str = "2026-09-19T06:20:00.000Z-0000-phone", **extra: object
) -> str:
value = {"date": "2026-09-19", "level": level, **extra}
return json.dumps({"alarm": {"fields": {"latest_energy": [value, hlc]}}})
class TestNewestEnergy:
"""Ranking and parsing across devices."""
def test_picks_the_newest_stamp(self) -> None:
published = [
("old", _log(2, "2026-09-10T06:20:00.000Z-0000-old")),
("new", _log(4, "2026-09-19T06:20:00.000Z-0000-new")),
]
assert newest_energy(published) == Energy("2026-09-19", 4, "new")
assert newest_energy(list(reversed(published))) == Energy(
"2026-09-19", 4, "new"
)
def test_device_id_breaks_an_exact_tie(self) -> None:
published = [("b", _log(3)), ("a", _log(1))]
assert newest_energy(published) == Energy("2026-09-19", 3, "b")
def test_skips_every_malformed_shape(self) -> None:
"""Only a 1-5 int with a date string counts; never a bool or 0/6."""
bad = [
("json", "not json"),
("list", "[1]"),
("no-alarm", "{}"),
("no-fields", json.dumps({"alarm": {}})),
("no-pair", json.dumps({"alarm": {"fields": {}}})),
("empty-pair", json.dumps({"alarm": {"fields": {"latest_energy": []}}})),
("not-dict", json.dumps({"alarm": {"fields": {"latest_energy": [3]}}})),
(
"no-date",
json.dumps({"alarm": {"fields": {"latest_energy": [{"level": 3}]}}}),
),
("bool", _log(level=True)),
("float", _log(3.5)),
("low", _log(0)),
("high", _log(6)),
]
assert newest_energy(bad) is None
assert newest_energy([*bad, ("ok", _log(5))]) == Energy("2026-09-19", 5, "ok")
class TestLatestEnergy:
"""The fetch wrapper and the CLI."""
def test_unreadable_backend_is_none(self) -> None:
with patch("wake_alarm._field_reader.fetch_firebase_logs", return_value=None):
assert latest_energy() is None
def test_no_rating_yet_is_none(self) -> None:
with patch("wake_alarm._field_reader.fetch_firebase_logs", return_value=[]):
assert latest_energy() is None
def test_main_prints_json(self, capsys: pytest.CaptureFixture[str]) -> None:
with patch(
"wake_alarm._field_reader.fetch_firebase_logs",
return_value=[("p", _log(3))],
):
assert main() == 0
assert json.loads(capsys.readouterr().out) == {"date": "2026-09-19", "level": 3}
def test_main_exits_1_with_nothing(self) -> None:
with patch("wake_alarm._field_reader.fetch_firebase_logs", return_value=None):
assert main() == 1