""" Tests for FileSelector component - no GUI required. """ import pytest from pathlib import Path import tempfile from src.ui.models import FileItem, FileStatus class TestFileSelectorConstants: """Test FileSelector constants and configuration.""" def test_supported_extensions(self): """Test supported file extensions.""" from src.ui.file_selector import FileSelector expected = [".txt", ".md", ".html", ".htm"] assert FileSelector.SUPPORTED_EXTENSIONS == expected def test_file_filter(self): """Test file filter string.""" from src.ui.file_selector import FileSelector filter_str = FileSelector.FILE_FILTER assert "*.txt" in filter_str assert "*.md" in filter_str assert "*.html" in filter_str class TestFileScanner: """Test FileScanner thread logic.""" def test_scanner_initialization(self): """Test scanner initialization with path and extensions.""" from src.ui.file_selector import FileScanner with tempfile.TemporaryDirectory() as tmpdir: test_path = Path(tmpdir) extensions = [".txt"] scanner = FileScanner(test_path, extensions) assert scanner._path == test_path assert scanner._extensions == extensions assert scanner._is_running is True def test_is_supported_file(self): """Test file extension checking.""" from src.ui.file_selector import FileScanner with tempfile.TemporaryDirectory() as tmpdir: test_path = Path(tmpdir) extensions = [".txt", ".md"] scanner = FileScanner(test_path, extensions) # Create test files (test_path / "test.txt").touch() (test_path / "test.md").touch() (test_path / "test.html").touch() assert scanner._is_supported_file(test_path / "test.txt") is True assert scanner._is_supported_file(test_path / "test.md") is True assert scanner._is_supported_file(test_path / "test.html") is False def test_create_file_item(self): """Test FileItem creation from path.""" from src.ui.file_selector import FileScanner with tempfile.TemporaryDirectory() as tmpdir: test_path = Path(tmpdir) extensions = [".txt"] # Create a test file with content test_file = test_path / "test.txt" test_file.write_text("Hello, World!") scanner = FileScanner(test_path, extensions) item = scanner._create_file_item(test_file) assert isinstance(item, FileItem) assert item.name == "test.txt" assert item.path == test_file assert item.size == len("Hello, World!") assert item.status == FileStatus.PENDING def test_stop_scanner(self): """Test stopping the scanner.""" from src.ui.file_selector import FileScanner with tempfile.TemporaryDirectory() as tmpdir: test_path = Path(tmpdir) extensions = [".txt"] scanner = FileScanner(test_path, extensions) scanner.stop() assert scanner._is_running is False class TestFileItemExtensions: """Test FileItem extensions for file selector.""" def test_file_item_with_real_file(self): """Test FileItem with actual file stats.""" with tempfile.TemporaryDirectory() as tmpdir: test_path = Path(tmpdir) / "novel.txt" content = "This is a test novel file.\n" * 10 test_path.write_text(content) item = FileItem( path=test_path, name=test_path.name, size=test_path.stat().st_size, status=FileStatus.READY ) assert item.name == "novel.txt" assert item.size == len(content) assert item.status == FileStatus.READY assert item.total_words == 0 assert item.translated_words == 0 def test_file_item_progress(self): """Test FileItem progress calculation.""" item = FileItem( path=Path("/test/novel.txt"), name="novel.txt", size=10000, total_words=5000, translated_words=2500, status=FileStatus.TRANSLATING ) assert item.progress == 50.0 def test_file_item_size_formatting(self): """Test FileItem size formatting for various sizes.""" # Bytes item1 = FileItem(Path("/test/small.txt"), "small.txt", 512) assert "B" in item1.size_formatted # Kilobytes item2 = FileItem(Path("/test/medium.txt"), "medium.txt", 2048) assert "KB" in item2.size_formatted # Megabytes item3 = FileItem(Path("/test/large.txt"), "large.txt", 1024 * 1024 * 5) assert "MB" in item3.size_formatted class TestFileValidation: """Test file validation logic.""" def test_validate_existing_files(self): """Test validation of existing files.""" with tempfile.TemporaryDirectory() as tmpdir: test_path = Path(tmpdir) # Create test files (test_path / "test.txt").touch() (test_path / "test.md").touch() (test_path / "test.html").touch() files = [ test_path / "test.txt", test_path / "test.md", test_path / "test.html", ] # All should be valid for file_path in files: assert file_path.exists() assert file_path.is_file() def test_validate_nonexistent_file(self): """Test validation of non-existent file.""" from src.ui.file_selector import FileScanner with tempfile.TemporaryDirectory() as tmpdir: test_path = Path(tmpdir) extensions = [".txt"] scanner = FileScanner(test_path, extensions) nonexistent = test_path / "does_not_exist.txt" assert scanner._is_supported_file(nonexistent) is True assert nonexistent.exists() is False def test_validate_unsupported_extension(self): """Test validation of unsupported file type.""" from src.ui.file_selector import FileScanner with tempfile.TemporaryDirectory() as tmpdir: test_path = Path(tmpdir) extensions = [".txt", ".md"] scanner = FileScanner(test_path, extensions) # Create file with unsupported extension unsupported = test_path / "test.pdf" unsupported.touch() assert scanner._is_supported_file(unsupported) is False def test_validate_directory_vs_file(self): """Test validation distinguishes files from directories.""" with tempfile.TemporaryDirectory() as tmpdir: test_path = Path(tmpdir) # Create a directory test_dir = test_path / "subdir" test_dir.mkdir() # Create a file test_file = test_path / "test.txt" test_file.touch() assert test_dir.is_dir() is True assert test_file.is_file() is True assert test_dir.is_file() is False assert test_file.is_dir() is False