import pytest from src.core import Food def test_food_initialization(): """Test food initialization""" food = Food(20) assert food.block_size == 20 assert food.color == (255, 0, 0) # Default red color assert food.position == (0, 0) # Default position def test_food_spawn(): """Test food spawning mechanics""" food = Food(20) width, height = 800, 600 snake_body = [(100, 100), (120, 100)] # Mock snake body # Test spawning food.spawn(width, height, snake_body) # Check if food is within bounds assert 0 <= food.position[0] < width assert 0 <= food.position[1] < height # Check if position aligns with grid assert food.position[0] % food.block_size == 0 assert food.position[1] % food.block_size == 0 # Check if food doesn't spawn on snake assert food.position not in snake_body def test_food_collision(): """Test food collision detection""" food = Food(20) food.position = (100, 100) # Test collision assert food.check_collision((100, 100)) == True # Test no collision assert food.check_collision((120, 120)) == False def test_food_spawn_full_grid(): """Test food spawning when grid is mostly occupied""" food = Food(20) width, height = 60, 60 # Small grid # Create a snake that occupies all positions except (40, 40) all_positions = [(x, y) for x in range(0, 60, 20) for y in range(0, 60, 20)] free_spot = (40, 40) snake_body = [pos for pos in all_positions if pos != free_spot] # Verify our test setup is correct assert len(snake_body) == len(all_positions) - 1 assert free_spot not in snake_body # Spawn food food.spawn(width, height, snake_body) # Food must spawn in the only free spot assert food.position == free_spot