#!/usr/bin/env python3 import sys import asyncio 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 import CrosswordGenerator async def test_boundary_fix(): """Test that the boundary fix works correctly.""" # Sample words that are known to cause boundary issues test_words = [ {"word": "COMPUTER", "clue": "Electronic device"}, {"word": "MACHINE", "clue": "Device with moving parts"}, {"word": "SCIENCE", "clue": "Systematic study"}, {"word": "EXPERT", "clue": "Specialist"}, {"word": "CODE", "clue": "Programming text"}, {"word": "DATA", "clue": "Information"} ] generator = CrosswordGenerator() print("๐Ÿงช Testing Boundary Fix") print("=" * 50) # Generate a crossword result = generator._create_grid(test_words) if not result: print("โŒ Grid generation failed") return False grid = result["grid"] placed_words = result["placed_words"] print(f"โœ… Generated grid with {len(placed_words)} words") print(f"Grid size: {len(grid)}x{len(grid[0])}") # Display the grid print("\nGenerated Grid:") for i, row in enumerate(grid): row_str = " ".join(cell if cell != "." else " " for cell in row) print(f"{i:2d} | {row_str}") print(f"\nPlaced Words:") for word in placed_words: print(f" {word['word']} at ({word['row']},{word['col']}) {word['direction']}") # Analyze for boundary violations print(f"\n๐Ÿ” Analyzing for boundary violations...") violations = [] # Check horizontal words for r in range(len(grid)): current_word = "" word_start = -1 for c in range(len(grid[r])): if grid[r][c] != ".": if current_word == "": word_start = c current_word += grid[r][c] else: if current_word: # Word ended - check if it's a valid placed word is_valid_word = any( placed['word'] == current_word and placed['row'] == r and placed['col'] == word_start and placed['direction'] == 'horizontal' for placed in placed_words ) if not is_valid_word and len(current_word) > 1: violations.append(f"Invalid horizontal word '{current_word}' at ({r},{word_start})") current_word = "" # Check word at end of row if current_word: is_valid_word = any( placed['word'] == current_word and placed['row'] == r and placed['col'] == word_start and placed['direction'] == 'horizontal' for placed in placed_words ) if not is_valid_word and len(current_word) > 1: violations.append(f"Invalid horizontal word '{current_word}' at ({r},{word_start})") # Check vertical words for c in range(len(grid[0])): current_word = "" word_start = -1 for r in range(len(grid)): if grid[r][c] != ".": if current_word == "": word_start = r current_word += grid[r][c] else: if current_word: # Word ended - check if it's a valid placed word is_valid_word = any( placed['word'] == current_word and placed['row'] == word_start and placed['col'] == c and placed['direction'] == 'vertical' for placed in placed_words ) if not is_valid_word and len(current_word) > 1: violations.append(f"Invalid vertical word '{current_word}' at ({word_start},{c})") current_word = "" # Check word at end of column if current_word: is_valid_word = any( placed['word'] == current_word and placed['row'] == word_start and placed['col'] == c and placed['direction'] == 'vertical' for placed in placed_words ) if not is_valid_word and len(current_word) > 1: violations.append(f"Invalid vertical word '{current_word}' at ({word_start},{c})") # Report results if violations: print(f"โŒ Found {len(violations)} boundary violations:") for violation in violations: print(f" - {violation}") return False else: print(f"โœ… No boundary violations found!") print(f"โœ… All words in grid are properly placed and bounded") return True if __name__ == "__main__": success = asyncio.run(test_boundary_fix()) if success: print(f"\n๐ŸŽ‰ Boundary fix is working correctly!") else: print(f"\n๐Ÿ’ฅ Boundary fix needs more work!")