One workout duration bar for every activity: advertise 30, accept 25 (walks included)

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Krzysztof Rudnicki 2026-11-01 17:33:33 +01:00
parent 8b0fb81d7b
commit dc885b9903
9 changed files with 64 additions and 55 deletions

View File

@ -53,13 +53,13 @@ WORKOUT_APP_JSON_REMOTES = (
)
# Port the workout app's HTTP server listens on (no ADB/developer-options needed).
WORKOUT_HTTP_PORT = 8765
# The ONE duration bar every workout type answers to — weightlifting, manual
# entries and runs alike. This is the ADVERTISED number: it is the only one
# that may ever appear in a message the user reads.
MIN_WORKOUT_DURATION_MINUTES = 40
# The ONE duration bar every activity answers to — weightlifting, manual
# entries, runs and walks (summed moving time) alike. This is the ADVERTISED
# number: it is the only one that may ever appear in a message the user reads.
MIN_WORKOUT_DURATION_MINUTES = 30
# ...and this is the leeway that is deliberately INVISIBLE. A session is
# actually accepted at MIN_WORKOUT_DURATION_MINUTES - this, so 35 minutes
# passes while every string still says 40. That gap is intentional (the user
# actually accepted at MIN_WORKOUT_DURATION_MINUTES - this, so 25 minutes
# passes while every string still says 30. That gap is intentional (the user
# asked for it explicitly): do NOT "fix" the inconsistency by advertising the
# real cutoff, and never interpolate WORKOUT_DURATION_ACCEPT_MINUTES into an
# f-string.
@ -67,7 +67,7 @@ WORKOUT_DURATION_LEEWAY_MINUTES = 5
# Derived so the advertised bar and the accept bar cannot drift apart.
# Comparisons use this; messages use MIN_WORKOUT_DURATION_MINUTES. Every
# caller compares with `>=` — one operator across all workout types, so a
# 35.0-minute session cannot pass one gate and fail another.
# 25.0-minute session cannot pass one gate and fail another.
WORKOUT_DURATION_ACCEPT_MINUTES = (
MIN_WORKOUT_DURATION_MINUTES - WORKOUT_DURATION_LEEWAY_MINUTES
)
@ -106,8 +106,8 @@ REST_DAY_FILE = Path(__file__).resolve().parent / "rest_days.json"
REST_DAYS_PER_ISO_WEEK = 2
GRACE_STATE_FILE = Path(__file__).resolve().parent / "grace_floor.json"
# A day's RunnerUp walks (commute legs) count as the workout once their
# MOVING time sums to this; moving = trackpoint pairs at walking speed.
WALK_MIN_MOVING_MINUTES = 40
# MOVING time reaches WORKOUT_DURATION_ACCEPT_MINUTES (the shared bar above);
# moving = trackpoint pairs at walking speed.
WALK_SPEED_MPS = (0.5, 3.0)
WALK_MAX_GAP_SECONDS = 30
# Workout records this PC must keep deleted in the sync store, pushed as

View File

