#!/usr/bin/env python3 """Unit tests for the RE-1 log parsers and the two-run comparison.""" from __future__ import annotations import copy import json import sys import unittest from pathlib import Path HERE = Path(__file__).resolve().parent sys.path.insert(0, str(HERE.parent / "harness")) import analyze # noqa: E402 import run_pipeline # noqa: E402 FIXTURES = HERE.parent / "fixtures" class ParseTests(unittest.TestCase): def test_parse_ppl(self) -> None: text = (FIXTURES / "ppl-sample.log").read_text() parsed = run_pipeline.parse_ppl(text) self.assertEqual(parsed["ppl"], 15.2105) self.assertEqual(parsed["ppl_err"], 0.98064) self.assertIsNotNone(parsed["load_time_ms"]) def test_parse_ppl_missing_fails_closed(self) -> None: with self.assertRaises(ValueError): run_pipeline.parse_ppl("no numbers here\n") def test_parse_hellaswag_takes_last_row(self) -> None: text = (FIXTURES / "hellaswag-sample.log").read_text() parsed = run_pipeline.parse_hellaswag(text) self.assertEqual(parsed["hellaswag_tasks_scored"], 8) self.assertEqual(parsed["hellaswag_pct"], 37.5) self.assertAlmostEqual(parsed["hellaswag_lo"], 13.6844) self.assertAlmostEqual(parsed["hellaswag_hi"], 69.4258) def test_parse_hellaswag_missing_fails_closed(self) -> None: with self.assertRaises(ValueError): run_pipeline.parse_hellaswag("no table\n") def test_parse_bench_combined_log(self) -> None: text = (FIXTURES / "bench-sample.log").read_text() parsed = run_pipeline.parse_bench(text) self.assertAlmostEqual(parsed["pp512_ts"], 102.089135) self.assertAlmostEqual(parsed["tg128_ts"], 11.720742) def fake_run(run_id: str, scale: float = 1.0) -> dict: """Minimal run dict with the shape flatten() consumes.""" def arm(ppl: float, hs: float, rss: int, load: float, pp: float, tg: float) -> dict: return { "ppl": ppl, "ppl_err": 0.01, "hellaswag_pct": hs, "hellaswag_lo": hs - 5.0, "hellaswag_hi": hs + 5.0, "hellaswag_tasks_scored": 200, "ppl_peak_rss_kib": rss, "load_time_ms": load, "pp512_ts": pp, "tg128_ts": tg, } models = {} for key, ppl, hs, rss, load, pp, tg in ( ("model-a", 15.0, 30.0, 1_500_000, 500.0, 100.0, 10.0), ("model-b", 25.0, 40.0, 900_000, 300.0, 200.0, 20.0), ): models[key] = { "model": key, "f16": {"bytes": 1_000_000_000}, "q4_k_m": {"bytes": 400_000_000}, "arms": { "f16": arm(ppl, hs, rss, load, pp, tg), "q4_k_m": arm(ppl * scale, hs + 1.0, int(rss * 0.9), load * 0.9, pp * 1.1, tg * 1.2), }, "bench": { "pp512_ts_f16": pp, "pp512_ts_q4": pp * 1.1, "tg128_ts_f16": tg, "tg128_ts_q4": tg * 1.2, "pp_ratio_median": 1.1, "tg_ratio_median": 1.2, }, } return {"run_id": run_id, "started_at": "2026-09-17T00:00:00Z", "models": models} class AnalyzeTests(unittest.TestCase): def test_identical_runs_pass(self) -> None: run1 = fake_run("r1") result = analyze.compare(run1, copy.deepcopy(run1)) self.assertEqual(result["decision"], "capability_proven") self.assertEqual(result["failing"], []) self.assertEqual(result["missing"], []) def test_delta_tolerance_is_scaled_by_reference_arm(self) -> None: run1 = fake_run("r1") run2 = fake_run("r2", scale=1.03) result = analyze.compare(run1, run2) # ppl_delta for model-a is 0.45 in both runs (15.0 * 1.03 - 15.0 = 0.45); # its denominator is the f16 baseline 15.0, so the 3% arm shift moves the # delta by exactly 0.45, which is 3% of baseline -> must fail. self.assertEqual(result["quantities"]["model-a.ppl_delta"]["denominator"], 15.0) self.assertFalse(result["quantities"]["model-a.ppl_delta"]["pass"]) self.assertEqual(result["decision"], "capability_gap") def test_missing_quantity_fails_closed(self) -> None: run1 = fake_run("r1") run2 = fake_run("r2") del run2["models"]["model-b"] result = analyze.compare(run1, run2) self.assertEqual(result["decision"], "capability_gap") self.assertTrue(any(k.startswith("model-b.") for k in result["missing"])) def test_markdown_renders(self) -> None: run1 = fake_run("r1") md = analyze.to_markdown(analyze.compare(run1, copy.deepcopy(run1))) self.assertIn("capability_proven", md) self.assertIn("model-a.ppl_delta", md) if __name__ == "__main__": unittest.main()