import pytest import random from world.world import World, Position, Rotation from world.objects import FoodObject, DefaultCell from tests.benchmarking import HeadlessSimulationBenchmark # Hardcoded simulation parameters (copied from config/constants.py) CELL_SIZE = 20 GRID_WIDTH = 30 GRID_HEIGHT = 25 FOOD_OBJECTS_COUNT = 500 RANDOM_SEED = 12345 def _setup_world(seed=RANDOM_SEED): world = World(CELL_SIZE, (CELL_SIZE * GRID_WIDTH, CELL_SIZE * GRID_HEIGHT)) random.seed(seed) half_width = GRID_WIDTH * CELL_SIZE // 2 half_height = GRID_HEIGHT * CELL_SIZE // 2 for _ in range(FOOD_OBJECTS_COUNT): x = random.randint(-half_width, half_width) y = random.randint(-half_height, half_height) world.add_object(FoodObject(Position(x=x, y=y))) for _ in range(300): new_cell = DefaultCell( Position(x=random.randint(-half_width, half_width), y=random.randint(-half_height, half_height)), Rotation(angle=0) ) new_cell.behavioral_model = new_cell.behavioral_model.mutate(3) world.add_object(new_cell) return world def test_simulation_determinism(): bench1 = HeadlessSimulationBenchmark(lambda seed: _setup_world(seed), random_seed=RANDOM_SEED) bench2 = HeadlessSimulationBenchmark(lambda seed: _setup_world(seed), random_seed=RANDOM_SEED) bench1.start(ticks=100) bench2.start(ticks=100) hash1 = bench1.get_simulation_hash() hash2 = bench2.get_simulation_hash() assert hash1 == hash2, f"Simulation hashes differ: {hash1} != {hash2}" def test_simulation_benchmark(): bench = HeadlessSimulationBenchmark(lambda seed: _setup_world(seed), random_seed=RANDOM_SEED+1) tick_count = 100 bench.start(ticks=tick_count) summary = bench.get_summary() print(f"{tick_count} ticks took {summary.get('ticks_elapsed_time', 0):.4f} seconds, TPS avg: {summary['tps_avg']:.2f}, stddev: {summary['tps_stddev']:.2f}") assert summary['tps_avg'] > 0, "Average TPS should be greater than zero" assert summary['ticks_elapsed_time'] > 0, "Elapsed time should be greater than zero"