@ -1,8 +1,8 @@
"""Walking in RunnerUp TCX exports: which files are walks, and their moving time.
A day's walking counts as the workout once its walks -- usually separate
commute legs, one file each -- add up to ``WALK_MIN_MOVING_MINUTES`` of
*moving* time.
commute legs, one file each -- add up to the shared workout duration bar
(``WORKOUT_DURATION_ACCEPT_MINUTES``) of *moving* time.
**Which files are walks.** RunnerUp's WebDAV and File synchronisers both
export with ``ExportOptions.getDefault()``, whose TCX writer knows only
@ -29,9 +29,10 @@ import logging
from typing import TYPE_CHECKING, Any
from screen_locker._constants import (
MIN_WORKOUT_DURATION_MINUTES,
WALK_MAX_GAP_SECONDS,
WALK_MIN_MOVING_MINUTES,
WALK_SPEED_MPS,
WORKOUT_DURATION_ACCEPT_MINUTES,
)
if TYPE_CHECKING:
@ -109,7 +110,7 @@ class WalkDay:
@property
def qualifies(self) -> bool:
"""Whether the day's walking counts as its workout."""
return self.moving_minutes >= WALK_MIN_MOVING_MINUTES
return self.moving_minutes >= WORKOUT_DURATION_ACCEPT_MINUTES
def message(self) -> str:
"""The verification line, in the RunnerUp verifier's own style."""
@ -121,7 +122,7 @@ class WalkDay:
def walk_shortfall(day: WalkDay) -> str:
"""Why a day's walking did not count yet."""
return f"{day.message()} -- need {WALK_MIN_MOVING_MINUTES}+ min moving"
return f"{day.message()} -- need {MIN_WORKOUT_DURATION_MINUTES}+ min moving"
def walk_day(walks: Iterable[dict[str, Any]]) -> WalkDay:
@ -134,7 +135,7 @@ def walk_day(walks: Iterable[dict[str, Any]]) -> WalkDay:
completed_at: float | None = None
for walk in ordered:
moving += float(walk.get("moving_seconds", 0.0))
if completed_at is None and moving / 60 >= WALK_MIN_MOVING_MINUTES:
if completed_at is None and moving / 60 >= WORKOUT_DURATION_ACCEPT_MINUTES:
completed_at = walk.get("ended_at")
return WalkDay(
legs=len(ordered),

View File

@ -8,16 +8,16 @@
"threshold, change it here and watch both suites fail together.",
"",
"advertised_minutes is the ONLY number that may appear in a user-facing",
"message. accept_minutes is deliberately invisible: a 35-minute session",
"passes while every string still says 40. Do not advertise accept_minutes."
"message. accept_minutes is deliberately invisible: a 25-minute session",
"passes while every string still says 30. Do not advertise accept_minutes."
],
"advertised_minutes": 40,
"advertised_minutes": 30,
"leeway_minutes": 5,
"accept_minutes": 35,
"accept_minutes": 25,
"cases": [
{ "duration_minutes": 34, "accepted": false },
{ "duration_minutes": 35, "accepted": true },
{ "duration_minutes": 40, "accepted": true },
{ "duration_minutes": 24, "accepted": false },
{ "duration_minutes": 25, "accepted": true },
{ "duration_minutes": 30, "accepted": true },
{ "duration_minutes": 60, "accepted": true }
]
}

View File

