-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathtest_javascript_e2e.py
More file actions
285 lines (216 loc) · 9.59 KB
/
test_javascript_e2e.py
File metadata and controls
285 lines (216 loc) · 9.59 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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
"""End-to-end integration tests for JavaScript pipeline.
Tests the full optimization pipeline for JavaScript:
- Function discovery
- Code context extraction
- Test discovery
- Code replacement
Note: These tests require JS/TS language support to be registered.
They will be skipped in environments where only Python is supported.
"""
import tempfile
from pathlib import Path
import pytest
from codeflash.languages.base import Language
def skip_if_js_not_supported():
"""Skip test if JavaScript/TypeScript languages are not supported."""
try:
from codeflash.languages import get_language_support
get_language_support(Language.JAVASCRIPT)
except Exception as e:
pytest.skip(f"JavaScript/TypeScript language support not available: {e}")
class TestJavaScriptFunctionDiscovery:
"""Tests for JavaScript function discovery in the main pipeline."""
@pytest.fixture
def js_project_dir(self):
"""Get the JavaScript sample project directory."""
project_root = Path(__file__).parent.parent.parent
js_dir = project_root / "code_to_optimize" / "js" / "code_to_optimize_js"
if not js_dir.exists():
pytest.skip("code_to_optimize_js directory not found")
return js_dir
def test_discover_functions_in_fibonacci(self, js_project_dir):
"""Test discovering functions in fibonacci.js."""
skip_if_js_not_supported()
from codeflash.discovery.functions_to_optimize import find_all_functions_in_file
fib_file = js_project_dir / "fibonacci.js"
if not fib_file.exists():
pytest.skip("fibonacci.js not found")
functions = find_all_functions_in_file(fib_file)
assert fib_file in functions
func_list = functions[fib_file]
func_names = {f.function_name for f in func_list}
assert func_names == {"fibonacci", "isFibonacci", "isPerfectSquare", "fibonacciSequence"}
for func in func_list:
assert func.language == "javascript"
def test_discover_functions_in_bubble_sort(self, js_project_dir):
"""Test discovering functions in bubble_sort.js."""
skip_if_js_not_supported()
from codeflash.discovery.functions_to_optimize import find_all_functions_in_file
sort_file = js_project_dir / "bubble_sort.js"
if not sort_file.exists():
pytest.skip("bubble_sort.js not found")
functions = find_all_functions_in_file(sort_file)
assert sort_file in functions
func_list = functions[sort_file]
func_names = {f.function_name for f in func_list}
assert "bubbleSort" in func_names
def test_get_javascript_files(self, js_project_dir):
"""Test getting JavaScript files from directory."""
skip_if_js_not_supported()
from codeflash.discovery.functions_to_optimize import get_files_for_language
files = get_files_for_language(js_project_dir, Language.JAVASCRIPT)
js_files = [f for f in files if f.suffix == ".js"]
assert len(js_files) >= 3
root_files = [f for f in js_files if f.parent == js_project_dir]
assert len(root_files) >= 3
class TestJavaScriptCodeContext:
"""Tests for JavaScript code context extraction."""
@pytest.fixture
def js_project_dir(self):
"""Get the JavaScript sample project directory."""
project_root = Path(__file__).parent.parent.parent
js_dir = project_root / "code_to_optimize" / "js" / "code_to_optimize_js"
if not js_dir.exists():
pytest.skip("code_to_optimize_js directory not found")
return js_dir
def test_extract_code_context_for_javascript(self, js_project_dir):
"""Test extracting code context for a JavaScript function."""
skip_if_js_not_supported()
from codeflash.discovery.functions_to_optimize import find_all_functions_in_file
from codeflash.languages import get_language_support
from codeflash.languages.javascript.function_optimizer import JavaScriptFunctionOptimizer
fib_file = js_project_dir / "fibonacci.js"
if not fib_file.exists():
pytest.skip("fibonacci.js not found")
functions = find_all_functions_in_file(fib_file)
func_list = functions[fib_file]
fib_func = next((f for f in func_list if f.function_name == "fibonacci"), None)
assert fib_func is not None
js_support = get_language_support(Language.JAVASCRIPT)
code_context = js_support.extract_code_context(fib_func, js_project_dir, js_project_dir)
context = JavaScriptFunctionOptimizer._build_optimization_context(
code_context, fib_file, "javascript", js_project_dir
)
assert context.read_writable_code is not None
assert context.read_writable_code.language == "javascript"
assert len(context.read_writable_code.code_strings) > 0
code = context.read_writable_code.code_strings[0].code
expected_code = """/**
* Calculate the nth Fibonacci number using naive recursion.
* This is intentionally slow to demonstrate optimization potential.
* @param {number} n - The index of the Fibonacci number to calculate
* @returns {number} - The nth Fibonacci number
*/
function fibonacci(n) {
if (n <= 1) {
return n;
}
return fibonacci(n - 1) + fibonacci(n - 2);
}
"""
assert code == expected_code
class TestJavaScriptCodeReplacement:
"""Tests for JavaScript code replacement."""
def test_replace_function_in_javascript_file(self):
"""Test replacing a function in a JavaScript file."""
skip_if_js_not_supported()
from codeflash.languages import get_language_support
from codeflash.languages.base import FunctionInfo
original_source = """
export function add(a, b) {
return a + b;
}
export function multiply(a, b) {
return a * b;
}
"""
new_function = """export function add(a, b) {
// Optimized version
return a + b;
}"""
js_support = get_language_support(Language.JAVASCRIPT)
func_info = FunctionInfo(
function_name="add", file_path=Path("/tmp/test.js"), starting_line=2, ending_line=4, language="javascript"
)
result = js_support.replace_function(original_source, func_info, new_function)
expected_result = """
export function add(a, b) {
// Optimized version
return a + b;
}
export function multiply(a, b) {
return a * b;
}
"""
assert result == expected_result
class TestJavaScriptTestDiscovery:
"""Tests for JavaScript test discovery."""
@pytest.fixture
def js_project_dir(self):
"""Get the JavaScript sample project directory."""
project_root = Path(__file__).parent.parent.parent
js_dir = project_root / "code_to_optimize" / "js" / "code_to_optimize_js"
if not js_dir.exists():
pytest.skip("code_to_optimize_js directory not found")
return js_dir
def test_discover_jest_tests(self, js_project_dir):
"""Test discovering Jest tests for JavaScript functions."""
skip_if_js_not_supported()
from codeflash.languages import get_language_support
from codeflash.languages.base import FunctionInfo
js_support = get_language_support(Language.JAVASCRIPT)
test_root = js_project_dir / "tests"
if not test_root.exists():
pytest.skip("tests directory not found")
fib_file = js_project_dir / "fibonacci.js"
func_info = FunctionInfo(
function_name="fibonacci", file_path=fib_file, starting_line=11, ending_line=16, language="javascript"
)
tests = js_support.discover_tests(test_root, [func_info])
assert func_info.qualified_name in tests or len(tests) > 0
class TestJavaScriptPipelineIntegration:
"""Integration tests for the full JavaScript pipeline."""
def test_function_to_optimize_has_correct_fields(self):
"""Test that FunctionToOptimize from JavaScript has all required fields."""
skip_if_js_not_supported()
from codeflash.discovery.functions_to_optimize import find_all_functions_in_file
with tempfile.NamedTemporaryFile(suffix=".js", mode="w", delete=False) as f:
f.write("""
export class Calculator {
add(a, b) {
return a + b;
}
subtract(a, b) {
return a - b;
}
}
export function standalone(x) {
return x * 2;
}
""")
f.flush()
file_path = Path(f.name)
functions = find_all_functions_in_file(file_path)
assert len(functions.get(file_path, [])) >= 3
standalone_fn = next((fn for fn in functions[file_path] if fn.function_name == "standalone"), None)
assert standalone_fn is not None
assert standalone_fn.language == "javascript"
assert len(standalone_fn.parents) == 0
add_fn = next((fn for fn in functions[file_path] if fn.function_name == "add"), None)
assert add_fn is not None
assert add_fn.language == "javascript"
assert len(add_fn.parents) == 1
assert add_fn.parents[0].name == "Calculator"
def test_code_strings_markdown_uses_javascript_tag(self):
"""Test that CodeStringsMarkdown uses javascript for code blocks."""
from codeflash.models.models import CodeString, CodeStringsMarkdown
code_strings = CodeStringsMarkdown(
code_strings=[
CodeString(
code="function add(a, b) { return a + b; }", file_path=Path("test.js"), language="javascript"
)
],
language="javascript",
)
markdown = code_strings.markdown
assert "```javascript" in markdown