File size: 2,906 Bytes
38c016b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env python3
"""
Quick test to verify the bounds checking fix.
"""

import sys
from pathlib import Path

# Add project root to path
project_root = Path(__file__).parent.parent  # Go up from test-integration to backend-py
sys.path.insert(0, str(project_root))

from src.services.crossword_generator_fixed import CrosswordGeneratorFixed

def test_bounds_checking():
    """Test that placement score calculation doesn't crash with out-of-bounds access."""
    print("πŸ§ͺ Testing bounds checking fix...")
    
    generator = CrosswordGeneratorFixed()
    
    # Create a small grid
    grid = [["." for _ in range(5)] for _ in range(5)]
    
    # Test placement that would go out of bounds
    placement = {
        "row": 3,       # Starting at row 3
        "col": 2,       # Starting at col 2  
        "direction": "vertical"
    }
    
    # Word that would extend beyond grid (3+8=11 > 5)
    word = "ELEPHANT"  # 8 letters, would go from row 3 to row 10 (out of bounds)
    
    try:
        # This should NOT crash with bounds checking
        score = generator._calculate_placement_score(grid, word, placement, [])
        print(f"βœ… Success! Placement score calculated: {score}")
        print("βœ… Bounds checking is working correctly")
        return True
    except IndexError as e:
        print(f"❌ IndexError still occurs: {e}")
        return False
    except Exception as e:
        print(f"❌ Other error: {e}")
        return False

def test_valid_placement():
    """Test that valid placements still work correctly."""
    print("\nπŸ§ͺ Testing valid placement scoring...")
    
    generator = CrosswordGeneratorFixed()
    
    # Create a grid with some letters
    grid = [["." for _ in range(8)] for _ in range(8)]
    grid[2][2] = "A"  # Place an 'A' at position (2,2)
    
    # Test placement that intersects properly
    placement = {
        "row": 2,
        "col": 1,
        "direction": "horizontal"
    }
    
    word = "CAT"  # Should intersect at the 'A'
    
    try:
        score = generator._calculate_placement_score(grid, word, placement, [])
        print(f"βœ… Valid placement score: {score}")
        
        # Should have intersection bonus (score > 100)
        if score > 300:  # Base 100 + intersection 200
            print("βœ… Intersection detection working")
        else:
            print(f"⚠️ Expected intersection bonus, got score {score}")
        
        return True
    except Exception as e:
        print(f"❌ Error with valid placement: {e}")
        return False

if __name__ == "__main__":
    print("πŸ”§ Testing crossword generator bounds fix\n")
    
    test1_pass = test_bounds_checking()
    test2_pass = test_valid_placement()
    
    if test1_pass and test2_pass:
        print("\nβœ… All tests passed! The bounds checking fix is working.")
    else:
        print("\n❌ Some tests failed. More work needed.")