Files
torbenandClaude Sonnet 5.5 74574b948c
CI / checks (pull_request) Successful in 1m5s
Add baseline report formats and --baseline/--format CLI switches
Text and Markdown comparison reports (regressions first, IDs escaped);
exit code still comes from the current arm only, errors give exit 2.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-12-04 23:16:48 +01:00

168 lines
5.4 KiB
Python

import subprocess
from pathlib import Path
import pytest
from chemenu.evals.cli import main
EVAL_YAML = """\
name: "demo"
threshold: 0.8
output_model: "chemenu.evals.example:ExampleOutput"
"""
CASE_YAML = """\
input:
text: "Hallo"
expect:
- grader: "equals"
path: "/language"
value: "de"
- grader: "contains"
path: "/entities"
items: ["a"]
"""
GOOD = '{"language": "de", "entities": ["a"]}'
BAD = '{"language": "en", "entities": []}'
def make_recorded(root: Path, data: dict[str, str]) -> Path:
(root / "cases").mkdir(parents=True)
(root / "eval.yaml").write_text(EVAL_YAML, encoding="utf-8")
for stem in ("a", "b"):
(root / "cases" / f"{stem}.yaml").write_text(CASE_YAML, encoding="utf-8")
(root / "recorded").mkdir()
for stem, text in data.items():
(root / "recorded" / f"{stem}.json").write_text(text, encoding="utf-8")
return root
def test_exit_0_when_all_pass(tmp_path: Path, capsys: pytest.CaptureFixture[str]) -> None:
make_recorded(tmp_path, {"a": GOOD, "b": GOOD})
assert main([str(tmp_path)]) == 0
out = capsys.readouterr().out
assert "PASS a 1.00" in out
assert "demo: 2/2 passed, mean 1.00, threshold 0.8" in out
def test_exit_1_below_threshold(tmp_path: Path, capsys: pytest.CaptureFixture[str]) -> None:
make_recorded(tmp_path, {"a": GOOD, "b": BAD})
assert main([str(tmp_path)]) == 1
assert "FAIL b 0.00" in capsys.readouterr().out
def test_exit_1_on_missing_response(tmp_path: Path, capsys: pytest.CaptureFixture[str]) -> None:
make_recorded(tmp_path, {"a": GOOD})
assert main([str(tmp_path)]) == 1
assert "FAIL b 0.00 missing response" in capsys.readouterr().out
def test_exit_2_on_load_error(tmp_path: Path, capsys: pytest.CaptureFixture[str]) -> None:
assert main([str(tmp_path)]) == 2
captured = capsys.readouterr()
assert captured.err.startswith("chemenu-eval: ")
assert "eval.yaml" in captured.err
assert captured.out == ""
def test_exit_1_on_unreadable_response(tmp_path: Path, capsys: pytest.CaptureFixture[str]) -> None:
make_recorded(tmp_path, {"a": GOOD})
(tmp_path / "recorded" / "b.json").write_bytes(b"\xff\xfe\x00{")
assert main([str(tmp_path)]) == 1
assert "FAIL b 0.00 cannot read response" in capsys.readouterr().out
@pytest.mark.parametrize("raw", ["{bad", '{"language": "de"}'])
def test_exit_1_on_invalid_response(
tmp_path: Path, capsys: pytest.CaptureFixture[str], raw: str
) -> None:
make_recorded(tmp_path, {"a": GOOD, "b": raw})
assert main([str(tmp_path)]) == 1
assert "FAIL b 0.00 invalid response" in capsys.readouterr().out
def test_exit_2_on_unknown_output_model(tmp_path: Path, capsys: pytest.CaptureFixture[str]) -> None:
make_recorded(tmp_path, {})
(tmp_path / "eval.yaml").write_text(
EVAL_YAML.replace("ExampleOutput", "Missing"), encoding="utf-8"
)
assert main([str(tmp_path)]) == 2
assert "output_model" in capsys.readouterr().err
def test_exit_2_on_empty_set(tmp_path: Path, capsys: pytest.CaptureFixture[str]) -> None:
make_recorded(tmp_path, {})
for case in (tmp_path / "cases").iterdir():
case.unlink()
assert main([str(tmp_path)]) == 2
assert "no cases found" in capsys.readouterr().err
GIT = [
"git",
"-c",
"user.name=t",
"-c",
"user.email=t@example.invalid",
"-c",
"commit.gpgsign=false",
]
def git(repo: Path, *args: str) -> None:
subprocess.run([*GIT, "-C", str(repo), *args], capture_output=True, check=True) # noqa: S603
def commit_all(repo: Path) -> None:
git(repo, "add", "-A")
git(repo, "commit", "-q", "-m", "x")
def record(set_dir: Path, a: str, b: str) -> None:
(set_dir / "recorded" / "a.json").write_text(a, encoding="utf-8")
(set_dir / "recorded" / "b.json").write_text(b, encoding="utf-8")
def test_baseline_exit_code_follows_current_arm(
tmp_path: Path, capsys: pytest.CaptureFixture[str]
) -> None:
git(tmp_path, "init", "-q")
set_dir = make_recorded(tmp_path / "demo", {"a": GOOD, "b": GOOD})
commit_all(tmp_path)
record(set_dir, GOOD, BAD)
assert main([str(set_dir), "--baseline", "HEAD"]) == 1
out = capsys.readouterr().out
assert "demo: 1/2 passed" in out
assert "REGRESSION b" in out
commit_all(tmp_path)
record(set_dir, GOOD, GOOD)
assert main([str(set_dir), "--baseline", "HEAD"]) == 0
assert "FIXED b" in capsys.readouterr().out
def test_baseline_unknown_ref_exit_2(tmp_path: Path, capsys: pytest.CaptureFixture[str]) -> None:
git(tmp_path, "init", "-q")
set_dir = make_recorded(tmp_path / "demo", {"a": GOOD, "b": GOOD})
commit_all(tmp_path)
assert main([str(set_dir), "--baseline", "no-such-ref"]) == 2
captured = capsys.readouterr()
assert captured.err.startswith("chemenu-eval: ")
assert captured.out == ""
def test_markdown_requires_baseline(tmp_path: Path) -> None:
make_recorded(tmp_path, {"a": GOOD, "b": GOOD})
with pytest.raises(SystemExit) as excinfo:
main([str(tmp_path), "--format", "markdown"])
assert excinfo.value.code == 2
def test_baseline_new_set_exit_0(tmp_path: Path, capsys: pytest.CaptureFixture[str]) -> None:
git(tmp_path, "init", "-q")
(tmp_path / "other.txt").write_text("x")
commit_all(tmp_path)
set_dir = make_recorded(tmp_path / "demo", {"a": GOOD, "b": GOOD})
assert main([str(set_dir), "--baseline", "HEAD", "--format", "markdown"]) == 0
assert "new set" in capsys.readouterr().out