@ -156,16 +156,16 @@ class TestValidateRunnerupData:
) -> None:
"""Both criteria missed → too_short naming both.
0.1 km is far under the distance line and 30 min is under the duration
0.1 km is far under the distance line and 20 min is under the duration
line, so neither branch of the OR qualifies.
"""
locker = create_locker(mock_tk, tmp_path)
status, msg = locker._validate_runnerup_data(
{"sport": 0, "duration_seconds": 1800, "distance_m": 100}
{"sport": 0, "duration_seconds": 1200, "distance_m": 100}
)
assert status == "too_short"
assert "3.5+ km" in msg
assert "40+ min" in msg
assert "30+ min" in msg
def test_valid_run_returns_verified(
self,

View File

@ -106,14 +106,14 @@ class TestValidateRunnerupDataOrCriteria:
mock_sys_exit: MagicMock,
tmp_path: Path,
) -> None:
"""3.0 km in 25 min misses both branches; message names both."""
"""3.0 km in 20 min misses both branches; message names both."""
locker = create_locker(mock_tk, tmp_path)
status, msg = locker._validate_runnerup_data(
{"sport": 0, "duration_seconds": 1500, "distance_m": 3000}
{"sport": 0, "duration_seconds": 1200, "distance_m": 3000}
)
assert status == "too_short"
# Pinned literally: a ':.0f' format spec would render 3.5 as "4".
assert "need 3.5+ km or 40+ min" in msg
assert "need 3.5+ km or 30+ min" in msg
def test_distance_boundary_is_inclusive(
self,
@ -134,22 +134,22 @@ class TestValidateRunnerupDataOrCriteria:
mock_sys_exit: MagicMock,
tmp_path: Path,
) -> None:
"""Runs answer to the shared accept bar (35), not the advertised 40.
"""Runs answer to the shared accept bar (25), not the advertised 30.
The distance stays at 1 km throughout so the OR distance arm cannot
rescue the run — this pins the duration arm alone.
"""
locker = create_locker(mock_tk, tmp_path)
just_under, _ = locker._validate_runnerup_data(
{"sport": 0, "duration_seconds": 2094, "distance_m": 1000}
{"sport": 0, "duration_seconds": 1494, "distance_m": 1000}
)
assert just_under == "too_short"
at_accept_bar, _ = locker._validate_runnerup_data(
{"sport": 0, "duration_seconds": 2100, "distance_m": 1000}
{"sport": 0, "duration_seconds": 1500, "distance_m": 1000}
)
assert at_accept_bar == "verified"
def test_too_short_message_advertises_40_not_the_real_cutoff(
def test_too_short_message_advertises_30_not_the_real_cutoff(
self,
mock_tk: MagicMock,
mock_sys_exit: MagicMock,

View File

@ -91,12 +91,12 @@ class TestValidateRunnerupDataDistanceTolerance:
) -> None:
"""3.45 km * 1.05 = 3.62 >= 3.5 km minimum → verified.
Duration is held under 40 min so the distance branch is what is
Duration is held under the 25-min accept bar so the distance branch is what is
actually under test, not the duration branch of the OR.
"""
locker = create_locker(mock_tk, tmp_path)
status, msg = locker._validate_runnerup_data(
{"sport": 0, "duration_seconds": 1500, "distance_m": 3450.0}
{"sport": 0, "duration_seconds": 1200, "distance_m": 3450.0}
)
assert status == "verified"
assert "3.5 km" in msg
@ -109,11 +109,12 @@ class TestValidateRunnerupDataDistanceTolerance:
) -> None:
"""3.0 km * 1.05 = 3.15 km, still short of the 3.5 km minimum → too_short.
Duration is under 40 min so the duration branch cannot rescue it.
Duration is under the 25-min accept bar so the duration branch cannot
rescue it.
"""
locker = create_locker(mock_tk, tmp_path)
status, msg = locker._validate_runnerup_data(
{"sport": 0, "duration_seconds": 1500, "distance_m": 3000}
{"sport": 0, "duration_seconds": 1200, "distance_m": 3000}
)
assert status == "too_short"
assert "3.5+ km" in msg
@ -128,6 +129,6 @@ class TestValidateRunnerupDataDistanceTolerance:
additive leeway, not a stricter requirement)."""
locker = create_locker(mock_tk, tmp_path)
status, _ = locker._validate_runnerup_data(
{"sport": 0, "duration_seconds": 1947, "distance_m": 5000}
{"sport": 0, "duration_seconds": 1200, "distance_m": 5000}
)
assert status == "verified"

View File

