mirror of
https://github.com/SikongJueluo/Mini-Nav.git
synced 2026-07-13 04:25:32 +08:00
refactor(simulator): extract habitat simulator and visualization modules
This commit is contained in:
124
mini-nav/tests/test_habitat_simulator.py
Normal file
124
mini-nav/tests/test_habitat_simulator.py
Normal file
@@ -0,0 +1,124 @@
|
||||
from types import SimpleNamespace
|
||||
|
||||
import pytest
|
||||
|
||||
from simulator import HabitatSimulatorConfig, create_habitat_simulator
|
||||
|
||||
|
||||
class _FakeSimulatorConfiguration:
|
||||
def __init__(self):
|
||||
self.scene_id = ""
|
||||
self.enable_physics = True
|
||||
|
||||
|
||||
class _FakeAgentConfiguration:
|
||||
def __init__(self):
|
||||
self.sensor_specifications = []
|
||||
self.action_space = {}
|
||||
|
||||
|
||||
class _FakeCameraSensorSpec:
|
||||
def __init__(self):
|
||||
self.uuid = ""
|
||||
self.sensor_type = None
|
||||
self.resolution = []
|
||||
self.position = []
|
||||
|
||||
|
||||
class _FakeActuationSpec:
|
||||
def __init__(self, amount):
|
||||
self.amount = amount
|
||||
|
||||
|
||||
class _FakeActionSpec:
|
||||
def __init__(self, name, actuation):
|
||||
self.name = name
|
||||
self.actuation = actuation
|
||||
|
||||
|
||||
class _FakeConfiguration:
|
||||
def __init__(self, sim_cfg, agent_cfgs):
|
||||
self.sim_cfg = sim_cfg
|
||||
self.agent_cfgs = agent_cfgs
|
||||
|
||||
|
||||
class _FakeSimulator:
|
||||
def __init__(self, cfg):
|
||||
self.cfg = cfg
|
||||
self.initialized_agent_id = None
|
||||
|
||||
def initialize_agent(self, agent_id):
|
||||
self.initialized_agent_id = agent_id
|
||||
return {"agent_id": agent_id}
|
||||
|
||||
|
||||
def _create_fake_habitat_module():
|
||||
return SimpleNamespace(
|
||||
SimulatorConfiguration=_FakeSimulatorConfiguration,
|
||||
CameraSensorSpec=_FakeCameraSensorSpec,
|
||||
SensorType=SimpleNamespace(COLOR="color"),
|
||||
Configuration=_FakeConfiguration,
|
||||
Simulator=_FakeSimulator,
|
||||
agent=SimpleNamespace(
|
||||
AgentConfiguration=_FakeAgentConfiguration,
|
||||
ActionSpec=_FakeActionSpec,
|
||||
ActuationSpec=_FakeActuationSpec,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
def test_create_habitat_simulator_builds_expected_configuration():
|
||||
fake_habitat = _create_fake_habitat_module()
|
||||
config = HabitatSimulatorConfig(
|
||||
scene_path="scene.glb",
|
||||
views_per_room=8,
|
||||
image_size=128,
|
||||
sensor_height=1.25,
|
||||
move_forward_step=0.5,
|
||||
enable_physics=False,
|
||||
sensor_uuid="rgb",
|
||||
agent_id=2,
|
||||
)
|
||||
|
||||
simulator, agent = create_habitat_simulator(config, habitat_sim_module=fake_habitat)
|
||||
|
||||
assert simulator.cfg.sim_cfg.scene_id == "scene.glb"
|
||||
assert simulator.cfg.sim_cfg.enable_physics is False
|
||||
|
||||
created_agent_cfg = simulator.cfg.agent_cfgs[0]
|
||||
sensor = created_agent_cfg.sensor_specifications[0]
|
||||
assert sensor.uuid == "rgb"
|
||||
assert sensor.sensor_type == "color"
|
||||
assert sensor.resolution == [128, 128]
|
||||
assert sensor.position == [0.0, 1.25, 0.0]
|
||||
|
||||
assert created_agent_cfg.action_space["move_forward"].actuation.amount == 0.5
|
||||
assert created_agent_cfg.action_space["turn_left"].actuation.amount == 45.0
|
||||
assert created_agent_cfg.action_space["turn_right"].actuation.amount == 45.0
|
||||
|
||||
assert simulator.initialized_agent_id == 2
|
||||
assert agent == {"agent_id": 2}
|
||||
|
||||
|
||||
def test_create_habitat_simulator_validates_views_per_room():
|
||||
fake_habitat = _create_fake_habitat_module()
|
||||
config = HabitatSimulatorConfig(scene_path="scene.glb", views_per_room=0)
|
||||
|
||||
with pytest.raises(ValueError, match="views_per_room"):
|
||||
create_habitat_simulator(config, habitat_sim_module=fake_habitat)
|
||||
|
||||
|
||||
def test_create_habitat_simulator_validates_image_size():
|
||||
fake_habitat = _create_fake_habitat_module()
|
||||
config = HabitatSimulatorConfig(scene_path="scene.glb", image_size=0)
|
||||
|
||||
with pytest.raises(ValueError, match="image_size"):
|
||||
create_habitat_simulator(config, habitat_sim_module=fake_habitat)
|
||||
|
||||
|
||||
def test_create_habitat_simulator_validates_move_forward_step():
|
||||
fake_habitat = _create_fake_habitat_module()
|
||||
config = HabitatSimulatorConfig(scene_path="scene.glb", move_forward_step=0)
|
||||
|
||||
with pytest.raises(ValueError, match="move_forward_step"):
|
||||
create_habitat_simulator(config, habitat_sim_module=fake_habitat)
|
||||
Reference in New Issue
Block a user