Files
wodify-cli/tests/test_workout_shape.py
lucioandClaude Opus 5 5a61890011 feat: read-only Wodify athlete client
A CLI that answers "what classes ran", "who was there" and "what was the
workout" with a single HTTP POST each -- no browser at query time.

Wodify's athlete app is an OutSystems Reactive SPA. Driving it with a headless
browser works but is slow and, for an LLM agent, expensive: a full page render
to read a few lines back out of the DOM. Its data actions are plain JSON
endpoints, so this talks to them directly.

  wodify list date 2026-07-25      time, coach, booked/max, title, duration, ID
  wodify list workout <id>         attendee names + that class's workout

Two phases. `prime` attaches over CDP to a browser that is already logged in and
captures the session cookies, the CSRF token, and each action's apiVersion and
body shape. Everything after that is stdlib HTTP -- the query path has no
third-party dependencies at all.

Three things genuinely require the browser, which is why priming exists:
- the session bearer nr1W_Theme_UI is HttpOnly, so it cannot be exported by hand
- X-CSRFToken is not in cookies, localStorage, or any JS global
- apiVersion is per-action, not global

Read-only is enforced in code, not by the credential (Wodify issues no
read-only token): an explicit allowlist plus a write-marker guard applied even
to allowlisted paths, so a careless registry edit fails closed. Tests assert
that real mutating endpoint names are refused.

Responses are cached in SQLite, keyed by the local date they were fetched on, so
a repeat query the same day touches no network. All fetching funnels through
service.py so a call site cannot forget the cache.

Staleness is explicit rather than silent: responses carry
hasModuleVersionChanged/hasApiVersionChanged, and the client raises
VersionStale/SessionExpired instead of returning a plausible empty result. That
matters because every bug found while building this produced confident wrong
output rather than an error -- see CLAUDE.md for the field-name traps.

History squashed before first publication: earlier commits embedded real gym
members' names in test fixtures. Fixtures now use fictional names.

122 tests, no network required.

Co-Authored-By: Claude Opus 5 <[email protected]>
2026-07-25 09:23:19 -07:00

115 lines
4.0 KiB
Python

"""GetAllWorkoutData's real response shape, plus bookings range filtering.
Shape taken from a live capture on 2026-07-25: the workout lives at
Response.ResponseWOD.ResponseWorkout, not in ProgramWODList.
"""
from __future__ import annotations
from wodify import render
LIVE_SHAPE = {
"Response": {
"HasActiveMembership": True,
"ErrorMessage": "",
"ResponseWOD": {
"WorkoutError": {"HasError": False, "ErrorMessage": ""},
"ResponseWorkout": {
"Id": "25201299",
"Date": "2026-07-25",
"Name": "CrossFit - Sat, Jul 25",
"Notes": "<h2>Saturday July 25</h2><p>Daily Coach's Brief</p>",
"WorkoutComponents": {"List": [
{"Id": "123372298", "IsSection": True, "Name": "WARM-UP",
"Comment": "<p>General Warm-Up</p><p>2 x</p><p>15 Banded Good Mornings</p>"},
{"Id": "123372299", "IsSection": False,
"Name": "091022 Games Mash-Up",
"Comment": "<p>7 Rounds</p><p>3 Wall Walks</p>"},
]},
},
},
}
}
UNPOSTED = {
"Response": {
"ErrorMessage": "",
"ResponseWOD": {
"WorkoutError": {"HasError": False, "ErrorMessage": ""},
"ResponseWorkout": {"Id": "0", "Date": "1900-01-01", "Name": "",
"Notes": "", "WorkoutComponents": {"List": []}},
},
}
}
def test_live_shape_renders_title_and_components():
out = render.workout(LIVE_SHAPE)
assert out.startswith("# CrossFit - Sat, Jul 25")
assert "## WARM-UP" in out, "IsSection components should be marked as sections"
assert "15 Banded Good Mornings" in out
assert "091022 Games Mash-Up" in out and "3 Wall Walks" in out
assert "<" not in out
def test_coach_brief_excluded_by_default():
"""Notes runs to thousands of characters; keeping output small is the point."""
assert "Daily Coach's Brief" not in render.workout(LIVE_SHAPE)
def test_coach_brief_included_on_request():
out = render.workout(LIVE_SHAPE, notes=True)
assert "## Coach's brief" in out and "Daily Coach's Brief" in out
def test_unposted_workout_is_reported_as_not_posted():
out = render.workout(UNPOSTED)
assert "not posted" in out.lower()
assert "# " not in out
# --- bookings range filtering -------------------------------------------------
BOOKINGS = {"Response": {"List": [
{"Class": {"StartDate": "2026-07-20", "StartTime": "17:30:00",
"ClassName": "CrossFit: 5:30 PM", "ClassId": "101"},
"StatusLabel": "Attended"},
{"Class": {"StartDate": "2026-07-22", "StartTime": "17:30:00",
"ClassName": "CrossFit: 5:30 PM", "ClassId": "102"},
"StatusLabel": "Attended"},
{"Class": {"StartDate": "2026-07-27", "StartTime": "17:30:00",
"ClassName": "CrossFit: 5:30 PM", "ClassId": "103"},
"StatusLabel": "Reserved"},
]}}
def test_booking_rows_are_sorted_and_expose_class_id():
rows = render.booking_rows(BOOKINGS)
assert [r["date"] for r in rows] == ["2026-07-20", "2026-07-22", "2026-07-27"]
assert rows[0]["class_id"] == "101"
def test_bookings_since_filter():
out = render.bookings(BOOKINGS, since="2026-07-22")
assert "2026-07-20" not in out and "2026-07-22" in out and "2026-07-27" in out
def test_bookings_until_filter():
out = render.bookings(BOOKINGS, until="2026-07-22")
assert "2026-07-27" not in out and "2026-07-20" in out
def test_bookings_attended_only_excludes_future_reservations():
out = render.bookings(BOOKINGS, attended_only=True)
assert "2026-07-27" not in out
assert "2026-07-20" in out and "2026-07-22" in out
def test_bookings_exposes_class_id_for_chaining():
"""Requirement: list attended classes, then fetch each one's workout."""
assert "class_id=101" in render.bookings(BOOKINGS)
def test_empty_filter_result_reports_pre_filter_count():
out = render.bookings(BOOKINGS, since="2030-01-01")
assert "No bookings found" in out and "3 returned before filtering" in out