Files
wodify-cli/tests/test_store.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.3 KiB
Python

"""SQLite cache behaviour. No network."""
from __future__ import annotations
import datetime as _dt
import pytest
from wodify import store
@pytest.fixture()
def conn(tmp_path, monkeypatch):
monkeypatch.setenv("WODIFY_DB", str(tmp_path / "t.db"))
c = store.connect()
yield c
c.close()
def test_payload_round_trip(conn):
store.put_payload(conn, "k", {"a": 1})
hit = store.get_payload(conn, "k")
assert hit is not None
data, fetched_at = hit
assert data == {"a": 1} and fetched_at
def test_payload_miss_returns_none(conn):
assert store.get_payload(conn, "absent") is None
def test_payload_from_a_different_day_is_a_miss(conn):
"""Freshness is per calendar day: yesterday's copy must not be served."""
store.put_payload(conn, "k", {"a": 1})
yesterday = (_dt.date.today() - _dt.timedelta(days=1)).isoformat()
conn.execute("UPDATE payloads SET fetched_on = ?", (yesterday,))
conn.commit()
assert store.get_payload(conn, "k") is None
def test_payload_overwrite_updates_in_place(conn):
store.put_payload(conn, "k", {"v": 1})
store.put_payload(conn, "k", {"v": 2})
assert store.get_payload(conn, "k")[0] == {"v": 2}
assert conn.execute("SELECT COUNT(*) FROM payloads").fetchone()[0] == 1
ROWS = [
{"class_id": "190917816", "name": "CrossFit: 5:30 PM", "start": "17:30:00",
"end": "18:30:00", "program_id": "133059", "coach": "Pat Doyle",
"booked": 9, "limit": 10, "waitlist": 0},
{"class_id": "190918133", "name": "CrossFit Pump: 6:30 PM", "start": "18:30:00",
"end": "19:30:00", "program_id": "131311", "coach": "Pat Doyle",
"booked": 7, "limit": 10, "waitlist": 0},
]
def test_upsert_and_read_back_by_date(conn):
assert store.upsert_classes(conn, "2026-07-20", ROWS) == 2
rows = store.classes_for_date(conn, "2026-07-20")
assert [r["class_id"] for r in rows] == ["190917816", "190918133"]
assert rows[0]["class_limit"] == 10
def test_get_class_resolves_date_and_program(conn):
"""This is what lets `list workout <id>` work: the id alone carries neither."""
store.upsert_classes(conn, "2026-07-20", ROWS)
klass = store.get_class(conn, "190918133")
assert klass["date"] == "2026-07-20"
assert klass["program_id"] == "131311"
def test_get_unknown_class_returns_none(conn):
assert store.get_class(conn, "does-not-exist") is None
def test_rows_without_class_id_are_skipped(conn):
assert store.upsert_classes(conn, "2026-07-20", [{"name": "no id"}]) == 0
def test_upsert_is_idempotent(conn):
store.upsert_classes(conn, "2026-07-20", ROWS)
store.upsert_classes(conn, "2026-07-20", ROWS)
assert conn.execute("SELECT COUNT(*) FROM classes").fetchone()[0] == 2
def test_classes_for_date_can_ignore_freshness(conn):
"""Past days do not change, so a stale index is still useful for id lookup."""
store.upsert_classes(conn, "2026-07-20", ROWS)
conn.execute("UPDATE classes SET fetched_on = '2020-01-01'")
conn.commit()
assert store.classes_for_date(conn, "2026-07-20") == []
assert len(store.classes_for_date(conn, "2026-07-20", fresh_only=False)) == 2
def test_stats_reports_counts(conn):
store.upsert_classes(conn, "2026-07-20", ROWS)
store.put_payload(conn, "k", {})
st = store.stats(conn)
assert st["classes"] == 2 and st["days"] == 1
assert st["payloads"] == 1 and st["payloads_fresh_today"] == 1