|
|
|
|
|
|
|
|
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']}") |
|
|
|
|
|
|
|
|
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") |
|
|
|
|
|
|
|
|
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 = [] |
|
|
|
|
|
|
|
|
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')) |
|
|
|
|
|
|
|
|
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: |
|
|
|
|
|
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 = "" |
|
|
|
|
|
|
|
|
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})") |
|
|
|
|
|
|
|
|
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: |
|
|
|
|
|
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 = "" |
|
|
|
|
|
|
|
|
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.") |