from unittest import mock import os import pathlib import re import subprocess import sys import tempfile import unittest sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) from larv_review.scoring_setup import bullets, CLAUDE_MD, file_globs, preloaded_files, prepare_scoring, project_values, removed_setup, score_bullets, section from failure import Failure from test_env import isolated_env TOOL = pathlib.Path(__file__).resolve().parents[1] / "larv-review" SKILL = pathlib.Path(__file__).resolve().parents[2] / "skills" / "comprehension-panel" / "SKILL.md" VALUES = {"language": "Python", "kind": "a CLI", "domain": "parsing", "domain docs": "the spec", "3am question": "the parse hangs"} class ScoreBullets(unittest.TestCase): def test_gives_the_six_anchors_and_four_dimensions_unbolded_each_on_one_line(self): anchors, dimensions = score_bullets(SKILL.read_text()) self.assertEqual(len(anchors), 6) self.assertEqual(anchors[0], "- 1 — Spaghetti. No boundary holds. Finding where a behaviour lives is grep archaeology; changing it safely is guesswork, and the reliable technique is to change it and see what breaks. No map the reader builds survives contact with the code.") self.assertTrue(anchors[5].startswith("- 10 — Duress-proof. Hungover,")) self.assertEqual([line.split(" — ")[0] for line in dimensions], ["- Navigation", "- Locality", "- Shape", "- Self-sufficiency"]) self.assertEqual(dimensions[1], "- Locality — understanding and changing one piece without holding the rest: hidden state, order dependence, action at a distance.") class ProjectValues(unittest.TestCase): def test_reads_each_spelling_the_projects_use(self): for spelling in ( "## Scoring run\n\nThe values:\n\n- `{language}`: Python\n- `{kind}`: a CLI\n- `{domain}`: parsing\n- `{domain docs}`: the spec\n- `{3am question}`: the parse\n hangs\n\n## Next\n\n- `{kind}`: other\n", "# Scoring run\n\n- **Language:** Python\n- **Kind:** a CLI\n- **Domain:** parsing\n- **Domain docs:** the spec\n- **3am question:** the parse hangs\n", "## 8. Scoring run\n\n- Language: Python.\n- Kind: a CLI.\n- Domain: parsing.\n- Domain docs: the spec.\n- 3am question: the parse hangs.\n", ): with self.subTest(spelling=spelling[:20]): self.assertEqual(project_values(spelling, "AGENTS.md"), VALUES) def test_keeps_a_colon_an_x_and_an_ellipsis_in_a_value_and_skips_a_bullet_without_a_colon(self): text = "## Scoring run\n\n- Language: Python\n- Kind: a CLI: small\n- Kind\n- Domain: LaTeX\n- Domain docs: the spec\n- 3am question: it hangs...\n" self.assertEqual(project_values(text, "AGENTS.md"), {**VALUES, "kind": "a CLI: small", "domain": "LaTeX", "3am question": "it hangs..."}) def test_names_the_values_a_section_lacks_and_a_file_without_one(self): self.assertEqual(project_values("## Scoring run\n\n- Language: Python\n- Kind: a CLI\n", "AGENTS.md"), Failure("AGENTS.md → \"Scoring run\" gives no {domain}, {domain docs} or {3am question}; add them as seat-prompts.md lists them")) self.assertEqual(project_values("# P\n", "AGENTS.md"), Failure("AGENTS.md has no \"Scoring run\" section to fill the seats' prompts from; add one with {language}, {kind}, {domain}, {domain docs} and {3am question}")) class FileGlobs(unittest.TestCase): def test_names_no_test_files_where_every_glob_holds_none(self): self.assertEqual(file_globs(["bin/a.py", "tests/x.py", "README.md"]), "`bin/**` and `README.md`") def test_gives_one_glob_per_directory_and_each_top_level_file_leaving_out_tests_and_the_forbidden_files(self): paths = ["README.md", "ci.sh", "bin/run", "bin/a/main.py", "bin/a/test_main.py", "docs/decisions.md", "docs/guide.md", "todo.md", "tests/x.py", "web/app.test.ts"] self.assertEqual(file_globs(paths), "`bin/**` except test files, `docs/**`, `README.md` and `ci.sh`, test files being `test_*`, `*_test.*`, `*.test.*`, `*.spec.*` and those under a `test` or `tests` folder") class Section(unittest.TestCase): def test_holds_deeper_headings_and_fenced_lines_and_ends_at_its_own_level(self): text = "# P\n\n## Scoring run\n\na\n\n### Sub\n\n```\n## fenced\n```\n\n## Next\n\nb\n" self.assertEqual(section(text, "Scoring run"), ["", "a", "", "### Sub", "", "```", "## fenced", "```", ""]) self.assertIsNone(section(text, "Missing")) class Bullets(unittest.TestCase): def test_joins_continuations_and_splits_runs_at_other_lines(self): lines = [" stray", "- a ", " more", "Text", "- b", "", "- c"] self.assertEqual(bullets(lines), [["- a more"], ["- b"], ["- c"]]) class PrepareScoring(unittest.TestCase): def setUp(self): temp = tempfile.TemporaryDirectory() self.addCleanup(temp.cleanup) self.root = pathlib.Path(os.path.realpath(temp.name)) self.env = {**isolated_env(), "GIT_CONFIG_GLOBAL": os.devnull, "HOME": str(self.root)} self.checkout = self.root / "proj" self.checkout.mkdir() self.git("init", "-q", "-b", "item-9") (self.checkout / "README.md").write_text("# P\n") (self.checkout / "bin").mkdir() (self.checkout / "bin" / "tool.py").write_text("") def git(self, *args): return subprocess.run(["git", "-c", "user.name=t", "-c", "user.email=t@t", *args], cwd=self.checkout, check=True, env=self.env, capture_output=True, text=True).stdout def commit(self, agents=None): if agents is not None: (self.checkout / "AGENTS.md").write_text(agents) self.git("add", "-A") self.git("commit", "-q", "-m", "start") def prepare(self, preloaded=None): """What prepare-scoring returns in the checkout, run in this process so mutation tests see the lines it runs.""" with mock.patch.dict(os.environ, self.env, clear=True): return prepare_scoring(str(self.checkout), preloaded) def run_tool(self, *args): return subprocess.run([sys.executable, str(TOOL), *args], capture_output=True, cwd=self.checkout, env=self.env, text=True) def test_writes_each_seats_prompt_with_every_placeholder_filled_and_a_worktree_at_head(self): self.commit("# A\n\n## Scoring run\n\n- Language: Python\n- Kind: a CLI\n- Domain: parsing\n- Domain docs: the spec\n- 3am question: the parse hangs\n") ran = self.prepare("`README.md`") folder = self.root / ".local" / "state" / "larv-review" / "proj" / "scoring" worktree = folder / "checkout" prompts = [folder / f"{seat}.md" for seat in ("junior-a", "mid-a", "maintainability-senior", "inherited-architect")] self.assertEqual(ran, f"Scoring worktree, at {self.git('rev-parse', '--short', 'HEAD').strip()}: {worktree}\nPrompts:\n" + "\n".join(str(prompt) for prompt in prompts)) self.assertEqual(subprocess.run(["git", "rev-parse", "HEAD"], cwd=worktree, env=self.env, capture_output=True, text=True).stdout, self.git("rev-parse", "HEAD")) junior, _, senior, architect = (prompt.read_text() for prompt in prompts) for prompt in (junior, architect): self.assertEqual(re.findall(r"\{[a-z0-9 ]+\}", prompt), []) self.assertTrue(junior.startswith(f"You are one seat of a comprehension panel. Tools: Read, Grep, Glob, and Bash only for `git -C {worktree} ls-files` and `ls`.")) self.assertIn("\n\nYour background: ~2 years in Python, first codebase of this kind (a CLI); no exposure to the domain at all.\n\n", junior) self.assertIn(f"Target: the project checked out at {worktree} (branch `item-9`). Read ONLY there — never {self.checkout}, which is a different version.", junior) self.assertIn("every non-test tracked file: `bin/**`, `AGENTS.md` and `README.md`. Do NOT open todo.md, docs/decisions.md,", junior) self.assertIn("from a DIFFERENT version (main): `README.md`.", junior) self.assertIn("\n- 7 — Predictable. The layout answers", junior) self.assertIn("Report, in this shape:", senior) self.assertIn("~15 years taking over other people's systems", architect) self.assertIn("The 3am question: the parse hangs — walking in cold", architect) self.assertNotIn("Report, in this shape:", architect) (folder / "mid-a.md").write_text("stale") again = self.prepare() self.assertTrue(again.startswith(f"Removed the scoring run set up before in {folder}.\nScoring worktree, at ")) self.assertNotEqual((folder / "mid-a.md").read_text(), "stale") self.assertEqual(len(self.git("worktree", "list").splitlines()), 2) def test_says_it_could_not_remove_a_checkout_git_does_not_hold(self): self.commit() checkout = self.root / ".local" / "state" / "larv-review" / "proj" / "scoring" / "checkout" checkout.mkdir(parents=True) with mock.patch.dict(os.environ, self.env, clear=True): removed = removed_setup(str(self.checkout)) self.assertIsInstance(removed, Failure) self.assertTrue(removed.reason.startswith(f"could not remove the scoring worktree {checkout}: "), removed.reason) self.assertTrue(checkout.exists()) def test_refuses_writing_nothing_where_the_project_gives_no_values(self): self.commit() ran = self.run_tool("prepare-scoring") self.assertEqual(ran.returncode, 1) self.assertIn("AGENTS.md has no \"Scoring run\" section", ran.stderr) self.assertFalse((self.root / ".local" / "state" / "larv-review" / "proj" / "scoring").exists()) self.assertEqual(len(self.git("worktree", "list").splitlines()), 1) def test_lists_the_harness_files_where_no_preloaded_is_given(self): (self.checkout / "CLAUDE.md").write_text("@AGENTS.md\n") self.commit("## Scoring run\n\n- Language: Python\n- Kind: a CLI\n- Domain: parsing\n- Domain docs: the spec\n- 3am question: the parse hangs\n") self.assertNotIsInstance(self.prepare(), Failure) junior = (self.root / ".local" / "state" / "larv-review" / "proj" / "scoring" / "junior-a.md").read_text() self.assertIn("from a DIFFERENT version (main): `CLAUDE.md` and `AGENTS.md`, which it imports.", junior) def test_leaves_out_a_claude_md_the_project_settings_exclude(self): (self.root / ".claude").mkdir() (self.root / ".claude" / "CLAUDE.md").write_text("# Home\n") (self.root / ".claude" / "rules").mkdir() rule = "always" (self.root / ".claude" / "rules" / f"{rule}.md").write_text("# Always\n") (self.checkout / ".claude").mkdir() (self.checkout / ".claude" / "settings.json").write_text('{"claudeMdExcludes": ["**/.claude/CLAUDE.md"]}\n') (self.checkout / ".claude" / "settings.local.json").write_text("not json") self.assertEqual(preloaded_files(self.checkout, self.root), f"`~/.claude/rules/{rule}.md`") (self.checkout / ".claude" / "settings.json").write_text('{"claudeMdExcludes": [3]}\n') self.assertEqual(preloaded_files(self.checkout, self.root), f"`~/.claude/CLAUDE.md` and `~/.claude/rules/{rule}.md`") def test_names_each_import_and_each_rule_without_paths_leaving_out_what_the_user_settings_exclude(self): (self.root / ".claude" / "rules").mkdir(parents=True) (self.root / ".claude" / "settings.json").write_text('{"claudeMdExcludes": ["**/rules/skipped.md"]}') (self.root / ".claude" / "rules" / "skipped.md").write_text("# Skipped\n") (self.checkout / "CLAUDE.md").write_text("# Project\n\n@AGENTS.md\n@docs/x.md\n") (self.checkout / ".claude" / "rules").mkdir(parents=True) (self.checkout / ".claude" / "rules" / "always.md").write_text("# Always\n") (self.checkout / ".claude" / "rules" / "scoped.md").write_text("---\npaths: ['*.py']\n---\n") imports, rule = "`AGENTS.md`, which it imports, `docs/x.md`, which it imports", "always" self.assertEqual(preloaded_files(self.checkout, self.root), f"`{CLAUDE_MD}`, {imports} and `.claude/rules/{rule}.md`") def test_reads_the_excludes_of_both_settings_files_and_names_none_where_nothing_loads(self): (self.root / ".claude").mkdir() (self.root / ".claude" / "CLAUDE.md").write_text("# Home\n") (self.checkout / "CLAUDE.md").write_text("# Project\n") (self.checkout / ".claude").mkdir() settings, local = self.checkout / ".claude" / "settings.json", self.checkout / ".claude" / "settings.local.json" for first, second in (("not json", '{"claudeMdExcludes": ["**/.claude/*.md", "**/proj/*.md"]}'), ('{"claudeMdExcludes": ["**/.claude/*.md"]}', '{"claudeMdExcludes": ["**/proj/*.md"]}')): with self.subTest(first=first): settings.write_text(first) local.write_text(second) self.assertEqual(preloaded_files(self.checkout, self.root), "none") for odd in ("[1]", '{"claudeMdExcludes": 3}'): with self.subTest(odd=odd): settings.write_text(odd) local.write_text(odd) self.assertEqual(preloaded_files(self.checkout, self.root), f"`~/.claude/{CLAUDE_MD}` and `{CLAUDE_MD}`") if __name__ == "__main__": unittest.main()