forked from FSoft-AI4Code/CodeWiki
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_fqdn_normalization.py
More file actions
242 lines (177 loc) · 8.16 KB
/
Copy pathtest_fqdn_normalization.py
File metadata and controls
242 lines (177 loc) · 8.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
"""
Test Cases for FQDN Normalization Fix
Run with: python -m pytest test_fqdn_normalization.py -v
"""
import pytest
from collections import namedtuple
# Mock Node class for testing
Node = namedtuple('Node', ['short_id'])
def test_strip_deps_prefix():
"""Test that 'deps.' prefix is correctly stripped."""
# Simulated components dictionary
components = {
"openframe-oss-lib.src.main.java.Class": Node(short_id="Class"),
"openframe-api.src.main.java.Service": Node(short_id="Service"),
}
# Simulated LLM output with "deps." prefix
llm_output = [
"deps.openframe-oss-lib.src.main.java.Class",
"deps.openframe-api.src.main.java.Service",
]
# Expected normalized output (after stripping)
expected = [
"openframe-oss-lib.src.main.java.Class",
"openframe-api.src.main.java.Service",
]
# Test normalization
for llm_id, expected_fqdn in zip(llm_output, expected):
stripped = llm_id[5:] if llm_id.startswith("deps.") else llm_id
assert stripped in components, f"Failed to find {stripped} after stripping"
assert stripped == expected_fqdn
def test_fuzzy_component_name_match():
"""Test fuzzy matching by component name (last segment)."""
components = {
"openframe-oss-lib.src.main.java.config.pinot.PinotConfigInitializer": Node(short_id="PinotConfigInitializer"),
"openframe-api.src.main.java.auth.AuthService": Node(short_id="AuthService"),
}
# LLM output with wrong path but correct component name
llm_id = "deps.openframe-oss-lib.openframe-management-service-core.src.main.java.PinotConfigInitializer"
# Extract component name
component_name = llm_id.split('.')[-1]
assert component_name == "PinotConfigInitializer"
# Find matches
matches = [fqdn for fqdn in components.keys() if fqdn.split('.')[-1] == component_name]
assert len(matches) == 1, f"Expected 1 match, found {len(matches)}"
assert matches[0] == "openframe-oss-lib.src.main.java.config.pinot.PinotConfigInitializer"
def test_path_suffix_matching():
"""Test matching by path suffix (last N segments)."""
components = {
"openframe-oss-lib.different.path.java.config.pinot.PinotConfigInitializer": Node(short_id="PinotConfigInitializer"),
}
llm_id = "deps.openframe-oss-lib.src.main.java.config.pinot.PinotConfigInitializer"
# Try matching last 3 segments
segments = llm_id.split('.')
suffix_3 = '.'.join(segments[-3:]) # "config.pinot.PinotConfigInitializer"
matches = [fqdn for fqdn in components.keys() if fqdn.endswith(suffix_3)]
assert len(matches) == 1
assert matches[0] == "openframe-oss-lib.different.path.java.config.pinot.PinotConfigInitializer"
def test_exact_fqdn_match():
"""Test that exact FQDN matches work without modification."""
components = {
"main-repo.src.services.user_service.UserService": Node(short_id="UserService"),
}
llm_id = "main-repo.src.services.user_service.UserService"
assert llm_id in components
def test_short_id_mapping():
"""Test that short ID → FQDN mapping works."""
components = {
"main-repo.src.services.user_service.UserService": Node(short_id="UserService"),
"main-repo.src.utils.logger.Logger": Node(short_id="Logger"),
}
# Build mapping
mapping = {}
for fqdn, node in components.items():
short_id = node.short_id or fqdn.split('.')[-1]
mapping[short_id] = fqdn
# Test mapping
assert mapping["UserService"] == "main-repo.src.services.user_service.UserService"
assert mapping["Logger"] == "main-repo.src.utils.logger.Logger"
def test_partial_path_mapping():
"""Test that partial paths are mapped correctly."""
components = {
"main-repo.src.services.auth.UserService": Node(short_id="UserService"),
}
# Build enhanced mapping with partial paths
mapping = {}
for fqdn, node in components.items():
segments = fqdn.split('.')
# Map short ID
short_id = node.short_id or segments[-1]
mapping[short_id] = fqdn
# Map partial paths (last 2-4 segments)
for i in range(2, min(5, len(segments) + 1)):
partial = '.'.join(segments[-i:])
if partial not in mapping:
mapping[partial] = fqdn
# Test mappings
assert "UserService" in mapping
assert "auth.UserService" in mapping
assert "services.auth.UserService" in mapping
assert "src.services.auth.UserService" in mapping
def test_collision_detection():
"""Test that collisions are detected when same short ID maps to multiple FQDNs."""
from collections import defaultdict
components = {
"main-repo.src.services.user_service.UserService": Node(short_id="UserService"),
"main-repo.src.admin.user_service.UserService": Node(short_id="UserService"), # Collision!
}
mapping = {}
collisions = defaultdict(list)
for fqdn, node in components.items():
short_id = node.short_id or fqdn.split('.')[-1]
if short_id in mapping:
collisions[short_id].append(fqdn)
else:
mapping[short_id] = fqdn
assert "UserService" in collisions
assert len(collisions["UserService"]) >= 1 # At least one collision
def test_best_path_match_scoring():
"""Test the path similarity scoring algorithm."""
llm_id = "deps.openframe-oss-lib.src.main.java.config.pinot.PinotConfigInitializer"
candidates = [
"openframe-oss-lib.src.main.java.config.pinot.PinotConfigInitializer", # Perfect match
"openframe-oss-lib.different.path.config.pinot.PinotConfigInitializer", # Partial match
"other-repo.src.main.java.config.pinot.PinotConfigInitializer", # Different namespace
]
# Score candidates by matching segments
llm_segments = llm_id.split('.')
scores = []
for candidate in candidates:
candidate_segments = candidate.split('.')
matches = sum(1 for seg in llm_segments if seg in candidate_segments)
scores.append((candidate, matches))
# Sort by score
scores.sort(key=lambda x: x[1], reverse=True)
# Best match should have highest score
assert scores[0][0] == "openframe-oss-lib.src.main.java.config.pinot.PinotConfigInitializer"
assert scores[0][1] > scores[2][1] # Better than different namespace
def test_non_existent_component():
"""Test that non-existent components fail normalization."""
components = {
"main-repo.src.services.UserService": Node(short_id="UserService"),
}
llm_id = "deps.openframe-oss-lib.hallucinated.Component"
# Should not match anything
stripped = llm_id[5:] if llm_id.startswith("deps.") else llm_id
assert stripped not in components
component_name = llm_id.split('.')[-1]
matches = [fqdn for fqdn in components.keys() if component_name in fqdn]
assert len(matches) == 0
def test_double_class_name():
"""Test handling of paths with duplicate component names."""
# This tests the scenario: PintoConfigInitializer.PinotConfigInitializer
components = {
"openframe-oss-lib.src.main.java.config.pinot.PintoConfigInitializer.PinotConfigInitializer": Node(
short_id="PinotConfigInitializer"
),
}
llm_id = "deps.openframe-oss-lib.openframe-management-service-core.src.main.java.com.openframe.management.config.pinot.PintoConfigInitializer.PinotConfigInitializer"
# Extract last segment (might be duplicated)
component_name = llm_id.split('.')[-1]
# Should match by component name
matches = [fqdn for fqdn in components.keys() if fqdn.split('.')[-1] == component_name]
assert len(matches) == 1
def test_java_package_path():
"""Test handling of Java package paths with com.openframe prefix."""
components = {
"openframe-oss-lib.src.main.java.com.openframe.management.config.PinotConfig": Node(
short_id="PinotConfig"
),
}
# LLM might include full Java package path
llm_id = "deps.openframe-oss-lib.src.main.java.com.openframe.management.config.PinotConfig"
# Strip deps prefix
stripped = llm_id[5:] if llm_id.startswith("deps.") else llm_id
assert stripped in components
if __name__ == "__main__":
pytest.main([__file__, "-v"])