tests / unittest (push) Failing after 28s
drafter: local runs the pi agent against the model already loaded on a self-hosted server, as an unprivileged user, in the same sandbox as the Claude drafter. It fails soft when the server is unreachable or has no model loaded. Both drafters now refuse to continue if git metadata changed during drafting, and strip project pi configuration from the sandbox. Tested live against a self-hosted server: one run, 12 turns, docs PR opened. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
182 lines
8.3 KiB
Python
182 lines
8.3 KiB
Python
import unittest
|
|
from pathlib import Path
|
|
|
|
from docs_review.inputs import (
|
|
expand_patterns,
|
|
matches_any,
|
|
normalize_drafter,
|
|
parse_inputs,
|
|
split_review_only,
|
|
unmatched_patterns,
|
|
)
|
|
from tests.helpers import tmp_tree
|
|
|
|
|
|
class DrafterTest(unittest.TestCase):
|
|
def test_drafter_defaults_to_none(self):
|
|
for raw in (None, "", "claud", "yes", "true"):
|
|
self.assertEqual(normalize_drafter(raw), "none")
|
|
self.assertEqual(normalize_drafter(" Claude\n"), "claude")
|
|
self.assertEqual(normalize_drafter("local"), "local")
|
|
|
|
|
|
class ParseInputsTest(unittest.TestCase):
|
|
def test_parse_inputs_newline_lists_and_defaults(self):
|
|
i = parse_inputs({"INPUT_DOCS": "README.md\n docs/book/*.qmd \n\n"})
|
|
self.assertEqual(i.docs, ("README.md", "docs/book/*.qmd"))
|
|
self.assertEqual((i.drafter, i.model, i.workstream), ("none", "claude-sonnet-5-5", "docs"))
|
|
self.assertEqual(i.source_dirs, ("R", "scripts"))
|
|
self.assertEqual(i.top_level, ("README.md",))
|
|
self.assertEqual(i.generated, ())
|
|
self.assertIsNone(i.tracking_issue)
|
|
self.assertIsNone(i.doc_guide)
|
|
|
|
def test_parse_inputs_requires_docs(self):
|
|
for env in ({}, {"INPUT_DOCS": ""}, {"INPUT_DOCS": " \n "}):
|
|
with self.assertRaisesRegex(ValueError, "docs"):
|
|
parse_inputs(env)
|
|
|
|
def test_parse_inputs_values(self):
|
|
i = parse_inputs(
|
|
{
|
|
"INPUT_DOCS": "a.md",
|
|
"INPUT_DRAFTER": "Local",
|
|
"INPUT_LOCAL_URL": " http://model-host:8888/v1/ ",
|
|
"INPUT_TRACKING_ISSUE": " 42 ",
|
|
"INPUT_DOC_GUIDE": "docs/guide.md",
|
|
"INPUT_MODEL": "m",
|
|
"INPUT_WORKSTREAM": "model",
|
|
"INPUT_SOURCE_DIRS": "src\nlib",
|
|
"INPUT_TOP_LEVEL": "A.md\nB.md",
|
|
"INPUT_GENERATED": "docs/gen/*",
|
|
}
|
|
)
|
|
self.assertEqual(i.drafter, "local")
|
|
self.assertEqual(i.local_url, "http://model-host:8888/v1")
|
|
self.assertEqual(i.tracking_issue, 42)
|
|
self.assertEqual(i.doc_guide, "docs/guide.md")
|
|
self.assertEqual((i.model, i.workstream), ("m", "model"))
|
|
self.assertEqual(i.source_dirs, ("src", "lib"))
|
|
self.assertEqual(i.top_level, ("A.md", "B.md"))
|
|
self.assertEqual(i.generated, ("docs/gen/*",))
|
|
|
|
def test_blank_tracking_issue_and_guide_are_none(self):
|
|
i = parse_inputs(
|
|
{"INPUT_DOCS": "a.md", "INPUT_TRACKING_ISSUE": " ", "INPUT_DOC_GUIDE": ""}
|
|
)
|
|
self.assertIsNone(i.tracking_issue)
|
|
self.assertIsNone(i.doc_guide)
|
|
|
|
def test_review_only_defaults_to_empty(self):
|
|
self.assertEqual(parse_inputs({"INPUT_DOCS": "a.md"}).review_only, ())
|
|
|
|
def test_review_only_entries_within_docs(self):
|
|
i = parse_inputs({"INPUT_DOCS": "README.md\ndocs/*.md\npm/**/*.md",
|
|
"INPUT_REVIEW_ONLY": " docs/vision.md \n\nREADME.md\npm/v*.md\n"})
|
|
self.assertEqual(i.review_only, ("docs/vision.md", "README.md", "pm/v*.md"))
|
|
|
|
def test_review_only_entry_outside_docs_raises(self):
|
|
for entry in ("other/vision.md", "docs/sub/vision.md", "*.md"):
|
|
with self.assertRaisesRegex(ValueError, f"review_only.*{entry.replace('*', '[*]')}"):
|
|
parse_inputs({"INPUT_DOCS": "README.md\ndocs/*.md",
|
|
"INPUT_REVIEW_ONLY": f"docs/a.md\n{entry}"})
|
|
|
|
def test_split_review_only(self):
|
|
editable, read_only = split_review_only(
|
|
("README.md", "docs/a.md", "docs/vision.md"), ("docs/v*.md",))
|
|
self.assertEqual(editable, ("README.md", "docs/a.md"))
|
|
self.assertEqual(read_only, ("docs/vision.md",))
|
|
self.assertEqual(split_review_only(("a.md",), ()), (("a.md",), ()))
|
|
|
|
def test_non_integer_tracking_issue_raises(self):
|
|
with self.assertRaisesRegex(ValueError, "tracking_issue"):
|
|
parse_inputs({"INPUT_DOCS": "a.md", "INPUT_TRACKING_ISSUE": "abc"})
|
|
|
|
def test_local_url_required_for_local(self):
|
|
for url in (None, "", " "):
|
|
with self.assertRaisesRegex(ValueError, "local_url"):
|
|
parse_inputs({"INPUT_DOCS": "a.md", "INPUT_DRAFTER": "local",
|
|
"INPUT_LOCAL_URL": url})
|
|
|
|
def test_local_url_must_be_http(self):
|
|
for url in ("ftp://h/v1", "file:///etc/passwd", "model-host:8888/v1", "http://",
|
|
"http:///v1"):
|
|
with self.assertRaisesRegex(ValueError, "local_url"):
|
|
parse_inputs({"INPUT_DOCS": "a.md", "INPUT_DRAFTER": "local",
|
|
"INPUT_LOCAL_URL": url})
|
|
i = parse_inputs({"INPUT_DOCS": "a.md", "INPUT_DRAFTER": "local",
|
|
"INPUT_LOCAL_URL": "https://h/v1"})
|
|
self.assertEqual(i.local_url, "https://h/v1")
|
|
|
|
def test_local_url_ignored_for_other_drafters(self):
|
|
i = parse_inputs({"INPUT_DOCS": "a.md", "INPUT_DRAFTER": "claude",
|
|
"INPUT_LOCAL_URL": "ftp://x"})
|
|
self.assertIsNone(i.local_url)
|
|
|
|
|
|
class PatternTest(unittest.TestCase):
|
|
def test_expand_sorted_deduplicated_posix(self):
|
|
with tmp_tree(["README.md", "docs/b.qmd", "docs/a.qmd", "docs/x.md"]) as d:
|
|
root = Path(d)
|
|
got = expand_patterns(["docs/*.qmd", "README.md", "docs/a.qmd"], root)
|
|
self.assertEqual(got, ("README.md", "docs/a.qmd", "docs/b.qmd"))
|
|
|
|
def test_expand_recursive_and_skips_directories(self):
|
|
with tmp_tree(["docs/a/b.md", "docs/c.md"]) as d:
|
|
got = expand_patterns(["docs/**/*.md"], Path(d))
|
|
self.assertEqual(got, ("docs/a/b.md", "docs/c.md"))
|
|
self.assertEqual(expand_patterns(["docs/*"], Path(d)), ("docs/c.md",))
|
|
|
|
def test_unmatched_patterns(self):
|
|
with tmp_tree(["README.md"]) as d:
|
|
root = Path(d)
|
|
self.assertEqual(expand_patterns(["README.md", "nope/*.md"], root), ("README.md",))
|
|
self.assertEqual(
|
|
unmatched_patterns(["README.md", "nope/*.md", "gone.md"], root),
|
|
("nope/*.md", "gone.md"),
|
|
)
|
|
|
|
def test_matches_any(self):
|
|
self.assertTrue(matches_any("docs/a.qmd", ["README.md", "docs/*.qmd"]))
|
|
self.assertFalse(matches_any("docs/x/a.qmd", ["docs/*.qmd"]))
|
|
self.assertFalse(matches_any("R/a.R", ["README.md", "docs/*.qmd"]))
|
|
self.assertFalse(matches_any("R/a.R", []))
|
|
|
|
def test_double_star_segment_matches_zero_directories(self):
|
|
self.assertTrue(matches_any("docs/c.md", ["docs/**/*.md"]))
|
|
self.assertTrue(matches_any("docs/a/b/c.md", ["docs/**/*.md"]))
|
|
self.assertTrue(matches_any("a/z.md", ["**/a/**/z.md"]))
|
|
self.assertTrue(matches_any("a/b/z.md", ["**/a/**/z.md"]))
|
|
self.assertFalse(matches_any("other/c.md", ["docs/**/*.md"]))
|
|
|
|
def test_wildcards_never_cross_a_slash(self):
|
|
self.assertFalse(matches_any("docs/book/x/y.qmd", ["docs/book/*.qmd"]))
|
|
self.assertTrue(matches_any("docs/book/y.qmd", ["docs/book/*.qmd"]))
|
|
self.assertFalse(matches_any(".claude/agents/a.md", ["*.md"]))
|
|
self.assertTrue(matches_any("README.md", ["*.md"]))
|
|
self.assertFalse(matches_any("docs/a/b.md", ["docs?a/b.md"]))
|
|
self.assertTrue(matches_any("docs/x/b.md", ["docs/?/b.md"]))
|
|
self.assertFalse(matches_any("docs/a/b.md", ["docs[/]a/b.md"]))
|
|
self.assertTrue(matches_any("docs/b.md", ["docs/[ab].md"]))
|
|
self.assertFalse(matches_any("docs/c.md", ["docs/[ab].md"]))
|
|
self.assertTrue(matches_any("docs/c.md", ["docs/[!ab].md"]))
|
|
self.assertFalse(matches_any("docs/x/z.md", ["docs/**z.md"]))
|
|
|
|
def test_literal_characters_are_escaped(self):
|
|
self.assertTrue(matches_any("a+b (1).md", ["a+b (1).md"]))
|
|
self.assertFalse(matches_any("axb.md", ["a.b.md"]))
|
|
self.assertTrue(matches_any("[x.md", ["[x.md"]))
|
|
|
|
def test_matcher_agrees_with_expand_patterns(self):
|
|
files = ["README.md", "docs/a.md", "docs/w/b.qmd", "docs/w/x/c.qmd", ".c/a.md"]
|
|
patterns = ["*.md", "docs/**/*.qmd", "docs/w/*.qmd", "docs/*.md", "docs/w/?.qmd"]
|
|
with tmp_tree(files) as d:
|
|
for pattern in patterns:
|
|
expanded = set(expand_patterns([pattern], Path(d)))
|
|
matched = {f for f in files if matches_any(f, [pattern])}
|
|
self.assertEqual(matched, expanded, pattern)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|