Skip to content

Commit 4745b0c

Browse files
committed
reset test strings to strict
1 parent 112a8eb commit 4745b0c

3 files changed

Lines changed: 120 additions & 129 deletions

File tree

.github/workflows/js-tests.yml

Lines changed: 10 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: JavaScript/TypeScript Tests
1+
name: JavaScript/TypeScript Integration Tests
22

33
on:
44
push:
@@ -12,59 +12,8 @@ concurrency:
1212
cancel-in-progress: true
1313

1414
jobs:
15-
jest-tests:
16-
name: Jest Tests
17-
runs-on: ubuntu-latest
18-
strategy:
19-
matrix:
20-
project:
21-
- code_to_optimize_js
22-
- code_to_optimize_ts
23-
steps:
24-
- name: Checkout code
25-
uses: actions/checkout@v4
26-
with:
27-
fetch-depth: 0
28-
token: ${{ secrets.GITHUB_TOKEN }}
29-
30-
- name: Setup Node.js
31-
uses: actions/setup-node@v4
32-
with:
33-
node-version: '20'
34-
35-
- name: Install dependencies
36-
working-directory: code_to_optimize/js/${{ matrix.project }}
37-
run: npm install
38-
39-
- name: Run Jest tests
40-
working-directory: code_to_optimize/js/${{ matrix.project }}
41-
run: npm test
42-
43-
vitest-tests:
44-
name: Vitest Tests
45-
runs-on: ubuntu-latest
46-
steps:
47-
- name: Checkout code
48-
uses: actions/checkout@v4
49-
with:
50-
fetch-depth: 0
51-
token: ${{ secrets.GITHUB_TOKEN }}
52-
53-
- name: Setup Node.js
54-
uses: actions/setup-node@v4
55-
with:
56-
node-version: '20'
57-
58-
- name: Install dependencies
59-
working-directory: code_to_optimize/js/code_to_optimize_vitest
60-
run: npm install
61-
62-
- name: Run Vitest tests
63-
working-directory: code_to_optimize/js/code_to_optimize_vitest
64-
run: npm test
65-
66-
python-js-tests:
67-
name: Python JS/TS Integration Tests
15+
js-integration-tests:
16+
name: JS/TS Integration Tests
6817
runs-on: ubuntu-latest
6918
steps:
7019
- name: Checkout code
@@ -92,14 +41,10 @@ jobs:
9241
npm install --prefix code_to_optimize/js/code_to_optimize_ts
9342
npm install --prefix code_to_optimize/js/code_to_optimize_vitest
9443
95-
- name: Run Vitest runner unit tests
96-
run: uv run pytest tests/languages/javascript/test_vitest_runner.py -v
97-
98-
- name: Run Vitest E2E tests
99-
run: uv run pytest tests/test_languages/test_vitest_e2e.py -v
100-
101-
- name: Run JavaScript E2E tests
102-
run: uv run pytest tests/test_languages/test_javascript_e2e.py -v
103-
104-
- name: Run config_js tests
105-
run: uv run pytest tests/code_utils/test_config_js.py -v
44+
- name: Run JavaScript integration tests
45+
run: |
46+
uv run pytest tests/languages/javascript/ -v
47+
uv run pytest tests/test_languages/test_vitest_e2e.py -v
48+
uv run pytest tests/test_languages/test_javascript_e2e.py -v
49+
uv run pytest tests/test_languages/test_javascript_support.py -v
50+
uv run pytest tests/code_utils/test_config_js.py -v

tests/test_languages/test_javascript_e2e.py

Lines changed: 61 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,29 @@
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

1013
import tempfile
1114
from pathlib import Path
1215

1316
import pytest
14-
from codeflash.discovery.functions_to_optimize import find_all_functions_in_file, get_files_for_language
17+
1518
from 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+
1831
class 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

124148
class 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 = """
133158
function 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

161193
class 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

197227
class 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("""
204237
class 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

Comments
 (0)