207 lines
14 KiB
Python
207 lines
14 KiB
Python
import contextlib
|
|
import dataclasses
|
|
import fcntl
|
|
import io
|
|
import pathlib
|
|
import sys
|
|
import unittest
|
|
|
|
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
|
|
|
|
from do_epic.test_main import DESCRIPTION, DoEpicTest
|
|
from do_epic.epic_record import EpicRecord
|
|
from failure import Failure
|
|
from loop_effects.doubles import FakeEffects
|
|
from do_epic import findings, main
|
|
from record_store.held import Held
|
|
from do_epic.backend.fake import MemoryEpic
|
|
from loop_state.paths import paths_from
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class WrittenDescription:
|
|
"""Stands in for an epic's description in Jira that takes writes: kept holds each text it held, the last one now. It refuses a write that claims the human's approval, and notes in locked whether lock was held during each write."""
|
|
kept: list[str]
|
|
lock: pathlib.Path
|
|
locked: list[bool] = dataclasses.field(default_factory=list)
|
|
approved: frozenset[str] | None = frozenset()
|
|
|
|
@property
|
|
def text(self) -> str:
|
|
return self.kept[-1]
|
|
|
|
def read(self) -> str | Failure:
|
|
return self.text
|
|
|
|
def held(self) -> Held | Failure:
|
|
return Held(self.text)
|
|
|
|
def write(self, text: str, version: str | None) -> Held | Failure:
|
|
if self.approved != frozenset():
|
|
return Failure(f"approved is {self.approved!r}")
|
|
with open(self.lock, "a") as other:
|
|
try:
|
|
fcntl.flock(other, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
|
self.locked.append(False)
|
|
except BlockingIOError:
|
|
self.locked.append(True)
|
|
self.kept.append(text)
|
|
return Held(text)
|
|
|
|
|
|
class FindingsTest(DoEpicTest):
|
|
def setUp(self) -> None:
|
|
super().setUp()
|
|
self.store = MemoryEpic("ABC-12", list(self.issues))
|
|
self.written = WrittenDescription([DESCRIPTION], self.home / ".local/state/do-epic/ABC-12/decisions.lock")
|
|
self.keys: list[str] = []
|
|
|
|
def backend(self, config: object, key: str) -> tuple[MemoryEpic, EpicRecord]:
|
|
self.keys.append(key)
|
|
return self.store, EpicRecord(self.written) # type: ignore[arg-type]
|
|
|
|
def run_with(self, argv: list[str], stdin: str = "") -> tuple[int, str, str, FakeEffects]:
|
|
out, err, fake = io.StringIO(), io.StringIO(), FakeEffects()
|
|
with contextlib.redirect_stdout(out), contextlib.redirect_stderr(err):
|
|
code = main.main(argv, {}, paths_from(self.home), pathlib.Path("/b/do-epic"), fake.effects(), self.backend, io.StringIO(stdin))
|
|
# Without the time each line of do-epic opens with.
|
|
return code, out.getvalue().replace("<1790000000> ", ""), err.getvalue().replace("<1790000000> ", ""), fake
|
|
|
|
|
|
class FilingAFinding(FindingsTest):
|
|
def test_files_an_issue_under_the_epic_with_the_detail_piped_in_and_doesnt_start_anything(self) -> None:
|
|
code, out, err, fake = self.run_with(["ABC-12", "--file", "Retry the import on a 503"], "The client times out.\n")
|
|
self.assertEqual((code, out, err), (0, "Filed ABC-102 in the epic ABC-12. It has no label that says where it is worked, so the next `do-epic ABC-12` picks one: `repo:<name>` for the repo that works it, `do-epic-claude` for work of Claude's that changes no repo, or `do-epic-human` for a task of the human's.\n", ""))
|
|
self.assertEqual(self.keys, ["ABC-12"])
|
|
filed = self.store.issues[-1]
|
|
self.assertEqual((filed.key, filed.summary, filed.detail, filed.labels, filed.parent, filed.by_loop), ("ABC-102", "Retry the import on a 503", "The client times out.", (), "ABC-12", True))
|
|
self.assertEqual(fake.calls, [], "filing needs no tmux, clone or claude")
|
|
|
|
def test_a_label_says_where_the_issue_is_worked(self) -> None:
|
|
for argv, label in ((["ABC-12", "--file", "Retry", "--label", "repo:api"], "repo:api"), (["ABC-12", "--file", "--label", "do-epic-claude", "Retry"], "do-epic-claude"), (["ABC-12", "--file", "Retry", "--label", "do-epic-human"], "do-epic-human")):
|
|
with self.subTest(argv=argv):
|
|
code, out, _, _ = self.run_with(argv)
|
|
filed = self.store.issues[-1]
|
|
self.assertEqual((code, out, filed.labels, filed.detail), (0, f"Filed {filed.key} in the epic ABC-12.\n", (label,), ""))
|
|
|
|
def test_a_label_of_the_teams_says_where_the_issue_is_worked(self) -> None:
|
|
with (self.home / ".claude-projects/do-epic/config.toml").open("a", encoding="utf-8") as config:
|
|
config.write('\n[label-repos]\ntwu-4 = "tenant-web-ui-4"\n')
|
|
code, out, _, _ = self.run_with(["ABC-12", "--file", "Retry", "--label", "twu-4"])
|
|
filed = self.store.issues[-1]
|
|
self.assertEqual((code, out, filed.labels), (0, f"Filed {filed.key} in the epic ABC-12.\n", ("twu-4",)))
|
|
code, _, err, _ = self.run_with(["ABC-12", "--file", "Retry", "--label", "twu-4", "--label", "repo:api"])
|
|
self.assertEqual((code, err), (1, "cannot file the finding in the epic ABC-12: twu-4 and repo:api each say who works it; write one of them\n"))
|
|
_, _, err, _ = self.run_with(["ABC-12", "--file", "Retry", "--label", "twu"])
|
|
self.assertTrue(err.endswith("or `do-epic-human` for a task of the human's (or the team's `twu-4`)\n"), err)
|
|
|
|
def test_a_release_review_takes_its_label_beside_its_repo(self) -> None:
|
|
code, out, _, _ = self.run_with(["ABC-12", "--file", "Review api", "--label", "repo:api", "--label", "do-epic-release-review"])
|
|
filed = self.store.issues[-1]
|
|
self.assertEqual((code, out, filed.labels), (0, f"Filed {filed.key} in the epic ABC-12.\n", ("do-epic-release-review", "repo:api")))
|
|
code, out, _, _ = self.run_with(["ABC-12", "--file", "Review api", "--label", "do-epic-release-review"])
|
|
self.assertEqual((code, out), (0, f"Filed {self.store.issues[-1].key} in the epic ABC-12. It has no label that says where it is worked, so the next `do-epic ABC-12` picks one: `repo:<name>` for the repo that works it, `do-epic-claude` for work of Claude's that changes no repo, or `do-epic-human` for a task of the human's.\n"))
|
|
|
|
def test_files_the_next_step_of_a_change_blocked_by_the_issue_before_it(self) -> None:
|
|
code, out, err, _ = self.run_with(["ABC-12", "--file", "Change it", "--label", "repo:api", "--blocked-by", "ABC-20"], "Detail.\n")
|
|
filed = self.store.issues[-1]
|
|
self.assertEqual((code, out, err), (0, f"Filed {filed.key} in the epic ABC-12. It is blocked by ABC-20.\n", ""))
|
|
self.assertEqual((filed.parent, filed.blockers, filed.labels), ("ABC-12", ("ABC-20",), ("repo:api",)))
|
|
|
|
def test_files_a_sub_task_under_a_child_issue_of_the_epic(self) -> None:
|
|
code, out, _, _ = self.run_with(["ABC-12", "--file", "--parent", "ABC-20", "Change it", "--label", "repo:api", "--blocked-by", "ABC-21"])
|
|
filed = self.store.issues[-1]
|
|
self.assertEqual((code, out, filed.parent, filed.blockers), (0, f"Filed {filed.key} under ABC-20 in the epic ABC-12. It is blocked by ABC-21.\n", "ABC-20", ("ABC-21",)))
|
|
|
|
def test_refuses_a_blocker_or_parent_outside_the_epic(self) -> None:
|
|
for argv, reason in ((["--blocked-by", "ZZZ-1"], "ZZZ-1 is neither a child issue of the epic ABC-12 nor a sub-task of one, so it cannot block a new issue"), (["--parent", "ZZZ-1"], "ZZZ-1 is neither the epic ABC-12 nor one of its child issues, so nothing can be filed under it")):
|
|
with self.subTest(argv=argv):
|
|
code, out, err, _ = self.run_with(["ABC-12", "--file", "Retry", "--label", "repo:api", *argv])
|
|
self.assertEqual((code, out, err, len(self.store.issues)), (1, "", f"cannot file the finding in the epic ABC-12: {reason}\n", 2))
|
|
|
|
def test_refuses_a_label_the_scheduler_doesnt_read(self) -> None:
|
|
for label in ("api", "repo:", "repo:a b"):
|
|
with self.subTest(label=label):
|
|
code, out, err, _ = self.run_with(["ABC-12", "--file", "Retry", "--label", label])
|
|
self.assertEqual((code, out, len(self.store.issues)), (1, "", 2))
|
|
self.assertEqual(err, f"cannot file the finding in the epic ABC-12: do-epic doesn't read the label {label!r}; write `repo:<name>` for the repo that works it, `do-epic-claude` for work of Claude's that changes no repo, or `do-epic-human` for a task of the human's\n")
|
|
|
|
def test_refuses_two_labels_that_each_say_who_works_the_issue(self) -> None:
|
|
code, out, err, _ = self.run_with(["ABC-12", "--file", "Retry", "--label", "repo:api", "--label", "repo:web"])
|
|
self.assertEqual((code, out, len(self.store.issues)), (1, "", 2))
|
|
self.assertEqual(err, "cannot file the finding in the epic ABC-12: repo:api and repo:web each say who works it; write one of them\n")
|
|
|
|
def test_says_why_an_issue_was_not_filed(self) -> None:
|
|
self.store.fails = "Jira answered 401"
|
|
code, _, err, _ = self.run_with(["ABC-12", "--file", "Retry"])
|
|
self.assertEqual((code, err), (1, "cannot file the finding in the epic ABC-12: Jira answered 401\n"))
|
|
|
|
def test_stops_where_the_machines_settings_cannot_be_read(self) -> None:
|
|
(self.home / ".claude-projects/do-epic/config.toml").unlink()
|
|
code, out, err, _ = self.run_with(["ABC-12", "--file", "Retry"])
|
|
self.assertEqual((code, out, len(self.store.issues)), (1, "", 2))
|
|
self.assertIn("config.toml", err)
|
|
|
|
def test_prints_the_help_for_a_filing_spelt_wrong(self) -> None:
|
|
for argv in (["ABC-12", "--file", "a", "--blocked-by", ""], ["ABC-12", "--file", "a", "--parent", " "], ["ABC-12", "--file"], ["ABC-12", "--file", "a", "b"], ["ABC-12", "--file", "a", "--label"], ["ABC-12", "--file", "a", "--labels", "x"], ["--file", "ABC-12", "a"], ["ABC-12", "--file", "--label", "x"], ["ABC-12", "--file", "--lable"], ["ABC-12", "--file", "a", "--blocked-by"], ["ABC-12", "--file", "a", "--blocked-by", "ABC-100", "--blocked-by", "ABC-101"], ["ABC-12", "--file", "a", "--parent", "ABC-100", "--parent", "ABC-101"], ["ABC-12", "--file", "a", "--parent", "--label", "x"], ["ABC-12", "--record-decision", "a", "--blocked-by", "ABC-100"], ["ABC-12", "--file", "a", "--label", "--x"], ["--chunk", "ABC-20", "ABC-12", "--file", "a"]):
|
|
with self.subTest(argv=argv):
|
|
code, out, err, _ = self.run_with(argv)
|
|
self.assertEqual((code, out, err, len(self.store.issues)), (1, "", main.HELP + "\n", 2))
|
|
|
|
|
|
class Instructions(unittest.TestCase):
|
|
def test_say_nothing_of_a_next_pull_request_without_the_chunks_issue(self) -> None:
|
|
told = findings.instructions("ABC-12", pathlib.Path("/b/do-epic"), "Swedish", {})
|
|
self.assertTrue(told.endswith("its text on stdin. Write the issue and the decision in Swedish, the epic's language."), told)
|
|
self.assertNotIn("--blocked-by", told)
|
|
|
|
|
|
class RecordingADecision(FindingsTest):
|
|
def test_appends_the_entry_to_the_descriptions_decisions(self) -> None:
|
|
code, out, err, fake = self.run_with(["ABC-12", "--record-decision", "Imports retry three times"], "2026-11-18, the human's answer. Valid while the client's API rate-limits.\n")
|
|
self.assertEqual((code, out, err, fake.calls), (0, "Recorded the decision in the epic ABC-12's description.\n", "", []))
|
|
self.assertEqual(self.written.text, DESCRIPTION + "\n## Decisions\n\n### Imports retry three times\n\n2026-11-18, the human's answer. Valid while the client's API rate-limits.\n")
|
|
self.assertEqual(self.run_with(["ABC-12", "--record-decision", "Merges squash"], "Always.")[0], 0)
|
|
self.assertTrue(self.written.text.endswith("rate-limits.\n\n### Merges squash\n\nAlways.\n"), self.written.text)
|
|
self.assertIn("## Goals\nShip the import.\n\n## Audience\nThe client's team.\n", self.written.text)
|
|
self.assertEqual((self.keys, self.written.locked), (["ABC-12", "ABC-12"], [True, True]))
|
|
|
|
def test_recording_a_decision_twice_keeps_one(self) -> None:
|
|
self.run_with(["ABC-12", "--record-decision", "Merges squash"], "Always.\n\n```\n### not a title\n```\n")
|
|
self.run_with(["ABC-12", "--record-decision", "Imports retry"], "Three times.")
|
|
held = self.written.text
|
|
code, out, err, _ = self.run_with(["ABC-12", "--record-decision", " Merges squash "], "Always.\n\n```\n### not a title\n```")
|
|
self.assertEqual((code, out, err, self.written.text), (0, "The epic ABC-12's description already holds that decision.\n", "", held))
|
|
self.assertEqual(len(self.written.kept), 3)
|
|
|
|
def test_refuses_another_decision_under_a_title_the_description_holds(self) -> None:
|
|
self.run_with(["ABC-12", "--record-decision", "Merges squash"], "Always.")
|
|
held = self.written.text
|
|
code, out, err, _ = self.run_with(["ABC-12", "--record-decision", "Merges squash"], "Always")
|
|
self.assertEqual((code, out, self.written.text), (1, "", held))
|
|
self.assertEqual(err, "cannot record the decision in the epic ABC-12's description: ## Decisions already holds another decision titled 'Merges squash': give this one a title of its own\n")
|
|
|
|
def test_says_why_the_lock_was_not_taken(self) -> None:
|
|
self.written.lock.parent.parent.mkdir(parents=True)
|
|
self.written.lock.parent.write_text("")
|
|
code, out, err, _ = self.run_with(["ABC-12", "--record-decision", "Merges squash"], "Always.")
|
|
self.assertEqual((code, out, self.written.text), (1, "", DESCRIPTION))
|
|
self.assertRegex(err, f"^cannot record the decision in the epic ABC-12's description: cannot lock {self.written.lock.parent}: \\w")
|
|
|
|
def test_refuses_a_decision_without_a_title_or_a_body(self) -> None:
|
|
for argv, stdin, reason in ((["ABC-12", "--record-decision", "Merges squash"], " \n", "pipe the decision's text in"), (["ABC-12", "--record-decision", "two\nlines"], "Always.", "the title must be one line of text"), (["ABC-12", "--record-decision", " "], "Always.", "the title must be one line of text"), (["ABC-12", "--record-decision", "Merges squash"], "## Goals\nNone.", "the body of ## Decisions holds a level 1 or 2 heading, which would start another section")):
|
|
with self.subTest(argv=argv, stdin=stdin):
|
|
code, out, err, _ = self.run_with(argv, stdin)
|
|
self.assertEqual((code, out, err, self.written.text), (1, "", f"cannot record the decision in the epic ABC-12's description: {reason}\n", DESCRIPTION))
|
|
|
|
def test_prints_the_help_for_a_decision_spelt_wrong(self) -> None:
|
|
for argv in (["ABC-12", "--record-decision"], ["ABC-12", "--record-decision", "a", "b"], ["ABC-12", "--decide", "a"]):
|
|
with self.subTest(argv=argv):
|
|
code, out, err, _ = self.run_with(argv, "Always.")
|
|
self.assertEqual((code, out, err, self.written.text), (1, "", main.HELP + "\n", DESCRIPTION))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
import unittest
|
|
unittest.main()
|