252 lines
14 KiB
Python
252 lines
14 KiB
Python
import decimal
|
|
import os
|
|
import pathlib
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
import textwrap
|
|
import unittest
|
|
|
|
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent))
|
|
|
|
from larv_review.history import appended, entries, entry_text, Place, scoring_runs
|
|
from failure import Failure
|
|
from larv_review.rules import first_ruling, HistoryChange, means, measured_commit, plan_items, PlanItem, round_counts, scoring_run_due, under_floor
|
|
from test_loop_claude_black_box import host_env
|
|
from larv_review.seats import report, ruling, scores, Seat, votes
|
|
|
|
TOOL = pathlib.Path(__file__).resolve().parent / "larv-review"
|
|
SCORING = "## 2026-10-08T12:00:00Z, PR #1 at aaaaaaa\n\n| Seat | Overall |\n|---|---|\n| Mean | 7.00 |\n"
|
|
|
|
|
|
def paired(time: str, verdict: str, commit: str = "bbbbbbb") -> str:
|
|
return f"## {time}, PR #2 at {commit}, against aaaaaaa\n\nRuling: {verdict}\n"
|
|
|
|
|
|
def item(id_: int, release: tuple[int, int, int], tagline: str = "**Do it.**") -> PlanItem:
|
|
return PlanItem(id_, release, tagline)
|
|
|
|
|
|
def todo(*rows: str) -> str:
|
|
return "## Items\n\n| ID | Release | Exempt | Item | R | S | A | G | Goals | Score |\n|---|---|---|---|---|---|---|---|---|---|\n" + "".join(f"{row}\n" for row in rows)
|
|
|
|
|
|
class Seats(unittest.TestCase):
|
|
def test_reads_each_seats_line_and_places_and_joins_a_wrapped_place(self):
|
|
given = report("PR #2 at abc1234, against def5678\n\nMid A: worse\n\n- `a.py:1` `f`: hard\n to read\n\nInherited architect: same\n")
|
|
self.assertEqual(given.read, "PR #2 at abc1234, against def5678")
|
|
self.assertEqual(given.seats, [Seat("Mid A", "worse", ["`a.py:1` `f`: hard to read"]), Seat("Inherited architect", "same", [])])
|
|
|
|
def test_refuses_a_line_that_is_neither_a_seat_nor_a_place_and_a_seat_twice(self):
|
|
self.assertIsInstance(report("PR #2 at abc1234\n\nMid A worse\n"), Failure)
|
|
self.assertIsInstance(report("PR #2 at abc1234\n\nMid A: worse\n\nMid A: same\n"), Failure)
|
|
self.assertIsInstance(report(""), Failure)
|
|
|
|
def test_counts_a_vote_naming_no_place_as_same(self):
|
|
seats = [Seat("Mid A", "worse", ["the whole skill"]), Seat("Inherited architect", "better", ["`a.py:3` `f`: shorter"])]
|
|
self.assertEqual(votes(seats), {"Mid A": "same", "Inherited architect": "better"})
|
|
|
|
def test_takes_the_first_two_seats_then_the_other_two_in_order(self):
|
|
first = [Seat("Mid A", "same", []), Seat("Inherited architect", "same", [])]
|
|
self.assertIsInstance(votes(first[::-1]), Failure)
|
|
self.assertIsInstance(votes(first + [Seat("Junior A", "same", [])]), Failure)
|
|
self.assertIsInstance(votes([first[0], Seat("Inherited architect", "worst", [])]), Failure)
|
|
|
|
def test_rules_by_a_lead_of_two_and_splits_two_seats_that_differ(self):
|
|
cases = [
|
|
(["worse", "worse"], "worse"),
|
|
(["same", "same"], "same"),
|
|
(["worse", "same"], None),
|
|
(["worse", "better", "worse", "worse"], "worse"),
|
|
(["worse", "same", "worse", "same"], "same"),
|
|
(["worse", "same", "worse", "better"], "same"),
|
|
(["better", "same", "better", "better"], "better"),
|
|
]
|
|
for counted, expected in cases:
|
|
with self.subTest(counted):
|
|
self.assertEqual(ruling(counted), expected)
|
|
|
|
def test_reads_four_seats_scores_and_refuses_an_overall_above_the_lowest_by_more_than_one(self):
|
|
line = "Navigation 7, Locality 6.5, Shape 7, Self-sufficiency 6.5, Overall 7"
|
|
seats = [Seat(name, line, []) for name in ("Junior A", "Mid A", "Maintainability senior", "Inherited architect")]
|
|
self.assertEqual(scores(seats)["Mid A"], ["7", "6.5", "7", "6.5", "7"])
|
|
self.assertIsInstance(scores(seats[:3]), Failure)
|
|
self.assertIsInstance(scores([*seats[:3], Seat("Inherited architect", line.replace("Overall 7", "Overall 8"), [])]), Failure)
|
|
self.assertIsInstance(scores([*seats[:3], Seat("Inherited architect", line.replace("Shape 7", "Shape 11"), [])]), Failure)
|
|
|
|
def test_rounds_each_mean_half_up_and_names_each_under_the_floor(self):
|
|
found = means({"a": ["7.5", "6.5", "7", "7", "7"], "b": ["7.5", "7", "7", "6.5", "7"], "c": ["7", "6.5", "7", "6.5", "6.5"], "d": ["7.5", "6.5", "7", "6.5", "7"]})
|
|
self.assertEqual([str(mean) for mean in found], ["7.38", "6.63", "7.00", "6.63", "6.88"])
|
|
self.assertEqual(under_floor(found), ["Locality 6.63", "Self-sufficiency 6.63", "Overall 6.88"])
|
|
self.assertEqual(under_floor([decimal.Decimal("7.00")] * 5), [])
|
|
|
|
|
|
class History(unittest.TestCase):
|
|
def test_reads_back_what_it_writes(self):
|
|
ruled = entry_text("2026-10-08T18:37:21Z, PR #2 at bbbbbbb, against aaaaaaa", ["Ruling: worse"], [["Seat", "Vote"], ["Mid A", "worse"]], [("Mid A, decided by:", ["`bin/a.py:3` `f`: hard, with `:9` `g`"]), ("Inherited architect, decided by:", [])])
|
|
history = appended(appended("", SCORING), ruled)
|
|
self.assertTrue(history.startswith("# Comprehension history\n\n## 2026-10-08T12:00:00Z"))
|
|
self.assertNotIn("Inherited architect, decided by:", history)
|
|
found = entries(history)
|
|
self.assertEqual([(entry.commit, entry.against, entry.ruling) for entry in found], [("aaaaaaa", None, None), ("bbbbbbb", "aaaaaaa", "worse")])
|
|
self.assertEqual(found[1].places, [Place("bin/a.py", "f"), Place("bin/a.py", "g")])
|
|
self.assertEqual([run.commit for run in scoring_runs(history)], ["aaaaaaa"])
|
|
|
|
|
|
class Base(unittest.TestCase):
|
|
def test_takes_the_newest_change_adding_a_scoring_run_or_ending_on_a_better_ruling(self):
|
|
older = HistoryChange("old", "", SCORING)
|
|
same = HistoryChange("same", SCORING, SCORING + paired("2026-10-08T13:00:00Z", "same"))
|
|
self.assertEqual(measured_commit([same, older]), "old")
|
|
# The file holds the later ruling first, as a merge conflict resolved the other way would.
|
|
better = HistoryChange("better", SCORING, SCORING + paired("2026-10-08T14:00:00Z", "better") + paired("2026-10-08T13:00:00Z", "worse"))
|
|
self.assertEqual(measured_commit([better, older]), "better")
|
|
worse_last = HistoryChange("worse", SCORING, SCORING + paired("2026-10-08T13:00:00Z", "better") + paired("2026-10-08T14:00:00Z", "worse"))
|
|
self.assertEqual(measured_commit([worse_last, older]), "old")
|
|
self.assertIsNone(measured_commit([same]))
|
|
|
|
def test_finds_the_earliest_ruling_by_its_time(self):
|
|
ruled = first_ruling(paired("2026-10-08T14:00:00Z", "same", "ccccccc") + paired("2026-10-08T13:00:00Z", "worse"))
|
|
self.assertEqual(ruled and ruled.commit, "bbbbbbb")
|
|
self.assertIsNone(first_ruling(SCORING))
|
|
|
|
|
|
class Rounds(unittest.TestCase):
|
|
def test_counts_each_passs_highest_round(self):
|
|
counts = round_counts(["Answer prose round 1: x", "Answer full scan round 2: y", "Answer full scan round 1: z", "Answer stability: no round", "Rescore: w"])
|
|
self.assertEqual((counts["prose"], counts["full scan"], counts["stability"], counts["architecture"]), (1, 2, 0, 0))
|
|
|
|
|
|
class ScoringRunDue(unittest.TestCase):
|
|
def test_reads_each_rows_id_release_and_tagline(self):
|
|
self.assertEqual(plan_items(todo("| 3 | 0.1.0 | | **Do it.** | 1 | 1 | 1 | 1 | 1 | 9.0 |")), [item(3, (0, 1, 0))])
|
|
self.assertEqual(plan_items("# todo\n"), [])
|
|
self.assertIsInstance(plan_items(todo("| x | 0.1.0 | | **Do it.** | 1 | 1 | 1 | 1 | 1 | 9.0 |")), Failure)
|
|
|
|
def test_is_due_for_an_item_naming_the_floor(self):
|
|
plan = [item(1, (0, 1, 0), "**Lift the project to the Comprehension floor.**"), item(2, (0, 1, 0))]
|
|
self.assertEqual(scoring_run_due(plan, plan, [1], True), ["ships item 1, which names the comprehension floor"])
|
|
|
|
def test_is_due_when_the_chunk_ships_a_releases_last_item(self):
|
|
plan = [item(1, (0, 1, 0)), item(2, (0, 2, 0))]
|
|
self.assertEqual(scoring_run_due(plan, plan, [1], False), ["leaves release 0.1.0 with no plan item"])
|
|
self.assertEqual(scoring_run_due(plan, plan + [item(3, (0, 1, 0))], [1], True), [])
|
|
|
|
def test_is_due_when_the_chunk_removes_the_next_releases_last_item_after_work_landed(self):
|
|
before = [item(1, (0, 1, 0)), item(2, (0, 1, 0)), item(3, (0, 2, 0))]
|
|
after = [item(2, (0, 1, 0)), item(3, (0, 2, 0))]
|
|
self.assertEqual(scoring_run_due(before, after, [2], True), ["leaves release 0.1.0 with no plan item"])
|
|
self.assertEqual(scoring_run_due(before, after, [], True), [])
|
|
emptied = [item(3, (0, 2, 0))]
|
|
self.assertEqual(scoring_run_due(before, emptied, [2], False), ["leaves release 0.1.0 with no plan item"])
|
|
self.assertEqual(scoring_run_due(before, emptied, [], True), ["leaves release 0.1.0 with no plan item"])
|
|
self.assertEqual(scoring_run_due(before, emptied, [], False), [])
|
|
|
|
def test_is_not_due_when_a_later_release_loses_its_last_item_unshipped(self):
|
|
before = [item(1, (0, 1, 0)), item(3, (0, 2, 0))]
|
|
self.assertEqual(scoring_run_due(before, before[:1], [], True), [])
|
|
|
|
|
|
class LarvReviewTool(unittest.TestCase):
|
|
def setUp(self):
|
|
temp = tempfile.TemporaryDirectory()
|
|
self.addCleanup(temp.cleanup)
|
|
root = pathlib.Path(os.path.realpath(temp.name))
|
|
self.env = {**host_env(), "GIT_CONFIG_GLOBAL": os.devnull, "HOME": str(root)}
|
|
self.checkout = root / "proj"
|
|
subprocess.run(["git", "init", "-q", "--bare", "-b", "main", str(root / "origin.git")], check=True, env=self.env)
|
|
subprocess.run(["git", "clone", "-q", str(root / "origin.git"), str(self.checkout)], check=True, env=self.env, capture_output=True)
|
|
self.commit("start", {"todo.md": todo("| 1 | 0.1.0 | | **Do it.** | 1 | 1 | 1 | 1 | 1 | 9.0 |", "| 2 | 0.2.0 | | **Then this.** | 1 | 1 | 1 | 1 | 1 | 9.0 |", "| 3 | 0.2.0 | | **And this.** | 1 | 1 | 1 | 1 | 1 | 9.0 |")})
|
|
self.git("push", "-q", "origin", "HEAD:main")
|
|
self.git("remote", "set-head", "origin", "main")
|
|
self.git("switch", "-q", "-c", "chunk")
|
|
|
|
def git(self, *args):
|
|
return subprocess.run(["git", "-c", "user.name=t", "-c", "user.email=t@t", *args], cwd=self.checkout, check=True, env=self.env, capture_output=True, text=True).stdout
|
|
|
|
def commit(self, title, files=None):
|
|
for name, text in (files or {}).items():
|
|
(self.checkout / name).parent.mkdir(parents=True, exist_ok=True)
|
|
(self.checkout / name).write_text(text)
|
|
self.git("add", name)
|
|
self.git("commit", "-q", "--allow-empty", "-m", title)
|
|
|
|
def run_tool(self, *args):
|
|
return subprocess.run([sys.executable, str(TOOL), *args], capture_output=True, cwd=self.checkout, env=self.env, text=True)
|
|
|
|
def seats_file(self, text):
|
|
path = self.checkout.parent / "seats.md"
|
|
path.write_text(textwrap.dedent(text))
|
|
return str(path)
|
|
|
|
def test_takes_the_merge_base_until_a_merged_change_records_a_scoring_run(self):
|
|
start = self.git("rev-parse", "HEAD").strip()
|
|
self.commit("work")
|
|
ran = self.run_tool("ruling-base")
|
|
self.assertEqual((ran.returncode, ran.stdout.splitlines()[0]), (0, start), ran.stderr)
|
|
self.git("switch", "-q", "-c", "measured", "main")
|
|
self.commit("Merge a scoring run", {"docs/comprehension-history.md": "# Comprehension history\n\n" + SCORING})
|
|
self.git("push", "-q", "origin", "HEAD:main")
|
|
measured = self.git("rev-parse", "HEAD").strip()
|
|
self.git("switch", "-q", "chunk")
|
|
self.assertEqual(self.run_tool("ruling-base").stdout.splitlines()[0], measured)
|
|
|
|
def test_counts_rounds_on_this_branch_alone(self):
|
|
self.commit("Answer stability round 1: a")
|
|
self.commit("Answer stability round 2: b")
|
|
self.commit("Answer prose round 1: c")
|
|
self.commit("Answer stability round 3: d")
|
|
ran = self.run_tool("round-count")
|
|
self.assertIn("stability: 3, at the cap\n", ran.stdout)
|
|
self.assertIn("prose: 1\n", ran.stdout)
|
|
self.assertIn("architecture: 0\n", ran.stdout)
|
|
|
|
def test_says_whether_a_scoring_run_is_due_and_refuses_an_unknown_item(self):
|
|
self.assertEqual(self.run_tool("needs-scoring-run", "1").stdout, "Scoring run due: the chunk leaves release 0.1.0 with no plan item.\n")
|
|
self.assertEqual(self.run_tool("needs-scoring-run", "2").stdout, "No scoring run due.\n")
|
|
unknown = self.run_tool("needs-scoring-run", "9")
|
|
self.assertEqual(unknown.returncode, 1)
|
|
self.assertIn("item 9", unknown.stderr)
|
|
|
|
def test_records_a_ruling_once_four_seats_settle_a_split(self):
|
|
lines = """\
|
|
PR #2 at abc1234, against def5678
|
|
|
|
Mid A: worse
|
|
|
|
- `bin/a.py:3` `f`: three branches
|
|
|
|
Inherited architect: same
|
|
"""
|
|
split = self.run_tool("record-ruling", self.seats_file(lines))
|
|
self.assertEqual(split.returncode, 0, split.stderr)
|
|
self.assertTrue(split.stdout.startswith("Split: Mid A worse, Inherited architect same."))
|
|
history = self.checkout / "docs" / "comprehension-history.md"
|
|
self.assertFalse(history.exists())
|
|
lines += "\n\t\t\tMaintainability senior: worse\n\n\t\t\t- `bin/a.py:3` `f`: hard\n\n\t\t\tJunior A: worse\n"
|
|
ran = self.run_tool("record-ruling", self.seats_file(lines))
|
|
self.assertEqual(ran.stdout.splitlines()[0], "Ruling: same. Votes as counted: Mid A worse, Inherited architect same, Maintainability senior worse, Junior A same.")
|
|
recorded = entries(history.read_text())
|
|
self.assertEqual([(entry.commit, entry.against, entry.ruling) for entry in recorded], [("abc1234", "def5678", "same")])
|
|
self.assertIn("| Junior A | same (voted worse, naming no place) |", history.read_text())
|
|
|
|
def test_records_a_scoring_run_with_its_means(self):
|
|
line = "Navigation 7, Locality 6.5, Shape 7, Self-sufficiency 6.5, Overall 7"
|
|
seats = "".join(f"\n{seat}: {line}\n\n- `bin/a.py:3` `f`: long\n" for seat in ("Junior A", "Mid A", "Maintainability senior", "Inherited architect"))
|
|
ran = self.run_tool("record-scoring", self.seats_file("PR #2 at abc1234\n" + seats))
|
|
self.assertEqual(ran.returncode, 0, ran.stderr)
|
|
self.assertIn("Under the floor of 7.0: Locality 6.50, Self-sufficiency 6.50.", ran.stdout)
|
|
text = (self.checkout / "docs" / "comprehension-history.md").read_text()
|
|
self.assertIn("| Mean | 7.00 | 6.50 | 7.00 | 6.50 | 7.00 |", text)
|
|
self.assertIn("Inherited architect, would restructure first:", text)
|
|
self.assertEqual([run.places for run in scoring_runs(text)], [[Place("bin/a.py", "f")] * 4])
|
|
|
|
def test_refuses_a_ruling_file_that_names_no_base(self):
|
|
ran = self.run_tool("record-ruling", self.seats_file("PR #2 at abc1234\n\nMid A: same\n\nInherited architect: same\n"))
|
|
self.assertEqual(ran.returncode, 1)
|
|
self.assertIn("against <base>", ran.stderr)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|