mirror of
https://github.com/kuhyx/screen-locker.git
synced 2026-11-11 19:13:55 +01:00
A manual workout may be dated today..today+7 days (no past dates). A future entry is held out of log.json and earns nothing until its date; the first sync/lock/reset pass on that day ingests and credits it like a workout logged that day. - ingest_manual_records defers records dated after the local day and counts the budget (2/7d, 10/30d) against the workout's own date over logged credits plus pending future records (a pending record never rejects itself) - PC-logged future entries (Tk dialog, --log-manual-workout --date) go to a durable pending ledger re-published on every push (a one-shot publish to devices/pc is overwritten by the next push); tombstones win, entries leave the ledger once logged or deleted - one shared date helper (validate_workout_date) used by the dialog and CLI; the form's date field rides in the sport row to keep the lock screen fitting - poke start_the_day also ingests due local entries after the daily reset - PC admission rule pinned to the phone's by the shared budget fixture (planned.expected_by_date) Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015mmTBiMpvyfLuEAXc6nDYT
248 lines
9.6 KiB
Python
248 lines
9.6 KiB
Python
"""Tests for the manual-workout evidence-form dialog mixin."""
|
|
# pylint: disable=protected-access
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import TYPE_CHECKING
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
from screen_locker import _manual_workout
|
|
from screen_locker._day import today_str
|
|
from screen_locker.tests.conftest import create_locker
|
|
|
|
if TYPE_CHECKING:
|
|
from pathlib import Path
|
|
|
|
_CLOSED = "screen_locker._manual_workout_date.budget_closed_for_all_days"
|
|
|
|
|
|
class TestShowManualWorkoutForm:
|
|
"""Tests for _show_manual_workout_form."""
|
|
|
|
def test_renders_form_when_budget_available(
|
|
self, mock_tk: MagicMock, mock_sys_exit: MagicMock, tmp_path: Path
|
|
) -> None:
|
|
locker = create_locker(mock_tk, tmp_path)
|
|
with patch(_CLOSED, return_value=False):
|
|
locker._show_manual_workout_form()
|
|
button_texts = {c.kwargs.get("text") for c in mock_tk.Button.call_args_list}
|
|
assert "SUBMIT" in button_texts
|
|
assert "BACK" in button_texts
|
|
|
|
def test_shows_exhausted_message_and_hides_submit(
|
|
self, mock_tk: MagicMock, mock_sys_exit: MagicMock, tmp_path: Path
|
|
) -> None:
|
|
locker = create_locker(mock_tk, tmp_path)
|
|
with patch(_CLOSED, return_value=True):
|
|
locker._show_manual_workout_form()
|
|
button_texts = {c.kwargs.get("text") for c in mock_tk.Button.call_args_list}
|
|
assert "BACK" in button_texts
|
|
assert "SUBMIT" not in button_texts
|
|
text_calls = [c.kwargs.get("text", "") for c in mock_tk.Label.call_args_list]
|
|
assert any("exhausted" in t for t in text_calls)
|
|
|
|
|
|
class TestSportToggle:
|
|
"""Tests for _on_mw_sport_changed and _current_mw_sport."""
|
|
|
|
def test_selecting_other_shows_other_frame(
|
|
self, mock_tk: MagicMock, mock_sys_exit: MagicMock, tmp_path: Path
|
|
) -> None:
|
|
locker = create_locker(mock_tk, tmp_path)
|
|
locker._mw_tt_frame = MagicMock()
|
|
locker._mw_other_frame = MagicMock()
|
|
locker._mw_sport_var = MagicMock()
|
|
locker._on_mw_sport_changed("Other")
|
|
locker._mw_tt_frame.grid_remove.assert_called_once()
|
|
locker._mw_other_frame.grid.assert_called_once_with()
|
|
|
|
def test_selecting_table_tennis_shows_tt_frame(
|
|
self, mock_tk: MagicMock, mock_sys_exit: MagicMock, tmp_path: Path
|
|
) -> None:
|
|
locker = create_locker(mock_tk, tmp_path)
|
|
locker._mw_tt_frame = MagicMock()
|
|
locker._mw_other_frame = MagicMock()
|
|
locker._mw_sport_var = MagicMock()
|
|
locker._on_mw_sport_changed("Table tennis")
|
|
locker._mw_other_frame.grid_remove.assert_called_once()
|
|
locker._mw_tt_frame.grid.assert_called_once_with()
|
|
|
|
def test_unknown_label_defaults_to_table_tennis(
|
|
self, mock_tk: MagicMock, mock_sys_exit: MagicMock, tmp_path: Path
|
|
) -> None:
|
|
locker = create_locker(mock_tk, tmp_path)
|
|
locker._mw_tt_frame = MagicMock()
|
|
locker._mw_other_frame = MagicMock()
|
|
locker._mw_sport_var = MagicMock()
|
|
locker._mw_sport_var.get.return_value = "not a real label"
|
|
assert locker._current_mw_sport() == _manual_workout.SPORT_TABLE_TENNIS
|
|
|
|
|
|
class TestGridCursor:
|
|
"""Tests for the two-column grid cursor helper _mw_next_full_row."""
|
|
|
|
def test_full_row_from_even_cursor_advances_by_a_row(
|
|
self, mock_tk: MagicMock, mock_sys_exit: MagicMock, tmp_path: Path
|
|
) -> None:
|
|
locker = create_locker(mock_tk, tmp_path)
|
|
locker._mw_grid_counters = {}
|
|
parent = object()
|
|
# Fresh (even) cursor: reserves row 0, then row 1 on the next call.
|
|
assert locker._mw_next_full_row(parent) == 0
|
|
assert locker._mw_next_full_row(parent) == 1
|
|
|
|
def test_full_row_from_odd_cursor_bumps_to_next_row(
|
|
self, mock_tk: MagicMock, mock_sys_exit: MagicMock, tmp_path: Path
|
|
) -> None:
|
|
locker = create_locker(mock_tk, tmp_path)
|
|
parent = object()
|
|
# An odd cursor (a half-width cell already placed on the left) must be
|
|
# bumped to a fresh row before a full-width item is placed.
|
|
locker._mw_grid_counters = {id(parent): 1}
|
|
assert locker._mw_next_full_row(parent) == 1
|
|
|
|
|
|
class TestSubmitManualWorkoutForm:
|
|
"""Tests for _submit_manual_workout_form."""
|
|
|
|
def _setup_locker(
|
|
self,
|
|
mock_tk: MagicMock,
|
|
tmp_path: Path,
|
|
*,
|
|
sport_label: str = "Table tennis",
|
|
fields: dict[str, object] | None = None,
|
|
) -> object:
|
|
defaults: dict[str, object] = {
|
|
"date": today_str(),
|
|
"start_time": "12:00",
|
|
"end_time": "14:00",
|
|
"location_name": "Osrodek Solec",
|
|
"transport_method": "bike",
|
|
"cost": "60 PLN",
|
|
"reservation_phone": "",
|
|
"techniques_practiced": "",
|
|
"warm_up_minutes": "",
|
|
"pain_or_injury": "none",
|
|
"racket": "pro spin",
|
|
"balls": "nittaku",
|
|
"activity_type_other": "squash",
|
|
"went_well": "x" * 30,
|
|
"to_improve": "y" * 30,
|
|
"overall_feeling": "z" * 30,
|
|
"activity_details": "q" * 45,
|
|
}
|
|
if fields:
|
|
defaults.update(fields)
|
|
|
|
locker = create_locker(mock_tk, tmp_path)
|
|
locker._mw_sport_var = MagicMock()
|
|
locker._mw_sport_var.get.return_value = sport_label
|
|
locker._mw_rpe_var = MagicMock()
|
|
locker._mw_rpe_var.get.return_value = 6
|
|
|
|
locker._mw_vars = {}
|
|
for key in (
|
|
"start_time",
|
|
"end_time",
|
|
"location_name",
|
|
"transport_method",
|
|
"cost",
|
|
"reservation_phone",
|
|
"techniques_practiced",
|
|
"warm_up_minutes",
|
|
"pain_or_injury",
|
|
"racket",
|
|
"balls",
|
|
"activity_type_other",
|
|
"equipment",
|
|
"date",
|
|
):
|
|
var = MagicMock()
|
|
var.get.return_value = defaults.get(key, "")
|
|
locker._mw_vars[key] = var
|
|
|
|
locker._mw_int_vars = {}
|
|
for key, default_val in (
|
|
("matches_won", 1),
|
|
("matches_lost", 4),
|
|
("sets_won", 2),
|
|
("sets_lost", 9),
|
|
):
|
|
var = MagicMock()
|
|
var.get.return_value = defaults.get(key, default_val)
|
|
locker._mw_int_vars[key] = var
|
|
|
|
locker._mw_text_widgets = {}
|
|
for key in ("went_well", "to_improve", "overall_feeling", "activity_details"):
|
|
widget = MagicMock()
|
|
widget.get.return_value = defaults[key]
|
|
locker._mw_text_widgets[key] = widget
|
|
|
|
locker._mw_error_label = MagicMock()
|
|
object.__setattr__(locker, "_on_manual_workout_saved", MagicMock())
|
|
return locker
|
|
|
|
def test_valid_table_tennis_calls_saved_hook(
|
|
self, mock_tk: MagicMock, mock_sys_exit: MagicMock, tmp_path: Path
|
|
) -> None:
|
|
locker = self._setup_locker(mock_tk, tmp_path, sport_label="Table tennis")
|
|
locker._submit_manual_workout_form()
|
|
locker._on_manual_workout_saved.assert_called_once()
|
|
entry = locker._on_manual_workout_saved.call_args.args[0]
|
|
assert entry["sport"] == "table_tennis"
|
|
assert entry["matches_lost"] == 4
|
|
locker._mw_error_label.config.assert_not_called()
|
|
|
|
def test_valid_other_sport_calls_saved_hook(
|
|
self, mock_tk: MagicMock, mock_sys_exit: MagicMock, tmp_path: Path
|
|
) -> None:
|
|
locker = self._setup_locker(mock_tk, tmp_path, sport_label="Other")
|
|
locker._submit_manual_workout_form()
|
|
locker._on_manual_workout_saved.assert_called_once()
|
|
entry = locker._on_manual_workout_saved.call_args.args[0]
|
|
assert entry["sport"] == "other"
|
|
assert entry["activity_type"] == "squash"
|
|
|
|
def test_validation_failure_shows_error_and_skips_hook(
|
|
self, mock_tk: MagicMock, mock_sys_exit: MagicMock, tmp_path: Path
|
|
) -> None:
|
|
locker = self._setup_locker(mock_tk, tmp_path, fields={"location_name": " "})
|
|
locker._submit_manual_workout_form()
|
|
locker._mw_error_label.config.assert_called_once()
|
|
locker._on_manual_workout_saved.assert_not_called()
|
|
|
|
def test_rpe_value_error_treated_as_invalid(
|
|
self, mock_tk: MagicMock, mock_sys_exit: MagicMock, tmp_path: Path
|
|
) -> None:
|
|
locker = self._setup_locker(mock_tk, tmp_path)
|
|
locker._mw_rpe_var.get.side_effect = ValueError("bad")
|
|
locker._submit_manual_workout_form()
|
|
locker._mw_error_label.config.assert_called_once()
|
|
locker._on_manual_workout_saved.assert_not_called()
|
|
|
|
def test_int_field_value_error_treated_as_zero(
|
|
self, mock_tk: MagicMock, mock_sys_exit: MagicMock, tmp_path: Path
|
|
) -> None:
|
|
locker = self._setup_locker(mock_tk, tmp_path)
|
|
locker._mw_int_vars["matches_won"].get.side_effect = ValueError("bad")
|
|
locker._mw_int_vars["matches_lost"].get.return_value = 0
|
|
locker._submit_manual_workout_form()
|
|
# matches_won=0 (from the ValueError fallback) and matches_lost=0
|
|
# -> "zero matches played" validation error.
|
|
locker._mw_error_label.config.assert_called_once()
|
|
locker._on_manual_workout_saved.assert_not_called()
|
|
|
|
|
|
class TestInitialKeyboardFocus:
|
|
"""Exactly one field takes focus when a typed-input screen is built."""
|
|
|
|
def test_manual_workout_form_focuses_one_entry(
|
|
self, mock_tk: MagicMock, mock_sys_exit: MagicMock, tmp_path: Path
|
|
) -> None:
|
|
locker = create_locker(mock_tk, tmp_path)
|
|
with patch(_CLOSED, return_value=False):
|
|
locker._show_manual_workout_form()
|
|
focused = mock_tk.Entry.return_value.focus_set.call_count
|
|
assert focused == 1, f"expected exactly one focused entry, got {focused}"
|