"""Tests for Local Code Assistant utilities.""" import pytest from pathlib import Path from unittest.mock import Mock, patch from local_code_assistant.utils.language import ( detect_language_from_extension, detect_language_from_content, detect_language, get_language_config, get_supported_languages, is_language_supported, get_file_extension, get_comment_style, get_testing_framework, ) from local_code_assistant.utils.context import ( should_ignore, scan_directory, build_context_string, get_project_info, ) class TestLanguageDetection: """Tests for language detection utilities.""" def test_detect_python_from_extension(self): """Test Python detection from extension.""" path = Path("test.py") assert detect_language_from_extension(path) == "python" def test_detect_javascript_from_extension(self): """Test JavaScript detection from extension.""" path = Path("script.js") assert detect_language_from_extension(path) == "javascript" def test_detect_typescript_from_extension(self): """Test TypeScript detection from extension.""" path = Path("module.ts") assert detect_language_from_extension(path) == "typescript" def test_detect_go_from_extension(self): """Test Go detection from extension.""" path = Path("main.go") assert detect_language_from_extension(path) == "go" def test_detect_rust_from_extension(self): """Test Rust detection from extension.""" path = Path("lib.rs") assert detect_language_from_extension(path) == "rust" def test_detect_from_content_python(self): """Test Python detection from content.""" code = "def hello():\n print('world')" assert detect_language_from_content(code) == "python" def test_detect_from_content_javascript(self): """Test JavaScript detection from content.""" code = "function hello() {\n console.log('world');\n}" assert detect_language_from_content(code) == "javascript" def test_detect_from_content_go(self): """Test Go detection from content.""" code = "func main() {\n fmt.Println(\"Hello\")\n}" assert detect_language_from_content(code) == "go" def test_detect_from_content_rust(self): """Test Rust detection from content.""" code = "fn main() {\n println!(\"Hello\");\n}" assert detect_language_from_content(code) == "rust" def test_detect_language_combined(self): """Test combined detection (extension + content).""" path = Path("script.py") assert detect_language(path) == "python" def test_get_supported_languages(self): """Test getting supported languages list.""" languages = get_supported_languages() assert "python" in languages assert "javascript" in languages def test_is_language_supported(self): """Test language support checking.""" assert is_language_supported("python") is True assert is_language_supported("unknown") is False def test_get_file_extension(self): """Test file extension retrieval.""" assert get_file_extension("python") == ".py" assert get_file_extension("javascript") == ".js" def test_get_comment_style(self): """Test comment style retrieval.""" assert get_comment_style("python") == "#" assert get_comment_style("javascript") == "//" def test_get_testing_framework(self): """Test testing framework retrieval.""" assert get_testing_framework("python") == "pytest" assert get_testing_framework("rust") == "test" class TestContextBuilding: """Tests for context building utilities.""" def test_should_ignore_python_cache(self): """Test ignoring __pycache__ directories.""" path = Path("project/__pycache__/module.pyc") assert should_ignore(path) is True def test_should_ignore_git(self): """Test ignoring .git directories.""" path = Path("project/.git/config") assert should_ignore(path) is True def test_should_ignore_node_modules(self): """Test ignoring node_modules.""" path = Path("project/node_modules/package/index.js") assert should_ignore(path) is True def test_should_not_ignore_source_files(self): """Test not ignoring source files.""" path = Path("project/src/main.py") assert should_ignore(path) is False def test_build_context_string(self): """Test context string building.""" files = { "/project/main.py": "def main(): pass", "/project/utils.py": "def util(): pass" } context = build_context_string(files, max_files=5) assert "main.py" in context assert "utils.py" in context assert "def main(): pass" in context def test_build_context_string_limit(self): """Test context string file limit.""" files = { "/project/f1.py": "code1", "/project/f2.py": "code2", "/project/f3.py": "code3" } context = build_context_string(files, max_files=2) assert context.count("===") == 2 def test_scan_directory_empty(self, tmp_path): """Test scanning empty directory.""" files = scan_directory(tmp_path) assert files == {} def test_scan_directory_nonexistent(self): """Test scanning nonexistent directory.""" files = scan_directory(Path("/nonexistent")) assert files == {} def test_get_project_info_python(self, tmp_path): """Test detecting Python project.""" (tmp_path / "pyproject.toml").write_text("[tool.poetry]") info = get_project_info(tmp_path) assert info.get("build_system") == "python" def test_get_project_info_javascript(self, tmp_path): """Test detecting JavaScript project.""" (tmp_path / "package.json").write_text("{\"name\": \"test\"}") info = get_project_info(tmp_path) assert info.get("build_system") == "javascript" def test_get_project_info_go(self, tmp_path): """Test detecting Go project.""" (tmp_path / "go.mod").write_text("module test") info = get_project_info(tmp_path) assert info.get("build_system") == "go" def test_get_project_info_rust(self, tmp_path): """Test detecting Rust project.""" (tmp_path / "Cargo.toml").write_text("[package]") info = get_project_info(tmp_path) assert info.get("build_system") == "rust"