Files
claude-config/bin/do_epic/test_gates.py
T

201 lines
14 KiB
Python

import json
import pathlib
import sys
import unittest
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
from do_epic.gates import APPROVAL, CHECKS, gate, Gate, MERGE, NEW_REVIEW, Open, Unmergeable, Unopened
from failure import Failure
WORKTREES = "worktree /code/billing\nHEAD aaa\nbranch refs/heads/main\n\nworktree /code/billing-ABC-21\nHEAD abc\nbranch refs/heads/ABC-21\n"
HEAD = "abc" * 4
def thread(resolved: bool, last: str, path: str = "a.py") -> dict:
return {"isResolved": resolved, "path": path, "comments": {"nodes": [{"author": {"login": last}}]}}
def review(state: str, at: str, by: str = "reviewer", on: str = HEAD) -> dict:
"""A review as `gh pr view --json reviews` prints it: made by `by` at `at`, on the commit `on`."""
return {"author": {"login": by}, "state": state, "submittedAt": at, "commit": {"oid": on}}
def comment(by: str, at: str) -> dict:
return {"author": {"login": by}, "createdAt": at}
class Forge:
"""Stands in for git and the forge's CLI in the checkout and the chunk's worktree; `ran` holds each folder a command ran in. Only the CLI of origin's forge answers: `gh` fails on a Gitea origin, and `tea` on a GitHub one."""
def __init__(self, origin: str = "git@github.com:o/billing.git", pulls: str = f"7\t{HEAD}\tfalse\n", draft: bool = False, decision: str = "REVIEW_REQUIRED", buckets: tuple[str, ...] = ("pass",), threads: tuple[dict, ...] = (), status: str = '{"state": "success", "total_count": 1}', mergeable: str = "MERGEABLE", merge_state: str = "CLEAN", worktrees: str = WORKTREES, fails: str = "", reviews: tuple[dict, ...] = (), comments: tuple[dict, ...] = (), title: str = "Add x", base: str = "main") -> None:
self.cli = "gh" if "github.com" in origin else "tea"
self.ran: list[str] = []
self.commands: list[list[str]] = []
self.fails = fails
self.answers = {
"git worktree": worktrees,
"git remote": origin + "\n",
"git rev-parse": HEAD + "\n",
"git symbolic-ref": "origin/main\n",
"git config": Failure("git config: unset"),
"gh pr list": pulls,
"gh pr view": json.dumps({"isDraft": draft, "reviewDecision": decision, "author": {"login": "worker"}, "reviews": list(reviews), "comments": list(comments), "mergeable": mergeable, "mergeStateStatus": merge_state, "baseRefName": base}),
"gh pr checks": json.dumps([{"bucket": bucket} for bucket in buckets]),
"gh api graphql": json.dumps({"data": {"repository": {"pullRequest": {"author": {"login": "worker"}, "reviewThreads": {"nodes": list(threads)}}}}}),
"tea api repos/{owner}/{repo}/pulls?state=open&limit=50": json.dumps([{"number": 7, "merged": False, "head": {"label": "ABC-21", "sha": HEAD, "repo": {"full_name": "o/billing"}}, "base": {"repo": {"full_name": "o/billing"}}}]),
"tea api repos/{owner}/{repo}/pulls/7": json.dumps({"number": 7, "title": title, "mergeable": mergeable == "MERGEABLE", "base": {"ref": base}}),
"tea api repos/{owner}/{repo}/branches/main": '{"name": "main"}',
"tea api repos/{owner}/{repo}/commits/" + HEAD + "/status": status,
}
def at(self, folder: str):
def run(command: list[str]) -> str | Failure:
self.ran.append(folder)
self.commands.append(command)
if command[0] in ("gh", "tea") and command[0] != self.cli:
return Failure(f"{command[0]} cannot read this origin")
key = " ".join(command) if command[0] == "tea" else " ".join(command[:3] if command[0] == "gh" else command[:2])
return Failure(f"{key} failed") if self.fails and key.startswith(self.fails) else self.answers[key]
return run
def gate(self, by_human: bool = False) -> Gate | Open | Failure:
return gate("ABC-21", "/code/billing", self.at, by_human)
class GateOfAChunk(unittest.TestCase):
def test_a_pull_request_whose_checks_passed_awaits_the_approval_its_protection_requires(self):
forge = Forge()
self.assertEqual(forge.gate(), Gate("/code/billing-ABC-21", "7", APPROVAL))
self.assertEqual((forge.ran[0], set(forge.ran[1:])), ("/code/billing", {"/code/billing-ABC-21"}))
self.assertEqual(forge.commands[:4], [["git", "worktree", "list", "--porcelain"], ["git", "remote", "get-url", "origin"], ["git", "rev-parse", "HEAD"], ["git", "symbolic-ref", "--short", "refs/remotes/origin/HEAD"]])
self.assertIn(["gh", "pr", "view", "7", "--json", "isDraft,reviewDecision,author,reviews,comments"], forge.commands)
self.assertIn(["gh", "pr", "checks", "7", "--json", "bucket"], forge.commands)
self.assertTrue(any(command[:3] == ["gh", "api", "graphql"] and "number=7" in command for command in forge.commands), forge.commands)
def test_where_a_person_merges_a_pull_request_with_nothing_else_to_wait_on_awaits_that_merge(self):
gitea = "https://gitea.larvit.se/o/billing.git"
for forge in (Forge(decision="APPROVED"), Forge(decision=""), Forge(gitea)):
with self.subTest(forge.answers["git remote"]):
self.assertEqual(forge.gate(by_human=True), Gate("/code/billing-ABC-21", "7", MERGE))
self.assertEqual(Forge(decision="APPROVED").gate(by_human=False), Open("pull request #7 passed its checks and is approved"))
def test_where_a_person_merges_a_pull_request_that_no_longer_merges_into_its_base_is_news_to_level_it(self):
gitea = "https://gitea.larvit.se/o/billing.git"
again = "run chunk-pr merge again to level the branch"
self.assertEqual(Forge(decision="APPROVED", mergeable="CONFLICTING", merge_state="DIRTY").gate(by_human=True), Unmergeable(f"pull request #7 conflicts with its base branch; {again}"))
self.assertEqual(Forge(gitea, mergeable="CONFLICTING").gate(by_human=True), Unmergeable(f"pull request #7 conflicts with its base branch; {again}"))
self.assertEqual(Forge(decision="", merge_state="BEHIND").gate(by_human=True), Unmergeable(f"pull request #7 is behind its base branch, whose protection requires it up to date; {again}"))
def test_where_a_person_merges_a_pull_request_awaits_that_merge_while_github_computes_whether_it_merges(self):
self.assertEqual(Forge(decision="APPROVED", mergeable="UNKNOWN", merge_state="UNKNOWN").gate(by_human=True), Gate("/code/billing-ABC-21", "7", MERGE))
def test_where_a_person_merges_a_mergeability_that_cannot_be_read_is_a_failure(self):
forge = Forge("https://gitea.larvit.se/o/billing.git")
forge.answers["tea api repos/{owner}/{repo}/pulls/7"] = '{"number": 7, "title": "Add x"}'
self.assertEqual(forge.gate(by_human=True), Failure("tea api answered pull request 7 in a shape that can't be read: {'number': 7, 'title': 'Add x'}"))
def test_where_the_chunk_merges_nobody_reads_whether_the_pull_request_merges(self):
forge = Forge(decision="APPROVED", mergeable="CONFLICTING")
self.assertEqual(forge.gate(), Open("pull request #7 passed its checks and is approved"))
self.assertNotIn(["gh", "pr", "view", "7", "--json", "mergeable,mergeStateStatus"], forge.commands)
def test_where_a_person_merges_every_other_gate_and_all_news_stay_as_they_are(self):
same = (Forge(), Forge(buckets=("pending",)), Forge(draft=True), Forge(decision="CHANGES_REQUESTED"), Forge(threads=(thread(False, "reviewer"),)), Forge(buckets=("fail",)), Forge(pulls=""))
for forge in same:
with self.subTest(forge.answers):
self.assertEqual(forge.gate(by_human=True), forge.gate())
def test_a_review_decision_gh_prints_in_no_known_shape_is_a_failure(self):
forge = Forge()
forge.answers["gh pr view"] = "[1]"
self.assertEqual(forge.gate(), Failure("gh pr view printed no review decision: [1]"))
def test_a_pull_request_awaits_its_checks_while_one_runs_whatever_its_reviews_say(self):
for decision in ("REVIEW_REQUIRED", "APPROVED", "CHANGES_REQUESTED", ""):
with self.subTest(decision=decision):
self.assertEqual(Forge(decision=decision, buckets=("pass", "pending")).gate(), Gate("/code/billing-ABC-21", "7", CHECKS))
def test_a_gitea_pull_request_awaits_only_its_checks(self):
gitea = "https://gitea.larvit.se/o/billing.git"
self.assertEqual(Forge(gitea, status='{"state": "pending", "total_count": 1}').gate(), Gate("/code/billing-ABC-21", "7", CHECKS))
self.assertEqual(Forge(gitea).gate(), Open("pull request #7 passed its checks"))
def test_a_chunk_that_merges_into_an_epics_working_branch_reads_the_checks_that_branch_requires(self):
forge = Forge("https://gitea.larvit.se/o/billing.git", base="ABC-12")
forge.answers["git config"] = "ABC-12\n"
forge.answers["tea api repos/{owner}/{repo}/branches/ABC-12"] = '{"enable_status_check": true, "status_check_contexts": ["epic-gate"]}'
self.assertEqual(forge.gate(), Gate("/code/billing-ABC-21", "7", CHECKS))
self.assertIn(["git", "config", "--get", "branch.ABC-21.chunk-base"], forge.commands)
self.assertNotIn(["tea", "api", "repos/{owner}/{repo}/branches/main"], forge.commands)
def test_a_pull_request_the_forge_merges_into_another_branch_than_the_checkout_records_is_a_failure_naming_both(self):
differs = "pull request #7 merges into {} on the forge, but this checkout takes {} as the base of its branch; a person settles which holds, and changes the pull request's base on the forge or the branch's `chunk-base` in git's config"
for origin in ("git@github.com:o/billing.git", "https://gitea.larvit.se/o/billing.git"):
with self.subTest(origin):
recorded = Forge(origin, decision="APPROVED")
recorded.answers["git config"] = "ABC-12\n"
self.assertEqual(recorded.gate(by_human=False), Failure(differs.format("main", "ABC-12")), "no pull request into the default branch reads as one that merges without a person")
self.assertNotIn(["tea", "api", "repos/{owner}/{repo}/branches/ABC-12"], recorded.commands)
self.assertEqual(Forge(origin, decision="APPROVED", base="ABC-12").gate(by_human=True), Failure(differs.format("ABC-12", "main")))
def test_a_gitea_draft_awaits_nothing_though_its_checks_run(self):
gitea = "https://gitea.larvit.se/o/billing.git"
for title in ("WIP: Add x", "[wip] Add x"):
with self.subTest(title=title):
self.assertEqual(Forge(gitea, title=title, status='{"state": "pending", "total_count": 1}').gate(), Open("pull request #7 is a draft"))
self.assertEqual(Forge(gitea, title="Add WIP: x", status='{"state": "pending", "total_count": 1}').gate(), Gate("/code/billing-ABC-21", "7", CHECKS))
unread = Forge(gitea)
unread.answers["tea api repos/{owner}/{repo}/pulls/7"] = "[1]"
self.assertEqual(unread.gate(), Failure("tea api printed no title of pull request 7: [1]"))
def test_requested_changes_are_news_until_the_worker_pushed_or_replied_and_then_await_a_new_review(self):
asked = review("CHANGES_REQUESTED", "2026-11-18T10:00:00Z")
before, after, old = "2026-11-18T09:00:00Z", "2026-11-18T11:00:00Z", "fed" * 4
news, waits = Open("a reviewer requested changes on pull request #7"), Gate("/code/billing-ABC-21", "7", NEW_REVIEW)
cases = {
"nobody answered": ((asked,), (), news),
"the worker pushed": ((review("CHANGES_REQUESTED", "2026-11-18T10:00:00Z", on=old),), (), waits),
"the worker replied in a thread": ((asked, review("COMMENTED", after, by="worker")), (), waits),
"the worker commented": ((asked,), (comment("worker", after),), waits),
"the worker spoke only before": ((review("COMMENTED", before, by="worker"), asked, review("PENDING", None, by="worker")), (comment("worker", before),), news), # type: ignore[arg-type]
"only a reviewer spoke after": ((asked, review("COMMENTED", after)), (comment("reviewer", after),), news),
"a reviewer asked again on the new head": ((asked, review("CHANGES_REQUESTED", before, on=old), review("COMMENTED", "2026-11-18T09:30:00Z", by="worker")), (), news),
"GitHub lists no such review": ((review("APPROVED", before, on=old),), (comment("worker", after),), news),
}
for case, (reviews, comments, found) in cases.items():
with self.subTest(case=case):
self.assertEqual(Forge(decision="CHANGES_REQUESTED", reviews=reviews, comments=comments).gate(), found)
self.assertEqual(NEW_REVIEW, "a new review")
def test_a_reviewers_unanswered_comment_is_news_though_the_worker_answered_the_requested_changes(self):
forge = Forge(decision="CHANGES_REQUESTED", reviews=(review("CHANGES_REQUESTED", "2026-11-18T10:00:00Z", on="fed" * 4),), threads=(thread(False, "reviewer"),))
self.assertEqual(forge.gate(), Open("a reviewer's comment on pull request #7 waits for an answer, in a.py"))
def test_the_worker_goes_on_where_nothing_but_the_worker_moves_the_pull_request(self):
cases = {
"pull request #7 passed its checks and is approved": Forge(decision="APPROVED"),
"pull request #7 passed its checks": Forge(decision=""),
"pull request #7 is a draft": Forge(draft=True, buckets=("pending",)),
"a check on pull request 7 failed; fix it and push": Forge(buckets=("fail",)),
"a reviewer requested changes on pull request #7": Forge(decision="CHANGES_REQUESTED"),
"a reviewer's comment on pull request #7 waits for an answer, in a.py, b.py": Forge(threads=(thread(False, "reviewer", "b.py"), thread(False, "reviewer"), thread(True, "reviewer", "c.py"), thread(False, "worker", "d.py"))),
"no worktree of /code/billing has the branch ABC-21 checked out": Forge(worktrees="worktree /code/billing\nHEAD aaa\nbranch refs/heads/main\n"),
}
for news, forge in cases.items():
with self.subTest(news=news):
self.assertEqual(forge.gate(), Open(news))
self.assertEqual(Forge(pulls="").gate(), Unopened("no pull request from origin's ABC-21 is open at abcabcabcabc; it may be merged or closed already"))
def test_a_read_that_fails_is_a_failure_and_says_nothing_of_the_gate(self):
for command in ("git worktree", "git remote", "gh pr list", "gh pr view", "gh pr checks", "gh api graphql"):
with self.subTest(command=command):
self.assertEqual(Forge(fails=command).gate(), Failure(f"{command} failed"))
for endpoint in ("pulls?state=open&limit=50", "pulls/7", "branches/main"):
with self.subTest(endpoint=endpoint):
self.assertEqual(Forge("https://gitea.larvit.se/o/billing.git", fails=f"tea api repos/{{owner}}/{{repo}}/{endpoint}").gate(), Failure(f"tea api repos/{{owner}}/{{repo}}/{endpoint} failed"))
if __name__ == "__main__":
unittest.main()