File size: 4,727 Bytes
38c016b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
#!/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.") |