"""Drill context, implausible values and rejected-text reporting in build_report.""" from __future__ import annotations import importlib.util from pathlib import Path import pytest from ai_coach.swing_report import repository as repo from .test_swing_report_report import GOOD, build, by_group, by_metric, fake_generator, llm_settings from .test_swing_report_validation import payload STRIDE = "stride_relative_length" def practice(kind: str, stride: float = 24.0) -> dict: p = payload() p["context"]["practice_type"] = kind p["measurements"][1]["value"] = stride return p def codes(report: dict) -> set[str]: return {n["code"] for n in report["data_quality"]} def test_walk_through_stride_is_not_compared(): r = build(practice("walk_through", stride=97.6)) stride = by_metric(r)[STRIDE] assert stride["rating"] == "not_rated" and stride["not_rated_reason"] == "not_comparable_for_drill" assert stride["score"] is None and stride["coach_tip"] is None assert r["focus_area"]["metric_id"] != STRIDE assert "not_comparable_for_drill" in codes(r) # Only the separation result scores the stride group, so the planned step cannot lower it. assert by_group(r)["stride"]["score"] == by_metric(r)["hip_shoulder_separation_at_foot_down"]["score"] def test_implausible_stride_is_a_measurement_problem_not_a_finding(): r = build(practice("tee", stride=97.6)) stride = by_metric(r)[STRIDE] assert stride["not_rated_reason"] == "implausible_value" and stride["rating_label"] == "Check measurement" assert r["focus_area"]["metric_id"] != STRIDE assert "implausible_value" in codes(r) assert by_metric(build(practice("tee", stride=60.0)))[STRIDE]["score"] is not None # longest reference strides def test_implausible_only_member_explains_the_group(): p = practice("tee", stride=97.6) p["measurements"][0].update(value=None, status="unavailable", reasons=["required_landmarks_unusable"]) assert by_group(build(p))["stride"]["summary"] == repo.text("group_not_rated_by_reason")["implausible_value"] def test_unknown_drill_still_coaches_but_never_the_stride(): r = build(practice("unknown", stride=70.0)) # long but plausible focus = r["focus_area"] assert focus["kind"] == "fix" and focus["metric_id"] == "hip_shoulder_separation_at_foot_down" stride = by_metric(r)[STRIDE] assert stride["score"] is not None and stride["coach_tip"] is None assert "reference range" in stride["result_text"] assert "practice_context_unknown" in codes(r) assert "practice_context_unverified" not in codes(r) def test_unknown_drill_rejects_model_stride_advice_and_reports_why(tmp_path): answer = {**GOOD, "measurements": [ GOOD["measurements"][0], {"metric_id": STRIDE, "result_text": "Your step is a little long, so try a shorter step.", "coach_tip": ""}]} r = build(practice("unknown", stride=50.0), settings=llm_settings(tmp_path), generator=fake_generator(answer)) assert "unsupported_stride_advice" in r["meta"]["text_rejections"][f"result:{STRIDE}"] assert "shorter" not in by_metric(r)[STRIDE]["result_text"] def test_general_focus_drill_is_not_presented_as_a_correction(): r = build(practice("dry_swing")) assert r["focus_area"]["kind"] == "general" assert r["focus_area"]["coach_message"].endswith(repo.text("general_drill_not_a_correction")) def test_plausible_ranges_yaml_matches_generator(): tool = Path(__file__).resolve().parents[1] / "tools" / "build_plausible_ranges.py" spec = importlib.util.spec_from_file_location("build_plausible_ranges", tool) module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) # type: ignore[union-attr] if not all((module.CALIBRATION / path).is_file() for path in module.SOURCES): pytest.skip("OBP per-swing values not present next to this checkout") assert module.OUT.read_text(encoding="utf-8") == module.build()