mirror of
https://github.com/kuhyx/screen-locker.git
synced 2026-11-10 19:07:16 +01:00
The workout log stored ONE workout per calendar day, so a second same-day
workout was silently dropped and could never earn credit.
Multiple workouts per day:
- workout_log.json: {date: entry} -> {date: [entry, ...]}. Legacy entries are
wrapped on read (_log_io.load_workout_log) so their HMACs stay valid — no
re-signing; every write normalizes the file, migrating it progressively.
- New write chokepoint (_log_mixin.write_signed_entry) APPENDS and dedups by a
stable workout_id, returning RecordResult so callers can tell a new workout
from a duplicate. Re-verifying the same workout is a no-op.
- count_weekly_workouts is anti-gaming: each VERIFIED workout counts
individually; self-reported manual entries count at most once per day. Four
real workouts in one day satisfy the week; four manual entries count as one.
- Reward: the day's first counted workout gets the base +2h; an additional
same-day VERIFIED workout gets +1h; an additional manual gets nothing. The
old "+1h when weekly_count > 4" gate is replaced — weekly volume is the
banked bonus's job and shouldn't be paid twice.
- Fixed: the unlock screen labelled the +1h "#{weekly_count - 4}", which can
render negative under the new rule. Removed the dead runnerup_manual type.
- Flutter: the manual budget counts distinct DAYS, matching the PC.
PC->phone sync (it had never worked once):
- push_pc_manuals pushed from a side-store written at log-time, so anything
logged before that feature existed could never publish. _manual_push now
derives the crdt log directly from workout_log.json — the single source of
truth — covering manual AND verified runs. The record id is the workout's own
workout_id (derived for legacy entries to match the phone's format) and the
HLC comes from the entry's own timestamp, so a re-push is byte-identical and
the repo never churns.
- The phone reads every kind (readMergedWorkoutPayloads) and lists the PC's
workouts under "SYNCED FROM PC". The manual budget still sees manual-only, so
a verified run can't consume a self-report slot.
- Settings claimed "Connected." whenever a token STRING existed. It now
verifies against the API on open, so a revoked token reports 401 instead of
showing a green lie.
Never fail silently:
- 30 swallowed exceptions across 19 files now log WARNING with the concrete
cause AND consequence — plus the token guards in _workout_sync that returned
[] in total silence, the exact bug that hid the dead sync for weeks.
- New pre-commit hook (scripts/check_silent_failures.py) fails any except that
neither re-raises nor logs loudly. ruff's S110 only catches `except: pass`;
the real pattern here was `except: log.info(); return []`. Documented in
CLAUDE.md.
Verified on real data: the Jul 13 run (pulled from its actual TCX over ADB) now
counts alongside that day's manual squash, and all three workouts are published
to kuhyx/syncs.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Fv8ULVt3Q67GwCFbM7doHB
246 lines
7.6 KiB
Python
246 lines
7.6 KiB
Python
"""Tests for screen_locker initialization, logging, and basic operations."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import datetime, timezone
|
|
import json
|
|
import tkinter as tk
|
|
from typing import TYPE_CHECKING, Any
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
import pytest
|
|
|
|
from screen_locker.screen_lock import ScreenLocker
|
|
from screen_locker.tests.conftest import create_locker
|
|
|
|
if TYPE_CHECKING:
|
|
from pathlib import Path
|
|
|
|
|
|
class TestRun:
|
|
"""Tests for run method."""
|
|
|
|
def test_run_starts_mainloop(
|
|
self,
|
|
mock_tk: MagicMock,
|
|
mock_sys_exit: MagicMock,
|
|
tmp_path: Path,
|
|
) -> None:
|
|
"""Test run starts the tkinter mainloop."""
|
|
locker = create_locker(mock_tk, tmp_path)
|
|
|
|
locker.run()
|
|
|
|
locker.root.mainloop.assert_called_once()
|
|
|
|
|
|
class TestAutoUpgradeSickDay:
|
|
"""Tests for sick_day → phone_verified silent upgrade helpers."""
|
|
|
|
def test_upgrade_succeeds_when_phone_verified(
|
|
self,
|
|
mock_tk: MagicMock,
|
|
mock_sys_exit: MagicMock,
|
|
tmp_path: Path,
|
|
) -> None:
|
|
"""Verified phone workout overwrites today's sick_day entry."""
|
|
log_file = tmp_path / "workout_log.json"
|
|
locker = create_locker(mock_tk, tmp_path)
|
|
locker.log_file = log_file
|
|
with (
|
|
patch.object(
|
|
locker,
|
|
"_verify_phone_workout",
|
|
return_value=("verified", "Workout verified! (1 session)"),
|
|
),
|
|
patch.object(
|
|
locker,
|
|
"_adjust_shutdown_time_later",
|
|
return_value=True,
|
|
) as mock_adjust,
|
|
patch(
|
|
"screen_locker._log_mixin.compute_entry_hmac",
|
|
return_value="sig",
|
|
),
|
|
):
|
|
assert locker._try_auto_upgrade_sick_day() is True
|
|
mock_adjust.assert_called_once()
|
|
|
|
today = datetime.now(tz=timezone.utc).strftime("%Y-%m-%d")
|
|
with log_file.open() as f:
|
|
data: dict[str, Any] = json.load(f)
|
|
# The log is day-keyed with a LIST of workouts; the upgrade appends one.
|
|
assert len(data[today]) == 1
|
|
entry = data[today][0]
|
|
assert entry["workout_data"]["type"] == "phone_verified"
|
|
assert entry["workout_data"]["after_sick_day"] == "true"
|
|
assert entry["workout_id"] == f"phone_verified:{today}"
|
|
|
|
def test_upgrade_skipped_when_not_verified(
|
|
self,
|
|
mock_tk: MagicMock,
|
|
mock_sys_exit: MagicMock,
|
|
tmp_path: Path,
|
|
) -> None:
|
|
"""Non-verified statuses leave the sick_day entry untouched."""
|
|
locker = create_locker(mock_tk, tmp_path)
|
|
with patch.object(
|
|
locker,
|
|
"_verify_phone_workout",
|
|
return_value=("no_phone", "No phone connected"),
|
|
):
|
|
assert locker._try_auto_upgrade_sick_day() is False
|
|
assert locker.workout_data == {}
|
|
|
|
def test_upgrade_skipped_on_exception(
|
|
self,
|
|
mock_tk: MagicMock,
|
|
mock_sys_exit: MagicMock,
|
|
tmp_path: Path,
|
|
) -> None:
|
|
"""Transient OSError/RuntimeError during check is non-fatal."""
|
|
locker = create_locker(mock_tk, tmp_path)
|
|
with patch.object(
|
|
locker,
|
|
"_verify_phone_workout",
|
|
side_effect=OSError("transient"),
|
|
):
|
|
assert locker._try_auto_upgrade_sick_day() is False
|
|
|
|
def test_init_exits_when_sick_day_upgrade_succeeds(
|
|
self,
|
|
mock_tk: MagicMock,
|
|
mock_sys_exit: MagicMock,
|
|
tmp_path: Path,
|
|
) -> None:
|
|
"""Startup exits 0 after a successful silent sick_day upgrade."""
|
|
mock_sys_exit.side_effect = SystemExit(0)
|
|
with (
|
|
patch.object(
|
|
ScreenLocker,
|
|
"_try_auto_upgrade_sick_day",
|
|
return_value=True,
|
|
) as mock_upgrade,
|
|
pytest.raises(SystemExit),
|
|
):
|
|
create_locker(mock_tk, tmp_path, is_sick_day_log=True)
|
|
mock_upgrade.assert_called_once()
|
|
mock_sys_exit.assert_called_once_with(0)
|
|
|
|
|
|
class TestMainEntry:
|
|
"""Tests for main entry point."""
|
|
|
|
def test_main_demo_mode_default(
|
|
self,
|
|
mock_tk: MagicMock,
|
|
mock_sys_exit: MagicMock,
|
|
tmp_path: Path,
|
|
) -> None:
|
|
"""Test main defaults to demo mode."""
|
|
locker = create_locker(mock_tk, tmp_path, demo_mode=True)
|
|
|
|
assert locker.demo_mode is True
|
|
|
|
def test_main_production_mode_flag(
|
|
self,
|
|
mock_tk: MagicMock,
|
|
mock_sys_exit: MagicMock,
|
|
tmp_path: Path,
|
|
) -> None:
|
|
"""Test main with --production flag."""
|
|
locker = create_locker(mock_tk, tmp_path, demo_mode=False)
|
|
|
|
assert locker.demo_mode is False
|
|
|
|
|
|
class TestAdjustShutdownTimeLater:
|
|
"""Tests for _adjust_shutdown_time_later method."""
|
|
|
|
def test_adjust_shutdown_time_later_success(
|
|
self,
|
|
mock_tk: MagicMock,
|
|
mock_sys_exit: MagicMock,
|
|
tmp_path: Path,
|
|
) -> None:
|
|
"""Test _adjust_shutdown_time_later adds hours successfully."""
|
|
locker = create_locker(mock_tk, tmp_path)
|
|
object.__setattr__(
|
|
locker, "_read_shutdown_config", MagicMock(return_value=(21, 22, 8))
|
|
)
|
|
object.__setattr__(
|
|
locker, "_write_shutdown_config", MagicMock(return_value=True)
|
|
)
|
|
|
|
result = locker._adjust_shutdown_time_later()
|
|
|
|
assert result is True
|
|
locker._write_shutdown_config.assert_called_once_with(23, 23, 8, restore=True)
|
|
|
|
def test_adjust_shutdown_time_later_caps_at_23(
|
|
self,
|
|
mock_tk: MagicMock,
|
|
mock_sys_exit: MagicMock,
|
|
tmp_path: Path,
|
|
) -> None:
|
|
"""Test _adjust_shutdown_time_later caps hours at 23."""
|
|
locker = create_locker(mock_tk, tmp_path)
|
|
object.__setattr__(
|
|
locker, "_read_shutdown_config", MagicMock(return_value=(22, 23, 8))
|
|
)
|
|
object.__setattr__(
|
|
locker, "_write_shutdown_config", MagicMock(return_value=True)
|
|
)
|
|
|
|
result = locker._adjust_shutdown_time_later()
|
|
|
|
assert result is True
|
|
# 22+2=24 capped to 23, 23+2=25 capped to 23
|
|
locker._write_shutdown_config.assert_called_once_with(23, 23, 8, restore=True)
|
|
|
|
def test_adjust_shutdown_time_later_no_config(
|
|
self,
|
|
mock_tk: MagicMock,
|
|
mock_sys_exit: MagicMock,
|
|
tmp_path: Path,
|
|
) -> None:
|
|
"""Test _adjust_shutdown_time_later returns False if config missing."""
|
|
locker = create_locker(mock_tk, tmp_path)
|
|
object.__setattr__(
|
|
locker, "_read_shutdown_config", MagicMock(return_value=None)
|
|
)
|
|
|
|
result = locker._adjust_shutdown_time_later()
|
|
|
|
assert result is False
|
|
|
|
def test_adjust_shutdown_time_later_oserror(
|
|
self,
|
|
mock_tk: MagicMock,
|
|
mock_sys_exit: MagicMock,
|
|
tmp_path: Path,
|
|
) -> None:
|
|
"""Test _adjust_shutdown_time_later handles OSError."""
|
|
locker = create_locker(mock_tk, tmp_path)
|
|
object.__setattr__(
|
|
locker,
|
|
"_read_shutdown_config",
|
|
MagicMock(side_effect=OSError("permission denied")),
|
|
)
|
|
|
|
result = locker._adjust_shutdown_time_later()
|
|
|
|
assert result is False
|
|
|
|
|
|
class TestGrabInput:
|
|
"""Tests for _grab_input method."""
|
|
|
|
def test_production_global_grab_tcl_error(
|
|
self, mock_tk: MagicMock, mock_sys_exit: MagicMock, tmp_path: Path
|
|
) -> None:
|
|
"""Test production mode falls back when global grab fails."""
|
|
mock_tk.Tk.return_value.grab_set_global.side_effect = tk.TclError("grab failed")
|
|
locker = create_locker(mock_tk, tmp_path, demo_mode=False)
|
|
assert locker.demo_mode is False
|