"""Failure-oriented checks for full-delivery evidence and source clocks.""" import numpy as np import pytest from pydantic import ValidationError from test_pitching_api import options, payload from test_sam_batting import pose from baseball_batting.api_contract import TimingInput from baseball_batting.ingest import ingest from baseball_batting.pitching_coach import build_pitch_payload from baseball_batting.pitching_sequence import sequencing from baseball_batting.sam_motion import MHR def test_known_slow_motion_and_variable_frame_rate_keep_physical_clock(video): capture = ingest(video, {"timing": {"kind": "constant_slowdown", "source": "fixture", "factor": 4, "all_captured_frames_retained": True}}) times = np.array([f["physical_s"] for f in capture["frames"]]) playback = np.array([f["playback_s"] for f in capture["frames"]]) np.testing.assert_allclose(times, playback / 4) assert not np.allclose(np.diff(times), np.diff(times)[0]) mapping = [0, .005, .01, .02, .04, .055, .07, .1] explicit = ingest(video, {"timing": {"kind": "explicit", "source": "known speed ramp", "physical_times_s": mapping}}) assert [f["physical_s"] for f in explicit["frames"]] == mapping @pytest.mark.parametrize("patch", [ {"kind": "unknown", "factor": 4}, {"kind": "unretimed", "source": "claimed", "physical_times_s": [0, 1]}, {"kind": "explicit", "source": "bad", "physical_times_s": [False, True]}, {"kind": "constant_slowdown", "source": "bad", "factor": True, "all_captured_frames_retained": True}, ]) def test_contradictory_or_boolean_timing_rejected(patch): with pytest.raises(ValidationError): TimingInput.model_validate(patch) def test_missing_clock_never_supplies_duration_velocity_sequence(): evidence = payload(clock=False)["delivery_evidence"] assert evidence["clock"]["median_capture_fps"] is None assert all(p["duration_ms"] is None for p in evidence["phases"]) assert evidence["sequencing"]["status"] == "unavailable" assert evidence["consistency"] == "not_assessable_single_delivery" def test_missing_landmark_does_not_disappear_from_curve_or_phase(): points = pose(8) points[4, MHR["left_knee"]] = np.nan evidence = payload(points=points)["delivery_evidence"] knee = next(c for c in evidence["curves"] if c["curve_id"] == "front_knee_flexion") assert knee["samples"][4]["value"] is None and knee["usable_frame_count"] < knee["total_frame_count"] phase = next(p for p in evidence["phases"] if p["phase"] == "plant_to_release") motion = next(m for m in phase["motion"] if m["curve_id"] == "front_knee_flexion") assert motion["change"] is None and "phase_tracking_incomplete" in motion["reasons"] def test_interpolated_event_is_withheld_not_scored(): points = pose(8) corrected = {"points_relative": points, "valid": np.isfinite(points).all(axis=-1), "boundary": np.zeros((8, 70), bool), "interpolated": np.zeros((8, 70), bool)} corrected["interpolated"][5, MHR["right_wrist"]] = True p = build_pitch_payload(options(), {"analysis_id": "quality", "analysis_revision": 1}, corrected, np.arange(8) / 30, np.arange(8) / 30) elbow = next(m for m in p.measurements if m.metric_id == "throwing_elbow_flexion_at_release") assert elbow.value is None and "temporal_inconsistency" in elbow.reasons def test_sequence_order_requires_resolved_peaks_and_preserves_overlap(): times = np.arange(101) / 120 hip = np.cumsum(np.exp(-((np.arange(101) - 35) / 8) ** 2)) shoulders = np.cumsum(np.exp(-((np.arange(101) - 60) / 8) ** 2)) result = sequencing(hip, shoulders, times, 0, 100) assert result["order"] == "pelvis_then_shoulders" and result["status"] == "provisional" assert sequencing(hip, hip, times, 0, 100)["order"] == "unresolved" hip[40] = np.nan assert sequencing(hip, shoulders, times, 0, 100)["status"] == "unavailable" def test_phase_direction_is_withheld_when_adjacent_marks_reverse_change(monkeypatch): from baseball_batting.pitching_geometry import curves def ambiguous(*args, **kwargs): result = curves(*args, **kwargs) # The marked plant-to-release change is positive, but moving either # reviewed event one frame can reverse it, despite modest angle variation. result["torso"] = np.array([10, 10, 9, 10, 11, 12, 8, 10], float) return result monkeypatch.setattr("baseball_batting.pitching_sequence.curves", ambiguous) phase = next(p for p in payload()["delivery_evidence"]["phases"] if p["phase"] == "plant_to_release") torso = next(m for m in phase["motion"] if m["curve_id"] == "torso_inclination") assert torso["start_value"] == 10 and torso["end_value"] == 12 assert torso["change"] is None assert "phase_direction_unresolved" in torso["reasons"] def test_retired_visual_review_never_reads_media_or_calls_provider(): from baseball_batting.pitching_visual import review_pitch_video def forbidden(*args, **kwargs): pytest.fail("Pitching must not send video or images to a generative model") # An invalid path is intentional: even legacy callers must not open media. result = review_pitch_video("/does/not/exist.mp4", None, None, None, generator=forbidden, settings=object()) assert result.status == "unavailable" assert result.reason == "disabled_ml_measurements_only" assert result.observations == [] and result.sampled_frames == []