|
| 1 | +"""Test for inject_test_globals duplicate import bug. |
| 2 | +
|
| 3 | +This test reproduces the bug where AI-generated tests already have vitest imports, |
| 4 | +but inject_test_globals() adds them again because the string-based check doesn't |
| 5 | +catch semantic duplicates with different identifier orders. |
| 6 | +""" |
| 7 | + |
| 8 | +import pytest |
| 9 | +from codeflash.languages.javascript.edit_tests import inject_test_globals |
| 10 | +from codeflash.models.models import GeneratedTests, GeneratedTestsList |
| 11 | +from pathlib import Path |
| 12 | + |
| 13 | + |
| 14 | +def test_inject_test_globals_skips_existing_vitest_imports() -> None: |
| 15 | + """Test that inject_test_globals skips injection when vitest import already exists.""" |
| 16 | + # AI service generated this test with vitest imports already present |
| 17 | + # (note: different order and identifiers than what inject_test_globals would add) |
| 18 | + ai_generated_test = """// vitest imports (REQUIRED for vitest - globals are NOT enabled by default) |
| 19 | +import { describe, test, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 20 | +// function import |
| 21 | +import { isWindowsDrivePath } from './infra/archive-path'; |
| 22 | +
|
| 23 | +// unit tests |
| 24 | +describe('isWindowsDrivePath', () => { |
| 25 | + test('should return true for Windows drive paths', () => { |
| 26 | + expect(isWindowsDrivePath('C:\\\\')).toBe(true); |
| 27 | + }); |
| 28 | +}); |
| 29 | +""" |
| 30 | + |
| 31 | + generated_tests = GeneratedTestsList( |
| 32 | + generated_tests=[ |
| 33 | + GeneratedTests( |
| 34 | + generated_original_test_source=ai_generated_test, |
| 35 | + instrumented_behavior_test_source=ai_generated_test, |
| 36 | + instrumented_perf_test_source=ai_generated_test, |
| 37 | + behavior_file_path=Path("/tmp/test_isWindowsDrivePath.test.ts"), |
| 38 | + perf_file_path=Path("/tmp/test_isWindowsDrivePath_perf.test.ts"), |
| 39 | + ) |
| 40 | + ] |
| 41 | + ) |
| 42 | + |
| 43 | + # Call inject_test_globals for vitest + esm (this is what the CLI does) |
| 44 | + result = inject_test_globals(generated_tests, test_framework="vitest", module_system="esm") |
| 45 | + |
| 46 | + # Check that the import was NOT duplicated |
| 47 | + result_source = result.generated_tests[0].generated_original_test_source |
| 48 | + |
| 49 | + # Count how many times "from 'vitest'" appears |
| 50 | + import_count = result_source.count("from 'vitest'") |
| 51 | + |
| 52 | + # Should be exactly 1 import, not 2 |
| 53 | + assert import_count == 1, ( |
| 54 | + f"Expected exactly 1 vitest import, but found {import_count}. " |
| 55 | + f"inject_test_globals() added a duplicate import when one already existed.\n" |
| 56 | + f"Result:\n{result_source[:500]}" |
| 57 | + ) |
| 58 | + |
| 59 | + # Also verify that we have the expected number of import statements |
| 60 | + # Count actual import statements, not comments containing the word "import" |
| 61 | + import_lines = [line for line in result_source.split('\n') if line.strip().startswith('import ')] |
| 62 | + assert len(import_lines) == 2, f"Should have 2 import statements (vitest + function), found {len(import_lines)}: {import_lines}" |
| 63 | + |
| 64 | + |
| 65 | +def test_inject_test_globals_adds_import_when_missing() -> None: |
| 66 | + """Test that inject_test_globals DOES add import when it's truly missing.""" |
| 67 | + # Test without any vitest imports |
| 68 | + test_without_imports = """// function import |
| 69 | +import { isWindowsDrivePath } from './infra/archive-path'; |
| 70 | +
|
| 71 | +describe('isWindowsDrivePath', () => { |
| 72 | + test('should return true', () => { |
| 73 | + expect(isWindowsDrivePath('C:\\\\')).toBe(true); |
| 74 | + }); |
| 75 | +}); |
| 76 | +""" |
| 77 | + |
| 78 | + generated_tests = GeneratedTestsList( |
| 79 | + generated_tests=[ |
| 80 | + GeneratedTests( |
| 81 | + generated_original_test_source=test_without_imports, |
| 82 | + instrumented_behavior_test_source=test_without_imports, |
| 83 | + instrumented_perf_test_source=test_without_imports, |
| 84 | + behavior_file_path=Path("/tmp/test.test.ts"), |
| 85 | + perf_file_path=Path("/tmp/test_perf.test.ts"), |
| 86 | + ) |
| 87 | + ] |
| 88 | + ) |
| 89 | + |
| 90 | + result = inject_test_globals(generated_tests, test_framework="vitest", module_system="esm") |
| 91 | + result_source = result.generated_tests[0].generated_original_test_source |
| 92 | + |
| 93 | + # Should have exactly 1 vitest import (the one we added) |
| 94 | + import_count = result_source.count("from 'vitest'") |
| 95 | + assert import_count == 1, f"Expected vitest import to be added, found {import_count}" |
| 96 | + |
| 97 | + # Should be at the beginning of the file |
| 98 | + assert result_source.startswith("import { vi, describe, it, expect"), ( |
| 99 | + "Vitest import should be added at the beginning" |
| 100 | + ) |
0 commit comments