wake-alarm/wake_alarm/tests/test_alarm_payload.py
Krzysztof kuhy Rudnicki 6be5420fcf Stop trusting an alarm the phone published months ago
Belt-and-braces for the publishing race fixed in the previous commit. That bug
is gone, but the shape of the failure is worth closing on this side too: a
publish that never happens is indistinguishable from one that has not changed,
so the last successful publish stayed authoritative forever. One record stamped
2026-08-09 had the PC starting the morning routine at 07:00 for three weeks.

get_alarm now skips any record the phone published longer ago than
PUBLISHED_ALARM_MAX_AGE_SECONDS (14 days) and falls back. That is safe
precisely because FALLBACK_ALARM_HOUR is the phone's own baseline, so a lapsed
record degrades to the right answer rather than to a guess -- and an override
is one-shot anyway, so reverting to the baseline is what the phone would say.

The check is on when the PHONE stamped it, never on when we fetched, and that
distinction is the whole design. Checking the fetch would be trivially
defeated: the cache is rewritten from the same stale record every few minutes,
so `fetched_at` is always fresh no matter how old the record behind it is.
So the winning HLC's instant is now carried out of _alarm_payload, through
_sync_transport, and persisted in the cache as `published_at`. Nothing else
reads that cache any more -- the shutdown wrapper that used to went with the
PC's waking role -- so widening its shape is free.

An undated record is trusted rather than expired. The GitHub mirror reads a
single fixed path with no per-field HLC, so treating "cannot tell how old this
is" as "too old" would break that whole backend, and refusing an alarm we
merely failed to date is a worse failure than honouring it.

test_sync.py and test_sync_backends.py crossed the 250-line cap, so the new
lapse cases and the backend-ordering cases move to test_sync_lapsed.py and
test_sync_dispatch.py.

168 tests at 100% branch coverage. Verified against the real backend: the
cache now carries published_at, and `_sync --show` still resolves 08:00 from
the record the phone stamped minutes earlier.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PcdngP688mW6rJye3LRkSN
2026-08-30 16:37:13 +02:00

218 lines
7.4 KiB
Python