@ -97,8 +97,8 @@ class TestVerifyToday:
return verifier._verify_runnerup_via_files()
def test_walks_summing_to_the_bar_verify(self, verifier: Verifier) -> None:
drop(verifier.folder, "08-00-00_Walking", tcx("Other", 25, "08:00"))
drop(verifier.folder, "17-00-00_Walking", tcx("Other", 20, "17:00"))
drop(verifier.folder, "08-00-00_Walking", tcx("Other", 15, "08:00"))
drop(verifier.folder, "17-00-00_Walking", tcx("Other", 12, "17:00"))
result = self._verify(verifier)
assert result is not None
assert result[0] == "verified"
@ -111,7 +111,7 @@ class TestVerifyToday:
drop(verifier.folder, "08-00-00_Walking", tcx("Other", 60, "08:00", 0.1))
assert self._verify(verifier) == (
"too_short",
"Walking: 1 walk(s), 0.4 km, 0 min moving -- need 40+ min moving",
"Walking: 1 walk(s), 0.4 km, 0 min moving -- need 30+ min moving",
)
def test_a_run_still_verifies_beside_short_walks(self, verifier: Verifier) -> None:
@ -135,8 +135,8 @@ class TestBackfill:
def test_walks_are_logged_as_one_workout(
self, verifier: Verifier, tmp_path: Path
) -> None:
drop(verifier.folder, "08-00-00_Walking", tcx("Other", 25, "08:00"))
drop(verifier.folder, "17-00-00_Walking", tcx("Other", 20, "17:00"))
drop(verifier.folder, "08-00-00_Walking", tcx("Other", 15, "08:00"))
drop(verifier.folder, "17-00-00_Walking", tcx("Other", 12, "17:00"))
log = tmp_path / "log.json"
ingested = MagicMock()
assert verifier._try_fill_runnerup_for_date(_DAY, log, ingested) is True
@ -144,10 +144,10 @@ class TestBackfill:
data = entry["workout_data"]
assert data["type"] == "runnerup_verified"
assert data["activity"] == "walking"
assert data["duration_minutes"] == pytest.approx(45)
assert data["duration_minutes"] == pytest.approx(27)
assert (
data["completed_at"]
== datetime(2026, 10, 10, 17, 20, tzinfo=UTC).timestamp()
== datetime(2026, 10, 10, 17, 12, tzinfo=UTC).timestamp()
)
ingested.assert_called_once()
# The same day again is the same slot: nothing new.
@ -159,7 +159,7 @@ class TestBackfill:
tmp_path: Path,
caplog: pytest.LogCaptureFixture,
) -> None:
drop(verifier.folder, "08-00-00_Walking", tcx("Other", 30, "08:00"))
drop(verifier.folder, "08-00-00_Walking", tcx("Other", 20, "08:00"))
log = tmp_path / "log.json"
with caplog.at_level(logging.WARNING):
assert verifier._try_fill_runnerup_for_date(_DAY, log) is False

View File

@ -65,16 +65,16 @@ class TestValidateSession:
assert "not a number" in detail
def test_accepts_exactly_the_hidden_accept_threshold(self) -> None:
"""35 min passes; the message still advertises the round 40."""
ok, _ = validate_session(_session(duration_seconds=35 * 60))
"""25 min passes; the message still advertises the round 30."""
ok, _ = validate_session(_session(duration_seconds=25 * 60))
assert ok
def test_rejects_just_below_the_accept_threshold(self) -> None:
ok, detail = validate_session(_session(duration_seconds=34 * 60 + 59))
ok, detail = validate_session(_session(duration_seconds=24 * 60 + 59))
assert not ok
# The real bar (35) must never leak into a user-visible string.
assert "35" not in detail
assert "40 min" in detail
# The real bar (25) must never leak into a user-visible string.
assert "25" not in detail
assert "30 min" in detail
class TestBuildSessionEntry:

View File

@ -111,12 +111,12 @@ class TestWalkDay:
assert day.legs == 3
assert day.moving_minutes == pytest.approx(50)
assert day.distance_km == pytest.approx(6)
assert day.completed_at == 200.0 # a (25) + b (20) crosses 40 at b's end
assert day.completed_at == 100.0 # a (25) alone crosses the bar at its end
assert day.qualifies
assert day.message() == "Walking: 3 walk(s), 6.0 km, 50 min moving"
def test_the_same_walk_from_two_sources_counts_once(self) -> None:
copy = leg("same", 30, 100.0)
copy = leg("same", 20, 100.0)
day = walk_day([copy, dict(copy)])
assert day.legs == 1
assert not day.qualifies
@ -130,5 +130,12 @@ class TestWalkDay:
assert day.completed_at is None
def test_shortfall_says_what_is_missing(self) -> None:
text = walk_shortfall(walk_day([leg("a", 30, 1.0)]))
assert text.endswith("need 40+ min moving")
text = walk_shortfall(walk_day([leg("a", 20, 1.0)]))
assert text.endswith("need 30+ min moving")
def test_walks_share_the_one_workout_accept_bar(self) -> None:
# Same advertised-30 / accept-25 bar as every other workout type.
assert not walk_day([leg("a", 24.9, 1.0)]).qualifies
day = walk_day([leg("a", 25, 1.0)])
assert day.qualifies
assert day.completed_at == 1.0