#!/usr/bin/env python3 import requests import json def test_api_crossword(): """Test that the API generates valid crosswords without boundary issues.""" url = "http://localhost:7860/api/generate" data = { "topics": ["animals"], "difficulty": "medium", "useAI": True } print("๐Ÿงช Testing API Crossword Generation") print("=" * 50) try: response = requests.post(url, json=data, timeout=30) if response.status_code != 200: print(f"โŒ API Error: {response.status_code}") print(response.text) return False result = response.json() if 'detail' in result: print(f"โŒ Error: {result['detail']}") return False grid = result['grid'] clues = result['clues'] metadata = result['metadata'] print(f"โœ… Generated crossword with {metadata['wordCount']} words") print(f"Grid size: {len(grid)}x{len(grid[0])}") print(f"AI Generated: {metadata['aiGenerated']}") # Validate boundary issues violations = validate_word_boundaries(grid, clues) if violations: print(f"\nโŒ Found {len(violations)} boundary violations:") for violation in violations: print(f" - {violation}") return False else: print(f"\nโœ… No boundary violations found!") print(f"โœ… All words are properly bounded") # Display sample of the grid print(f"\nSample Grid (first 8 rows):") for i, row in enumerate(grid[:8]): row_str = " ".join(cell if cell != "." else " " for cell in row) print(f"{i:2d} | {row_str}") return True except Exception as e: print(f"โŒ Test failed: {e}") return False def validate_word_boundaries(grid, clues): """Validate that all words in the grid have proper boundaries.""" violations = [] # Create a set of valid word placements from clues valid_words = set() for clue in clues: word = clue['word'] pos = clue['position'] direction = clue['direction'] row, col = pos['row'], pos['col'] if direction == 'across': valid_words.add((word, row, col, 'horizontal')) else: valid_words.add((word, row, col, 'vertical')) # Check all horizontal sequences in grid 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 and len(current_word) > 1: # Check if this is a valid placed word if (current_word, r, word_start, 'horizontal') not in valid_words: violations.append(f"Invalid horizontal word '{current_word}' at ({r},{word_start})") current_word = "" # Check word at end of row if current_word and len(current_word) > 1: if (current_word, r, word_start, 'horizontal') not in valid_words: violations.append(f"Invalid horizontal word '{current_word}' at ({r},{word_start})") # Check all vertical sequences in grid 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 and len(current_word) > 1: # Check if this is a valid placed word if (current_word, word_start, c, 'vertical') not in valid_words: violations.append(f"Invalid vertical word '{current_word}' at ({word_start},{c})") current_word = "" # Check word at end of column if current_word and len(current_word) > 1: if (current_word, word_start, c, 'vertical') not in valid_words: violations.append(f"Invalid vertical word '{current_word}' at ({word_start},{c})") return violations if __name__ == "__main__": success = test_api_crossword() if success: print(f"\n๐ŸŽ‰ All tests passed! The boundary fix is working correctly.") else: print(f"\n๐Ÿ’ฅ Tests failed! The boundary issue still exists.")