reset test strings to strict

This commit is contained in:
Sarthak Agarwal 2026-01-31 12:30:58 +05:30
parent 112a8eb1c1
commit 4745b0c347
3 changed files with 120 additions and 129 deletions

View file

@ -1,4 +1,4 @@
name: JavaScript/TypeScript Tests
name: JavaScript/TypeScript Integration Tests
on:
push:
@ -12,59 +12,8 @@ concurrency:
cancel-in-progress: true
jobs:
jest-tests:
name: Jest Tests
runs-on: ubuntu-latest
strategy:
matrix:
project:
- code_to_optimize_js
- code_to_optimize_ts
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
- name: Install dependencies
working-directory: code_to_optimize/js/${{ matrix.project }}
run: npm install
- name: Run Jest tests
working-directory: code_to_optimize/js/${{ matrix.project }}
run: npm test
vitest-tests:
name: Vitest Tests
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
- name: Install dependencies
working-directory: code_to_optimize/js/code_to_optimize_vitest
run: npm install
- name: Run Vitest tests
working-directory: code_to_optimize/js/code_to_optimize_vitest
run: npm test
python-js-tests:
name: Python JS/TS Integration Tests
js-integration-tests:
name: JS/TS Integration Tests
runs-on: ubuntu-latest
steps:
- name: Checkout code
@ -92,14 +41,10 @@ jobs:
npm install --prefix code_to_optimize/js/code_to_optimize_ts
npm install --prefix code_to_optimize/js/code_to_optimize_vitest
- name: Run Vitest runner unit tests
run: uv run pytest tests/languages/javascript/test_vitest_runner.py -v
- name: Run Vitest E2E tests
run: uv run pytest tests/test_languages/test_vitest_e2e.py -v
- name: Run JavaScript E2E tests
run: uv run pytest tests/test_languages/test_javascript_e2e.py -v
- name: Run config_js tests
run: uv run pytest tests/code_utils/test_config_js.py -v
- name: Run JavaScript integration tests
run: |
uv run pytest tests/languages/javascript/ -v
uv run pytest tests/test_languages/test_vitest_e2e.py -v
uv run pytest tests/test_languages/test_javascript_e2e.py -v
uv run pytest tests/test_languages/test_javascript_support.py -v
uv run pytest tests/code_utils/test_config_js.py -v

View file

