add pytest
This commit is contained in:
@@ -1,54 +1,48 @@
|
||||
import random
|
||||
import asyncio
|
||||
import pytest
|
||||
from unittest.mock import patch, MagicMock
|
||||
|
||||
from src.sim.simulator import Simulator
|
||||
from src.classes.avatar import Avatar, Gender
|
||||
from src.classes.calendar import Month, Year, MonthStamp, create_month_stamp
|
||||
from src.classes.world import World
|
||||
from src.classes.map import Map
|
||||
from src.classes.action.move_to_direction import MoveToDirection
|
||||
from src.classes.tile import TileType
|
||||
from src.classes.action import Move
|
||||
from src.classes.name import get_random_name
|
||||
from src.classes.age import Age
|
||||
from src.classes.cultivation import Realm
|
||||
from src.classes.action_runtime import ActionInstance
|
||||
|
||||
def test_simulator_step_moves_avatar_and_sets_tile(base_world, dummy_avatar):
|
||||
# Set initial position
|
||||
dummy_avatar.pos_x = 1
|
||||
dummy_avatar.pos_y = 1
|
||||
# Ensure tile is updated to initial position (fixture puts it at 0,0)
|
||||
dummy_avatar.tile = base_world.map.get_tile(1, 1)
|
||||
|
||||
def test_simulator_step_moves_avatar_and_sets_tile():
|
||||
# 固定随机种子,确保决定的移动是可预测的
|
||||
random.seed(0)
|
||||
sim = Simulator(base_world)
|
||||
base_world.avatar_manager.avatars[dummy_avatar.id] = dummy_avatar
|
||||
|
||||
# 构建 3x3 地图并填充地块
|
||||
game_map = Map(width=3, height=3)
|
||||
for x in range(3):
|
||||
for y in range(3):
|
||||
game_map.create_tile(x, y, TileType.PLAIN)
|
||||
# Manually assign a MoveToDirection action to avoid relying on LLM
|
||||
action = MoveToDirection(dummy_avatar, base_world)
|
||||
# "East" means x + 1
|
||||
direction = "East"
|
||||
action.start(direction=direction) # Initialize start_monthstamp etc.
|
||||
|
||||
# Wrap in ActionInstance
|
||||
dummy_avatar.current_action = ActionInstance(action=action, params={"direction": direction})
|
||||
|
||||
world = World(map=game_map, month_stamp=create_month_stamp(Year(1), Month.JANUARY))
|
||||
# Mock LLM to avoid external calls or errors
|
||||
with patch("src.sim.simulator.llm_ai") as mock_ai:
|
||||
mock_ai.decide = MagicMock(return_value={})
|
||||
|
||||
print(f"DEBUG: Before step: pos_x={dummy_avatar.pos_x}")
|
||||
# Run step synchronously
|
||||
asyncio.run(sim.step())
|
||||
print(f"DEBUG: After step: pos_x={dummy_avatar.pos_x}")
|
||||
print(f"DEBUG: move_step_length={getattr(dummy_avatar, 'move_step_length', 'Not set')}")
|
||||
print(f"DEBUG: effects={dummy_avatar.effects}")
|
||||
|
||||
# 将角色放在地图中心,避免越界
|
||||
avatar = Avatar(
|
||||
world=world,
|
||||
name=get_random_name(Gender.MALE),
|
||||
id="1",
|
||||
birth_month_stamp=create_month_stamp(Year(2000), Month.JANUARY),
|
||||
age=Age(20, Realm.Qi_Refinement),
|
||||
gender=Gender.MALE,
|
||||
pos_x=1,
|
||||
pos_y=1,
|
||||
)
|
||||
|
||||
|
||||
sim = Simulator(world)
|
||||
world.avatar_manager.avatars["1"] = avatar
|
||||
|
||||
# 执行一步模拟
|
||||
sim.step()
|
||||
|
||||
# 断言位置在边界内
|
||||
assert 0 <= avatar.pos_x < game_map.width
|
||||
assert 0 <= avatar.pos_y < game_map.height
|
||||
|
||||
# 断言 tile 已正确设置且与位置一致
|
||||
assert avatar.tile is not None
|
||||
assert avatar.tile.x == avatar.pos_x
|
||||
assert avatar.tile.y == avatar.pos_y
|
||||
# Assert moved East (x increased by move_step_length)
|
||||
# Current move step for Qi Refinement is 2
|
||||
assert dummy_avatar.pos_x == 3
|
||||
assert dummy_avatar.pos_y == 1
|
||||
|
||||
# Assert tile is updated
|
||||
assert dummy_avatar.tile is not None
|
||||
assert dummy_avatar.tile.x == 3
|
||||
assert dummy_avatar.tile.y == 1
|
||||
|
||||
Reference in New Issue
Block a user