173 lines
10 KiB
Python
173 lines
10 KiB
Python
import contextlib
|
|
import dataclasses
|
|
import io
|
|
import json
|
|
import pathlib
|
|
import subprocess
|
|
import sys
|
|
|
|
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
|
|
|
|
from project_config.test_project_config import configured
|
|
from do_epic.test_main import Description, DoEpicTest, structured as says
|
|
from do_epic.backend.protocol import EpicIssue
|
|
from do_epic.epic_record import EpicRecord
|
|
from failure import Failure
|
|
from loop_claude.test_loop_claude import FakeEffects
|
|
from do_epic.test_scheduler import issue
|
|
from claude_code.reply import Limited
|
|
from do_epic.config import load
|
|
from do_epic.backend.fake import MemoryEpic
|
|
from plan.todo_table import parse_moves
|
|
from do_epic import planning
|
|
|
|
RANKED = "## Goals\n1. **Achieve:** Ship the import.\n\n## Audience\nThe client's team.\n\n## Scoring\n`Score = V`\n"
|
|
LIMIT = {"type": "result", "is_error": True, "api_error_status": 429, "result": "You've hit your limit"}
|
|
|
|
|
|
def scored(key: str, moves: str, *labels: str, blockers: tuple[str, ...] = (), by_loop: bool = True) -> EpicIssue:
|
|
"""An open issue the loop filed, holding scores and moving the goals moves names."""
|
|
parsed = parse_moves(key, moves)
|
|
assert not isinstance(parsed, Failure), parsed
|
|
return dataclasses.replace(issue(key, *labels, blockers=blockers), scores={"L": 1, "E": 1, "I": 1, "S": 1}, moves=parsed, by_loop=by_loop)
|
|
|
|
|
|
class Planning(DoEpicTest):
|
|
def plan(self, fake: FakeEffects) -> tuple[list[EpicIssue] | Failure | Limited, MemoryEpic, str]:
|
|
"""What planning answers for self.issues, the store it changed and what it printed."""
|
|
config = load(self.home)
|
|
assert not isinstance(config, Failure), config
|
|
store = MemoryEpic("ABC-12", list(self.issues))
|
|
epic = planning.Planned("ABC-12", config, store, EpicRecord(Description(self.description)), self.home, fake.effects()) # type: ignore[arg-type]
|
|
out = io.StringIO()
|
|
with contextlib.redirect_stdout(out), contextlib.redirect_stderr(out):
|
|
read = store.read()
|
|
assert not isinstance(read, Failure)
|
|
return planning.planned(epic, read), store, out.getvalue()
|
|
|
|
def labels(self, store: MemoryEpic) -> dict[str, tuple[str, ...]]:
|
|
return {each.key: each.labels for each in store.issues}
|
|
|
|
def test_an_issue_a_label_places_is_planned_without_claude(self) -> None:
|
|
self.issues = [issue("ABC-20", "repo:api"), issue("ABC-21", "do-epic-claude"), issue("ABC-22", "split-by-repo"), issue("ABC-23", done=True)]
|
|
fake = FakeEffects()
|
|
found, _, out = self.plan(fake)
|
|
self.assertEqual((found, out, fake.made("claude")), (self.issues, "", []))
|
|
|
|
def test_labels_an_unplaced_issue_with_the_repo_claude_picks_on_the_repoless_models_rung(self) -> None:
|
|
self.issues = [issue("ABC-20")]
|
|
fake = FakeEffects(claude=[says({"parts": [{"repo": "api", "change": "Add the endpoint."}]}), says({"pick": "repo", "repo": "api"})])
|
|
found, store, out = self.plan(fake)
|
|
self.assertEqual(found, [issue("ABC-20", "repo:api")])
|
|
self.assertEqual(self.labels(store), {"ABC-20": ("repo:api",)})
|
|
self.assertIn("ABC-20 is labelled repo:api\n", out)
|
|
for args, _ in fake.made("claude"):
|
|
self.assertEqual(args[args.index("--model") + 1], "claude-opus-5-5")
|
|
self.assertIn("A label that equals a repo's name names that repo.", fake.made("claude")[1][1])
|
|
|
|
def test_labels_work_without_a_repo_as_claudes_or_a_humans(self) -> None:
|
|
for pick, label in (("claude", "do-epic-claude"), ("human", "do-epic-human")):
|
|
with self.subTest(pick=pick):
|
|
self.issues = [issue("ABC-20")]
|
|
_, store, _ = self.plan(FakeEffects(claude=[says({"parts": []}), says({"pick": pick})]))
|
|
self.assertEqual(self.labels(store), {"ABC-20": (label,)})
|
|
|
|
def test_splits_an_issue_that_changes_several_repos_into_a_sub_task_for_each_and_picks_no_repo_for_it(self) -> None:
|
|
self.issues = [issue("ABC-20")]
|
|
fake = FakeEffects(claude=[says({"parts": [{"repo": "api", "change": "Add the endpoint."}, {"repo": "web", "change": "Call it."}], "comment": "Split by repo: api and web."})])
|
|
found, store, out = self.plan(fake)
|
|
assert not isinstance(found, Failure | Limited), found
|
|
self.assertEqual([(each.key, each.parent, each.labels) for each in found], [("ABC-20", "ABC-12", ("split-by-repo",)), ("ABC-101", "ABC-20", ("repo:api",)), ("ABC-102", "ABC-20", ("repo:web",))])
|
|
# A person filed ABC-20, so the team is told.
|
|
self.assertEqual(store.comments, {"ABC-20": ["Split by repo: api and web."]})
|
|
self.assertIn("ABC-20 is split into a sub-task for each of api, web\n", out)
|
|
|
|
def test_the_split_reads_the_checkout_of_each_repo_a_label_of_the_epic_names(self) -> None:
|
|
checkout = self.home / "code" / "api"
|
|
configured(self, checkout, home=self.home)
|
|
self.issues = [issue("ABC-19", "repo:api", done=True), issue("ABC-20")]
|
|
fake = FakeEffects(claude=[says({"parts": []}), says({"pick": "claude"})])
|
|
self.plan(fake)
|
|
args = fake.made("claude")[0][0]
|
|
self.assertEqual(args[args.index("--add-dir") + 1], str(checkout))
|
|
|
|
def test_asks_the_human_for_the_label_of_an_issue_the_rule_leaves_unclear_until_one_is_typed(self) -> None:
|
|
self.issues = [issue("ABC-20")]
|
|
fake = FakeEffects(claude=[says({"parts": []}), says({"pick": "unclear", "question": "Is the importer in api or in web?"})], inputs=["api", " repo:web "])
|
|
found, store, out = self.plan(fake)
|
|
self.assertEqual(found, [issue("ABC-20", "repo:web")])
|
|
self.assertIn("ABC-20, Do ABC-20: Is the importer in api or in web?\n", out)
|
|
self.assertIn("`api` is none of those labels\n", out)
|
|
self.assertEqual(fake.made("input"), ["The label that says where ABC-20 is worked, `repo:<name>`, `do-epic-claude` or `do-epic-human`: "] * 2)
|
|
|
|
def test_an_unclear_issue_waits_where_nobody_can_answer_and_the_next_is_planned(self) -> None:
|
|
unclear = says({"pick": "unclear", "question": "Is the importer in api or in web?"})
|
|
for fake in (FakeEffects(claude=[says({"parts": []}), unclear, says({"parts": []}), says({"pick": "claude"})], on_terminal=False), FakeEffects(claude=[says({"parts": []}), unclear, says({"parts": []}), says({"pick": "claude"})], inputs=[EOFError()])):
|
|
with self.subTest(on_terminal=fake.terminal):
|
|
self.issues = [issue("ABC-20"), issue("ABC-21")]
|
|
_, store, out = self.plan(fake)
|
|
self.assertEqual(self.labels(store), {"ABC-20": (), "ABC-21": ("do-epic-claude",)})
|
|
if not fake.terminal:
|
|
self.assertIn("ABC-20 waits for your answer: Is the importer in api or in web? Run `do-epic ABC-12` on a terminal to answer, or label the issue in Jira.\n", out)
|
|
|
|
def test_an_issue_claude_fails_on_is_named_and_the_next_is_planned(self) -> None:
|
|
self.issues = [issue("ABC-20"), issue("ABC-21")]
|
|
fake = FakeEffects(claude=[subprocess.CompletedProcess([], 1, "", ""), says({"parts": []}), says({"pick": "claude"})])
|
|
_, store, out = self.plan(fake)
|
|
self.assertEqual(self.labels(store), {"ABC-20": (), "ABC-21": ("do-epic-claude",)})
|
|
self.assertIn("ABC-20 is not planned: the repo split exited 1\n", out)
|
|
|
|
def test_a_usage_limit_ends_the_planning(self) -> None:
|
|
self.issues = [issue("ABC-20"), issue("ABC-21")]
|
|
fake = FakeEffects(claude=[subprocess.CompletedProcess([], 0, json.dumps(LIMIT), "")])
|
|
found, _, _ = self.plan(fake)
|
|
self.assertIsInstance(found, Limited)
|
|
self.assertEqual(len(fake.made("claude")), 1)
|
|
|
|
def test_re_ranks_the_loops_scored_issues_by_score_with_a_blocker_first_and_leaves_every_other_issue_in_place(self) -> None:
|
|
self.description = RANKED
|
|
self.issues = [scored("ABC-20", "epic 1 by 2", "do-epic-claude"), scored("ABC-21", "epic 1 by 9", "do-epic-claude", by_loop=False), scored("ABC-22", "epic 1 by 5", "do-epic-claude", blockers=("ABC-24",)), dataclasses.replace(issue("ABC-23", "do-epic-claude"), by_loop=True), scored("ABC-24", "epic 1 by 3", "do-epic-claude")]
|
|
found, store, out = self.plan(FakeEffects())
|
|
assert not isinstance(found, Failure | Limited), found
|
|
# ABC-22 scores highest, and ABC-24 blocks it. ABC-21 is a person's, and nobody scored ABC-23.
|
|
self.assertEqual([each.key for each in found], ["ABC-24", "ABC-21", "ABC-22", "ABC-23", "ABC-20"])
|
|
self.assertIn("re-ranking the loop's issues in ABC-12's backlog\n", out)
|
|
|
|
def test_ranks_an_issue_in_a_repo_by_that_repos_goals_after_the_epics(self) -> None:
|
|
checkout = self.home / "code" / "api"
|
|
configured(self, checkout, home=self.home)
|
|
(checkout / "README.md").write_text("## Goals\n\n**Void:** code health, since it is a one-off script.\n\n1. **Achieve:** Serve the API.\n\n## Audience\nIts callers.\n", encoding="utf-8")
|
|
self.description = RANKED
|
|
self.issues = [scored("ABC-20", "repo 1 by 9", "repo:api"), scored("ABC-21", "epic 1 by 9", "do-epic-claude")]
|
|
found, _, _ = self.plan(FakeEffects())
|
|
assert not isinstance(found, Failure | Limited), found
|
|
self.assertEqual([each.key for each in found], ["ABC-21", "ABC-20"])
|
|
|
|
def test_issues_already_in_their_order_are_not_ranked_again(self) -> None:
|
|
self.description = RANKED
|
|
self.issues = [scored("ABC-20", "epic 1 by 9", "do-epic-claude"), scored("ABC-21", "epic 1 by 2", "do-epic-claude")]
|
|
found, _, out = self.plan(FakeEffects())
|
|
self.assertEqual((found, out), (self.issues, ""))
|
|
|
|
def test_fails_where_the_epic_states_no_formula_to_rank_two_scored_issues_by(self) -> None:
|
|
self.issues = [scored("ABC-20", "epic 1 by 2", "do-epic-claude"), scored("ABC-21", "epic 1 by 9", "do-epic-claude")]
|
|
found, store, _ = self.plan(FakeEffects())
|
|
self.assertEqual(found, Failure("the epic ABC-12's description states `Score = ...` 0 times under ## Scoring; state the formula that ranks its issues once, on a line of its own, such as `Score = 100 * V / (2**S + 32)`"))
|
|
self.assertEqual([each.key for each in store.issues], ["ABC-20", "ABC-21"])
|
|
|
|
def test_one_scored_issue_needs_no_formula(self) -> None:
|
|
self.issues = [scored("ABC-20", "epic 1 by 2", "do-epic-claude"), issue("ABC-21", "do-epic-claude")]
|
|
self.assertEqual(self.plan(FakeEffects())[0], self.issues)
|
|
|
|
def test_fails_where_the_tracker_cannot_be_read_after_a_pick(self) -> None:
|
|
self.issues = [issue("ABC-20")]
|
|
|
|
def failing(_: object) -> subprocess.CompletedProcess[str]:
|
|
self.store.fails = "Jira answered 503"
|
|
return says({"parts": []})
|
|
config = load(self.home)
|
|
self.store = MemoryEpic("ABC-12", list(self.issues))
|
|
epic = planning.Planned("ABC-12", config, self.store, EpicRecord(Description(self.description)), self.home, FakeEffects(claude=[failing, says({"pick": "claude"})]).effects()) # type: ignore[arg-type]
|
|
with contextlib.redirect_stdout(io.StringIO()):
|
|
self.assertEqual(planning.planned(epic, list(self.issues)), Failure("Jira answered 503"))
|