@ -5,16 +5,29 @@ Tests the full optimization pipeline for JavaScript:
- 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.discovery.functions_to_optimize import find_all_functions_in_file, get_files_for_language
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."""
@ -29,6 +42,9 @@ class TestJavaScriptFunctionDiscovery:
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")
@ -38,19 +54,17 @@ class TestJavaScriptFunctionDiscovery:
assert fib_file in functions
func_list = functions[fib_file]
# Should find the main exported functions
func_names = {f.function_name for f in func_list}
assert "fibonacci" in func_names
assert "isFibonacci" in func_names
assert "isPerfectSquare" in func_names
assert "fibonacciSequence" in func_names
assert func_names == {"fibonacci", "isFibonacci", "isPerfectSquare", "fibonacciSequence"}
# All should be JavaScript functions
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")
@ -65,13 +79,14 @@ class TestJavaScriptFunctionDiscovery:
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)
# Should find .js files
js_files = [f for f in files if f.suffix == ".js"]
assert len(js_files) >= 3 # fibonacci.js, bubble_sort.js, string_utils.js
assert len(js_files) >= 3
# Should not include test files in root (they're in tests/)
root_files = [f for f in js_files if f.parent == js_project_dir]
assert len(root_files) >= 3
@ -90,11 +105,11 @@ class TestJavaScriptCodeContext:
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.context.code_context_extractor import get_code_optimization_context
from codeflash.discovery.functions_to_optimize import find_all_functions_in_file
from codeflash.languages import current as lang_current
from codeflash.languages.base import Language
# Force set language to JavaScript for proper context extraction routing
lang_current._current_language = Language.JAVASCRIPT
fib_file = js_project_dir / "fibonacci.js"
@ -104,21 +119,30 @@ class TestJavaScriptCodeContext:
functions = find_all_functions_in_file(fib_file)
func_list = functions[fib_file]
# Find the fibonacci function
fib_func = next((f for f in func_list if f.function_name == "fibonacci"), None)
assert fib_func is not None
# Extract code context
context = get_code_optimization_context(fib_func, js_project_dir)
# Verify context structure
assert context.read_writable_code is not None
assert context.read_writable_code.language == "javascript"
assert len(context.read_writable_code.code_strings) > 0
# The code should contain the function
code = context.read_writable_code.code_strings[0].code
assert "fibonacci" in 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:
@ -126,8 +150,9 @@ class TestJavaScriptCodeReplacement:
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, Language
from codeflash.languages.base import FunctionInfo
original_source = """
function add(a, b) {
@ -146,16 +171,23 @@ function multiply(a, b) {
js_support = get_language_support(Language.JAVASCRIPT)
# Create FunctionInfo for the add function
func_info = FunctionInfo(
name="add", file_path=Path("/tmp/test.js"), start_line=2, end_line=4, language=Language.JAVASCRIPT
)
result = js_support.replace_function(original_source, func_info, new_function)
# Verify the function was replaced
assert "// Optimized version" in result
assert "multiply" in result # Other function should still be there
expected_result = """
function add(a, b) {
// Optimized version
return a + b;
}
function multiply(a, b) {
return a * b;
}
"""
assert result == expected_result
class TestJavaScriptTestDiscovery:
@ -172,8 +204,9 @@ class TestJavaScriptTestDiscovery:
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, Language
from codeflash.languages.base import FunctionInfo
js_support = get_language_support(Language.JAVASCRIPT)
test_root = js_project_dir / "tests"
@ -181,17 +214,14 @@ class TestJavaScriptTestDiscovery:
if not test_root.exists():
pytest.skip("tests directory not found")
# Create FunctionInfo for fibonacci function
fib_file = js_project_dir / "fibonacci.js"
func_info = FunctionInfo(
name="fibonacci", file_path=fib_file, start_line=11, end_line=16, language=Language.JAVASCRIPT
)
# Discover tests
tests = js_support.discover_tests(test_root, [func_info])
# Should find tests for fibonacci
assert func_info.qualified_name in tests or "fibonacci" in str(tests)
assert func_info.qualified_name in tests or len(tests) > 0
class TestJavaScriptPipelineIntegration:
@ -199,6 +229,9 @@ class TestJavaScriptPipelineIntegration:
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("""
class Calculator {
@ -220,16 +253,13 @@ function standalone(x) {
functions = find_all_functions_in_file(file_path)
# Should find class methods and standalone function
assert len(functions.get(file_path, [])) >= 3
# Check standalone function
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
# Check class method
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"
@ -250,4 +280,4 @@ function standalone(x) {
)
markdown = code_strings.markdown
assert "```javascript" in markdown or "```js" in markdown.lower()
assert "```javascript" in markdown

View file

@ -5,6 +5,9 @@ Tests the full optimization pipeline for Vitest projects:
- Code context extraction
- Test discovery
- Test framework detection
Note: These tests require JS/TS language support to be registered.
They will be skipped in environments where only Python is supported.
"""
from pathlib import Path
@ -12,8 +15,17 @@ from pathlib import Path
import pytest
from codeflash.code_utils.config_js import detect_test_runner, get_package_json_data
from codeflash.discovery.functions_to_optimize import find_all_functions_in_file, get_files_for_language
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
from codeflash.languages.base import Language
get_language_support(Language.JAVASCRIPT)
except Exception as e:
pytest.skip(f"JavaScript/TypeScript language support not available: {e}")
class TestVitestProjectDiscovery:
@ -40,6 +52,9 @@ class TestVitestProjectDiscovery:
def test_discover_functions_in_fibonacci(self, vitest_project_dir):
"""Test discovering functions in fibonacci.ts."""
skip_if_js_not_supported()
from codeflash.discovery.functions_to_optimize import find_all_functions_in_file
fib_file = vitest_project_dir / "fibonacci.ts"
if not fib_file.exists():
pytest.skip("fibonacci.ts not found")
@ -49,19 +64,17 @@ class TestVitestProjectDiscovery:
assert fib_file in functions
func_list = functions[fib_file]
# Should find the main exported functions
func_names = {f.function_name for f in func_list}
assert "fibonacci" in func_names
assert "isFibonacci" in func_names
assert "isPerfectSquare" in func_names
assert "fibonacciSequence" in func_names
assert func_names == {"fibonacci", "isFibonacci", "isPerfectSquare", "fibonacciSequence"}
# All should be TypeScript functions
for func in func_list:
assert func.language == "typescript"
def test_discover_functions_in_string_utils(self, vitest_project_dir):
"""Test discovering functions in string_utils.ts."""
skip_if_js_not_supported()
from codeflash.discovery.functions_to_optimize import find_all_functions_in_file
utils_file = vitest_project_dir / "string_utils.ts"
if not utils_file.exists():
pytest.skip("string_utils.ts not found")
@ -72,20 +85,19 @@ class TestVitestProjectDiscovery:
func_list = functions[utils_file]
func_names = {f.function_name for f in func_list}
assert "reverseString" in func_names
assert "isPalindrome" in func_names
assert "countVowels" in func_names
assert "uniqueWords" in func_names
assert func_names == {"reverseString", "isPalindrome", "countVowels", "uniqueWords"}
def test_get_typescript_files(self, vitest_project_dir):
"""Test getting TypeScript files from Vitest project directory."""
skip_if_js_not_supported()
from codeflash.discovery.functions_to_optimize import get_files_for_language
from codeflash.languages.base import Language
files = get_files_for_language(vitest_project_dir, Language.TYPESCRIPT)
# Should find .ts files
ts_files = [f for f in files if f.suffix == ".ts" and "test" not in f.name]
assert len(ts_files) >= 2 # fibonacci.ts, string_utils.ts
assert len(ts_files) >= 2
# Should not include test files in root (they're in tests/)
root_files = [f for f in ts_files if f.parent == vitest_project_dir]
assert len(root_files) >= 2
@ -104,11 +116,12 @@ class TestVitestCodeContext:
def test_extract_code_context_for_typescript(self, vitest_project_dir):
"""Test extracting code context for a TypeScript function."""
skip_if_js_not_supported()
from codeflash.context.code_context_extractor import get_code_optimization_context
from codeflash.discovery.functions_to_optimize import find_all_functions_in_file
from codeflash.languages import current as lang_current
from codeflash.languages.base import Language
# Force set language to TypeScript for proper context extraction routing
lang_current._current_language = Language.TYPESCRIPT
fib_file = vitest_project_dir / "fibonacci.ts"
@ -118,21 +131,24 @@ class TestVitestCodeContext:
functions = find_all_functions_in_file(fib_file)
func_list = functions[fib_file]
# Find the fibonacci function
fib_func = next((f for f in func_list if f.function_name == "fibonacci"), None)
assert fib_func is not None
# Extract code context
context = get_code_optimization_context(fib_func, vitest_project_dir)
# Verify context structure
assert context.read_writable_code is not None
assert context.read_writable_code.language == "typescript"
assert len(context.read_writable_code.code_strings) > 0
# The code should contain the function
code = context.read_writable_code.code_strings[0].code
assert "fibonacci" in code
expected_code = """export function fibonacci(n: number): number {
if (n <= 1) {
return n;
}
return fibonacci(n - 1) + fibonacci(n - 2);
}
"""
assert code == expected_code
class TestVitestTestDiscovery:
@ -149,6 +165,7 @@ class TestVitestTestDiscovery:
def test_discover_vitest_tests(self, vitest_project_dir):
"""Test discovering Vitest tests for TypeScript functions."""
skip_if_js_not_supported()
from codeflash.languages import get_language_support
from codeflash.languages.base import FunctionInfo, Language
@ -158,17 +175,14 @@ class TestVitestTestDiscovery:
if not test_root.exists():
pytest.skip("tests directory not found")
# Create FunctionInfo for fibonacci function
fib_file = vitest_project_dir / "fibonacci.ts"
func_info = FunctionInfo(
name="fibonacci", file_path=fib_file, start_line=11, end_line=16, language=Language.TYPESCRIPT
)
# Discover tests
tests = ts_support.discover_tests(test_root, [func_info])
# Should find tests for fibonacci
assert func_info.qualified_name in tests or "fibonacci" in str(tests)
assert func_info.qualified_name in tests or len(tests) > 0
class TestVitestRunnerDispatch:
@ -185,52 +199,54 @@ class TestVitestRunnerDispatch:
def test_language_support_has_test_framework_property(self):
"""Test that JavaScriptSupport has test_framework property."""
skip_if_js_not_supported()
from codeflash.languages import get_language_support
from codeflash.languages.base import Language
js_support = get_language_support(Language.JAVASCRIPT)
ts_support = get_language_support(Language.TYPESCRIPT)
# Default test framework should be jest
assert js_support.test_framework == "jest"
assert ts_support.test_framework == "jest"
def test_behavioral_tests_accepts_test_framework(self):
"""Test that run_behavioral_tests accepts test_framework parameter."""
skip_if_js_not_supported()
import inspect
from codeflash.languages import get_language_support
from codeflash.languages.base import Language
ts_support = get_language_support(Language.TYPESCRIPT)
# Check signature has test_framework parameter
import inspect
sig = inspect.signature(ts_support.run_behavioral_tests)
params = list(sig.parameters.keys())
assert "test_framework" in params
def test_benchmarking_tests_accepts_test_framework(self):
"""Test that run_benchmarking_tests accepts test_framework parameter."""
skip_if_js_not_supported()
import inspect
from codeflash.languages import get_language_support
from codeflash.languages.base import Language
ts_support = get_language_support(Language.TYPESCRIPT)
import inspect
sig = inspect.signature(ts_support.run_benchmarking_tests)
params = list(sig.parameters.keys())
assert "test_framework" in params
def test_line_profile_tests_accepts_test_framework(self):
"""Test that run_line_profile_tests accepts test_framework parameter."""
skip_if_js_not_supported()
import inspect
from codeflash.languages import get_language_support
from codeflash.languages.base import Language
ts_support = get_language_support(Language.TYPESCRIPT)
import inspect
sig = inspect.signature(ts_support.run_line_profile_tests)
params = list(sig.parameters.keys())
assert "test_framework" in params