Files
claude-config/bin/do_epic/e2e/test_scenario.py
T
Lilleman b7d7a1f1ea
CI / tests (pull_request) Has been cancelled
Kill the surviving mutant in the e2e's follow-up check
2026-11-19 16:58:43 +01:00

276 lines
13 KiB
Python

import dataclasses
import pathlib
import subprocess
import sys
import tempfile
import typing
import unittest
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[2]))
from do_epic.repo_pick import CLAUDE_LABEL
from failure import Failure
from do_epic import findings, finish
from do_epic.config import load
from do_epic.backend.fake import MemoryEpic
from do_epic.e2e.settings import Sandbox
from do_epic.e2e import scenario
EPIC = "SBX-1"
def done(code: int, out: str = "", err: str = "") -> subprocess.CompletedProcess[str]:
return subprocess.CompletedProcess([], code, out, err)
@dataclasses.dataclass
class Scripted:
"""A do-epic that answers each call with the next of its answers, each a function that may also change the epic as do-epic would, and notes the call."""
answers: list[typing.Callable[[], subprocess.CompletedProcess[str]]]
calls: list[tuple[tuple[str, ...], str]] = dataclasses.field(default_factory=list)
def do_epic(self, *args: str, stdin: str = "") -> subprocess.CompletedProcess[str]:
self.calls.append((args, stdin))
return self.answers.pop(0)()
class ReportTest(unittest.TestCase):
def test_sorts_each_check_into_what_held_and_what_broke(self) -> None:
report = scenario.Report()
report.check(True, "one")
report.check(False, "two")
self.assertEqual((report.held, report.broke), (["one"], ["two"]))
class ItemsFiledTest(unittest.TestCase):
def test_holds_where_the_epic_answers_the_issues_in_order(self) -> None:
report = scenario.Report()
scenario.items_filed(report, MemoryEpic(EPIC), EPIC)
self.assertEqual(report.broke, [])
self.assertEqual(len(report.held), 4)
def test_breaks_where_the_issues_come_back_in_another_order(self) -> None:
class Reversed(MemoryEpic):
def read(self) -> list[typing.Any] | Failure:
found = super().read()
return found if isinstance(found, Failure) else found[::-1]
report = scenario.Report()
scenario.items_filed(report, Reversed(EPIC), EPIC)
self.assertEqual(len(report.held), 3)
self.assertEqual(len(report.broke), 1)
self.assertIn("read the filed issues back in order", report.broke[0])
def test_breaks_naming_why_where_the_epic_refuses_every_call(self) -> None:
report = scenario.Report()
scenario.items_filed(report, MemoryEpic(EPIC, fails="Jira is down"), EPIC)
self.assertEqual(report.held, [])
self.assertEqual(report.broke[:3], [f"file {what} in the epic: Jira is down" for what in ("an item", "a second item", "a sub-task")])
self.assertEqual(report.broke[3], "read the epic's issues after filing: Jira is down")
self.assertIn("found []", report.broke[4])
class FindingFiledTest(unittest.TestCase):
def filing(self, store: MemoryEpic, labels: tuple[str, ...] = (CLAUDE_LABEL,), code: int = 0, out: str = "10:00 Filed SBX-100") -> typing.Callable[[], subprocess.CompletedProcess[str]]:
def answer() -> subprocess.CompletedProcess[str]:
store.file(EPIC, "Sandbox finding", "Why the finding matters.", labels)
return done(code, out)
return answer
def test_holds_where_do_epic_files_the_finding_with_its_label_and_detail(self) -> None:
store = MemoryEpic(EPIC)
machine = Scripted([self.filing(store)])
report = scenario.Report()
scenario.finding_filed(report, machine, store, EPIC)
self.assertEqual((report.broke, len(report.held)), ([], 2))
self.assertEqual(machine.calls, [((EPIC, findings.FILE, "Sandbox finding", findings.LABEL, CLAUDE_LABEL), "Why the finding matters.\n")])
def test_breaks_where_do_epic_fails_and_files_nothing(self) -> None:
report = scenario.Report()
scenario.finding_filed(report, Scripted([lambda: done(1, err="no")]), MemoryEpic(EPIC), EPIC)
self.assertEqual(report.held, [])
self.assertIn("exit 1: no", report.broke[0])
self.assertIn("found []", report.broke[1])
def test_breaks_where_the_finding_lacks_the_label_asked_for(self) -> None:
store = MemoryEpic(EPIC)
report = scenario.Report()
scenario.finding_filed(report, Scripted([self.filing(store, labels=())]), store, EPIC)
self.assertEqual(len(report.held), 1)
self.assertEqual(len(report.broke), 1)
self.assertIn("with its label", report.broke[0])
def test_breaks_where_do_epic_exits_cleanly_but_says_nothing_of_filing(self) -> None:
store = MemoryEpic(EPIC)
report = scenario.Report()
scenario.finding_filed(report, Scripted([self.filing(store, out="")]), store, EPIC)
self.assertEqual(len(report.held), 1)
self.assertIn("files the finding and says so", report.broke[0])
class FollowUpFiledTest(unittest.TestCase):
def store(self) -> MemoryEpic:
store = MemoryEpic(EPIC)
store.file(EPIC, "Sandbox finding", "", (CLAUDE_LABEL,))
return store
def following(self, store: MemoryEpic, flipped: bool = False) -> typing.Callable[[], subprocess.CompletedProcess[str]]:
def answer() -> subprocess.CompletedProcess[str]:
key = store.file(EPIC, "Sandbox follow-up finding", "", (CLAUDE_LABEL,), None if flipped else "SBX-100")
if flipped:
store.change("SBX-100", blockers=(str(key),))
return done(0, "Filed SBX-101 in the epic SBX-1. It is blocked by SBX-100.")
return answer
def test_holds_where_the_follow_up_is_blocked_by_the_first_finding(self) -> None:
store = self.store()
machine = Scripted([self.following(store)])
report = scenario.Report()
scenario.follow_up_filed(report, machine, store, EPIC)
self.assertEqual((report.broke, len(report.held)), ([], 2))
self.assertEqual(machine.calls, [((EPIC, findings.FILE, "Sandbox follow-up finding", findings.LABEL, CLAUDE_LABEL, findings.BLOCKED_BY, "SBX-100"), "Waits for the first finding.\n")])
def test_breaks_where_the_link_points_the_wrong_way(self) -> None:
store = self.store()
report = scenario.Report()
scenario.follow_up_filed(report, Scripted([self.following(store, flipped=True)]), store, EPIC)
self.assertEqual(len(report.broke), 1)
self.assertIn("not the other way round", report.broke[0])
def test_breaks_where_do_epic_exits_cleanly_but_says_nothing_of_the_blocker(self) -> None:
store = self.store()
report = scenario.Report()
scenario.follow_up_filed(report, Scripted([lambda: (self.following(store)(), done(0, "Filed SBX-101 in the epic SBX-1."))[1]]), store, EPIC)
self.assertEqual(len(report.broke), 1)
self.assertIn("says it is blocked", report.broke[0])
def test_breaks_where_do_epic_fails_and_files_nothing(self) -> None:
report = scenario.Report()
scenario.follow_up_filed(report, Scripted([lambda: done(1, err="no")]), self.store(), EPIC)
self.assertEqual(len(report.broke), 2)
self.assertIn("exit 1: no", report.broke[0])
class DecisionRecordedTest(unittest.TestCase):
SECTIONS: typing.ClassVar[dict[str, str | Failure]] = {"Decisions": "### Sandbox decision\nThe text of the decision.", "Goals": "1. Prove do-epic's commands work."}
def recorded(self, answer: subprocess.CompletedProcess[str], **sections: str | Failure) -> tuple[scenario.Report, Scripted]:
machine = Scripted([lambda: answer])
report = scenario.Report()
found = {**self.SECTIONS, **sections}
scenario.decision_recorded(report, machine, lambda name: found[name], EPIC)
return report, machine
def test_holds_where_the_decision_is_in_the_description_and_the_goals_are_as_they_were(self) -> None:
report, machine = self.recorded(done(0, "10:00 Recorded the decision"))
self.assertEqual((report.broke, len(report.held)), ([], 3))
self.assertEqual(machine.calls, [((EPIC, findings.RECORD_DECISION, "Sandbox decision"), "The text of the decision.\n")])
def test_breaks_where_the_decision_is_not_recorded(self) -> None:
report, _ = self.recorded(done(1, err="no"), Decisions="")
self.assertEqual(len(report.held), 1)
self.assertIn("exit 1: no", report.broke[0])
self.assertIn("holds the decision under ## Decisions", report.broke[1])
def test_breaks_where_only_half_of_the_decision_is_recorded(self) -> None:
report, _ = self.recorded(done(0, "Recorded the decision"), Decisions="### Sandbox decision")
self.assertEqual(len(report.broke), 1)
def test_breaks_where_the_goals_changed(self) -> None:
report, _ = self.recorded(done(0, "Recorded the decision"), Goals="")
self.assertEqual(len(report.broke), 1)
self.assertIn("leaves the description's ## Goals", report.broke[0])
def test_breaks_without_raising_where_the_description_cannot_be_read(self) -> None:
report, _ = self.recorded(done(0, "Recorded the decision"), Decisions=Failure("Jira is down"), Goals=Failure("Jira is down"))
self.assertEqual((len(report.held), len(report.broke)), (1, 2))
class ClosedTest(unittest.TestCase):
def closing(self, store: MemoryEpic, early: typing.Callable[[], subprocess.CompletedProcess[str]] | None = None, late: typing.Callable[[], subprocess.CompletedProcess[str]] | None = None) -> scenario.Report:
def closes() -> subprocess.CompletedProcess[str]:
store.close("Everything the sandbox epic held is done.")
return done(0, "10:00 Posted the summary and closed it")
store.file(EPIC, "An open issue", "", ())
report = scenario.Report()
self.machine = Scripted([early or (lambda: done(1, err="SBX-100 is still open")), late or closes])
scenario.closed(report, self.machine, store, EPIC)
return report
def test_holds_where_do_epic_refuses_while_an_issue_is_open_and_closes_the_epic_after(self) -> None:
store = MemoryEpic(EPIC)
report = self.closing(store)
self.assertEqual((report.broke, len(report.held)), ([], 4))
self.assertEqual(store.comments[EPIC], ["Everything the sandbox epic held is done."])
self.assertEqual(self.machine.calls, [((EPIC, finish.CLOSE), "Too early.\n"), ((EPIC, finish.CLOSE), "Everything the sandbox epic held is done.\n")])
self.assertTrue(all(issue.done for issue in store.issues))
def test_breaks_where_the_early_close_closes_the_epic(self) -> None:
store = MemoryEpic(EPIC)
def closes_early() -> subprocess.CompletedProcess[str]:
store.close("Too early.")
return done(1, err="SBX-100 is still open")
report = self.closing(store, early=closes_early)
self.assertEqual(len(report.broke), 1)
self.assertIn("refuses while issues are open", report.broke[0])
def test_breaks_where_the_late_close_exits_cleanly_but_leaves_the_epic_open(self) -> None:
report = self.closing(MemoryEpic(EPIC), late=lambda: done(0, "Posted the summary and closed it"))
self.assertEqual(len(report.broke), 1)
self.assertIn("Jira counts the epic as done (found False)", report.broke[0])
def test_breaks_where_the_late_close_fails(self) -> None:
report = self.closing(MemoryEpic(EPIC), late=lambda: done(1, err="refused"))
self.assertIn("exit 1: refused", report.broke[0])
def test_breaks_without_raising_where_the_epic_cannot_be_read(self) -> None:
report = self.closing(MemoryEpic(EPIC, fails="Jira is down"))
self.assertEqual(len(report.held), 1)
self.assertIn("read the epic's issues before finishing them: Jira is down", report.broke)
self.assertIn("Jira counts the epic as done (found Failure(reason='Jira is down'))", report.broke)
class MachineTest(unittest.TestCase):
def setUp(self) -> None:
made = tempfile.TemporaryDirectory()
self.addCleanup(made.cleanup)
self.folder = pathlib.Path(made.name).resolve()
self.env_file = self.folder / "jira.env"
self.env_file.write_text("ATLASSIAN_EMAIL=a@example.com\nATLASSIAN_API_TOKEN=secret\n", encoding="utf-8")
self.env_file.chmod(0o600)
self.sandbox = Sandbox("https://acme.atlassian.net", self.env_file, "SBX")
def script(self, body: str) -> pathlib.Path:
launcher = self.folder / "launcher"
launcher.write_text(f"#!/bin/sh\n{body}\n", encoding="utf-8")
launcher.chmod(0o755)
return launcher
def test_writes_settings_that_do_epic_loads_and_that_point_at_the_sandbox(self) -> None:
home = self.folder / "home"
machine = scenario.machine_made(home, self.sandbox, pathlib.Path("do-epic"), {"A": "b"})
assert not isinstance(machine, Failure), machine
self.assertEqual((machine.home, machine.launcher, machine.env), (home, pathlib.Path("do-epic"), {"A": "b"}))
config = load(home)
assert not isinstance(config, Failure), config
self.assertEqual((config.jira_url, config.env_file, config.repo_rule), ("https://acme.atlassian.net", self.env_file, scenario.RULE))
self.assertEqual((config.finish.transition, config.finish.status) if config.finish else None, ("Done", "Done"))
def test_runs_do_epic_in_the_given_environment_with_the_machines_home_and_the_words_and_stdin_given(self) -> None:
machine = scenario.Machine(self.folder, self.script('echo "$HOME $KEPT $*"; cat'), {"KEPT": "yes", "HOME": "/elsewhere"})
finished = machine.do_epic("ABC-1", "--file", stdin="detail")
self.assertEqual((finished.returncode, finished.stdout), (0, f"{self.folder} yes ABC-1 --file\ndetail"))
def test_answers_a_failing_do_epic_with_its_exit_and_output(self) -> None:
finished = scenario.Machine(self.folder, self.script("echo out; echo err >&2; exit 3"), {}).do_epic("ABC-1")
self.assertEqual((finished.returncode, finished.stdout, finished.stderr), (3, "out\n", "err\n"))
def test_stops_a_do_epic_that_runs_over_its_time_and_says_so(self) -> None:
finished = scenario.Machine(self.folder, self.script("exec sleep 30"), {}, timeout=0.2).do_epic("ABC-1")
self.assertEqual(finished.returncode, 124)
self.assertIn("ran over 0.2 seconds", finished.stderr)
self.assertIn("ABC-1", finished.stderr)