File size: 3,274 Bytes
6963661
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"""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"])