|
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_patterns |
|
63
|
|
|
64
|
def test_contains_react_dom(self): |
|
65
|
enricher = ReactEnricher(_mock_store()) |
|
66
|
assert "react-dom" in enricher.detection_patterns |
|
67
|
|
|
68
|
def test_contains_next(self): |
|
69
|
enricher = ReactEnricher(_mock_store()) |
|
70
|
assert "next" in enricher.detection_patterns |
|
71
|
|
|
72
|
def test_returns_list(self): |
|
73
|
enricher = ReactEnricher(_mock_store()) |
|
74
|
assert isinstance(enricher.detection_patterns, list) |
|
75
|
|
|
76
|
def test_detection_files_contains_next_config_variants(self): |
|
77
|
enricher = ReactEnricher(_mock_store()) |
|
78
|
files = enricher.detection_files |
|
79
|
assert any("next.config" in f for f in files) |
|
80
|
|
|
81
|
|
|
82
|
# ── detect() ───────────────────────────────────────────────────────────────── |
|
83
|
|
|
84
|
|
|
85
|
class TestDetect: |
|
86
|
def test_returns_true_when_react_pattern_found(self): |
|
87
|
store = _mock_store(result_set=[[1]]) |
|
88
|
enricher = ReactEnricher(store) |
|
89
|
assert enricher.detect() is True |
|
90
|
|
|
91
|
def test_returns_false_when_no_pattern_found(self): |
|
92
|
store = _mock_store(result_set=[[0]]) |
|
93
|
enricher = ReactEnricher(store) |
|
94
|
assert enricher.detect() is False |
|
95
|
|
|
96
|
def test_returns_false_on_empty_result_set(self): |
|
97
|
store = _mock_store(result_set=[]) |
|
98
|
enricher = ReactEnricher(store) |
|
99
|
assert enricher.detect() is False |
|
100
|
|
|
101
|
def test_short_circuits_on_first_match(self): |
|
102
|
store = _mock_store(result_set=[[3]]) |
|
103
|
enricher = ReactEnricher(store) |
|
104
|
assert enricher.detect() is True |
|
105
|
assert store._graph.query.call_count == 1 |
|
106
|
|
|
107
|
|
|
108
|
# ── enrich() ───────────────────────────────────────────────────────────────── |
|
109
|
|
|
110
|
|
|
111
|
class TestEnrich: |
|
112
|
def _make_enricher_empty(self): |
|
113
|
"""Enricher that returns empty result sets for every pattern query.""" |
|
114
|
# 5 pattern queries: components, pages, api_routes, hooks, stores |
|
115
|
store = _mock_store_with_responses([[], [], [], [], []]) |
|
116
|
return ReactEnricher(store) |
|
117
|
|
|
118
|
def test_returns_enrichment_result(self): |
|
119
|
enricher = self._make_enricher_empty() |
|
120
|
assert isinstance(enricher.enrich(), EnrichmentResult) |
|
121
|
|
|
122
|
def test_zero_promoted_when_no_matches(self): |
|
123
|
enricher = self._make_enricher_empty() |
|
124
|
result = enricher.enrich() |
|
125
|
assert result.promoted == 0 |
|
126
|
|
|
127
|
def test_patterns_found_keys_present(self): |
|
128
|
enricher = self._make_enricher_empty() |
|
129
|
result = enricher.enrich() |
|
130
|
assert "components" in result.patterns_found |
|
131
|
assert "pages" in result.patterns_found |
|
132
|
assert "api_routes" in result.patterns_found |
|
133
|
assert "hooks" in result.patterns_found |
|
134
|
assert "stores" in result.patterns_found |
|
135
|
|
|
136
|
def test_promotes_component_nodes(self): |
|
137
|
"""A JSX file node should be promoted to ReactComponent.""" |
|
138
|
store = _mock_store_with_responses( |
|
139
|
[ |
|
140
|
[["Button", "src/components/Button.jsx"]], # components |
|
141
|
[], # pages |
|
142
|
[], # api_routes |
|
143
|
[], # hooks |
|
144
|
[], # stores |
|
145
|
# _promote_node calls handled by fallback |
|
146
|
] |
|
147
|
) |
|
148
|
enricher = ReactEnricher(store) |
|
149
|
result = enricher.enrich() |
|
150
|
assert result.promoted == 1 |
|
151
|
assert result.patterns_found["components"] == 1 |
|
152
|
|
|
153
|
# Verify _promote_node called store.query with correct semantic_type |
|
154
|
calls = store._graph.query.call_args_list |
|
155
|
promote_calls = [c for c in calls if "SET n.semantic_type" in c[0][0]] |
|
156
|
assert len(promote_calls) == 1 |
|
157
|
_, params = promote_calls[0][0] |
|
158
|
assert params["semantic_type"] == "ReactComponent" |
|
159
|
assert params["name"] == "Button" |
|
160
|
|
|
161
|
def test_promotes_page_nodes(self): |
|
162
|
"""Nodes inside /pages/ directory should become NextPage.""" |
|
163
|
store = _mock_store_with_responses( |
|
164
|
[ |
|
165
|
[], # components |
|
166
|
[["IndexPage", "src/pages/index.tsx"]], # pages |
|
167
|
[], # api_routes |
|
168
|
[], # hooks |
|
169
|
[], # stores |
|
170
|
] |
|
171
|
) |
|
172
|
enricher = ReactEnricher(store) |
|
173
|
result = enricher.enrich() |
|
174
|
assert result.promoted == 1 |
|
175
|
assert result.patterns_found["pages"] == 1 |
|
176
|
|
|
177
|
calls = store._graph.query.call_args_list |
|
178
|
promote_calls = [c for c in calls if "SET n.semantic_type" in c[0][0]] |
|
179
|
assert promote_calls[0][0][1]["semantic_type"] == "NextPage" |
|
180
|
|
|
181
|
def test_promotes_api_route_nodes(self): |
|
182
|
"""Nodes inside /pages/api/ should become NextApiRoute.""" |
|
183
|
store = _mock_store_with_responses( |
|
184
|
[ |
|
185
|
[], # components |
|
186
|
[], # pages |
|
187
|
[["handler", "src/pages/api/user.ts"]], # api_routes |
|
188
|
[], # hooks |
|
189
|
[], # stores |
|
190
|
] |
|
191
|
) |
|
192
|
enricher = ReactEnricher(store) |
|
193
|
result = enricher.enrich() |
|
194
|
assert result.promoted == 1 |
|
195
|
assert result.patterns_found["api_routes"] == 1 |
|
196
|
|
|
197
|
calls = store._graph.query.call_args_list |
|
198
|
promote_calls = [c for c in calls if "SET n.semantic_type" in c[0][0]] |
|
199
|
assert promote_calls[0][0][1]["semantic_type"] == "NextApiRoute" |
|
200
|
|
|
201
|
def test_promotes_hook_nodes(self): |
|
202
|
"""Functions starting with 'use' should become ReactHook.""" |
|
203
|
store = _mock_store_with_responses( |
|
204
|
[ |
|
205
|
[], # components |
|
206
|
[], # pages |
|
207
|
[], # api_routes |
|
208
|
[["useAuth", "src/hooks/useAuth.ts"]], # hooks |
|
209
|
[], # stores |
|
210
|
] |
|
211
|
) |
|
212
|
enricher = ReactEnricher(store) |
|
213
|
result = enricher.enrich() |
|
214
|
assert result.promoted == 1 |
|
215
|
assert result.patterns_found["hooks"] == 1 |
|
216
|
|
|
217
|
calls = store._graph.query.call_args_list |
|
218
|
promote_calls = [c for c in calls if "SET n.semantic_type" in c[0][0]] |
|
219
|
assert promote_calls[0][0][1]["semantic_type"] == "ReactHook" |
|
220
|
|
|
221
|
def test_promotes_store_nodes(self): |
|
222
|
"""createStore / useStore patterns should become ReactStore.""" |
|
223
|
store = _mock_store_with_responses( |
|
224
|
[ |
|
225
|
[], # components |
|
226
|
[], # pages |
|
227
|
[], # api_routes |
|
228
|
[], # hooks |
|
229
|
[["createStore", "src/store/index.ts"]], # stores |
|
230
|
] |
|
231
|
) |
|
232
|
enricher = ReactEnricher(store) |
|
233
|
result = enricher.enrich() |
|
234
|
assert result.promoted == 1 |
|
235
|
assert result.patterns_found["stores"] == 1 |
|
236
|
|
|
237
|
calls = store._graph.query.call_args_list |
|
238
|
promote_calls = [c for c in calls if "SET n.semantic_type" in c[0][0]] |
|
239
|
assert promote_calls[0][0][1]["semantic_type"] == "ReactStore" |
|
240
|
|
|
241
|
def test_promoted_count_accumulates_across_patterns(self): |
|
242
|
"""promoted should be the sum of all matched nodes.""" |
|
243
|
# Each matched node triggers a _promote_node SET query after its pattern query. |
|
244
|
# Responses must be interleaved: pattern_query, promote, promote, pattern_query, ... |
|
245
|
store = _mock_store_with_responses( |
|
246
|
[ |
|
247
|
[["Btn", "a.jsx"], ["Input", "b.tsx"]], # components query (2 rows) |
|
248
|
None, # _promote_node for Btn |
|
249
|
None, # _promote_node for Input |
|
250
|
[["Home", "pages/index.tsx"]], # pages query (1 row) |
|
251
|
None, # _promote_node for Home |
|
252
|
[], # api_routes query |
|
253
|
[["useUser", "hooks/useUser.ts"]], # hooks query (1 row) |
|
254
|
None, # _promote_node for useUser |
|
255
|
[], # stores query |
|
256
|
] |
|
257
|
) |
|
258
|
enricher = ReactEnricher(store) |
|
259
|
result = enricher.enrich() |
|
260
|
assert result.promoted == 4 |
|
261
|
assert result.patterns_found["components"] == 2 |
|
262
|
assert result.patterns_found["pages"] == 1 |
|
263
|
assert result.patterns_found["hooks"] == 1 |
|
264
|
|