|
| 1 | +"""Tests for the JavaScript analyzer - extraction only (no DB required).""" |
| 2 | + |
| 3 | +import unittest |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +from api.analyzers.javascript.analyzer import JavaScriptAnalyzer |
| 7 | +from api.entities.entity import Entity |
| 8 | +from api.entities.file import File |
| 9 | + |
| 10 | + |
| 11 | +def _entity_name(analyzer, entity): |
| 12 | + """Get the name of an entity using the analyzer.""" |
| 13 | + return analyzer.get_entity_name(entity.node) |
| 14 | + |
| 15 | + |
| 16 | +class TestJavaScriptAnalyzer(unittest.TestCase): |
| 17 | + @classmethod |
| 18 | + def setUpClass(cls): |
| 19 | + cls.analyzer = JavaScriptAnalyzer() |
| 20 | + source_dir = Path(__file__).parent / "source_files" / "javascript" |
| 21 | + cls.sample_path = source_dir / "sample.js" |
| 22 | + source = cls.sample_path.read_bytes() |
| 23 | + tree = cls.analyzer.parser.parse(source) |
| 24 | + cls.file = File(cls.sample_path, tree) |
| 25 | + |
| 26 | + # Walk AST and extract entities (mirrors create_hierarchy without Graph) |
| 27 | + types = cls.analyzer.get_entity_types() |
| 28 | + stack = [tree.root_node] |
| 29 | + while stack: |
| 30 | + node = stack.pop() |
| 31 | + if node.type in types: |
| 32 | + entity = Entity(node) |
| 33 | + cls.analyzer.add_symbols(entity) |
| 34 | + cls.file.add_entity(entity) |
| 35 | + # Also recurse into entity children (e.g., class body methods) |
| 36 | + stack.extend(node.children) |
| 37 | + else: |
| 38 | + stack.extend(node.children) |
| 39 | + |
| 40 | + def _entity_names(self): |
| 41 | + return [_entity_name(self.analyzer, e) for e in self.file.entities.values()] |
| 42 | + |
| 43 | + def test_discovers_js_files(self): |
| 44 | + """SourceAnalyzer should enumerate .js files.""" |
| 45 | + source_dir = Path(__file__).parent / "source_files" / "javascript" |
| 46 | + js_files = list(source_dir.rglob("*.js")) |
| 47 | + self.assertTrue(len(js_files) > 0, "Should find .js files") |
| 48 | + |
| 49 | + def test_entity_types(self): |
| 50 | + """Analyzer should recognise JS entity types.""" |
| 51 | + self.assertEqual( |
| 52 | + self.analyzer.get_entity_types(), |
| 53 | + ['function_declaration', 'class_declaration', 'method_definition'], |
| 54 | + ) |
| 55 | + |
| 56 | + def test_class_extraction(self): |
| 57 | + """Classes should be extracted from sample.js.""" |
| 58 | + names = self._entity_names() |
| 59 | + self.assertIn("Shape", names) |
| 60 | + self.assertIn("Circle", names) |
| 61 | + |
| 62 | + def test_function_extraction(self): |
| 63 | + """Top-level functions should be extracted.""" |
| 64 | + names = self._entity_names() |
| 65 | + self.assertIn("calculateTotal", names) |
| 66 | + |
| 67 | + def test_method_extraction(self): |
| 68 | + """Class methods should be extracted.""" |
| 69 | + names = self._entity_names() |
| 70 | + self.assertIn("area", names) |
| 71 | + self.assertIn("constructor", names) |
| 72 | + |
| 73 | + def test_class_labels(self): |
| 74 | + """Classes should get the 'Class' label.""" |
| 75 | + for entity in self.file.entities.values(): |
| 76 | + if _entity_name(self.analyzer, entity) in ("Shape", "Circle"): |
| 77 | + self.assertEqual(self.analyzer.get_entity_label(entity.node), "Class") |
| 78 | + |
| 79 | + def test_function_label(self): |
| 80 | + """Functions should get the 'Function' label.""" |
| 81 | + for entity in self.file.entities.values(): |
| 82 | + if _entity_name(self.analyzer, entity) == "calculateTotal": |
| 83 | + self.assertEqual(self.analyzer.get_entity_label(entity.node), "Function") |
| 84 | + |
| 85 | + def test_base_class_symbol(self): |
| 86 | + """Circle should have Shape as a base_class symbol.""" |
| 87 | + for entity in self.file.entities.values(): |
| 88 | + if _entity_name(self.analyzer, entity) == "Circle": |
| 89 | + base_names = [ |
| 90 | + s.symbol.text.decode("utf-8") |
| 91 | + for s in entity.symbols.get("base_class", []) |
| 92 | + ] |
| 93 | + self.assertIn("Shape", base_names) |
| 94 | + |
| 95 | + def test_is_dependency(self): |
| 96 | + """node_modules paths should be flagged as dependencies.""" |
| 97 | + self.assertTrue(self.analyzer.is_dependency("foo/node_modules/bar/index.js")) |
| 98 | + self.assertFalse(self.analyzer.is_dependency("src/utils.js")) |
| 99 | + |
| 100 | + |
| 101 | +if __name__ == "__main__": |
| 102 | + unittest.main() |
0 commit comments