Compare commits

...

1 Commits

Author SHA1 Message Date
Alexander Payne
77e7e5daeb feat(test): add dependency_graph test suite + fix self-cycle duplicate
Some checks failed
Test / pytest (pull_request) Failing after 9s
- 20 unit/integration tests covering normalize_repo_name, scan_file_for_deps,
  detect_cycles, and full repo scan.
- Fix detect_cycles self-loop returning duplicate node ([A,A] → [A]).
- Integration test validates timmy-config cross-repo detection.

Ref: #93 – provides test coverage to enable confident maintenance
  of cross-repo dependency graph capability.
2026-04-26 00:51:08 -04:00
3 changed files with 188 additions and 1 deletions

View File

@@ -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

0
tests/__init__.py Normal file
View File

View File

@@ -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()