55- Code context extraction
66- Test discovery
77- Code replacement
8+
9+ Note: These tests require JS/TS language support to be registered.
10+ They will be skipped in environments where only Python is supported.
811"""
912
1013import tempfile
1114from pathlib import Path
1215
1316import pytest
14- from codeflash . discovery . functions_to_optimize import find_all_functions_in_file , get_files_for_language
17+
1518from codeflash .languages .base import Language
1619
1720
21+ def skip_if_js_not_supported ():
22+ """Skip test if JavaScript/TypeScript languages are not supported."""
23+ try :
24+ from codeflash .languages import get_language_support
25+
26+ get_language_support (Language .JAVASCRIPT )
27+ except Exception as e :
28+ pytest .skip (f"JavaScript/TypeScript language support not available: { e } " )
29+
30+
1831class TestJavaScriptFunctionDiscovery :
1932 """Tests for JavaScript function discovery in the main pipeline."""
2033
@@ -29,6 +42,9 @@ def js_project_dir(self):
2942
3043 def test_discover_functions_in_fibonacci (self , js_project_dir ):
3144 """Test discovering functions in fibonacci.js."""
45+ skip_if_js_not_supported ()
46+ from codeflash .discovery .functions_to_optimize import find_all_functions_in_file
47+
3248 fib_file = js_project_dir / "fibonacci.js"
3349 if not fib_file .exists ():
3450 pytest .skip ("fibonacci.js not found" )
@@ -38,19 +54,17 @@ def test_discover_functions_in_fibonacci(self, js_project_dir):
3854 assert fib_file in functions
3955 func_list = functions [fib_file ]
4056
41- # Should find the main exported functions
4257 func_names = {f .function_name for f in func_list }
43- assert "fibonacci" in func_names
44- assert "isFibonacci" in func_names
45- assert "isPerfectSquare" in func_names
46- assert "fibonacciSequence" in func_names
58+ assert func_names == {"fibonacci" , "isFibonacci" , "isPerfectSquare" , "fibonacciSequence" }
4759
48- # All should be JavaScript functions
4960 for func in func_list :
5061 assert func .language == "javascript"
5162
5263 def test_discover_functions_in_bubble_sort (self , js_project_dir ):
5364 """Test discovering functions in bubble_sort.js."""
65+ skip_if_js_not_supported ()
66+ from codeflash .discovery .functions_to_optimize import find_all_functions_in_file
67+
5468 sort_file = js_project_dir / "bubble_sort.js"
5569 if not sort_file .exists ():
5670 pytest .skip ("bubble_sort.js not found" )
@@ -65,13 +79,14 @@ def test_discover_functions_in_bubble_sort(self, js_project_dir):
6579
6680 def test_get_javascript_files (self , js_project_dir ):
6781 """Test getting JavaScript files from directory."""
82+ skip_if_js_not_supported ()
83+ from codeflash .discovery .functions_to_optimize import get_files_for_language
84+
6885 files = get_files_for_language (js_project_dir , Language .JAVASCRIPT )
6986
70- # Should find .js files
7187 js_files = [f for f in files if f .suffix == ".js" ]
72- assert len (js_files ) >= 3 # fibonacci.js, bubble_sort.js, string_utils.js
88+ assert len (js_files ) >= 3
7389
74- # Should not include test files in root (they're in tests/)
7590 root_files = [f for f in js_files if f .parent == js_project_dir ]
7691 assert len (root_files ) >= 3
7792
@@ -90,11 +105,11 @@ def js_project_dir(self):
90105
91106 def test_extract_code_context_for_javascript (self , js_project_dir ):
92107 """Test extracting code context for a JavaScript function."""
108+ skip_if_js_not_supported ()
93109 from codeflash .context .code_context_extractor import get_code_optimization_context
110+ from codeflash .discovery .functions_to_optimize import find_all_functions_in_file
94111 from codeflash .languages import current as lang_current
95- from codeflash .languages .base import Language
96112
97- # Force set language to JavaScript for proper context extraction routing
98113 lang_current ._current_language = Language .JAVASCRIPT
99114
100115 fib_file = js_project_dir / "fibonacci.js"
@@ -104,30 +119,40 @@ def test_extract_code_context_for_javascript(self, js_project_dir):
104119 functions = find_all_functions_in_file (fib_file )
105120 func_list = functions [fib_file ]
106121
107- # Find the fibonacci function
108122 fib_func = next ((f for f in func_list if f .function_name == "fibonacci" ), None )
109123 assert fib_func is not None
110124
111- # Extract code context
112125 context = get_code_optimization_context (fib_func , js_project_dir )
113126
114- # Verify context structure
115127 assert context .read_writable_code is not None
116128 assert context .read_writable_code .language == "javascript"
117129 assert len (context .read_writable_code .code_strings ) > 0
118130
119- # The code should contain the function
120131 code = context .read_writable_code .code_strings [0 ].code
121- assert "fibonacci" in code
132+ expected_code = """/**
133+ * Calculate the nth Fibonacci number using naive recursion.
134+ * This is intentionally slow to demonstrate optimization potential.
135+ * @param {number} n - The index of the Fibonacci number to calculate
136+ * @returns {number} - The nth Fibonacci number
137+ */
138+ function fibonacci(n) {
139+ if (n <= 1) {
140+ return n;
141+ }
142+ return fibonacci(n - 1) + fibonacci(n - 2);
143+ }
144+ """
145+ assert code == expected_code
122146
123147
124148class TestJavaScriptCodeReplacement :
125149 """Tests for JavaScript code replacement."""
126150
127151 def test_replace_function_in_javascript_file (self ):
128152 """Test replacing a function in a JavaScript file."""
153+ skip_if_js_not_supported ()
129154 from codeflash .languages import get_language_support
130- from codeflash .languages .base import FunctionInfo , Language
155+ from codeflash .languages .base import FunctionInfo
131156
132157 original_source = """
133158function add(a, b) {
@@ -146,16 +171,23 @@ def test_replace_function_in_javascript_file(self):
146171
147172 js_support = get_language_support (Language .JAVASCRIPT )
148173
149- # Create FunctionInfo for the add function
150174 func_info = FunctionInfo (
151175 name = "add" , file_path = Path ("/tmp/test.js" ), start_line = 2 , end_line = 4 , language = Language .JAVASCRIPT
152176 )
153177
154178 result = js_support .replace_function (original_source , func_info , new_function )
155179
156- # Verify the function was replaced
157- assert "// Optimized version" in result
158- assert "multiply" in result # Other function should still be there
180+ expected_result = """
181+ function add(a, b) {
182+ // Optimized version
183+ return a + b;
184+ }
185+
186+ function multiply(a, b) {
187+ return a * b;
188+ }
189+ """
190+ assert result == expected_result
159191
160192
161193class TestJavaScriptTestDiscovery :
@@ -172,33 +204,34 @@ def js_project_dir(self):
172204
173205 def test_discover_jest_tests (self , js_project_dir ):
174206 """Test discovering Jest tests for JavaScript functions."""
207+ skip_if_js_not_supported ()
175208 from codeflash .languages import get_language_support
176- from codeflash .languages .base import FunctionInfo , Language
209+ from codeflash .languages .base import FunctionInfo
177210
178211 js_support = get_language_support (Language .JAVASCRIPT )
179212 test_root = js_project_dir / "tests"
180213
181214 if not test_root .exists ():
182215 pytest .skip ("tests directory not found" )
183216
184- # Create FunctionInfo for fibonacci function
185217 fib_file = js_project_dir / "fibonacci.js"
186218 func_info = FunctionInfo (
187219 name = "fibonacci" , file_path = fib_file , start_line = 11 , end_line = 16 , language = Language .JAVASCRIPT
188220 )
189221
190- # Discover tests
191222 tests = js_support .discover_tests (test_root , [func_info ])
192223
193- # Should find tests for fibonacci
194- assert func_info .qualified_name in tests or "fibonacci" in str (tests )
224+ assert func_info .qualified_name in tests or len (tests ) > 0
195225
196226
197227class TestJavaScriptPipelineIntegration :
198228 """Integration tests for the full JavaScript pipeline."""
199229
200230 def test_function_to_optimize_has_correct_fields (self ):
201231 """Test that FunctionToOptimize from JavaScript has all required fields."""
232+ skip_if_js_not_supported ()
233+ from codeflash .discovery .functions_to_optimize import find_all_functions_in_file
234+
202235 with tempfile .NamedTemporaryFile (suffix = ".js" , mode = "w" , delete = False ) as f :
203236 f .write ("""
204237class Calculator {
@@ -220,16 +253,13 @@ class Calculator {
220253
221254 functions = find_all_functions_in_file (file_path )
222255
223- # Should find class methods and standalone function
224256 assert len (functions .get (file_path , [])) >= 3
225257
226- # Check standalone function
227258 standalone_fn = next ((fn for fn in functions [file_path ] if fn .function_name == "standalone" ), None )
228259 assert standalone_fn is not None
229260 assert standalone_fn .language == "javascript"
230261 assert len (standalone_fn .parents ) == 0
231262
232- # Check class method
233263 add_fn = next ((fn for fn in functions [file_path ] if fn .function_name == "add" ), None )
234264 assert add_fn is not None
235265 assert add_fn .language == "javascript"
@@ -250,4 +280,4 @@ def test_code_strings_markdown_uses_javascript_tag(self):
250280 )
251281
252282 markdown = code_strings .markdown
253- assert "```javascript" in markdown or "```js" in markdown . lower ()
283+ assert "```javascript" in markdown
0 commit comments