Files
wodify-cli/tests/test_service.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

101 lines
3.5 KiB
Python

"""Cache-through behaviour: the second read of the day must not hit the network."""
from __future__ import annotations
import pytest
from wodify import service, store
@pytest.fixture()
def conn(tmp_path, monkeypatch):
monkeypatch.setenv("WODIFY_DB", str(tmp_path / "t.db"))
c = store.connect()
yield c
c.close()
SCHEDULE = {"Response": {"ResponseClassList": {"Class": {"List": [
{"Id": "190917816", "Name": "CrossFit: 5:30 PM", "StartTime": "17:30:00",
"EndTime": "18:30:00", "StartDate": "2026-07-20", "ProgramId": "133059",
"ClassLimit": 10, "ReservationCount": 9, "WaitlistCount": 0,
"Coaches": {"List": [{"Name": "Pat Doyle"}]}},
]}}}}
def test_first_call_hits_network_second_uses_cache(conn, monkeypatch):
calls = []
def fake_call(key, overrides=None, **kw):
calls.append(key)
return SCHEDULE
monkeypatch.setattr(service.client, "call", fake_call)
rows, first = service.day_classes(conn, "2026-07-20")
assert first.source == "network" and len(rows) == 1
assert calls == ["schedule"]
rows2, second = service.day_classes(conn, "2026-07-20")
assert second.source == "cache" and second.cached
assert calls == ["schedule"], "second read must not touch the network"
assert rows2 == rows
def test_refresh_forces_a_network_read(conn, monkeypatch):
calls = []
monkeypatch.setattr(service.client, "call",
lambda k, o=None, **kw: calls.append(k) or SCHEDULE)
service.day_classes(conn, "2026-07-20")
service.day_classes(conn, "2026-07-20", refresh=True)
assert len(calls) == 2
def test_day_classes_indexes_ids_for_later_lookup(conn, monkeypatch):
monkeypatch.setattr(service.client, "call", lambda k, o=None, **kw: SCHEDULE)
service.day_classes(conn, "2026-07-20")
klass = service.resolve_class(conn, "190917816")
assert klass["date"] == "2026-07-20"
assert klass["program_id"] == "133059"
def test_cache_key_separates_dates(conn, monkeypatch):
seen = []
monkeypatch.setattr(service.client, "call",
lambda k, o=None, **kw: seen.append(dict(o or {})) or SCHEDULE)
service.day_classes(conn, "2026-07-20")
service.day_classes(conn, "2026-07-21")
assert [s["FromDate"] for s in seen] == ["2026-07-20", "2026-07-21"]
def test_workout_cache_key_separates_programs(conn, monkeypatch):
seen = []
monkeypatch.setattr(service.client, "call",
lambda k, o=None, **kw: seen.append(dict(o or {})) or {"x": 1})
service.workout(conn, "2026-07-20", "133059")
service.workout(conn, "2026-07-20", "131311")
service.workout(conn, "2026-07-20", "133059") # cached
assert [s.get("GymProgramId") for s in seen] == ["133059", "131311"]
def test_attendees_raises_maxrecords_for_large_classes(conn, monkeypatch):
seen = {}
monkeypatch.setattr(service.client, "call",
lambda k, o=None, **kw: seen.update(o or {}) or {"x": 1})
service.attendees(conn, "190918189", 30)
assert seen["MaxRecords"] == 30, "must not silently truncate a large class"
def test_attendees_cached_per_class(conn, monkeypatch):
calls = []
monkeypatch.setattr(service.client, "call",
lambda k, o=None, **kw: calls.append(o) or {"x": 1})
service.attendees(conn, "1", 10)
service.attendees(conn, "1", 10)
service.attendees(conn, "2", 10)
assert len(calls) == 2
def test_resolve_unknown_class_returns_none(conn):
assert service.resolve_class(conn, "999") is None