|
| 1 | +"""Test for false positive test discovery bug (Bug #4).""" |
| 2 | + |
| 3 | +from pathlib import Path |
| 4 | +from tempfile import TemporaryDirectory |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from codeflash.discovery.functions_to_optimize import FunctionToOptimize |
| 9 | +from codeflash.languages.javascript.support import TypeScriptSupport |
| 10 | +from codeflash.models.models import CodePosition |
| 11 | + |
| 12 | + |
| 13 | +def test_discover_tests_should_not_match_mocked_functions(): |
| 14 | + """Test that functions mentioned only in mocks are not matched as test targets. |
| 15 | +
|
| 16 | + Regression test for Bug #4: False positive test discovery due to substring matching. |
| 17 | +
|
| 18 | + When a test file mocks a function (e.g., vi.mock("./restart-request.js", () => ({...}))), |
| 19 | + that function should NOT be considered as tested by that file, since it's only mocked, |
| 20 | + not actually called or tested. |
| 21 | + """ |
| 22 | + support = TypeScriptSupport() |
| 23 | + |
| 24 | + with TemporaryDirectory() as tmpdir: |
| 25 | + test_root = Path(tmpdir) |
| 26 | + |
| 27 | + # Create a test file that MOCKS parseRestartRequestParams but doesn't test it |
| 28 | + test_file = test_root / "update.test.ts" |
| 29 | + test_file.write_text( |
| 30 | + ''' |
| 31 | +import { updateSomething } from "./update.js"; |
| 32 | +
|
| 33 | +vi.mock("./restart-request.js", () => ({ |
| 34 | + parseRestartRequestParams: (params: any) => ({ sessionKey: undefined }), |
| 35 | +})); |
| 36 | +
|
| 37 | +describe("updateSomething", () => { |
| 38 | + it("should update successfully", () => { |
| 39 | + const result = updateSomething(); |
| 40 | + expect(result).toBe(true); |
| 41 | + }); |
| 42 | +}); |
| 43 | +''' |
| 44 | + ) |
| 45 | + |
| 46 | + # Source function that is only mocked, not tested |
| 47 | + source_function = FunctionToOptimize( |
| 48 | + qualified_name="parseRestartRequestParams", |
| 49 | + function_name="parseRestartRequestParams", |
| 50 | + file_path=test_root / "restart-request.ts", |
| 51 | + starting_line=1, |
| 52 | + ending_line=10, |
| 53 | + function_signature="", |
| 54 | + code_position=CodePosition(line_no=1, col_no=0), |
| 55 | + file_path_relative_to_project_root="restart-request.ts", |
| 56 | + ) |
| 57 | + |
| 58 | + # Discover tests |
| 59 | + result = support.discover_tests(test_root, [source_function]) |
| 60 | + |
| 61 | + # The bug: discovers update.test.ts as a test for parseRestartRequestParams |
| 62 | + # because "parseRestartRequestParams" appears as a substring in the mock |
| 63 | + # Expected: should NOT match (empty result) |
| 64 | + assert ( |
| 65 | + source_function.qualified_name not in result or len(result[source_function.qualified_name]) == 0 |
| 66 | + ), f"Should not match mocked function, but found: {result.get(source_function.qualified_name, [])}" |
| 67 | + |
| 68 | + |
| 69 | +def test_discover_tests_should_match_actually_imported_functions(): |
| 70 | + """Test that functions actually imported and tested ARE correctly matched. |
| 71 | +
|
| 72 | + This is the positive case to ensure we don't break legitimate test discovery. |
| 73 | + """ |
| 74 | + support = TypeScriptSupport() |
| 75 | + |
| 76 | + with TemporaryDirectory() as tmpdir: |
| 77 | + test_root = Path(tmpdir) |
| 78 | + |
| 79 | + # Create a test file that ACTUALLY imports and tests the function |
| 80 | + test_file = test_root / "restart-request.test.ts" |
| 81 | + test_file.write_text( |
| 82 | + ''' |
| 83 | +import { parseRestartRequestParams } from "./restart-request.js"; |
| 84 | +
|
| 85 | +describe("parseRestartRequestParams", () => { |
| 86 | + it("should parse valid params", () => { |
| 87 | + const result = parseRestartRequestParams({ sessionKey: "abc" }); |
| 88 | + expect(result.sessionKey).toBe("abc"); |
| 89 | + }); |
| 90 | +}); |
| 91 | +''' |
| 92 | + ) |
| 93 | + |
| 94 | + source_function = FunctionToOptimize( |
| 95 | + qualified_name="parseRestartRequestParams", |
| 96 | + function_name="parseRestartRequestParams", |
| 97 | + file_path=test_root / "restart-request.ts", |
| 98 | + starting_line=1, |
| 99 | + ending_line=10, |
| 100 | + function_signature="", |
| 101 | + code_position=CodePosition(line_no=1, col_no=0), |
| 102 | + file_path_relative_to_project_root="restart-request.ts", |
| 103 | + ) |
| 104 | + |
| 105 | + result = support.discover_tests(test_root, [source_function]) |
| 106 | + |
| 107 | + # Should match: function is imported and tested |
| 108 | + assert source_function.qualified_name in result, f"Should match imported function, but got: {result}" |
| 109 | + assert len(result[source_function.qualified_name]) > 0, "Should find at least one test" |
0 commit comments