import pathlib import sys import unittest sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) from model_routing.ladder import floored, highest, LADDER, picked, project_rung, review_rung, Rung, stepped_up OPUS = Rung("claude-opus-5-5", "medium") class Ladder(unittest.TestCase): def test_runs_from_haiku_at_low_effort_to_fable_at_max_with_opus_at_medium_between(self): self.assertEqual(LADDER[0], Rung("claude-haiku-5-5", "low")) self.assertEqual(LADDER[-1], Rung("claude-fable-5-1", "max")) self.assertEqual(len(LADDER), 11) self.assertEqual(len(set(LADDER)), len(LADDER)) def test_puts_a_project_on_its_models_cheapest_rung_and_none_off_the_ladder(self): self.assertEqual(project_rung("claude-opus-5-5"), OPUS) self.assertEqual(project_rung("claude-sonnet-5-5"), Rung("claude-sonnet-5-5", "high")) self.assertIsNone(project_rung("claude-opus-5")) self.assertIsNone(project_rung("opus")) def test_steps_each_band_of_r_plus_c_from_the_projects_rung(self): position = LADDER.index(OPUS) self.assertEqual([LADDER.index(picked(OPUS, total)) - position for total in range(2, 21)], [-3, -3, -3, -2, -2, -1, -1, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 4, 4]) def test_a_step_past_either_end_stops_there(self): self.assertEqual(picked(OPUS, 4), Rung("claude-haiku-5-5", "xhigh")) self.assertEqual(picked(OPUS, 17), Rung("claude-fable-5-1", "max")) self.assertEqual(picked(LADDER[1], 2), LADDER[0]) self.assertEqual(stepped_up(LADDER[-1]), LADDER[-1]) self.assertEqual(stepped_up(OPUS), Rung("claude-opus-5-5", "high")) def test_floors_the_picked_rung_at_the_projects_rung(self): self.assertEqual(floored(OPUS, 2), OPUS) self.assertEqual(floored(OPUS, 12), Rung("claude-opus-5-5", "high")) def test_names_each_rung_by_a_family_the_agent_tool_takes(self): self.assertEqual({rung.agent_args().split(",")[0] for rung in LADDER}, {"model haiku", "model sonnet", "model opus", "model fable"}) def test_ranks_a_rung_off_the_ladder_below_every_rung_on_it(self): self.assertEqual(highest(OPUS, LADDER[-1], LADDER[0]), LADDER[-1]) self.assertEqual(highest(Rung("claude-opus-5-5", "low"), LADDER[0]), LADDER[0]) self.assertEqual(stepped_up(Rung("claude-opus-5-5", "low")), LADDER[0]) def test_passes_the_effort_only_where_the_rung_names_one(self): self.assertEqual(Rung("claude-opus-5-5", "high").flags(), ["--model", "claude-opus-5-5", "--effort", "high"]) self.assertEqual(Rung("worker-model").flags(), ["--model", "worker-model"]) self.assertEqual(str(Rung("claude-opus-5-5", "high")), "claude-opus-5-5 at high effort") self.assertEqual(str(Rung("worker-model")), "worker-model") def test_parses_what_str_wrote_for_each_rung_on_the_ladder(self): for rung in LADDER: with self.subTest(rung=rung): self.assertEqual(Rung.parse(str(rung)), rung) def test_parses_a_rung_off_the_ladder_too_for_the_caller_to_judge(self): self.assertEqual(Rung.parse("claude-opus-5-5 at low effort"), Rung("claude-opus-5-5", "low")) def test_parses_no_other_wording_as_a_rung(self): for text in ("", "claude-opus-5-5", "claude-opus-5-5 medium", "claude-opus-5-5 at medium", "at medium effort", "a b at medium effort"): with self.subTest(text=text): self.assertIsNone(Rung.parse(text)) def test_reads_the_agent_tools_words_back_as_a_rung(self): for rung in LADDER: with self.subTest(rung=rung): model, effort = (part.split(" ")[1] for part in rung.agent_args().split(", ")) self.assertEqual(Rung.from_agent(model, effort), rung) def test_reads_a_family_named_without_an_effort_as_its_cheapest_rung(self): self.assertEqual(Rung.from_agent("opus", ""), OPUS) self.assertEqual(Rung.from_agent("sonnet", ""), Rung("claude-sonnet-5-5", "high")) def test_reads_an_unknown_family_or_an_effort_off_the_ladder_as_a_rung_off_it(self): for model, effort in (("gpt", "high"), ("gpt", ""), ("opus", "low"), ("haiku", "bogus")): with self.subTest(model=model, effort=effort): self.assertNotIn(Rung.from_agent(model, effort), LADDER) self.assertEqual(Rung.from_agent("opus", "low"), Rung("claude-opus-5-5", "low")) def test_holds_a_reviewer_to_the_higher_of_the_workers_rung_and_the_projects_and_anything_else_to_the_projects(self): haiku, fable = LADDER[4], LADDER[-1] self.assertEqual(review_rung("reviewer", OPUS, haiku), OPUS) self.assertEqual(review_rung("reviewer", OPUS, fable), fable) for kind in ("seat", "security"): with self.subTest(kind=kind): self.assertEqual(review_rung(kind, OPUS, fable), OPUS) self.assertEqual(review_rung(kind, OPUS, haiku), OPUS) if __name__ == "__main__": unittest.main()