#!/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.")