File size: 2,666 Bytes
950b0bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"""
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()