"""What the e2e command does to a sandbox epic: runs `do-epic --file`, `--record-decision` and `--close-epic`, each as a process of its own against the real Jira site, and checks what the site holds after each.""" import dataclasses import pathlib import subprocess import tempfile import typing from do_epic.repo_pick import CLAUDE_LABEL, HUMAN_LABEL from project_config.settings import Epic, Finish from do_epic import epic_record, findings, finish from record_store.jira_description import EpicDescription from do_epic.backend.protocol import EpicIssue, EpicStore from failure import Failure from do_epic.e2e.sandbox import FINISH_STATUS, FINISH_TRANSITION from do_epic.backend.jira import JiraEpic from model_routing.ladder_file import read_ladder, SHIPPED from do_epic.e2e.settings import Sandbox # Seconds one do-epic command may take. TIMEOUT = 300 RULE = "A label that equals a repo's name names that repo.\n" @dataclasses.dataclass class Report: """What the scenario found: the lines of what held, and of what didn't.""" held: list[str] = dataclasses.field(default_factory=list) broke: list[str] = dataclasses.field(default_factory=list) def check(self, holds: bool, what: str) -> None: (self.held if holds else self.broke).append(what) class DoEpic(typing.Protocol): """What the steps run do-epic through: a Machine, or a stand-in in a test.""" def do_epic(self, *args: str, stdin: str = "") -> subprocess.CompletedProcess[str]: """Runs `do-epic args`, feeding it stdin.""" @dataclasses.dataclass(frozen=True) class Machine: """A home folder of its own, holding the do-epic settings that point at the sandbox, so that do-epic writes its state there. The sandbox's env-file stays where it is. do-epic runs in env with HOME set to the home folder, and is stopped after timeout seconds.""" home: pathlib.Path launcher: pathlib.Path env: typing.Mapping[str, str] timeout: float = TIMEOUT def do_epic(self, *args: str, stdin: str = "") -> subprocess.CompletedProcess[str]: env = {**self.env, "HOME": str(self.home)} try: return subprocess.run([str(self.launcher), *args], input=stdin, env=env, capture_output=True, text=True, timeout=self.timeout, check=False) except subprocess.TimeoutExpired: return subprocess.CompletedProcess(args, 124, "", f"do-epic {' '.join(args)} ran over {self.timeout} seconds and was stopped") def machine_made(home: pathlib.Path, sandbox: Sandbox, launcher: pathlib.Path, env: typing.Mapping[str, str]) -> Machine | Failure: folder = home / ".claude-projects" / "do-epic" folder.mkdir(parents=True) ladder = read_ladder(SHIPPED) if isinstance(ladder, Failure): return ladder (folder / "config.toml").write_text( f'jira-url = "{sandbox.jira_url}"\nenv-file = "{sandbox.env_file}"\nrepoless-model = "{ladder.rungs[0].model}"\n' f'jira-finish-transition = "{FINISH_TRANSITION}"\njira-finish-status = "{FINISH_STATUS}"\n', encoding="utf-8") (folder / "repo-rule.md").write_text(RULE, encoding="utf-8") return Machine(home, launcher, env) def run(sandbox: Sandbox, epic_key: str, launcher: pathlib.Path, env: typing.Mapping[str, str]) -> Report: """Drives do-epic against the epic epic_key in the sandbox, an epic with no issue yet.""" report = Report() epic = Epic(sandbox.jira_url, epic_key) store = JiraEpic(epic, sandbox.env_file, finish=Finish(FINISH_TRANSITION, FINISH_STATUS)) record = epic_record.EpicRecord(EpicDescription(epic, sandbox.env_file)) with tempfile.TemporaryDirectory() as home: machine = machine_made(pathlib.Path(home).resolve(), sandbox, launcher, env) if isinstance(machine, Failure): report.check(False, f"set up do-epic's settings: {machine.reason}") return report items_filed(report, store, epic_key) finding_filed(report, machine, store, epic_key) follow_up_filed(report, machine, store, epic_key) decision_recorded(report, machine, record.section, epic_key) closed(report, machine, store, epic_key) return report def read(report: Report, store: EpicStore, what: str) -> list[EpicIssue]: issues = store.read() if isinstance(issues, Failure): report.check(False, f"read the epic's issues {what}: {issues.reason}") return [] return issues def items_filed(report: Report, store: EpicStore, epic_key: str) -> None: """Files an item, its sub-task and a second item in the epic, and checks that a read answers them in the tracker's order.""" parent = store.file(epic_key, "Sandbox item with a sub-task", "The first item.", (HUMAN_LABEL,)) second = store.file(epic_key, "Sandbox second item", "", ()) child = store.file(parent, "Sandbox sub-task", "", ()) if isinstance(parent, str) else parent for what, key in (("an item", parent), ("a second item", second), ("a sub-task", child)): report.check(isinstance(key, str), f"file {what} in the epic" + (f": {key.reason}" if isinstance(key, Failure) else "")) issues = read(report, store, "after filing") report.check([issue.summary for issue in issues] == ["Sandbox item with a sub-task", "Sandbox sub-task", "Sandbox second item"], f"read the filed issues back in order, the sub-task after its parent (found {[issue.summary for issue in issues]})") def finding_filed(report: Report, machine: DoEpic, store: EpicStore, epic_key: str) -> None: done = machine.do_epic(epic_key, findings.FILE, "Sandbox finding", findings.LABEL, CLAUDE_LABEL, stdin="Why the finding matters.\n") report.check(done.returncode == 0 and " Filed " in done.stdout, f"`do-epic {findings.FILE}` files the finding and says so (exit {done.returncode}: {done.stdout.strip() or done.stderr.strip()})") found = [issue for issue in read(report, store, "after the finding") if issue.summary == "Sandbox finding"] report.check(len(found) == 1 and found[0].labels == (CLAUDE_LABEL,) and found[0].detail == "Why the finding matters." and found[0].by_loop, f"the finding is an issue of the epic with its label, its detail and the loop's mark (found {found})") def follow_up_filed(report: Report, machine: DoEpic, store: EpicStore, epic_key: str) -> None: """Files a follow-up finding that waits for the first finding, and checks that Jira reads the link in that direction.""" first = next((issue.key for issue in read(report, store, "before the follow-up") if issue.summary == "Sandbox finding"), "") done = machine.do_epic(epic_key, findings.FILE, "Sandbox follow-up finding", findings.LABEL, CLAUDE_LABEL, findings.BLOCKED_BY, first, stdin="Waits for the first finding.\n") report.check(done.returncode == 0 and f"It is blocked by {first}." in done.stdout, f"`do-epic {findings.FILE} {findings.BLOCKED_BY}` files the follow-up and says it is blocked (exit {done.returncode}: {done.stdout.strip() or done.stderr.strip()})") issues = read(report, store, "after the follow-up") waiting = [issue for issue in issues if issue.summary == "Sandbox follow-up finding"] report.check(len(waiting) == 1 and waiting[0].blockers == (first,) and not any(issue.blockers for issue in issues if issue.key == first), f"the follow-up is blocked by the first finding, not the other way round (found {[(issue.key, issue.blockers) for issue in issues if issue.key == first or issue.summary == 'Sandbox follow-up finding']})") def decision_recorded(report: Report, machine: DoEpic, section: typing.Callable[[str], str | Failure], epic_key: str) -> None: done = machine.do_epic(epic_key, findings.RECORD_DECISION, "Sandbox decision", stdin="The text of the decision.\n") report.check(done.returncode == 0 and "Recorded the decision" in done.stdout, f"`do-epic {findings.RECORD_DECISION}` records the decision and says so (exit {done.returncode}: {done.stdout.strip() or done.stderr.strip()})") decisions = section("Decisions") report.check(isinstance(decisions, str) and "Sandbox decision" in decisions and "The text of the decision." in decisions, f"the epic's description holds the decision under ## Decisions (found {decisions!r})") goals = section("Goals") report.check(isinstance(goals, str) and "Prove do-epic's commands" in goals, f"the decision leaves the description's ## Goals as it was (found {goals!r})") def closed(report: Report, machine: DoEpic, store: EpicStore, epic_key: str) -> None: early = machine.do_epic(epic_key, finish.CLOSE, stdin="Too early.\n") still = store.closed() report.check(early.returncode == 1 and " open" in early.stderr and still is False, f"`do-epic {finish.CLOSE}` refuses while issues are open and leaves the epic open (exit {early.returncode}: {early.stderr.strip()}; epic closed: {still!r})") for issue in read(report, store, "before finishing them"): done = store.finish_issue(issue.key) report.check(done is None, f"finish {issue.key}" + (f": {done.reason}" if isinstance(done, Failure) else "")) late = machine.do_epic(epic_key, finish.CLOSE, stdin="Everything the sandbox epic held is done.\n") report.check(late.returncode == 0 and "closed it" in late.stdout, f"`do-epic {finish.CLOSE}` posts the summary and closes the epic (exit {late.returncode}: {late.stdout.strip() or late.stderr.strip()})") now = store.closed() report.check(now is True, f"Jira counts the epic as done (found {now!r})")