Spaces:
Running
Running
| """ | |
| Test validation of word files for MIN_REQUIRED threshold compliance. | |
| """ | |
| import os | |
| import tempfile | |
| import shutil | |
| from wrdler.word_loader_ai import _save_ai_words_to_file | |
| from wrdler.word_loader import MIN_REQUIRED | |
| def test_save_ai_words_validates_min_required(): | |
| """Test that _save_ai_words_to_file returns insufficiency info.""" | |
| # Create a temporary directory for test files | |
| test_dir = tempfile.mkdtemp() | |
| try: | |
| # Mock the words directory to point to our temp dir | |
| import wrdler.word_loader_ai as wl_ai | |
| original_dirname = wl_ai.os.path.dirname | |
| def mock_dirname(path): | |
| if "word_loader_ai.py" in path: | |
| return test_dir | |
| return original_dirname(path) | |
| wl_ai.os.path.dirname = mock_dirname | |
| # Test case 1: Insufficient words (should return non-empty dict) | |
| insufficient_words = [ | |
| "COOK", "BAKE", "HEAT", # 3 x 4-letter (need 25) | |
| "ROAST", "GRILL", "STEAM", # 3 x 5-letter (need 25) | |
| "SIMMER", "BRAISE", # 2 x 6-letter (need 25) | |
| ] | |
| filename, insufficient = _save_ai_words_to_file("test_topic", insufficient_words) | |
| assert filename == "test_topic.txt", f"Expected 'test_topic.txt', got '{filename}'" | |
| assert len(insufficient) > 0, "Expected insufficient_lengths to be non-empty" | |
| assert 4 in insufficient, "Expected 4-letter words to be insufficient" | |
| assert 5 in insufficient, "Expected 5-letter words to be insufficient" | |
| assert 6 in insufficient, "Expected 6-letter words to be insufficient" | |
| # Test case 2: Sufficient words (should return empty dict) | |
| sufficient_words = [] | |
| for length in [4, 5, 6]: | |
| for i in range(MIN_REQUIRED): | |
| # Generate unique words of the required length | |
| word = chr(65 + (i % 26)) * length + str(i).zfill(length - 1) | |
| sufficient_words.append(word[:length].upper()) | |
| filename2, insufficient2 = _save_ai_words_to_file("test_sufficient", sufficient_words) | |
| assert filename2 == "test_sufficient.txt", f"Expected 'test_sufficient.txt', got '{filename2}'" | |
| assert len(insufficient2) == 0, f"Expected empty insufficient_lengths, got {insufficient2}" | |
| print("? All validation tests passed!") | |
| finally: | |
| # Restore original dirname | |
| wl_ai.os.path.dirname = original_dirname | |
| # Clean up temp directory | |
| shutil.rmtree(test_dir, ignore_errors=True) | |
| if __name__ == "__main__": | |
| test_save_ai_words_validates_min_required() | |