| | @@ -0,0 +1,260 @@ |
| 1 | +"""Tests for navegador.enrichment.react — ReactEnricher."""
|
| 2 | +
|
| 3 | +from unittest.mock import MagicMock, call
|
| 4 | +
|
| 5 | +import pytest
|
| 6 | +
|
| 7 | +from navegador.enrichment import EnrichmentResult
|
| 8 | +from navegador.enrichment.react import ReactEnricher
|
| 9 | +from navegador.graph.store import GraphStore
|
| 10 | +
|
| 11 | +
|
| 12 | +# ── Helpers ───────────────────────────────────────────────────────────────────
|
| 13 | +
|
| 14 | +
|
| 15 | +def _mock_store(result_set=None):
|
| 16 | + """Return a GraphStore backed by a mock FalkorDB graph."""
|
| 17 | + client = MagicMock()
|
| 18 | + graph = MagicMock()
|
| 19 | + graph.query.return_value = MagicMock(result_set=result_set)
|
| 20 | + client.select_graph.return_value = graph
|
| 21 | + return GraphStore(client)
|
| 22 | +
|
| 23 | +
|
| 24 | +def _mock_store_with_responses(responses):
|
| 25 | + """Return a GraphStore whose graph.query returns the given result sets in order.
|
| 26 | +
|
| 27 | + Any calls beyond the provided list (e.g. _promote_node SET queries) receive
|
| 28 | + a MagicMock with result_set=None, which is harmless since the enrichers do
|
| 29 | + not inspect the return value of promote calls.
|
| 30 | + """
|
| 31 | + client = MagicMock()
|
| 32 | + graph = MagicMock()
|
| 33 | + response_iter = iter(responses)
|
| 34 | +
|
| 35 | + def _side_effect(cypher, params):
|
| 36 | + try:
|
| 37 | + rs = next(response_iter)
|
| 38 | + except StopIteration:
|
| 39 | + rs = None
|
| 40 | + return MagicMock(result_set=rs)
|
| 41 | +
|
| 42 | + graph.query.side_effect = _side_effect
|
| 43 | + client.select_graph.return_value = graph
|
| 44 | + return GraphStore(client)
|
| 45 | +
|
| 46 | +
|
| 47 | +# ── framework_name ────────────────────────────────────────────────────────────
|
| 48 | +
|
| 49 | +
|
| 50 | +class TestFrameworkName:
|
| 51 | + def test_framework_name_is_react(self):
|
| 52 | + enricher = ReactEnricher(_mock_store())
|
| 53 | + assert enricher.framework_name == "react"
|
| 54 | +
|
| 55 | +
|
| 56 | +# ── detection_patterns ────────────────────────────────────────────────────────
|
| 57 | +
|
| 58 | +
|
| 59 | +class TestDetectionPatterns:
|
| 60 | + def test_contains_react(self):
|
| 61 | + enricher = ReactEnricher(_mock_store())
|
| 62 | + assert "react" in enricher.detection_patteReact(self):
|
| 63 | + enricher = ReactEnricher(_mock_store())
|
| 64 | + assert "React"""Tests for navegador.enrichment"""Test(self):
|
| 65 | + enricher = ReactEnricher(_mock_store())
|
| 66 | + assert "react" in enricher.detection_patterns
|
| 67 | +
|
| 68 | + def test_contains_react_dom(self):
|
| 69 | + enricher = ReactEnricher(_mock_store())
|
| 70 | + assert "react-dom" in enricher.detection_patterns
|
| 71 | +
|
| 72 | + def test_contains_next(self):
|
| 73 | + enricher = ReactEnricher(_mock_store())
|
| 74 | + assert "next" in enricher.detection_patterns
|
| 75 | +
|
| 76 | + def test_returns_list(self):
|
| 77 | + enricher = ReactEnricher(_mock_store())
|
| 78 | + assert isinstance(enricher.detection_patterns, list)
|
| 79 | +
|
| 80 | + def test_detection_files_contains_next_config_variants(self):
|
| 81 | + enricher = ReactEnricher(_mock_store())
|
| 82 | + files = enricher.detection_files
|
| 83 | + assert any("next.config" in f for f in files)
|
| 84 | +
|
| 85 | +
|
| 86 | +# ── detect() ─────────────────────────────────────────────────────────────────
|
| 87 | +
|
| 88 | +
|
| 89 | +class TestDetect:
|
| 90 | + def test_returns_true_when_react_pattern_found(self):
|
| 91 | + store = _mock_store(result_set=[[1]])
|
| 92 | + enricher = ReactEnricher(store)
|
| 93 | + assert enricher.detect() is True
|
| 94 | +
|
| 95 | + def test_returns_false_when_no_pattern_found(self):
|
| 96 | + store = _mock_store(result_set=[[0]])
|
| 97 | + enricher = ReactEnricher(store)
|
| 98 | + assert enricher.detect() is False
|
| 99 | +
|
| 100 | + def test_returns_false_on_empty_result_set(self):
|
| 101 | + store = _mock_store(result_set=[])
|
| 102 | + enricher = ReactEnricher(store)
|
| 103 | + assert enricher.detect() is False
|
| 104 | +
|
| 105 | + def test_short_circuits_on_first_match(self):
|
| 106 | + store = _mock_store(result_set=[[3]])
|
| 107 | + enricher = ReactEnricher(store)
|
| 108 | + assert enricher.detect() is True
|
| 109 | + assert store._graph.query.call_count == 1
|
| 110 | +
|
| 111 | +
|
| 112 | +# ── enrich() ─────────────────────────────────────────────────────────────────
|
| 113 | +
|
| 114 | +
|
| 115 | +class TestEnrich:
|
| 116 | + def _make_enricher_empty(self):
|
| 117 | + """Enricher that returns empty result sets for every pattern query."""
|
| 118 | + # 5 pattern queries: components, pages, api_routes, hooks, stores
|
| 119 | + store = _mock_store_with_responses([[], [], [], [], []])
|
| 120 | + return ReactEnricher(store)
|
| 121 | +
|
| 122 | + def test_returns_enrichment_result(self):
|
| 123 | + enricher = self._make_enricher_empty()
|
| 124 | + assert isinstance(enricher.enrich(), EnrichmentResult)
|
| 125 | +
|
| 126 | + def test_zero_promoted_when_no_matches(self):
|
| 127 | + enricher = self._make_enricher_empty()
|
| 128 | + result = enricher.enrich()
|
| 129 | + assert result.promoted == 0
|
| 130 | +
|
| 131 | + def test_patterns_found_keys_present(self):
|
| 132 | + enricher = self._make_enricher_empty()
|
| 133 | + result = enricher.enrich()
|
| 134 | + assert "components" in result.patterns_found
|
| 135 | + assert "pages" in result.patterns_found
|
| 136 | + assert "api_routes" in result.patterns_found
|
| 137 | + assert "hooks" in result.patterns_found
|
| 138 | + assert "stores" in result.patterns_found
|
| 139 | +
|
| 140 | + def test_promotes_component_nodes(self):
|
| 141 | + """A JSX file node should be promoted to ReactComponent."""
|
| 142 | + store = _mock_store_with_responses(
|
| 143 | + [
|
| 144 | + [["Button", "src/components/Button.jsx"]], # components
|
| 145 | + [], # pages
|
| 146 | + [], # api_routes
|
| 147 | + [], # hooks
|
| 148 | + [], # stores
|
| 149 | + # _promote_node calls handled by fallback
|
| 150 | + ]
|
| 151 | + )
|
| 152 | + enricher = ReactEnricher(store)
|
| 153 | + result = enricher.enrich()
|
| 154 | + assert result.promoted == 1
|
| 155 | + assert result.patterns_found["components"] == 1
|
| 156 | +
|
| 157 | + # Verify _promote_node called store.query with correct semantic_type
|
| 158 | + calls = store._graph.query.call_args_list
|
| 159 | + promote_calls = [c for c in calls if "SET n.semantic_type" in c[0][0]]
|
| 160 | + assert len(promote_calls) == 1
|
| 161 | + _, params = promote_calls[0][0]
|
| 162 | + assert params["semantic_type"] == "ReactComponent"
|
| 163 | + assert params["name"] == "Button"
|
| 164 | +
|
| 165 | + def test_promotes_page_nodes(self):
|
| 166 | + """Nodes inside /pages/ directory should become NextPage."""
|
| 167 | + store = _mock_store_with_responses(
|
| 168 | + [
|
| 169 | + [], # components
|
| 170 | + [["IndexPage", "src/pages/index.tsx"]], # pages
|
| 171 | + [], # api_routes
|
| 172 | + [], # hooks
|
| 173 | + [], # stores
|
| 174 | + ]
|
| 175 | + )
|
| 176 | + enricher = ReactEnricher(store)
|
| 177 | + result = enricher.enrich()
|
| 178 | + assert result.promoted == 1
|
| 179 | + assert result.patterns_found["pages"] == 1
|
| 180 | +
|
| 181 | + calls = store._graph.query.call_args_list
|
| 182 | + promote_calls = [c for c in calls if "SET n.semantic_type" in c[0][0]]
|
| 183 | + assert promote_calls[0][0][1]["semantic_type"] == "NextPage"
|
| 184 | +
|
| 185 | + def test_promotes_api_route_nodes(self):
|
| 186 | + """Nodes inside /pages/api/ should become NextApiRoute."""
|
| 187 | + store = _mock_store_with_responses(
|
| 188 | + [
|
| 189 | + [], # components
|
| 190 | + [], # pages
|
| 191 | + [["handler", "src/pages/api/user.ts"]], # api_routes
|
| 192 | + [], # hooks
|
| 193 | + [], # stores
|
| 194 | + ]
|
| 195 | + )
|
| 196 | + enricher = ReactEnricher(store)
|
| 197 | + result = enricher.enrich()
|
| 198 | + assert result.promoted == 1
|
| 199 | + assert result.patterns_found["api_routes"] == 1
|
| 200 | +
|
| 201 | + calls = store._graph.query.call_args_list
|
| 202 | + promote_calls = [c for c in calls if "SET n.semantic_type" in c[0][0]]
|
| 203 | + assert promote_calls[0][0][1]["semantic_type"] == "NextApiRoute"
|
| 204 | +
|
| 205 | + def test_promotes_hook_nodes(self):
|
| 206 | + """Functions starting with 'use' should become ReactHook."""
|
| 207 | + store = _mock_store_with_responses(
|
| 208 | + [
|
| 209 | + [], # components
|
| 210 | + [], # pages
|
| 211 | + [], # api_routes
|
| 212 | + [["useAuth", "src/hooks/useAuth.ts"]], # hooks
|
| 213 | + [], # stores
|
| 214 | + ]
|
| 215 | + )
|
| 216 | + enricher = ReactEnricher(store)
|
| 217 | + result = enricher.enrich()
|
| 218 | + assert result.promoted == 1
|
| 219 | + assert result.patterns_found["hooks"] == 1
|
| 220 | +
|
| 221 | + calls = store._graph.query.call_args_list
|
| 222 | + promote_calls = [c for c in calls if "SET n.semantic_type" in c[0][0]]
|
| 223 | + assert promote_calls[0][0][1]["semantic_type"] == "ReactHook"
|
| 224 | +
|
| 225 | + def test_promotes_store_nodes(self):
|
| 226 | + """createStore / useStore patterns should become ReactStore."""
|
| 227 | + store = _mock_store_with_responses(
|
| 228 | + [
|
| 229 | + [], # components
|
| 230 | + [], # pages
|
| 231 | + [], # api_routes
|
| 232 | + [], # hooks
|
| 233 | + [["createStore", "src/store/index.ts"]], # stores
|
| 234 | + ]
|
| 235 | + )
|
| 236 | + enricher = ReactEnricher(store)
|
| 237 | + result = enricher.enrich()
|
| 238 | + assert result.promoted == 1
|
| 239 | + assert result.patterns_found["stores"] == 1
|
| 240 | +
|
| 241 | + calls = store._graph.query.call_args_list
|
| 242 | + promote_calls = [c for c in calls if "SET n.semantic_type" in c[0][0]]
|
| 243 | + assert promote_calls[0][0][1]["semantic_type"] == "ReactStore"
|
| 244 | +
|
| 245 | + def test_promoted_count_accumulates_across_patterns(self):
|
| 246 | + """promoted should be the sum of all matched nodes."""
|
| 247 | + # Each matched node triggers a _promote_node SET query after its pattern query.
|
| 248 | + # Responses must be interleaved: pattern_query, promote, promote, pattern_query, ...
|
| 249 | + store = _mock_store_with_responses(
|
| 250 | + [
|
| 251 | + [["Btn", "a.jsx"], ["Input", "b.tsx"]], # components query (2 rows)
|
| 252 | + None, # _promote_node for Btn
|
| 253 | + None, # _promote_node for Input
|
| 254 | + [["Home", "pages/index.tsx"]], # pages query (1 row)
|
| 255 | + None, # _promote_node for Home
|
| 256 | + [], # api_routes query
|
| 257 | + [["useUser", "hooks/useUser.ts"]], # hooks query (1 row)
|
| 258 | + None, # _promote_node for useUser
|
| 259 | + [], # stores query
|
| 260 | + |