"""The app-facing response (v2.0): built from the note, the score and the timing section.""" from ai_coach.input_builder import build_coach_input from ai_coach.response import CoachResponse from ai_coach.service import CoachService from .helpers import FakeGenerator, example_note, load_example HITTING = "hitting_stride_and_lunging_240fps" PITCHING = "pitching_short_stride_240fps" def service(settings, name, llm=True): s = settings if llm else settings.model_copy(update={"google_api_key": None}) return CoachService(s, generator=FakeGenerator([example_note(name)]) if llm else None) def test_the_response_carries_every_screen_part(keyed_settings): r = service(keyed_settings, HITTING).respond(load_example(HITTING)) assert isinstance(r, CoachResponse) and r.response_version == "2.0" assert r.summary and r.strengths is not None and r.focus and r.drill and r.score.value is not None assert r.drill.cue # the priority fix's cue travels with the drill assert r.focus[0].problem_key == "lunging" and r.focus[0].evidence assert r.meta.source == "llm" and r.meta.model and r.discipline == "hitting" def test_the_json_has_no_internal_fields(keyed_settings): data = service(keyed_settings, HITTING).respond(load_example(HITTING)).model_dump() assert set(data) == { "response_version", "request_id", "session_id", "discipline", "level", "level_assumed", "score", "summary", "strengths", "focus", "drill", "timing", "data_quality", "arm_care", "meta", } assert "usage" not in data["meta"] and "violations" not in data["meta"] def test_pitching_arm_care_comes_from_the_input_not_the_models_wording(settings): session = load_example(PITCHING) session["arm_care"] = { "known": True, "pitch_count_today": 42, "daily_max": 95, "next_eligible_date": "2026-10-13", "alerts": [], } r = service(settings, PITCHING, llm=False).respond(session) assert r.arm_care.pitch_count_today == 42 and r.arm_care.daily_max == 95 assert "You threw 42 of 95 pitches today." in r.arm_care.lines assert not any("42 of 95" in line for line in r.data_quality) # said once, in arm_care def test_hitting_has_no_arm_care(settings): assert service(settings, HITTING, llm=False).respond(load_example(HITTING)).arm_care is None def test_no_score_and_a_record_again_note_when_the_video_failed(settings): session = load_example(HITTING) session["capture"] = {"ok": False, "reasons": ["too_dark"]} r = service(settings, HITTING, llm=False).respond(session) assert r.score.value is None and r.score.reason_code == "capture_not_ok" assert r.focus == [] and r.drill is None and any("record again" in c.lower() for c in r.data_quality) def test_the_note_never_sees_the_score_or_the_timing(keyed_settings): gen = FakeGenerator([example_note(HITTING)]) session = load_example(HITTING) session["timing"] = {"schema_version": "3.1", "capture": {"capture_fps": 240, "time_source": "original_pts"}, "reps": []} CoachService(keyed_settings, generator=gen).respond(session) sent = gen.calls[0]["session_json"] assert "timing" not in sent and "score" not in sent def test_respond_and_coach_agree_on_the_note(keyed_settings): session = load_example(HITTING) svc = service(keyed_settings, HITTING) response, result = svc.respond(session), svc.coach(session) assert result.cache_hit and result.note.headline == response.summary def test_pitching_timing_is_ignored_not_an_error(settings): session = load_example(PITCHING) session["timing"] = {"schema_version": "3.1", "reps": []} assert service(settings, PITCHING, llm=False).respond(session).timing is None def test_the_score_matches_the_input_the_model_saw(keyed_settings): from ai_coach.scoring import compute_score session = load_example(HITTING) assert service(keyed_settings, HITTING).respond(session).score == compute_score(build_coach_input(session)) def test_a_standalone_timing_payload_gets_a_timing_only_response(settings): """The engine's /load-timing output can be posted as it is: timing section only, no note, no score, no model call.""" import json from pathlib import Path payload = json.loads((Path(__file__).parent / "timing_30_example.json").read_text()) r = CoachService(settings).respond(payload) assert r.timing and [o.value_ms for o in r.timing.observations] == [530, 270] # time_basis "capture" is real time assert r.score.value is None and r.focus == [] and r.drill is None assert r.summary == r.timing.summary and r.meta.fallback_reason == "timing_only" and r.meta.source == "rules" assert any("not slow motion" in c for c in r.data_quality) # the capture reason still reaches the player def test_playback_time_basis_withholds_the_numbers(settings): import json from pathlib import Path payload = json.loads((Path(__file__).parent / "timing_30_example.json").read_text()) payload["time_basis"] = "playback" r = CoachService(settings).respond(payload) assert r.timing.observations == [] and "unknown" in r.timing.data_quality[0]