import csv import json import pytest from grounding_eval.cli import main from grounding_eval.data import read_jsonl, write_jsonl def sample(sid, did, turn, ref, split="test_holdout"): return {"sample_id": sid, "dialogue_id": did, "turn": turn, "split": split, "task_type": "strategy_conditioned_response", "reference_response": ref, "messages": [{"role": "system", "content": "s"}, {"role": "user", "content": "u"}]} @pytest.fixture def instructions(tmp_path): path = tmp_path / "i.jsonl" write_jsonl(path, [sample("A", "D1", 2, "연간 400개 거래를 고려해 4,900원을 제안드립니다."), sample("B", "D2", 2, "대체 공급사 견적도 받아 보았습니다."), sample("C", "D3", 2, "학습용", split="train")]) return path def test_extract(instructions, tmp_path, capsys): out = tmp_path / "items.jsonl" main(["extract", "--instructions", str(instructions), "--out", str(out)]) rows = read_jsonl(out) assert [r["sample_id"] for r in rows] == ["A", "B"] assert "reference" in rows[0] and "messages" in rows[0] with pytest.raises(SystemExit): main(["extract", "--instructions", str(instructions), "--out", str(out)]) def test_generate_without_yes_makes_no_call(instructions, tmp_path, capsys): out = tmp_path / "b.jsonl" main(["generate", "--instructions", str(instructions), "--system", "baseline", "--base-url", "http://127.0.0.1:9/v1", "--model", "m", "--out", str(out)]) assert "--yes" in capsys.readouterr().out assert not out.exists() def test_score_writes_turn_table_and_summary(instructions, tmp_path): base, dev = tmp_path / "b.jsonl", tmp_path / "d.jsonl" write_jsonl(base, [{"sample_id": "A", "candidate": "조금 더 깎아 주실 수 있을까요"}, {"sample_id": "B", "candidate": "검토 부탁드립니다"}]) write_jsonl(dev, [{"sample_id": "A", "candidate": "연간 400개 거래를 감안해 4900원을 제안드립니다"}, {"sample_id": "B", "candidate": "대체 공급사 견적을 받아 보았습니다"}]) out_dir = tmp_path / "result" main(["score", "--instructions", str(instructions), "--baseline", str(base), "--developed", str(dev), "--out-dir", str(out_dir), "--bootstrap", "200"]) summary = json.loads((out_dir / "summary.json").read_text(encoding="utf-8")) with (out_dir / "turn_scores.csv").open(encoding="utf-8-sig") as f: table = list(csv.DictReader(f)) assert (summary["sessions"], summary["turns"]) == (2, 2) assert summary["metrics"]["rouge1"]["developed"] > summary["metrics"]["rouge1"]["baseline"] assert summary["achieved"] is True assert summary["conditions"]["tokenizer"] == "kiwi" and summary["conditions"]["tokenizer_version"] assert [r["sample_id"] for r in table] == ["A", "B"] with pytest.raises(SystemExit): main(["score", "--instructions", str(instructions), "--baseline", str(base), "--developed", str(dev), "--out-dir", str(out_dir)]) def test_score_reports_missing_candidates(instructions, tmp_path): base = tmp_path / "b.jsonl" write_jsonl(base, [{"sample_id": "A", "candidate": "x"}]) with pytest.raises(SystemExit, match="생성 발화가 없는 문항"): main(["score", "--instructions", str(instructions), "--baseline", str(base), "--developed", str(base), "--out-dir", str(tmp_path / "r")])