110 lines
7.3 KiB
Python
110 lines
7.3 KiB
Python
import decimal
|
|
import pathlib
|
|
import sys
|
|
import unittest
|
|
|
|
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
|
|
|
|
from do_epic.ranking import chain, EpicItem, ranked, RepoPremises
|
|
from failure import Failure
|
|
from plan.todo_table import Item, parse_moves
|
|
|
|
EPIC_GOALS = "1. **Achieve:** Ship invoices.\n2. **Achieve:** Cut support mail.\n3. **Maintain:** Leak no customer data.\n"
|
|
VOIDED = RepoPremises("## Goals\n\n**Void:** code health, since it is a one-off script.\n\n1. **Achieve:** Fast builds.\n", "", "", decimal.Decimal("7.0"))
|
|
HEALTHY = RepoPremises("## Goals\n\n1. **Achieve:** Fast builds.\n2. **Maintain:** Stay on the LTS.\n", "", "", decimal.Decimal("7.0"))
|
|
# What the three achieve goals of the chain epic 1, epic 2, repo 1 weigh together before they are scaled to sum to 1, each 0.97 of the one above.
|
|
TOTAL = 1 + 0.97 + 0.97**2
|
|
|
|
|
|
def item(id_: int, moves: str, repo: str | None = "billing-api", exempt: str = "", blocked_by: tuple[int, ...] = ()) -> EpicItem:
|
|
parsed = parse_moves(f"item {id_}", moves)
|
|
assert not isinstance(parsed, Failure), parsed
|
|
return EpicItem(Item(id_, (0, 0, 0), exempt, f"**Item {id_}.**", {"L": 1, "E": 1, "I": 1, "S": 1}, parsed, blocked_by), repo) # type: ignore[arg-type]
|
|
|
|
|
|
def order(items: list[EpicItem], repos: dict[str, RepoPremises], formula: str = "V") -> list[int] | Failure:
|
|
found = ranked(items, formula, EPIC_GOALS, repos)
|
|
return found if isinstance(found, Failure) else [scored.id for scored, _ in found]
|
|
|
|
|
|
class MovesSpellingTest(unittest.TestCase):
|
|
def test_a_moves_cell_names_an_epics_goal_and_a_repos_goal_by_their_scope(self) -> None:
|
|
self.assertEqual(parse_moves("item 1", "epic 1.1 by 8, code health by 5, repo 2 by 4"), (("epic 1.1", 8), ("code health", 5), ("repo 2", 4)))
|
|
|
|
def test_a_moves_cell_refuses_an_epic_goal_beside_its_own_sub_goal(self) -> None:
|
|
self.assertIsInstance(parse_moves("item 1", "epic 1 by 8, epic 1.1 by 5"), Failure)
|
|
self.assertEqual(parse_moves("item 1", "epic 1 by 8, repo 1.1 by 5"), (("epic 1", 8), ("repo 1.1", 5)))
|
|
|
|
|
|
class ChainTest(unittest.TestCase):
|
|
def test_the_chain_is_the_epics_goals_then_code_health_then_the_repos_goals(self) -> None:
|
|
self.assertEqual(chain(EPIC_GOALS, "web", HEALTHY), [("epic 1", "Achieve"), ("epic 2", "Achieve"), ("epic 3", "Maintain"), ("code health", "Achieve"), ("repo 1", "Achieve"), ("repo 2", "Maintain")])
|
|
|
|
def test_a_repo_that_voids_code_health_leaves_it_out_of_its_chain(self) -> None:
|
|
self.assertEqual(chain(EPIC_GOALS, "billing-api", VOIDED), [("epic 1", "Achieve"), ("epic 2", "Achieve"), ("epic 3", "Maintain"), ("repo 1", "Achieve")])
|
|
|
|
def test_an_item_without_a_repo_weighs_only_the_epics_goals(self) -> None:
|
|
self.assertEqual(chain(EPIC_GOALS, None, None), [("epic 1", "Achieve"), ("epic 2", "Achieve"), ("epic 3", "Maintain")])
|
|
|
|
def test_an_epic_without_a_numbered_goal_fails(self) -> None:
|
|
self.assertEqual(chain("Ship it.\n", None, None), Failure("the epic's ## Goals has no numbered goal"))
|
|
|
|
|
|
class RankedTest(unittest.TestCase):
|
|
def test_each_goal_weighs_0_97_of_the_one_above_it_in_the_chain(self) -> None:
|
|
found = ranked([item(1, "repo 1 by 10"), item(2, "epic 2 by 10"), item(3, "epic 1 by 10")], "V", EPIC_GOALS, {"billing-api": VOIDED})
|
|
assert not isinstance(found, Failure), found
|
|
self.assertEqual([scored.id for scored, _ in found], [3, 2, 1])
|
|
for (_, value), expected in zip(found, (10 / TOTAL, 9.7 / TOTAL, 10 * 0.97**2 / TOTAL)):
|
|
self.assertAlmostEqual(value, expected)
|
|
|
|
def test_where_the_epic_and_a_repo_disagree_the_epic_wins(self) -> None:
|
|
self.assertEqual(order([item(1, "repo 1 by 5"), item(2, "epic 2 by 5")], {"billing-api": VOIDED}), [2, 1])
|
|
|
|
def test_items_of_two_repos_rank_in_one_order_each_by_its_own_repos_chain(self) -> None:
|
|
repos = {"billing-api": VOIDED, "web": HEALTHY}
|
|
self.assertEqual(order([item(1, "repo 1 by 5"), item(2, "repo 1 by 6", repo="web"), item(3, "epic 1 by 9", repo="web")], repos, "V * 0 + S + I"), [1, 2, 3])
|
|
self.assertEqual(order([item(1, "repo 1 by 5"), item(2, "repo 2 by 9", repo="web")], repos), [2, 1])
|
|
|
|
def test_an_item_without_a_repo_scores_by_the_epics_goals_alone(self) -> None:
|
|
found = ranked([item(1, "epic 1 by 10", repo=None)], "V", EPIC_GOALS, {})
|
|
assert not isinstance(found, Failure), found
|
|
self.assertAlmostEqual(found[0][1], 10 / 1.97)
|
|
|
|
def test_an_item_without_a_repo_may_not_name_a_repos_goal_or_code_health(self) -> None:
|
|
self.assertEqual(order([item(4, "repo 1 by 5", repo=None)], {}), Failure("item 4 names repo 1, but has no repo whose goal that could be; name only the epic's goals, as in `epic 1 by 5`"))
|
|
self.assertEqual(order([item(4, "code health by 5", repo=None)], {}), Failure("item 4 names code health, but has no repo whose code that could be; name only the epic's goals, as in `epic 1 by 5`"))
|
|
|
|
def test_a_goal_without_its_scope_is_refused(self) -> None:
|
|
self.assertEqual(order([item(4, "1 by 5")], {"billing-api": VOIDED}), Failure("item 4 names goal 1 without saying whose it is; write `epic 1` for the epic's goal or `repo 1` for its repo's"))
|
|
|
|
def test_a_goal_the_chain_does_not_hold_is_refused(self) -> None:
|
|
self.assertEqual(order([item(4, "epic 9 by 5")], {"billing-api": VOIDED}), Failure("item 4 names epic 9, which the epic's ## Goals does not number"))
|
|
self.assertEqual(order([item(4, "repo 9 by 5")], {"billing-api": VOIDED}), Failure("item 4 names repo 9, which the ## Goals of billing-api's goals file does not number"))
|
|
self.assertEqual(order([item(4, "code health by 5")], {"billing-api": VOIDED}), Failure("item 4 names code health, which billing-api's goals file voids"))
|
|
|
|
def test_an_item_in_a_repo_without_premises_fails(self) -> None:
|
|
self.assertEqual(order([item(4, "epic 1 by 5", repo="ghost")], {}), Failure("item 4 is in the repo ghost, whose goals were not read"))
|
|
|
|
def test_a_goal_marked_neither_way_fails_naming_whose_it_is(self) -> None:
|
|
unmarked = RepoPremises("## Goals\n\n1. Fast builds.\n", "", "", decimal.Decimal("7.0"))
|
|
self.assertEqual(order([item(4, "epic 1 by 5")], {"billing-api": unmarked}), Failure("billing-api's goal 1 is marked neither **Maintain:** nor **Achieve:**; mark it, as ~/.claude/rules/goals-and-audience.md says"))
|
|
found = ranked([item(4, "epic 1 by 5", repo=None)], "V", "1. Ship invoices.\n", {})
|
|
self.assertEqual(found, Failure("the epic's goal 1 is marked neither **Maintain:** nor **Achieve:**; mark it, as ~/.claude/rules/goals-and-audience.md says"))
|
|
|
|
def test_a_repos_goals_file_that_cannot_be_read_fails_naming_the_repo(self) -> None:
|
|
self.assertEqual(order([item(4, "epic 1 by 5")], {"billing-api": RepoPremises("# Billing\n", "", "", decimal.Decimal("7.0"))}), Failure("billing-api: the goals file has no ## Goals heading"))
|
|
|
|
def test_a_question_then_a_security_finding_rank_before_any_score(self) -> None:
|
|
self.assertEqual(order([item(1, "epic 1 by 10"), item(2, "epic 2 by 1", exempt="security"), item(3, "", exempt="question")], {"billing-api": VOIDED}), [3, 2, 1])
|
|
|
|
def test_a_blocker_ranks_above_the_item_it_blocks_and_a_tie_goes_to_the_older_item(self) -> None:
|
|
self.assertEqual(order([item(1, "epic 1 by 9", blocked_by=(3,)), item(2, "epic 1 by 2"), item(3, "epic 1 by 1"), item(5, "epic 1 by 2")], {"billing-api": VOIDED}), [3, 1, 2, 5])
|
|
|
|
def test_a_formula_that_cannot_be_read_fails(self) -> None:
|
|
self.assertIsInstance(order([item(1, "epic 1 by 9")], {"billing-api": VOIDED}, "V +"), Failure)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|