Spaces:
Running on T4
Running on T4
Download tests/test_job_terminal.py from modventures/baseball-engine: direct link, hf CLI and curl.
- Browser
- Download file 6.73 kB
-
https://huggingface.co/spaces/modventures/baseball-engine/resolve/main/tests/test_job_terminal.py
- Command line
-
hf download hf://spaces/modventures/baseball-engine/tests/test_job_terminal.py
-
curl -L -o test_job_terminal.py https://huggingface.co/spaces/modventures/baseball-engine/resolve/main/tests/test_job_terminal.py
6.73 kB
| import sys | |
| from test_api import fake_service, settings | |
| from baseball_batting.job_terminal import JobTerminal | |
| from baseball_batting.storage import atomic_json | |
| def test_progress_is_visible_but_throttled_with_heartbeat(tmp_path, capsys): | |
| now = [0.] | |
| log = JobTerminal('job-123', tmp_path, clock=lambda: now[0]) | |
| atomic_json(tmp_path/'progress.json', {'stage':'pose', 'completed_frames':0, 'total_frames':100}) | |
| log.progress() | |
| for i in range(1, 15): | |
| now[0] = i | |
| atomic_json(tmp_path/'progress.json', {'stage':'pose', 'completed_frames':i, 'total_frames':100}) | |
| log.progress() | |
| assert len(capsys.readouterr().out.splitlines()) == 1 | |
| now[0] = 15 | |
| log.progress() | |
| assert '14/100 (14%)' in capsys.readouterr().out | |
| now[0] = 30 | |
| log.progress() | |
| assert 'elapsed=30.0s' in capsys.readouterr().out | |
| now[0] = 31 | |
| atomic_json(tmp_path/'progress.json', {'stage':'measurements'}) | |
| log.progress() | |
| assert 'measurements' in capsys.readouterr().out | |
| def test_results_are_short_and_do_not_dump_payloads(tmp_path, capsys): | |
| atomic_json(tmp_path/'result.json', {'private':'DO_NOT_LOG', 'coordinates':{ | |
| 'up_estimation':{'status':'estimated', 'up_vector':[0,-1,0], 'up_source':'DO_NOT_LOG'}, | |
| 'pitcher_estimation':{'status':'unknown', 'reason_code':'insufficient_clear_home_plate_frames'}}}) | |
| JobTerminal('id', tmp_path).finish('completed') | |
| out = capsys.readouterr().out | |
| assert 'up=estimated vector=[0.000, -1.000, 0.000]' in out | |
| assert 'pitcher=unknown reason=insufficient_clear_home_plate_frames' in out | |
| assert 'completed elapsed=' in out and 'DO_NOT_LOG' not in out | |
| assert len(out.splitlines()) == 3 | |
| def test_mixed_direction_rejections_show_causes_and_numeric_evidence(tmp_path, capsys): | |
| atomic_json(tmp_path/'result.json', {'coordinates':{'pitcher_estimation':{ | |
| 'status':'unknown', 'method':'automatic_body_swing_direction_v1', | |
| 'reason_code':'no_clear_setup_and_swing', 'accepted_candidate_count':0, | |
| 'limits':{'maximum_setup_spread_deg':12}, 'private':'DO_NOT_LOG', | |
| 'candidates':[ | |
| {'peak_frame':18, 'accepted':False, 'reason_code':'no_stable_loaded_setup'}, | |
| {'peak_frame':40, 'accepted':False, 'reason_code':'setup_axis_unstable', 'setup_spread_deg':18.75}, | |
| {'peak_frame':60, 'accepted':False, 'reason_code':'no_stable_loaded_setup'}, | |
| ]}}}) | |
| JobTerminal('id', tmp_path).finish('completed') | |
| output = capsys.readouterr().out | |
| assert 'reason=no_clear_setup_and_swing' in output | |
| assert 'pitcher_checks candidates=3 accepted=0 rejected=no_stable_loaded_setup:2,setup_axis_unstable:1' in output | |
| assert 'frame=40 reason=setup_axis_unstable setup_spread_deg=18.750 maximum_setup_spread_deg=12.000' in output | |
| assert 'DO_NOT_LOG' not in output | |
| assert len(output.splitlines()) == 4 | |
| def test_direction_diagnostics_are_bounded_and_ignore_raw_strings(): | |
| from baseball_batting.job_terminal import _pitcher_checks | |
| reference = {'status':'unknown', 'limits':{'maximum_setup_spread_deg':'DO_NOT_LOG'}, | |
| 'candidates':[ | |
| {'accepted':False, 'reason_code':'setup_axis_unstable', 'setup_spread_deg':16, 'peak_frame':10}, | |
| {'accepted':False, 'reason_code':'hand_motion_opposes_loaded_side', 'forward_velocity_fraction':-.3, 'peak_frame':20}, | |
| {'accepted':False, 'reason_code':'hip_turn_does_not_support_swing', 'hip_forward_turn_deg':5, 'peak_frame':30}, | |
| {'accepted':False, 'reason_code':'shoulder_turn_does_not_support_swing', 'shoulder_forward_turn_deg':'DO_NOT_LOG', 'peak_frame':40}, | |
| {'accepted':False, 'reason_code':'DO_NOT_LOG', 'peak_frame':50}, | |
| {'accepted':False, 'reason_code':['DO_NOT_LOG']}, None, | |
| ]*100} | |
| lines = _pitcher_checks(reference) | |
| assert len(lines) <= 3 and all(len(line) < 500 for line in lines) | |
| assert 'DO_NOT_LOG' not in '\n'.join(lines) | |
| assert 'other:' in lines[0] | |
| assert _pitcher_checks({**reference, 'status':'estimated'}) == [] | |
| assert _pitcher_checks({'status':'unknown', 'candidates':None}) == [] | |
| def test_missing_and_partial_files_do_not_break_logging(tmp_path, capsys): | |
| log = JobTerminal('id', tmp_path) | |
| log.progress(force=True) | |
| (tmp_path/'progress.json').write_text('{') | |
| log.progress(force=True) | |
| log.finish('cancelled') | |
| assert 'cancelled' in capsys.readouterr().out | |
| def test_accepted_direction_logs_the_selected_setup_and_swing_frames(): | |
| from baseball_batting.job_terminal import _pitcher_checks | |
| row = {'accepted':True, 'setup_start_frame':61, 'setup_end_frame':65, 'peak_frame':72, | |
| 'extension_start_frame':73, 'extension_end_frame':75, 'setup_spread_deg':4.125, | |
| 'private':'DO_NOT_LOG'} | |
| reference = {'status':'estimated', 'candidates':[None, {}, row]} | |
| assert _pitcher_checks(reference) == [ | |
| 'pitcher_evidence frame=72 setup=61-65 extension=73-75 setup_spread_deg=4.125'] | |
| row['setup_end_frame'] = 80 | |
| assert _pitcher_checks(reference) == [] | |
| def test_motion_result_reports_frame_counts(tmp_path, capsys): | |
| atomic_json(tmp_path/'result.json', {'sampling':{'frame_count':120}, 'quality':{'usable_frame_count':112}}) | |
| JobTerminal('id', tmp_path).finish('completed') | |
| assert 'result frames=120 usable=112' in capsys.readouterr().out | |
| def test_queue_relays_subprocess_progress_to_server_stdout(tmp_path, capsys): | |
| service = fake_service(settings(tmp_path), delay=.7) | |
| identifier = service.store.create(8) | |
| directory = service.job_directory(identifier) | |
| directory.mkdir() | |
| service.store.update(identifier, 'queued') | |
| assert service.store.claim() == identifier | |
| service.execute(identifier) | |
| out = capsys.readouterr().out | |
| assert f'[analysis {identifier}] started' in out | |
| assert 'pose_2d 1/2 (50%)' in out | |
| assert 'completed elapsed=' in out | |
| assert service.store.get(identifier)['status'] == 'completed' | |
| def test_failure_includes_public_reason_without_traceback(tmp_path, capsys): | |
| service = fake_service(settings(tmp_path)) | |
| identifier = service.store.create(8) | |
| directory = service.job_directory(identifier) | |
| directory.mkdir() | |
| atomic_json(directory/'error.json', {'code':'inference_failed', 'reason_code':'no_usable_motion', 'private':'DO_NOT_LOG'}) | |
| service.command_factory = lambda _: [sys.executable, '-c', 'raise SystemExit(1)'] | |
| service.store.update(identifier, 'queued') | |
| service.store.claim() | |
| service.execute(identifier) | |
| out = capsys.readouterr().out | |
| assert 'failure_reason=no_usable_motion' in out | |
| assert 'failed elapsed=' in out and 'DO_NOT_LOG' not in out | |