|
| 1 | +"""Test that js_project_root is recalculated per function, not cached.""" |
| 2 | + |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +from codeflash.languages.javascript.test_runner import find_node_project_root |
| 6 | + |
| 7 | + |
| 8 | +def test_find_node_project_root_returns_different_roots_for_different_files(tmp_path: Path) -> None: |
| 9 | + """Test that find_node_project_root returns the correct root for each file.""" |
| 10 | + # Create main project structure |
| 11 | + main_project = (tmp_path / "project").resolve() |
| 12 | + main_project.mkdir() |
| 13 | + (main_project / "package.json").write_text("{}", encoding="utf-8") |
| 14 | + (main_project / "src").mkdir() |
| 15 | + main_file = (main_project / "src" / "main.ts").resolve() |
| 16 | + main_file.write_text("// main file", encoding="utf-8") |
| 17 | + |
| 18 | + # Create extension subdirectory with its own package.json |
| 19 | + extension_dir = (main_project / "extensions" / "discord").resolve() |
| 20 | + extension_dir.mkdir(parents=True) |
| 21 | + (extension_dir / "package.json").write_text("{}", encoding="utf-8") |
| 22 | + (extension_dir / "src").mkdir() |
| 23 | + extension_file = (extension_dir / "src" / "accounts.ts").resolve() |
| 24 | + extension_file.write_text("// extension file", encoding="utf-8") |
| 25 | + |
| 26 | + # Extension file should return extension directory |
| 27 | + result1 = find_node_project_root(extension_file) |
| 28 | + assert result1 == extension_dir, f"Expected {extension_dir}, got {result1}" |
| 29 | + |
| 30 | + # Main file should return main project directory |
| 31 | + result2 = find_node_project_root(main_file) |
| 32 | + assert result2 == main_project, f"Expected {main_project}, got {result2}" |
| 33 | + |
| 34 | + # Calling again with extension file should still return extension dir |
| 35 | + result3 = find_node_project_root(extension_file) |
| 36 | + assert result3 == extension_dir, f"Expected {extension_dir}, got {result3}" |
| 37 | + |
| 38 | + |
| 39 | +def test_js_project_root_recalculated_per_function(tmp_path: Path) -> None: |
| 40 | + """Each function in a monorepo should resolve to its own nearest package.json root.""" |
| 41 | + # Create main project |
| 42 | + main_project = (tmp_path / "project").resolve() |
| 43 | + main_project.mkdir() |
| 44 | + (main_project / "package.json").write_text('{"name": "main"}', encoding="utf-8") |
| 45 | + (main_project / "src").mkdir() |
| 46 | + |
| 47 | + # Create extension with its own package.json |
| 48 | + extension_dir = (main_project / "extensions" / "discord").resolve() |
| 49 | + extension_dir.mkdir(parents=True) |
| 50 | + (extension_dir / "package.json").write_text('{"name": "discord-extension"}', encoding="utf-8") |
| 51 | + (extension_dir / "src").mkdir() |
| 52 | + |
| 53 | + extension_file = (extension_dir / "src" / "accounts.ts").resolve() |
| 54 | + extension_file.write_text("export function foo() {}", encoding="utf-8") |
| 55 | + |
| 56 | + main_file = (main_project / "src" / "commands.ts").resolve() |
| 57 | + main_file.write_text("export function bar() {}", encoding="utf-8") |
| 58 | + |
| 59 | + js_project_root_1 = find_node_project_root(extension_file) |
| 60 | + assert js_project_root_1 == extension_dir |
| 61 | + |
| 62 | + js_project_root_2 = find_node_project_root(main_file) |
| 63 | + assert js_project_root_2 == main_project, ( |
| 64 | + f"Expected {main_project}, got {js_project_root_2}. " |
| 65 | + f"Happens when js_project_root is not recalculated per function." |
| 66 | + ) |
0 commit comments