wake-alarm/wake_alarm/tests/test_correlation.py
Krzysztof kuhy Rudnicki f5cfc3e452 Phase 1: structured JSONL logging with cross-process dedupe
Adds the diagnostic substrate the rest of the hardening work builds on. A
6am failure should be reconstructable afterwards instead of re-debugged.

Two sinks, deliberately. stderr -> journal is unchanged. Alongside it, one
JSON object per line in ~/.local/state/wake_alarm/events.jsonl, so a morning
can be replayed with jq.

The hard constraint is that _trigger is a fresh process every 60 seconds, so
an in-memory rate limiter would reset on every tick and suppress nothing.
_log_dedupe therefore persists the last emitted fingerprint to disk and
flushes a suppressed_repeats count onto the next distinct record. Verified
across separate processes: 4 invocations logging the same routine event
produce 1 record plus an accurate count of 3.

That is what satisfies the standing "do not raise the level of _sync's
degraded-read paths" rule by construction, rather than by keeping levels
artificially low and losing the signal.

The correlation id is derived, never generated: both halves compute
wake-YYYYMMDD-HHMM from the resolved alarm instant, so the PC and the phone
agree with no handshake and no uuid exchange -- which matters precisely when
sync is down.

Logging never raises. An unwritable sink degrades to journal-only.

_alarm.py and _sync.py keep the default component= for now; tagging them
means splitting them for the 250-line gate, which belongs in the phases that
actually restructure those files.

389 tests pass, 100% branch coverage held.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EckWmyfetYq3Mi8MAnUudh
2026-08-26 23:00:08 +02:00

44 lines
1.6 KiB
Python

"""Tests for the derived correlation id."""
from __future__ import annotations
from datetime import datetime
from wake_alarm._correlation import correlation_id, correlation_id_for
def _local(year: int, month: int, day: int, hour: int, minute: int) -> datetime:
"""Build an aware local datetime, matching the idiom in test_trigger.py."""
return datetime(year, month, day, hour, minute, tzinfo=None).astimezone()
def test_correlation_id_formats_local_instant() -> None:
"""The id encodes the local date and the alarm's wall-clock time."""
assert correlation_id(_local(2026, 8, 26, 7, 0)) == "wake-20260826-0700"
def test_correlation_id_pads_single_digits() -> None:
"""Single-digit months, days, hours and minutes are zero padded."""
assert correlation_id(_local(2026, 1, 2, 3, 4)) == "wake-20260102-0304"
def test_correlation_id_is_stable_for_the_same_instant() -> None:
"""Two independent computations agree, which is the whole point."""
moment = _local(2026, 12, 31, 23, 59)
assert correlation_id(moment) == correlation_id(moment)
def test_correlation_id_for_uses_the_date_of_now() -> None:
"""The (hour, minute) form takes its date from the supplied instant."""
assert (
correlation_id_for(7, 30, now=_local(2026, 8, 26, 22, 15))
== "wake-20260826-0730"
)
def test_correlation_id_for_ignores_the_time_of_now() -> None:
"""Only *now*'s date matters; its own time is replaced."""
early = correlation_id_for(6, 0, now=_local(2026, 8, 26, 1, 2))
late = correlation_id_for(6, 0, now=_local(2026, 8, 26, 23, 58))
assert early == late == "wake-20260826-0600"