|
| 1 | +import importlib.util |
| 2 | +import json |
| 3 | +import subprocess |
| 4 | +import sys |
| 5 | +import tempfile |
| 6 | +import unittest |
| 7 | +from io import StringIO |
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | + |
| 11 | +ROOT = Path(__file__).resolve().parents[1] |
| 12 | +CLI = ROOT / "tools" / "gateflow_cli.py" |
| 13 | + |
| 14 | + |
| 15 | +def load_cli(): |
| 16 | + spec = importlib.util.spec_from_file_location("gateflow_cli", CLI) |
| 17 | + module = importlib.util.module_from_spec(spec) |
| 18 | + spec.loader.exec_module(module) |
| 19 | + return module |
| 20 | + |
| 21 | + |
| 22 | +class GateFlowCliTests(unittest.TestCase): |
| 23 | + def test_create_agent_writes_claude_agent_file(self): |
| 24 | + cli = load_cli() |
| 25 | + |
| 26 | + with tempfile.TemporaryDirectory() as tmp: |
| 27 | + root = Path(tmp) |
| 28 | + result = cli.create_agent( |
| 29 | + root=root, |
| 30 | + name="Timing Closer", |
| 31 | + role="timing closure specialist", |
| 32 | + description="Closes timing on FPGA builds", |
| 33 | + color="cyan", |
| 34 | + tools=["Read", "Edit", "Bash"], |
| 35 | + force=False, |
| 36 | + ) |
| 37 | + |
| 38 | + self.assertEqual(root / "plugins/gateflow/agents/timing-closer.md", result.path) |
| 39 | + content = result.path.read_text(encoding="utf-8") |
| 40 | + self.assertIn("name: timing-closer", content) |
| 41 | + self.assertIn("color: cyan", content) |
| 42 | + self.assertIn(" - Bash", content) |
| 43 | + self.assertIn("timing closure specialist", content) |
| 44 | + self.assertIn("Closes timing on FPGA builds", content) |
| 45 | + |
| 46 | + def test_cli_agents_create_outputs_created_path(self): |
| 47 | + with tempfile.TemporaryDirectory() as tmp: |
| 48 | + completed = subprocess.run( |
| 49 | + [ |
| 50 | + sys.executable, |
| 51 | + str(CLI), |
| 52 | + "--root", |
| 53 | + tmp, |
| 54 | + "--plain", |
| 55 | + "agents", |
| 56 | + "create", |
| 57 | + "CDC Reviewer", |
| 58 | + "--role", |
| 59 | + "clock-domain crossing reviewer", |
| 60 | + "--description", |
| 61 | + "Reviews synchronizers and CDC constraints", |
| 62 | + "--tool", |
| 63 | + "Read", |
| 64 | + "--tool", |
| 65 | + "Grep", |
| 66 | + ], |
| 67 | + text=True, |
| 68 | + capture_output=True, |
| 69 | + check=False, |
| 70 | + ) |
| 71 | + |
| 72 | + self.assertEqual("", completed.stderr) |
| 73 | + self.assertEqual(0, completed.returncode) |
| 74 | + self.assertIn("created", completed.stdout) |
| 75 | + self.assertIn("cdc-reviewer.md", completed.stdout) |
| 76 | + self.assertTrue((Path(tmp) / "plugins/gateflow/agents/cdc-reviewer.md").exists()) |
| 77 | + |
| 78 | + def test_cli_status_json_returns_plugin_payload(self): |
| 79 | + completed = subprocess.run( |
| 80 | + [sys.executable, str(CLI), "--root", str(ROOT), "status", "--json"], |
| 81 | + text=True, |
| 82 | + capture_output=True, |
| 83 | + check=False, |
| 84 | + ) |
| 85 | + |
| 86 | + self.assertEqual("", completed.stderr) |
| 87 | + self.assertEqual(0, completed.returncode) |
| 88 | + payload = json.loads(completed.stdout) |
| 89 | + self.assertEqual("gateflow", payload["plugin"]["name"]) |
| 90 | + self.assertIn("actions", payload) |
| 91 | + |
| 92 | + def test_shell_help_mentions_agent_creation(self): |
| 93 | + cli = load_cli() |
| 94 | + |
| 95 | + help_text = cli.shell_help() |
| 96 | + |
| 97 | + self.assertIn("create-agent", help_text) |
| 98 | + self.assertIn("agents create", help_text) |
| 99 | + |
| 100 | + def test_agents_list_clips_long_descriptions(self): |
| 101 | + cli = load_cli() |
| 102 | + |
| 103 | + with tempfile.TemporaryDirectory() as tmp: |
| 104 | + root = Path(tmp) |
| 105 | + cli.create_agent( |
| 106 | + root=root, |
| 107 | + name="Long Description Agent", |
| 108 | + role="agent list display test", |
| 109 | + description="Reviews " + "very " * 30 + "long agent descriptions", |
| 110 | + color="green", |
| 111 | + tools=["Read"], |
| 112 | + force=False, |
| 113 | + ) |
| 114 | + output = StringIO() |
| 115 | + |
| 116 | + result = cli._print_agents(root, as_json=False, plain=True, output=output) |
| 117 | + |
| 118 | + lines = output.getvalue().splitlines() |
| 119 | + self.assertEqual(0, result) |
| 120 | + self.assertLessEqual(max(len(line) for line in lines), 100) |
| 121 | + self.assertIn("...", output.getvalue()) |
| 122 | + |
| 123 | + |
| 124 | +if __name__ == "__main__": |
| 125 | + unittest.main() |
0 commit comments