"""Tests for MiniGrid observation text rendering.""" from types import SimpleNamespace import numpy as np from MiniGridEnv.env.grid_to_text import grid_to_text def _empty_obs(): grid = np.ones((7, 7, 3), dtype=np.int64) # object type: empty grid[:, :, 1] = 0 # default color red (unused for empties) grid[:, :, 2] = 0 return {"image": grid, "direction": 0, "mission": "go to the red ball"} def test_empty_room_description(): text = grid_to_text(_empty_obs()) assert "Mission: go to the red ball" in text assert "You are facing east." in text assert "Notable objects: none visible." in text def test_object_detection(): obs = _empty_obs() obs["image"][4, 2, 0] = 6 # ball obs["image"][4, 2, 1] = 0 # red text = grid_to_text(obs) assert "red ball" in text def test_door_states(): obs = _empty_obs() obs["image"][5, 3, 0] = 4 # door ahead obs["image"][5, 3, 1] = 2 # blue obs["image"][5, 3, 2] = 1 # closed text = grid_to_text(obs) assert "closed blue door" in text def test_wall_boundaries(): obs = _empty_obs() obs["image"][5, 3, 0] = 2 # wall text = grid_to_text(obs) assert "Directly ahead: a wall." in text def test_carrying_object(): carrying = SimpleNamespace(type="key", color="blue") text = grid_to_text(_empty_obs(), carrying=carrying) assert "You are carrying: a blue key." in text def test_egocentric_directions(): obs = _empty_obs() obs["image"][1, 3, 0] = 5 # key obs["image"][1, 3, 1] = 1 # green text = grid_to_text(obs) assert "5 steps ahead" in text def test_multiple_objects(): obs = _empty_obs() obs["image"][5, 2, 0] = 5 # key obs["image"][5, 2, 1] = 2 # blue obs["image"][4, 4, 0] = 6 # ball obs["image"][4, 4, 1] = 0 # red obs["image"][5, 3, 0] = 4 # door obs["image"][5, 3, 1] = 4 # yellow obs["image"][5, 3, 2] = 0 # open text = grid_to_text(obs) assert "blue key" in text assert "red ball" in text assert "open yellow door" in text def test_unseen_cells_ignored(): obs = _empty_obs() obs["image"][1, 1, 0] = 0 # unseen text = grid_to_text(obs) assert "Notable objects: none visible." in text