|
| 1 | +""" |
| 2 | +Unit tests for ConfigFixer FileSystemAnalyzer. |
| 3 | +
|
| 4 | +Tests verify correct usage of FileIdentifier for analyzing project files. |
| 5 | +""" |
| 6 | + |
| 7 | +from unittest.mock import Mock, patch, MagicMock |
| 8 | +from code_indexer.services.config_fixer import FileSystemAnalyzer |
| 9 | +from code_indexer.config import Config |
| 10 | + |
| 11 | + |
| 12 | +class TestFileSystemAnalyzer: |
| 13 | + """Test FileSystemAnalyzer.analyze_project_files() method.""" |
| 14 | + |
| 15 | + def test_analyze_project_files_uses_file_identifier(self, tmp_path): |
| 16 | + """Verify that analyze_project_files uses FileIdentifier correctly.""" |
| 17 | + # Create a mock config |
| 18 | + config = Mock(spec=Config) |
| 19 | + codebase_dir = tmp_path / "test_project" |
| 20 | + codebase_dir.mkdir() |
| 21 | + |
| 22 | + # Create some test files |
| 23 | + (codebase_dir / "test.py").write_text("print('hello')") |
| 24 | + (codebase_dir / "test2.py").write_text("print('world')") |
| 25 | + |
| 26 | + # Mock FileIdentifier to verify it's called correctly |
| 27 | + # Note: Import is inside method, so we mock it at the file_identifier module level |
| 28 | + with patch( |
| 29 | + "code_indexer.services.file_identifier.FileIdentifier" |
| 30 | + ) as mock_file_identifier: |
| 31 | + # Setup mock to return expected data structure |
| 32 | + mock_instance = MagicMock() |
| 33 | + mock_file_identifier.return_value = mock_instance |
| 34 | + mock_instance.get_current_files.return_value = { |
| 35 | + "test.py": {"file_path": "test.py", "file_hash": "abc123"}, |
| 36 | + "test2.py": {"file_path": "test2.py", "file_hash": "def456"}, |
| 37 | + } |
| 38 | + |
| 39 | + # Call the method under test |
| 40 | + result = FileSystemAnalyzer.analyze_project_files(codebase_dir, config) |
| 41 | + |
| 42 | + # Verify FileIdentifier was instantiated with correct parameters |
| 43 | + mock_file_identifier.assert_called_once_with(codebase_dir, config) |
| 44 | + |
| 45 | + # Verify get_current_files() was called (not get_indexable_files()) |
| 46 | + mock_instance.get_current_files.assert_called_once() |
| 47 | + |
| 48 | + # Verify the result has the expected structure |
| 49 | + assert "total_files_to_index" in result |
| 50 | + assert result["total_files_to_index"] == 2 |
| 51 | + assert "discovered_files" in result |
| 52 | + assert len(result["discovered_files"]) == 2 |
| 53 | + |
| 54 | + def test_analyze_project_files_returns_correct_structure(self, tmp_path): |
| 55 | + """Verify analyze_project_files returns the expected dictionary structure.""" |
| 56 | + config = Mock(spec=Config) |
| 57 | + codebase_dir = tmp_path / "test_project" |
| 58 | + codebase_dir.mkdir() |
| 59 | + |
| 60 | + # Create test files with different extensions |
| 61 | + (codebase_dir / "file1.py").write_text("# python") |
| 62 | + (codebase_dir / "file2.js").write_text("// javascript") |
| 63 | + (codebase_dir / "file3.md").write_text("# markdown") |
| 64 | + |
| 65 | + with patch( |
| 66 | + "code_indexer.services.file_identifier.FileIdentifier" |
| 67 | + ) as mock_file_identifier: |
| 68 | + mock_instance = MagicMock() |
| 69 | + mock_file_identifier.return_value = mock_instance |
| 70 | + mock_instance.get_current_files.return_value = { |
| 71 | + "file1.py": {"file_path": "file1.py"}, |
| 72 | + "file2.js": {"file_path": "file2.js"}, |
| 73 | + "file3.md": {"file_path": "file3.md"}, |
| 74 | + } |
| 75 | + |
| 76 | + result = FileSystemAnalyzer.analyze_project_files(codebase_dir, config) |
| 77 | + |
| 78 | + # Verify all required keys exist |
| 79 | + assert "total_files_to_index" in result |
| 80 | + assert "discovered_files" in result |
| 81 | + assert "file_extensions_found" in result |
| 82 | + |
| 83 | + # Verify correct counts |
| 84 | + assert result["total_files_to_index"] == 3 |
| 85 | + assert len(result["discovered_files"]) == 3 |
| 86 | + |
| 87 | + # Verify file extensions are extracted |
| 88 | + extensions = result["file_extensions_found"] |
| 89 | + assert isinstance(extensions, list) |
| 90 | + assert set(extensions) == {"py", "js", "md"} |
| 91 | + |
| 92 | + def test_analyze_project_files_handles_empty_directory(self, tmp_path): |
| 93 | + """Verify analyze_project_files handles directory with no indexable files.""" |
| 94 | + config = Mock(spec=Config) |
| 95 | + codebase_dir = tmp_path / "empty_project" |
| 96 | + codebase_dir.mkdir() |
| 97 | + |
| 98 | + with patch( |
| 99 | + "code_indexer.services.file_identifier.FileIdentifier" |
| 100 | + ) as mock_file_identifier: |
| 101 | + mock_instance = MagicMock() |
| 102 | + mock_file_identifier.return_value = mock_instance |
| 103 | + mock_instance.get_current_files.return_value = {} |
| 104 | + |
| 105 | + result = FileSystemAnalyzer.analyze_project_files(codebase_dir, config) |
| 106 | + |
| 107 | + assert result["total_files_to_index"] == 0 |
| 108 | + assert result["discovered_files"] == [] |
| 109 | + assert result["file_extensions_found"] == [] |
| 110 | + |
| 111 | + def test_analyze_project_files_handles_exceptions_gracefully(self, tmp_path): |
| 112 | + """Verify analyze_project_files returns safe defaults on exceptions.""" |
| 113 | + config = Mock(spec=Config) |
| 114 | + codebase_dir = tmp_path / "test_project" |
| 115 | + codebase_dir.mkdir() |
| 116 | + |
| 117 | + with patch( |
| 118 | + "code_indexer.services.file_identifier.FileIdentifier" |
| 119 | + ) as mock_file_identifier: |
| 120 | + # Simulate an exception during file identification |
| 121 | + mock_file_identifier.side_effect = Exception("File system error") |
| 122 | + |
| 123 | + result = FileSystemAnalyzer.analyze_project_files(codebase_dir, config) |
| 124 | + |
| 125 | + # Should return safe defaults on error |
| 126 | + assert result["total_files_to_index"] == 0 |
| 127 | + assert result["discovered_files"] == [] |
| 128 | + assert result["file_extensions_found"] == [] |
| 129 | + |
| 130 | + def test_analyze_project_files_extracts_file_paths_correctly(self, tmp_path): |
| 131 | + """Verify analyze_project_files extracts file paths from get_current_files() result.""" |
| 132 | + config = Mock(spec=Config) |
| 133 | + codebase_dir = tmp_path / "test_project" |
| 134 | + codebase_dir.mkdir() |
| 135 | + |
| 136 | + with patch( |
| 137 | + "code_indexer.services.file_identifier.FileIdentifier" |
| 138 | + ) as mock_file_identifier: |
| 139 | + mock_instance = MagicMock() |
| 140 | + mock_file_identifier.return_value = mock_instance |
| 141 | + |
| 142 | + # get_current_files() returns Dict[str, Dict[str, Any]] |
| 143 | + # Keys are file paths (strings), values are metadata dicts |
| 144 | + mock_instance.get_current_files.return_value = { |
| 145 | + "src/main.py": {"file_hash": "hash1"}, |
| 146 | + "tests/test_main.py": {"file_hash": "hash2"}, |
| 147 | + "README.md": {"file_hash": "hash3"}, |
| 148 | + } |
| 149 | + |
| 150 | + result = FileSystemAnalyzer.analyze_project_files(codebase_dir, config) |
| 151 | + |
| 152 | + # Verify file paths are extracted from dictionary keys |
| 153 | + discovered = result["discovered_files"] |
| 154 | + assert len(discovered) == 3 |
| 155 | + assert "src/main.py" in discovered |
| 156 | + assert "tests/test_main.py" in discovered |
| 157 | + assert "README.md" in discovered |
0 commit comments