File size: 5,617 Bytes
35e80ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
"""The agent's own reference data is complete, consistent and safe to rely on."""

import pytest

from ai_coach import knowledge as kb
from ai_coach.text_metrics import flesch_kincaid_grade

HITTING_PROBLEMS = {
    "over_stride",
    "rushing",
    "late_launch",
    "head_drift",
    "lunging",
    "soft_front_side",
    "early_shoulder_open",
}
PITCHING_PROBLEMS = {
    "short_stride",
    "soft_front_leg",
    "upright_finish",
    "excess_lateral_lean",
    "slot_inconsistency",
    "rushing",
    "early_trunk_rotation",
}
HITTING_METRICS = {
    "stride_pct_height",
    "foot_plant_to_contact_ms",
    "load_tempo_s",
    "load_launch_ratio",
    "head_movement_pct_height",
    "lead_knee_angle_contact_deg",
    "lateral_trunk_tilt_contact_deg",
    "hip_shoulder_separation_foot_plant_deg",
    "com_drift_pct_height",
}
PITCHING_METRICS = {
    "stride_pct_height",
    "lead_knee_flexion_fp_deg",
    "lead_knee_flexion_release_deg",
    "lead_knee_block_deg",
    "trunk_forward_tilt_release_deg",
    "trunk_lateral_tilt_release_deg",
    "arm_slot_deg",
    "leg_lift_to_fp_ms",
    "fp_to_release_ms",
    "head_stability_pct_height",
    "hip_shoulder_sep_fp",
}
EXPECTED = {"hitting": (HITTING_PROBLEMS, HITTING_METRICS), "pitching": (PITCHING_PROBLEMS, PITCHING_METRICS)}


@pytest.mark.parametrize("discipline", ["hitting", "pitching"])
def test_problem_and_metric_libraries_match_the_documented_lists(discipline):
    problems, metrics = EXPECTED[discipline]
    assert set(kb.problem_keys(discipline)) == problems
    assert set(kb.metric_keys(discipline)) == metrics


@pytest.mark.parametrize("discipline", ["hitting", "pitching"])
def test_every_problem_rule_points_at_a_real_metric_and_a_valid_shape(discipline):
    valid_metrics = EXPECTED[discipline][1]
    for key, spec in kb.problems(discipline).items():
        assert spec["tier"] in kb.tier_weights() and spec["rules"], key
        for rule in spec["rules"]:
            assert rule["metric"] in valid_metrics, (key, rule)
            assert rule["type"] in {"band", "spread", "never"}
            if rule["type"] == "band":
                assert rule["direction"] in {"above", "below"}
            if rule["type"] == "never":
                assert rule["reason"]


@pytest.mark.parametrize("discipline", ["hitting", "pitching"])
def test_problem_copy_is_complete_and_easy_to_read(discipline):
    for key, spec in kb.problems(discipline).items():
        assert all(spec[f] for f in ("label", "what", "why", "cue")), key
        assert flesch_kincaid_grade(f"{spec['what']} {spec['why']} {spec['cue']}") <= 9.0, key


@pytest.mark.parametrize("discipline", ["hitting", "pitching"])
def test_every_judgeable_problem_has_at_least_one_drill(discipline):
    for key, spec in kb.problems(discipline).items():
        if all(r["type"] == "never" for r in spec["rules"]):
            continue
        assert kb.drills_for(discipline, key), f"{discipline}/{key} has no drill"


def test_drill_library_is_consistent():
    drills = kb.all_drills()
    assert len(drills) == 30 and len({d["id"] for d in drills}) == 30
    names = [d["name"] for d in drills]
    assert len(set(names)) == len(names), "drill names must be unique so the guardrail can match them"
    for d in drills:
        assert d["discipline"] in {"hitting", "pitching"} and d["rep_scheme"] and d["cue"] and d["faults"]
        assert set(d["faults"]) <= set(kb.problem_keys(d["discipline"])), d["id"]


@pytest.mark.parametrize("discipline", ["hitting", "pitching"])
def test_ranges_are_well_formed(discipline):
    for key in EXPECTED[discipline][1]:
        for level in kb.LEVELS:
            rng = kb.metric_range(discipline, key, level)
            if rng is None:
                continue
            assert rng.low < rng.high and rng.status in {"calibrated", "confirmed", "adjusted", "unverified"}


def test_known_ranges_and_flags():
    stride = kb.metric_range("hitting", "stride_pct_height", "hs_varsity")
    assert (stride.low, stride.high, stride.status, stride.placeholder) == (39.9, 52.2, "calibrated", False)
    assert kb.metric_range("hitting", "load_tempo_s", "hs_varsity") is None  # org-set by design
    assert kb.metric_range("pitching", "arm_slot_deg", "hs_varsity").comparable is False
    college = kb.metric_range("pitching", "trunk_forward_tilt_release_deg", "college")
    assert (college.low, college.high, college.status) == (14.8, 46.4, "calibrated")
    assert kb.metric_range("hitting", "stride_pct_height", "hs_varsity").confidence == "low"


def test_overrides_win_and_are_marked():
    rng = kb.metric_range("hitting", "load_tempo_s", "hs_varsity", {"load_tempo_s": (0.4, 0.7)})
    assert (rng.low, rng.high, rng.source, rng.status) == (0.4, 0.7, "override", "confirmed")


def test_every_metric_has_aliases_and_estimate_only_metrics_have_no_praise_line():
    metrics = kb._load("metrics.yaml")
    for discipline, keys in ((d, EXPECTED[d][1]) for d in EXPECTED):
        for key in keys:
            assert metrics[discipline][key]["aliases"], key
    assert kb.metric_info("hitting", "hip_shoulder_separation_foot_plant_deg")["solid"] is None
    assert kb.metric_info("pitching", "hip_shoulder_sep_fp")["solid"] is None
    assert kb.metric_info("hitting", "unknown_metric")["label"] == "Unknown metric"


def test_thresholds_are_sane():
    th = kb.thresholds()
    assert 0 < th["rep_tolerance_band_fraction"] < 0.5 and 0 < th["min_frequency"] < 1 and 0 < th["min_evaluated_share"] <= 1
    assert kb.tier_weights()["lower"] > kb.tier_weights()["upper"]
    assert set(kb.confidence_weights()) == {"high", "medium"}