import json import sys import unittest from collections import Counter from pathlib import Path ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT)) from constraints import check, features from evaluate import evaluate, read_jsonl class EvaluationTests(unittest.TestCase): @classmethod def setUpClass(cls): cls.prompts = read_jsonl(ROOT / "data/test.jsonl") cls.examples = read_jsonl(ROOT / "examples/responses.jsonl") def mechanical(self, number, answer): prompt = self.prompts[number - 1] return check({"id": prompt["id"], "final": answer, "prompt": prompt})["checks"] def test_dataset_inventory(self): self.assertEqual([p["id"] for p in self.prompts], [f"chat_{i:03d}" for i in range(1, 56)]) self.assertEqual(set(Counter(p["category"] for p in self.prompts).values()), {5}) self.assertEqual(sum(len(p["checks"]) for p in self.prompts), 162) self.assertEqual(sum(len(p["semantic_criteria"]) for p in self.prompts), 154) def test_all_empty_answers_fail(self): responses = [{"id": p["id"], "response": ""} for p in self.prompts] report, _ = evaluate(self.prompts, responses) self.assertEqual(report["summary"]["constraints"]["failed"], 162) self.assertEqual(report["summary"]["semantics"]["failed"], 154) self.assertEqual(report["summary"]["joint"]["pass_rate"], 0) def test_every_checker_accepts_arbitrary_answer_text(self): for answer in ["Something.", "A\nB\nC", "[]", "1. First.\n2. Second."]: responses = [{"id": p["id"], "response": answer} for p in self.prompts] report, _ = evaluate(self.prompts, responses) self.assertEqual(report["summary"]["constraints"]["total"], 162) def test_semantics_are_pending_without_review(self): report, _ = evaluate(self.prompts, self.examples, allow_partial=True) self.assertEqual(report["summary"]["semantics"]["pending"], 5) self.assertIsNone(report["summary"]["semantics"]["pass_rate"]) self.assertIsNone(report["summary"]["joint"]["pass_rate"]) self.assertEqual(report["summary"]["constraints"]["pending"], 1) def test_reviewed_examples_pass(self): reviews = json.loads((ROOT / "examples/reviews.json").read_text()) report, _ = evaluate(self.prompts, self.examples, reviews, allow_partial=True) self.assertEqual(report["summary"]["joint"]["passed"], 2) self.assertEqual(report["summary"]["joint"]["pass_rate"], 1) self.assertEqual(report["results"][1]["constraints"][3]["source"], "review") def test_failed_semantics_do_not_change_constraint_score(self): reviews = {"chat_001": {"semantic_criteria": [False, True]}} report, _ = evaluate(self.prompts, self.examples[:1], reviews, allow_partial=True) self.assertTrue(report["results"][0]["all_constraints_pass"]) self.assertFalse(report["results"][0]["joint_pass"]) def test_thinking_scope_and_unfinished_thinking(self): answer = self.examples[0] trace = {"id": answer["id"], "response": "Forbidden words: love romance." + answer["response"]} report, _ = evaluate(self.prompts, [trace], response_format="thinking", allow_partial=True) self.assertTrue(report["results"][0]["all_constraints_pass"]) trace["response"] = "Still considering the answer." report, _ = evaluate(self.prompts, [trace], response_format="thinking", allow_partial=True) self.assertFalse(report["results"][0]["usable_final_answer"]) self.assertEqual(report["summary"]["semantics"]["failed"], 2) def test_missing_duplicate_and_unknown_ids(self): for responses in [self.examples, self.examples * 2, [{"id": "chat_999", "response": "Hello"}]]: with self.assertRaises(ValueError): evaluate(self.prompts, responses) def test_invalid_review_is_rejected(self): bad_reviews = [ {"chat_001": {"semantic_criteria": [True]}}, {"chat_001": {"semantic_criteria": [1, True]}}, {"chat_001": {"constraint_overrides": {"0": True}}}, {"chat_001": {"constraint_overrides": {"9": True}, "notes": "Invalid index"}}, ] for reviews in bad_reviews: with self.assertRaises(ValueError): evaluate(self.prompts, self.examples, reviews, allow_partial=True) def test_minutes_allow_optional_units_and_decimals(self): for duration in ["2.5", "2.5 minutes", "2.5 min"]: answer = "\n".join(f"{i}. ({duration}) Put things away." for i in range(1, 5)) self.assertEqual(self.mechanical(14, answer), [True, True, True]) answer = "\n".join(f"{i}. (3) Put things away." for i in range(1, 5)) self.assertFalse(self.mechanical(14, answer)[2]) def test_table_requires_separator_and_two_rows(self): answer = "Method | Tradeoff\n--- | ---\nVote | Popular; can exclude minority tastes\nTurns | Fair turns; occasional disappointment" self.assertEqual(self.mechanical(25, answer), [True, True, True]) self.assertFalse(self.mechanical(25, answer.replace("--- | ---", "bogus | separator"))[0]) def test_json_requires_objects(self): self.assertFalse(self.mechanical(40, '["a", "b"]')[0]) valid = json.dumps([{"person": "Maya", "action": "return blanket", "deadline": "Tuesday"}, {"person": "I", "action": "send photos", "deadline": "Friday"}]) self.assertEqual(self.mechanical(40, valid), [True, True, True]) def test_word_and_sentence_boundaries(self): self.assertEqual(len(features('1. Wait 2.5 minutes.\n2. Say "Done." Then leave.')[2]), 3) self.assertTrue(self.mechanical(1, "Sunny romance. Silent lovers. Small love.")[0]) self.assertFalse(self.mechanical(1, "Sunny romance. Silent lovers. Small love.")[2]) if __name__ == "__main__": unittest.main()