|
| 1 | +import os |
| 2 | +import subprocess |
| 3 | +import sys |
| 4 | +import tempfile |
| 5 | +import unittest |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | +from validate_agent import validate_agent |
| 9 | + |
| 10 | + |
| 11 | +ROOT = Path(__file__).resolve().parents[1] |
| 12 | +VALID = """--- |
| 13 | +schema_version: 1 |
| 14 | +kind: subagent |
| 15 | +id: investigator |
| 16 | +name: Investigator |
| 17 | +description: Investigate code |
| 18 | +tools: [Read, Grep] |
| 19 | +readonly: true |
| 20 | +--- |
| 21 | +Inspect the requested code and report evidence. |
| 22 | +""" |
| 23 | + |
| 24 | + |
| 25 | +class AgentValidationTests(unittest.TestCase): |
| 26 | + def check_text(self, text, scope=None): |
| 27 | + with tempfile.TemporaryDirectory() as directory: |
| 28 | + path = Path(directory) / "agent.md" |
| 29 | + path.write_bytes(text.encode("utf-8")) |
| 30 | + before = path.read_bytes() |
| 31 | + errors = validate_agent(path, scope) |
| 32 | + self.assertEqual(path.read_bytes(), before) |
| 33 | + return errors |
| 34 | + |
| 35 | + def test_bundled_templates(self): |
| 36 | + self.assertEqual(validate_agent(ROOT / "assets/mode.md", "user"), []) |
| 37 | + self.assertEqual(validate_agent(ROOT / "assets/subagent.md", "project"), []) |
| 38 | + |
| 39 | + def test_optional_fields_and_line_endings(self): |
| 40 | + for change in [ |
| 41 | + VALID.replace("tools: [Read, Grep]\n", ""), |
| 42 | + VALID.replace("[Read, Grep]", "[]"), |
| 43 | + VALID.replace("[Read, Grep]", "Read, Grep"), |
| 44 | + VALID.replace("readonly: true", "model: my-private-model-42"), |
| 45 | + VALID.replace("readonly: true", "model: inherit"), |
| 46 | + VALID.replace("\n", "\r\n"), |
| 47 | + ]: |
| 48 | + with self.subTest(text=change): |
| 49 | + self.assertEqual(self.check_text(change), []) |
| 50 | + |
| 51 | + def test_invalid_yaml_and_document_shape(self): |
| 52 | + for change in [ |
| 53 | + VALID.replace("[Read, Grep]", "[Read"), |
| 54 | + VALID.replace("id: investigator", "id: investigator\nid: duplicate"), |
| 55 | + VALID.replace("readonly: true", "readonly: !!python/object:example {}"), |
| 56 | + "---\n[one, two]\n---\nPrompt", "---\n---\nPrompt", |
| 57 | + "\ufeff" + VALID, VALID.replace("---\n", "", 1), |
| 58 | + VALID.split("Inspect the requested")[0], |
| 59 | + ]: |
| 60 | + with self.subTest(text=change): |
| 61 | + self.assertTrue(self.check_text(change)) |
| 62 | + |
| 63 | + def test_field_values(self): |
| 64 | + changes = [ |
| 65 | + ("schema_version: 1", "schema_version: true"), |
| 66 | + ("schema_version: 1", "schema_version: 2"), |
| 67 | + ("kind: subagent", "kind: other"), |
| 68 | + ("id: investigator", "id: ../agent"), |
| 69 | + ("name: Investigator", "name: null"), |
| 70 | + ("description: Investigate code", 'description: ""'), |
| 71 | + ("readonly: true", 'readonly: "true"'), |
| 72 | + ("readonly: true", "readonly: yes"), |
| 73 | + ("readonly: true", "model: 42"), |
| 74 | + ("readonly: true", "model: null"), |
| 75 | + ("readonly: true", "typo: true"), |
| 76 | + ("[Read, Grep]", "[Read, UnknownTool]"), |
| 77 | + ("[Read, Grep]", "[Read, mcp__example__tool]"), |
| 78 | + ("[Read, Grep]", "[Read, 1]"), |
| 79 | + ("[Read, Grep]", "null"), |
| 80 | + ("[Read, Grep]", "[read]"), |
| 81 | + ("readonly: true", "user_context_policy: [unknown]"), |
| 82 | + ] |
| 83 | + for old, new in changes: |
| 84 | + with self.subTest(new=new): |
| 85 | + self.assertTrue(self.check_text(VALID.replace(old, new))) |
| 86 | + |
| 87 | + def test_scope_and_review_kind(self): |
| 88 | + mode = VALID.replace("kind: subagent", "kind: mode") |
| 89 | + self.assertTrue(self.check_text(mode, "project")) |
| 90 | + self.assertEqual(self.check_text(mode, "user"), []) |
| 91 | + self.assertTrue(self.check_text(mode.replace("readonly: true", "review: true"))) |
| 92 | + |
| 93 | + def test_cli_exit_codes_and_missing_file(self): |
| 94 | + for name, expected in [("assets/mode.md", 0), ("missing.md", 1)]: |
| 95 | + result = subprocess.run( |
| 96 | + [sys.executable, str(ROOT / "scripts/validate_agent.py"), str(ROOT / name)], |
| 97 | + capture_output=True, text=True, |
| 98 | + creationflags=subprocess.CREATE_NO_WINDOW if os.name == "nt" else 0, |
| 99 | + ) |
| 100 | + self.assertEqual(result.returncode, expected, result.stderr) |
| 101 | + self.assertNotIn("Traceback", result.stderr) |
| 102 | + |
| 103 | + |
| 104 | +if __name__ == "__main__": |
| 105 | + unittest.main() |
0 commit comments