"""Offline regression tests for the release's Math-Shepherd evaluator.""" import contextlib import hashlib import io import json from pathlib import Path import sys import tempfile import unittest from unittest import mock import pyarrow as pa import pyarrow.parquet as pq import run_math_shepherd as evaluator class MathShepherdEvaluatorTests(unittest.TestCase): def test_packaged_benchmark_has_614_rows_and_expected_hash(self): path = Path(evaluator.__file__).with_name("math_shepherd_614_test_20260327.parquet") self.assertEqual(pq.ParquetFile(path).metadata.num_rows, 614) self.assertEqual( hashlib.sha256(path.read_bytes()).hexdigest(), "e01fabbc80b99f01307aa9095b9e186f13cae392ed0bff983dcbdd61ee0d1087", ) def test_parquet_selection_includes_release_filename(self): with tempfile.TemporaryDirectory() as directory: path = Path(directory) / "math_shepherd_614_test_20260327.parquet" path.touch() self.assertEqual(evaluator.resolve_test_parquets(directory), [str(path)]) def test_gold_span_and_label_dependent_suffix(self): steps = ["Correct", "Wrong", "Correct again"] response, labels = evaluator.completions_to_response_and_gold( steps, [True, False, True] ) self.assertEqual(response, "Correct\nWrong\nCorrect again") self.assertEqual(labels, [{"start": 8, "end": 14}]) tp, pred, gold, *_ = evaluator.cal_metrics_math_shepherd( response, labels, ["Wrong"], steps ) self.assertEqual((tp, pred, gold), (6, len(response) - 8, 6)) def test_metric_only_accepts_old_jsonl_without_completions(self): with tempfile.TemporaryDirectory() as directory: path = Path(directory) / "saved.jsonl" path.write_text(json.dumps({ "response": "A\nB", "labels": [{"start": 2, "end": 3}], "output": ["B"], "task_type": "Math", }) + "\n", encoding="utf-8") with mock.patch.object(sys, "argv", ["run_math_shepherd.py", "--filepath", str(path)]): evaluator.main() metrics = json.loads((Path(directory) / "saved_recomputed_metric.json").read_text(encoding="utf-8")) self.assertEqual(metrics["test_data_size"], 1) self.assertEqual(metrics["metrics"][1]["span_level"]["f1"], 1.0) def test_full_mock_run_and_incomplete_run_detection(self): with tempfile.TemporaryDirectory() as directory: data_file = Path(directory) / "math_shepherd_614_test_20260327.parquet" pq.write_table(pa.Table.from_pylist([ {"prompt": "Find x", "completions": ["A", "B"], "labels": [True, False]}, {"prompt": "Find y", "completions": ["C"], "labels": [True]}, ]), data_file) output_dir = Path(directory) / "results" argv = [ "run_math_shepherd.py", "--data-file", str(data_file), "--model-name", "demo/model", "--model-url", "http://localhost:7010/v1", "--num-threads", "1", "--output-dir", str(output_dir), ] fake_response = lambda prompt, **kwargs: ( '{"hallucination list": ["B"]}' if "Find x" in prompt else '{"hallucination list": []}' ) with mock.patch.object(sys, "argv", argv), mock.patch.object( evaluator, "req_model_vllm", side_effect=fake_response ): evaluator.main() result_files = list(output_dir.glob("*.jsonl")) self.assertEqual(len(result_files), 1) self.assertEqual(len(result_files[0].read_text(encoding="utf-8").splitlines()), 2) metric_files = list(output_dir.glob("*_metric.json")) self.assertEqual(len(metric_files), 1) metrics = json.loads(metric_files[0].read_text(encoding="utf-8")) self.assertEqual(metrics["test_data_size"], 2) self.assertEqual(metrics["model_name"], "demo/model") self.assertEqual(metrics["metrics"][1]["span_level"]["f1"], 1.0) with mock.patch.object(sys, "argv", argv), mock.patch.object( evaluator, "req_model_vllm", return_value="malformed response" ), contextlib.redirect_stdout(io.StringIO()), contextlib.redirect_stderr(io.StringIO()): with self.assertRaisesRegex(RuntimeError, "Only 0/2 rows evaluated"): evaluator.main() self.assertEqual(len(list(output_dir.glob("*.jsonl"))), 1) def test_no_reasoning_changes_instruction(self): steps = ["Step 1: 2+2=5"] ordinary = evaluator.build_user_instruction("Compute 2+2", steps, True, False) compact = evaluator.build_user_instruction("Compute 2+2", steps, False, False) self.assertIn("step-by-step verification", ordinary) self.assertNotIn("step-by-step verification", compact) self.assertIn('"hallucination list"', compact) if __name__ == "__main__": unittest.main()