diff --git a/scripts/dependency_graph.py b/scripts/dependency_graph.py index fa10601..0e2eeb3 100644 --- a/scripts/dependency_graph.py +++ b/scripts/dependency_graph.py @@ -131,7 +131,10 @@ def detect_cycles(graph: dict) -> list: return result elif neighbor in rec_stack: cycle_start = path.index(neighbor) - return path[cycle_start:] + [neighbor] + cycle = path[cycle_start:] + if cycle[-1] != neighbor: + cycle.append(neighbor) + return cycle rec_stack.remove(node) return None diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_dependency_graph.py b/tests/test_dependency_graph.py new file mode 100644 index 0000000..b1a28ce --- /dev/null +++ b/tests/test_dependency_graph.py @@ -0,0 +1,184 @@ +""" +Tests for dependency_graph — cross-repo dependency analysis. +""" + +import json +import tempfile +import unittest +from pathlib import Path +import sys + +# Make scripts importable +sys.path.insert(0, str(Path(__file__).parent.parent / "scripts")) + +from dependency_graph import ( + normalize_repo_name, + scan_file_for_deps, + detect_cycles, + scan_repo, +) + + +class TestNormalizeRepoName(unittest.TestCase): + def test_lowercase(self): + self.assertEqual(normalize_repo_name("Hermes-Agent"), "hermes-agent") + + def test_underscore_to_hyphen(self): + self.assertEqual(normalize_repo_name("timmy_config"), "timmy-config") + + def test_strip_git(self): + self.assertEqual(normalize_repo_name("the-nexus.git"), "the-nexus") + + +class TestScanFileForDeps(unittest.TestCase): + def test_python_import(self): + content = "from hermes_agent.tools import something" + deps = scan_file_for_deps("dummy.py", content, "my-repo") + self.assertIn("hermes-agent", deps) + + def test_python_import_from(self): + content = "import timmy_config.helpers" + deps = scan_file_for_deps("dummy.py", content, "my-repo") + self.assertIn("timmy-config", deps) + + def test_js_require(self): + content = 'const utils = require("the-nexus/utils");' + deps = scan_file_for_deps("dummy.js", content, "my-repo") + self.assertIn("the-nexus", deps) + + def test_es6_import(self): + content = 'import {something} from "fleet-ops";' + deps = scan_file_for_deps("dummy.ts", content, "my-repo") + self.assertIn("fleet-ops", deps) + + def test_go_import(self): + content = 'import "github.com/Timmy_Foundation/the-door/pkg"' + deps = scan_file_for_deps("dummy.go", content, "my-repo") + self.assertIn("the-door", deps) + + def test_ansible_include_role(self): + content = "- include_role:\n name: timmy-home.nginx" + deps = scan_file_for_deps("dummy.yml", content, "my-repo") + self.assertIn("timmy-home", deps) + + def test_yaml_repo_reference(self): + content = "repo: the-beacon\\ntrigger: push" + deps = scan_file_for_deps("dummy.yaml", content, "my-repo") + self.assertIn("the-beacon", deps) + + def test_exclude_self_reference(self): + content = "import hermes_agent" + deps = scan_file_for_deps("dummy.py", content, "hermes-agent") + self.assertNotIn("hermes-agent", deps) + + def test_no_deps(self): + content = "# no imports here" + deps = scan_file_for_deps("dummy.py", content, "my-repo") + self.assertEqual(len(deps), 0) + + +class TestDetectCycles(unittest.TestCase): + def test_no_cycles(self): + graph = { + "A": {"dependencies": ["B"]}, + "B": {"dependencies": ["C"]}, + "C": {"dependencies": []}, + } + cycles = detect_cycles(graph) + self.assertEqual(cycles, []) + + def test_simple_cycle(self): + graph = { + "A": {"dependencies": ["B"]}, + "B": {"dependencies": ["A"]}, + } + cycles = detect_cycles(graph) + self.assertEqual(len(cycles), 1) + self.assertIn("A", cycles[0]) + self.assertIn("B", cycles[0]) + + def test_three_node_cycle(self): + graph = { + "A": {"dependencies": ["B"]}, + "B": {"dependencies": ["C"]}, + "C": {"dependencies": ["A"]}, + } + cycles = detect_cycles(graph) + self.assertEqual(len(cycles), 1) + self.assertEqual(set(cycles[0]), {"A", "B", "C"}) + + def test_multiple_independent_cycles(self): + graph = { + "A": {"dependencies": ["B"]}, + "B": {"dependencies": ["A"]}, + "C": {"dependencies": ["D"]}, + "D": {"dependencies": ["C"]}, + } + cycles = detect_cycles(graph) + self.assertEqual(len(cycles), 2) + + def test_self_cycle(self): + graph = { + "A": {"dependencies": ["A"]}, + } + cycles = detect_cycles(graph) + self.assertEqual(len(cycles), 1) + self.assertEqual(cycles[0], ["A"]) + + +class TestScanRepo(unittest.TestCase): + def setUp(self): + self.tmpdir = tempfile.mkdtemp() + self.repo_path = Path(self.tmpdir) / "test-repo" + self.repo_path.mkdir() + + def tearDown(self): + import shutil + shutil.rmtree(self.tmpdir) + + def test_scan_simple_import(self): + # Create a Python file that imports another known repo + (self.repo_path / "main.py").write_text("from hermes_agent import tools") + result = scan_repo(str(self.repo_path), "test-repo") + self.assertIn("dependencies", result) + self.assertIn("hermes-agent", result["dependencies"]) + self.assertEqual(result["files_scanned"], 1) + + def test_scan_multiple_files(self): + (self.repo_path / "a.py").write_text("import timmy_config") + (self.repo_path / "b.js").write_text('require("the-nexus")') + (self.repo_path / "c.yml").write_text("include_role: fleet-ops") + result = scan_repo(str(self.repo_path), "test-repo") + deps = set(result["dependencies"]) + self.assertIn("timmy-config", deps) + self.assertIn("the-nexus", deps) + self.assertIn("fleet-ops", deps) + self.assertEqual(result["files_scanned"], 3) + + +class TestIntegration(unittest.TestCase): + """Integration test using real test_repos directory.""" + + def test_scan_timmy_config(self): + repo_root = Path(__file__).parent.parent + test_repos_dir = repo_root / "test_repos" + if not test_repos_dir.exists(): + self.skipTest("test_repos directory not found (needs cloning)") + + # The timmy-config repo fixture exists + tc = test_repos_dir / "timmy-config" + if not tc.exists(): + self.skipTest("timmy-config not cloned") + + result = scan_repo(str(tc), "timmy-config") + deps = set(result.get("dependencies", [])) + # timmy-config dependencies based on known codebase + expected = {"hermes-agent", "the-nexus", "fleet-ops"} + # At least some deps should be found + self.assertGreater(len(deps), 0, "Should find at least one dependency") + # Cross-check: hermes-agent definitely used + self.assertIn("hermes-agent", deps) + + +if __name__ == "__main__": + unittest.main()