import json import pathlib import sys import tempfile import unittest sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) from failure import Failure from review_rules.reviewer_context import hook_reply, principles_line, reviewer_context, SECTIONS from review_rules.review_passes import REVIEWERS REPO = pathlib.Path(__file__).resolve().parents[2] CLAUDE_MD = "# Global\n\n## Working loop\n\nThe worker's.\n\n## Shared goal\n\nCode health.\n\n## Prose\n\nWrite short.\n\n## Git\n\nOne line.\n\n## Vocabulary\n\n- **chunk** — a unit.\n" class ReviewerContext(unittest.TestCase): def setUp(self): self.home = pathlib.Path(self.enterContext(tempfile.TemporaryDirectory())) (self.home / ".claude").mkdir() (self.home / ".claude" / "CLAUDE.md").write_text(CLAUDE_MD) def test_holds_the_shared_goal_prose_and_vocabulary_sections_and_none_of_the_workers(self): self.assertEqual(reviewer_context(self.home), "You start without `~/.claude/CLAUDE.md`. These sections of it hold for you:\n\n## Shared goal\n\nCode health.\n\n## Prose\n\nWrite short.\n\n## Vocabulary\n\n- **chunk** — a unit.") def test_fails_on_a_missing_section_or_file(self): (self.home / ".claude" / "CLAUDE.md").write_text("## Prose\n\nWrite short.\n") self.assertEqual(reviewer_context(self.home), Failure(f"{self.home / '.claude' / 'CLAUDE.md'} holds no section Shared goal, Vocabulary")) (self.home / ".claude" / "CLAUDE.md").unlink() self.assertIsInstance(reviewer_context(self.home), Failure) def test_the_repos_claude_md_holds_each_section(self): headings = [line[3:] for line in (REPO / "CLAUDE.md").read_text().splitlines() if line.startswith("## ")] self.assertEqual([name for name in SECTIONS if name not in headings], []) def test_the_hook_hands_a_reviewer_the_context_and_any_other_agent_nothing(self): for agent in REVIEWERS.values(): with self.subTest(agent=agent): reply = hook_reply(json.dumps({"hook_event_name": "SubagentStart", "agent_type": agent}), self.home) self.assertEqual(json.loads(reply), {"hookSpecificOutput": {"hookEventName": "SubagentStart", "additionalContext": reviewer_context(self.home)}}) for given in (json.dumps({"agent_type": "Explore"}), json.dumps({}), json.dumps([]), "no json"): with self.subTest(given=given): self.assertEqual(hook_reply(given, self.home), "") def test_the_hook_fails_for_a_reviewer_when_a_section_is_missing(self): (self.home / ".claude" / "CLAUDE.md").write_text("") self.assertIsInstance(hook_reply(json.dumps({"agent_type": "prose-reviewer"}), self.home), Failure) class PrinciplesLine(unittest.TestCase): def setUp(self): self.worktree = pathlib.Path(self.enterContext(tempfile.TemporaryDirectory())) def test_names_agents_md_where_the_project_keeps_one(self): (self.worktree / "AGENTS.md").write_text("") (self.worktree / "docs").mkdir() (self.worktree / "docs" / "decisions.md").write_text("") self.assertEqual(principles_line(str(self.worktree)), f"Principles: read {self.worktree}/AGENTS.md first, for the project principles and the decision titles.") def test_names_the_head_of_the_decision_log_where_the_project_keeps_no_agents_md(self): (self.worktree / "docs").mkdir() (self.worktree / "docs" / "decisions.md").write_text("") self.assertEqual(principles_line(str(self.worktree)), f"Principles: read {self.worktree}/docs/decisions.md first, up to `# Decisions`, for the project principles and the decision titles.") def test_names_nothing_where_the_project_keeps_neither(self): self.assertIsNone(principles_line(str(self.worktree))) class Reviewers(unittest.TestCase): def test_each_agent_starts_without_claude_md(self): for path in sorted((REPO / "agents").glob("*.md")): with self.subTest(agent=path.stem): frontmatter = path.read_text().split("---\n")[1] self.assertIn("omitClaudeMd: true\n", frontmatter) def test_settings_run_the_hook_as_each_subagent_starts(self): hooks = json.loads((REPO / "settings.json").read_text())["hooks"]["SubagentStart"] self.assertEqual(hooks, [{"hooks": [{"command": "~/.claude/bin/larv-review reviewer-context", "timeout": 30, "type": "command"}]}]) if __name__ == "__main__": unittest.main()