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