import json import pathlib import sys import unittest sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) from do_epic.issue_scores import answer_of, asked, prompt, SCHEMA, Scores from do_epic.test_repo_pick import Claude, says from do_epic.backend.protocol import EpicIssue from failure import Failure from model_routing.ladder import Rung ANSWER = {"L": 2, "E": 1, "I": 2, "S": 3, "moves": " epic 1 by 5 ", "comment": " Scored to pick what starts first. "} ISSUE = EpicIssue("ABC-13", "Count the runs", "Count each run once.", ("api",), None, "To Do", False, "ABC-12") def expected(output: object) -> Failure: return Failure(f"the scoring answered {json.dumps(output)}; expected a whole number for each of L, E, I, S, the moves as text and a comment") class TheAnswer(unittest.TestCase): def test_reads_the_scores_the_moves_and_the_comment(self) -> None: self.assertEqual(answer_of(ANSWER), Scores({"L": 2, "E": 1, "I": 2, "S": 3}, "epic 1 by 5", "Scored to pick what starts first.")) def test_refuses_an_answer_that_is_not_an_object(self) -> None: self.assertEqual(answer_of(["L", 2]), expected(["L", 2])) def test_refuses_a_score_that_is_not_a_whole_number(self) -> None: for score in ("2", 2.0, True, None): with self.subTest(score=score): self.assertEqual(answer_of(ANSWER | {"I": score}), expected(ANSWER | {"I": score})) def test_refuses_moves_that_are_not_text(self) -> None: self.assertEqual(answer_of(ANSWER | {"moves": None}), expected(ANSWER | {"moves": None})) def test_refuses_an_answer_without_a_comment(self) -> None: for comment in (None, " ", 5): with self.subTest(comment=comment): self.assertEqual(answer_of(ANSWER | {"comment": comment}), expected(ANSWER | {"comment": comment})) def test_quotes_the_first_200_characters_of_an_answer_it_refuses(self) -> None: long = {"comment": "c" * 300} self.assertEqual(answer_of(long), Failure(f"the scoring answered {json.dumps(long)[:200]}; expected a whole number for each of L, E, I, S, the moves as text and a comment")) class TheCall(unittest.TestCase): def ask(self, claude: Claude, issue: EpicIssue = ISSUE) -> object: return asked(issue, "L is the chance of a defect.", "An item moves only these: `epic 1`.", "Swedish", Rung("claude-sonnet-5-5", "medium"), claude, "/state/scoring") def test_asks_claude_for_a_json_reply_on_the_given_rung_and_without_tools(self) -> None: claude = says(ANSWER) self.assertEqual(self.ask(claude), answer_of(ANSWER)) args, cwd, _ = claude.calls[0] self.assertEqual(cwd, "/state/scoring") self.assertEqual(args[:10], ["-p", "--json-schema", json.dumps(SCHEMA), "--model", "claude-sonnet-5-5", "--effort", "medium", "--tools", "", "--strict-mcp-config"]) def test_names_the_scoring_where_claude_fails(self) -> None: failed = self.ask(Claude("", 1)) assert isinstance(failed, Failure), failed self.assertEqual(failed.reason, "the scoring exited 1") def test_puts_the_rule_the_goals_the_language_and_the_item_in_the_prompt(self) -> None: text = prompt(ISSUE, "L is the chance of a defect.", "An item moves only these: `epic 1`.", "Swedish") for part in ("L is the chance of a defect.", "An item moves only these: `epic 1`.", "Write the comment in Swedish", "## Item ABC-13: Count the runs", "Labels: api", "Count each run once."): with self.subTest(part=part): self.assertIn(part, text) self.assertNotIn("(no description)", text) def test_says_where_the_item_has_no_labels_or_description(self) -> None: text = prompt(EpicIssue("ABC-13", "Count the runs", "", (), None, "To Do", False, "ABC-12"), "Rule.", "Goals.", "English") self.assertTrue(text.endswith("Labels: none\n\n(no description)\n"), text[-60:]) if __name__ == "__main__": unittest.main()