|
| 1 | +"""Test fix_jest_mock_paths function with vitest mocks.""" |
| 2 | + |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +from codeflash.languages.javascript.instrument import fix_jest_mock_paths |
| 6 | + |
| 7 | + |
| 8 | +def test_fix_vitest_mock_paths(): |
| 9 | + """Test that vi.mock() paths are fixed correctly.""" |
| 10 | + # Simulate source at src/agents/workspace.ts importing from ../routing/session-key |
| 11 | + # Test at test/test_workspace.test.ts should mock ../src/routing/session-key, not ../routing/session-key |
| 12 | + |
| 13 | + test_code = """ |
| 14 | +vi.mock('../routing/session-key', () => ({ |
| 15 | + isSubagentSessionKey: vi.fn(), |
| 16 | + isCronSessionKey: vi.fn(), |
| 17 | +})); |
| 18 | +
|
| 19 | +import { filterBootstrapFilesForSession } from '../src/agents/workspace.js'; |
| 20 | + """ |
| 21 | + |
| 22 | + # Create temp directories and files for testing |
| 23 | + import tempfile |
| 24 | + |
| 25 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 26 | + project = Path(tmpdir) |
| 27 | + |
| 28 | + # Create directory structure |
| 29 | + src = project / "src" |
| 30 | + src_agents = src / "agents" |
| 31 | + src_routing = src / "routing" |
| 32 | + test_dir = project / "test" |
| 33 | + |
| 34 | + src_agents.mkdir(parents=True) |
| 35 | + src_routing.mkdir(parents=True) |
| 36 | + test_dir.mkdir(parents=True) |
| 37 | + |
| 38 | + # Create files |
| 39 | + source_file = src_agents / "workspace.ts" |
| 40 | + source_file.write_text("export function filterBootstrapFilesForSession() {}") |
| 41 | + |
| 42 | + routing_file = src_routing / "session-key.ts" |
| 43 | + routing_file.write_text("export function isSubagentSessionKey() {}") |
| 44 | + |
| 45 | + test_file = test_dir / "test_workspace.test.ts" |
| 46 | + test_file.write_text(test_code) |
| 47 | + |
| 48 | + # Fix the paths |
| 49 | + fixed = fix_jest_mock_paths(test_code, test_file, source_file, test_dir) |
| 50 | + |
| 51 | + # Should change ../routing/session-key to ../src/routing/session-key |
| 52 | + assert "../src/routing/session-key" in fixed, f"Expected path to be fixed, got: {fixed}" |
| 53 | + assert "../routing/session-key" not in fixed or "../src/routing/session-key" in fixed |
| 54 | + |
| 55 | + |
| 56 | +def test_fix_jest_mock_paths_still_works(): |
| 57 | + """Test that jest.mock() paths are still fixed correctly.""" |
| 58 | + test_code = """ |
| 59 | +jest.mock('../routing/session-key', () => ({ |
| 60 | + isSubagentSessionKey: jest.fn(), |
| 61 | +})); |
| 62 | + """ |
| 63 | + |
| 64 | + import tempfile |
| 65 | + |
| 66 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 67 | + project = Path(tmpdir) |
| 68 | + src = project / "src" |
| 69 | + src_agents = src / "agents" |
| 70 | + src_routing = src / "routing" |
| 71 | + test_dir = project / "test" |
| 72 | + |
| 73 | + src_agents.mkdir(parents=True) |
| 74 | + src_routing.mkdir(parents=True) |
| 75 | + test_dir.mkdir(parents=True) |
| 76 | + |
| 77 | + source_file = src_agents / "workspace.ts" |
| 78 | + source_file.write_text("") |
| 79 | + |
| 80 | + routing_file = src_routing / "session-key.ts" |
| 81 | + routing_file.write_text("") |
| 82 | + |
| 83 | + test_file = test_dir / "test_workspace.test.ts" |
| 84 | + test_file.write_text(test_code) |
| 85 | + |
| 86 | + fixed = fix_jest_mock_paths(test_code, test_file, source_file, test_dir) |
| 87 | + |
| 88 | + assert "../src/routing/session-key" in fixed |
| 89 | + |
| 90 | + |
| 91 | +if __name__ == "__main__": |
| 92 | + test_fix_vitest_mock_paths() |
| 93 | + test_fix_jest_mock_paths_still_works() |
| 94 | + print("All tests passed!") |
0 commit comments