screen-locker/screen_locker/tests/test_source_health.py
Krzysztof kuhy Rudnicki 16ada6e5a2 Tell "could not check" apart from "no workout", and stop double-granting bonuses
A real 1h57m workout on 2026-08-24 synced to Firebase and the locker still
locked the screen. The trigger was auth: screen_locker was the only app with no
cached firebase_auth.json, so it fell back to a stale shared password and got
HTTP 400 every run. The defect was what it did next — an unreadable primary
source was reported as "no workout" rather than "could not check", which is the
same conflation that took 2026-06-12.

Recover the session by borrowing a live refresh token from a sibling app, track
degraded sources as state instead of a log line, and say so on the lock screen:
name the backend, the error, and how stale the data is, so a sync fault reads
as a sync fault.

Also fix two bugs found while finishing the recovery:

restore_and_bonus.py banked `existing + bonus` with no idempotence guard while
finish_2026_08_24.sh advertised "safe to re-run" — a second run silently
granted the compensation hours again. Guard on a durable
`shutdown_bonus_granted_for` list; the bonus map alone cannot answer the
question, since a week's total mixes earned streak hours with corrections.

_extra_benefits._save_state rebuilt state from four hardcoded keys, so the next
weekly rollover would have dropped that guard and re-armed the double-grant.
Merge onto loaded state instead.

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

69 lines
2.4 KiB
Python

"""A backend that answers with stale data must still be called broken.
The 2026-08-24 lockout had two causes and only one of them raised. These
tests cover the silent one: the GitHub mirror answered every request
correctly while the phone had stopped writing to it nine days earlier.
"""
from __future__ import annotations
from datetime import datetime, timedelta, timezone
from screen_locker._source_health import (
SourceFinding,
describe_staleness,
explain_findings,
)
_NOW = datetime(2026, 8, 24, 12, 0, tzinfo=timezone.utc)
class TestDescribeStaleness:
"""Turning "answered fine, but the data is old" into a stated fault."""
def test_fresh_source_is_not_a_finding(self) -> None:
"""Today's data must not raise an alarm."""
newest = _NOW - timedelta(hours=3)
assert describe_staleness("GitHub mirror", newest, now=_NOW) is None
def test_nine_day_silence_is_reported_with_its_age(self) -> None:
"""The real 2026-08-15 mirror silence, named in days."""
newest = datetime(2026, 8, 15, 18, 0, tzinfo=timezone.utc)
finding = describe_staleness("GitHub mirror", newest, now=_NOW)
assert finding is not None
assert "8 days" in finding.headline
assert "2026-08-15" in finding.detail
def test_boundary_is_not_flagged(self) -> None:
"""Just inside the window stays quiet, so rest days do not cry wolf."""
newest = _NOW - timedelta(days=1, hours=23)
assert describe_staleness("GitHub mirror", newest, now=_NOW) is None
def test_empty_backend_is_its_own_finding(self) -> None:
""" "Answered with nothing" is different from "answered with old"."""
finding = describe_staleness("Firebase", None, now=_NOW)
assert finding is not None
assert "no workouts at all" in finding.headline
class TestExplainFindings:
"""The text a locked-out user actually reads."""
def test_no_findings_render_nothing(self) -> None:
"""A healthy machine must not show a diagnostics block."""
assert explain_findings([]) == ""
def test_findings_name_the_backend_and_the_consequence(self) -> None:
"""The panel must say it is a sync fault, not a missed workout."""
text = explain_findings(
[SourceFinding("Firebase", "Firebase is unreadable", "HTTP 400.")]
)
assert "Firebase is unreadable" in text
assert "sync fault, not a missed workout" in text