Files
wodify-cli/tests/test_schedule.py
T
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

87 lines
3.0 KiB
Python

"""Schedule parsing.
Shape from a live capture 2026-07-25: Response.ResponseClassList.Class.List.
Two bugs are pinned here because both produced plausible-looking wrong output
rather than an error.
"""
from __future__ import annotations
from wodify import render
# Abridged real row. GroupMemberReservationStatus.ClassId == "0" is the trap:
# flattening nested dicts up clobbers the real Id and collapses every class
# into a single row.
def _row(cid, name, start, program, booked, coach):
return {
"Id": cid,
"Name": name,
"StartTime": start,
"EndTime": "13:00:00",
"StartDate": "2026-07-20",
"ProgramId": program,
"ClassLimit": 10,
"ReservationCount": booked,
"WaitlistCount": 0,
"Status": "",
"Coaches": {"List": [{"Name": coach, "Id": "5706147"}]},
"GroupMemberReservationStatus": {"ClassId": "0", "IsSignedIn": {"List": []}},
}
DAY = {"Response": {"ResponseClassList": {"Class": {"List": [
_row("190917816", "CrossFit: 5:30 PM", "17:30:00", "133059", 9, "Pat Doyle"),
_row("190917671", "CrossFit: 12:00 PM", "12:00:00", "133059", 10, "Chris Nolan"),
_row("190918133", "CrossFit Pump: 6:30 PM", "18:30:00", "131311", 7, "Pat Doyle"),
]}}}}
def test_all_classes_are_returned_not_collapsed():
"""Regression: nested ClassId "0" previously collapsed all rows into one."""
rows = render.class_rows(DAY)
assert len(rows) == 3
assert [r["class_id"] for r in rows] == ["190917671", "190917816", "190918133"]
def test_rows_sorted_by_start_time():
assert [r["start"] for r in render.class_rows(DAY)] == \
["12:00:00", "17:30:00", "18:30:00"]
def test_real_id_survives_nested_class_id_zero():
row = render.class_rows(DAY)[1]
assert row["class_id"] == "190917816"
assert row["class_id"] != "0"
def test_coach_extracted_from_nested_list():
by_id = {r["class_id"]: r for r in render.class_rows(DAY)}
assert by_id["190917671"]["coach"] == "Chris Nolan"
assert by_id["190918133"]["coach"] == "Pat Doyle"
def test_program_ids_distinguish_workouts():
"""Classes in different programs have different WODs, so program_id must
survive -- reporting one program's workout for another is a wrong answer."""
programs = {r["program_id"] for r in render.class_rows(DAY)}
assert programs == {"133059", "131311"}
def test_missing_coach_list_does_not_crash():
row = _row("1", "X", "06:00:00", "133059", 1, "Y")
row["Coaches"] = {"List": []}
assert render.class_rows({"Class": {"List": [row]}})[0]["coach"] == ""
def test_placeholder_rows_dropped():
day = {"Class": {"List": [_row("0", "", "00:00:00", "0", 0, "")]}}
assert render.class_rows(day) == []
def test_classes_render_includes_capacity_coach_and_id():
out = render.classes(DAY)
assert "CrossFit: 5:30 PM" in out
assert "9 | 10" in out
assert "coach=Pat Doyle" in out
assert "class_id=190917816" in out
assert out.count("\n- ") + 1 == 3, "all three classes should be listed"