baseball-engine / AI-coach-agent /tests /test_fallback.py
github-actions[bot]
Automated Deploy: 08c32afb2197fc9ad9b7c3451f02ed400d479a42
3249fe4
Raw History Blame Contribute Delete
6.8 kB
import pytest
from ai_coach.fallback import REASON_CAVEATS, build_fallback_note
from ai_coach.guardrails import check_note
from ai_coach.input_builder import build_coach_input
from ai_coach.knowledge import drills_for
from .helpers import example_names, load_example, make_input, measurements, second_problem
@pytest.mark.parametrize("name", example_names())
def test_fallback_passes_every_guardrail_for_every_example(name, real_inputs):
coach_input = real_inputs[name]
assert check_note(build_fallback_note(coach_input), coach_input) == []
def test_every_capture_reason_caveat_is_clean_wording():
"""The caveat text is also checked by the guardrails (no good/bad, no invented numbers) for every reason."""
for reason, text in REASON_CAVEATS.items():
coach_input = make_input(capture=make_input().capture.model_copy(update={"ok": False, "reasons": [reason]}), problems=[])
note = build_fallback_note(coach_input)
assert text in note.caveats, reason
assert check_note(note, coach_input) == [], reason
def test_fallback_is_deterministic():
coach_input = make_input()
assert build_fallback_note(coach_input) == build_fallback_note(coach_input)
def test_fallback_names_the_baseline_problem_with_evidence_cue_and_a_library_drill():
note = build_fallback_note(make_input())
assert note.work_on[0].fault_key == "over_stride" and note.work_on[0].evidence == ["stride_pct_height"]
assert "75%" in note.work_on[0].text and note.cue
assert note.drill.name == "Short Step Tee" and "3x8" in note.drill.how
assert [i.metric_key for i in note.looks_solid] == ["head_movement_pct_height"]
def test_fallback_adds_the_second_problem_and_a_real_drill_for_real_inputs():
coach_input = make_input(problems=[make_input().problems[0], second_problem()])
note = build_fallback_note(coach_input)
assert [i.fault_key for i in note.work_on] == ["over_stride", "head_drift"]
built = build_coach_input(load_example("pitching_short_stride_soft_knee_240fps"))
top = build_fallback_note(built)
assert top.drill.name in {d["name"] for d in drills_for("pitching", top.work_on[0].fault_key)}
def test_fallback_never_praises_estimates_and_explains_them():
note = build_fallback_note(make_input())
assert all(i.metric_key != "hip_shoulder_separation_foot_plant_deg" for i in note.looks_solid)
assert any("estimate" in c for c in note.caveats)
def test_fallback_for_failed_capture_only_asks_to_record_again():
failed = make_input(
capture=make_input().capture.model_copy(update={"ok": False, "reasons": ["partly_out_of_frame", "view_uncertain"]})
)
note = build_fallback_note(failed)
assert not (note.looks_solid or note.work_on or note.cue or note.drill)
assert "record again" in note.caveats[0].lower() and len(note.caveats) <= 3
def test_unknown_reason_still_asks_to_record_again():
failed = make_input(capture=make_input().capture.model_copy(update={"ok": False, "reasons": []}))
assert "record again" in build_fallback_note(failed).caveats[0].lower()
def test_assumed_level_is_disclosed():
session = measurements(
series={"stride_pct_height": ("%", "high", [47.0] * 4), "head_movement_pct_height": ("%", "high", [2.0] * 4)}
)
session.pop("level")
note = build_fallback_note(build_coach_input(session))
assert any("did not know your level" in c for c in note.caveats)
def test_real_low_confidence_clip_gets_a_cautious_note(real_inputs):
note = build_fallback_note(real_inputs["real_hitting_30fps_low_confidence"])
assert not note.looks_solid and not note.work_on
assert "could not read" in note.headline.lower()
assert load_example("real_hitting_30fps_low_confidence")["capture"]["ok"] is False
def test_record_again_is_never_truncated_away_even_with_many_reasons():
reasons = ["a_bit_dark", "pose_confidence_fair", "view_uncertain", "too_dark"]
failed = make_input(capture=make_input().capture.model_copy(update={"ok": False, "reasons": reasons}))
note = build_fallback_note(failed)
assert "record again" in note.caveats[0].lower() and len(note.caveats) == 3
assert check_note(note, failed) == []
def test_the_important_caveats_survive_truncation():
session = measurements(
series={
"stride_pct_height": ("%", "high", [47.0] * 2),
"hip_shoulder_separation_foot_plant_deg": ("est", "est", [40.0] * 2),
},
capture={"ok": True, "reasons": ["a_bit_dark", "a_bit_blurry", "view_uncertain"]},
)
session["reps"][0]["metrics"][1].update(unit="deg", confidence="est")
note = build_fallback_note(build_coach_input(session))
assert len(note.caveats) == 3 # three capture reasons already fill the cap; nothing else breaks the rules
assert check_note(note, build_coach_input(session)) == []
def test_the_fallback_repeats_arm_care_facts_verbatim_within_the_cap():
arm = {
"known": True,
"pitch_count_today": 40,
"daily_max": 95,
"next_eligible_date": "2026-10-13",
"alerts": [{"message": "Three days in a row: rest tomorrow."}],
}
series = {"stride_pct_height": ("%", "high", [83.0] * 4)}
built = build_coach_input(measurements("pitching", series=series, arm_care=arm))
note = build_fallback_note(built)
assert note.caveats[0] == "You threw 40 of 95 pitches today."
assert "Three days in a row: rest tomorrow." in note.caveats and len(note.caveats) <= 3
assert check_note(note, built) == []
full = build_fallback_note(build_coach_input(measurements("pitching", series=series, arm_care={**arm, "alerts": []})))
assert "You can pitch again on 2026-10-13." in full.caveats
def test_arm_care_text_that_breaks_the_wording_rules_is_skipped_not_repeated():
arm = {"known": True, "pitch_count_today": 40, "daily_max": 95, "alerts": [{"message": "Risk of arm injury and pain."}]}
series = {"stride_pct_height": ("%", "high", [83.0] * 4)}
built = build_coach_input(measurements("pitching", series=series, arm_care=arm))
note = build_fallback_note(built)
assert not any("injury" in c for c in note.caveats) and check_note(note, built) == []
def test_a_metric_cited_as_problem_evidence_is_not_praised_by_the_fallback():
stride = [65.0, 65.0, 49.0, 49.0, 49.0] # the median is in range, but the pattern repeats in 2 of 5 reps (40%)
session = measurements(
series={"stride_pct_height": ("%", "high", stride), "com_drift_pct_height": ("%", "high", [-24.5] * 5)}
)
built = build_coach_input(session)
assert [p.key for p in built.problems] == ["over_stride"]
note = build_fallback_note(built)
assert [i.metric_key for i in note.looks_solid] == ["com_drift_pct_height"]
assert check_note(note, built) == []