@@ -978,6 +978,62 @@ def find_test_root(self, project_root: Path) -> Path | None:
978978
979979 return None
980980
981+ def get_module_path (
982+ self , source_file : Path , project_root : Path , tests_root : Path | None = None
983+ ) -> str :
984+ """Get the module path for importing a JavaScript source file from tests.
985+
986+ For JavaScript, this returns a relative path from the tests directory to the source file
987+ (e.g., '../fibonacci' for source at /project/fibonacci.js and tests at /project/tests/).
988+
989+ Args:
990+ source_file: Path to the source file.
991+ project_root: Root of the project.
992+ tests_root: Root directory for tests (required for JS relative path calculation).
993+
994+ Returns:
995+ Relative path string for importing the module from tests.
996+
997+ """
998+ import os
999+
1000+ from codeflash .cli_cmds .console import logger
1001+
1002+ if tests_root is None :
1003+ tests_root = self .find_test_root (project_root ) or project_root
1004+
1005+ try :
1006+ # Resolve both paths to absolute to ensure consistent relative path calculation
1007+ source_file_abs = source_file .resolve ().with_suffix ("" )
1008+ tests_root_abs = tests_root .resolve ()
1009+
1010+ # Find the project root using language support
1011+ project_root_from_lang = self .find_test_root (project_root )
1012+
1013+ # Validate that tests_root is within the same project as the source file
1014+ if project_root_from_lang :
1015+ try :
1016+ tests_root_abs .relative_to (project_root_from_lang )
1017+ except ValueError :
1018+ # tests_root is outside the project - use default
1019+ logger .warning (
1020+ f"Configured tests_root { tests_root_abs } is outside project { project_root_from_lang } . "
1021+ f"Using default: { project_root_from_lang / 'tests' } "
1022+ )
1023+ tests_root_abs = project_root_from_lang / "tests"
1024+ if not tests_root_abs .exists ():
1025+ tests_root_abs = project_root_from_lang
1026+
1027+ # Use os.path.relpath to compute relative path from tests_root to source file
1028+ rel_path = os .path .relpath (str (source_file_abs ), str (tests_root_abs ))
1029+ logger .debug (
1030+ f"!lsp|Module path: source={ source_file_abs } , tests_root={ tests_root_abs } , rel_path={ rel_path } "
1031+ )
1032+ return rel_path
1033+ except ValueError :
1034+ # Fallback if paths are on different drives (Windows)
1035+ rel_path = source_file .relative_to (project_root )
1036+ return "../" + rel_path .with_suffix ("" ).as_posix ()
9811037
9821038 def ensure_runtime_environment (self , project_root : Path ) -> bool :
9831039 """Ensure codeflash npm package is installed.
0 commit comments