Skip to content

Commit 69fb9cc

Browse files
committed
Remove instrumentation of js tests from aiservice and into client
1 parent 28f8eb1 commit 69fb9cc

2 files changed

Lines changed: 69 additions & 19 deletions

File tree

codeflash/languages/javascript/instrument.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,3 +408,40 @@ def get_instrumented_test_path(original_path: Path, mode: str) -> Path:
408408
new_stem = f"{stem}{suffix}"
409409

410410
return original_path.parent / f"{new_stem}{original_path.suffix}"
411+
412+
413+
def instrument_generated_js_test(
414+
test_code: str,
415+
function_name: str,
416+
qualified_name: str,
417+
mode: str = TestingMode.BEHAVIOR,
418+
) -> str:
419+
"""Instrument generated JavaScript/TypeScript test code.
420+
421+
This function is used to instrument tests generated by the aiservice.
422+
Unlike inject_profiling_into_existing_js_test, this takes the test code
423+
as a string rather than reading from a file.
424+
425+
Args:
426+
test_code: The generated test code to instrument.
427+
function_name: Name of the function being tested.
428+
qualified_name: Fully qualified function name (e.g., 'module.funcName').
429+
mode: Testing mode - "behavior" or "performance".
430+
431+
Returns:
432+
Instrumented test code.
433+
434+
"""
435+
if not test_code or not test_code.strip():
436+
return test_code
437+
438+
# Use the internal instrumentation function
439+
instrumented_code = _instrument_js_test_code(
440+
code=test_code,
441+
func_name=function_name,
442+
test_file_path="generated_test",
443+
mode=mode,
444+
qualified_name=qualified_name,
445+
)
446+
447+
return instrumented_code

codeflash/verification/verifier.py

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -62,36 +62,49 @@ def generate_tests(
6262
generated_test_source, instrumented_behavior_test_source, instrumented_perf_test_source = response
6363
temp_run_dir = get_run_tmp_file(Path()).as_posix()
6464

65-
instrumented_behavior_test_source = instrumented_behavior_test_source.replace(
66-
"{codeflash_run_tmp_dir_client_side}", temp_run_dir
67-
)
68-
instrumented_perf_test_source = instrumented_perf_test_source.replace(
69-
"{codeflash_run_tmp_dir_client_side}", temp_run_dir
70-
)
71-
72-
# For JavaScript/TypeScript, validate and fix import styles to match source exports
65+
# For JavaScript/TypeScript, instrumentation is done locally (aiservice returns uninstrumented code)
7366
if is_javascript():
74-
from codeflash.languages.javascript.instrument import validate_and_fix_import_style
67+
from codeflash.languages.javascript.instrument import (
68+
TestingMode,
69+
instrument_generated_js_test,
70+
validate_and_fix_import_style,
71+
)
7572
from codeflash.languages.javascript.module_system import ensure_module_system_compatibility
7673

7774
source_file = Path(function_to_optimize.file_path)
7875
func_name = function_to_optimize.function_name
76+
qualified_name = function_to_optimize.qualified_name
7977

78+
# First validate and fix import styles
8079
generated_test_source = validate_and_fix_import_style(generated_test_source, source_file, func_name)
81-
instrumented_behavior_test_source = validate_and_fix_import_style(
82-
instrumented_behavior_test_source, source_file, func_name
83-
)
84-
instrumented_perf_test_source = validate_and_fix_import_style(
85-
instrumented_perf_test_source, source_file, func_name
86-
)
8780

8881
# Convert module system if needed (e.g., CommonJS -> ESM for ESM projects)
8982
generated_test_source = ensure_module_system_compatibility(generated_test_source, project_module_system)
90-
instrumented_behavior_test_source = ensure_module_system_compatibility(
91-
instrumented_behavior_test_source, project_module_system
83+
84+
# Instrument for behavior verification (writes to SQLite)
85+
instrumented_behavior_test_source = instrument_generated_js_test(
86+
test_code=generated_test_source,
87+
function_name=func_name,
88+
qualified_name=qualified_name,
89+
mode=TestingMode.BEHAVIOR,
90+
)
91+
92+
# Instrument for performance measurement (prints to stdout)
93+
instrumented_perf_test_source = instrument_generated_js_test(
94+
test_code=generated_test_source,
95+
function_name=func_name,
96+
qualified_name=qualified_name,
97+
mode=TestingMode.PERFORMANCE,
98+
)
99+
100+
logger.debug(f"Instrumented JS/TS tests locally for {func_name}")
101+
else:
102+
# Python: instrumentation is done by aiservice, just replace temp dir placeholders
103+
instrumented_behavior_test_source = instrumented_behavior_test_source.replace(
104+
"{codeflash_run_tmp_dir_client_side}", temp_run_dir
92105
)
93-
instrumented_perf_test_source = ensure_module_system_compatibility(
94-
instrumented_perf_test_source, project_module_system
106+
instrumented_perf_test_source = instrumented_perf_test_source.replace(
107+
"{codeflash_run_tmp_dir_client_side}", temp_run_dir
95108
)
96109
else:
97110
logger.warning(f"Failed to generate and instrument tests for {function_to_optimize.function_name}")

0 commit comments

Comments
 (0)