|
|
|
|
|
""" |
|
|
Simple debug test for crossword generator index errors. |
|
|
""" |
|
|
|
|
|
import asyncio |
|
|
import sys |
|
|
import logging |
|
|
from pathlib import Path |
|
|
|
|
|
|
|
|
project_root = Path(__file__).parent |
|
|
sys.path.insert(0, str(project_root)) |
|
|
|
|
|
from src.services.crossword_generator_fixed import CrosswordGeneratorFixed |
|
|
|
|
|
|
|
|
logging.basicConfig(level=logging.DEBUG) |
|
|
logger = logging.getLogger(__name__) |
|
|
|
|
|
async def test_with_static_words(): |
|
|
"""Test generator with static word lists.""" |
|
|
|
|
|
|
|
|
generator = CrosswordGeneratorFixed(vector_service=None) |
|
|
|
|
|
|
|
|
test_words = [ |
|
|
{"word": "CAT", "clue": "Feline pet"}, |
|
|
{"word": "DOG", "clue": "Man's best friend"}, |
|
|
{"word": "BIRD", "clue": "Flying animal"}, |
|
|
{"word": "FISH", "clue": "Aquatic animal"}, |
|
|
{"word": "ELEPHANT", "clue": "Large mammal"}, |
|
|
{"word": "TIGER", "clue": "Striped cat"}, |
|
|
{"word": "HORSE", "clue": "Riding animal"}, |
|
|
{"word": "BEAR", "clue": "Large carnivore"} |
|
|
] |
|
|
|
|
|
print(f"🧪 Testing crossword generation with {len(test_words)} words...") |
|
|
|
|
|
try: |
|
|
|
|
|
for attempt in range(10): |
|
|
print(f"\n🔬 Attempt {attempt + 1}/10") |
|
|
|
|
|
|
|
|
import random |
|
|
random.shuffle(test_words) |
|
|
|
|
|
|
|
|
generator._select_words = lambda topics, difficulty, use_ai: test_words |
|
|
|
|
|
result = await generator.generate_puzzle(["animals"], "medium", use_ai=False) |
|
|
|
|
|
if result: |
|
|
grid_size = len(result['grid']) |
|
|
word_count = len(result['clues']) |
|
|
print(f"✅ Attempt {attempt + 1} succeeded: {grid_size}x{grid_size} grid, {word_count} words") |
|
|
else: |
|
|
print(f"⚠️ Attempt {attempt + 1} returned None") |
|
|
|
|
|
except IndexError as e: |
|
|
print(f"❌ INDEX ERROR caught on attempt {attempt + 1}: {e}") |
|
|
import traceback |
|
|
traceback.print_exc() |
|
|
return False |
|
|
except Exception as e: |
|
|
print(f"❌ Other error on attempt {attempt + 1}: {e}") |
|
|
import traceback |
|
|
traceback.print_exc() |
|
|
return False |
|
|
|
|
|
print(f"\n✅ All 10 attempts completed successfully!") |
|
|
return True |
|
|
|
|
|
async def test_grid_placement_directly(): |
|
|
"""Test grid placement functions directly with problematic data.""" |
|
|
|
|
|
generator = CrosswordGeneratorFixed(vector_service=None) |
|
|
|
|
|
|
|
|
test_cases = [ |
|
|
{ |
|
|
"words": ["A", "I"], |
|
|
"description": "Very short words" |
|
|
}, |
|
|
{ |
|
|
"words": ["VERYLONGWORDTHATMIGHTCAUSEISSUES", "SHORT"], |
|
|
"description": "Very long word with short word" |
|
|
}, |
|
|
{ |
|
|
"words": ["ABCDEFGHIJKLMNOP", "QRSTUVWXYZ"], |
|
|
"description": "Two long words" |
|
|
}, |
|
|
{ |
|
|
"words": ["TEST", "SETS", "NETS", "PETS"], |
|
|
"description": "Similar words with same endings" |
|
|
} |
|
|
] |
|
|
|
|
|
for i, test_case in enumerate(test_cases): |
|
|
print(f"\n🔬 Grid test {i+1}: {test_case['description']}") |
|
|
|
|
|
try: |
|
|
word_list = test_case["words"] |
|
|
word_objs = [{"word": w, "clue": f"Clue for {w}"} for w in word_list] |
|
|
|
|
|
result = generator._create_grid(word_objs) |
|
|
|
|
|
if result: |
|
|
grid_size = len(result['grid']) |
|
|
word_count = len(result['placed_words']) |
|
|
print(f"✅ Grid test {i+1} succeeded: {grid_size}x{grid_size} grid, {word_count} words") |
|
|
else: |
|
|
print(f"⚠️ Grid test {i+1} returned None") |
|
|
|
|
|
except IndexError as e: |
|
|
print(f"❌ INDEX ERROR in grid test {i+1}: {e}") |
|
|
import traceback |
|
|
traceback.print_exc() |
|
|
return False |
|
|
except Exception as e: |
|
|
print(f"❌ Other error in grid test {i+1}: {e}") |
|
|
import traceback |
|
|
traceback.print_exc() |
|
|
return False |
|
|
|
|
|
return True |
|
|
|
|
|
if __name__ == "__main__": |
|
|
print("🧪 Starting debug tests for crossword generator...") |
|
|
|
|
|
async def run_tests(): |
|
|
success1 = await test_with_static_words() |
|
|
success2 = await test_grid_placement_directly() |
|
|
|
|
|
if success1 and success2: |
|
|
print("\n🎉 All debug tests passed! No index errors detected.") |
|
|
else: |
|
|
print("\n❌ Some debug tests failed.") |
|
|
|
|
|
asyncio.run(run_tests()) |