test_file_selector.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. """
  2. Tests for FileSelector component - no GUI required.
  3. """
  4. import pytest
  5. from pathlib import Path
  6. import tempfile
  7. from src.ui.models import FileItem, FileStatus
  8. class TestFileSelectorConstants:
  9. """Test FileSelector constants and configuration."""
  10. def test_supported_extensions(self):
  11. """Test supported file extensions."""
  12. from src.ui.file_selector import FileSelector
  13. expected = [".txt", ".md", ".html", ".htm"]
  14. assert FileSelector.SUPPORTED_EXTENSIONS == expected
  15. def test_file_filter(self):
  16. """Test file filter string."""
  17. from src.ui.file_selector import FileSelector
  18. filter_str = FileSelector.FILE_FILTER
  19. assert "*.txt" in filter_str
  20. assert "*.md" in filter_str
  21. assert "*.html" in filter_str
  22. class TestFileScanner:
  23. """Test FileScanner thread logic."""
  24. def test_scanner_initialization(self):
  25. """Test scanner initialization with path and extensions."""
  26. from src.ui.file_selector import FileScanner
  27. with tempfile.TemporaryDirectory() as tmpdir:
  28. test_path = Path(tmpdir)
  29. extensions = [".txt"]
  30. scanner = FileScanner(test_path, extensions)
  31. assert scanner._path == test_path
  32. assert scanner._extensions == extensions
  33. assert scanner._is_running is True
  34. def test_is_supported_file(self):
  35. """Test file extension checking."""
  36. from src.ui.file_selector import FileScanner
  37. with tempfile.TemporaryDirectory() as tmpdir:
  38. test_path = Path(tmpdir)
  39. extensions = [".txt", ".md"]
  40. scanner = FileScanner(test_path, extensions)
  41. # Create test files
  42. (test_path / "test.txt").touch()
  43. (test_path / "test.md").touch()
  44. (test_path / "test.html").touch()
  45. assert scanner._is_supported_file(test_path / "test.txt") is True
  46. assert scanner._is_supported_file(test_path / "test.md") is True
  47. assert scanner._is_supported_file(test_path / "test.html") is False
  48. def test_create_file_item(self):
  49. """Test FileItem creation from path."""
  50. from src.ui.file_selector import FileScanner
  51. with tempfile.TemporaryDirectory() as tmpdir:
  52. test_path = Path(tmpdir)
  53. extensions = [".txt"]
  54. # Create a test file with content
  55. test_file = test_path / "test.txt"
  56. test_file.write_text("Hello, World!")
  57. scanner = FileScanner(test_path, extensions)
  58. item = scanner._create_file_item(test_file)
  59. assert isinstance(item, FileItem)
  60. assert item.name == "test.txt"
  61. assert item.path == test_file
  62. assert item.size == len("Hello, World!")
  63. assert item.status == FileStatus.PENDING
  64. def test_stop_scanner(self):
  65. """Test stopping the scanner."""
  66. from src.ui.file_selector import FileScanner
  67. with tempfile.TemporaryDirectory() as tmpdir:
  68. test_path = Path(tmpdir)
  69. extensions = [".txt"]
  70. scanner = FileScanner(test_path, extensions)
  71. scanner.stop()
  72. assert scanner._is_running is False
  73. class TestFileItemExtensions:
  74. """Test FileItem extensions for file selector."""
  75. def test_file_item_with_real_file(self):
  76. """Test FileItem with actual file stats."""
  77. with tempfile.TemporaryDirectory() as tmpdir:
  78. test_path = Path(tmpdir) / "novel.txt"
  79. content = "This is a test novel file.\n" * 10
  80. test_path.write_text(content)
  81. item = FileItem(
  82. path=test_path,
  83. name=test_path.name,
  84. size=test_path.stat().st_size,
  85. status=FileStatus.READY
  86. )
  87. assert item.name == "novel.txt"
  88. assert item.size == len(content)
  89. assert item.status == FileStatus.READY
  90. assert item.total_words == 0
  91. assert item.translated_words == 0
  92. def test_file_item_progress(self):
  93. """Test FileItem progress calculation."""
  94. item = FileItem(
  95. path=Path("/test/novel.txt"),
  96. name="novel.txt",
  97. size=10000,
  98. total_words=5000,
  99. translated_words=2500,
  100. status=FileStatus.TRANSLATING
  101. )
  102. assert item.progress == 50.0
  103. def test_file_item_size_formatting(self):
  104. """Test FileItem size formatting for various sizes."""
  105. # Bytes
  106. item1 = FileItem(Path("/test/small.txt"), "small.txt", 512)
  107. assert "B" in item1.size_formatted
  108. # Kilobytes
  109. item2 = FileItem(Path("/test/medium.txt"), "medium.txt", 2048)
  110. assert "KB" in item2.size_formatted
  111. # Megabytes
  112. item3 = FileItem(Path("/test/large.txt"), "large.txt", 1024 * 1024 * 5)
  113. assert "MB" in item3.size_formatted
  114. class TestFileValidation:
  115. """Test file validation logic."""
  116. def test_validate_existing_files(self):
  117. """Test validation of existing files."""
  118. with tempfile.TemporaryDirectory() as tmpdir:
  119. test_path = Path(tmpdir)
  120. # Create test files
  121. (test_path / "test.txt").touch()
  122. (test_path / "test.md").touch()
  123. (test_path / "test.html").touch()
  124. files = [
  125. test_path / "test.txt",
  126. test_path / "test.md",
  127. test_path / "test.html",
  128. ]
  129. # All should be valid
  130. for file_path in files:
  131. assert file_path.exists()
  132. assert file_path.is_file()
  133. def test_validate_nonexistent_file(self):
  134. """Test validation of non-existent file."""
  135. from src.ui.file_selector import FileScanner
  136. with tempfile.TemporaryDirectory() as tmpdir:
  137. test_path = Path(tmpdir)
  138. extensions = [".txt"]
  139. scanner = FileScanner(test_path, extensions)
  140. nonexistent = test_path / "does_not_exist.txt"
  141. assert scanner._is_supported_file(nonexistent) is True
  142. assert nonexistent.exists() is False
  143. def test_validate_unsupported_extension(self):
  144. """Test validation of unsupported file type."""
  145. from src.ui.file_selector import FileScanner
  146. with tempfile.TemporaryDirectory() as tmpdir:
  147. test_path = Path(tmpdir)
  148. extensions = [".txt", ".md"]
  149. scanner = FileScanner(test_path, extensions)
  150. # Create file with unsupported extension
  151. unsupported = test_path / "test.pdf"
  152. unsupported.touch()
  153. assert scanner._is_supported_file(unsupported) is False
  154. def test_validate_directory_vs_file(self):
  155. """Test validation distinguishes files from directories."""
  156. with tempfile.TemporaryDirectory() as tmpdir:
  157. test_path = Path(tmpdir)
  158. # Create a directory
  159. test_dir = test_path / "subdir"
  160. test_dir.mkdir()
  161. # Create a file
  162. test_file = test_path / "test.txt"
  163. test_file.touch()
  164. assert test_dir.is_dir() is True
  165. assert test_file.is_file() is True
  166. assert test_dir.is_file() is False
  167. assert test_file.is_dir() is False