Spaces:
Running on T4
Running on T4
Download backend/tests/integration/test_pitch_reports.py from modventures/baseball-engine: direct link, hf CLI and curl.
- Browser
- Download file 3.27 kB
-
https://huggingface.co/spaces/modventures/baseball-engine/resolve/main/backend/tests/integration/test_pitch_reports.py
- Command line
-
hf download hf://spaces/modventures/baseball-engine/backend/tests/integration/test_pitch_reports.py
-
curl -L -o test_pitch_reports.py https://huggingface.co/spaces/modventures/baseball-engine/resolve/main/backend/tests/integration/test_pitch_reports.py
3.27 kB
| """H1: POST /v1/sessions/{id}/pitch-report (contract pitch-1.0, same report shape as the swing report).""" | |
| import copy | |
| import json | |
| from pathlib import Path | |
| from tests.integration.test_swing_reports import ATHLETE, PAYLOAD, load_session, make_account, make_session | |
| EXAMPLE = Path(__file__).resolve().parents[3] / "AI-coach-agent" / "docs" / "examples" / ( | |
| "pitch_analysis_payload_example.json") | |
| PITCH = json.loads(EXAMPLE.read_text(encoding="utf-8")) | |
| def url(session_id) -> str: | |
| return f"/v1/sessions/{session_id}/pitch-report" | |
| def payload(**changes) -> dict: | |
| p = copy.deepcopy(PITCH) | |
| p.update(changes) | |
| return p | |
| async def test_pitch_report_success_and_saved(client): | |
| owner, headers = await make_account() | |
| sid = await make_session(owner, mode="p") | |
| response = await client.post(url(sid), json=PITCH, headers={**headers, "X-Request-Id": "req_pitch"}) | |
| assert response.status_code == 200, response.text | |
| report = response.json() | |
| assert [g["group"] for g in report["groups"]] == ["leg_lift_balance", "stride_landing", "arm_action", | |
| "release_finish"] | |
| assert report["focus_area"]["metric_id"] == "stride_length_pct_height" | |
| assert report["meta"]["input_schema_version"] == "pitch-1.0" | |
| assert report["meta"]["text_source"] == "rules" and report["meta"]["request_id"] == "req_pitch" | |
| row = await load_session(sid) | |
| assert row.report == report and row.measurement_payload == PITCH | |
| assert (row.analysis_id, row.analysis_revision) == ("pitch-example-001", 1) | |
| assert {row.marks[k] for k in ("peak_knee_height_frame", "foot_plant_frame", "ball_release_frame")} == {27, 45, 49} | |
| async def test_same_revision_returns_stored_pitch_report(client): | |
| owner, headers = await make_account() | |
| sid = await make_session(owner, mode="p") | |
| first = await client.post(url(sid), json=PITCH, headers=headers) | |
| second = await client.post(url(sid), json=PITCH, headers=headers) | |
| assert second.status_code == 200 and second.json() == first.json() | |
| stale = await client.post(url(sid), json=payload(analysis_id="other"), headers=headers) | |
| assert stale.json()["error"]["code"] == "ANALYSIS_MISMATCH" | |
| async def test_hitting_session_rejects_pitch_report_and_pitch_rejects_swing_payload(client): | |
| owner, headers = await make_account() | |
| sid = await make_session(owner, mode="h") | |
| response = await client.post(url(sid), json=PITCH, headers=headers) | |
| assert response.status_code == 422 and response.json()["error"]["code"] == "MODE_NOT_SUPPORTED" | |
| sid = await make_session(owner, mode="p") | |
| response = await client.post(url(sid), json=PAYLOAD, headers=headers) | |
| assert response.status_code == 422 and response.json()["error"]["code"] == "INVALID_PAYLOAD" | |
| async def test_profile_throwing_hand_conflict_is_reported(client): | |
| owner, headers = await make_account() | |
| sid = await make_session(owner, mode="p", athlete={**ATHLETE, "throws": "L"}) | |
| response = await client.post(url(sid), json=PITCH, headers=headers) | |
| assert response.status_code == 200 | |
| report = response.json() | |
| assert "throwing_hand_conflict" in {n["code"] for n in report["data_quality"]} | |
| assert all(m["coach_tip"] is None for m in report["measurements"]) | |