"""Tests for _alarm_payload.py — parsing and ranking what the phone pushed.
Split out of test_sync.py: these exercise the wire format, which is a
different module from the fetching/caching this file's siblings cover.
"""
from __future__ import annotations
from datetime import UTC, datetime
import json
from typing import TYPE_CHECKING, Any
from unittest.mock import patch
import pytest
from wake_alarm._alarm_payload import (
extract_field,
extract_hlc,
hlc_instant,
newest_alarm,
parse_log,
)
if TYPE_CHECKING:
from pathlib import Path
def _log(hour: object = 7, minute: object = 30) -> str:
"""Build a crdt_sync log payload shaped like the phone's."""
return json.dumps(
{
"alarm": {
"fields": {
"alarm_hour": [hour, "hlc-1"],
"alarm_minute": [minute, "hlc-1"],
}
}
}
)
@pytest.fixture
def config_file(tmp_path: Path) -> Path:
"""Provide a temporary sync config path."""
return tmp_path / "sync.json"
@pytest.fixture
def cache_file(tmp_path: Path) -> Path:
"""Provide a temporary alarm cache path."""
return tmp_path / "cache" / "alarm_cache.json"
@pytest.fixture
def fallback_files(tmp_path: Path) -> tuple[Path, Path]:
"""Provide two temporary sibling-app token paths."""
return tmp_path / "diet_guard_token", tmp_path / "screen_locker_token"
@pytest.fixture(autouse=True)
def _patch_paths(
config_file: Path, cache_file: Path, fallback_files: tuple[Path, Path]
) -> None:
"""Redirect every on-disk path into tmp_path for every test."""
with (
patch("wake_alarm._sync_transport.SYNC_CONFIG_FILE", config_file),
patch("wake_alarm._sync.ALARM_CACHE_FILE", cache_file),
patch("wake_alarm._sync_transport.SYNC_TOKEN_FALLBACK_FILES", fallback_files),
):
yield
class TestExtractField:
"""Tests for extract_field's tolerance of malformed logs."""
@pytest.mark.parametrize(
"log",
[
{},
{"alarm": "not a dict"},
{"alarm": {}},
{"alarm": {"fields": "not a dict"}},
{"alarm": {"fields": {}}},
{"alarm": {"fields": {"alarm_hour": "not a list"}}},
{"alarm": {"fields": {"alarm_hour": []}}},
{"alarm": {"fields": {"alarm_hour": ["7", "hlc"]}}},
{"alarm": {"fields": {"alarm_hour": [True, "hlc"]}}},
],
)
def test_malformed_shapes_yield_none(self, log: dict[str, Any]) -> None:
"""Every unexpected shape degrades to None rather than raising."""
assert extract_field(log, "alarm_hour") is None
def test_extracts_value(self) -> None:
"""The value is element 0 of the (value, HLC) pair."""
log = {"alarm": {"fields": {"alarm_hour": [6, "hlc"]}}}
assert extract_field(log, "alarm_hour") == 6
class TestParseLog:
"""Tests for parse_log."""
def test_valid(self) -> None:
"""A well-formed log parses to an (hour, minute) pair."""
assert parse_log(_log(7, 30)) == (7, 30)
def test_invalid_json(self) -> None:
"""Unparsable JSON yields None."""
assert parse_log("{nope") is None
def test_not_a_dict(self) -> None:
"""A JSON array is not a log."""
assert parse_log("[]") is None
def test_missing_fields(self) -> None:
"""A log without the alarm fields yields None."""
assert parse_log(json.dumps({"alarm": {"fields": {}}})) is None
@pytest.mark.parametrize(("hour", "minute"), [(24, 0), (-1, 0), (7, 60), (7, -1)])
def test_out_of_range(self, hour: int, minute: int) -> None:
"""Values outside 0-23 / 0-59 are rejected."""
assert parse_log(_log(hour, minute)) is None
class TestExtractHlc:
"""The HLC stamp is how the newest device's alarm wins the ranking."""
def test_returns_the_stamp_from_a_well_formed_pair(self) -> None:
"""A `[value, hlc]` pair yields its second element."""
log = {"alarm": {"fields": {"alarm_hour": [7, "2026-08-27T06:00:00Z"]}}}
assert extract_hlc(log, "alarm_hour") == "2026-08-27T06:00:00Z"
def test_a_missing_field_ranks_below_every_real_stamp(self) -> None:
"""Empty string sorts first, so a malformed device never outranks."""
assert extract_hlc({"alarm": {"fields": {}}}, "alarm_hour") == ""
def test_a_short_pair_ranks_below_every_real_stamp(self) -> None:
"""A value with no stamp is unusable for ranking."""
log = {"alarm": {"fields": {"alarm_hour": [7]}}}
assert extract_hlc(log, "alarm_hour") == ""
def test_a_non_string_stamp_is_rejected(self) -> None:
"""A numeric stamp would compare wrongly against real ISO strings."""
log = {"alarm": {"fields": {"alarm_hour": [7, 12345]}}}
assert extract_hlc(log, "alarm_hour") == ""
class TestNewestAlarm:
"""Ranking every device's published alarm, newest HLC wins."""
def test_no_devices_at_all_yields_none(self) -> None:
"""An empty listing must not be mistaken for a valid alarm."""
assert newest_alarm([]) is None
def test_every_device_unusable_yields_none(self) -> None:
"""A device whose payload will not parse is skipped, not trusted."""
assert newest_alarm([("phone", "not json"), ("tablet", "{}")]) is None
def test_the_newest_hlc_wins(self) -> None:
"""The point of the stamp: the most recently set alarm is used."""
older = json.dumps(
{
"alarm": {
"fields": {
"alarm_hour": [6, "2026-08-26T06:00:00Z"],
"alarm_minute": [0, "2026-08-26T06:00:00Z"],
}
}
},
)
newer = json.dumps(
{
"alarm": {
"fields": {
"alarm_hour": [7, "2026-08-27T06:00:00Z"],
"alarm_minute": [30, "2026-08-27T06:00:00Z"],
}
}
},
)
winner = newest_alarm([("old", older), ("new", newer)])
assert winner is not None
assert winner[:2] == (7, 30)
def test_an_unusable_device_does_not_block_a_good_one(self) -> None:
"""One broken device must not cost the user their alarm."""
good = json.dumps(
{
"alarm": {
"fields": {
"alarm_hour": [5, "2026-08-27T06:00:00Z"],
"alarm_minute": [15, "2026-08-27T06:00:00Z"],
}
}
},
)
winner = newest_alarm([("broken", "{}"), ("good", good)])
assert winner is not None
assert winner[:2] == (5, 15)
class TestHlcInstant:
"""Dating a record, so a lapsed one can be spotted."""
def test_reads_the_iso_prefix(self) -> None:
"""The ISO part ends at its Z, which is what makes it splittable."""
stamp = hlc_instant("2026-08-09T16:28:05.575Z-0000-device")
assert stamp is not None
assert datetime.fromtimestamp(stamp, UTC).day == 9
def test_an_absent_stamp_is_undated(self) -> None:
"""A field with no HLC cannot be dated, and says so."""
assert hlc_instant("") is None
def test_an_unparsable_prefix_is_undated(self) -> None:
"""Garbage before the Z must not raise out of a per-minute tick."""
assert hlc_instant("not-a-timestampZ-0000-device") is None