From 7eca3f41458af8aefdb73e6b80e871b1e487c7da Mon Sep 17 00:00:00 2001 From: Krzysztof kuhy Rudnicki Date: Mon, 20 Jul 2026 08:57:25 +0200 Subject: [PATCH] Pin the StrongLifts week-scan tests to a fixed Wednesday MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit test_fills_gap_for_past_week_day and test_rescanning_an_already_filled_day built their fixture date as date.today() - 1 day and asserted it was "this week". _try_fill_stronglifts_for_week accepts week_start <= entry <= today, and on a Monday week_start IS today — so yesterday (Sunday) belongs to the previous ISO week and both tests failed. They passed Tue-Sun and failed every Monday, which is exactly when they were noticed: they blocked an unrelated push. Pin "now" to Wednesday 2026-07-22 with the _FakeDatetime pattern already used in test_status_part2, so the fixture day is Tuesday — comfortably inside the week whatever day the suite actually runs. Verified under faketime on Mon, Tue, Fri and Sun: 8 passed each time. The neighbouring "last week" case needs no pinning: 8 days back always precedes a week start, on any weekday. Also covers the remaining branch in _try_fill_stronglifts_for_week — an in-week JSON that fails content validation must not be written. It was unreachable on a Monday, so the file had never hit 100% on one; the suite is now 859 passed / 100% coverage. Co-Authored-By: Claude Opus 4.8 (1M context) --- .../tests/test_phone_verification_part5.py | 61 +++++++++++++++++-- 1 file changed, 55 insertions(+), 6 deletions(-) diff --git a/screen_locker/tests/test_phone_verification_part5.py b/screen_locker/tests/test_phone_verification_part5.py index 8a4f910..ee7b44e 100644 --- a/screen_locker/tests/test_phone_verification_part5.py +++ b/screen_locker/tests/test_phone_verification_part5.py @@ -2,7 +2,7 @@ from __future__ import annotations -from datetime import date, timedelta +from datetime import date, datetime, timedelta, timezone import json from typing import TYPE_CHECKING from unittest.mock import MagicMock, patch @@ -13,6 +13,22 @@ if TYPE_CHECKING: from pathlib import Path +# These two tests need a day that is BOTH in the past and inside the current +# ISO week. On a Monday no such day exists (week_start == today), so using +# date.today() made them fail every Monday and pass Tue-Sun. Pin "now" to a +# Wednesday instead: yesterday is then Tuesday, comfortably inside the week. +_FROZEN_NOW = datetime(2026, 7, 22, 12, 0, tzinfo=timezone.utc) # a Wednesday +_FROZEN_YESTERDAY = (_FROZEN_NOW.date() - timedelta(days=1)).isoformat() + + +class _FakeDatetime(datetime): + """datetime whose now() is pinned to _FROZEN_NOW.""" + + @classmethod + def now(cls, tz=None): + return _FROZEN_NOW.astimezone(tz) if tz else _FROZEN_NOW + + class TestTryFillStronglifitsForWeek: """Tests for _try_fill_stronglifts_for_week (week-scan backfill).""" @@ -81,7 +97,7 @@ class TestTryFillStronglifitsForWeek: locker = create_locker(mock_tk, tmp_path) log_file = tmp_path / "log.json" log_file.write_text("{}") - yesterday = (date.today() - timedelta(days=1)).isoformat() + yesterday = _FROZEN_YESTERDAY object.__setattr__(locker, "_has_adb_device", MagicMock(return_value=True)) object.__setattr__( locker, @@ -97,7 +113,7 @@ class TestTryFillStronglifitsForWeek: locker = create_locker(mock_tk, tmp_path) log_file = tmp_path / "log.json" log_file.write_text("{}") - yesterday = (date.today() - timedelta(days=1)).isoformat() + yesterday = _FROZEN_YESTERDAY object.__setattr__(locker, "_has_adb_device", MagicMock(return_value=True)) object.__setattr__( locker, @@ -112,7 +128,10 @@ class TestTryFillStronglifitsForWeek: ), ) - with patch("screen_locker._log_mixin.compute_entry_hmac", return_value="sig"): + with ( + patch("screen_locker._log_mixin.compute_entry_hmac", return_value="sig"), + patch("screen_locker._phone_verification.datetime", _FakeDatetime), + ): result = locker._try_fill_stronglifts_for_week(log_file) assert result == 1 @@ -127,7 +146,7 @@ class TestTryFillStronglifitsForWeek: locker = create_locker(mock_tk, tmp_path) log_file = tmp_path / "log.json" log_file.write_text("{}") - yesterday = (date.today() - timedelta(days=1)).isoformat() + yesterday = _FROZEN_YESTERDAY data = { "date": yesterday, "exercises": ["x"], @@ -139,8 +158,38 @@ class TestTryFillStronglifitsForWeek: locker, "_pull_workout_app_json", MagicMock(return_value=data) ) - with patch("screen_locker._log_mixin.compute_entry_hmac", return_value="sig"): + with ( + patch("screen_locker._log_mixin.compute_entry_hmac", return_value="sig"), + patch("screen_locker._phone_verification.datetime", _FakeDatetime), + ): first = locker._try_fill_stronglifts_for_week(log_file) second = locker._try_fill_stronglifts_for_week(log_file) assert (first, second) == (1, 0) + + def test_in_week_but_unverified_json_is_not_written( + self, mock_tk: MagicMock, mock_sys_exit: MagicMock, tmp_path: Path + ) -> None: + """A this-week JSON that fails validation is rejected, not backfilled. + + The date gate and the content gate are separate: being in the current + week only earns the JSON a validation attempt, never a log entry. + """ + locker = create_locker(mock_tk, tmp_path) + log_file = tmp_path / "log.json" + log_file.write_text("{}") + object.__setattr__(locker, "_has_adb_device", MagicMock(return_value=True)) + object.__setattr__( + locker, + "_pull_workout_app_json", + MagicMock(return_value={"date": _FROZEN_YESTERDAY, "exercises": []}), + ) + object.__setattr__( + locker, + "_validate_json_content", + MagicMock(return_value=("rejected", "no exercises")), + ) + + with patch("screen_locker._phone_verification.datetime", _FakeDatetime): + assert locker._try_fill_stronglifts_for_week(log_file) == 0 + assert json.loads(log_file.read_text()) == {}